Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / scheduler / renderer / task_cost_estimator_unittest.cc
blobb4bb48611ff059123f0932939b07e5af2613eaa4
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 "base/test/simple_test_tick_clock.h"
6 #include "components/scheduler/child/test_time_source.h"
7 #include "components/scheduler/renderer/task_cost_estimator.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace scheduler {
14 class TaskCostEstimatorTest : public testing::Test {
15 public:
16 TaskCostEstimatorTest() {}
17 ~TaskCostEstimatorTest() override {}
19 void SetUp() override {}
21 base::SimpleTestTickClock clock_;
24 class TaskCostEstimatorForTest : public TaskCostEstimator {
25 public:
26 TaskCostEstimatorForTest(base::SimpleTestTickClock* clock,
27 int sample_count,
28 double estimation_percentile)
29 : TaskCostEstimator(sample_count, estimation_percentile) {
30 SetTimeSourceForTesting(make_scoped_ptr(new TestTimeSource(clock)));
34 TEST_F(TaskCostEstimatorTest, BasicEstimation) {
35 TaskCostEstimatorForTest estimator(&clock_, 1, 100);
36 base::PendingTask task(FROM_HERE, base::Closure());
38 estimator.WillProcessTask(task);
39 clock_.Advance(base::TimeDelta::FromMilliseconds(500));
40 estimator.DidProcessTask(task);
42 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
43 estimator.expected_task_duration());
46 TEST_F(TaskCostEstimatorTest, Clear) {
47 TaskCostEstimatorForTest estimator(&clock_, 1, 100);
48 base::PendingTask task(FROM_HERE, base::Closure());
50 estimator.WillProcessTask(task);
51 clock_.Advance(base::TimeDelta::FromMilliseconds(500));
52 estimator.DidProcessTask(task);
54 estimator.Clear();
56 EXPECT_EQ(base::TimeDelta(), estimator.expected_task_duration());
59 TEST_F(TaskCostEstimatorTest, NestedRunLoop) {
60 TaskCostEstimatorForTest estimator(&clock_, 1, 100);
61 base::PendingTask task(FROM_HERE, base::Closure());
63 // Make sure we ignore the tasks inside the nested run loop.
64 estimator.WillProcessTask(task);
65 estimator.WillProcessTask(task);
66 clock_.Advance(base::TimeDelta::FromMilliseconds(500));
67 estimator.DidProcessTask(task);
68 clock_.Advance(base::TimeDelta::FromMilliseconds(500));
69 estimator.DidProcessTask(task);
71 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000),
72 estimator.expected_task_duration());
75 } // namespace scheduler