Chromium Code Reviews| 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 "base/bind.h" | |
| 6 #include "base/bind_helpers.h" | |
| 7 #include "base/message_loop/message_loop_proxy.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "cc/base/delayed_unique_notifier.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 namespace cc { | |
| 14 namespace { | |
| 15 | |
| 16 class DelayedUniqueNotifierTest : public testing::Test { | |
| 17 public: | |
| 18 DelayedUniqueNotifierTest() : notification_count_(0) {} | |
| 19 | |
| 20 virtual void SetUp() OVERRIDE { | |
| 21 notification_count_ = 0; | |
| 22 last_notification_time_ = base::TimeTicks(); | |
| 23 } | |
| 24 | |
| 25 void Notify() { | |
| 26 ++notification_count_; | |
| 27 last_notification_time_ = base::TimeTicks::Now(); | |
| 28 } | |
| 29 | |
| 30 int NotificationCount() const { return notification_count_; } | |
| 31 | |
| 32 base::TimeTicks LastNotificationTime() const { | |
| 33 return last_notification_time_; | |
| 34 } | |
| 35 | |
| 36 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.
| |
| 37 base::RunLoop run_loop; | |
| 38 base::MessageLoopProxy::current()->PostDelayedTask( | |
| 39 FROM_HERE, run_loop.QuitClosure(), delay); | |
| 40 run_loop.Run(); | |
| 41 } | |
| 42 | |
| 43 protected: | |
| 44 int notification_count_; | |
| 45 base::TimeTicks last_notification_time_; | |
| 46 }; | |
| 47 | |
| 48 TEST_F(DelayedUniqueNotifierTest, HalfSecondDelay) { | |
| 49 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)
| |
| 50 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.
| |
| 51 DelayedUniqueNotifier notifier( | |
| 52 base::MessageLoopProxy::current(), | |
| 53 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
| 54 delay); | |
| 55 | |
| 56 EXPECT_EQ(0, NotificationCount()); | |
| 57 | |
| 58 // Basic schedule for 0.5 seconds from now. | |
| 59 base::TimeTicks expected_time = base::TimeTicks::Now() + delay; | |
| 60 notifier.Schedule(); | |
| 61 | |
| 62 // Expect that we ran only once at least |delay| from now. | |
| 63 RunForTime(run_delay); | |
| 64 EXPECT_EQ(1, NotificationCount()); | |
| 65 EXPECT_GE(LastNotificationTime(), expected_time); | |
| 66 | |
| 67 // 5 schedules should result in only one run. | |
| 68 for (int i = 0; i < 5; ++i) { | |
| 69 expected_time = base::TimeTicks::Now() + delay; | |
| 70 notifier.Schedule(); | |
| 71 } | |
| 72 | |
| 73 RunForTime(run_delay); | |
| 74 EXPECT_EQ(2, NotificationCount()); | |
| 75 EXPECT_GE(LastNotificationTime(), expected_time); | |
| 76 } | |
| 77 | |
| 78 TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { | |
| 79 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(500); | |
| 80 base::TimeDelta run_delay = base::TimeDelta::FromMilliseconds(600); | |
| 81 DelayedUniqueNotifier notifier( | |
| 82 base::MessageLoopProxy::current(), | |
| 83 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
| 84 delay); | |
| 85 | |
| 86 EXPECT_EQ(0, NotificationCount()); | |
| 87 | |
| 88 // Schedule constantly for some large number of times, so that the expected | |
| 89 // time goes beyond |delay| from now by some more or less considerable time. | |
| 90 base::TimeTicks expected_time; | |
| 91 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.
| |
| 92 expected_time = base::TimeTicks::Now() + delay; | |
| 93 notifier.Schedule(); | |
| 94 } | |
| 95 | |
| 96 // Finish running and expect only one run. | |
| 97 RunForTime(run_delay); | |
| 98 EXPECT_EQ(1, NotificationCount()); | |
| 99 | |
| 100 EXPECT_GE(LastNotificationTime(), expected_time); | |
| 101 } | |
| 102 | |
| 103 TEST_F(DelayedUniqueNotifierTest, Cancel) { | |
| 104 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(500); | |
| 105 base::TimeDelta run_delay = base::TimeDelta::FromMilliseconds(600); | |
| 106 DelayedUniqueNotifier notifier( | |
| 107 base::MessageLoopProxy::current(), | |
| 108 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
| 109 delay); | |
| 110 | |
| 111 EXPECT_EQ(0, NotificationCount()); | |
| 112 | |
| 113 // Schedule for |delay| seconds from now. | |
| 114 notifier.Schedule(); | |
| 115 | |
| 116 // Cancel the run. | |
| 117 notifier.Cancel(); | |
| 118 | |
| 119 RunForTime(run_delay); | |
| 120 EXPECT_EQ(0, NotificationCount()); | |
| 121 | |
| 122 // Schedule for |delay| seconds from now and let it run. | |
| 123 base::TimeTicks expected_time = base::TimeTicks::Now() + delay; | |
| 124 notifier.Schedule(); | |
| 125 | |
| 126 RunForTime(run_delay); | |
| 127 EXPECT_EQ(1, NotificationCount()); | |
| 128 EXPECT_GE(LastNotificationTime(), expected_time); | |
| 129 | |
| 130 // Schedule for |delay| seconds from now. | |
| 131 notifier.Schedule(); | |
| 132 | |
| 133 // Cancel the run again. | |
| 134 notifier.Cancel(); | |
| 135 | |
| 136 RunForTime(run_delay); | |
| 137 EXPECT_EQ(1, NotificationCount()); | |
| 138 } | |
| 139 } // namespace | |
| 140 } // namespace cc | |
| OLD | NEW |