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()); |
| 32 | EXPECT_EQ(0, WriteFile(lib_.uma_events_file_, "", 0)); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 33 | // Defeat metrics enabled caching between tests. |
| 34 | lib_.cached_enabled_time_ = 0; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 37 | void SetMetricsConsent(bool enabled) { |
| 38 | if (enabled) { |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 39 | ASSERT_EQ(base::WriteFile(lib_.consent_file_, "", 0), 0); |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 40 | } else { |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 41 | ASSERT_TRUE(base::DeleteFile(lib_.consent_file_, false)); |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 42 | } |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 45 | void VerifyEnabledCacheHit(bool to_value); |
| 46 | void VerifyEnabledCacheEviction(bool to_value); |
| 47 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 48 | MetricsLibrary lib_; |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 49 | base::ScopedTempDir temp_dir_; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 52 | TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) { |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 53 | SetMetricsConsent(false); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 54 | EXPECT_FALSE(lib_.AreMetricsEnabled()); |
| 55 | } |
| 56 | |
| 57 | TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) { |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 58 | SetMetricsConsent(true); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 59 | EXPECT_TRUE(lib_.AreMetricsEnabled()); |
| 60 | } |
| 61 | |
| 62 | void MetricsLibraryTest::VerifyEnabledCacheHit(bool to_value) { |
| 63 | // We might step from one second to the next one time, but not 100 |
| 64 | // times in a row. |
| 65 | for (int i = 0; i < 100; ++i) { |
| 66 | lib_.cached_enabled_time_ = 0; |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 67 | SetMetricsConsent(to_value); |
| 68 | lib_.AreMetricsEnabled(); |
| 69 | // If we check the metrics status twice in a row, we use the cached value |
| 70 | // the second time. |
| 71 | SetMetricsConsent(!to_value); |
| 72 | if (lib_.AreMetricsEnabled() == to_value) |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | ADD_FAILURE() << "Did not see evidence of caching"; |
| 76 | } |
| 77 | |
| 78 | void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) { |
| 79 | lib_.cached_enabled_time_ = 0; |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 80 | SetMetricsConsent(!to_value); |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 81 | ASSERT_EQ(!to_value, lib_.AreMetricsEnabled()); |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 82 | |
| 83 | SetMetricsConsent(to_value); |
| 84 | // Sleep one second (or cheat to be faster) and check that we are not using |
| 85 | // the cached value. |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 86 | --lib_.cached_enabled_time_; |
| 87 | ASSERT_EQ(to_value, lib_.AreMetricsEnabled()); |
| 88 | } |
| 89 | |
| 90 | TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) { |
| 91 | VerifyEnabledCacheHit(false); |
| 92 | VerifyEnabledCacheHit(true); |
| 93 | VerifyEnabledCacheEviction(false); |
| 94 | VerifyEnabledCacheEviction(true); |
| 95 | } |