OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 <deque> | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/test/test_pending_task.h" | |
10 #include "base/test/test_simple_task_runner.h" | |
11 #include "cc/base/delayed_unique_notifier.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace cc { | |
15 namespace { | |
16 | |
17 class TestNotifier : public DelayedUniqueNotifier { | |
18 public: | |
19 TestNotifier(base::SequencedTaskRunner* task_runner, | |
20 const base::Closure& closure, | |
21 const base::TimeDelta& delay) | |
22 : DelayedUniqueNotifier(task_runner, closure, delay) {} | |
23 virtual ~TestNotifier() {} | |
24 | |
25 virtual base::TimeTicks Now() const OVERRIDE { return now_; } | |
reveman
2014/05/28 19:50:02
nit: Add // Overridden from DelayedUniqueNotifier:
vmpstr
2014/05/28 20:17:13
Done.
| |
26 | |
27 void SetNow(base::TimeTicks now) { now_ = now; } | |
28 | |
29 private: | |
30 base::TimeTicks now_; | |
31 }; | |
32 | |
33 class DelayedUniqueNotifierTest : public testing::Test { | |
34 public: | |
35 DelayedUniqueNotifierTest() : notification_count_(0) {} | |
36 | |
37 virtual void SetUp() OVERRIDE { | |
38 notification_count_ = 0; | |
39 task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner); | |
40 } | |
41 | |
42 void Notify() { ++notification_count_; } | |
43 | |
44 int NotificationCount() const { return notification_count_; } | |
45 | |
46 std::deque<base::TestPendingTask> TakePendingTasks() { | |
47 std::deque<base::TestPendingTask> tasks = task_runner_->GetPendingTasks(); | |
48 task_runner_->ClearPendingTasks(); | |
49 return tasks; | |
50 } | |
51 | |
52 protected: | |
53 int notification_count_; | |
54 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
55 }; | |
56 | |
57 TEST_F(DelayedUniqueNotifierTest, ZeroDelay) { | |
58 base::TimeDelta delay = base::TimeDelta::FromInternalValue(0); | |
59 TestNotifier notifier( | |
60 task_runner_, | |
61 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
62 delay); | |
63 | |
64 EXPECT_EQ(0, NotificationCount()); | |
65 | |
66 // Basic schedule for |delay| from now. | |
67 base::TimeTicks schedule_time = | |
68 base::TimeTicks() + base::TimeDelta::FromInternalValue(10); | |
69 | |
70 notifier.SetNow(schedule_time); | |
71 notifier.Schedule(); | |
72 | |
73 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | |
74 ASSERT_EQ(1u, tasks.size()); | |
75 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | |
76 | |
77 tasks[0].task.Run(); | |
78 EXPECT_EQ(1, NotificationCount()); | |
79 | |
80 // 5 schedules should result in only one run. | |
81 for (int i = 0; i < 5; ++i) | |
82 notifier.Schedule(); | |
83 | |
84 tasks = TakePendingTasks(); | |
85 ASSERT_EQ(1u, tasks.size()); | |
86 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | |
87 | |
88 tasks[0].task.Run(); | |
89 EXPECT_EQ(2, NotificationCount()); | |
90 } | |
91 | |
92 TEST_F(DelayedUniqueNotifierTest, SmallDelay) { | |
93 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); | |
94 TestNotifier notifier( | |
95 task_runner_, | |
96 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
97 delay); | |
98 | |
99 EXPECT_EQ(0, NotificationCount()); | |
100 | |
101 // Basic schedule for |delay| from now (now: 30, run time: 50). | |
102 base::TimeTicks schedule_time = | |
103 base::TimeTicks() + base::TimeDelta::FromInternalValue(30); | |
104 | |
105 notifier.SetNow(schedule_time); | |
106 notifier.Schedule(); | |
107 | |
108 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | |
109 | |
110 ASSERT_EQ(1u, tasks.size()); | |
111 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | |
112 | |
113 // It's not yet time to run, so we expect no notifications. | |
114 tasks[0].task.Run(); | |
115 EXPECT_EQ(0, NotificationCount()); | |
116 | |
117 tasks = TakePendingTasks(); | |
118 | |
119 ASSERT_EQ(1u, tasks.size()); | |
120 // Now the time should be delay minus whatever the value of now happens to be | |
121 // (now: 30, run time: 50). | |
122 base::TimeTicks scheduled_run_time = notifier.Now() + delay; | |
123 base::TimeTicks scheduled_delay = | |
124 base::TimeTicks() + (scheduled_run_time - notifier.Now()); | |
125 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun()); | |
126 | |
127 // Move closer to the run time (time: 49, run time: 50). | |
128 notifier.SetNow(notifier.Now() + base::TimeDelta::FromInternalValue(19)); | |
129 | |
130 // It's not yet time to run, so we expect no notifications. | |
131 tasks[0].task.Run(); | |
132 EXPECT_EQ(0, NotificationCount()); | |
133 | |
134 tasks = TakePendingTasks(); | |
135 ASSERT_EQ(1u, tasks.size()); | |
136 | |
137 // Now the time should be delay minus whatever the value of now happens to be. | |
138 scheduled_delay = base::TimeTicks() + (scheduled_run_time - notifier.Now()); | |
139 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun()); | |
140 | |
141 // Move to exactly the run time (time: 50, run time: 50). | |
142 notifier.SetNow(notifier.Now() + base::TimeDelta::FromInternalValue(1)); | |
143 | |
144 // It's time to run! | |
145 tasks[0].task.Run(); | |
146 EXPECT_EQ(1, NotificationCount()); | |
147 | |
148 tasks = TakePendingTasks(); | |
149 EXPECT_EQ(0u, tasks.size()); | |
150 } | |
151 | |
152 TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { | |
153 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); | |
154 TestNotifier notifier( | |
155 task_runner_, | |
156 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
157 delay); | |
158 | |
159 base::TimeTicks schedule_time; | |
160 // Move time 19 units forward and reschedule, expecting that we still need to | |
161 // run in |delay| time and we don't get a notification. | |
162 for (int i = 0; i < 10; ++i) { | |
163 EXPECT_EQ(0, NotificationCount()); | |
164 | |
165 // Move time forward 19 units. | |
166 schedule_time = notifier.Now() + base::TimeDelta::FromInternalValue(19); | |
167 notifier.SetNow(schedule_time); | |
168 notifier.Schedule(); | |
169 | |
170 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | |
171 | |
172 ASSERT_EQ(1u, tasks.size()); | |
173 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | |
174 | |
175 // It's not yet time to run, so we expect no notifications. | |
176 tasks[0].task.Run(); | |
177 EXPECT_EQ(0, NotificationCount()); | |
178 } | |
179 | |
180 // Move time forward 20 units, expecting a notification. | |
181 schedule_time = notifier.Now() + base::TimeDelta::FromInternalValue(20); | |
182 notifier.SetNow(schedule_time); | |
183 | |
184 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | |
185 | |
186 ASSERT_EQ(1u, tasks.size()); | |
187 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | |
188 | |
189 // Time to run! | |
190 tasks[0].task.Run(); | |
191 EXPECT_EQ(1, NotificationCount()); | |
192 } | |
193 | |
194 TEST_F(DelayedUniqueNotifierTest, Cancel) { | |
reveman
2014/05/28 19:50:02
As part of this test, can you add a simple check t
vmpstr
2014/05/28 20:17:13
Done.
| |
195 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); | |
196 TestNotifier notifier( | |
197 task_runner_, | |
198 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
199 delay); | |
200 | |
201 EXPECT_EQ(0, NotificationCount()); | |
202 | |
203 // Schedule for |delay| seconds from now. | |
204 base::TimeTicks schedule_time = | |
205 notifier.Now() + base::TimeDelta::FromInternalValue(10); | |
206 notifier.SetNow(schedule_time); | |
207 notifier.Schedule(); | |
208 | |
209 // Cancel the run. | |
210 notifier.Cancel(); | |
211 | |
212 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | |
213 | |
214 ASSERT_EQ(1u, tasks.size()); | |
215 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | |
216 | |
217 // Time to run, but a canceled task! | |
218 tasks[0].task.Run(); | |
219 EXPECT_EQ(0, NotificationCount()); | |
220 | |
221 tasks = TakePendingTasks(); | |
222 EXPECT_EQ(0u, tasks.size()); | |
223 } | |
224 | |
225 } // namespace | |
226 } // namespace cc | |
OLD | NEW |