OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/doodle/doodle_service.h" | 5 #include "components/doodle/doodle_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/test/histogram_tester.h" | |
14 #include "base/test/simple_test_tick_clock.h" | 15 #include "base/test/simple_test_tick_clock.h" |
15 #include "base/test/test_mock_time_task_runner.h" | 16 #include "base/test/test_mock_time_task_runner.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "components/prefs/testing_pref_service.h" | 19 #include "components/prefs/testing_pref_service.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 using testing::Eq; | 23 using testing::Eq; |
23 using testing::StrictMock; | 24 using testing::StrictMock; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 void RecreateService() { | 83 void RecreateService() { |
83 auto expiry_timer = base::MakeUnique<base::OneShotTimer>(tick_clock_.get()); | 84 auto expiry_timer = base::MakeUnique<base::OneShotTimer>(tick_clock_.get()); |
84 expiry_timer->SetTaskRunner(task_runner_); | 85 expiry_timer->SetTaskRunner(task_runner_); |
85 expiry_timer_ = expiry_timer.get(); | 86 expiry_timer_ = expiry_timer.get(); |
86 | 87 |
87 auto fetcher = base::MakeUnique<FakeDoodleFetcher>(); | 88 auto fetcher = base::MakeUnique<FakeDoodleFetcher>(); |
88 fetcher_ = fetcher.get(); | 89 fetcher_ = fetcher.get(); |
89 | 90 |
90 service_ = base::MakeUnique<DoodleService>( | 91 service_ = base::MakeUnique<DoodleService>( |
91 &pref_service_, std::move(fetcher), std::move(expiry_timer), | 92 &pref_service_, std::move(fetcher), std::move(expiry_timer), |
92 task_runner_->GetMockClock()); | 93 task_runner_->GetMockClock(), task_runner_->GetMockTickClock()); |
93 } | 94 } |
94 | 95 |
95 DoodleService* service() { return service_.get(); } | 96 DoodleService* service() { return service_.get(); } |
96 FakeDoodleFetcher* fetcher() { return fetcher_; } | 97 FakeDoodleFetcher* fetcher() { return fetcher_; } |
97 | 98 |
98 base::TestMockTimeTaskRunner* task_runner() { return task_runner_.get(); } | 99 base::TestMockTimeTaskRunner* task_runner() { return task_runner_.get(); } |
99 | 100 |
100 private: | 101 private: |
101 TestingPrefServiceSimple pref_service_; | 102 TestingPrefServiceSimple pref_service_; |
102 | 103 |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 // Now load an expired config again. The cached one should go away. | 350 // Now load an expired config again. The cached one should go away. |
350 service()->Refresh(); | 351 service()->Refresh(); |
351 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt))); | 352 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(base::nullopt))); |
352 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, | 353 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, |
353 base::TimeDelta::FromSeconds(0), config); | 354 base::TimeDelta::FromSeconds(0), config); |
354 | 355 |
355 // Remove the observer before the service gets destroyed. | 356 // Remove the observer before the service gets destroyed. |
356 service()->RemoveObserver(&observer); | 357 service()->RemoveObserver(&observer); |
357 } | 358 } |
358 | 359 |
360 TEST_F(DoodleServiceTest, RecordsMetricsForSuccessfulDownload) { | |
361 base::HistogramTester histograms; | |
362 | |
363 // Load a doodle config as usual, but let it take some time. | |
364 service()->Refresh(); | |
365 task_runner()->FastForwardBy(base::TimeDelta::FromSeconds(5)); | |
366 DoodleConfig config = CreateConfig(DoodleType::SIMPLE); | |
367 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, | |
368 base::TimeDelta::FromHours(1), config); | |
369 ASSERT_THAT(service()->config(), Eq(config)); | |
370 | |
371 // Metrics should have been recorded. | |
fhorschig
2017/03/22 09:47:10
nitty nit: This is pretty obvious ;)
The later com
Marc Treib
2017/03/22 10:47:22
True, but I still like it to visually separate the
| |
372 histograms.ExpectUniqueSample("Doodle.ConfigDownloadOutcome", | |
373 0, // OUTCOME_NEW_DOODLE | |
374 1); | |
375 histograms.ExpectTotalCount("Doodle.ConfigDownloadTime", 1); | |
376 histograms.ExpectTimeBucketCount("Doodle.ConfigDownloadTime", | |
377 base::TimeDelta::FromSeconds(5), 1); | |
378 } | |
379 | |
380 TEST_F(DoodleServiceTest, RecordsMetricsForEmptyDownload) { | |
381 base::HistogramTester histograms; | |
382 | |
383 // Send a "no doodle" response after some time. | |
384 service()->Refresh(); | |
385 task_runner()->FastForwardBy(base::TimeDelta::FromSeconds(5)); | |
386 fetcher()->ServeAllCallbacks(DoodleState::NO_DOODLE, base::TimeDelta(), | |
387 base::nullopt); | |
388 ASSERT_THAT(service()->config(), Eq(base::nullopt)); | |
389 | |
390 // Metrics should have been recorded. | |
fhorschig
2017/03/22 09:47:10
The obvious comment nit again.
| |
391 histograms.ExpectUniqueSample("Doodle.ConfigDownloadOutcome", | |
392 3, // OUTCOME_NO_DOODLE | |
393 1); | |
394 histograms.ExpectTotalCount("Doodle.ConfigDownloadTime", 1); | |
395 histograms.ExpectTimeBucketCount("Doodle.ConfigDownloadTime", | |
396 base::TimeDelta::FromSeconds(5), 1); | |
397 } | |
398 | |
399 TEST_F(DoodleServiceTest, RecordsMetricsForFailedDownload) { | |
400 base::HistogramTester histograms; | |
401 | |
402 // Let the download fail after some time. | |
403 service()->Refresh(); | |
404 task_runner()->FastForwardBy(base::TimeDelta::FromSeconds(5)); | |
405 fetcher()->ServeAllCallbacks(DoodleState::DOWNLOAD_ERROR, base::TimeDelta(), | |
406 base::nullopt); | |
407 ASSERT_THAT(service()->config(), Eq(base::nullopt)); | |
408 | |
409 // The outcome should have been recorded, but not the time - we only record | |
410 // that for successful downloads. | |
411 histograms.ExpectUniqueSample("Doodle.ConfigDownloadOutcome", | |
412 5, // OUTCOME_DOWNLOAD_ERROR | |
413 1); | |
414 histograms.ExpectTotalCount("Doodle.ConfigDownloadTime", 0); | |
415 } | |
416 | |
417 TEST_F(DoodleServiceTest, DoesNotRecordMetricsAtStartup) { | |
418 // Creating the service should not emit any histogram samples. | |
419 base::HistogramTester histograms; | |
420 RecreateService(); | |
421 ASSERT_THAT(service()->config(), Eq(base::nullopt)); | |
422 histograms.ExpectTotalCount("Doodle.ConfigDownloadOutcome", 0); | |
423 histograms.ExpectTotalCount("Doodle.ConfigDownloadTime", 0); | |
424 } | |
425 | |
426 TEST_F(DoodleServiceTest, DoesNotRecordMetricsAtStartupWithConfig) { | |
427 // Load a doodle config as usual. | |
428 service()->Refresh(); | |
429 DoodleConfig config = CreateConfig(DoodleType::SIMPLE); | |
430 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, | |
431 base::TimeDelta::FromHours(1), config); | |
432 ASSERT_THAT(service()->config(), Eq(config)); | |
433 | |
434 // Recreating the service should not emit any histogram samples, even though | |
435 // a config gets loaded. | |
436 base::HistogramTester histograms; | |
437 RecreateService(); | |
438 ASSERT_THAT(service()->config(), Eq(config)); | |
439 histograms.ExpectTotalCount("Doodle.ConfigDownloadOutcome", 0); | |
440 histograms.ExpectTotalCount("Doodle.ConfigDownloadTime", 0); | |
441 } | |
442 | |
443 TEST_F(DoodleServiceTest, DoesNotRecordMetricsWhenConfigExpires) { | |
444 // Load some doodle config. | |
445 service()->Refresh(); | |
446 DoodleConfig config = CreateConfig(DoodleType::SIMPLE); | |
447 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, | |
448 base::TimeDelta::FromHours(1), config); | |
449 ASSERT_THAT(service()->config(), Eq(config)); | |
450 | |
451 base::HistogramTester histograms; | |
452 | |
453 // Fast-forward time so that the config expires. | |
454 task_runner()->FastForwardBy(base::TimeDelta::FromHours(1)); | |
455 ASSERT_THAT(service()->config(), Eq(base::nullopt)); | |
456 | |
457 // This should not have resulted in any metrics being emitted. | |
458 histograms.ExpectTotalCount("Doodle.ConfigDownloadOutcome", 0); | |
459 histograms.ExpectTotalCount("Doodle.ConfigDownloadTime", 0); | |
460 } | |
461 | |
359 } // namespace doodle | 462 } // namespace doodle |
OLD | NEW |