Index: cc/base/delayed_unique_notifier_unittest.cc |
diff --git a/cc/base/delayed_unique_notifier_unittest.cc b/cc/base/delayed_unique_notifier_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1a38232145c925709abd7e4ee4fb6d73cf5075c9 |
--- /dev/null |
+++ b/cc/base/delayed_unique_notifier_unittest.cc |
@@ -0,0 +1,226 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <deque> |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/test/test_pending_task.h" |
+#include "base/test/test_simple_task_runner.h" |
+#include "cc/base/delayed_unique_notifier.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace cc { |
+namespace { |
+ |
+class TestNotifier : public DelayedUniqueNotifier { |
+ public: |
+ TestNotifier(base::SequencedTaskRunner* task_runner, |
+ const base::Closure& closure, |
+ const base::TimeDelta& delay) |
+ : DelayedUniqueNotifier(task_runner, closure, delay) {} |
+ virtual ~TestNotifier() {} |
+ |
+ 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.
|
+ |
+ void SetNow(base::TimeTicks now) { now_ = now; } |
+ |
+ private: |
+ base::TimeTicks now_; |
+}; |
+ |
+class DelayedUniqueNotifierTest : public testing::Test { |
+ public: |
+ DelayedUniqueNotifierTest() : notification_count_(0) {} |
+ |
+ virtual void SetUp() OVERRIDE { |
+ notification_count_ = 0; |
+ task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner); |
+ } |
+ |
+ void Notify() { ++notification_count_; } |
+ |
+ int NotificationCount() const { return notification_count_; } |
+ |
+ std::deque<base::TestPendingTask> TakePendingTasks() { |
+ std::deque<base::TestPendingTask> tasks = task_runner_->GetPendingTasks(); |
+ task_runner_->ClearPendingTasks(); |
+ return tasks; |
+ } |
+ |
+ protected: |
+ int notification_count_; |
+ scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
+}; |
+ |
+TEST_F(DelayedUniqueNotifierTest, ZeroDelay) { |
+ base::TimeDelta delay = base::TimeDelta::FromInternalValue(0); |
+ TestNotifier notifier( |
+ task_runner_, |
+ base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
+ delay); |
+ |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ // Basic schedule for |delay| from now. |
+ base::TimeTicks schedule_time = |
+ base::TimeTicks() + base::TimeDelta::FromInternalValue(10); |
+ |
+ notifier.SetNow(schedule_time); |
+ notifier.Schedule(); |
+ |
+ std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
+ ASSERT_EQ(1u, tasks.size()); |
+ EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
+ |
+ tasks[0].task.Run(); |
+ EXPECT_EQ(1, NotificationCount()); |
+ |
+ // 5 schedules should result in only one run. |
+ for (int i = 0; i < 5; ++i) |
+ notifier.Schedule(); |
+ |
+ tasks = TakePendingTasks(); |
+ ASSERT_EQ(1u, tasks.size()); |
+ EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
+ |
+ tasks[0].task.Run(); |
+ EXPECT_EQ(2, NotificationCount()); |
+} |
+ |
+TEST_F(DelayedUniqueNotifierTest, SmallDelay) { |
+ base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); |
+ TestNotifier notifier( |
+ task_runner_, |
+ base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
+ delay); |
+ |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ // Basic schedule for |delay| from now (now: 30, run time: 50). |
+ base::TimeTicks schedule_time = |
+ base::TimeTicks() + base::TimeDelta::FromInternalValue(30); |
+ |
+ notifier.SetNow(schedule_time); |
+ notifier.Schedule(); |
+ |
+ std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
+ |
+ ASSERT_EQ(1u, tasks.size()); |
+ EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
+ |
+ // It's not yet time to run, so we expect no notifications. |
+ tasks[0].task.Run(); |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ tasks = TakePendingTasks(); |
+ |
+ ASSERT_EQ(1u, tasks.size()); |
+ // Now the time should be delay minus whatever the value of now happens to be |
+ // (now: 30, run time: 50). |
+ base::TimeTicks scheduled_run_time = notifier.Now() + delay; |
+ base::TimeTicks scheduled_delay = |
+ base::TimeTicks() + (scheduled_run_time - notifier.Now()); |
+ EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun()); |
+ |
+ // Move closer to the run time (time: 49, run time: 50). |
+ notifier.SetNow(notifier.Now() + base::TimeDelta::FromInternalValue(19)); |
+ |
+ // It's not yet time to run, so we expect no notifications. |
+ tasks[0].task.Run(); |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ tasks = TakePendingTasks(); |
+ ASSERT_EQ(1u, tasks.size()); |
+ |
+ // Now the time should be delay minus whatever the value of now happens to be. |
+ scheduled_delay = base::TimeTicks() + (scheduled_run_time - notifier.Now()); |
+ EXPECT_EQ(scheduled_delay, tasks[0].GetTimeToRun()); |
+ |
+ // Move to exactly the run time (time: 50, run time: 50). |
+ notifier.SetNow(notifier.Now() + base::TimeDelta::FromInternalValue(1)); |
+ |
+ // It's time to run! |
+ tasks[0].task.Run(); |
+ EXPECT_EQ(1, NotificationCount()); |
+ |
+ tasks = TakePendingTasks(); |
+ EXPECT_EQ(0u, tasks.size()); |
+} |
+ |
+TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { |
+ base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); |
+ TestNotifier notifier( |
+ task_runner_, |
+ base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
+ delay); |
+ |
+ base::TimeTicks schedule_time; |
+ // Move time 19 units forward and reschedule, expecting that we still need to |
+ // run in |delay| time and we don't get a notification. |
+ for (int i = 0; i < 10; ++i) { |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ // Move time forward 19 units. |
+ schedule_time = notifier.Now() + base::TimeDelta::FromInternalValue(19); |
+ notifier.SetNow(schedule_time); |
+ notifier.Schedule(); |
+ |
+ std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
+ |
+ ASSERT_EQ(1u, tasks.size()); |
+ EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
+ |
+ // It's not yet time to run, so we expect no notifications. |
+ tasks[0].task.Run(); |
+ EXPECT_EQ(0, NotificationCount()); |
+ } |
+ |
+ // Move time forward 20 units, expecting a notification. |
+ schedule_time = notifier.Now() + base::TimeDelta::FromInternalValue(20); |
+ notifier.SetNow(schedule_time); |
+ |
+ std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
+ |
+ ASSERT_EQ(1u, tasks.size()); |
+ EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
+ |
+ // Time to run! |
+ tasks[0].task.Run(); |
+ EXPECT_EQ(1, NotificationCount()); |
+} |
+ |
+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.
|
+ base::TimeDelta delay = base::TimeDelta::FromInternalValue(20); |
+ TestNotifier notifier( |
+ task_runner_, |
+ base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
+ delay); |
+ |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ // Schedule for |delay| seconds from now. |
+ base::TimeTicks schedule_time = |
+ notifier.Now() + base::TimeDelta::FromInternalValue(10); |
+ notifier.SetNow(schedule_time); |
+ notifier.Schedule(); |
+ |
+ // Cancel the run. |
+ notifier.Cancel(); |
+ |
+ std::deque<base::TestPendingTask> tasks = TakePendingTasks(); |
+ |
+ ASSERT_EQ(1u, tasks.size()); |
+ EXPECT_EQ(base::TimeTicks() + delay, tasks[0].GetTimeToRun()); |
+ |
+ // Time to run, but a canceled task! |
+ tasks[0].task.Run(); |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ tasks = TakePendingTasks(); |
+ EXPECT_EQ(0u, tasks.size()); |
+} |
+ |
+} // namespace |
+} // namespace cc |