blob: be8a4bb984562f8234de1e6178d8752cfa3ba324 [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
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 Petkov11b8eb32010-05-18 11:00:59 -070016
Darin Petkov11b8eb32010-05-18 11:00:59 -070017
Ben Chan51bf92a2014-09-05 08:21:06 -070018#include <base/files/file_util.h>
Bertrand SIMONNET12531862015-08-31 11:11:57 -070019#include <base/files/scoped_temp_dir.h>
Ken Mixterb2f17092011-07-22 14:59:51 -070020#include <gmock/gmock.h>
Darin Petkov11b8eb32010-05-18 11:00:59 -070021#include <gtest/gtest.h>
22
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -070023#include "metrics/c_metrics_library.h"
24#include "metrics/metrics_library.h"
Sam Leffler10b301d2010-06-17 14:22:43 -070025
Ken Mixter4c5daa42010-08-26 18:35:06 -070026
Darin Petkov11b8eb32010-05-18 11:00:59 -070027class MetricsLibraryTest : public testing::Test {
28 protected:
29 virtual void SetUp() {
Bertrand SIMONNET12531862015-08-31 11:11:57 -070030 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070031 lib_.InitForTest(temp_dir_.path());
32 EXPECT_EQ(0, WriteFile(lib_.uma_events_file_, "", 0));
Ken Mixter4c5daa42010-08-26 18:35:06 -070033 // Defeat metrics enabled caching between tests.
34 lib_.cached_enabled_time_ = 0;
Darin Petkov11b8eb32010-05-18 11:00:59 -070035 }
36
Bertrand SIMONNET12531862015-08-31 11:11:57 -070037 void SetMetricsConsent(bool enabled) {
38 if (enabled) {
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070039 ASSERT_EQ(base::WriteFile(lib_.consent_file_, "", 0), 0);
Bertrand SIMONNET12531862015-08-31 11:11:57 -070040 } else {
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070041 ASSERT_TRUE(base::DeleteFile(lib_.consent_file_, false));
Bertrand SIMONNET12531862015-08-31 11:11:57 -070042 }
Darin Petkov11b8eb32010-05-18 11:00:59 -070043 }
44
Ken Mixter4c5daa42010-08-26 18:35:06 -070045 void VerifyEnabledCacheHit(bool to_value);
46 void VerifyEnabledCacheEviction(bool to_value);
47
Darin Petkov11b8eb32010-05-18 11:00:59 -070048 MetricsLibrary lib_;
Bertrand SIMONNET12531862015-08-31 11:11:57 -070049 base::ScopedTempDir temp_dir_;
Darin Petkov11b8eb32010-05-18 11:00:59 -070050};
51
Ken Mixter4c5daa42010-08-26 18:35:06 -070052TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) {
Bertrand SIMONNET12531862015-08-31 11:11:57 -070053 SetMetricsConsent(false);
Ken Mixter4c5daa42010-08-26 18:35:06 -070054 EXPECT_FALSE(lib_.AreMetricsEnabled());
55}
56
57TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) {
Bertrand SIMONNET12531862015-08-31 11:11:57 -070058 SetMetricsConsent(true);
Ken Mixter4c5daa42010-08-26 18:35:06 -070059 EXPECT_TRUE(lib_.AreMetricsEnabled());
60}
61
62void 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 SIMONNET12531862015-08-31 11:11:57 -070067 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 Mixter4c5daa42010-08-26 18:35:06 -070073 return;
74 }
75 ADD_FAILURE() << "Did not see evidence of caching";
76}
77
78void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) {
79 lib_.cached_enabled_time_ = 0;
Bertrand SIMONNET12531862015-08-31 11:11:57 -070080 SetMetricsConsent(!to_value);
Ken Mixter4c5daa42010-08-26 18:35:06 -070081 ASSERT_EQ(!to_value, lib_.AreMetricsEnabled());
Bertrand SIMONNET12531862015-08-31 11:11:57 -070082
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 Mixter4c5daa42010-08-26 18:35:06 -070086 --lib_.cached_enabled_time_;
87 ASSERT_EQ(to_value, lib_.AreMetricsEnabled());
88}
89
90TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) {
91 VerifyEnabledCacheHit(false);
92 VerifyEnabledCacheHit(true);
93 VerifyEnabledCacheEviction(false);
94 VerifyEnabledCacheEviction(true);
95}
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -070096
97TEST_F(MetricsLibraryTest, AreMetricsEnabledNoCaching) {
98 // disable caching.
99 lib_.use_caching_ = false;
100
101 // Checking the consent repeatedly should return the right result.
102 for (int i=0; i<100; ++i) {
103 SetMetricsConsent(true);
104 ASSERT_EQ(true, lib_.AreMetricsEnabled());
105 SetMetricsConsent(false);
106 ASSERT_EQ(false, lib_.AreMetricsEnabled());
107 }
108}