blob: 52fcce3a0b8fca71f8c8fd2995932f4618530917 [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());
Ken Mixter4c5daa42010-08-26 18:35:06 -070032 // Defeat metrics enabled caching between tests.
33 lib_.cached_enabled_time_ = 0;
Darin Petkov11b8eb32010-05-18 11:00:59 -070034 }
35
Bertrand SIMONNET12531862015-08-31 11:11:57 -070036 void SetMetricsConsent(bool enabled) {
37 if (enabled) {
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070038 ASSERT_EQ(base::WriteFile(lib_.consent_file_, "", 0), 0);
Bertrand SIMONNET12531862015-08-31 11:11:57 -070039 } else {
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070040 ASSERT_TRUE(base::DeleteFile(lib_.consent_file_, false));
Bertrand SIMONNET12531862015-08-31 11:11:57 -070041 }
Darin Petkov11b8eb32010-05-18 11:00:59 -070042 }
43
Ken Mixter4c5daa42010-08-26 18:35:06 -070044 void VerifyEnabledCacheHit(bool to_value);
45 void VerifyEnabledCacheEviction(bool to_value);
46
Darin Petkov11b8eb32010-05-18 11:00:59 -070047 MetricsLibrary lib_;
Bertrand SIMONNET12531862015-08-31 11:11:57 -070048 base::ScopedTempDir temp_dir_;
Darin Petkov11b8eb32010-05-18 11:00:59 -070049};
50
Ken Mixter4c5daa42010-08-26 18:35:06 -070051TEST_F(MetricsLibraryTest, AreMetricsEnabledFalse) {
Bertrand SIMONNET12531862015-08-31 11:11:57 -070052 SetMetricsConsent(false);
Ken Mixter4c5daa42010-08-26 18:35:06 -070053 EXPECT_FALSE(lib_.AreMetricsEnabled());
54}
55
56TEST_F(MetricsLibraryTest, AreMetricsEnabledTrue) {
Bertrand SIMONNET12531862015-08-31 11:11:57 -070057 SetMetricsConsent(true);
Ken Mixter4c5daa42010-08-26 18:35:06 -070058 EXPECT_TRUE(lib_.AreMetricsEnabled());
59}
60
61void 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 SIMONNET12531862015-08-31 11:11:57 -070066 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 Mixter4c5daa42010-08-26 18:35:06 -070072 return;
73 }
74 ADD_FAILURE() << "Did not see evidence of caching";
75}
76
77void MetricsLibraryTest::VerifyEnabledCacheEviction(bool to_value) {
78 lib_.cached_enabled_time_ = 0;
Bertrand SIMONNET12531862015-08-31 11:11:57 -070079 SetMetricsConsent(!to_value);
Ken Mixter4c5daa42010-08-26 18:35:06 -070080 ASSERT_EQ(!to_value, lib_.AreMetricsEnabled());
Bertrand SIMONNET12531862015-08-31 11:11:57 -070081
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 Mixter4c5daa42010-08-26 18:35:06 -070085 --lib_.cached_enabled_time_;
86 ASSERT_EQ(to_value, lib_.AreMetricsEnabled());
87}
88
89TEST_F(MetricsLibraryTest, AreMetricsEnabledCaching) {
90 VerifyEnabledCacheHit(false);
91 VerifyEnabledCacheHit(true);
92 VerifyEnabledCacheEviction(false);
93 VerifyEnabledCacheEviction(true);
94}
Bertrand SIMONNETa5b40d02015-10-02 16:40:51 -070095
96TEST_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}