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/unique_notifier.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace cc { |
| 14 namespace { |
| 15 |
| 16 class UniqueNotifierTest : public testing::Test { |
| 17 public: |
| 18 UniqueNotifierTest() : 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<UniqueNotifier> notifier_; |
| 45 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 46 scoped_ptr<base::RunLoop> run_loop_; |
| 47 }; |
| 48 |
| 49 TEST_F(UniqueNotifierTest, ZeroDelay) { |
| 50 notifier_ = make_scoped_ptr(new UniqueNotifier( |
| 51 task_runner_.get(), |
| 52 base::Bind(&UniqueNotifierTest::Notify, base::Unretained(this)), |
| 53 base::TimeDelta())); |
| 54 |
| 55 EXPECT_EQ(0, NotificationCount()); |
| 56 |
| 57 // Basic schedule should result in a run. |
| 58 notifier_->Schedule(); |
| 59 |
| 60 RunForTime(base::TimeDelta()); |
| 61 EXPECT_EQ(1, NotificationCount()); |
| 62 |
| 63 // Multiple schedules should only result in one run. |
| 64 for (int i = 0; i < 5; ++i) |
| 65 notifier_->Schedule(); |
| 66 |
| 67 RunForTime(base::TimeDelta()); |
| 68 EXPECT_EQ(2, NotificationCount()); |
| 69 |
| 70 // Schedule and notifier going away should not result in any runs. |
| 71 notifier_->Schedule(); |
| 72 notifier_.reset(); |
| 73 |
| 74 RunForTime(base::TimeDelta()); |
| 75 EXPECT_EQ(2, NotificationCount()); |
| 76 } |
| 77 |
| 78 TEST_F(UniqueNotifierTest, SecondDelay) { |
| 79 notifier_ = make_scoped_ptr(new UniqueNotifier( |
| 80 task_runner_.get(), |
| 81 base::Bind(&UniqueNotifierTest::Notify, base::Unretained(this)), |
| 82 base::TimeDelta::FromSeconds(1))); |
| 83 |
| 84 EXPECT_EQ(0, NotificationCount()); |
| 85 |
| 86 // Basic schedule for 1 second from now. |
| 87 notifier_->Schedule(); |
| 88 |
| 89 // Half a second passed, we should have no runs. |
| 90 RunForTime(base::TimeDelta::FromMilliseconds(500)); |
| 91 EXPECT_EQ(0, NotificationCount()); |
| 92 |
| 93 // After another 0.6 seconds, we should get a run. |
| 94 RunForTime(base::TimeDelta::FromMilliseconds(600)); |
| 95 EXPECT_EQ(1, NotificationCount()); |
| 96 |
| 97 // 5 schedules should result in only one run. |
| 98 for (int i = 0; i < 5; ++i) |
| 99 notifier_->Schedule(); |
| 100 |
| 101 RunForTime(base::TimeDelta::FromMilliseconds(1100)); |
| 102 EXPECT_EQ(2, NotificationCount()); |
| 103 } |
| 104 |
| 105 TEST_F(UniqueNotifierTest, RescheduleDelay) { |
| 106 notifier_ = make_scoped_ptr(new UniqueNotifier( |
| 107 task_runner_.get(), |
| 108 base::Bind(&UniqueNotifierTest::Notify, base::Unretained(this)), |
| 109 base::TimeDelta::FromSeconds(1))); |
| 110 |
| 111 EXPECT_EQ(0, NotificationCount()); |
| 112 |
| 113 // Schedule for 1 second from now. |
| 114 notifier_->Schedule(); |
| 115 |
| 116 // Half a second later, we should have no runs. |
| 117 RunForTime(base::TimeDelta::FromMilliseconds(500)); |
| 118 EXPECT_EQ(0, NotificationCount()); |
| 119 |
| 120 // Reschedule for an extra second. |
| 121 notifier_->Schedule(); |
| 122 |
| 123 // Half a second later, we should have no runs. |
| 124 RunForTime(base::TimeDelta::FromMilliseconds(500)); |
| 125 EXPECT_EQ(0, NotificationCount()); |
| 126 |
| 127 // Reschedule for an extra second. |
| 128 notifier_->Schedule(); |
| 129 |
| 130 // Half a second later, we should have no runs. |
| 131 RunForTime(base::TimeDelta::FromMilliseconds(500)); |
| 132 EXPECT_EQ(0, NotificationCount()); |
| 133 |
| 134 // After another 0.6 seconds, we should get a run. |
| 135 RunForTime(base::TimeDelta::FromMilliseconds(600)); |
| 136 EXPECT_EQ(1, NotificationCount()); |
| 137 } |
| 138 |
| 139 } // namespace |
| 140 } // namespace cc |
OLD | NEW |