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

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

Issue 866163002: Allow use of metrics url override outside of Chromecast (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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') | components/metrics/metrics_switches.h » ('j') | 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/command_line.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/statistics_recorder.h" 13 #include "base/metrics/statistics_recorder.h"
13 #include "base/prefs/testing_pref_service.h" 14 #include "base/prefs/testing_pref_service.h"
15 #include "base/strings/string16.h"
14 #include "base/threading/platform_thread.h" 16 #include "base/threading/platform_thread.h"
15 #include "components/metrics/client_info.h" 17 #include "components/metrics/client_info.h"
16 #include "components/metrics/compression_utils.h" 18 #include "components/metrics/compression_utils.h"
17 #include "components/metrics/metrics_hashes.h" 19 #include "components/metrics/metrics_hashes.h"
18 #include "components/metrics/metrics_log.h" 20 #include "components/metrics/metrics_log.h"
21 #include "components/metrics/metrics_log_uploader.h"
19 #include "components/metrics/metrics_pref_names.h" 22 #include "components/metrics/metrics_pref_names.h"
20 #include "components/metrics/metrics_state_manager.h" 23 #include "components/metrics/metrics_state_manager.h"
24 #include "components/metrics/metrics_switches.h"
25 #include "components/metrics/mock_metrics_service_client.h"
26 #include "components/metrics/proto/system_profile.pb.h"
21 #include "components/metrics/test_metrics_service_client.h" 27 #include "components/metrics/test_metrics_service_client.h"
22 #include "components/variations/metrics_util.h" 28 #include "components/variations/metrics_util.h"
29 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
24 31
32 using ::testing::Return;
33 using ::testing::_;
34
25 namespace metrics { 35 namespace metrics {
26 36
27 namespace { 37 namespace {
28 38
29 void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) { 39 void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) {
30 } 40 }
31 41
32 scoped_ptr<ClientInfo> ReturnNoBackup() { 42 scoped_ptr<ClientInfo> ReturnNoBackup() {
33 return scoped_ptr<ClientInfo>(); 43 return scoped_ptr<ClientInfo>();
34 } 44 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 MetricsLog::ONGOING_LOG, 93 MetricsLog::ONGOING_LOG,
84 client, 94 client,
85 local_state) {} 95 local_state) {}
86 96
87 ~TestMetricsLog() override {} 97 ~TestMetricsLog() override {}
88 98
89 private: 99 private:
90 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); 100 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog);
91 }; 101 };
92 102
103 class TestMetricsLogUploader : public MetricsLogUploader {
104 public:
105 TestMetricsLogUploader(
106 const std::string& server_url,
107 const std::string& mime_type,
108 const base::Callback<void(int)>& on_upload_complete):
109 MetricsLogUploader(server_url, mime_type, on_upload_complete) {}
110
111 ~TestMetricsLogUploader() override {}
112
113 bool UploadLog(const std::string& compressed_log_data,
114 const std::string& log_hash) override {
115 return true;
116 }
117
118 private:
119 DISALLOW_COPY_AND_ASSIGN(TestMetricsLogUploader);
120 };
121
93 class MetricsServiceTest : public testing::Test { 122 class MetricsServiceTest : public testing::Test {
94 public: 123 public:
95 MetricsServiceTest() : is_metrics_reporting_enabled_(false) { 124 MetricsServiceTest() : is_metrics_reporting_enabled_(false) {
96 MetricsService::RegisterPrefs(testing_local_state_.registry()); 125 MetricsService::RegisterPrefs(testing_local_state_.registry());
97 metrics_state_manager_ = MetricsStateManager::Create( 126 metrics_state_manager_ = MetricsStateManager::Create(
98 GetLocalState(), 127 GetLocalState(),
99 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled, 128 base::Bind(&MetricsServiceTest::is_metrics_reporting_enabled,
100 base::Unretained(this)), 129 base::Unretained(this)),
101 base::Bind(&StoreNoClientInfoBackup), 130 base::Bind(&StoreNoClientInfoBackup),
102 base::Bind(&ReturnNoBackup)); 131 base::Bind(&ReturnNoBackup));
103 } 132 }
104 133
105 ~MetricsServiceTest() override { 134 ~MetricsServiceTest() override {
106 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE, 135 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE,
107 GetLocalState()); 136 GetLocalState());
108 } 137 }
109 138
110 MetricsStateManager* GetMetricsStateManager() { 139 MetricsStateManager* GetMetricsStateManager() {
111 return metrics_state_manager_.get(); 140 return metrics_state_manager_.get();
112 } 141 }
113 142
114 PrefService* GetLocalState() { return &testing_local_state_; } 143 PrefService* GetLocalState() { return &testing_local_state_; }
115 144
116 // Sets metrics reporting as enabled for testing. 145 // Sets metrics reporting as enabled for testing.
117 void EnableMetricsReporting() { 146 void EnableMetricsReporting() {
118 is_metrics_reporting_enabled_ = true; 147 is_metrics_reporting_enabled_ = true;
119 } 148 }
120 149
150 void PopulateStabilityLog(MetricsServiceClient& client,
151 bool exited_cleanly) {
152 // Record stability build time and version from previous session, so that
153 // stability metrics (including exited cleanly flag) won't be cleared.
154 testing_local_state_.SetInt64(prefs::kStabilityStatsBuildTime,
155 MetricsLog::GetBuildTime());
156 testing_local_state_.SetString(prefs::kStabilityStatsVersion,
157 client.GetVersionString());
158
159 // Set the clean exit flag, as that will otherwise cause a stabilty
160 // log to be produced, irrespective provider requests.
161 testing_local_state_.SetBoolean(
162 prefs::kStabilityExitedCleanly, exited_cleanly);
163 }
164
121 // Waits until base::TimeTicks::Now() no longer equals |value|. This should 165 // Waits until base::TimeTicks::Now() no longer equals |value|. This should
122 // take between 1-15ms per the documented resolution of base::TimeTicks. 166 // take between 1-15ms per the documented resolution of base::TimeTicks.
123 void WaitUntilTimeChanges(const base::TimeTicks& value) { 167 void WaitUntilTimeChanges(const base::TimeTicks& value) {
124 while (base::TimeTicks::Now() == value) { 168 while (base::TimeTicks::Now() == value) {
125 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); 169 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
126 } 170 }
127 } 171 }
128 172
129 // Returns true if there is a synthetic trial in the given vector that matches 173 // Returns true if there is a synthetic trial in the given vector that matches
130 // the given trial name and trial group; returns false otherwise. 174 // the given trial name and trial group; returns false otherwise.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 EnableMetricsReporting(); 254 EnableMetricsReporting();
211 255
212 // Save an existing system profile to prefs, to correspond to what would be 256 // Save an existing system profile to prefs, to correspond to what would be
213 // saved from a previous session. 257 // saved from a previous session.
214 TestMetricsServiceClient client; 258 TestMetricsServiceClient client;
215 TestMetricsLog log("client", 1, &client, GetLocalState()); 259 TestMetricsLog log("client", 1, &client, GetLocalState());
216 log.RecordEnvironment(std::vector<MetricsProvider*>(), 260 log.RecordEnvironment(std::vector<MetricsProvider*>(),
217 std::vector<variations::ActiveGroupId>(), 261 std::vector<variations::ActiveGroupId>(),
218 0); 262 0);
219 263
220 // Record stability build time and version from previous session, so that 264 PopulateStabilityLog(client, true);
221 // stability metrics (including exited cleanly flag) won't be cleared.
222 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime,
223 MetricsLog::GetBuildTime());
224 GetLocalState()->SetString(prefs::kStabilityStatsVersion,
225 client.GetVersionString());
226
227 // Set the clean exit flag, as that will otherwise cause a stabilty
228 // log to be produced, irrespective provider requests.
229 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true);
230 265
231 TestMetricsService service( 266 TestMetricsService service(
232 GetMetricsStateManager(), &client, GetLocalState()); 267 GetMetricsStateManager(), &client, GetLocalState());
233 // Add a metrics provider that requests a stability log. 268 // Add a metrics provider that requests a stability log.
234 TestMetricsProvider* test_provider = new TestMetricsProvider(true); 269 TestMetricsProvider* test_provider = new TestMetricsProvider(true);
235 service.RegisterMetricsProvider( 270 service.RegisterMetricsProvider(
236 scoped_ptr<MetricsProvider>(test_provider)); 271 scoped_ptr<MetricsProvider>(test_provider));
237 272
238 service.InitializeMetricsRecordingState(); 273 service.InitializeMetricsRecordingState();
239 274
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 310
276 // Set up prefs to simulate restarting after a crash. 311 // Set up prefs to simulate restarting after a crash.
277 312
278 // Save an existing system profile to prefs, to correspond to what would be 313 // Save an existing system profile to prefs, to correspond to what would be
279 // saved from a previous session. 314 // saved from a previous session.
280 TestMetricsServiceClient client; 315 TestMetricsServiceClient client;
281 TestMetricsLog log("client", 1, &client, GetLocalState()); 316 TestMetricsLog log("client", 1, &client, GetLocalState());
282 log.RecordEnvironment(std::vector<MetricsProvider*>(), 317 log.RecordEnvironment(std::vector<MetricsProvider*>(),
283 std::vector<variations::ActiveGroupId>(), 318 std::vector<variations::ActiveGroupId>(),
284 0); 319 0);
285 320 PopulateStabilityLog(client, false);
286 // Record stability build time and version from previous session, so that
287 // stability metrics (including exited cleanly flag) won't be cleared.
288 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime,
289 MetricsLog::GetBuildTime());
290 GetLocalState()->SetString(prefs::kStabilityStatsVersion,
291 client.GetVersionString());
292
293 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false);
294 321
295 TestMetricsService service( 322 TestMetricsService service(
296 GetMetricsStateManager(), &client, GetLocalState()); 323 GetMetricsStateManager(), &client, GetLocalState());
297 service.InitializeMetricsRecordingState(); 324 service.InitializeMetricsRecordingState();
298 325
299 // The initial stability log should be generated and persisted in unsent logs. 326 // The initial stability log should be generated and persisted in unsent logs.
300 MetricsLogManager* log_manager = service.log_manager(); 327 MetricsLogManager* log_manager = service.log_manager();
301 EXPECT_TRUE(log_manager->has_unsent_logs()); 328 EXPECT_TRUE(log_manager->has_unsent_logs());
302 EXPECT_FALSE(log_manager->has_staged_log()); 329 EXPECT_FALSE(log_manager->has_staged_log());
303 330
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 scoped_ptr<MetricsLog>(new MetricsLog( 406 scoped_ptr<MetricsLog>(new MetricsLog(
380 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState()))); 407 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState())));
381 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); 408 service.GetCurrentSyntheticFieldTrials(&synthetic_trials);
382 EXPECT_EQ(3U, synthetic_trials.size()); 409 EXPECT_EQ(3U, synthetic_trials.size());
383 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); 410 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2"));
384 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); 411 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
385 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); 412 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3"));
386 service.log_manager_.FinishCurrentLog(); 413 service.log_manager_.FinishCurrentLog();
387 } 414 }
388 415
416 TEST_F(MetricsServiceTest, ServerUrlOverrideSet) {
417 EnableMetricsReporting();
418
419 std::string server_url = "https://127.0.0.1/v2";
420 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
421 // This is the switch that we want to test.
422 command_line->AppendSwitchASCII(switches::kOverrideMetricsUploadUrl,
423 server_url);
424
425 MockMetricsServiceClient client;
426 EXPECT_CALL(client, GetRegistryBackupKey())
427 .WillOnce(Return(base::string16()));
428 EXPECT_CALL(client, GetChannel())
429 .WillRepeatedly(Return(SystemProfileProto::CHANNEL_UNKNOWN));
430 // The main expectation of this test: ensures that when the command line
431 // switch is set, the uploader is created with the correct url.
432 EXPECT_CALL(client, MockCreateUploader(server_url, _, _))
433 .WillOnce(Return(new TestMetricsLogUploader(server_url, "mime_type",
434 base::Bind(&MockMetricsServiceClient::DoNothing,
435 base::Unretained(&client)))));
436
437 // Generate a set of logs for upload
438 TestMetricsLog log("client", 1, &client, GetLocalState());
439 log.RecordEnvironment(std::vector<MetricsProvider*>(),
440 std::vector<variations::ActiveGroupId>(),
441 0);
442 PopulateStabilityLog(client, true);
443
444 MetricsService service(GetMetricsStateManager(), &client, GetLocalState());
445 TestMetricsProvider* test_provider = new TestMetricsProvider(true);
446 service.RegisterMetricsProvider(
447 scoped_ptr<MetricsProvider>(test_provider));
448 service.InitializeMetricsRecordingState();
449 EXPECT_TRUE(service.log_manager()->has_unsent_logs());
450
451 service.log_manager()->StageNextLogForUpload();
452 EXPECT_TRUE(service.log_manager()->has_staged_log());
453
454 service.SendStagedLog();
455 }
456
389 } // namespace metrics 457 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/metrics_service.cc ('k') | components/metrics/metrics_switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698