Home | History | Annotate | Download | only in metricsd
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 #include <base/files/file_util.h>
     19 #include <base/files/scoped_temp_dir.h>
     20 #include <gmock/gmock.h>
     21 #include <gtest/gtest.h>
     22 
     23 #include "metrics/c_metrics_library.h"
     24 #include "metrics/metrics_library.h"
     25 
     26 
     27 class MetricsLibraryTest : public testing::Test {
     28  protected:
     29   virtual void SetUp() {
     30     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     31     lib_.InitForTest(temp_dir_.path());
     32     // Defeat metrics enabled caching between tests.
     33     lib_.cached_enabled_time_ = 0;
     34   }
     35 
     36   void SetMetricsConsent(bool enabled) {
     37     if (enabled) {
     38       ASSERT_EQ(base::WriteFile(lib_.consent_file_, "", 0), 0);
     39     } else {
     40       ASSERT_TRUE(base::DeleteFile(lib_.consent_file_, false));
     41     }
     42   }
     43 
     44   void VerifyEnabledCacheHit(bool to_value);
     45   void VerifyEnabledCacheEviction(bool to_value);
     46 
     47   MetricsLibrary lib_;
     48   base::ScopedTempDir temp_dir_;
     49 };
     50 
     51 TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) {
     52   SetMetricsConsent(false);
     53   EXPECT_FALSE(lib_.AreMetricsEnabled());
     54 }
     55 
     56 TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) {
     57   SetMetricsConsent(true);
     58   EXPECT_TRUE(lib_.AreMetricsEnabled());
     59 }
     60 
     61 void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) {
     62   // We might step from one second to the next one time, but not 100
     63   // times in a row.
     64   for (int i = 0; i < 100; ++i) {
     65     lib_.cached_enabled_time_ = 0;
     66     SetMetricsConsent(to_value);
     67     lib_.AreMetricsEnabled();
     68     // If we check the metrics status twice in a row, we use the cached value
     69     // the second time.
     70     SetMetricsConsent(!to_value);
     71     if (lib_.AreMetricsEnabled() == to_value)
     72       return;
     73   }
     74   ADD_FAILURE() << "Did not see evidence of caching";
     75 }
     76 
     77 void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) {
     78   lib_.cached_enabled_time_ = 0;
     79   SetMetricsConsent(!to_value);
     80   ASSERT_EQ(!to_value, lib_.AreMetricsEnabled());
     81 
     82   SetMetricsConsent(to_value);
     83   // Sleep one second (or cheat to be faster) and check that we are not using
     84   // the cached value.
     85   --lib_.cached_enabled_time_;
     86   ASSERT_EQ(to_value, lib_.AreMetricsEnabled());
     87 }
     88 
     89 TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) {
     90   VerifyEnabledCacheHit(false);
     91   VerifyEnabledCacheHit(true);
     92   VerifyEnabledCacheEviction(false);
     93   VerifyEnabledCacheEviction(true);
     94 }
     95 
     96 TEST_F(MetricsLibraryTest, AreMetricsEnabledNoCaching) {
     97   // disable caching.
     98   lib_.use_caching_ = false;
     99 
    100   // Checking the consent repeatedly should return the right result.
    101   for (int i=0; i<100; ++i) {
    102     SetMetricsConsent(true);
    103     ASSERT_EQ(true, lib_.AreMetricsEnabled());
    104     SetMetricsConsent(false);
    105     ASSERT_EQ(false, lib_.AreMetricsEnabled());
    106   }
    107 }
    108