Chromium Code Reviews| 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/callback_forward.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/test/test_mock_time_task_runner.h" | |
| 17 #include "base/test/test_pending_task.h" | |
| 18 #include "base/time/time.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace base { | |
| 22 | |
| 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 void DummyClosure() {} | |
|
gab
2016/11/18 15:13:55
Use base::DoNothing() for this (without the base::
bruthig
2016/11/18 16:44:53
Done.
| |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 class ScopedMockTimeMessageLoopTaskRunnerTest : public testing::Test { | |
| 41 public: | |
| 42 ScopedMockTimeMessageLoopTaskRunnerTest(); | |
| 43 ~ScopedMockTimeMessageLoopTaskRunnerTest() override; | |
| 44 | |
| 45 protected: | |
| 46 TestMockTimeTaskRunner* original_task_runner() { | |
| 47 return original_task_runner_.get(); | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 scoped_refptr<TestMockTimeTaskRunner> original_task_runner_; | |
| 52 | |
| 53 MessageLoop message_loop_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(ScopedMockTimeMessageLoopTaskRunnerTest); | |
| 56 }; | |
| 57 | |
| 58 ScopedMockTimeMessageLoopTaskRunnerTest:: | |
| 59 ScopedMockTimeMessageLoopTaskRunnerTest() | |
| 60 : original_task_runner_(new TestMockTimeTaskRunner()), message_loop_() { | |
|
gab
2016/11/18 15:13:55
No need for "message_loop_()", the same thing happ
bruthig
2016/11/18 16:44:53
Done.
| |
| 61 MessageLoop::current()->SetTaskRunner(original_task_runner_); | |
| 62 } | |
|
gab
2016/11/18 15:13:54
I generally tend to prefer inlining definitions in
bruthig
2016/11/18 16:44:53
Done.
| |
| 63 | |
| 64 ScopedMockTimeMessageLoopTaskRunnerTest:: | |
| 65 ~ScopedMockTimeMessageLoopTaskRunnerTest() {} | |
|
gab
2016/11/18 15:13:54
Don't think you need an explicit destructor since
bruthig
2016/11/18 16:44:53
Done.
| |
| 66 | |
| 67 // Verifies a new TaskRunner is installed while a | |
| 68 // ScopedMockTimeMessageLoopTaskRunner exists and the previous one is installed | |
| 69 // after destruction. | |
| 70 TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, CurrentTaskRunners) { | |
| 71 std::unique_ptr<ScopedMockTimeMessageLoopTaskRunner> scoped_task_runner_ = | |
| 72 base::MakeUnique<ScopedMockTimeMessageLoopTaskRunner>(); | |
| 73 | |
| 74 EXPECT_EQ(scoped_task_runner_->task_runner(), GetCurrentTaskRunner()); | |
| 75 | |
| 76 scoped_task_runner_.reset(); | |
| 77 | |
| 78 EXPECT_EQ(original_task_runner(), GetCurrentTaskRunner()); | |
| 79 } | |
| 80 | |
| 81 TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, | |
| 82 IncompleteTasksAreCopiedToPreviousTaskRunnerAfterDestruction) { | |
| 83 std::unique_ptr<ScopedMockTimeMessageLoopTaskRunner> scoped_task_runner_ = | |
| 84 base::MakeUnique<ScopedMockTimeMessageLoopTaskRunner>(); | |
| 85 | |
| 86 Closure task_1 = Bind(&DummyClosure); | |
| 87 Closure task_2 = Bind(&DummyClosure); | |
| 88 Closure task_10 = Bind(&DummyClosure); | |
| 89 Closure task_11 = Bind(&DummyClosure); | |
| 90 | |
| 91 const int task_1_delay = 1; | |
| 92 const int task_2_delay = 2; | |
| 93 const int task_10_delay = 10; | |
| 94 const int task_11_delay = 11; | |
|
gab
2016/11/18 15:13:55
Use
constexpr TimeDelta task_1_delay = TimeDelta:
bruthig
2016/11/18 16:44:53
Done.
| |
| 95 | |
| 96 const int step_time_by = 5; | |
| 97 | |
| 98 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_1, | |
| 99 TimeDelta::FromSeconds(task_1_delay)); | |
| 100 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_2, | |
| 101 TimeDelta::FromSeconds(task_2_delay)); | |
| 102 GetCurrentTaskRunner()->PostDelayedTask( | |
| 103 FROM_HERE, task_10, TimeDelta::FromSeconds(task_10_delay)); | |
| 104 GetCurrentTaskRunner()->PostDelayedTask( | |
| 105 FROM_HERE, task_11, TimeDelta::FromSeconds(task_11_delay)); | |
| 106 | |
| 107 scoped_task_runner_->task_runner()->FastForwardBy( | |
| 108 TimeDelta::FromSeconds(step_time_by)); | |
| 109 | |
| 110 scoped_task_runner_.reset(); | |
| 111 | |
| 112 std::deque<TestPendingTask> pending_tasks = | |
| 113 original_task_runner()->TakePendingTasks(); | |
| 114 | |
| 115 EXPECT_EQ(size_t(2), pending_tasks.size()); | |
|
gab
2016/11/18 15:13:54
s/size_t(2)/2U/
bruthig
2016/11/18 16:44:53
Done.
| |
| 116 | |
| 117 TestPendingTask pending_task = PopFront(&pending_tasks); | |
| 118 EXPECT_TRUE(task_10.Equals(pending_task.task)); | |
| 119 EXPECT_EQ(TimeDelta::FromSeconds(task_10_delay - step_time_by), | |
| 120 pending_task.delay); | |
| 121 | |
| 122 pending_task = PopFront(&pending_tasks); | |
| 123 EXPECT_TRUE(task_11.Equals(pending_task.task)); | |
| 124 EXPECT_EQ(TimeDelta::FromSeconds(task_11_delay - step_time_by), | |
| 125 pending_task.delay); | |
| 126 } | |
| 127 | |
| 128 } // namespace base | |
| OLD | NEW |