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 | 18 |
27 namespace { | 19 class MetricsStateManagerTest : public testing::Test { |
28 | |
29 class TestMetricsService : public MetricsService { | |
30 public: | 20 public: |
31 TestMetricsService() {} | 21 MetricsStateManagerTest() { |
32 virtual ~TestMetricsService() {} | 22 MetricsStateManager::RegisterPrefs(prefs_.registry()); |
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 } | 23 } |
49 | 24 |
50 protected: | 25 protected: |
51 // Don't touch bluetooth information, as it won't be correctly initialized. | 26 TestingPrefServiceSimple prefs_; |
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 | 27 |
68 private: | 28 private: |
69 virtual gfx::Size GetScreenSize() const OVERRIDE { | 29 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest); |
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 }; | 30 }; |
83 | 31 |
84 class MetricsServiceTest : public testing::Test { | 32 // Ensure the ClientId is formatted as expected. |
85 public: | 33 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) { |
86 MetricsServiceTest() | 34 MetricsStateManager state_manager(&prefs_); |
87 : testing_local_state_(TestingBrowserProcess::GetGlobal()) { | 35 state_manager.ForceClientIdCreation(); |
88 } | |
89 | 36 |
90 virtual ~MetricsServiceTest() { | 37 const std::string client_id = state_manager.client_id(); |
91 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 38 EXPECT_EQ(36U, client_id.length()); |
92 } | |
93 | 39 |
94 PrefService* GetLocalState() { | 40 for (size_t i = 0; i < client_id.length(); ++i) { |
95 return testing_local_state_.Get(); | 41 char current = client_id[i]; |
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 | |
132 // Ensure the ClientId is formatted as expected. | |
133 TEST_F(MetricsServiceTest, ClientIdCorrectlyFormatted) { | |
134 std::string clientid = MetricsService::GenerateClientID(); | |
135 EXPECT_EQ(36U, clientid.length()); | |
136 | |
137 for (size_t i = 0; i < clientid.length(); ++i) { | |
138 char current = clientid[i]; | |
139 if (i == 8 || i == 13 || i == 18 || i == 23) | 42 if (i == 8 || i == 13 || i == 18 || i == 23) |
140 EXPECT_EQ('-', current); | 43 EXPECT_EQ('-', current); |
141 else | 44 else |
142 EXPECT_TRUE(isxdigit(current)); | 45 EXPECT_TRUE(isxdigit(current)); |
143 } | 46 } |
144 } | 47 } |
145 | 48 |
146 TEST_F(MetricsServiceTest, IsPluginProcess) { | 49 TEST_F(MetricsStateManagerTest, EntropySourceUsed_Low) { |
147 EXPECT_TRUE( | 50 MetricsStateManager state_manager(&prefs_); |
148 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PLUGIN)); | 51 state_manager.CreateEntropyProvider(); |
149 EXPECT_TRUE( | 52 EXPECT_EQ(MetricsStateManager::ENTROPY_SOURCE_LOW, |
150 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PPAPI_PLUGIN)); | 53 state_manager.entropy_source_returned()); |
151 EXPECT_FALSE( | |
152 MetricsService::IsPluginProcess(content::PROCESS_TYPE_GPU)); | |
153 } | 54 } |
154 | 55 |
155 TEST_F(MetricsServiceTest, LowEntropySource0NotReset) { | 56 TEST_F(MetricsStateManagerTest, EntropySourceUsed_High) { |
156 MetricsService service; | 57 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 58 switches::kEnableMetricsReportingForTesting); |
| 59 |
| 60 MetricsStateManager state_manager(&prefs_); |
| 61 state_manager.CreateEntropyProvider(); |
| 62 EXPECT_EQ(MetricsStateManager::ENTROPY_SOURCE_HIGH, |
| 63 state_manager.entropy_source_returned()); |
| 64 } |
| 65 |
| 66 TEST_F(MetricsStateManagerTest, LowEntropySource0NotReset) { |
| 67 MetricsStateManager state_manager(&prefs_); |
157 | 68 |
158 // Get the low entropy source once, to initialize it. | 69 // Get the low entropy source once, to initialize it. |
159 service.GetLowEntropySource(); | 70 state_manager.GetLowEntropySource(); |
160 | 71 |
161 // Now, set it to 0 and ensure it doesn't get reset. | 72 // Now, set it to 0 and ensure it doesn't get reset. |
162 service.low_entropy_source_ = 0; | 73 state_manager.low_entropy_source_ = 0; |
163 EXPECT_EQ(0, service.GetLowEntropySource()); | 74 EXPECT_EQ(0, state_manager.GetLowEntropySource()); |
164 // Call it another time, just to make sure. | 75 // Call it another time, just to make sure. |
165 EXPECT_EQ(0, service.GetLowEntropySource()); | 76 EXPECT_EQ(0, state_manager.GetLowEntropySource()); |
166 } | 77 } |
167 | 78 |
168 TEST_F(MetricsServiceTest, PermutedEntropyCacheClearedWhenLowEntropyReset) { | 79 TEST_F(MetricsStateManagerTest, |
| 80 PermutedEntropyCacheClearedWhenLowEntropyReset) { |
169 const PrefService::Preference* low_entropy_pref = | 81 const PrefService::Preference* low_entropy_pref = |
170 GetLocalState()->FindPreference(prefs::kMetricsLowEntropySource); | 82 prefs_.FindPreference(prefs::kMetricsLowEntropySource); |
171 const char* kCachePrefName = prefs::kMetricsPermutedEntropyCache; | 83 const char* kCachePrefName = prefs::kMetricsPermutedEntropyCache; |
172 int low_entropy_value = -1; | 84 int low_entropy_value = -1; |
173 | 85 |
174 // First, generate an initial low entropy source value. | 86 // First, generate an initial low entropy source value. |
175 { | 87 { |
176 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); | 88 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); |
177 | 89 |
178 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 90 MetricsStateManager state_manager(&prefs_); |
179 MetricsService service; | 91 state_manager.GetLowEntropySource(); |
180 service.GetLowEntropySource(); | |
181 | 92 |
182 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); | 93 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); |
183 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); | 94 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); |
184 } | 95 } |
185 | 96 |
186 // Now, set a dummy value in the permuted entropy cache pref and verify that | 97 // Now, set a dummy value in the permuted entropy cache pref and verify that |
187 // another call to GetLowEntropySource() doesn't clobber it when | 98 // another call to GetLowEntropySource() doesn't clobber it when |
188 // --reset-variation-state wasn't specified. | 99 // --reset-variation-state wasn't specified. |
189 { | 100 { |
190 GetLocalState()->SetString(kCachePrefName, "test"); | 101 prefs_.SetString(kCachePrefName, "test"); |
191 | 102 |
192 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 103 MetricsStateManager state_manager(&prefs_); |
193 MetricsService service; | 104 state_manager.GetLowEntropySource(); |
194 service.GetLowEntropySource(); | |
195 | 105 |
196 EXPECT_EQ("test", GetLocalState()->GetString(kCachePrefName)); | 106 EXPECT_EQ("test", prefs_.GetString(kCachePrefName)); |
197 EXPECT_EQ(low_entropy_value, | 107 EXPECT_EQ(low_entropy_value, |
198 GetLocalState()->GetInteger(prefs::kMetricsLowEntropySource)); | 108 prefs_.GetInteger(prefs::kMetricsLowEntropySource)); |
199 } | 109 } |
200 | 110 |
201 // Verify that the cache does get reset if --reset-variations-state is passed. | 111 // Verify that the cache does get reset if --reset-variations-state is passed. |
202 { | 112 { |
203 CommandLine::ForCurrentProcess()->AppendSwitch( | 113 CommandLine::ForCurrentProcess()->AppendSwitch( |
204 switches::kResetVariationState); | 114 switches::kResetVariationState); |
205 | 115 |
206 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 116 MetricsStateManager state_manager(&prefs_); |
207 MetricsService service; | 117 state_manager.GetLowEntropySource(); |
208 service.GetLowEntropySource(); | |
209 | 118 |
210 EXPECT_TRUE(GetLocalState()->GetString(kCachePrefName).empty()); | 119 EXPECT_TRUE(prefs_.GetString(kCachePrefName).empty()); |
211 } | 120 } |
212 } | 121 } |
213 | 122 |
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 | 123 // 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 | 124 // 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. | 125 // cannot ensure that metrics state manager won't generate the same id again. |
380 TEST_F(MetricsServiceTest, ResetMetricsIDs) { | 126 TEST_F(MetricsStateManagerTest, ResetMetricsIDs) { |
381 // Set an initial client id in prefs. It should not be possible for the | 127 // Set an initial client id in prefs. It should not be possible for the |
382 // metrics service to generate this id randomly. | 128 // metrics state manager to generate this id randomly. |
383 const std::string kInitialClientId = "initial client id"; | 129 const std::string kInitialClientId = "initial client id"; |
384 GetLocalState()->SetString(prefs::kMetricsClientID, kInitialClientId); | 130 prefs_.SetString(prefs::kMetricsClientID, kInitialClientId); |
385 | 131 |
386 // Make sure the initial client id isn't reset by the metrics service. | 132 // Make sure the initial client id isn't reset by the metrics state manager. |
387 { | 133 { |
388 MetricsService service; | 134 MetricsStateManager state_manager(&prefs_); |
389 service.ForceClientIdCreation(); | 135 state_manager.ForceClientIdCreation(); |
390 EXPECT_TRUE(service.metrics_ids_reset_check_performed_); | 136 EXPECT_EQ(kInitialClientId, state_manager.client_id()); |
391 EXPECT_EQ(kInitialClientId, service.client_id_); | |
392 } | 137 } |
393 | 138 |
394 | |
395 // Set the reset pref to cause the IDs to be reset. | 139 // Set the reset pref to cause the IDs to be reset. |
396 GetLocalState()->SetBoolean(prefs::kMetricsResetIds, true); | 140 prefs_.SetBoolean(prefs::kMetricsResetIds, true); |
397 | 141 |
398 // Cause the actual reset to happen. | 142 // Cause the actual reset to happen. |
399 { | 143 { |
400 MetricsService service; | 144 MetricsStateManager state_manager(&prefs_); |
401 service.ForceClientIdCreation(); | 145 state_manager.ForceClientIdCreation(); |
402 EXPECT_TRUE(service.metrics_ids_reset_check_performed_); | 146 EXPECT_NE(kInitialClientId, state_manager.client_id()); |
403 EXPECT_NE(kInitialClientId, service.client_id_); | |
404 | 147 |
405 service.GetLowEntropySource(); | 148 state_manager.GetLowEntropySource(); |
406 | 149 |
407 EXPECT_FALSE(GetLocalState()->GetBoolean(prefs::kMetricsResetIds)); | 150 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); |
408 } | 151 } |
409 | 152 |
410 std::string new_client_id = | 153 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); |
411 GetLocalState()->GetString(prefs::kMetricsClientID); | 154 } |
412 | 155 |
413 EXPECT_NE(kInitialClientId, new_client_id); | 156 } // namespace metrics |
414 } | |
OLD | NEW |