OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/scheduler/renderer/throttling_helper.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/test/simple_test_tick_clock.h" |
| 10 #include "cc/test/ordered_simple_task_runner.h" |
| 11 #include "components/scheduler/base/test_time_source.h" |
| 12 #include "components/scheduler/child/scheduler_tqm_delegate_for_test.h" |
| 13 #include "components/scheduler/renderer/renderer_scheduler_impl.h" |
| 14 #include "components/scheduler/renderer/web_frame_scheduler_impl.h" |
| 15 #include "components/scheduler/renderer/web_view_scheduler_impl.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "third_party/WebKit/public/platform/WebTaskRunner.h" |
| 19 #include "third_party/WebKit/public/platform/WebTraceLocation.h" |
| 20 |
| 21 using testing::ElementsAre; |
| 22 |
| 23 namespace scheduler { |
| 24 |
| 25 class ThrottlingHelperTest : public testing::Test { |
| 26 public: |
| 27 ThrottlingHelperTest() {} |
| 28 ~ThrottlingHelperTest() override {} |
| 29 |
| 30 void SetUp() override { |
| 31 clock_.reset(new base::SimpleTestTickClock()); |
| 32 clock_->Advance(base::TimeDelta::FromMicroseconds(5000)); |
| 33 mock_task_runner_ = |
| 34 make_scoped_refptr(new cc::OrderedSimpleTaskRunner(clock_.get(), true)); |
| 35 delegate_ = SchedulerTqmDelegateForTest::Create( |
| 36 mock_task_runner_, make_scoped_ptr(new TestTimeSource(clock_.get()))); |
| 37 scheduler_.reset(new RendererSchedulerImpl(delegate_)); |
| 38 throttling_helper_ = scheduler_->throttling_helper(); |
| 39 web_view_scheduler_.reset( |
| 40 new WebViewSchedulerImpl(nullptr, scheduler_.get())); |
| 41 web_frame_scheduler_ = web_view_scheduler_->createWebFrameSchedulerImpl(); |
| 42 } |
| 43 |
| 44 void TearDown() override { |
| 45 web_frame_scheduler_.reset(); |
| 46 web_view_scheduler_.reset(); |
| 47 scheduler_->Shutdown(); |
| 48 scheduler_.reset(); |
| 49 } |
| 50 |
| 51 protected: |
| 52 scoped_ptr<base::SimpleTestTickClock> clock_; |
| 53 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; |
| 54 scoped_refptr<SchedulerTqmDelegate> delegate_; |
| 55 scoped_ptr<RendererSchedulerImpl> scheduler_; |
| 56 scoped_ptr<WebViewSchedulerImpl> web_view_scheduler_; |
| 57 scoped_ptr<WebFrameSchedulerImpl> web_frame_scheduler_; |
| 58 ThrottlingHelper* throttling_helper_; // NOT OWNED |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(ThrottlingHelperTest); |
| 61 }; |
| 62 |
| 63 TEST_F(ThrottlingHelperTest, DelayToNextRunTimeInSeconds) { |
| 64 EXPECT_EQ(base::TimeDelta::FromSecondsD(1.0), |
| 65 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 66 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.0))); |
| 67 |
| 68 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.9), |
| 69 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 70 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.1))); |
| 71 |
| 72 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.8), |
| 73 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 74 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.2))); |
| 75 |
| 76 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.5), |
| 77 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 78 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.5))); |
| 79 |
| 80 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.2), |
| 81 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 82 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.8))); |
| 83 |
| 84 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.1), |
| 85 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 86 base::TimeTicks() + base::TimeDelta::FromSecondsD(0.9))); |
| 87 |
| 88 EXPECT_EQ(base::TimeDelta::FromSecondsD(1.0), |
| 89 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 90 base::TimeTicks() + base::TimeDelta::FromSecondsD(1.0))); |
| 91 |
| 92 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.9), |
| 93 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 94 base::TimeTicks() + base::TimeDelta::FromSecondsD(1.1))); |
| 95 |
| 96 EXPECT_EQ(base::TimeDelta::FromSecondsD(1.0), |
| 97 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 98 base::TimeTicks() + base::TimeDelta::FromSecondsD(8.0))); |
| 99 |
| 100 EXPECT_EQ(base::TimeDelta::FromSecondsD(0.9), |
| 101 ThrottlingHelper::DelayToNextRunTimeInSeconds( |
| 102 base::TimeTicks() + base::TimeDelta::FromSecondsD(8.1))); |
| 103 } |
| 104 |
| 105 namespace { |
| 106 class TestTask : public blink::WebTaskRunner::Task { |
| 107 public: |
| 108 TestTask(std::vector<double>* run_times_millis, |
| 109 base::SimpleTestTickClock* clock) |
| 110 : run_times_millis_(run_times_millis), clock_(clock) {} |
| 111 |
| 112 ~TestTask() override {} |
| 113 |
| 114 void run() override { |
| 115 run_times_millis_->push_back( |
| 116 (clock_->NowTicks() - base::TimeTicks()).InMillisecondsF()); |
| 117 } |
| 118 |
| 119 private: |
| 120 std::vector<double>* run_times_millis_; // NOT OWNED |
| 121 base::SimpleTestTickClock* clock_; // NOT OWNED |
| 122 }; |
| 123 } // namespace |
| 124 |
| 125 TEST_F(ThrottlingHelperTest, TimerAlignment) { |
| 126 std::vector<double> run_times_millis; |
| 127 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 128 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 200.0); |
| 129 |
| 130 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 131 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 800.0); |
| 132 |
| 133 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 134 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 1200.0); |
| 135 |
| 136 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 137 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 8300.0); |
| 138 |
| 139 throttling_helper_->ThrottleBackgroundFrame( |
| 140 static_cast<WebFrameSchedulerImpl*>(web_frame_scheduler_.get())); |
| 141 |
| 142 mock_task_runner_->RunUntilIdle(); |
| 143 |
| 144 // Times are aligned to a multipple of 1000 milliseconds. |
| 145 EXPECT_THAT(run_times_millis, ElementsAre(1000.0, 1000.0, 2000.0, 9000.0)); |
| 146 } |
| 147 |
| 148 TEST_F(ThrottlingHelperTest, TimerAlignment_Unthrottled) { |
| 149 std::vector<double> run_times_millis; |
| 150 double start_time_millis = |
| 151 (clock_->NowTicks() - base::TimeTicks()).InMillisecondsF(); |
| 152 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 153 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 200.0); |
| 154 |
| 155 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 156 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 800.0); |
| 157 |
| 158 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 159 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 1200.0); |
| 160 |
| 161 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 162 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 8300.0); |
| 163 |
| 164 throttling_helper_->ThrottleBackgroundFrame( |
| 165 static_cast<WebFrameSchedulerImpl*>(web_frame_scheduler_.get())); |
| 166 throttling_helper_->UnthrottleForegroundFrame( |
| 167 static_cast<WebFrameSchedulerImpl*>(web_frame_scheduler_.get())); |
| 168 |
| 169 mock_task_runner_->RunUntilIdle(); |
| 170 |
| 171 // Times are not aligned. |
| 172 EXPECT_THAT( |
| 173 run_times_millis, |
| 174 ElementsAre(start_time_millis + 200.0, start_time_millis + 800.0, |
| 175 start_time_millis + 1200.0, start_time_millis + 8300.0)); |
| 176 } |
| 177 |
| 178 TEST_F(ThrottlingHelperTest, WakeUpForNonDelayedTask) { |
| 179 std::vector<double> run_times_millis; |
| 180 |
| 181 // Nothing is posted on web_frame_scheduler_ so PumpThrottledTasks will not |
| 182 // tick. |
| 183 throttling_helper_->ThrottleBackgroundFrame( |
| 184 static_cast<WebFrameSchedulerImpl*>(web_frame_scheduler_.get())); |
| 185 |
| 186 // Posting a task should trigger the pump. |
| 187 web_frame_scheduler_->timerTaskRunner()->postTask( |
| 188 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get())); |
| 189 |
| 190 mock_task_runner_->RunUntilIdle(); |
| 191 EXPECT_THAT(run_times_millis, ElementsAre(1000.0)); |
| 192 } |
| 193 |
| 194 TEST_F(ThrottlingHelperTest, WakeUpForDelayedTask) { |
| 195 std::vector<double> run_times_millis; |
| 196 |
| 197 // Nothing is posted on web_frame_scheduler_ so PumpThrottledTasks will not |
| 198 // tick. |
| 199 throttling_helper_->ThrottleBackgroundFrame( |
| 200 static_cast<WebFrameSchedulerImpl*>(web_frame_scheduler_.get())); |
| 201 |
| 202 // Posting a task should trigger the pump. |
| 203 web_frame_scheduler_->timerTaskRunner()->postDelayedTask( |
| 204 BLINK_FROM_HERE, new TestTask(&run_times_millis, clock_.get()), 1200.0); |
| 205 |
| 206 mock_task_runner_->RunUntilIdle(); |
| 207 EXPECT_THAT(run_times_millis, ElementsAre(2000.0)); |
| 208 } |
| 209 |
| 210 } // namespace scheduler |
OLD | NEW |