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

Side by Side Diff: components/metrics/metrics_service_unittest.cc

Issue 558653002: Allow MetricsProviders to request an initial stability log. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Move all MetricsProvider implementation to .cc file. Created 6 years, 3 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
« no previous file with comments | « components/metrics/metrics_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/metrics/metrics_service.h" 5 #include "components/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/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 13 matching lines...) Expand all
24 24
25 namespace { 25 namespace {
26 26
27 void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) { 27 void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) {
28 } 28 }
29 29
30 scoped_ptr<ClientInfo> ReturnNoBackup() { 30 scoped_ptr<ClientInfo> ReturnNoBackup() {
31 return scoped_ptr<ClientInfo>(); 31 return scoped_ptr<ClientInfo>();
32 } 32 }
33 33
34 class TestMetricsProvider : public metrics::MetricsProvider {
35 public:
36 explicit TestMetricsProvider(bool has_stability_metrics) :
37 has_stability_metrics_(has_stability_metrics),
38 provide_stability_metrics_called_(false) {
39 }
40
41 virtual bool HasStabilityMetrics() OVERRIDE { return has_stability_metrics_; }
42 virtual void ProvideStabilityMetrics(
43 SystemProfileProto* system_profile_proto) OVERRIDE {
44 provide_stability_metrics_called_ = true;
45 }
46
47 bool provide_stability_metrics_called() const {
48 return provide_stability_metrics_called_;
49 }
50
51 private:
52 bool has_stability_metrics_;
53 bool provide_stability_metrics_called_;
54
55 DISALLOW_COPY_AND_ASSIGN(TestMetricsProvider);
56 };
57
34 class TestMetricsService : public MetricsService { 58 class TestMetricsService : public MetricsService {
35 public: 59 public:
36 TestMetricsService(MetricsStateManager* state_manager, 60 TestMetricsService(MetricsStateManager* state_manager,
37 MetricsServiceClient* client, 61 MetricsServiceClient* client,
38 PrefService* local_state) 62 PrefService* local_state)
39 : MetricsService(state_manager, client, local_state) {} 63 : MetricsService(state_manager, client, local_state) {}
40 virtual ~TestMetricsService() {} 64 virtual ~TestMetricsService() {}
41 65
42 using MetricsService::log_manager; 66 using MetricsService::log_manager;
43 67
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 155
132 } // namespace 156 } // namespace
133 157
134 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { 158 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) {
135 EnableMetricsReporting(); 159 EnableMetricsReporting();
136 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); 160 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true);
137 161
138 TestMetricsServiceClient client; 162 TestMetricsServiceClient client;
139 TestMetricsService service( 163 TestMetricsService service(
140 GetMetricsStateManager(), &client, GetLocalState()); 164 GetMetricsStateManager(), &client, GetLocalState());
165
166 TestMetricsProvider* test_provider = new TestMetricsProvider(false);
167 service.RegisterMetricsProvider(
168 scoped_ptr<metrics::MetricsProvider>(test_provider));
169
141 service.InitializeMetricsRecordingState(); 170 service.InitializeMetricsRecordingState();
142 // No initial stability log should be generated. 171 // No initial stability log should be generated.
143 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); 172 EXPECT_FALSE(service.log_manager()->has_unsent_logs());
144 EXPECT_FALSE(service.log_manager()->has_staged_log()); 173 EXPECT_FALSE(service.log_manager()->has_staged_log());
174
175 // The test provider should not have been called upon to provide stability
176 // metrics.
177 EXPECT_FALSE(test_provider->provide_stability_metrics_called());
178 }
179
180 TEST_F(MetricsServiceTest, InitialStabilityLogAtProviderRequest) {
181 EnableMetricsReporting();
182
183 // Save an existing system profile to prefs, to correspond to what would be
184 // saved from a previous session.
185 TestMetricsServiceClient client;
186 TestMetricsLog log("client", 1, &client, GetLocalState());
187 log.RecordEnvironment(std::vector<metrics::MetricsProvider*>(),
188 std::vector<variations::ActiveGroupId>(),
189 0);
190
191 // Record stability build time and version from previous session, so that
192 // stability metrics (including exited cleanly flag) won't be cleared.
193 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime,
194 MetricsLog::GetBuildTime());
195 GetLocalState()->SetString(prefs::kStabilityStatsVersion,
196 client.GetVersionString());
197
198 // Set the clean exit flag, as that will otherwise cause a stabilty
199 // log to be produced, irrespective provider requests.
200 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true);
201
202 TestMetricsService service(
203 GetMetricsStateManager(), &client, GetLocalState());
204 // Add a metrics provider that requests a stability log.
205 TestMetricsProvider* test_provider = new TestMetricsProvider(true);
206 service.RegisterMetricsProvider(
207 scoped_ptr<MetricsProvider>(test_provider));
208
209 service.InitializeMetricsRecordingState();
210
211 // The initial stability log should be generated and persisted in unsent logs.
212 MetricsLogManager* log_manager = service.log_manager();
213 EXPECT_TRUE(log_manager->has_unsent_logs());
214 EXPECT_FALSE(log_manager->has_staged_log());
215
216 // The test provider should have been called upon to provide stability
217 // metrics.
218 EXPECT_TRUE(test_provider->provide_stability_metrics_called());
219
220 // Stage the log and retrieve it.
221 log_manager->StageNextLogForUpload();
222 EXPECT_TRUE(log_manager->has_staged_log());
223
224 std::string uncompressed_log;
225 EXPECT_TRUE(GzipUncompress(log_manager->staged_log(),
226 &uncompressed_log));
227
228 ChromeUserMetricsExtension uma_log;
229 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log));
230
231 EXPECT_TRUE(uma_log.has_client_id());
232 EXPECT_TRUE(uma_log.has_session_id());
233 EXPECT_TRUE(uma_log.has_system_profile());
234 EXPECT_EQ(0, uma_log.user_action_event_size());
235 EXPECT_EQ(0, uma_log.omnibox_event_size());
236 EXPECT_EQ(0, uma_log.histogram_event_size());
237 EXPECT_EQ(0, uma_log.profiler_event_size());
238 EXPECT_EQ(0, uma_log.perf_data_size());
239
240 // As there wasn't an unclean shutdown, this log has zero crash count.
241 EXPECT_EQ(0, uma_log.system_profile().stability().crash_count());
145 } 242 }
146 243
147 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { 244 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) {
148 EnableMetricsReporting(); 245 EnableMetricsReporting();
149 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly); 246 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly);
150 247
151 // Set up prefs to simulate restarting after a crash. 248 // Set up prefs to simulate restarting after a crash.
152 249
153 // Save an existing system profile to prefs, to correspond to what would be 250 // Save an existing system profile to prefs, to correspond to what would be
154 // saved from a previous session. 251 // saved from a previous session.
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState()))); 357 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState())));
261 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 358 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
262 EXPECT_EQ(3U, synthetic_trials.size()); 359 EXPECT_EQ(3U, synthetic_trials.size());
263 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); 360 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2"));
264 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 361 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
265 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); 362 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3"));
266 service.log_manager_.FinishCurrentLog(); 363 service.log_manager_.FinishCurrentLog();
267 } 364 }
268 365
269 } // namespace metrics 366 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/metrics_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698