1 // Copyright 2015 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.
5 #include "ash/metrics/task_switch_time_tracker.h"
9 #include "ash/test/task_switch_time_tracker_test_api.h"
10 #include "base/test/histogram_tester.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // A dummy histogram name.
18 const std::string kHistogramName
= "Dummy.Histogram";
22 class TaskSwitchTimeTrackerTest
: public testing::Test
{
24 TaskSwitchTimeTrackerTest();
25 ~TaskSwitchTimeTrackerTest() override
;
28 void SetUp() override
;
29 void TearDown() override
;
31 // Wrapper to the test targets OnTaskSwitch() method.
34 TaskSwitchTimeTracker
* time_tracker() {
35 return time_tracker_test_api_
->time_tracker();
39 // Used to verify recorded histogram data.
40 scoped_ptr
<base::HistogramTester
> histogram_tester_
;
42 // A Test API that wraps the test target.
43 scoped_ptr
<test::TaskSwitchTimeTrackerTestAPI
> time_tracker_test_api_
;
46 DISALLOW_COPY_AND_ASSIGN(TaskSwitchTimeTrackerTest
);
49 TaskSwitchTimeTrackerTest::TaskSwitchTimeTrackerTest() {
52 TaskSwitchTimeTrackerTest::~TaskSwitchTimeTrackerTest() {
55 void TaskSwitchTimeTrackerTest::SetUp() {
56 testing::Test::SetUp();
58 histogram_tester_
.reset(new base::HistogramTester());
59 time_tracker_test_api_
.reset(
60 new test::TaskSwitchTimeTrackerTestAPI(kHistogramName
));
61 // The TaskSwitchTimeTracker interprets a value of base::TimeTicks() as if the
62 // |last_action_time_| has not been set.
63 time_tracker_test_api_
->Advance(base::TimeDelta::FromMilliseconds(1));
66 void TaskSwitchTimeTrackerTest::TearDown() {
67 testing::Test::TearDown();
68 time_tracker_test_api_
.reset();
69 histogram_tester_
.reset();
72 void TaskSwitchTimeTrackerTest::OnTaskSwitch() {
73 time_tracker()->OnTaskSwitch();
76 // Verifies TaskSwitchTimeTracker::HasLastActionTime() returns false after
78 TEST_F(TaskSwitchTimeTrackerTest
,
79 HasLastActionTimeShouldBeFalseAfterConstruction
) {
80 EXPECT_FALSE(time_tracker_test_api_
->HasLastActionTime());
83 // Verifies TaskSwitchTimeTracker::HasLastActionTime() returns true after the
84 // first call to TaskSwitchTimeTracker::OnTaskSwitch() and no histogram data was
86 TEST_F(TaskSwitchTimeTrackerTest
,
87 HasLastActionTimeShouldBeTrueAfterOnTaskSwitch
) {
89 histogram_tester_
->ExpectTotalCount(kHistogramName
, 0);
92 // Verfies that the histogram data is recorded in the correct buckets.
93 TEST_F(TaskSwitchTimeTrackerTest
, RecordAfterTwoTaskSwitches
) {
96 time_tracker_test_api_
->Advance(base::TimeDelta::FromMilliseconds(2));
98 histogram_tester_
->ExpectTotalCount(kHistogramName
, 1);
99 histogram_tester_
->ExpectBucketCount(kHistogramName
, 0, 1);
101 time_tracker_test_api_
->Advance(base::TimeDelta::FromSeconds(1));
103 histogram_tester_
->ExpectTotalCount(kHistogramName
, 2);
104 histogram_tester_
->ExpectBucketCount(kHistogramName
, 1, 1);