OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/metrics/metrics_state_manager.h" | 5 #include "components/metrics/metrics_state_manager.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "chrome/common/pref_names.h" | |
15 #include "components/metrics/metrics_pref_names.h" | 13 #include "components/metrics/metrics_pref_names.h" |
| 14 #include "components/metrics/metrics_switches.h" |
16 #include "components/variations/caching_permuted_entropy_provider.h" | 15 #include "components/variations/caching_permuted_entropy_provider.h" |
17 #include "components/variations/pref_names.h" | 16 #include "components/variations/pref_names.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
19 | 18 |
20 namespace metrics { | 19 namespace metrics { |
21 | 20 |
22 class MetricsStateManagerTest : public testing::Test { | 21 class MetricsStateManagerTest : public testing::Test { |
23 public: | 22 public: |
24 MetricsStateManagerTest() : is_metrics_reporting_enabled_(false) { | 23 MetricsStateManagerTest() : is_metrics_reporting_enabled_(false) { |
25 MetricsStateManager::RegisterPrefs(prefs_.registry()); | 24 MetricsStateManager::RegisterPrefs(prefs_.registry()); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 // Now, set it to 0 and ensure it doesn't get reset. | 90 // Now, set it to 0 and ensure it doesn't get reset. |
92 state_manager->low_entropy_source_ = 0; | 91 state_manager->low_entropy_source_ = 0; |
93 EXPECT_EQ(0, state_manager->GetLowEntropySource()); | 92 EXPECT_EQ(0, state_manager->GetLowEntropySource()); |
94 // Call it another time, just to make sure. | 93 // Call it another time, just to make sure. |
95 EXPECT_EQ(0, state_manager->GetLowEntropySource()); | 94 EXPECT_EQ(0, state_manager->GetLowEntropySource()); |
96 } | 95 } |
97 | 96 |
98 TEST_F(MetricsStateManagerTest, | 97 TEST_F(MetricsStateManagerTest, |
99 PermutedEntropyCacheClearedWhenLowEntropyReset) { | 98 PermutedEntropyCacheClearedWhenLowEntropyReset) { |
100 const PrefService::Preference* low_entropy_pref = | 99 const PrefService::Preference* low_entropy_pref = |
101 prefs_.FindPreference(::prefs::kMetricsLowEntropySource); | 100 prefs_.FindPreference(prefs::kMetricsLowEntropySource); |
102 const char* kCachePrefName = prefs::kVariationsPermutedEntropyCache; | 101 const char* kCachePrefName = prefs::kVariationsPermutedEntropyCache; |
103 int low_entropy_value = -1; | 102 int low_entropy_value = -1; |
104 | 103 |
105 // First, generate an initial low entropy source value. | 104 // First, generate an initial low entropy source value. |
106 { | 105 { |
107 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); | 106 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); |
108 | 107 |
109 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 108 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
110 state_manager->GetLowEntropySource(); | 109 state_manager->GetLowEntropySource(); |
111 | 110 |
112 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); | 111 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); |
113 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); | 112 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); |
114 } | 113 } |
115 | 114 |
116 // Now, set a dummy value in the permuted entropy cache pref and verify that | 115 // Now, set a dummy value in the permuted entropy cache pref and verify that |
117 // another call to GetLowEntropySource() doesn't clobber it when | 116 // another call to GetLowEntropySource() doesn't clobber it when |
118 // --reset-variation-state wasn't specified. | 117 // --reset-variation-state wasn't specified. |
119 { | 118 { |
120 prefs_.SetString(kCachePrefName, "test"); | 119 prefs_.SetString(kCachePrefName, "test"); |
121 | 120 |
122 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 121 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
123 state_manager->GetLowEntropySource(); | 122 state_manager->GetLowEntropySource(); |
124 | 123 |
125 EXPECT_EQ("test", prefs_.GetString(kCachePrefName)); | 124 EXPECT_EQ("test", prefs_.GetString(kCachePrefName)); |
126 EXPECT_EQ(low_entropy_value, | 125 EXPECT_EQ(low_entropy_value, |
127 prefs_.GetInteger(::prefs::kMetricsLowEntropySource)); | 126 prefs_.GetInteger(prefs::kMetricsLowEntropySource)); |
128 } | 127 } |
129 | 128 |
130 // Verify that the cache does get reset if --reset-variations-state is passed. | 129 // Verify that the cache does get reset if --reset-variations-state is passed. |
131 { | 130 { |
132 CommandLine::ForCurrentProcess()->AppendSwitch( | 131 CommandLine::ForCurrentProcess()->AppendSwitch( |
133 switches::kResetVariationState); | 132 switches::kResetVariationState); |
134 | 133 |
135 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 134 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
136 state_manager->GetLowEntropySource(); | 135 state_manager->GetLowEntropySource(); |
137 | 136 |
138 EXPECT_TRUE(prefs_.GetString(kCachePrefName).empty()); | 137 EXPECT_TRUE(prefs_.GetString(kCachePrefName).empty()); |
139 } | 138 } |
140 } | 139 } |
141 | 140 |
142 // Check that setting the kMetricsResetIds pref to true causes the client id to | 141 // Check that setting the kMetricsResetIds pref to true causes the client id to |
143 // be reset. We do not check that the low entropy source is reset because we | 142 // be reset. We do not check that the low entropy source is reset because we |
144 // cannot ensure that metrics state manager won't generate the same id again. | 143 // cannot ensure that metrics state manager won't generate the same id again. |
145 TEST_F(MetricsStateManagerTest, ResetMetricsIDs) { | 144 TEST_F(MetricsStateManagerTest, ResetMetricsIDs) { |
146 // Set an initial client id in prefs. It should not be possible for the | 145 // Set an initial client id in prefs. It should not be possible for the |
147 // metrics state manager to generate this id randomly. | 146 // metrics state manager to generate this id randomly. |
148 const std::string kInitialClientId = "initial client id"; | 147 const std::string kInitialClientId = "initial client id"; |
149 prefs_.SetString(::prefs::kMetricsClientID, kInitialClientId); | 148 prefs_.SetString(prefs::kMetricsClientID, kInitialClientId); |
150 | 149 |
151 // Make sure the initial client id isn't reset by the metrics state manager. | 150 // Make sure the initial client id isn't reset by the metrics state manager. |
152 { | 151 { |
153 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 152 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
154 state_manager->ForceClientIdCreation(); | 153 state_manager->ForceClientIdCreation(); |
155 EXPECT_EQ(kInitialClientId, state_manager->client_id()); | 154 EXPECT_EQ(kInitialClientId, state_manager->client_id()); |
156 } | 155 } |
157 | 156 |
158 // Set the reset pref to cause the IDs to be reset. | 157 // Set the reset pref to cause the IDs to be reset. |
159 prefs_.SetBoolean(prefs::kMetricsResetIds, true); | 158 prefs_.SetBoolean(prefs::kMetricsResetIds, true); |
160 | 159 |
161 // Cause the actual reset to happen. | 160 // Cause the actual reset to happen. |
162 { | 161 { |
163 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 162 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
164 state_manager->ForceClientIdCreation(); | 163 state_manager->ForceClientIdCreation(); |
165 EXPECT_NE(kInitialClientId, state_manager->client_id()); | 164 EXPECT_NE(kInitialClientId, state_manager->client_id()); |
166 | 165 |
167 state_manager->GetLowEntropySource(); | 166 state_manager->GetLowEntropySource(); |
168 | 167 |
169 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); | 168 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); |
170 } | 169 } |
171 | 170 |
172 EXPECT_NE(kInitialClientId, prefs_.GetString(::prefs::kMetricsClientID)); | 171 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); |
173 } | 172 } |
174 | 173 |
175 } // namespace metrics | 174 } // namespace metrics |
OLD | NEW |