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"
14 class TaskCostEstimatorTest
: public testing::Test
{
16 TaskCostEstimatorTest() {}
17 ~TaskCostEstimatorTest() override
{}
19 void SetUp() override
{}
21 base::SimpleTestTickClock clock_
;
24 class TaskCostEstimatorForTest
: public TaskCostEstimator
{
26 TaskCostEstimatorForTest(base::SimpleTestTickClock
* clock
,
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
);
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