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 "base/test/scoped_mock_time_message_loop_task_runner.h" |
| 6 |
| 7 #include <deque> |
| 8 #include <memory> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/callback_forward.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/test/test_mock_time_task_runner.h" |
| 18 #include "base/test/test_pending_task.h" |
| 19 #include "base/time/time.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace base { |
| 23 namespace { |
| 24 |
| 25 TaskRunner* GetCurrentTaskRunner() { |
| 26 return MessageLoop::current()->task_runner().get(); |
| 27 } |
| 28 |
| 29 // Pops a task from the front of |pending_tasks| and returns it. |
| 30 TestPendingTask PopFront(std::deque<TestPendingTask>* pending_tasks) { |
| 31 TestPendingTask task = pending_tasks->front(); |
| 32 pending_tasks->pop_front(); |
| 33 return task; |
| 34 } |
| 35 |
| 36 class ScopedMockTimeMessageLoopTaskRunnerTest : public testing::Test { |
| 37 public: |
| 38 ScopedMockTimeMessageLoopTaskRunnerTest() |
| 39 : original_task_runner_(new TestMockTimeTaskRunner()) { |
| 40 MessageLoop::current()->SetTaskRunner(original_task_runner_); |
| 41 } |
| 42 |
| 43 protected: |
| 44 TestMockTimeTaskRunner* original_task_runner() { |
| 45 return original_task_runner_.get(); |
| 46 } |
| 47 |
| 48 private: |
| 49 scoped_refptr<TestMockTimeTaskRunner> original_task_runner_; |
| 50 |
| 51 MessageLoop message_loop_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(ScopedMockTimeMessageLoopTaskRunnerTest); |
| 54 }; |
| 55 |
| 56 // Verifies a new TaskRunner is installed while a |
| 57 // ScopedMockTimeMessageLoopTaskRunner exists and the previous one is installed |
| 58 // after destruction. |
| 59 TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, CurrentTaskRunners) { |
| 60 auto scoped_task_runner_ = |
| 61 base::MakeUnique<ScopedMockTimeMessageLoopTaskRunner>(); |
| 62 EXPECT_EQ(scoped_task_runner_->task_runner(), GetCurrentTaskRunner()); |
| 63 scoped_task_runner_.reset(); |
| 64 EXPECT_EQ(original_task_runner(), GetCurrentTaskRunner()); |
| 65 } |
| 66 |
| 67 TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, |
| 68 IncompleteTasksAreCopiedToPreviousTaskRunnerAfterDestruction) { |
| 69 auto scoped_task_runner_ = |
| 70 base::MakeUnique<ScopedMockTimeMessageLoopTaskRunner>(); |
| 71 |
| 72 Closure task_1 = Bind(&DoNothing); |
| 73 Closure task_2 = Bind(&DoNothing); |
| 74 Closure task_10 = Bind(&DoNothing); |
| 75 Closure task_11 = Bind(&DoNothing); |
| 76 |
| 77 constexpr TimeDelta task_1_delay = TimeDelta::FromSeconds(1); |
| 78 constexpr TimeDelta task_2_delay = TimeDelta::FromSeconds(2); |
| 79 constexpr TimeDelta task_10_delay = TimeDelta::FromSeconds(10); |
| 80 constexpr TimeDelta task_11_delay = TimeDelta::FromSeconds(11); |
| 81 |
| 82 constexpr TimeDelta step_time_by = TimeDelta::FromSeconds(5); |
| 83 |
| 84 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_1, task_1_delay); |
| 85 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_2, task_2_delay); |
| 86 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_10, task_10_delay); |
| 87 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_11, task_11_delay); |
| 88 |
| 89 scoped_task_runner_->task_runner()->FastForwardBy(step_time_by); |
| 90 |
| 91 scoped_task_runner_.reset(); |
| 92 |
| 93 std::deque<TestPendingTask> pending_tasks = |
| 94 original_task_runner()->TakePendingTasks(); |
| 95 |
| 96 EXPECT_EQ(2U, pending_tasks.size()); |
| 97 |
| 98 TestPendingTask pending_task = PopFront(&pending_tasks); |
| 99 EXPECT_TRUE(task_10.Equals(pending_task.task)); |
| 100 EXPECT_EQ(task_10_delay - step_time_by, pending_task.delay); |
| 101 |
| 102 pending_task = PopFront(&pending_tasks); |
| 103 EXPECT_TRUE(task_11.Equals(pending_task.task)); |
| 104 EXPECT_EQ(task_11_delay - step_time_by, pending_task.delay); |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 } // namespace base |
OLD | NEW |