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..04a0e01bdbe20d3d20c86bc7102fc0fd6e9e4662 |
--- /dev/null |
+++ b/cc/base/delayed_unique_notifier_unittest.cc |
@@ -0,0 +1,140 @@ |
+// 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 "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/message_loop/message_loop_proxy.h" |
+#include "base/run_loop.h" |
+#include "base/single_thread_task_runner.h" |
+#include "cc/base/delayed_unique_notifier.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace cc { |
+namespace { |
+ |
+class DelayedUniqueNotifierTest : public testing::Test { |
+ public: |
+ DelayedUniqueNotifierTest() : notification_count_(0) {} |
+ |
+ virtual void SetUp() OVERRIDE { |
+ notification_count_ = 0; |
+ last_notification_time_ = base::TimeTicks(); |
+ } |
+ |
+ void Notify() { |
+ ++notification_count_; |
+ last_notification_time_ = base::TimeTicks::Now(); |
+ } |
+ |
+ int NotificationCount() const { return notification_count_; } |
+ |
+ base::TimeTicks LastNotificationTime() const { |
+ return last_notification_time_; |
+ } |
+ |
+ void RunForTime(base::TimeDelta delay) { |
vmpstr
2014/05/27 18:14:59
Note that base::RunLoop().RunUntilIdle() doesn't s
reveman
2014/05/27 19:02:17
How about changing Notify() to quit the message lo
vmpstr
2014/05/27 19:25:49
Done.
|
+ base::RunLoop run_loop; |
+ base::MessageLoopProxy::current()->PostDelayedTask( |
+ FROM_HERE, run_loop.QuitClosure(), delay); |
+ run_loop.Run(); |
+ } |
+ |
+ protected: |
+ int notification_count_; |
+ base::TimeTicks last_notification_time_; |
+}; |
+ |
+TEST_F(DelayedUniqueNotifierTest, HalfSecondDelay) { |
+ base::TimeDelta delay = base::TimeDelta::FromMilliseconds(500); |
reveman
2014/05/27 19:02:17
Could this delay be 0 or TimeDelta::FromInternalVa
vmpstr
2014/05/27 19:25:49
Set to to FromInternalValue(1)
|
+ base::TimeDelta run_delay = base::TimeDelta::FromMilliseconds(600); |
vmpstr
2014/05/27 18:14:59
This is a delay just so if we schedule on the same
reveman
2014/05/27 19:02:17
My suggestion above would allow you to remove the
vmpstr
2014/05/27 19:25:49
Done.
|
+ DelayedUniqueNotifier notifier( |
+ base::MessageLoopProxy::current(), |
+ base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
+ delay); |
+ |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ // Basic schedule for 0.5 seconds from now. |
+ base::TimeTicks expected_time = base::TimeTicks::Now() + delay; |
+ notifier.Schedule(); |
+ |
+ // Expect that we ran only once at least |delay| from now. |
+ RunForTime(run_delay); |
+ EXPECT_EQ(1, NotificationCount()); |
+ EXPECT_GE(LastNotificationTime(), expected_time); |
+ |
+ // 5 schedules should result in only one run. |
+ for (int i = 0; i < 5; ++i) { |
+ expected_time = base::TimeTicks::Now() + delay; |
+ notifier.Schedule(); |
+ } |
+ |
+ RunForTime(run_delay); |
+ EXPECT_EQ(2, NotificationCount()); |
+ EXPECT_GE(LastNotificationTime(), expected_time); |
+} |
+ |
+TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { |
+ base::TimeDelta delay = base::TimeDelta::FromMilliseconds(500); |
+ base::TimeDelta run_delay = base::TimeDelta::FromMilliseconds(600); |
+ DelayedUniqueNotifier notifier( |
+ base::MessageLoopProxy::current(), |
+ base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
+ delay); |
+ |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ // Schedule constantly for some large number of times, so that the expected |
+ // time goes beyond |delay| from now by some more or less considerable time. |
+ base::TimeTicks expected_time; |
+ for (int i = 0; i < 100000; ++i) { |
vmpstr
2014/05/27 18:14:59
I could do some sort of a sleep here (as long as I
reveman
2014/05/27 19:02:17
100000 seems a bit random. Maybe we can just busy
vmpstr
2014/05/27 19:25:49
Done.
|
+ expected_time = base::TimeTicks::Now() + delay; |
+ notifier.Schedule(); |
+ } |
+ |
+ // Finish running and expect only one run. |
+ RunForTime(run_delay); |
+ EXPECT_EQ(1, NotificationCount()); |
+ |
+ EXPECT_GE(LastNotificationTime(), expected_time); |
+} |
+ |
+TEST_F(DelayedUniqueNotifierTest, Cancel) { |
+ base::TimeDelta delay = base::TimeDelta::FromMilliseconds(500); |
+ base::TimeDelta run_delay = base::TimeDelta::FromMilliseconds(600); |
+ DelayedUniqueNotifier notifier( |
+ base::MessageLoopProxy::current(), |
+ base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
+ delay); |
+ |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ // Schedule for |delay| seconds from now. |
+ notifier.Schedule(); |
+ |
+ // Cancel the run. |
+ notifier.Cancel(); |
+ |
+ RunForTime(run_delay); |
+ EXPECT_EQ(0, NotificationCount()); |
+ |
+ // Schedule for |delay| seconds from now and let it run. |
+ base::TimeTicks expected_time = base::TimeTicks::Now() + delay; |
+ notifier.Schedule(); |
+ |
+ RunForTime(run_delay); |
+ EXPECT_EQ(1, NotificationCount()); |
+ EXPECT_GE(LastNotificationTime(), expected_time); |
+ |
+ // Schedule for |delay| seconds from now. |
+ notifier.Schedule(); |
+ |
+ // Cancel the run again. |
+ notifier.Cancel(); |
+ |
+ RunForTime(run_delay); |
+ EXPECT_EQ(1, NotificationCount()); |
+} |
+} // namespace |
+} // namespace cc |