| OLD | NEW |
| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 class TestMetricsService : public MetricsService { | 46 class TestMetricsService : public MetricsService { |
| 47 public: | 47 public: |
| 48 TestMetricsService(MetricsStateManager* state_manager, | 48 TestMetricsService(MetricsStateManager* state_manager, |
| 49 MetricsServiceClient* client, | 49 MetricsServiceClient* client, |
| 50 PrefService* local_state) | 50 PrefService* local_state) |
| 51 : MetricsService(state_manager, client, local_state) {} | 51 : MetricsService(state_manager, client, local_state) {} |
| 52 ~TestMetricsService() override {} | 52 ~TestMetricsService() override {} |
| 53 | 53 |
| 54 using MetricsService::log_manager; | 54 using MetricsService::log_manager; |
| 55 using MetricsService::log_store; |
| 55 | 56 |
| 56 private: | 57 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); | 58 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); |
| 58 }; | 59 }; |
| 59 | 60 |
| 60 class TestMetricsLog : public MetricsLog { | 61 class TestMetricsLog : public MetricsLog { |
| 61 public: | 62 public: |
| 62 TestMetricsLog(const std::string& client_id, | 63 TestMetricsLog(const std::string& client_id, |
| 63 int session_id, | 64 int session_id, |
| 64 MetricsServiceClient* client, | 65 MetricsServiceClient* client, |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 TestMetricsService service( | 181 TestMetricsService service( |
| 181 GetMetricsStateManager(), &client, GetLocalState()); | 182 GetMetricsStateManager(), &client, GetLocalState()); |
| 182 | 183 |
| 183 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 184 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 184 service.RegisterMetricsProvider( | 185 service.RegisterMetricsProvider( |
| 185 std::unique_ptr<MetricsProvider>(test_provider)); | 186 std::unique_ptr<MetricsProvider>(test_provider)); |
| 186 | 187 |
| 187 service.InitializeMetricsRecordingState(); | 188 service.InitializeMetricsRecordingState(); |
| 188 | 189 |
| 189 // No initial stability log should be generated. | 190 // No initial stability log should be generated. |
| 190 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); | 191 EXPECT_FALSE(service.log_store()->has_unsent_logs()); |
| 191 EXPECT_FALSE(service.log_manager()->has_staged_log()); | 192 EXPECT_FALSE(service.log_store()->has_staged_log()); |
| 192 | 193 |
| 193 // Ensure that HasInitialStabilityMetrics() is always called on providers, | 194 // Ensure that HasInitialStabilityMetrics() is always called on providers, |
| 194 // for consistency, even if other conditions already indicate their presence. | 195 // for consistency, even if other conditions already indicate their presence. |
| 195 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); | 196 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); |
| 196 | 197 |
| 197 // The test provider should not have been called upon to provide initial | 198 // The test provider should not have been called upon to provide initial |
| 198 // stability nor regular stability metrics. | 199 // stability nor regular stability metrics. |
| 199 EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called()); | 200 EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called()); |
| 200 EXPECT_FALSE(test_provider->provide_stability_metrics_called()); | 201 EXPECT_FALSE(test_provider->provide_stability_metrics_called()); |
| 201 } | 202 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 225 GetMetricsStateManager(), &client, GetLocalState()); | 226 GetMetricsStateManager(), &client, GetLocalState()); |
| 226 // Add a metrics provider that requests a stability log. | 227 // Add a metrics provider that requests a stability log. |
| 227 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 228 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 228 test_provider->set_has_initial_stability_metrics(true); | 229 test_provider->set_has_initial_stability_metrics(true); |
| 229 service.RegisterMetricsProvider( | 230 service.RegisterMetricsProvider( |
| 230 std::unique_ptr<MetricsProvider>(test_provider)); | 231 std::unique_ptr<MetricsProvider>(test_provider)); |
| 231 | 232 |
| 232 service.InitializeMetricsRecordingState(); | 233 service.InitializeMetricsRecordingState(); |
| 233 | 234 |
| 234 // The initial stability log should be generated and persisted in unsent logs. | 235 // The initial stability log should be generated and persisted in unsent logs. |
| 235 MetricsLogManager* log_manager = service.log_manager(); | 236 MetricsLogStore* log_store = service.log_store(); |
| 236 EXPECT_TRUE(log_manager->has_unsent_logs()); | 237 EXPECT_TRUE(log_store->has_unsent_logs()); |
| 237 EXPECT_FALSE(log_manager->has_staged_log()); | 238 EXPECT_FALSE(log_store->has_staged_log()); |
| 238 | 239 |
| 239 // Ensure that HasInitialStabilityMetrics() is always called on providers, | 240 // Ensure that HasInitialStabilityMetrics() is always called on providers, |
| 240 // for consistency, even if other conditions already indicate their presence. | 241 // for consistency, even if other conditions already indicate their presence. |
| 241 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); | 242 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); |
| 242 | 243 |
| 243 // The test provider should have been called upon to provide initial | 244 // The test provider should have been called upon to provide initial |
| 244 // stability and regular stability metrics. | 245 // stability and regular stability metrics. |
| 245 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); | 246 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); |
| 246 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); | 247 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); |
| 247 | 248 |
| 248 // Stage the log and retrieve it. | 249 // Stage the log and retrieve it. |
| 249 log_manager->StageNextLogForUpload(); | 250 log_store->StageNextLog(); |
| 250 EXPECT_TRUE(log_manager->has_staged_log()); | 251 EXPECT_TRUE(log_store->has_staged_log()); |
| 251 | 252 |
| 252 std::string uncompressed_log; | 253 std::string uncompressed_log; |
| 253 EXPECT_TRUE(compression::GzipUncompress(log_manager->staged_log(), | 254 EXPECT_TRUE( |
| 254 &uncompressed_log)); | 255 compression::GzipUncompress(log_store->staged_log(), &uncompressed_log)); |
| 255 | 256 |
| 256 ChromeUserMetricsExtension uma_log; | 257 ChromeUserMetricsExtension uma_log; |
| 257 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); | 258 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); |
| 258 | 259 |
| 259 EXPECT_TRUE(uma_log.has_client_id()); | 260 EXPECT_TRUE(uma_log.has_client_id()); |
| 260 EXPECT_TRUE(uma_log.has_session_id()); | 261 EXPECT_TRUE(uma_log.has_session_id()); |
| 261 EXPECT_TRUE(uma_log.has_system_profile()); | 262 EXPECT_TRUE(uma_log.has_system_profile()); |
| 262 EXPECT_EQ(0, uma_log.user_action_event_size()); | 263 EXPECT_EQ(0, uma_log.user_action_event_size()); |
| 263 EXPECT_EQ(0, uma_log.omnibox_event_size()); | 264 EXPECT_EQ(0, uma_log.omnibox_event_size()); |
| 264 EXPECT_EQ(0, uma_log.profiler_event_size()); | 265 EXPECT_EQ(0, uma_log.profiler_event_size()); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 293 | 294 |
| 294 TestMetricsService service( | 295 TestMetricsService service( |
| 295 GetMetricsStateManager(), &client, GetLocalState()); | 296 GetMetricsStateManager(), &client, GetLocalState()); |
| 296 // Add a provider. | 297 // Add a provider. |
| 297 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 298 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 298 service.RegisterMetricsProvider( | 299 service.RegisterMetricsProvider( |
| 299 std::unique_ptr<MetricsProvider>(test_provider)); | 300 std::unique_ptr<MetricsProvider>(test_provider)); |
| 300 service.InitializeMetricsRecordingState(); | 301 service.InitializeMetricsRecordingState(); |
| 301 | 302 |
| 302 // The initial stability log should be generated and persisted in unsent logs. | 303 // The initial stability log should be generated and persisted in unsent logs. |
| 303 MetricsLogManager* log_manager = service.log_manager(); | 304 MetricsLogStore* log_store = service.log_store(); |
| 304 EXPECT_TRUE(log_manager->has_unsent_logs()); | 305 EXPECT_TRUE(log_store->has_unsent_logs()); |
| 305 EXPECT_FALSE(log_manager->has_staged_log()); | 306 EXPECT_FALSE(log_store->has_staged_log()); |
| 306 | 307 |
| 307 // Ensure that HasInitialStabilityMetrics() is always called on providers, | 308 // Ensure that HasInitialStabilityMetrics() is always called on providers, |
| 308 // for consistency, even if other conditions already indicate their presence. | 309 // for consistency, even if other conditions already indicate their presence. |
| 309 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); | 310 EXPECT_TRUE(test_provider->has_initial_stability_metrics_called()); |
| 310 | 311 |
| 311 // The test provider should have been called upon to provide initial | 312 // The test provider should have been called upon to provide initial |
| 312 // stability and regular stability metrics. | 313 // stability and regular stability metrics. |
| 313 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); | 314 EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called()); |
| 314 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); | 315 EXPECT_TRUE(test_provider->provide_stability_metrics_called()); |
| 315 | 316 |
| 316 // Stage the log and retrieve it. | 317 // Stage the log and retrieve it. |
| 317 log_manager->StageNextLogForUpload(); | 318 log_store->StageNextLog(); |
| 318 EXPECT_TRUE(log_manager->has_staged_log()); | 319 EXPECT_TRUE(log_store->has_staged_log()); |
| 319 | 320 |
| 320 std::string uncompressed_log; | 321 std::string uncompressed_log; |
| 321 EXPECT_TRUE(compression::GzipUncompress(log_manager->staged_log(), | 322 EXPECT_TRUE( |
| 322 &uncompressed_log)); | 323 compression::GzipUncompress(log_store->staged_log(), &uncompressed_log)); |
| 323 | 324 |
| 324 ChromeUserMetricsExtension uma_log; | 325 ChromeUserMetricsExtension uma_log; |
| 325 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); | 326 EXPECT_TRUE(uma_log.ParseFromString(uncompressed_log)); |
| 326 | 327 |
| 327 EXPECT_TRUE(uma_log.has_client_id()); | 328 EXPECT_TRUE(uma_log.has_client_id()); |
| 328 EXPECT_TRUE(uma_log.has_session_id()); | 329 EXPECT_TRUE(uma_log.has_session_id()); |
| 329 EXPECT_TRUE(uma_log.has_system_profile()); | 330 EXPECT_TRUE(uma_log.has_system_profile()); |
| 330 EXPECT_EQ(0, uma_log.user_action_event_size()); | 331 EXPECT_EQ(0, uma_log.user_action_event_size()); |
| 331 EXPECT_EQ(0, uma_log.omnibox_event_size()); | 332 EXPECT_EQ(0, uma_log.omnibox_event_size()); |
| 332 EXPECT_EQ(0, uma_log.profiler_event_size()); | 333 EXPECT_EQ(0, uma_log.profiler_event_size()); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 HashName("Group3")); | 383 HashName("Group3")); |
| 383 service.RegisterSyntheticFieldTrial(trial4); | 384 service.RegisterSyntheticFieldTrial(trial4); |
| 384 service.GetSyntheticFieldTrialsOlderThan(begin_log_time, &synthetic_trials); | 385 service.GetSyntheticFieldTrialsOlderThan(begin_log_time, &synthetic_trials); |
| 385 EXPECT_EQ(1U, synthetic_trials.size()); | 386 EXPECT_EQ(1U, synthetic_trials.size()); |
| 386 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 387 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
| 387 | 388 |
| 388 // Ensure that time has advanced by at least a tick before proceeding. | 389 // Ensure that time has advanced by at least a tick before proceeding. |
| 389 WaitUntilTimeChanges(base::TimeTicks::Now()); | 390 WaitUntilTimeChanges(base::TimeTicks::Now()); |
| 390 | 391 |
| 391 // Start a new log and ensure all three trials appear in it. | 392 // Start a new log and ensure all three trials appear in it. |
| 392 service.log_manager_.FinishCurrentLog(); | 393 service.log_manager_.FinishCurrentLog(service.log_store()); |
| 393 service.log_manager_.BeginLoggingWithLog( | 394 service.log_manager_.BeginLoggingWithLog( |
| 394 std::unique_ptr<MetricsLog>(new MetricsLog( | 395 std::unique_ptr<MetricsLog>(new MetricsLog( |
| 395 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState()))); | 396 "clientID", 1, MetricsLog::ONGOING_LOG, &client, GetLocalState()))); |
| 396 service.GetSyntheticFieldTrialsOlderThan( | 397 service.GetSyntheticFieldTrialsOlderThan( |
| 397 service.log_manager_.current_log()->creation_time(), &synthetic_trials); | 398 service.log_manager_.current_log()->creation_time(), &synthetic_trials); |
| 398 EXPECT_EQ(3U, synthetic_trials.size()); | 399 EXPECT_EQ(3U, synthetic_trials.size()); |
| 399 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); | 400 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); |
| 400 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 401 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
| 401 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); | 402 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); |
| 402 service.log_manager_.FinishCurrentLog(); | 403 service.log_manager_.FinishCurrentLog(service.log_store()); |
| 403 } | 404 } |
| 404 | 405 |
| 405 TEST_F(MetricsServiceTest, RegisterSyntheticMultiGroupFieldTrial) { | 406 TEST_F(MetricsServiceTest, RegisterSyntheticMultiGroupFieldTrial) { |
| 406 TestMetricsServiceClient client; | 407 TestMetricsServiceClient client; |
| 407 MetricsService service(GetMetricsStateManager(), &client, GetLocalState()); | 408 MetricsService service(GetMetricsStateManager(), &client, GetLocalState()); |
| 408 | 409 |
| 409 // Register a synthetic trial TestTrial1 with groups A and B. | 410 // Register a synthetic trial TestTrial1 with groups A and B. |
| 410 uint32_t trial_name_hash = HashName("TestTrial1"); | 411 uint32_t trial_name_hash = HashName("TestTrial1"); |
| 411 std::vector<uint32_t> group_name_hashes = {HashName("A"), HashName("B")}; | 412 std::vector<uint32_t> group_name_hashes = {HashName("A"), HashName("B")}; |
| 412 service.RegisterSyntheticMultiGroupFieldTrial(trial_name_hash, | 413 service.RegisterSyntheticMultiGroupFieldTrial(trial_name_hash, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 439 | 440 |
| 440 // Register a trial with no groups, which should effectively remove the trial. | 441 // Register a trial with no groups, which should effectively remove the trial. |
| 441 group_name_hashes.clear(); | 442 group_name_hashes.clear(); |
| 442 service.RegisterSyntheticMultiGroupFieldTrial(trial_name_hash, | 443 service.RegisterSyntheticMultiGroupFieldTrial(trial_name_hash, |
| 443 group_name_hashes); | 444 group_name_hashes); |
| 444 // Ensure that time has advanced by at least a tick before proceeding. | 445 // Ensure that time has advanced by at least a tick before proceeding. |
| 445 WaitUntilTimeChanges(base::TimeTicks::Now()); | 446 WaitUntilTimeChanges(base::TimeTicks::Now()); |
| 446 | 447 |
| 447 service.GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(), | 448 service.GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(), |
| 448 &synthetic_trials); | 449 &synthetic_trials); |
| 449 service.log_manager_.FinishCurrentLog(); | 450 service.log_manager_.FinishCurrentLog(service.log_store()); |
| 450 } | 451 } |
| 451 | 452 |
| 452 TEST_F(MetricsServiceTest, | 453 TEST_F(MetricsServiceTest, |
| 453 MetricsProviderOnRecordingDisabledCalledOnInitialStop) { | 454 MetricsProviderOnRecordingDisabledCalledOnInitialStop) { |
| 454 TestMetricsServiceClient client; | 455 TestMetricsServiceClient client; |
| 455 TestMetricsService service( | 456 TestMetricsService service( |
| 456 GetMetricsStateManager(), &client, GetLocalState()); | 457 GetMetricsStateManager(), &client, GetLocalState()); |
| 457 | 458 |
| 458 TestMetricsProvider* test_provider = new TestMetricsProvider(); | 459 TestMetricsProvider* test_provider = new TestMetricsProvider(); |
| 459 service.RegisterMetricsProvider( | 460 service.RegisterMetricsProvider( |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 client.uploader()->CompleteUpload(200); | 549 client.uploader()->CompleteUpload(200); |
| 549 EXPECT_FALSE(client.uploader()->is_uploading()); | 550 EXPECT_FALSE(client.uploader()->is_uploading()); |
| 550 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); | 551 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); |
| 551 // Upload should start. | 552 // Upload should start. |
| 552 task_runner_->RunPendingTasks(); | 553 task_runner_->RunPendingTasks(); |
| 553 EXPECT_TRUE(client.uploader()->is_uploading()); | 554 EXPECT_TRUE(client.uploader()->is_uploading()); |
| 554 EXPECT_EQ(0U, task_runner_->NumPendingTasks()); | 555 EXPECT_EQ(0U, task_runner_->NumPendingTasks()); |
| 555 } | 556 } |
| 556 | 557 |
| 557 } // namespace metrics | 558 } // namespace metrics |
| OLD | NEW |