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() | |
| 19 : notification_count_(0), expected_notification_count_(0) {} | |
| 20 | |
| 21 virtual void SetUp() OVERRIDE { | |
| 22 notification_count_ = 0; | |
| 23 expected_notification_count_ = 0; | |
| 24 last_notification_time_ = base::TimeTicks(); | |
| 25 } | |
| 26 | |
| 27 void Notify() { | |
| 28 ++notification_count_; | |
| 29 last_notification_time_ = base::TimeTicks::Now(); | |
| 30 if (notification_count_ >= expected_notification_count_) | |
| 31 run_loop_->Quit(); | |
| 32 } | |
| 33 | |
| 34 int NotificationCount() const { return notification_count_; } | |
| 35 | |
| 36 base::TimeTicks LastNotificationTime() const { | |
| 37 return last_notification_time_; | |
| 38 } | |
| 39 | |
| 40 void RunUntilNotificationCount(int count) { | |
| 41 expected_notification_count_ = count; | |
| 42 run_loop_.reset(new base::RunLoop); | |
| 43 run_loop_->Run(); | |
| 44 | |
| 45 // Pump through the remained or messages. | |
|
reveman
2014/05/27 21:20:24
nit: I don't understand this comment
vmpstr
2014/05/27 21:50:41
Changed it a bit. The problem is that if we errone
| |
| 46 base::RunLoop().RunUntilIdle(); | |
|
reveman
2014/05/27 21:20:24
Why do we need this? I assume run_loop_.reset() do
| |
| 47 } | |
| 48 | |
| 49 protected: | |
| 50 int notification_count_; | |
| 51 int expected_notification_count_; | |
| 52 base::TimeTicks last_notification_time_; | |
| 53 scoped_ptr<base::RunLoop> run_loop_; | |
| 54 }; | |
| 55 | |
| 56 TEST_F(DelayedUniqueNotifierTest, HalfSecondDelay) { | |
|
reveman
2014/05/27 21:20:24
HalfSecondDelay is not accurate anymore
vmpstr
2014/05/27 21:50:41
Oops. Changed.
| |
| 57 base::TimeDelta delay = base::TimeDelta::FromInternalValue(1); | |
| 58 DelayedUniqueNotifier notifier( | |
| 59 base::MessageLoopProxy::current(), | |
| 60 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
| 61 delay); | |
| 62 | |
| 63 EXPECT_EQ(0, NotificationCount()); | |
| 64 | |
| 65 // Basic schedule for |delay| from now. | |
| 66 base::TimeTicks expected_time = base::TimeTicks::Now() + delay; | |
| 67 notifier.Schedule(); | |
| 68 | |
| 69 // Expect that we ran only once at least |delay| from now. | |
| 70 RunUntilNotificationCount(1); | |
| 71 EXPECT_EQ(1, NotificationCount()); | |
| 72 EXPECT_GE(LastNotificationTime(), expected_time); | |
| 73 | |
| 74 // 5 schedules should result in only one run. | |
| 75 for (int i = 0; i < 5; ++i) { | |
| 76 expected_time = base::TimeTicks::Now() + delay; | |
| 77 notifier.Schedule(); | |
| 78 } | |
| 79 | |
| 80 RunUntilNotificationCount(2); | |
| 81 EXPECT_EQ(2, NotificationCount()); | |
| 82 EXPECT_GE(LastNotificationTime(), expected_time); | |
| 83 } | |
| 84 | |
| 85 TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { | |
| 86 base::TimeDelta delay = base::TimeDelta::FromInternalValue(1); | |
| 87 DelayedUniqueNotifier notifier( | |
| 88 base::MessageLoopProxy::current(), | |
| 89 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
| 90 delay); | |
| 91 | |
| 92 EXPECT_EQ(0, NotificationCount()); | |
| 93 | |
| 94 // Schedule constantly for a while. | |
| 95 base::TimeTicks expected_time; | |
| 96 base::TimeTicks future_time = | |
| 97 base::TimeTicks::Now() + base::TimeDelta::FromMilliseconds(1); | |
| 98 do { | |
| 99 expected_time = base::TimeTicks::Now() + delay; | |
| 100 notifier.Schedule(); | |
| 101 } while (base::TimeTicks::Now() < future_time); | |
| 102 | |
| 103 // Finish running and expect only one run. | |
| 104 RunUntilNotificationCount(1); | |
| 105 EXPECT_EQ(1, NotificationCount()); | |
| 106 | |
| 107 EXPECT_GE(LastNotificationTime(), expected_time); | |
| 108 } | |
| 109 | |
| 110 TEST_F(DelayedUniqueNotifierTest, Cancel) { | |
| 111 base::TimeDelta delay = base::TimeDelta::FromInternalValue(1); | |
| 112 DelayedUniqueNotifier notifier( | |
| 113 base::MessageLoopProxy::current(), | |
| 114 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), | |
| 115 delay); | |
| 116 | |
| 117 EXPECT_EQ(0, NotificationCount()); | |
| 118 | |
| 119 // Schedule for |delay| seconds from now. | |
| 120 notifier.Schedule(); | |
| 121 | |
| 122 // Cancel the run. | |
| 123 notifier.Cancel(); | |
| 124 | |
| 125 base::RunLoop().RunUntilIdle(); | |
| 126 EXPECT_EQ(0, NotificationCount()); | |
| 127 | |
| 128 // Schedule for |delay| seconds from now and let it run. | |
| 129 base::TimeTicks expected_time = base::TimeTicks::Now() + delay; | |
| 130 notifier.Schedule(); | |
| 131 | |
| 132 RunUntilNotificationCount(1); | |
| 133 EXPECT_EQ(1, NotificationCount()); | |
| 134 EXPECT_GE(LastNotificationTime(), expected_time); | |
| 135 | |
| 136 // Schedule for |delay| seconds from now. | |
| 137 notifier.Schedule(); | |
| 138 | |
| 139 // Cancel the run again. | |
| 140 notifier.Cancel(); | |
| 141 | |
| 142 base::RunLoop().RunUntilIdle(); | |
| 143 EXPECT_EQ(1, NotificationCount()); | |
| 144 } | |
|
reveman
2014/05/27 21:20:24
nit: blank line here
vmpstr
2014/05/27 21:50:41
Done.
| |
| 145 } // namespace | |
| 146 } // namespace cc | |
| OLD | NEW |