Index: base/test/scoped_mock_time_message_loop_task_runner_unittest.cc |
diff --git a/base/test/scoped_mock_time_message_loop_task_runner_unittest.cc b/base/test/scoped_mock_time_message_loop_task_runner_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9bdb35c3348d495bcc88ddf54ef08da669b614b9 |
--- /dev/null |
+++ b/base/test/scoped_mock_time_message_loop_task_runner_unittest.cc |
@@ -0,0 +1,128 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/test/scoped_mock_time_message_loop_task_runner.h" |
+ |
+#include <deque> |
+#include <memory> |
+ |
+#include "base/bind.h" |
+#include "base/callback_forward.h" |
+#include "base/macros.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/test/test_mock_time_task_runner.h" |
+#include "base/test/test_pending_task.h" |
+#include "base/time/time.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace base { |
+ |
+namespace { |
+ |
+TaskRunner* GetCurrentTaskRunner() { |
+ return MessageLoop::current()->task_runner().get(); |
+} |
+ |
+// Pops a task from the front of |pending_tasks| and returns it. |
+TestPendingTask PopFront(std::deque<TestPendingTask>* pending_tasks) { |
+ TestPendingTask task = pending_tasks->front(); |
+ pending_tasks->pop_front(); |
+ return task; |
+} |
+ |
+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.
|
+ |
+} // namespace |
+ |
+class ScopedMockTimeMessageLoopTaskRunnerTest : public testing::Test { |
+ public: |
+ ScopedMockTimeMessageLoopTaskRunnerTest(); |
+ ~ScopedMockTimeMessageLoopTaskRunnerTest() override; |
+ |
+ protected: |
+ TestMockTimeTaskRunner* original_task_runner() { |
+ return original_task_runner_.get(); |
+ } |
+ |
+ private: |
+ scoped_refptr<TestMockTimeTaskRunner> original_task_runner_; |
+ |
+ MessageLoop message_loop_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ScopedMockTimeMessageLoopTaskRunnerTest); |
+}; |
+ |
+ScopedMockTimeMessageLoopTaskRunnerTest:: |
+ ScopedMockTimeMessageLoopTaskRunnerTest() |
+ : 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.
|
+ MessageLoop::current()->SetTaskRunner(original_task_runner_); |
+} |
gab
2016/11/18 15:13:54
I generally tend to prefer inlining definitions in
bruthig
2016/11/18 16:44:53
Done.
|
+ |
+ScopedMockTimeMessageLoopTaskRunnerTest:: |
+ ~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.
|
+ |
+// Verifies a new TaskRunner is installed while a |
+// ScopedMockTimeMessageLoopTaskRunner exists and the previous one is installed |
+// after destruction. |
+TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, CurrentTaskRunners) { |
+ std::unique_ptr<ScopedMockTimeMessageLoopTaskRunner> scoped_task_runner_ = |
+ base::MakeUnique<ScopedMockTimeMessageLoopTaskRunner>(); |
+ |
+ EXPECT_EQ(scoped_task_runner_->task_runner(), GetCurrentTaskRunner()); |
+ |
+ scoped_task_runner_.reset(); |
+ |
+ EXPECT_EQ(original_task_runner(), GetCurrentTaskRunner()); |
+} |
+ |
+TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, |
+ IncompleteTasksAreCopiedToPreviousTaskRunnerAfterDestruction) { |
+ std::unique_ptr<ScopedMockTimeMessageLoopTaskRunner> scoped_task_runner_ = |
+ base::MakeUnique<ScopedMockTimeMessageLoopTaskRunner>(); |
+ |
+ Closure task_1 = Bind(&DummyClosure); |
+ Closure task_2 = Bind(&DummyClosure); |
+ Closure task_10 = Bind(&DummyClosure); |
+ Closure task_11 = Bind(&DummyClosure); |
+ |
+ const int task_1_delay = 1; |
+ const int task_2_delay = 2; |
+ const int task_10_delay = 10; |
+ 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.
|
+ |
+ const int step_time_by = 5; |
+ |
+ GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_1, |
+ TimeDelta::FromSeconds(task_1_delay)); |
+ GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_2, |
+ TimeDelta::FromSeconds(task_2_delay)); |
+ GetCurrentTaskRunner()->PostDelayedTask( |
+ FROM_HERE, task_10, TimeDelta::FromSeconds(task_10_delay)); |
+ GetCurrentTaskRunner()->PostDelayedTask( |
+ FROM_HERE, task_11, TimeDelta::FromSeconds(task_11_delay)); |
+ |
+ scoped_task_runner_->task_runner()->FastForwardBy( |
+ TimeDelta::FromSeconds(step_time_by)); |
+ |
+ scoped_task_runner_.reset(); |
+ |
+ std::deque<TestPendingTask> pending_tasks = |
+ original_task_runner()->TakePendingTasks(); |
+ |
+ 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.
|
+ |
+ TestPendingTask pending_task = PopFront(&pending_tasks); |
+ EXPECT_TRUE(task_10.Equals(pending_task.task)); |
+ EXPECT_EQ(TimeDelta::FromSeconds(task_10_delay - step_time_by), |
+ pending_task.delay); |
+ |
+ pending_task = PopFront(&pending_tasks); |
+ EXPECT_TRUE(task_11.Equals(pending_task.task)); |
+ EXPECT_EQ(TimeDelta::FromSeconds(task_11_delay - step_time_by), |
+ pending_task.delay); |
+} |
+ |
+} // namespace base |