Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Side by Side Diff: chrome/browser/metrics/metrics_service_unittest.cc

Issue 282093012: Remove dependencies of Metrics{Service,Log} on g_browser_process->local_state() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Android compile fix Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/bind.h" 9 #include "base/bind.h"
10 #include "base/prefs/testing_pref_service.h"
10 #include "base/threading/platform_thread.h" 11 #include "base/threading/platform_thread.h"
12 #include "chrome/browser/google/google_util.h"
11 #include "chrome/browser/metrics/metrics_state_manager.h" 13 #include "chrome/browser/metrics/metrics_state_manager.h"
12 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/scoped_testing_local_state.h" 15 #include "chrome/test/base/scoped_testing_local_state.h"
14 #include "chrome/test/base/testing_browser_process.h" 16 #include "chrome/test/base/testing_browser_process.h"
15 #include "components/metrics/metrics_log_base.h" 17 #include "components/metrics/metrics_log_base.h"
16 #include "components/metrics/metrics_service_observer.h" 18 #include "components/metrics/metrics_service_observer.h"
17 #include "components/metrics/test_metrics_service_client.h" 19 #include "components/metrics/test_metrics_service_client.h"
18 #include "components/variations/metrics_util.h" 20 #include "components/variations/metrics_util.h"
19 #include "content/public/test/test_browser_thread_bundle.h" 21 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 23
22 namespace { 24 namespace {
23 25
24 using metrics::MetricsLogManager; 26 using metrics::MetricsLogManager;
25 27
26 class TestMetricsService : public MetricsService { 28 class TestMetricsService : public MetricsService {
27 public: 29 public:
28 TestMetricsService(metrics::MetricsStateManager* state_manager, 30 TestMetricsService(metrics::MetricsStateManager* state_manager,
29 metrics::MetricsServiceClient* client) 31 metrics::MetricsServiceClient* client,
30 : MetricsService(state_manager, client) {} 32 PrefService* local_state)
33 : MetricsService(state_manager, client, local_state) {}
31 virtual ~TestMetricsService() {} 34 virtual ~TestMetricsService() {}
32 35
33 using MetricsService::log_manager; 36 using MetricsService::log_manager;
34 37
35 private: 38 private:
36 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); 39 DISALLOW_COPY_AND_ASSIGN(TestMetricsService);
37 }; 40 };
38 41
39 class TestMetricsLog : public MetricsLog { 42 class TestMetricsLog : public MetricsLog {
40 public: 43 public:
41 TestMetricsLog(const std::string& client_id, 44 TestMetricsLog(const std::string& client_id,
42 int session_id, 45 int session_id,
43 metrics::MetricsServiceClient* client) 46 metrics::MetricsServiceClient* client,
44 : MetricsLog(client_id, session_id, MetricsLog::ONGOING_LOG, client) { 47 PrefService* local_state)
45 } 48 : MetricsLog(client_id,
49 session_id,
50 MetricsLog::ONGOING_LOG,
51 client,
52 local_state) {}
46 53
47 virtual ~TestMetricsLog() {} 54 virtual ~TestMetricsLog() {}
48 55
49 private: 56 private:
50 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); 57 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog);
51 }; 58 };
52 59
53 class MetricsServiceTest : public testing::Test { 60 class MetricsServiceTest : public testing::Test {
54 public: 61 public:
55 MetricsServiceTest() 62 MetricsServiceTest() : is_metrics_reporting_enabled_(false) {
56 : testing_local_state_(TestingBrowserProcess::GetGlobal()), 63 MetricsService::RegisterPrefs(testing_local_state_.registry());
57 is_metrics_reporting_enabled_(false), 64 metrics_state_manager_ = metrics::MetricsStateManager::Create(
58 metrics_state_manager_( 65 GetLocalState(),
59 metrics::MetricsStateManager::Create( 66 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled,
60 GetLocalState(), 67 base::Unretained(this)));
61 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled,
62 base::Unretained(this)))) {
63 } 68 }
64 69
65 virtual ~MetricsServiceTest() { 70 virtual ~MetricsServiceTest() {
66 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); 71 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE,
72 GetLocalState());
67 } 73 }
68 74
69 metrics::MetricsStateManager* GetMetricsStateManager() { 75 metrics::MetricsStateManager* GetMetricsStateManager() {
70 return metrics_state_manager_.get(); 76 return metrics_state_manager_.get();
71 } 77 }
72 78
73 PrefService* GetLocalState() { 79 PrefService* GetLocalState() { return &testing_local_state_; }
74 return testing_local_state_.Get();
75 }
76 80
77 // Sets metrics reporting as enabled for testing. 81 // Sets metrics reporting as enabled for testing.
78 void EnableMetricsReporting() { 82 void EnableMetricsReporting() {
79 is_metrics_reporting_enabled_ = true; 83 is_metrics_reporting_enabled_ = true;
80 } 84 }
81 85
82 // Waits until base::TimeTicks::Now() no longer equals |value|. This should 86 // Waits until base::TimeTicks::Now() no longer equals |value|. This should
83 // take between 1-15ms per the documented resolution of base::TimeTicks. 87 // take between 1-15ms per the documented resolution of base::TimeTicks.
84 void WaitUntilTimeChanges(const base::TimeTicks& value) { 88 void WaitUntilTimeChanges(const base::TimeTicks& value) {
85 while (base::TimeTicks::Now() == value) { 89 while (base::TimeTicks::Now() == value) {
(...skipping 17 matching lines...) Expand all
103 } 107 }
104 return false; 108 return false;
105 } 109 }
106 110
107 private: 111 private:
108 bool is_metrics_reporting_enabled() const { 112 bool is_metrics_reporting_enabled() const {
109 return is_metrics_reporting_enabled_; 113 return is_metrics_reporting_enabled_;
110 } 114 }
111 115
112 content::TestBrowserThreadBundle thread_bundle_; 116 content::TestBrowserThreadBundle thread_bundle_;
113 ScopedTestingLocalState testing_local_state_;
114 bool is_metrics_reporting_enabled_; 117 bool is_metrics_reporting_enabled_;
118 TestingPrefServiceSimple testing_local_state_;
115 scoped_ptr<metrics::MetricsStateManager> metrics_state_manager_; 119 scoped_ptr<metrics::MetricsStateManager> metrics_state_manager_;
116 120
117 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest); 121 DISALLOW_COPY_AND_ASSIGN(MetricsServiceTest);
118 }; 122 };
119 123
120 class TestMetricsServiceObserver : public MetricsServiceObserver { 124 class TestMetricsServiceObserver : public MetricsServiceObserver {
121 public: 125 public:
122 TestMetricsServiceObserver(): observed_(0) {} 126 TestMetricsServiceObserver(): observed_(0) {}
123 virtual ~TestMetricsServiceObserver() {} 127 virtual ~TestMetricsServiceObserver() {}
124 128
125 virtual void OnDidCreateMetricsLog() OVERRIDE { 129 virtual void OnDidCreateMetricsLog() OVERRIDE {
126 ++observed_; 130 ++observed_;
127 } 131 }
128 int observed() const { return observed_; } 132 int observed() const { return observed_; }
129 133
130 private: 134 private:
131 int observed_; 135 int observed_;
132 136
133 DISALLOW_COPY_AND_ASSIGN(TestMetricsServiceObserver); 137 DISALLOW_COPY_AND_ASSIGN(TestMetricsServiceObserver);
134 }; 138 };
135 139
136 } // namespace 140 } // namespace
137 141
138 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { 142 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) {
139 EnableMetricsReporting(); 143 EnableMetricsReporting();
140 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); 144 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true);
141 145
142 metrics::TestMetricsServiceClient client; 146 metrics::TestMetricsServiceClient client;
143 TestMetricsService service(GetMetricsStateManager(), &client); 147 TestMetricsService service(
148 GetMetricsStateManager(), &client, GetLocalState());
144 service.InitializeMetricsRecordingState(); 149 service.InitializeMetricsRecordingState();
145 // No initial stability log should be generated. 150 // No initial stability log should be generated.
146 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); 151 EXPECT_FALSE(service.log_manager()->has_unsent_logs());
147 EXPECT_FALSE(service.log_manager()->has_staged_log()); 152 EXPECT_FALSE(service.log_manager()->has_staged_log());
148 } 153 }
149 154
150 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { 155 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) {
156 // TODO(asvitkine): Eliminate using |testing_local_state| in favor of using
157 // |GetLocalState()| once MetricsService no longer internally creates metrics
158 // providers that rely on g_browser_process->local_state() being correctly
159 // set up. crbug.com/375776.
160 ScopedTestingLocalState testing_local_state(
161 TestingBrowserProcess::GetGlobal());
162 TestingPrefServiceSimple* local_state = testing_local_state.Get();
151 EnableMetricsReporting(); 163 EnableMetricsReporting();
152 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly); 164 local_state->ClearPref(prefs::kStabilityExitedCleanly);
153 165
154 // Set up prefs to simulate restarting after a crash. 166 // Set up prefs to simulate restarting after a crash.
155 167
156 // Save an existing system profile to prefs, to correspond to what would be 168 // Save an existing system profile to prefs, to correspond to what would be
157 // saved from a previous session. 169 // saved from a previous session.
158 metrics::TestMetricsServiceClient client; 170 metrics::TestMetricsServiceClient client;
159 TestMetricsLog log("client", 1, &client); 171 TestMetricsLog log("client", 1, &client, local_state);
160 log.RecordEnvironment(std::vector<metrics::MetricsProvider*>(), 172 log.RecordEnvironment(std::vector<metrics::MetricsProvider*>(),
161 std::vector<variations::ActiveGroupId>()); 173 std::vector<variations::ActiveGroupId>());
162 174
163 // Record stability build time and version from previous session, so that 175 // Record stability build time and version from previous session, so that
164 // stability metrics (including exited cleanly flag) won't be cleared. 176 // stability metrics (including exited cleanly flag) won't be cleared.
165 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, 177 local_state->SetInt64(prefs::kStabilityStatsBuildTime,
166 MetricsLog::GetBuildTime()); 178 MetricsLog::GetBuildTime());
167 GetLocalState()->SetString(prefs::kStabilityStatsVersion, 179 local_state->SetString(prefs::kStabilityStatsVersion,
168 client.GetVersionString()); 180 client.GetVersionString());
169 181
170 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false); 182 local_state->SetBoolean(prefs::kStabilityExitedCleanly, false);
171 183
172 TestMetricsService service(GetMetricsStateManager(), &client); 184 TestMetricsService service(GetMetricsStateManager(), &client, local_state);
173 service.InitializeMetricsRecordingState(); 185 service.InitializeMetricsRecordingState();
174 186
175 // The initial stability log should be generated and persisted in unsent logs. 187 // The initial stability log should be generated and persisted in unsent logs.
176 MetricsLogManager* log_manager = service.log_manager(); 188 MetricsLogManager* log_manager = service.log_manager();
177 EXPECT_TRUE(log_manager->has_unsent_logs()); 189 EXPECT_TRUE(log_manager->has_unsent_logs());
178 EXPECT_FALSE(log_manager->has_staged_log()); 190 EXPECT_FALSE(log_manager->has_staged_log());
179 191
180 // Stage the log and retrieve it. 192 // Stage the log and retrieve it.
181 log_manager->StageNextLogForUpload(); 193 log_manager->StageNextLogForUpload();
182 EXPECT_TRUE(log_manager->has_staged_log()); 194 EXPECT_TRUE(log_manager->has_staged_log());
183 195
184 metrics::ChromeUserMetricsExtension uma_log; 196 metrics::ChromeUserMetricsExtension uma_log;
185 EXPECT_TRUE(uma_log.ParseFromString(log_manager->staged_log())); 197 EXPECT_TRUE(uma_log.ParseFromString(log_manager->staged_log()));
186 198
187 EXPECT_TRUE(uma_log.has_client_id()); 199 EXPECT_TRUE(uma_log.has_client_id());
188 EXPECT_TRUE(uma_log.has_session_id()); 200 EXPECT_TRUE(uma_log.has_session_id());
189 EXPECT_TRUE(uma_log.has_system_profile()); 201 EXPECT_TRUE(uma_log.has_system_profile());
190 EXPECT_EQ(0, uma_log.user_action_event_size()); 202 EXPECT_EQ(0, uma_log.user_action_event_size());
191 EXPECT_EQ(0, uma_log.omnibox_event_size()); 203 EXPECT_EQ(0, uma_log.omnibox_event_size());
192 EXPECT_EQ(0, uma_log.histogram_event_size()); 204 EXPECT_EQ(0, uma_log.histogram_event_size());
193 EXPECT_EQ(0, uma_log.profiler_event_size()); 205 EXPECT_EQ(0, uma_log.profiler_event_size());
194 EXPECT_EQ(0, uma_log.perf_data_size()); 206 EXPECT_EQ(0, uma_log.perf_data_size());
195 207
196 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count()); 208 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count());
197 } 209 }
198 210
199 TEST_F(MetricsServiceTest, RegisterSyntheticTrial) { 211 TEST_F(MetricsServiceTest, RegisterSyntheticTrial) {
200 metrics::TestMetricsServiceClient client; 212 metrics::TestMetricsServiceClient client;
201 MetricsService service(GetMetricsStateManager(), &client); 213 MetricsService service(GetMetricsStateManager(), &client, GetLocalState());
202 214
203 // Add two synthetic trials and confirm that they show up in the list. 215 // Add two synthetic trials and confirm that they show up in the list.
204 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), 216 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"),
205 metrics::HashName("Group1")); 217 metrics::HashName("Group1"));
206 service.RegisterSyntheticFieldTrial(trial1); 218 service.RegisterSyntheticFieldTrial(trial1);
207 219
208 SyntheticTrialGroup trial2(metrics::HashName("TestTrial2"), 220 SyntheticTrialGroup trial2(metrics::HashName("TestTrial2"),
209 metrics::HashName("Group2")); 221 metrics::HashName("Group2"));
210 service.RegisterSyntheticFieldTrial(trial2); 222 service.RegisterSyntheticFieldTrial(trial2);
211 // Ensure that time has advanced by at least a tick before proceeding. 223 // Ensure that time has advanced by at least a tick before proceeding.
212 WaitUntilTimeChanges(base::TimeTicks::Now()); 224 WaitUntilTimeChanges(base::TimeTicks::Now());
213 225
214 service.log_manager_.BeginLoggingWithLog( 226 service.log_manager_.BeginLoggingWithLog(scoped_ptr<metrics::MetricsLogBase>(
215 scoped_ptr<metrics::MetricsLogBase>(new MetricsLog( 227 new MetricsLog("clientID",
216 "clientID", 1, MetricsLog::INITIAL_STABILITY_LOG, &client))); 228 1,
229 MetricsLog::INITIAL_STABILITY_LOG,
230 &client,
231 GetLocalState())));
217 // Save the time when the log was started (it's okay for this to be greater 232 // Save the time when the log was started (it's okay for this to be greater
218 // than the time recorded by the above call since it's used to ensure the 233 // than the time recorded by the above call since it's used to ensure the
219 // value changes). 234 // value changes).
220 const base::TimeTicks begin_log_time = base::TimeTicks::Now(); 235 const base::TimeTicks begin_log_time = base::TimeTicks::Now();
221 236
222 std::vector<variations::ActiveGroupId> synthetic_trials; 237 std::vector<variations::ActiveGroupId> synthetic_trials;
223 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 238 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
224 EXPECT_EQ(2U, synthetic_trials.size()); 239 EXPECT_EQ(2U, synthetic_trials.size());
225 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group1")); 240 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group1"));
226 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 241 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
(...skipping 15 matching lines...) Expand all
242 service.RegisterSyntheticFieldTrial(trial4); 257 service.RegisterSyntheticFieldTrial(trial4);
243 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 258 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
244 EXPECT_EQ(1U, synthetic_trials.size()); 259 EXPECT_EQ(1U, synthetic_trials.size());
245 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 260 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
246 261
247 // Ensure that time has advanced by at least a tick before proceeding. 262 // Ensure that time has advanced by at least a tick before proceeding.
248 WaitUntilTimeChanges(base::TimeTicks::Now()); 263 WaitUntilTimeChanges(base::TimeTicks::Now());
249 264
250 // Start a new log and ensure all three trials appear in it. 265 // Start a new log and ensure all three trials appear in it.
251 service.log_manager_.FinishCurrentLog(); 266 service.log_manager_.FinishCurrentLog();
252 service.log_manager_.BeginLoggingWithLog(scoped_ptr<metrics::MetricsLogBase>( 267 service.log_manager_.BeginLoggingWithLog(
253 new MetricsLog("clientID", 1, MetricsLog::ONGOING_LOG, &client))); 268 scoped_ptr<metrics::MetricsLogBase>(new MetricsLog(
269 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState())));
254 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 270 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
255 EXPECT_EQ(3U, synthetic_trials.size()); 271 EXPECT_EQ(3U, synthetic_trials.size());
256 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); 272 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2"));
257 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 273 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
258 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); 274 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3"));
259 service.log_manager_.FinishCurrentLog(); 275 service.log_manager_.FinishCurrentLog();
260 } 276 }
261 277
262 TEST_F(MetricsServiceTest, MetricsServiceObserver) { 278 TEST_F(MetricsServiceTest, MetricsServiceObserver) {
263 metrics::TestMetricsServiceClient client; 279 metrics::TestMetricsServiceClient client;
264 MetricsService service(GetMetricsStateManager(), &client); 280 MetricsService service(GetMetricsStateManager(), &client, GetLocalState());
265 TestMetricsServiceObserver observer1; 281 TestMetricsServiceObserver observer1;
266 TestMetricsServiceObserver observer2; 282 TestMetricsServiceObserver observer2;
267 283
268 service.AddObserver(&observer1); 284 service.AddObserver(&observer1);
269 EXPECT_EQ(0, observer1.observed()); 285 EXPECT_EQ(0, observer1.observed());
270 EXPECT_EQ(0, observer2.observed()); 286 EXPECT_EQ(0, observer2.observed());
271 287
272 service.OpenNewLog(); 288 service.OpenNewLog();
273 EXPECT_EQ(1, observer1.observed()); 289 EXPECT_EQ(1, observer1.observed());
274 EXPECT_EQ(0, observer2.observed()); 290 EXPECT_EQ(0, observer2.observed());
275 service.log_manager_.FinishCurrentLog(); 291 service.log_manager_.FinishCurrentLog();
276 292
277 service.AddObserver(&observer2); 293 service.AddObserver(&observer2);
278 294
279 service.OpenNewLog(); 295 service.OpenNewLog();
280 EXPECT_EQ(2, observer1.observed()); 296 EXPECT_EQ(2, observer1.observed());
281 EXPECT_EQ(1, observer2.observed()); 297 EXPECT_EQ(1, observer2.observed());
282 service.log_manager_.FinishCurrentLog(); 298 service.log_manager_.FinishCurrentLog();
283 299
284 service.RemoveObserver(&observer1); 300 service.RemoveObserver(&observer1);
285 301
286 service.OpenNewLog(); 302 service.OpenNewLog();
287 EXPECT_EQ(2, observer1.observed()); 303 EXPECT_EQ(2, observer1.observed());
288 EXPECT_EQ(2, observer2.observed()); 304 EXPECT_EQ(2, observer2.observed());
289 service.log_manager_.FinishCurrentLog(); 305 service.log_manager_.FinishCurrentLog();
290 306
291 service.RemoveObserver(&observer2); 307 service.RemoveObserver(&observer2);
292 } 308 }
OLDNEW
« no previous file with comments | « chrome/browser/metrics/metrics_service.cc ('k') | chrome/browser/metrics/metrics_services_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698