OLD | NEW |
---|---|
1 // Copyright (c) 2012 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_service.h" | 5 #include "chrome/browser/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/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/threading/platform_thread.h" | 11 #include "base/prefs/testing_pref_service.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/metrics/caching_permuted_entropy_provider.h" | |
13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/scoped_testing_local_state.h" | |
15 #include "chrome/test/base/testing_browser_process.h" | |
16 #include "components/variations/metrics_util.h" | |
17 #include "content/public/common/process_type.h" | |
18 #include "content/public/common/webplugininfo.h" | |
19 #include "content/public/test/test_browser_thread_bundle.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "ui/gfx/size.h" | |
22 | 16 |
23 #if defined(OS_CHROMEOS) | 17 namespace metrics { |
24 #include "chrome/browser/metrics/metrics_log_chromeos.h" | |
25 #endif // OS_CHROMEOS | |
26 | |
27 namespace { | |
28 | |
29 class TestMetricsService : public MetricsService { | |
30 public: | |
31 TestMetricsService() {} | |
32 virtual ~TestMetricsService() {} | |
33 | |
34 MetricsLogManager* log_manager() { | |
35 return &log_manager_; | |
36 } | |
37 | |
38 private: | |
39 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); | |
40 }; | |
41 | |
42 #if defined(OS_CHROMEOS) | |
43 class TestMetricsLogChromeOS : public MetricsLogChromeOS { | |
44 public: | |
45 explicit TestMetricsLogChromeOS( | |
46 metrics::ChromeUserMetricsExtension* uma_proto) | |
47 : MetricsLogChromeOS(uma_proto) { | |
48 } | |
49 | |
50 protected: | |
51 // Don't touch bluetooth information, as it won't be correctly initialized. | |
52 virtual void WriteBluetoothProto() OVERRIDE { | |
53 } | |
54 }; | |
55 #endif // OS_CHROMEOS | |
56 | |
57 class TestMetricsLog : public MetricsLog { | |
58 public: | |
59 TestMetricsLog(const std::string& client_id, int session_id) | |
60 : MetricsLog(client_id, session_id, MetricsLog::ONGOING_LOG) { | |
61 #if defined(OS_CHROMEOS) | |
62 metrics_log_chromeos_.reset(new TestMetricsLogChromeOS( | |
63 MetricsLog::uma_proto())); | |
64 #endif // OS_CHROMEOS | |
65 } | |
66 virtual ~TestMetricsLog() {} | |
67 | |
68 private: | |
69 virtual gfx::Size GetScreenSize() const OVERRIDE { | |
70 return gfx::Size(1024, 768); | |
71 } | |
72 | |
73 virtual float GetScreenDeviceScaleFactor() const OVERRIDE { | |
74 return 1.0f; | |
75 } | |
76 | |
77 virtual int GetScreenCount() const OVERRIDE { | |
78 return 1; | |
79 } | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); | |
82 }; | |
83 | |
84 class MetricsServiceTest : public testing::Test { | |
85 public: | |
86 MetricsServiceTest() | |
87 : testing_local_state_(TestingBrowserProcess::GetGlobal()) { | |
88 } | |
89 | |
90 virtual ~MetricsServiceTest() { | |
91 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | |
92 } | |
93 | |
94 PrefService* GetLocalState() { | |
95 return testing_local_state_.Get(); | |
96 } | |
97 | |
98 // Waits until base::TimeTicks::Now() no longer equals |value|. This should | |
99 // take between 1-15ms per the documented resolution of base::TimeTicks. | |
100 void WaitUntilTimeChanges(const base::TimeTicks& value) { | |
101 while (base::TimeTicks::Now() == value) { | |
102 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | |
103 } | |
104 } | |
105 | |
106 // Returns true if there is a synthetic trial in the given vector that matches | |
107 // the given trial name and trial group; returns false otherwise. | |
108 bool HasSyntheticTrial( | |
109 const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials, | |
110 const std::string& trial_name, | |
111 const std::string& trial_group) { | |
112 uint32 trial_name_hash = metrics::HashName(trial_name); | |
113 uint32 trial_group_hash = metrics::HashName(trial_group); | |
114 for (std::vector<chrome_variations::ActiveGroupId>::const_iterator it = | |
115 synthetic_trials.begin(); | |
116 it != synthetic_trials.end(); ++it) { | |
117 if ((*it).name == trial_name_hash && (*it).group == trial_group_hash) | |
118 return true; | |
119 } | |
120 return false; | |
121 } | |
122 | |
123 private: | |
124 content::TestBrowserThreadBundle thread_bundle_; | |
125 ScopedTestingLocalState testing_local_state_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); | |
128 }; | |
129 | |
130 } // namespace | |
131 | 18 |
132 // Ensure the ClientId is formatted as expected. | 19 // Ensure the ClientId is formatted as expected. |
133 TEST_F(MetricsServiceTest, ClientIdCorrectlyFormatted) { | 20 TEST(MetricsStateManagerTest, ClientIdCorrectlyFormatted) { |
134 std::string clientid = MetricsService::GenerateClientID(); | 21 TestingPrefServiceSimple prefs; |
135 EXPECT_EQ(36U, clientid.length()); | 22 MetricsStateManager::RegisterPrefs(prefs.registry()); |
Ilya Sherman
2014/05/02 05:12:38
nit: Perhaps extract this out to a common SetUp()
Alexei Svitkine (slow)
2014/05/02 15:21:46
Done. I put it in the constructor instead since th
| |
136 | 23 |
137 for (size_t i = 0; i < clientid.length(); ++i) { | 24 MetricsStateManager state_manager(&prefs); |
138 char current = clientid[i]; | 25 state_manager.ForceClientIdCreation(); |
26 | |
27 const std::string client_id = state_manager.client_id(); | |
28 EXPECT_EQ(36U, client_id.length()); | |
29 | |
30 for (size_t i = 0; i < client_id.length(); ++i) { | |
31 char current = client_id[i]; | |
139 if (i == 8 || i == 13 || i == 18 || i == 23) | 32 if (i == 8 || i == 13 || i == 18 || i == 23) |
140 EXPECT_EQ('-', current); | 33 EXPECT_EQ('-', current); |
141 else | 34 else |
142 EXPECT_TRUE(isxdigit(current)); | 35 EXPECT_TRUE(isxdigit(current)); |
143 } | 36 } |
144 } | 37 } |
145 | 38 |
146 TEST_F(MetricsServiceTest, IsPluginProcess) { | 39 TEST(MetricsStateManagerTest, EntropySourceUsed_Low) { |
147 EXPECT_TRUE( | 40 TestingPrefServiceSimple prefs; |
148 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PLUGIN)); | 41 MetricsStateManager::RegisterPrefs(prefs.registry()); |
149 EXPECT_TRUE( | 42 |
150 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PPAPI_PLUGIN)); | 43 MetricsStateManager state_manager(&prefs); |
151 EXPECT_FALSE( | 44 state_manager.CreateEntropyProvider(); |
152 MetricsService::IsPluginProcess(content::PROCESS_TYPE_GPU)); | 45 EXPECT_EQ(MetricsStateManager::LAST_ENTROPY_LOW, |
46 state_manager.entropy_source_returned()); | |
153 } | 47 } |
154 | 48 |
155 TEST_F(MetricsServiceTest, LowEntropySource0NotReset) { | 49 TEST(MetricsStateManagerTest, EntropySourceUsed_High) { |
156 MetricsService service; | 50 TestingPrefServiceSimple prefs; |
51 MetricsStateManager::RegisterPrefs(prefs.registry()); | |
52 | |
53 CommandLine::ForCurrentProcess()->AppendSwitch( | |
54 switches::kEnableMetricsReportingForTesting); | |
55 | |
56 MetricsStateManager state_manager(&prefs); | |
57 state_manager.CreateEntropyProvider(); | |
58 EXPECT_EQ(MetricsStateManager::LAST_ENTROPY_HIGH, | |
59 state_manager.entropy_source_returned()); | |
60 } | |
61 | |
62 TEST(MetricsStateManagerTest, LowEntropySource0NotReset) { | |
63 TestingPrefServiceSimple prefs; | |
64 MetricsStateManager::RegisterPrefs(prefs.registry()); | |
65 | |
66 MetricsStateManager state_manager(&prefs); | |
157 | 67 |
158 // Get the low entropy source once, to initialize it. | 68 // Get the low entropy source once, to initialize it. |
159 service.GetLowEntropySource(); | 69 state_manager.GetLowEntropySource(); |
160 | 70 |
161 // Now, set it to 0 and ensure it doesn't get reset. | 71 // Now, set it to 0 and ensure it doesn't get reset. |
162 service.low_entropy_source_ = 0; | 72 state_manager.low_entropy_source_ = 0; |
163 EXPECT_EQ(0, service.GetLowEntropySource()); | 73 EXPECT_EQ(0, state_manager.GetLowEntropySource()); |
164 // Call it another time, just to make sure. | 74 // Call it another time, just to make sure. |
165 EXPECT_EQ(0, service.GetLowEntropySource()); | 75 EXPECT_EQ(0, state_manager.GetLowEntropySource()); |
166 } | 76 } |
167 | 77 |
168 TEST_F(MetricsServiceTest, PermutedEntropyCacheClearedWhenLowEntropyReset) { | 78 TEST(MetricsStateManagerTest, PermutedEntropyCacheClearedWhenLowEntropyReset) { |
79 TestingPrefServiceSimple prefs; | |
80 MetricsStateManager::RegisterPrefs(prefs.registry()); | |
81 | |
169 const PrefService::Preference* low_entropy_pref = | 82 const PrefService::Preference* low_entropy_pref = |
170 GetLocalState()->FindPreference(prefs::kMetricsLowEntropySource); | 83 prefs.FindPreference(prefs::kMetricsLowEntropySource); |
171 const char* kCachePrefName = prefs::kMetricsPermutedEntropyCache; | 84 const char* kCachePrefName = prefs::kMetricsPermutedEntropyCache; |
172 int low_entropy_value = -1; | 85 int low_entropy_value = -1; |
173 | 86 |
174 // First, generate an initial low entropy source value. | 87 // First, generate an initial low entropy source value. |
175 { | 88 { |
176 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); | 89 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); |
177 | 90 |
178 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 91 MetricsStateManager state_manager(&prefs); |
179 MetricsService service; | 92 state_manager.GetLowEntropySource(); |
180 service.GetLowEntropySource(); | |
181 | 93 |
182 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); | 94 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); |
183 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); | 95 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); |
184 } | 96 } |
185 | 97 |
186 // Now, set a dummy value in the permuted entropy cache pref and verify that | 98 // Now, set a dummy value in the permuted entropy cache pref and verify that |
187 // another call to GetLowEntropySource() doesn't clobber it when | 99 // another call to GetLowEntropySource() doesn't clobber it when |
188 // --reset-variation-state wasn't specified. | 100 // --reset-variation-state wasn't specified. |
189 { | 101 { |
190 GetLocalState()->SetString(kCachePrefName, "test"); | 102 prefs.SetString(kCachePrefName, "test"); |
191 | 103 |
192 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 104 MetricsStateManager state_manager(&prefs); |
193 MetricsService service; | 105 state_manager.GetLowEntropySource(); |
194 service.GetLowEntropySource(); | |
195 | 106 |
196 EXPECT_EQ("test", GetLocalState()->GetString(kCachePrefName)); | 107 EXPECT_EQ("test", prefs.GetString(kCachePrefName)); |
197 EXPECT_EQ(low_entropy_value, | 108 EXPECT_EQ(low_entropy_value, |
198 GetLocalState()->GetInteger(prefs::kMetricsLowEntropySource)); | 109 prefs.GetInteger(prefs::kMetricsLowEntropySource)); |
199 } | 110 } |
200 | 111 |
201 // Verify that the cache does get reset if --reset-variations-state is passed. | 112 // Verify that the cache does get reset if --reset-variations-state is passed. |
202 { | 113 { |
203 CommandLine::ForCurrentProcess()->AppendSwitch( | 114 CommandLine::ForCurrentProcess()->AppendSwitch( |
204 switches::kResetVariationState); | 115 switches::kResetVariationState); |
205 | 116 |
206 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 117 MetricsStateManager state_manager(&prefs); |
207 MetricsService service; | 118 state_manager.GetLowEntropySource(); |
208 service.GetLowEntropySource(); | |
209 | 119 |
210 EXPECT_TRUE(GetLocalState()->GetString(kCachePrefName).empty()); | 120 EXPECT_TRUE(prefs.GetString(kCachePrefName).empty()); |
211 } | 121 } |
212 } | 122 } |
213 | 123 |
214 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { | |
215 base::FieldTrialList field_trial_list(NULL); | |
216 base::FieldTrialList::CreateFieldTrial("UMAStability", "SeparateLog"); | |
217 | |
218 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); | |
219 | |
220 TestMetricsService service; | |
221 service.InitializeMetricsRecordingState(MetricsService::REPORTING_ENABLED); | |
222 // No initial stability log should be generated. | |
223 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); | |
224 EXPECT_FALSE(service.log_manager()->has_staged_log()); | |
225 } | |
226 | |
227 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { | |
228 base::FieldTrialList field_trial_list(NULL); | |
229 base::FieldTrialList::CreateFieldTrial("UMAStability", "SeparateLog"); | |
230 | |
231 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly); | |
232 | |
233 // Set up prefs to simulate restarting after a crash. | |
234 | |
235 // Save an existing system profile to prefs, to correspond to what would be | |
236 // saved from a previous session. | |
237 TestMetricsLog log("client", 1); | |
238 log.RecordEnvironment(std::vector<content::WebPluginInfo>(), | |
239 GoogleUpdateMetrics(), | |
240 std::vector<chrome_variations::ActiveGroupId>()); | |
241 | |
242 // Record stability build time and version from previous session, so that | |
243 // stability metrics (including exited cleanly flag) won't be cleared. | |
244 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, | |
245 MetricsLog::GetBuildTime()); | |
246 GetLocalState()->SetString(prefs::kStabilityStatsVersion, | |
247 MetricsLog::GetVersionString()); | |
248 | |
249 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false); | |
250 | |
251 TestMetricsService service; | |
252 service.InitializeMetricsRecordingState(MetricsService::REPORTING_ENABLED); | |
253 | |
254 // The initial stability log should be generated and persisted in unsent logs. | |
255 MetricsLogManager* log_manager = service.log_manager(); | |
256 EXPECT_TRUE(log_manager->has_unsent_logs()); | |
257 EXPECT_FALSE(log_manager->has_staged_log()); | |
258 | |
259 // Stage the log and retrieve it. | |
260 log_manager->StageNextLogForUpload(); | |
261 EXPECT_TRUE(log_manager->has_staged_log()); | |
262 | |
263 metrics::ChromeUserMetricsExtension uma_log; | |
264 EXPECT_TRUE(uma_log.ParseFromString(log_manager->staged_log_text())); | |
265 | |
266 EXPECT_TRUE(uma_log.has_client_id()); | |
267 EXPECT_TRUE(uma_log.has_session_id()); | |
268 EXPECT_TRUE(uma_log.has_system_profile()); | |
269 EXPECT_EQ(0, uma_log.user_action_event_size()); | |
270 EXPECT_EQ(0, uma_log.omnibox_event_size()); | |
271 EXPECT_EQ(0, uma_log.histogram_event_size()); | |
272 EXPECT_EQ(0, uma_log.profiler_event_size()); | |
273 EXPECT_EQ(0, uma_log.perf_data_size()); | |
274 | |
275 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count()); | |
276 } | |
277 | |
278 TEST_F(MetricsServiceTest, RegisterSyntheticTrial) { | |
279 MetricsService service; | |
280 | |
281 // Add two synthetic trials and confirm that they show up in the list. | |
282 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), | |
283 metrics::HashName("Group1")); | |
284 service.RegisterSyntheticFieldTrial(trial1); | |
285 | |
286 SyntheticTrialGroup trial2(metrics::HashName("TestTrial2"), | |
287 metrics::HashName("Group2")); | |
288 service.RegisterSyntheticFieldTrial(trial2); | |
289 // Ensure that time has advanced by at least a tick before proceeding. | |
290 WaitUntilTimeChanges(base::TimeTicks::Now()); | |
291 | |
292 service.log_manager_.BeginLoggingWithLog( | |
293 new MetricsLog("clientID", 1, MetricsLog::INITIAL_STABILITY_LOG)); | |
294 // Save the time when the log was started (it's okay for this to be greater | |
295 // than the time recorded by the above call since it's used to ensure the | |
296 // value changes). | |
297 const base::TimeTicks begin_log_time = base::TimeTicks::Now(); | |
298 | |
299 std::vector<chrome_variations::ActiveGroupId> synthetic_trials; | |
300 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | |
301 EXPECT_EQ(2U, synthetic_trials.size()); | |
302 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group1")); | |
303 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | |
304 | |
305 // Ensure that time has advanced by at least a tick before proceeding. | |
306 WaitUntilTimeChanges(begin_log_time); | |
307 | |
308 // Change the group for the first trial after the log started. | |
309 // TODO(asvitkine): Assumption that this is > than BeginLoggingWithLog() time. | |
310 SyntheticTrialGroup trial3(metrics::HashName("TestTrial1"), | |
311 metrics::HashName("Group2")); | |
312 service.RegisterSyntheticFieldTrial(trial3); | |
313 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | |
314 EXPECT_EQ(1U, synthetic_trials.size()); | |
315 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | |
316 | |
317 // Add a new trial after the log started and confirm that it doesn't show up. | |
318 SyntheticTrialGroup trial4(metrics::HashName("TestTrial3"), | |
319 metrics::HashName("Group3")); | |
320 service.RegisterSyntheticFieldTrial(trial4); | |
321 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | |
322 EXPECT_EQ(1U, synthetic_trials.size()); | |
323 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | |
324 | |
325 // Ensure that time has advanced by at least a tick before proceeding. | |
326 WaitUntilTimeChanges(base::TimeTicks::Now()); | |
327 | |
328 // Start a new log and ensure all three trials appear in it. | |
329 service.log_manager_.FinishCurrentLog(); | |
330 service.log_manager_.BeginLoggingWithLog( | |
331 new MetricsLog("clientID", 1, MetricsLog::ONGOING_LOG)); | |
332 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | |
333 EXPECT_EQ(3U, synthetic_trials.size()); | |
334 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); | |
335 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | |
336 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); | |
337 service.log_manager_.FinishCurrentLog(); | |
338 } | |
339 | |
340 TEST_F(MetricsServiceTest, MetricsReportingEnabled) { | |
341 #if !defined(OS_CHROMEOS) | |
342 GetLocalState()->SetBoolean(prefs::kMetricsReportingEnabled, false); | |
343 EXPECT_FALSE(MetricsServiceHelper::IsMetricsReportingEnabled()); | |
344 GetLocalState()->SetBoolean(prefs::kMetricsReportingEnabled, true); | |
345 EXPECT_TRUE(MetricsServiceHelper::IsMetricsReportingEnabled()); | |
346 GetLocalState()->ClearPref(prefs::kMetricsReportingEnabled); | |
347 EXPECT_FALSE(MetricsServiceHelper::IsMetricsReportingEnabled()); | |
348 #else | |
349 // ChromeOS does not register prefs::kMetricsReportingEnabled and uses | |
350 // device settings for metrics reporting. | |
351 EXPECT_FALSE(MetricsServiceHelper::IsMetricsReportingEnabled()); | |
352 #endif | |
353 } | |
354 | |
355 TEST_F(MetricsServiceTest, CrashReportingEnabled) { | |
356 #if defined(GOOGLE_CHROME_BUILD) | |
357 // ChromeOS has different device settings for crash reporting. | |
358 #if !defined(OS_CHROMEOS) | |
359 #if defined(OS_ANDROID) | |
360 const char* crash_pref = prefs::kCrashReportingEnabled; | |
361 #else | |
362 const char* crash_pref = prefs::kMetricsReportingEnabled; | |
363 #endif | |
364 GetLocalState()->SetBoolean(crash_pref, false); | |
365 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled()); | |
366 GetLocalState()->SetBoolean(crash_pref, true); | |
367 EXPECT_TRUE(MetricsServiceHelper::IsCrashReportingEnabled()); | |
368 GetLocalState()->ClearPref(crash_pref); | |
369 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled()); | |
370 #endif // !defined(OS_CHROMEOS) | |
371 #else // defined(GOOGLE_CHROME_BUILD) | |
372 // Chromium branded browsers never have crash reporting enabled. | |
373 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled()); | |
374 #endif // defined(GOOGLE_CHROME_BUILD) | |
375 } | |
376 | |
377 // Check that setting the kMetricsResetIds pref to true causes the client id to | 124 // Check that setting the kMetricsResetIds pref to true causes the client id to |
378 // be reset. We do not check that the low entropy source is reset because we | 125 // be reset. We do not check that the low entropy source is reset because we |
379 // cannot ensure that metrics service won't generate the same id again. | 126 // cannot ensure that metrics service won't generate the same id again. |
380 TEST_F(MetricsServiceTest, ResetMetricsIDs) { | 127 TEST(MetricsStateManagerTest, ResetMetricsIDs) { |
128 TestingPrefServiceSimple prefs; | |
129 MetricsStateManager::RegisterPrefs(prefs.registry()); | |
130 | |
381 // Set an initial client id in prefs. It should not be possible for the | 131 // Set an initial client id in prefs. It should not be possible for the |
382 // metrics service to generate this id randomly. | 132 // metrics state manager to generate this id randomly. |
383 const std::string kInitialClientId = "initial client id"; | 133 const std::string kInitialClientId = "initial client id"; |
384 GetLocalState()->SetString(prefs::kMetricsClientID, kInitialClientId); | 134 prefs.SetString(prefs::kMetricsClientID, kInitialClientId); |
385 | 135 |
386 // Make sure the initial client id isn't reset by the metrics service. | 136 // Make sure the initial client id isn't reset by the metrics service. |
Ilya Sherman
2014/05/02 05:12:38
nit: "metrics service" -> "metrics state manager"
Alexei Svitkine (slow)
2014/05/02 15:21:46
Done.
| |
387 { | 137 { |
388 MetricsService service; | 138 MetricsStateManager state_manager(&prefs); |
389 service.ForceClientIdCreation(); | 139 state_manager.ForceClientIdCreation(); |
390 EXPECT_TRUE(service.metrics_ids_reset_check_performed_); | 140 EXPECT_EQ(kInitialClientId, state_manager.client_id()); |
391 EXPECT_EQ(kInitialClientId, service.client_id_); | |
392 } | 141 } |
393 | 142 |
394 | |
395 // Set the reset pref to cause the IDs to be reset. | 143 // Set the reset pref to cause the IDs to be reset. |
396 GetLocalState()->SetBoolean(prefs::kMetricsResetIds, true); | 144 prefs.SetBoolean(prefs::kMetricsResetIds, true); |
397 | 145 |
398 // Cause the actual reset to happen. | 146 // Cause the actual reset to happen. |
399 { | 147 { |
400 MetricsService service; | 148 MetricsStateManager state_manager(&prefs); |
401 service.ForceClientIdCreation(); | 149 state_manager.ForceClientIdCreation(); |
402 EXPECT_TRUE(service.metrics_ids_reset_check_performed_); | 150 EXPECT_NE(kInitialClientId, state_manager.client_id()); |
403 EXPECT_NE(kInitialClientId, service.client_id_); | |
404 | 151 |
405 service.GetLowEntropySource(); | 152 state_manager.GetLowEntropySource(); |
406 | 153 |
407 EXPECT_FALSE(GetLocalState()->GetBoolean(prefs::kMetricsResetIds)); | 154 EXPECT_FALSE(prefs.GetBoolean(prefs::kMetricsResetIds)); |
408 } | 155 } |
409 | 156 |
410 std::string new_client_id = | 157 EXPECT_NE(kInitialClientId, prefs.GetString(prefs::kMetricsClientID)); |
411 GetLocalState()->GetString(prefs::kMetricsClientID); | 158 } |
412 | 159 |
413 EXPECT_NE(kInitialClientId, new_client_id); | 160 } // namespace metrics |
414 } | |
OLD | NEW |