Chromium Code Reviews| 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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | 185 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
| 186 | 186 |
| 187 ASSERT_EQ(1u, tasks.size()); | 187 ASSERT_EQ(1u, tasks.size()); |
| 188 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 188 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
| 189 | 189 |
| 190 // Time to run! | 190 // Time to run! |
| 191 tasks[0].task.Run(); | 191 tasks[0].task.Run(); |
| 192 EXPECT_EQ(1, NotificationCount()); | 192 EXPECT_EQ(1, NotificationCount()); |
| 193 } | 193 } |
| 194 | 194 |
| 195 TEST_F(DelayedUniqueNotifierTest, Cancel) { | 195 TEST_F(DelayedUniqueNotifierTest, CancelAndHasPendingNotification) { |
| 196 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); | 196 base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); |
| 197 TestNotifier notifier( | 197 TestNotifier notifier( |
| 198 task_runner_, | 198 task_runner_, |
| 199 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | 199 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
| 200 delay); | 200 delay); |
| 201 | 201 |
| 202 EXPECT_EQ(0, NotificationCount()); | 202 EXPECT_EQ(0, NotificationCount()); |
| 203 | 203 |
| 204 // Schedule for |delay| seconds from now. | 204 // Schedule for |delay| seconds from now. |
| 205 base::TimeTicks schedule_time = | 205 base::TimeTicks schedule_time = |
| 206 notifier.Now() + base::TimeDelta::FromInternalValue(10); | 206 notifier.Now() + base::TimeDelta::FromInternalValue(10); |
| 207 notifier.SetNow(schedule_time); | 207 notifier.SetNow(schedule_time); |
| 208 notifier.Schedule(); | 208 notifier.Schedule(); |
| 209 EXPECT_TRUE(notifier.HasPendingNotification()); | |
| 209 | 210 |
| 210 // Cancel the run. | 211 // Cancel the run. |
| 211 notifier.Cancel(); | 212 notifier.Cancel(); |
| 213 EXPECT_FALSE(notifier.HasPendingNotification()); | |
| 212 | 214 |
| 213 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); | 215 std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
| 214 | 216 |
| 215 ASSERT_EQ(1u, tasks.size()); | 217 ASSERT_EQ(1u, tasks.size()); |
| 216 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 218 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
| 217 | 219 |
| 218 // Time to run, but a canceled task! | 220 // Time to run, but a canceled task! |
| 219 tasks[0].task.Run(); | 221 tasks[0].task.Run(); |
| 220 EXPECT_EQ(0, NotificationCount()); | 222 EXPECT_EQ(0, NotificationCount()); |
| 223 EXPECT_FALSE(notifier.HasPendingNotification()); | |
| 221 | 224 |
| 222 tasks = TakePendingTasks(); | 225 tasks = TakePendingTasks(); |
| 223 EXPECT_EQ(0u, tasks.size()); | 226 EXPECT_EQ(0u, tasks.size()); |
| 224 | 227 |
| 225 notifier.Schedule(); | 228 notifier.Schedule(); |
| 229 EXPECT_TRUE(notifier.HasPendingNotification()); | |
| 226 tasks = TakePendingTasks(); | 230 tasks = TakePendingTasks(); |
| 227 | 231 |
| 228 ASSERT_EQ(1u, tasks.size()); | 232 ASSERT_EQ(1u, tasks.size()); |
| 229 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 233 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
| 230 | 234 |
| 231 // Advance the time. | 235 // Advance the time. |
| 232 notifier.SetNow(notifier.Now() + delay); | 236 notifier.SetNow(notifier.Now() + delay); |
| 233 | 237 |
| 234 // This should run since it wasn't scheduled. | 238 // This should run since it wasn't scheduled. |
| 235 tasks[0].task.Run(); | 239 tasks[0].task.Run(); |
| 236 EXPECT_EQ(1, NotificationCount()); | 240 EXPECT_EQ(1, NotificationCount()); |
| 241 EXPECT_FALSE(notifier.HasPendingNotification()); | |
| 237 | 242 |
| 238 for (int i = 0; i < 10; ++i) | 243 for (int i = 0; i < 10; ++i) |
| 239 notifier.Schedule(); | 244 notifier.Schedule(); |
| 240 | 245 |
| 246 EXPECT_TRUE(notifier.HasPendingNotification()); | |
| 241 notifier.Cancel(); | 247 notifier.Cancel(); |
|
reveman
2014/05/30 00:19:28
While here, can you move this Cancel() call to the
vmpstr
2014/05/30 00:38:34
Done.
| |
| 248 EXPECT_FALSE(notifier.HasPendingNotification()); | |
| 242 tasks = TakePendingTasks(); | 249 tasks = TakePendingTasks(); |
| 243 | 250 |
| 244 ASSERT_EQ(1u, tasks.size()); | 251 ASSERT_EQ(1u, tasks.size()); |
| 245 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); | 252 EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
| 246 | 253 |
| 247 // Time to run, but a canceled task! | 254 // Time to run, but a canceled task! |
| 248 notifier.SetNow(notifier.Now() + delay); | 255 notifier.SetNow(notifier.Now() + delay); |
| 249 tasks[0].task.Run(); | 256 tasks[0].task.Run(); |
| 250 EXPECT_EQ(1, NotificationCount()); | 257 EXPECT_EQ(1, NotificationCount()); |
| 251 | 258 |
| 252 tasks = TakePendingTasks(); | 259 tasks = TakePendingTasks(); |
| 253 EXPECT_EQ(0u, tasks.size()); | 260 EXPECT_EQ(0u, tasks.size()); |
| 261 EXPECT_FALSE(notifier.HasPendingNotification()); | |
| 254 } | 262 } |
| 255 | 263 |
| 256 } // namespace | 264 } // namespace |
| 257 } // namespace cc | 265 } // namespace cc |
| OLD | NEW |