OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ | 5 #ifndef BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ |
6 #define BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ | 6 #define BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <deque> | 10 #include <deque> |
11 #include <memory> | 11 #include <memory> |
12 #include <queue> | 12 #include <queue> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
| 15 #include "base/callback_helpers.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
18 #include "base/test/test_pending_task.h" | 19 #include "base/test/test_pending_task.h" |
19 #include "base/threading/thread_checker_impl.h" | 20 #include "base/threading/thread_checker_impl.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 | 24 |
24 class Clock; | 25 class Clock; |
(...skipping 15 matching lines...) Expand all Loading... |
40 // condition for preventing overflows is to make sure that the sum of all | 41 // condition for preventing overflows is to make sure that the sum of all |
41 // posted task delays and fast-forward increments is still representable by | 42 // posted task delays and fast-forward increments is still representable by |
42 // a TimeDelta, and that adding this delta to the starting values of Time | 43 // a TimeDelta, and that adding this delta to the starting values of Time |
43 // and TickTime is still within their respective range. | 44 // and TickTime is still within their respective range. |
44 // - Tasks aren't guaranteed to be destroyed immediately after they're run. | 45 // - Tasks aren't guaranteed to be destroyed immediately after they're run. |
45 // | 46 // |
46 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in | 47 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in |
47 // that it supports running delayed tasks in the correct temporal order. | 48 // that it supports running delayed tasks in the correct temporal order. |
48 class TestMockTimeTaskRunner : public SingleThreadTaskRunner { | 49 class TestMockTimeTaskRunner : public SingleThreadTaskRunner { |
49 public: | 50 public: |
| 51 // Everything that is executed in the scope of a ScopedContext will behave as |
| 52 // though it ran under |scope| (i.e. ThreadTaskRunnerHandle, |
| 53 // RunsTasksOnCurrentThread, etc.). This allows the test body to be all in one |
| 54 // block when multiple TestMockTimeTaskRunners share the main thread. For |
| 55 // example: |
| 56 // |
| 57 // class ExampleFixture { |
| 58 // protected: |
| 59 // DoBarOnFoo() { |
| 60 // DCHECK(foo_task_runner_->RunsOnCurrentThread()); |
| 61 // EXPECT_EQ(foo_task_runner_, ThreadTaskRunnerHandle::Get()); |
| 62 // DoBar(); |
| 63 // } |
| 64 // |
| 65 // // Mock main task runner. |
| 66 // base::MessageLoop message_loop_; |
| 67 // base::ScopedMockTimeMessageLoopTaskRunner main_task_runner_; |
| 68 // |
| 69 // // Mock foo task runner. |
| 70 // scoped_refptr<TestMockTimeTaskRunner> foo_task_runner_ = |
| 71 // new TestMockTimeTaskRunner(); |
| 72 // }; |
| 73 // |
| 74 // TEST_F(ExampleFixture, DoBarOnFoo) { |
| 75 // DoThingsOnMain(); |
| 76 // { |
| 77 // TestMockTimeTaskRunner::ScopedContext scoped_context( |
| 78 // foo_task_runner_.get()); |
| 79 // DoBarOnFoo(); |
| 80 // } |
| 81 // DoMoreThingsOnMain(); |
| 82 // } |
| 83 // |
| 84 class ScopedContext { |
| 85 public: |
| 86 // Note: |scope| is ran until idle as part of this constructor to ensure |
| 87 // that anything which runs in the underlying scope runs after any already |
| 88 // pending tasks (the contrary would break the SequencedTraskRunner |
| 89 // contract). |
| 90 explicit ScopedContext(scoped_refptr<TestMockTimeTaskRunner> scope); |
| 91 ~ScopedContext(); |
| 92 |
| 93 private: |
| 94 ScopedClosureRunner on_destroy_; |
| 95 DISALLOW_COPY_AND_ASSIGN(ScopedContext); |
| 96 }; |
| 97 |
50 // Constructs an instance whose virtual time will start at the Unix epoch, and | 98 // Constructs an instance whose virtual time will start at the Unix epoch, and |
51 // whose time ticks will start at zero. | 99 // whose time ticks will start at zero. |
52 TestMockTimeTaskRunner(); | 100 TestMockTimeTaskRunner(); |
53 | 101 |
54 // Constructs an instance starting at the given virtual time and time ticks. | 102 // Constructs an instance starting at the given virtual time and time ticks. |
55 TestMockTimeTaskRunner(Time start_time, TimeTicks start_ticks); | 103 TestMockTimeTaskRunner(Time start_time, TimeTicks start_ticks); |
56 | 104 |
57 // Fast-forwards virtual time by |delta|, causing all tasks with a remaining | 105 // Fast-forwards virtual time by |delta|, causing all tasks with a remaining |
58 // delay less than or equal to |delta| to be executed. |delta| must be | 106 // delay less than or equal to |delta| to be executed. |delta| must be |
59 // non-negative. | 107 // non-negative. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 size_t next_task_ordinal_; | 214 size_t next_task_ordinal_; |
167 | 215 |
168 Lock tasks_lock_; | 216 Lock tasks_lock_; |
169 | 217 |
170 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); | 218 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); |
171 }; | 219 }; |
172 | 220 |
173 } // namespace base | 221 } // namespace base |
174 | 222 |
175 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ | 223 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ |
OLD | NEW |