Chromium Code Reviews| 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..76e05cd0a3e670e8feec6474c660c7009b4aa2c3 |
| --- /dev/null |
| +++ b/cc/base/delayed_unique_notifier_unittest.cc |
| @@ -0,0 +1,146 @@ |
| +// 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), expected_notification_count_(0) {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + notification_count_ = 0; |
| + expected_notification_count_ = 0; |
| + last_notification_time_ = base::TimeTicks(); |
| + } |
| + |
| + void Notify() { |
| + ++notification_count_; |
| + last_notification_time_ = base::TimeTicks::Now(); |
| + if (notification_count_ >= expected_notification_count_) |
| + run_loop_->Quit(); |
| + } |
| + |
| + int NotificationCount() const { return notification_count_; } |
| + |
| + base::TimeTicks LastNotificationTime() const { |
| + return last_notification_time_; |
| + } |
| + |
| + void RunUntilNotificationCount(int count) { |
| + expected_notification_count_ = count; |
| + run_loop_.reset(new base::RunLoop); |
| + run_loop_->Run(); |
| + |
| + // 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
|
| + base::RunLoop().RunUntilIdle(); |
|
reveman
2014/05/27 21:20:24
Why do we need this? I assume run_loop_.reset() do
|
| + } |
| + |
| + protected: |
| + int notification_count_; |
| + int expected_notification_count_; |
| + base::TimeTicks last_notification_time_; |
| + scoped_ptr<base::RunLoop> run_loop_; |
| +}; |
| + |
| +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.
|
| + base::TimeDelta delay = base::TimeDelta::FromInternalValue(1); |
| + DelayedUniqueNotifier notifier( |
| + base::MessageLoopProxy::current(), |
| + base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
| + delay); |
| + |
| + EXPECT_EQ(0, NotificationCount()); |
| + |
| + // Basic schedule for |delay| from now. |
| + base::TimeTicks expected_time = base::TimeTicks::Now() + delay; |
| + notifier.Schedule(); |
| + |
| + // Expect that we ran only once at least |delay| from now. |
| + RunUntilNotificationCount(1); |
| + 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(); |
| + } |
| + |
| + RunUntilNotificationCount(2); |
| + EXPECT_EQ(2, NotificationCount()); |
| + EXPECT_GE(LastNotificationTime(), expected_time); |
| +} |
| + |
| +TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { |
| + base::TimeDelta delay = base::TimeDelta::FromInternalValue(1); |
| + DelayedUniqueNotifier notifier( |
| + base::MessageLoopProxy::current(), |
| + base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
| + delay); |
| + |
| + EXPECT_EQ(0, NotificationCount()); |
| + |
| + // Schedule constantly for a while. |
| + base::TimeTicks expected_time; |
| + base::TimeTicks future_time = |
| + base::TimeTicks::Now() + base::TimeDelta::FromMilliseconds(1); |
| + do { |
| + expected_time = base::TimeTicks::Now() + delay; |
| + notifier.Schedule(); |
| + } while (base::TimeTicks::Now() < future_time); |
| + |
| + // Finish running and expect only one run. |
| + RunUntilNotificationCount(1); |
| + EXPECT_EQ(1, NotificationCount()); |
| + |
| + EXPECT_GE(LastNotificationTime(), expected_time); |
| +} |
| + |
| +TEST_F(DelayedUniqueNotifierTest, Cancel) { |
| + base::TimeDelta delay = base::TimeDelta::FromInternalValue(1); |
| + 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(); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(0, NotificationCount()); |
| + |
| + // Schedule for |delay| seconds from now and let it run. |
| + base::TimeTicks expected_time = base::TimeTicks::Now() + delay; |
| + notifier.Schedule(); |
| + |
| + RunUntilNotificationCount(1); |
| + EXPECT_EQ(1, NotificationCount()); |
| + EXPECT_GE(LastNotificationTime(), expected_time); |
| + |
| + // Schedule for |delay| seconds from now. |
| + notifier.Schedule(); |
| + |
| + // Cancel the run again. |
| + notifier.Cancel(); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, NotificationCount()); |
| +} |
|
reveman
2014/05/27 21:20:24
nit: blank line here
vmpstr
2014/05/27 21:50:41
Done.
|
| +} // namespace |
| +} // namespace cc |