Remove use of JSONReader::DeprecatedRead from activity_log
[chromium-blink-merge.git] / ash / metrics / task_switch_time_tracker_unittest.cc
blob7fc3aba928305447e5c9890ff9d23fb537efbdff
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"
7 #include <string>
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"
13 namespace ash {
15 namespace {
17 // A dummy histogram name.
18 const std::string kHistogramName = "Dummy.Histogram";
20 } // namespace
22 class TaskSwitchTimeTrackerTest : public testing::Test {
23 public:
24 TaskSwitchTimeTrackerTest();
25 ~TaskSwitchTimeTrackerTest() override;
27 // testing::Test:
28 void SetUp() override;
29 void TearDown() override;
31 // Wrapper to the test targets OnTaskSwitch() method.
32 void OnTaskSwitch();
34 TaskSwitchTimeTracker* time_tracker() {
35 return time_tracker_test_api_->time_tracker();
38 protected:
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_;
45 private:
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
77 // construction.
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
85 // recorded.
86 TEST_F(TaskSwitchTimeTrackerTest,
87 HasLastActionTimeShouldBeTrueAfterOnTaskSwitch) {
88 OnTaskSwitch();
89 histogram_tester_->ExpectTotalCount(kHistogramName, 0);
92 // Verfies that the histogram data is recorded in the correct buckets.
93 TEST_F(TaskSwitchTimeTrackerTest, RecordAfterTwoTaskSwitches) {
94 OnTaskSwitch();
96 time_tracker_test_api_->Advance(base::TimeDelta::FromMilliseconds(2));
97 OnTaskSwitch();
98 histogram_tester_->ExpectTotalCount(kHistogramName, 1);
99 histogram_tester_->ExpectBucketCount(kHistogramName, 0, 1);
101 time_tracker_test_api_->Advance(base::TimeDelta::FromSeconds(1));
102 OnTaskSwitch();
103 histogram_tester_->ExpectTotalCount(kHistogramName, 2);
104 histogram_tester_->ExpectBucketCount(kHistogramName, 1, 1);
107 } // namespace ash