OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/prefs/testing_pref_service.h" | |
10 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
11 #include "chrome/browser/metrics/metrics_service_observer.h" | 12 #include "chrome/browser/metrics/metrics_service_observer.h" |
12 #include "chrome/browser/metrics/metrics_state_manager.h" | 13 #include "chrome/browser/metrics/metrics_state_manager.h" |
14 #include "chrome/browser/prefs/browser_prefs.h" | |
13 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/scoped_testing_local_state.h" | |
16 #include "chrome/test/base/testing_browser_process.h" | 17 #include "chrome/test/base/testing_browser_process.h" |
17 #include "components/variations/metrics_util.h" | 18 #include "components/variations/metrics_util.h" |
18 #include "content/public/common/process_type.h" | 19 #include "content/public/common/process_type.h" |
19 #include "content/public/common/webplugininfo.h" | 20 #include "content/public/common/webplugininfo.h" |
20 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "ui/gfx/size.h" | 23 #include "ui/gfx/size.h" |
23 | 24 |
24 #if defined(OS_CHROMEOS) | 25 #if defined(OS_CHROMEOS) |
25 #include "chrome/browser/metrics/metrics_log_chromeos.h" | 26 #include "chrome/browser/metrics/metrics_log_chromeos.h" |
26 #endif // OS_CHROMEOS | 27 #endif // OS_CHROMEOS |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 using metrics::MetricsLogManager; | 31 using metrics::MetricsLogManager; |
31 | 32 |
32 class TestMetricsService : public MetricsService { | 33 class TestMetricsService : public MetricsService { |
33 public: | 34 public: |
34 explicit TestMetricsService(metrics::MetricsStateManager* state_manager) | 35 explicit TestMetricsService(metrics::MetricsStateManager* state_manager, |
35 : MetricsService(state_manager) { | 36 PrefService* local_state) |
36 } | 37 : MetricsService(state_manager, local_state) {} |
37 virtual ~TestMetricsService() {} | 38 virtual ~TestMetricsService() {} |
38 | 39 |
39 MetricsLogManager* log_manager() { | 40 MetricsLogManager* log_manager() { |
40 return &log_manager_; | 41 return &log_manager_; |
41 } | 42 } |
42 | 43 |
43 private: | 44 private: |
44 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); | 45 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); |
45 }; | 46 }; |
46 | 47 |
47 #if defined(OS_CHROMEOS) | 48 #if defined(OS_CHROMEOS) |
48 class TestMetricsLogChromeOS : public MetricsLogChromeOS { | 49 class TestMetricsLogChromeOS : public MetricsLogChromeOS { |
49 public: | 50 public: |
50 explicit TestMetricsLogChromeOS( | 51 explicit TestMetricsLogChromeOS( |
51 metrics::ChromeUserMetricsExtension* uma_proto) | 52 metrics::ChromeUserMetricsExtension* uma_proto) |
52 : MetricsLogChromeOS(uma_proto) { | 53 : MetricsLogChromeOS(uma_proto) { |
53 } | 54 } |
54 | 55 |
55 protected: | 56 protected: |
56 // Don't touch bluetooth information, as it won't be correctly initialized. | 57 // Don't touch bluetooth information, as it won't be correctly initialized. |
57 virtual void WriteBluetoothProto() OVERRIDE { | 58 virtual void WriteBluetoothProto() OVERRIDE { |
58 } | 59 } |
59 }; | 60 }; |
60 #endif // OS_CHROMEOS | 61 #endif // OS_CHROMEOS |
61 | 62 |
62 class TestMetricsLog : public MetricsLog { | 63 class TestMetricsLog : public MetricsLog { |
63 public: | 64 public: |
64 TestMetricsLog(const std::string& client_id, int session_id) | 65 TestMetricsLog(const std::string& client_id, |
65 : MetricsLog(client_id, session_id, MetricsLog::ONGOING_LOG) { | 66 int session_id, |
67 PrefService* local_state) | |
68 : MetricsLog(client_id, | |
69 session_id, | |
70 MetricsLog::ONGOING_LOG, | |
71 local_state) { | |
66 #if defined(OS_CHROMEOS) | 72 #if defined(OS_CHROMEOS) |
67 metrics_log_chromeos_.reset(new TestMetricsLogChromeOS( | 73 metrics_log_chromeos_.reset(new TestMetricsLogChromeOS( |
68 MetricsLog::uma_proto())); | 74 MetricsLog::uma_proto())); |
69 #endif // OS_CHROMEOS | 75 #endif // OS_CHROMEOS |
70 } | 76 } |
71 virtual ~TestMetricsLog() {} | 77 virtual ~TestMetricsLog() {} |
72 | 78 |
73 private: | 79 private: |
74 virtual gfx::Size GetScreenSize() const OVERRIDE { | 80 virtual gfx::Size GetScreenSize() const OVERRIDE { |
75 return gfx::Size(1024, 768); | 81 return gfx::Size(1024, 768); |
76 } | 82 } |
77 | 83 |
78 virtual float GetScreenDeviceScaleFactor() const OVERRIDE { | 84 virtual float GetScreenDeviceScaleFactor() const OVERRIDE { |
79 return 1.0f; | 85 return 1.0f; |
80 } | 86 } |
81 | 87 |
82 virtual int GetScreenCount() const OVERRIDE { | 88 virtual int GetScreenCount() const OVERRIDE { |
83 return 1; | 89 return 1; |
84 } | 90 } |
85 | 91 |
86 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); | 92 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
87 }; | 93 }; |
88 | 94 |
89 class MetricsServiceTest : public testing::Test { | 95 class MetricsServiceTest : public testing::Test { |
90 public: | 96 public: |
91 MetricsServiceTest() | 97 MetricsServiceTest() { |
92 : testing_local_state_(TestingBrowserProcess::GetGlobal()), | 98 chrome::RegisterLocalState(testing_local_state_.registry()); |
Alexei Svitkine (slow)
2014/05/19 13:13:54
Instead of calling this, could this call MetricsSe
| |
93 metrics_state_manager_(metrics::MetricsStateManager::Create( | 99 metrics_state_manager_ = |
94 GetLocalState())) { | 100 metrics::MetricsStateManager::Create(GetLocalState()); |
95 } | 101 } |
96 | 102 |
97 virtual ~MetricsServiceTest() { | 103 virtual ~MetricsServiceTest() { |
98 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 104 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE, |
105 GetLocalState()); | |
99 } | 106 } |
100 | 107 |
101 metrics::MetricsStateManager* GetMetricsStateManager() { | 108 metrics::MetricsStateManager* GetMetricsStateManager() { |
102 return metrics_state_manager_.get(); | 109 return metrics_state_manager_.get(); |
103 } | 110 } |
104 | 111 |
105 PrefService* GetLocalState() { | 112 PrefService* GetLocalState() { return &testing_local_state_; } |
106 return testing_local_state_.Get(); | |
107 } | |
108 | 113 |
109 // Sets metrics reporting as enabled for testing. | 114 // Sets metrics reporting as enabled for testing. |
110 void EnableMetricsReporting() { | 115 void EnableMetricsReporting() { |
111 // TODO(asvitkine): Refactor the code to not need this flag and delete it. | 116 // TODO(asvitkine): Refactor the code to not need this flag and delete it. |
112 CommandLine::ForCurrentProcess()->AppendSwitch( | 117 CommandLine::ForCurrentProcess()->AppendSwitch( |
113 switches::kEnableMetricsReportingForTesting); | 118 switches::kEnableMetricsReportingForTesting); |
114 } | 119 } |
115 | 120 |
116 // Waits until base::TimeTicks::Now() no longer equals |value|. This should | 121 // Waits until base::TimeTicks::Now() no longer equals |value|. This should |
117 // take between 1-15ms per the documented resolution of base::TimeTicks. | 122 // take between 1-15ms per the documented resolution of base::TimeTicks. |
(...skipping 15 matching lines...) Expand all Loading... | |
133 synthetic_trials.begin(); | 138 synthetic_trials.begin(); |
134 it != synthetic_trials.end(); ++it) { | 139 it != synthetic_trials.end(); ++it) { |
135 if ((*it).name == trial_name_hash && (*it).group == trial_group_hash) | 140 if ((*it).name == trial_name_hash && (*it).group == trial_group_hash) |
136 return true; | 141 return true; |
137 } | 142 } |
138 return false; | 143 return false; |
139 } | 144 } |
140 | 145 |
141 private: | 146 private: |
142 content::TestBrowserThreadBundle thread_bundle_; | 147 content::TestBrowserThreadBundle thread_bundle_; |
143 ScopedTestingLocalState testing_local_state_; | 148 TestingPrefServiceSimple testing_local_state_; |
144 scoped_ptr<metrics::MetricsStateManager> metrics_state_manager_; | 149 scoped_ptr<metrics::MetricsStateManager> metrics_state_manager_; |
145 | 150 |
146 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); | 151 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); |
147 }; | 152 }; |
148 | 153 |
149 class TestMetricsServiceObserver : public MetricsServiceObserver { | 154 class TestMetricsServiceObserver : public MetricsServiceObserver { |
150 public: | 155 public: |
151 TestMetricsServiceObserver(): observed_(0) {} | 156 TestMetricsServiceObserver(): observed_(0) {} |
152 virtual ~TestMetricsServiceObserver() {} | 157 virtual ~TestMetricsServiceObserver() {} |
153 | 158 |
(...skipping 16 matching lines...) Expand all Loading... | |
170 EXPECT_TRUE( | 175 EXPECT_TRUE( |
171 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PPAPI_PLUGIN)); | 176 MetricsService::IsPluginProcess(content::PROCESS_TYPE_PPAPI_PLUGIN)); |
172 EXPECT_FALSE( | 177 EXPECT_FALSE( |
173 MetricsService::IsPluginProcess(content::PROCESS_TYPE_GPU)); | 178 MetricsService::IsPluginProcess(content::PROCESS_TYPE_GPU)); |
174 } | 179 } |
175 | 180 |
176 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { | 181 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { |
177 EnableMetricsReporting(); | 182 EnableMetricsReporting(); |
178 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); | 183 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); |
179 | 184 |
180 TestMetricsService service(GetMetricsStateManager()); | 185 TestMetricsService service(GetMetricsStateManager(), GetLocalState()); |
181 service.InitializeMetricsRecordingState(); | 186 service.InitializeMetricsRecordingState(); |
182 // No initial stability log should be generated. | 187 // No initial stability log should be generated. |
183 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); | 188 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); |
184 EXPECT_FALSE(service.log_manager()->has_staged_log()); | 189 EXPECT_FALSE(service.log_manager()->has_staged_log()); |
185 } | 190 } |
186 | 191 |
187 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { | 192 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { |
188 EnableMetricsReporting(); | 193 EnableMetricsReporting(); |
189 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly); | 194 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly); |
190 | 195 |
191 // Set up prefs to simulate restarting after a crash. | 196 // Set up prefs to simulate restarting after a crash. |
192 | 197 |
193 // Save an existing system profile to prefs, to correspond to what would be | 198 // Save an existing system profile to prefs, to correspond to what would be |
194 // saved from a previous session. | 199 // saved from a previous session. |
195 TestMetricsLog log("client", 1); | 200 TestMetricsLog log("client", 1, GetLocalState()); |
196 log.RecordEnvironment(std::vector<content::WebPluginInfo>(), | 201 log.RecordEnvironment(std::vector<content::WebPluginInfo>(), |
197 GoogleUpdateMetrics(), | 202 GoogleUpdateMetrics(), |
198 std::vector<chrome_variations::ActiveGroupId>()); | 203 std::vector<chrome_variations::ActiveGroupId>()); |
199 | 204 |
200 // Record stability build time and version from previous session, so that | 205 // Record stability build time and version from previous session, so that |
201 // stability metrics (including exited cleanly flag) won't be cleared. | 206 // stability metrics (including exited cleanly flag) won't be cleared. |
202 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, | 207 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, |
203 MetricsLog::GetBuildTime()); | 208 MetricsLog::GetBuildTime()); |
204 GetLocalState()->SetString(prefs::kStabilityStatsVersion, | 209 GetLocalState()->SetString(prefs::kStabilityStatsVersion, |
205 MetricsLog::GetVersionString()); | 210 MetricsLog::GetVersionString()); |
206 | 211 |
207 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false); | 212 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false); |
208 | 213 |
209 TestMetricsService service(GetMetricsStateManager()); | 214 TestMetricsService service(GetMetricsStateManager(), GetLocalState()); |
210 service.InitializeMetricsRecordingState(); | 215 service.InitializeMetricsRecordingState(); |
211 | 216 |
212 // The initial stability log should be generated and persisted in unsent logs. | 217 // The initial stability log should be generated and persisted in unsent logs. |
213 MetricsLogManager* log_manager = service.log_manager(); | 218 MetricsLogManager* log_manager = service.log_manager(); |
214 EXPECT_TRUE(log_manager->has_unsent_logs()); | 219 EXPECT_TRUE(log_manager->has_unsent_logs()); |
215 EXPECT_FALSE(log_manager->has_staged_log()); | 220 EXPECT_FALSE(log_manager->has_staged_log()); |
216 | 221 |
217 // Stage the log and retrieve it. | 222 // Stage the log and retrieve it. |
218 log_manager->StageNextLogForUpload(); | 223 log_manager->StageNextLogForUpload(); |
219 EXPECT_TRUE(log_manager->has_staged_log()); | 224 EXPECT_TRUE(log_manager->has_staged_log()); |
220 | 225 |
221 metrics::ChromeUserMetricsExtension uma_log; | 226 metrics::ChromeUserMetricsExtension uma_log; |
222 EXPECT_TRUE(uma_log.ParseFromString(log_manager->staged_log())); | 227 EXPECT_TRUE(uma_log.ParseFromString(log_manager->staged_log())); |
223 | 228 |
224 EXPECT_TRUE(uma_log.has_client_id()); | 229 EXPECT_TRUE(uma_log.has_client_id()); |
225 EXPECT_TRUE(uma_log.has_session_id()); | 230 EXPECT_TRUE(uma_log.has_session_id()); |
226 EXPECT_TRUE(uma_log.has_system_profile()); | 231 EXPECT_TRUE(uma_log.has_system_profile()); |
227 EXPECT_EQ(0, uma_log.user_action_event_size()); | 232 EXPECT_EQ(0, uma_log.user_action_event_size()); |
228 EXPECT_EQ(0, uma_log.omnibox_event_size()); | 233 EXPECT_EQ(0, uma_log.omnibox_event_size()); |
229 EXPECT_EQ(0, uma_log.histogram_event_size()); | 234 EXPECT_EQ(0, uma_log.histogram_event_size()); |
230 EXPECT_EQ(0, uma_log.profiler_event_size()); | 235 EXPECT_EQ(0, uma_log.profiler_event_size()); |
231 EXPECT_EQ(0, uma_log.perf_data_size()); | 236 EXPECT_EQ(0, uma_log.perf_data_size()); |
232 | 237 |
233 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count()); | 238 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count()); |
234 } | 239 } |
235 | 240 |
236 TEST_F(MetricsServiceTest, RegisterSyntheticTrial) { | 241 TEST_F(MetricsServiceTest, RegisterSyntheticTrial) { |
237 MetricsService service(GetMetricsStateManager()); | 242 MetricsService service(GetMetricsStateManager(), GetLocalState()); |
238 | 243 |
239 // Add two synthetic trials and confirm that they show up in the list. | 244 // Add two synthetic trials and confirm that they show up in the list. |
240 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), | 245 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), |
241 metrics::HashName("Group1")); | 246 metrics::HashName("Group1")); |
242 service.RegisterSyntheticFieldTrial(trial1); | 247 service.RegisterSyntheticFieldTrial(trial1); |
243 | 248 |
244 SyntheticTrialGroup trial2(metrics::HashName("TestTrial2"), | 249 SyntheticTrialGroup trial2(metrics::HashName("TestTrial2"), |
245 metrics::HashName("Group2")); | 250 metrics::HashName("Group2")); |
246 service.RegisterSyntheticFieldTrial(trial2); | 251 service.RegisterSyntheticFieldTrial(trial2); |
247 // Ensure that time has advanced by at least a tick before proceeding. | 252 // Ensure that time has advanced by at least a tick before proceeding. |
248 WaitUntilTimeChanges(base::TimeTicks::Now()); | 253 WaitUntilTimeChanges(base::TimeTicks::Now()); |
249 | 254 |
250 service.log_manager_.BeginLoggingWithLog( | 255 service.log_manager_.BeginLoggingWithLog(new MetricsLog( |
251 new MetricsLog("clientID", 1, MetricsLog::INITIAL_STABILITY_LOG)); | 256 "clientID", 1, MetricsLog::INITIAL_STABILITY_LOG, GetLocalState())); |
252 // Save the time when the log was started (it's okay for this to be greater | 257 // Save the time when the log was started (it's okay for this to be greater |
253 // than the time recorded by the above call since it's used to ensure the | 258 // than the time recorded by the above call since it's used to ensure the |
254 // value changes). | 259 // value changes). |
255 const base::TimeTicks begin_log_time = base::TimeTicks::Now(); | 260 const base::TimeTicks begin_log_time = base::TimeTicks::Now(); |
256 | 261 |
257 std::vector<chrome_variations::ActiveGroupId> synthetic_trials; | 262 std::vector<chrome_variations::ActiveGroupId> synthetic_trials; |
258 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | 263 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); |
259 EXPECT_EQ(2U, synthetic_trials.size()); | 264 EXPECT_EQ(2U, synthetic_trials.size()); |
260 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group1")); | 265 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group1")); |
261 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 266 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
(...skipping 16 matching lines...) Expand all Loading... | |
278 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | 283 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); |
279 EXPECT_EQ(1U, synthetic_trials.size()); | 284 EXPECT_EQ(1U, synthetic_trials.size()); |
280 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 285 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
281 | 286 |
282 // Ensure that time has advanced by at least a tick before proceeding. | 287 // Ensure that time has advanced by at least a tick before proceeding. |
283 WaitUntilTimeChanges(base::TimeTicks::Now()); | 288 WaitUntilTimeChanges(base::TimeTicks::Now()); |
284 | 289 |
285 // Start a new log and ensure all three trials appear in it. | 290 // Start a new log and ensure all three trials appear in it. |
286 service.log_manager_.FinishCurrentLog(); | 291 service.log_manager_.FinishCurrentLog(); |
287 service.log_manager_.BeginLoggingWithLog( | 292 service.log_manager_.BeginLoggingWithLog( |
288 new MetricsLog("clientID", 1, MetricsLog::ONGOING_LOG)); | 293 new MetricsLog("clientID", 1, MetricsLog::ONGOING_LOG, GetLocalState())); |
289 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | 294 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); |
290 EXPECT_EQ(3U, synthetic_trials.size()); | 295 EXPECT_EQ(3U, synthetic_trials.size()); |
291 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); | 296 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); |
292 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 297 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
293 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); | 298 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); |
294 service.log_manager_.FinishCurrentLog(); | 299 service.log_manager_.FinishCurrentLog(); |
295 } | 300 } |
296 | 301 |
297 TEST_F(MetricsServiceTest, MetricsReportingEnabled) { | 302 TEST_F(MetricsServiceTest, MetricsReportingEnabled) { |
298 #if !defined(OS_CHROMEOS) | 303 #if !defined(OS_CHROMEOS) |
299 GetLocalState()->SetBoolean(prefs::kMetricsReportingEnabled, false); | 304 GetLocalState()->SetBoolean(prefs::kMetricsReportingEnabled, false); |
300 EXPECT_FALSE(MetricsServiceHelper::IsMetricsReportingEnabled()); | 305 EXPECT_FALSE( |
306 MetricsServiceHelper::IsMetricsReportingEnabled(GetLocalState())); | |
301 GetLocalState()->SetBoolean(prefs::kMetricsReportingEnabled, true); | 307 GetLocalState()->SetBoolean(prefs::kMetricsReportingEnabled, true); |
302 EXPECT_TRUE(MetricsServiceHelper::IsMetricsReportingEnabled()); | 308 EXPECT_TRUE(MetricsServiceHelper::IsMetricsReportingEnabled(GetLocalState())); |
303 GetLocalState()->ClearPref(prefs::kMetricsReportingEnabled); | 309 GetLocalState()->ClearPref(prefs::kMetricsReportingEnabled); |
304 EXPECT_FALSE(MetricsServiceHelper::IsMetricsReportingEnabled()); | 310 EXPECT_FALSE( |
311 MetricsServiceHelper::IsMetricsReportingEnabled(GetLocalState())); | |
305 #else | 312 #else |
306 // ChromeOS does not register prefs::kMetricsReportingEnabled and uses | 313 // ChromeOS does not register prefs::kMetricsReportingEnabled and uses |
307 // device settings for metrics reporting. | 314 // device settings for metrics reporting. |
308 EXPECT_FALSE(MetricsServiceHelper::IsMetricsReportingEnabled()); | 315 EXPECT_FALSE( |
316 MetricsServiceHelper::IsMetricsReportingEnabled(GetLocalState())); | |
309 #endif | 317 #endif |
310 } | 318 } |
311 | 319 |
312 TEST_F(MetricsServiceTest, CrashReportingEnabled) { | 320 TEST_F(MetricsServiceTest, CrashReportingEnabled) { |
313 #if defined(GOOGLE_CHROME_BUILD) | 321 #if defined(GOOGLE_CHROME_BUILD) |
314 // ChromeOS has different device settings for crash reporting. | 322 // ChromeOS has different device settings for crash reporting. |
315 #if !defined(OS_CHROMEOS) | 323 #if !defined(OS_CHROMEOS) |
316 #if defined(OS_ANDROID) | 324 #if defined(OS_ANDROID) |
317 const char* crash_pref = prefs::kCrashReportingEnabled; | 325 const char* crash_pref = prefs::kCrashReportingEnabled; |
318 #else | 326 #else |
319 const char* crash_pref = prefs::kMetricsReportingEnabled; | 327 const char* crash_pref = prefs::kMetricsReportingEnabled; |
320 #endif | 328 #endif |
321 GetLocalState()->SetBoolean(crash_pref, false); | 329 GetLocalState()->SetBoolean(crash_pref, false); |
322 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled()); | 330 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled(GetLocalState())); |
323 GetLocalState()->SetBoolean(crash_pref, true); | 331 GetLocalState()->SetBoolean(crash_pref, true); |
324 EXPECT_TRUE(MetricsServiceHelper::IsCrashReportingEnabled()); | 332 EXPECT_TRUE(MetricsServiceHelper::IsCrashReportingEnabled(GetLocalState())); |
325 GetLocalState()->ClearPref(crash_pref); | 333 GetLocalState()->ClearPref(crash_pref); |
326 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled()); | 334 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled(GetLocalState())); |
327 #endif // !defined(OS_CHROMEOS) | 335 #endif // !defined(OS_CHROMEOS) |
328 #else // defined(GOOGLE_CHROME_BUILD) | 336 #else // defined(GOOGLE_CHROME_BUILD) |
329 // Chromium branded browsers never have crash reporting enabled. | 337 // Chromium branded browsers never have crash reporting enabled. |
330 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled()); | 338 EXPECT_FALSE(MetricsServiceHelper::IsCrashReportingEnabled(GetLocalState())); |
331 #endif // defined(GOOGLE_CHROME_BUILD) | 339 #endif // defined(GOOGLE_CHROME_BUILD) |
332 } | 340 } |
333 | 341 |
334 TEST_F(MetricsServiceTest, MetricsServiceObserver) { | 342 TEST_F(MetricsServiceTest, MetricsServiceObserver) { |
335 MetricsService service(GetMetricsStateManager()); | 343 MetricsService service(GetMetricsStateManager(), GetLocalState()); |
336 TestMetricsServiceObserver observer1; | 344 TestMetricsServiceObserver observer1; |
337 TestMetricsServiceObserver observer2; | 345 TestMetricsServiceObserver observer2; |
338 | 346 |
339 service.AddObserver(&observer1); | 347 service.AddObserver(&observer1); |
340 EXPECT_EQ(0, observer1.observed()); | 348 EXPECT_EQ(0, observer1.observed()); |
341 EXPECT_EQ(0, observer2.observed()); | 349 EXPECT_EQ(0, observer2.observed()); |
342 | 350 |
343 service.OpenNewLog(); | 351 service.OpenNewLog(); |
344 EXPECT_EQ(1, observer1.observed()); | 352 EXPECT_EQ(1, observer1.observed()); |
345 EXPECT_EQ(0, observer2.observed()); | 353 EXPECT_EQ(0, observer2.observed()); |
346 service.log_manager_.FinishCurrentLog(); | 354 service.log_manager_.FinishCurrentLog(); |
347 | 355 |
348 service.AddObserver(&observer2); | 356 service.AddObserver(&observer2); |
349 | 357 |
350 service.OpenNewLog(); | 358 service.OpenNewLog(); |
351 EXPECT_EQ(2, observer1.observed()); | 359 EXPECT_EQ(2, observer1.observed()); |
352 EXPECT_EQ(1, observer2.observed()); | 360 EXPECT_EQ(1, observer2.observed()); |
353 service.log_manager_.FinishCurrentLog(); | 361 service.log_manager_.FinishCurrentLog(); |
354 | 362 |
355 service.RemoveObserver(&observer1); | 363 service.RemoveObserver(&observer1); |
356 | 364 |
357 service.OpenNewLog(); | 365 service.OpenNewLog(); |
358 EXPECT_EQ(2, observer1.observed()); | 366 EXPECT_EQ(2, observer1.observed()); |
359 EXPECT_EQ(2, observer2.observed()); | 367 EXPECT_EQ(2, observer2.observed()); |
360 service.log_manager_.FinishCurrentLog(); | 368 service.log_manager_.FinishCurrentLog(); |
361 | 369 |
362 service.RemoveObserver(&observer2); | 370 service.RemoveObserver(&observer2); |
363 } | 371 } |
OLD | NEW |