OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include <deque> | 5 #include <deque> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/test/test_pending_task.h" | 9 #include "base/test/test_pending_task.h" |
10 #include "base/test/test_simple_task_runner.h" | 10 #include "base/test/test_simple_task_runner.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 base::TimeTicks schedule_time = | 66 base::TimeTicks schedule_time = |
67 base::TimeTicks() + base::TimeDelta::FromInternalValue(10); | 67 base::TimeTicks() + base::TimeDelta::FromInternalValue(10); |
68 | 68 |
69 notifier.SetNow(schedule_time); | 69 notifier.SetNow(schedule_time); |
70 notifier.Schedule(); | 70 notifier.Schedule(); |
71 | 71 |
72 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | 72 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
73 ASSERT_EQ(1u, tasks.size()); | 73 ASSERT_EQ(1u, tasks.size()); |
74 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 74 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
75 | 75 |
76 tasks[0].task.Run(); | 76 std::move(tasks[0].task).Run(); |
77 EXPECT_EQ(1, NotificationCount()); | 77 EXPECT_EQ(1, NotificationCount()); |
78 | 78 |
79 // 5 schedules should result in only one run. | 79 // 5 schedules should result in only one run. |
80 for (int i = 0; i < 5; ++i) | 80 for (int i = 0; i < 5; ++i) |
81 notifier.Schedule(); | 81 notifier.Schedule(); |
82 | 82 |
83 tasks = TakePendingTasks(); | 83 tasks = TakePendingTasks(); |
84 ASSERT_EQ(1u, tasks.size()); | 84 ASSERT_EQ(1u, tasks.size()); |
85 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 85 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
86 | 86 |
87 tasks[0].task.Run(); | 87 std::move(tasks[0].task).Run(); |
88 EXPECT_EQ(2, NotificationCount()); | 88 EXPECT_EQ(2, NotificationCount()); |
89 } | 89 } |
90 | 90 |
91 TEST_F(DelayedUniqueNotifierTest, SmallDelay) { | 91 TEST_F(DelayedUniqueNotifierTest, SmallDelay) { |
92 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); | 92 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); |
93 TestNotifier notifier( | 93 TestNotifier notifier( |
94 task_runner_.get(), | 94 task_runner_.get(), |
95 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | 95 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
96 delay); | 96 delay); |
97 | 97 |
98 EXPECT_EQ(0, NotificationCount()); | 98 EXPECT_EQ(0, NotificationCount()); |
99 | 99 |
100 // Basic schedule for |delay| from now (now: 30, run time: 50). | 100 // Basic schedule for |delay| from now (now: 30, run time: 50). |
101 base::TimeTicks schedule_time = | 101 base::TimeTicks schedule_time = |
102 base::TimeTicks() + base::TimeDelta::FromInternalValue(30); | 102 base::TimeTicks() + base::TimeDelta::FromInternalValue(30); |
103 | 103 |
104 notifier.SetNow(schedule_time); | 104 notifier.SetNow(schedule_time); |
105 notifier.Schedule(); | 105 notifier.Schedule(); |
106 | 106 |
107 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | 107 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
108 | 108 |
109 ASSERT_EQ(1u, tasks.size()); | 109 ASSERT_EQ(1u, tasks.size()); |
110 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 110 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
111 | 111 |
112 // It's not yet time to run, so we expect no notifications. | 112 // It's not yet time to run, so we expect no notifications. |
113 tasks[0].task.Run(); | 113 std::move(tasks[0].task).Run(); |
114 EXPECT_EQ(0, NotificationCount()); | 114 EXPECT_EQ(0, NotificationCount()); |
115 | 115 |
116 tasks = TakePendingTasks(); | 116 tasks = TakePendingTasks(); |
117 | 117 |
118 ASSERT_EQ(1u, tasks.size()); | 118 ASSERT_EQ(1u, tasks.size()); |
119 // Now the time should be delay minus whatever the value of now happens to be | 119 // Now the time should be delay minus whatever the value of now happens to be |
120 // (now: 30, run time: 50). | 120 // (now: 30, run time: 50). |
121 base::TimeTicks scheduled_run_time = notifier.Now() + delay; | 121 base::TimeTicks scheduled_run_time = notifier.Now() + delay; |
122 base::TimeTicks scheduled_delay = | 122 base::TimeTicks scheduled_delay = |
123 base::TimeTicks() + (scheduled_run_time - notifier.Now()); | 123 base::TimeTicks() + (scheduled_run_time - notifier.Now()); |
124 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun()); | 124 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun()); |
125 | 125 |
126 // Move closer to the run time (time: 49, run time: 50). | 126 // Move closer to the run time (time: 49, run time: 50). |
127 notifier.SetNow(notifier.Now() + base::TimeDelta::FromInternalValue(19)); | 127 notifier.SetNow(notifier.Now() + base::TimeDelta::FromInternalValue(19)); |
128 | 128 |
129 // It's not yet time to run, so we expect no notifications. | 129 // It's not yet time to run, so we expect no notifications. |
130 tasks[0].task.Run(); | 130 std::move(tasks[0].task).Run(); |
131 EXPECT_EQ(0, NotificationCount()); | 131 EXPECT_EQ(0, NotificationCount()); |
132 | 132 |
133 tasks = TakePendingTasks(); | 133 tasks = TakePendingTasks(); |
134 ASSERT_EQ(1u, tasks.size()); | 134 ASSERT_EQ(1u, tasks.size()); |
135 | 135 |
136 // Now the time should be delay minus whatever the value of now happens to be. | 136 // Now the time should be delay minus whatever the value of now happens to be. |
137 scheduled_delay = base::TimeTicks() + (scheduled_run_time - notifier.Now()); | 137 scheduled_delay = base::TimeTicks() + (scheduled_run_time - notifier.Now()); |
138 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun()); | 138 EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun()); |
139 | 139 |
140 // Move to exactly the run time (time: 50, run time: 50). | 140 // Move to exactly the run time (time: 50, run time: 50). |
141 notifier.SetNow(notifier.Now() + base::TimeDelta::FromInternalValue(1)); | 141 notifier.SetNow(notifier.Now() + base::TimeDelta::FromInternalValue(1)); |
142 | 142 |
143 // It's time to run! | 143 // It's time to run! |
144 tasks[0].task.Run(); | 144 std::move(tasks[0].task).Run(); |
145 EXPECT_EQ(1, NotificationCount()); | 145 EXPECT_EQ(1, NotificationCount()); |
146 | 146 |
147 tasks = TakePendingTasks(); | 147 tasks = TakePendingTasks(); |
148 EXPECT_EQ(0u, tasks.size()); | 148 EXPECT_EQ(0u, tasks.size()); |
149 } | 149 } |
150 | 150 |
151 TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { | 151 TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { |
152 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); | 152 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); |
153 TestNotifier notifier( | 153 TestNotifier notifier( |
154 task_runner_.get(), | 154 task_runner_.get(), |
(...skipping 10 matching lines...) Expand all Loading... |
165 schedule_time = notifier.Now() + base::TimeDelta::FromInternalValue(19); | 165 schedule_time = notifier.Now() + base::TimeDelta::FromInternalValue(19); |
166 notifier.SetNow(schedule_time); | 166 notifier.SetNow(schedule_time); |
167 notifier.Schedule(); | 167 notifier.Schedule(); |
168 | 168 |
169 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | 169 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
170 | 170 |
171 ASSERT_EQ(1u, tasks.size()); | 171 ASSERT_EQ(1u, tasks.size()); |
172 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 172 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
173 | 173 |
174 // It's not yet time to run, so we expect no notifications. | 174 // It's not yet time to run, so we expect no notifications. |
175 tasks[0].task.Run(); | 175 std::move(tasks[0].task).Run(); |
176 EXPECT_EQ(0, NotificationCount()); | 176 EXPECT_EQ(0, NotificationCount()); |
177 } | 177 } |
178 | 178 |
179 // Move time forward 20 units, expecting a notification. | 179 // Move time forward 20 units, expecting a notification. |
180 schedule_time = notifier.Now() + base::TimeDelta::FromInternalValue(20); | 180 schedule_time = notifier.Now() + base::TimeDelta::FromInternalValue(20); |
181 notifier.SetNow(schedule_time); | 181 notifier.SetNow(schedule_time); |
182 | 182 |
183 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | 183 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
184 | 184 |
185 ASSERT_EQ(1u, tasks.size()); | 185 ASSERT_EQ(1u, tasks.size()); |
186 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 186 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
187 | 187 |
188 // Time to run! | 188 // Time to run! |
189 tasks[0].task.Run(); | 189 std::move(tasks[0].task).Run(); |
190 EXPECT_EQ(1, NotificationCount()); | 190 EXPECT_EQ(1, NotificationCount()); |
191 } | 191 } |
192 | 192 |
193 TEST_F(DelayedUniqueNotifierTest, CancelAndHasPendingNotification) { | 193 TEST_F(DelayedUniqueNotifierTest, CancelAndHasPendingNotification) { |
194 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); | 194 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); |
195 TestNotifier notifier( | 195 TestNotifier notifier( |
196 task_runner_.get(), | 196 task_runner_.get(), |
197 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | 197 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
198 delay); | 198 delay); |
199 | 199 |
200 EXPECT_EQ(0, NotificationCount()); | 200 EXPECT_EQ(0, NotificationCount()); |
201 | 201 |
202 // Schedule for |delay| seconds from now. | 202 // Schedule for |delay| seconds from now. |
203 base::TimeTicks schedule_time = | 203 base::TimeTicks schedule_time = |
204 notifier.Now() + base::TimeDelta::FromInternalValue(10); | 204 notifier.Now() + base::TimeDelta::FromInternalValue(10); |
205 notifier.SetNow(schedule_time); | 205 notifier.SetNow(schedule_time); |
206 notifier.Schedule(); | 206 notifier.Schedule(); |
207 EXPECT_TRUE(notifier.HasPendingNotification()); | 207 EXPECT_TRUE(notifier.HasPendingNotification()); |
208 | 208 |
209 // Cancel the run. | 209 // Cancel the run. |
210 notifier.Cancel(); | 210 notifier.Cancel(); |
211 EXPECT_FALSE(notifier.HasPendingNotification()); | 211 EXPECT_FALSE(notifier.HasPendingNotification()); |
212 | 212 |
213 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | 213 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
214 | 214 |
215 ASSERT_EQ(1u, tasks.size()); | 215 ASSERT_EQ(1u, tasks.size()); |
216 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 216 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
217 | 217 |
218 // Time to run, but a canceled task! | 218 // Time to run, but a canceled task! |
219 tasks[0].task.Run(); | 219 std::move(tasks[0].task).Run(); |
220 EXPECT_EQ(0, NotificationCount()); | 220 EXPECT_EQ(0, NotificationCount()); |
221 EXPECT_FALSE(notifier.HasPendingNotification()); | 221 EXPECT_FALSE(notifier.HasPendingNotification()); |
222 | 222 |
223 tasks = TakePendingTasks(); | 223 tasks = TakePendingTasks(); |
224 EXPECT_EQ(0u, tasks.size()); | 224 EXPECT_EQ(0u, tasks.size()); |
225 | 225 |
226 notifier.Schedule(); | 226 notifier.Schedule(); |
227 EXPECT_TRUE(notifier.HasPendingNotification()); | 227 EXPECT_TRUE(notifier.HasPendingNotification()); |
228 tasks = TakePendingTasks(); | 228 tasks = TakePendingTasks(); |
229 | 229 |
230 ASSERT_EQ(1u, tasks.size()); | 230 ASSERT_EQ(1u, tasks.size()); |
231 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 231 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
232 | 232 |
233 // Advance the time. | 233 // Advance the time. |
234 notifier.SetNow(notifier.Now() + delay); | 234 notifier.SetNow(notifier.Now() + delay); |
235 | 235 |
236 // This should run since it wasn't canceled. | 236 // This should run since it wasn't canceled. |
237 tasks[0].task.Run(); | 237 std::move(tasks[0].task).Run(); |
238 EXPECT_EQ(1, NotificationCount()); | 238 EXPECT_EQ(1, NotificationCount()); |
239 EXPECT_FALSE(notifier.HasPendingNotification()); | 239 EXPECT_FALSE(notifier.HasPendingNotification()); |
240 | 240 |
241 for (int i = 0; i < 10; ++i) { | 241 for (int i = 0; i < 10; ++i) { |
242 notifier.Schedule(); | 242 notifier.Schedule(); |
243 EXPECT_TRUE(notifier.HasPendingNotification()); | 243 EXPECT_TRUE(notifier.HasPendingNotification()); |
244 notifier.Cancel(); | 244 notifier.Cancel(); |
245 EXPECT_FALSE(notifier.HasPendingNotification()); | 245 EXPECT_FALSE(notifier.HasPendingNotification()); |
246 } | 246 } |
247 | 247 |
248 tasks = TakePendingTasks(); | 248 tasks = TakePendingTasks(); |
249 | 249 |
250 ASSERT_EQ(1u, tasks.size()); | 250 ASSERT_EQ(1u, tasks.size()); |
251 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 251 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
252 | 252 |
253 // Time to run, but a canceled task! | 253 // Time to run, but a canceled task! |
254 notifier.SetNow(notifier.Now() + delay); | 254 notifier.SetNow(notifier.Now() + delay); |
255 tasks[0].task.Run(); | 255 std::move(tasks[0].task).Run(); |
256 EXPECT_EQ(1, NotificationCount()); | 256 EXPECT_EQ(1, NotificationCount()); |
257 | 257 |
258 tasks = TakePendingTasks(); | 258 tasks = TakePendingTasks(); |
259 EXPECT_EQ(0u, tasks.size()); | 259 EXPECT_EQ(0u, tasks.size()); |
260 EXPECT_FALSE(notifier.HasPendingNotification()); | 260 EXPECT_FALSE(notifier.HasPendingNotification()); |
261 } | 261 } |
262 | 262 |
263 TEST_F(DelayedUniqueNotifierTest, ShutdownWithScheduledTask) { | 263 TEST_F(DelayedUniqueNotifierTest, ShutdownWithScheduledTask) { |
264 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); | 264 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); |
265 TestNotifier notifier( | 265 TestNotifier notifier( |
(...skipping 11 matching lines...) Expand all Loading... |
277 EXPECT_TRUE(notifier.HasPendingNotification()); | 277 EXPECT_TRUE(notifier.HasPendingNotification()); |
278 | 278 |
279 // Shutdown the notifier. | 279 // Shutdown the notifier. |
280 notifier.Shutdown(); | 280 notifier.Shutdown(); |
281 | 281 |
282 // The task is still there, but... | 282 // The task is still there, but... |
283 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | 283 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
284 ASSERT_EQ(1u, tasks.size()); | 284 ASSERT_EQ(1u, tasks.size()); |
285 | 285 |
286 // Running the task after shutdown does nothing since it's cancelled. | 286 // Running the task after shutdown does nothing since it's cancelled. |
287 tasks[0].task.Run(); | 287 std::move(tasks[0].task).Run(); |
288 EXPECT_EQ(0, NotificationCount()); | 288 EXPECT_EQ(0, NotificationCount()); |
289 | 289 |
290 tasks = TakePendingTasks(); | 290 tasks = TakePendingTasks(); |
291 EXPECT_EQ(0u, tasks.size()); | 291 EXPECT_EQ(0u, tasks.size()); |
292 | 292 |
293 // We are no longer able to schedule tasks. | 293 // We are no longer able to schedule tasks. |
294 notifier.Schedule(); | 294 notifier.Schedule(); |
295 tasks = TakePendingTasks(); | 295 tasks = TakePendingTasks(); |
296 ASSERT_EQ(0u, tasks.size()); | 296 ASSERT_EQ(0u, tasks.size()); |
297 | 297 |
(...skipping 26 matching lines...) Expand all Loading... |
324 ASSERT_EQ(0u, tasks.size()); | 324 ASSERT_EQ(0u, tasks.size()); |
325 | 325 |
326 // Verify after the scheduled time happens there is still no task. | 326 // Verify after the scheduled time happens there is still no task. |
327 notifier.SetNow(notifier.Now() + delay); | 327 notifier.SetNow(notifier.Now() + delay); |
328 tasks = TakePendingTasks(); | 328 tasks = TakePendingTasks(); |
329 ASSERT_EQ(0u, tasks.size()); | 329 ASSERT_EQ(0u, tasks.size()); |
330 } | 330 } |
331 | 331 |
332 } // namespace | 332 } // namespace |
333 } // namespace cc | 333 } // namespace cc |
OLD | NEW |