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 | |
24 namespace { | |
25 | |
26 TaskRunner* GetCurrentTaskRunner() { | |
27 return MessageLoop::current()->task_runner().get(); | |
28 } | |
29 | |
30 // Pops a task from the front of |pending_tasks| and returns it. | |
31 TestPendingTask PopFront(std::deque<TestPendingTask>* pending_tasks) { | |
32 TestPendingTask task = pending_tasks->front(); | |
33 pending_tasks->pop_front(); | |
34 return task; | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 class ScopedMockTimeMessageLoopTaskRunnerTest : public testing::Test { | |
40 public: | |
41 ScopedMockTimeMessageLoopTaskRunnerTest() | |
42 : original_task_runner_(new TestMockTimeTaskRunner()) { | |
43 MessageLoop::current()->SetTaskRunner(original_task_runner_); | |
44 } | |
45 | |
46 protected: | |
47 TestMockTimeTaskRunner* original_task_runner() { | |
48 return original_task_runner_.get(); | |
49 } | |
50 | |
51 private: | |
52 scoped_refptr<TestMockTimeTaskRunner> original_task_runner_; | |
53 | |
54 MessageLoop message_loop_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(ScopedMockTimeMessageLoopTaskRunnerTest); | |
57 }; | |
58 | |
59 // Verifies a new TaskRunner is installed while a | |
60 // ScopedMockTimeMessageLoopTaskRunner exists and the previous one is installed | |
61 // after destruction. | |
62 TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, CurrentTaskRunners) { | |
63 std::unique_ptr<ScopedMockTimeMessageLoopTaskRunner> scoped_task_runner_ = | |
64 base::MakeUnique<ScopedMockTimeMessageLoopTaskRunner>(); | |
65 | |
66 EXPECT_EQ(scoped_task_runner_->task_runner(), GetCurrentTaskRunner()); | |
67 | |
68 scoped_task_runner_.reset(); | |
69 | |
70 EXPECT_EQ(original_task_runner(), GetCurrentTaskRunner()); | |
71 } | |
72 | |
73 TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, | |
74 IncompleteTasksAreCopiedToPreviousTaskRunnerAfterDestruction) { | |
75 std::unique_ptr<ScopedMockTimeMessageLoopTaskRunner> scoped_task_runner_ = | |
76 base::MakeUnique<ScopedMockTimeMessageLoopTaskRunner>(); | |
77 | |
78 Closure task_1 = Bind(&DoNothing); | |
79 Closure task_2 = Bind(&DoNothing); | |
80 Closure task_10 = Bind(&DoNothing); | |
81 Closure task_11 = Bind(&DoNothing); | |
82 | |
83 constexpr TimeDelta task_1_delay = TimeDelta::FromSeconds(1); | |
84 constexpr TimeDelta task_2_delay = TimeDelta::FromSeconds(2); | |
85 constexpr TimeDelta task_10_delay = TimeDelta::FromSeconds(10); | |
86 constexpr TimeDelta task_11_delay = TimeDelta::FromSeconds(11); | |
87 | |
88 constexpr TimeDelta step_time_by = TimeDelta::FromSeconds(5); | |
89 | |
90 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_1, task_1_delay); | |
91 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_2, task_2_delay); | |
92 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_10, task_10_delay); | |
93 GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_11, task_11_delay); | |
94 | |
95 scoped_task_runner_->task_runner()->FastForwardBy(step_time_by); | |
gab
2016/11/18 18:18:54
scoped_task_runner_->FastForwardBy()
bruthig
2016/11/18 18:47:59
It doesn't work here because |scoped_task_runner_|
gab
2016/11/18 18:53:37
Ah ok, you could do scoped_task_runner_.get()-> I
| |
96 | |
97 scoped_task_runner_.reset(); | |
98 | |
99 std::deque<TestPendingTask> pending_tasks = | |
100 original_task_runner()->TakePendingTasks(); | |
101 | |
102 EXPECT_EQ(2U, pending_tasks.size()); | |
103 | |
104 TestPendingTask pending_task = PopFront(&pending_tasks); | |
105 EXPECT_TRUE(task_10.Equals(pending_task.task)); | |
106 EXPECT_EQ(task_10_delay - step_time_by, pending_task.delay); | |
107 | |
108 pending_task = PopFront(&pending_tasks); | |
109 EXPECT_TRUE(task_11.Equals(pending_task.task)); | |
110 EXPECT_EQ(task_11_delay - step_time_by, pending_task.delay); | |
111 } | |
112 | |
113 } // namespace base | |
OLD | NEW |