OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "platform/scheduler/base/delayed_switch.h" |
| 6 |
| 7 #include "base/test/simple_test_tick_clock.h" |
| 8 #include "cc/test/ordered_simple_task_runner.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using testing::ElementsAre; |
| 13 |
| 14 namespace blink { |
| 15 namespace scheduler { |
| 16 |
| 17 class DelayedSwitchTest : public testing::Test { |
| 18 public: |
| 19 DelayedSwitchTest() {} |
| 20 ~DelayedSwitchTest() override {} |
| 21 |
| 22 void SetUp() override { |
| 23 clock_.reset(new base::SimpleTestTickClock()); |
| 24 mock_task_runner_ = make_scoped_refptr( |
| 25 new cc::OrderedSimpleTaskRunner(clock_.get(), true)); |
| 26 } |
| 27 |
| 28 protected: |
| 29 std::unique_ptr<base::SimpleTestTickClock> clock_; |
| 30 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(DelayedSwitchTest); |
| 33 }; |
| 34 |
| 35 namespace { |
| 36 |
| 37 void TestTask(std::vector<base::TimeTicks>* run_times, |
| 38 base::SimpleTestTickClock* clock) { |
| 39 run_times->push_back(clock->NowTicks()); |
| 40 } |
| 41 |
| 42 }; |
| 43 |
| 44 TEST_F(DelayedSwitchTest, TestDelayedSwitch) { |
| 45 std::vector<base::TimeTicks> enable_run_times; |
| 46 std::vector<base::TimeTicks> disable_run_times; |
| 47 |
| 48 DelayedSwitch delayed_switch( |
| 49 mock_task_runner_.get(), |
| 50 base::TimeDelta::FromSeconds(1), |
| 51 base::Bind(&TestTask, &enable_run_times, clock_.get()), |
| 52 base::Bind(&TestTask, &disable_run_times, clock_.get())); |
| 53 |
| 54 base::TimeTicks time_zero = clock_->NowTicks(); |
| 55 |
| 56 EXPECT_FALSE(delayed_switch.IsEnabled()); |
| 57 |
| 58 delayed_switch.Enable(); |
| 59 |
| 60 EXPECT_TRUE(delayed_switch.IsEnabled()); |
| 61 |
| 62 mock_task_runner_->RunUntilTime(time_zero + base::TimeDelta::FromMilliseconds(
250)); |
| 63 |
| 64 EXPECT_TRUE(delayed_switch.IsEnabled()); |
| 65 // Subsequent calls to |Enable| should not produce additional calls |
| 66 // to |enable_callback|. |
| 67 delayed_switch.Enable(); |
| 68 |
| 69 mock_task_runner_->RunUntilTime(time_zero + base::TimeDelta::FromMilliseconds(
500)); |
| 70 |
| 71 EXPECT_TRUE(delayed_switch.IsEnabled()); |
| 72 delayed_switch.Disable(); |
| 73 // |Disable| is scheduled, but not active yet. |
| 74 EXPECT_TRUE(delayed_switch.IsEnabled()); |
| 75 |
| 76 mock_task_runner_->RunUntilTime(time_zero + base::TimeDelta::FromMilliseconds(
1000)); |
| 77 |
| 78 EXPECT_TRUE(delayed_switch.IsEnabled()); |
| 79 // A call to |Disable| while one is scheduled should not do anything. |
| 80 delayed_switch.Disable(); |
| 81 EXPECT_TRUE(delayed_switch.IsEnabled()); |
| 82 |
| 83 mock_task_runner_->RunUntilTime(time_zero + base::TimeDelta::FromMilliseconds(
1600)); |
| 84 |
| 85 // At 1500 ms disable was activated after a call to |Disable| at 500 ms with |
| 86 // 1 s delay. |
| 87 EXPECT_FALSE(delayed_switch.IsEnabled()); |
| 88 delayed_switch.Enable(); |
| 89 EXPECT_TRUE(delayed_switch.IsEnabled()); |
| 90 |
| 91 mock_task_runner_->RunUntilTime(time_zero + base::TimeDelta::FromMilliseconds(
2100)); |
| 92 |
| 93 // We scheduled a call to |Disable| at 1000 ms with 1 s delay. |
| 94 // Make sure it has done nothing. |
| 95 EXPECT_TRUE(delayed_switch.IsEnabled()); |
| 96 |
| 97 EXPECT_THAT(enable_run_times, ElementsAre( |
| 98 time_zero, |
| 99 time_zero + base::TimeDelta::FromMilliseconds(1600))); |
| 100 |
| 101 EXPECT_THAT(disable_run_times, ElementsAre( |
| 102 time_zero + base::TimeDelta::FromMilliseconds(1500))); |
| 103 } |
| 104 |
| 105 } // namespace scheduler |
| 106 } // namespace blink |
OLD | NEW |