Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 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 | */ |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 16 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 17 | |
Ben Chan | 51bf92a | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 18 | #include <base/files/file_util.h> |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 19 | #include <base/files/scoped_temp_dir.h> |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 20 | #include <gmock/gmock.h> |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 23 | #include "metrics/c_metrics_library.h" |
| 24 | #include "metrics/metrics_library.h" |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 25 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 26 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 27 | class MetricsLibraryTest : public testing::Test { |
| 28 | protected: |
| 29 | virtual void SetUp() { |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 30 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 31 | lib_.InitForTest(temp_dir_.path()); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 32 | // Defeat metrics enabled caching between tests. |
| 33 | lib_.cached_enabled_time_ = 0; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 36 | void SetMetricsConsent(bool enabled) { |
| 37 | if (enabled) { |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 38 | ASSERT_EQ(base::WriteFile(lib_.consent_file_, "", 0), 0); |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 39 | } else { |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 40 | ASSERT_TRUE(base::DeleteFile(lib_.consent_file_, false)); |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 41 | } |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 44 | void VerifyEnabledCacheHit(bool to_value); |
| 45 | void VerifyEnabledCacheEviction(bool to_value); |
| 46 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 47 | MetricsLibrary lib_; |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 48 | base::ScopedTempDir temp_dir_; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 51 | TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) { |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 52 | SetMetricsConsent(false); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 53 | EXPECT_FALSE(lib_.AreMetricsEnabled()); |
| 54 | } |
| 55 | |
| 56 | TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) { |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 57 | SetMetricsConsent(true); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 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; |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 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) |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 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; |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 79 | SetMetricsConsent(!to_value); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 80 | ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 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. |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 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 | } |
Bertrand SIMONNET | a5b40d0 | 2015-10-02 16:40:51 -0700 | [diff] [blame] | 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 | } |