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 task_runner_ = base::MessageLoopProxy::current(); |
| 22 ResetNotificationCount(); |
| 23 } |
| 24 |
| 25 virtual void TearDown() OVERRIDE { |
| 26 notifier_.reset(); |
| 27 task_runner_ = NULL; |
| 28 } |
| 29 |
| 30 void Notify() { ++notification_count_; } |
| 31 |
| 32 int NotificationCount() const { return notification_count_; } |
| 33 |
| 34 void ResetNotificationCount() { notification_count_ = 0; } |
| 35 |
| 36 void RunForTime(base::TimeDelta delay) { |
| 37 run_loop_ = make_scoped_ptr(new base::RunLoop); |
| 38 task_runner_->PostDelayedTask(FROM_HERE, run_loop_->QuitClosure(), delay); |
| 39 run_loop_->Run(); |
| 40 } |
| 41 |
| 42 protected: |
| 43 int notification_count_; |
| 44 scoped_ptr<DelayedUniqueNotifier> notifier_; |
| 45 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 46 scoped_ptr<base::RunLoop> run_loop_; |
| 47 }; |
| 48 |
| 49 TEST_F(DelayedUniqueNotifierTest, SecondDelay) { |
| 50 notifier_ = make_scoped_ptr(new DelayedUniqueNotifier( |
| 51 task_runner_.get(), |
| 52 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
| 53 base::TimeDelta::FromSeconds(1))); |
| 54 |
| 55 EXPECT_EQ(0, NotificationCount()); |
| 56 |
| 57 // Basic schedule for 1 second from now. |
| 58 notifier_->Schedule(); |
| 59 |
| 60 // Half a second passed, we should have no runs. |
| 61 RunForTime(base::TimeDelta::FromMilliseconds(500)); |
| 62 EXPECT_EQ(0, NotificationCount()); |
| 63 |
| 64 // After another 0.6 seconds, we should get a run. |
| 65 RunForTime(base::TimeDelta::FromMilliseconds(600)); |
| 66 EXPECT_EQ(1, NotificationCount()); |
| 67 |
| 68 // 5 schedules should result in only one run. |
| 69 for (int i = 0; i < 5; ++i) |
| 70 notifier_->Schedule(); |
| 71 |
| 72 RunForTime(base::TimeDelta::FromMilliseconds(1100)); |
| 73 EXPECT_EQ(2, NotificationCount()); |
| 74 } |
| 75 |
| 76 TEST_F(DelayedUniqueNotifierTest, RescheduleDelay) { |
| 77 notifier_ = make_scoped_ptr(new DelayedUniqueNotifier( |
| 78 task_runner_.get(), |
| 79 base::Bind(&DelayedUniqueNotifierTest::Notify, base::Unretained(this)), |
| 80 base::TimeDelta::FromSeconds(1))); |
| 81 |
| 82 EXPECT_EQ(0, NotificationCount()); |
| 83 |
| 84 // Schedule for 1 second from now. |
| 85 notifier_->Schedule(); |
| 86 |
| 87 // Half a second later, we should have no runs. |
| 88 RunForTime(base::TimeDelta::FromMilliseconds(500)); |
| 89 EXPECT_EQ(0, NotificationCount()); |
| 90 |
| 91 // Reschedule for an extra second. |
| 92 notifier_->Schedule(); |
| 93 |
| 94 // Half a second later, we should have no runs. |
| 95 RunForTime(base::TimeDelta::FromMilliseconds(500)); |
| 96 EXPECT_EQ(0, NotificationCount()); |
| 97 |
| 98 // Reschedule for an extra second. |
| 99 notifier_->Schedule(); |
| 100 |
| 101 // Half a second later, we should have no runs. |
| 102 RunForTime(base::TimeDelta::FromMilliseconds(500)); |
| 103 EXPECT_EQ(0, NotificationCount()); |
| 104 |
| 105 // After another 0.6 seconds, we should get a run. |
| 106 RunForTime(base::TimeDelta::FromMilliseconds(600)); |
| 107 EXPECT_EQ(1, NotificationCount()); |
| 108 } |
| 109 |
| 110 } // namespace |
| 111 } // namespace cc |
OLD | NEW |