| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/service_worker/service_worker_lifetime_tracker.h" |
| 6 |
| 7 #include "base/test/histogram_tester.h" |
| 8 #include "base/test/simple_test_tick_clock.h" |
| 9 #include "base/time/tick_clock.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "content/public/test/test_utils.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 class ServiceWorkerLifetimeTrackerTest : public testing::Test { |
| 17 public: |
| 18 ServiceWorkerLifetimeTrackerTest() |
| 19 : tick_clock_(new base::SimpleTestTickClock()), |
| 20 tracker_(base::WrapUnique(tick_clock_)) {} |
| 21 |
| 22 base::SimpleTestTickClock* tick_clock() { return tick_clock_; } |
| 23 ServiceWorkerLifetimeTracker* tracker() { return &tracker_; } |
| 24 |
| 25 void AdvanceAndRunTimer(bool expect_running) { |
| 26 // Advance to the scheduled time and run the timer task. |
| 27 tick_clock()->SetNowTicks(tracker()->timer_.desired_run_time()); |
| 28 tracker()->timer_.user_task().Run(); |
| 29 |
| 30 // The timer's desired run time is still unchanged here. So reset it if the |
| 31 // timer is still running. |
| 32 if (expect_running) { |
| 33 EXPECT_TRUE(tracker()->timer_.IsRunning()); |
| 34 tracker()->timer_.Reset(); |
| 35 } else { |
| 36 EXPECT_FALSE(tracker()->timer_.IsRunning()); |
| 37 } |
| 38 } |
| 39 |
| 40 private: |
| 41 TestBrowserThreadBundle thread_bundle_; |
| 42 |
| 43 // Not owned. |
| 44 base::SimpleTestTickClock* tick_clock_; |
| 45 |
| 46 ServiceWorkerLifetimeTracker tracker_; |
| 47 }; |
| 48 |
| 49 TEST_F(ServiceWorkerLifetimeTrackerTest, Metrics) { |
| 50 int64_t kVersion1 = 13; // dummy value |
| 51 int64_t kVersion2 = 14; // dummy value |
| 52 |
| 53 tick_clock()->SetNowTicks(base::TimeTicks::Now()); |
| 54 |
| 55 // Start a worker to kick off the timer. |
| 56 tracker()->StartTiming(kVersion2); |
| 57 |
| 58 // Run a worker for 10 seconds. |
| 59 { |
| 60 base::HistogramTester metrics; |
| 61 tracker()->StartTiming(kVersion1); |
| 62 tick_clock()->Advance(base::TimeDelta::FromSeconds(10)); |
| 63 tracker()->StopTiming(kVersion1); |
| 64 metrics.ExpectTimeBucketCount("ServiceWorker.Runtime", |
| 65 base::TimeDelta::FromSeconds(10), 1); |
| 66 } |
| 67 |
| 68 // Advance to the 5 minute mark and run the timer. It should not record |
| 69 // StillRunning. |
| 70 { |
| 71 base::HistogramTester metrics; |
| 72 AdvanceAndRunTimer(true /* expect_running */); |
| 73 metrics.ExpectTotalCount("ServiceWorker.StillRunningTime", 0); |
| 74 } |
| 75 |
| 76 // Advance again. It should record StillRunning. |
| 77 { |
| 78 base::HistogramTester metrics; |
| 79 AdvanceAndRunTimer(true /* expect_running */); |
| 80 metrics.ExpectTimeBucketCount("ServiceWorker.StillRunningTime", |
| 81 base::TimeDelta::FromMinutes(10), 1); |
| 82 } |
| 83 |
| 84 // Advance 30 seconds and stop the worker. It should record Runtime. |
| 85 { |
| 86 base::HistogramTester metrics; |
| 87 tick_clock()->Advance(base::TimeDelta::FromSeconds(30)); |
| 88 tracker()->StopTiming(kVersion2); |
| 89 metrics.ExpectTimeBucketCount( |
| 90 "ServiceWorker.Runtime", |
| 91 base::TimeDelta::FromMinutes(10) + base::TimeDelta::FromSeconds(30), 1); |
| 92 } |
| 93 |
| 94 { |
| 95 base::HistogramTester metrics; |
| 96 // Start a worker and abort the timing. |
| 97 tracker()->StartTiming(kVersion1); |
| 98 tick_clock()->Advance(base::TimeDelta::FromSeconds(10)); |
| 99 tracker()->AbortTiming(kVersion1); |
| 100 |
| 101 // Advance to the next 5 minute mark. It should not record another |
| 102 // StillRunning. |
| 103 AdvanceAndRunTimer(false /* expect_running */); |
| 104 |
| 105 // Aborting multiple times should be fine. |
| 106 tracker()->AbortTiming(kVersion1); |
| 107 tracker()->AbortTiming(kVersion1); |
| 108 // StopTiming should not record another StillRunning. |
| 109 tracker()->StopTiming(kVersion1); |
| 110 |
| 111 metrics.ExpectTotalCount("ServiceWorker.StillRunningTime", 0); |
| 112 metrics.ExpectTotalCount("ServiceWorker.Runtime", 0); |
| 113 } |
| 114 } |
| 115 |
| 116 } // namespace content |
| OLD | NEW |