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