OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/idle_timer.h" | 5 #include "base/idle_timer.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 using base::IdleTimer; | 9 using base::IdleTimer; |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 class IdleTimerTest : public testing::Test { | |
14 private: | |
15 // IdleTimer requires a UI message loop on the current thread. | |
16 MessageLoopForUI message_loop_; | |
17 }; | |
18 | |
19 // We Mock the GetLastInputInfo function to return | 13 // We Mock the GetLastInputInfo function to return |
20 // the time stored here. | 14 // the time stored here. |
21 static DWORD mock_idle_time = GetTickCount(); | 15 static Time mock_timer_started; |
22 | 16 |
23 BOOL __stdcall MockGetLastInputInfoFunction(PLASTINPUTINFO plii) { | 17 bool MockIdleTimeSource(int32 *milliseconds_interval_since_last_event) { |
24 DCHECK(plii->cbSize == sizeof(LASTINPUTINFO)); | 18 TimeDelta delta = Time::Now() - mock_timer_started; |
25 plii->dwTime = mock_idle_time; | 19 *milliseconds_interval_since_last_event = |
26 return TRUE; | 20 static_cast<int32>(delta.InMilliseconds()); |
| 21 return true; |
27 } | 22 } |
28 | 23 |
29 // TestIdle task fires after 100ms of idle time. | 24 // TestIdle task fires after 100ms of idle time. |
30 class TestIdleTask : public IdleTimer { | 25 class TestIdleTask : public IdleTimer { |
31 public: | 26 public: |
32 TestIdleTask(bool repeat) | 27 TestIdleTask(bool repeat) |
33 : IdleTimer(TimeDelta::FromMilliseconds(100), repeat), | 28 : IdleTimer(TimeDelta::FromMilliseconds(100), repeat), |
34 idle_counter_(0) { | 29 idle_counter_(0) { |
35 set_last_input_info_fn(MockGetLastInputInfoFunction); | 30 set_idle_time_source(MockIdleTimeSource); |
36 } | 31 } |
37 | 32 |
38 int get_idle_counter() { return idle_counter_; } | 33 int get_idle_counter() { return idle_counter_; } |
39 | 34 |
40 virtual void OnIdle() { | 35 virtual void OnIdle() { |
41 idle_counter_++; | 36 idle_counter_++; |
42 } | 37 } |
43 | 38 |
44 private: | 39 private: |
45 int idle_counter_; | 40 int idle_counter_; |
46 }; | 41 }; |
47 | 42 |
48 // A task to help us quit the test. | 43 // A task to help us quit the test. |
49 class TestFinishedTask { | 44 class TestFinishedTask { |
50 public: | 45 public: |
51 TestFinishedTask() {} | 46 TestFinishedTask() {} |
52 void Run() { | 47 void Run() { |
53 MessageLoop::current()->Quit(); | 48 MessageLoop::current()->Quit(); |
54 } | 49 } |
55 }; | 50 }; |
56 | 51 |
57 // A timer which resets the idle clock. | 52 // A timer which resets the idle clock. |
58 class ResetIdleTask { | 53 class ResetIdleTask { |
59 public: | 54 public: |
60 ResetIdleTask() {} | 55 ResetIdleTask() {} |
61 void Run() { | 56 void Run() { |
62 mock_idle_time = GetTickCount(); | 57 mock_timer_started = Time::Now(); |
63 } | 58 } |
64 }; | 59 }; |
65 | 60 |
66 } // namespace | 61 class IdleTimerTest : public testing::Test { |
| 62 private: |
| 63 // IdleTimer requires a UI message loop on the current thread. |
| 64 MessageLoopForUI message_loop_; |
| 65 }; |
67 | 66 |
68 /////////////////////////////////////////////////////////////////////////////// | 67 /////////////////////////////////////////////////////////////////////////////// |
69 // NoRepeat tests: | 68 // NoRepeat tests: |
70 // A non-repeating idle timer will fire once on idle, and | 69 // A non-repeating idle timer will fire once on idle, and |
71 // then will not fire again unless it goes non-idle first. | 70 // then will not fire again unless it goes non-idle first. |
72 | 71 |
73 TEST_F(IdleTimerTest, NoRepeatIdle) { | 72 TEST_F(IdleTimerTest, NoRepeatIdle) { |
74 // Create an IdleTimer, which should fire once after 100ms. | 73 // Create an IdleTimer, which should fire once after 100ms. |
75 // Create a Quit timer which will fire after 1s. | 74 // Create a Quit timer which will fire after 1s. |
76 // Verify that we fired exactly once. | 75 // Verify that we fired exactly once. |
77 | 76 |
78 mock_idle_time = GetTickCount(); | 77 mock_timer_started = Time::Now(); |
79 TestIdleTask test_task(false); | 78 TestIdleTask test_task(false); |
80 | 79 |
81 TestFinishedTask finish_task; | 80 TestFinishedTask finish_task; |
82 base::OneShotTimer<TestFinishedTask> timer; | 81 base::OneShotTimer<TestFinishedTask> timer; |
83 timer.Start(TimeDelta::FromSeconds(1), &finish_task, &TestFinishedTask::Run); | 82 timer.Start(TimeDelta::FromSeconds(1), &finish_task, &TestFinishedTask::Run); |
84 | 83 |
85 test_task.Start(); | 84 test_task.Start(); |
86 MessageLoop::current()->Run(); | 85 MessageLoop::current()->Run(); |
87 | 86 |
88 EXPECT_EQ(test_task.get_idle_counter(), 1); | 87 EXPECT_EQ(test_task.get_idle_counter(), 1); |
89 } | 88 } |
90 | 89 |
91 TEST_F(IdleTimerTest, NoRepeatFlipIdleOnce) { | 90 TEST_F(IdleTimerTest, NoRepeatFlipIdleOnce) { |
92 // Create an IdleTimer, which should fire once after 100ms. | 91 // Create an IdleTimer, which should fire once after 100ms. |
93 // Create a Quit timer which will fire after 1s. | 92 // Create a Quit timer which will fire after 1s. |
94 // Create a timer to reset once, idle after 500ms. | 93 // Create a timer to reset once, idle after 500ms. |
95 // Verify that we fired exactly twice. | 94 // Verify that we fired exactly twice. |
96 | 95 |
97 mock_idle_time = GetTickCount(); | 96 mock_timer_started = Time::Now(); |
98 TestIdleTask test_task(false); | 97 TestIdleTask test_task(false); |
99 | 98 |
100 TestFinishedTask finish_task; | 99 TestFinishedTask finish_task; |
101 ResetIdleTask reset_task; | 100 ResetIdleTask reset_task; |
102 | 101 |
103 base::OneShotTimer<TestFinishedTask> t1; | 102 base::OneShotTimer<TestFinishedTask> t1; |
104 t1.Start(TimeDelta::FromMilliseconds(1000), &finish_task, | 103 t1.Start(TimeDelta::FromMilliseconds(1000), &finish_task, |
105 &TestFinishedTask::Run); | 104 &TestFinishedTask::Run); |
106 | 105 |
107 base::OneShotTimer<ResetIdleTask> t2; | 106 base::OneShotTimer<ResetIdleTask> t2; |
108 t2.Start(TimeDelta::FromMilliseconds(500), &reset_task, | 107 t2.Start(TimeDelta::FromMilliseconds(500), &reset_task, |
109 &ResetIdleTask::Run); | 108 &ResetIdleTask::Run); |
110 | 109 |
111 test_task.Start(); | 110 test_task.Start(); |
112 MessageLoop::current()->Run(); | 111 MessageLoop::current()->Run(); |
113 | 112 |
114 EXPECT_EQ(test_task.get_idle_counter(), 2); | 113 EXPECT_EQ(test_task.get_idle_counter(), 2); |
115 } | 114 } |
116 | 115 |
117 TEST_F(IdleTimerTest, NoRepeatNotIdle) { | 116 TEST_F(IdleTimerTest, NoRepeatNotIdle) { |
118 // Create an IdleTimer, which should fire once after 100ms. | 117 // Create an IdleTimer, which should fire once after 100ms. |
119 // Create a Quit timer which will fire after 1s. | 118 // Create a Quit timer which will fire after 1s. |
120 // Create a timer to reset idle every 50ms. | 119 // Create a timer to reset idle every 50ms. |
121 // Verify that we never fired. | 120 // Verify that we never fired. |
122 | 121 |
123 mock_idle_time = GetTickCount(); | 122 mock_timer_started = Time::Now(); |
124 TestIdleTask test_task(false); | 123 TestIdleTask test_task(false); |
125 | 124 |
126 TestFinishedTask finish_task; | 125 TestFinishedTask finish_task; |
127 ResetIdleTask reset_task; | 126 ResetIdleTask reset_task; |
128 | 127 |
129 base::OneShotTimer<TestFinishedTask> t; | 128 base::OneShotTimer<TestFinishedTask> t; |
130 t.Start(TimeDelta::FromMilliseconds(1000), &finish_task, | 129 t.Start(TimeDelta::FromMilliseconds(1000), &finish_task, |
131 &TestFinishedTask::Run); | 130 &TestFinishedTask::Run); |
132 | 131 |
133 base::RepeatingTimer<ResetIdleTask> reset_timer; | 132 base::RepeatingTimer<ResetIdleTask> reset_timer; |
(...skipping 12 matching lines...) Expand all Loading... |
146 /////////////////////////////////////////////////////////////////////////////// | 145 /////////////////////////////////////////////////////////////////////////////// |
147 // Repeat tests: | 146 // Repeat tests: |
148 // A repeating idle timer will fire repeatedly on each interval, as long | 147 // A repeating idle timer will fire repeatedly on each interval, as long |
149 // as it has been idle. So, if the machine remains idle, it will continue | 148 // as it has been idle. So, if the machine remains idle, it will continue |
150 // firing over and over. | 149 // firing over and over. |
151 | 150 |
152 TEST_F(IdleTimerTest, Repeat) { | 151 TEST_F(IdleTimerTest, Repeat) { |
153 // Create an IdleTimer, which should fire repeatedly after 100ms. | 152 // Create an IdleTimer, which should fire repeatedly after 100ms. |
154 // Create a Quit timer which will fire after 1.05s. | 153 // Create a Quit timer which will fire after 1.05s. |
155 // Verify that we fired 10 times. | 154 // Verify that we fired 10 times. |
156 mock_idle_time = GetTickCount(); | 155 mock_timer_started = Time::Now(); |
157 TestIdleTask test_task(true); | 156 TestIdleTask test_task(true); |
158 | 157 |
159 TestFinishedTask finish_task; | 158 TestFinishedTask finish_task; |
160 | 159 |
161 base::OneShotTimer<TestFinishedTask> t; | 160 base::OneShotTimer<TestFinishedTask> t; |
162 t.Start(TimeDelta::FromMilliseconds(1050), &finish_task, | 161 t.Start(TimeDelta::FromMilliseconds(1050), &finish_task, |
163 &TestFinishedTask::Run); | 162 &TestFinishedTask::Run); |
164 | 163 |
165 test_task.Start(); | 164 test_task.Start(); |
166 MessageLoop::current()->Run(); | 165 MessageLoop::current()->Run(); |
167 | 166 |
168 // In a perfect world, the idle_counter should be 10. However, | 167 // In a perfect world, the idle_counter should be 10. However, |
169 // since timers aren't guaranteed to fire perfectly, this can | 168 // since timers aren't guaranteed to fire perfectly, this can |
170 // be less. Just expect more than 5 and no more than 10. | 169 // be less. Just expect more than 5 and no more than 10. |
171 EXPECT_GT(test_task.get_idle_counter(), 5); | 170 EXPECT_GT(test_task.get_idle_counter(), 5); |
172 EXPECT_LE(test_task.get_idle_counter(), 10); | 171 EXPECT_LE(test_task.get_idle_counter(), 10); |
173 } | 172 } |
174 | 173 |
175 TEST_F(IdleTimerTest, RepeatIdleReset) { | 174 TEST_F(IdleTimerTest, RepeatIdleReset) { |
176 // Create an IdleTimer, which should fire repeatedly after 100ms. | 175 // Create an IdleTimer, which should fire repeatedly after 100ms. |
177 // Create a Quit timer which will fire after 1s. | 176 // Create a Quit timer which will fire after 1s. |
178 // Create a reset timer, which fires after 550ms | 177 // Create a reset timer, which fires after 550ms |
179 // Verify that we fired 9 times. | 178 // Verify that we fired 9 times. |
180 mock_idle_time = GetTickCount(); | 179 mock_timer_started = Time::Now(); |
181 TestIdleTask test_task(true); | 180 TestIdleTask test_task(true); |
182 | 181 |
183 ResetIdleTask reset_task; | 182 ResetIdleTask reset_task; |
184 TestFinishedTask finish_task; | 183 TestFinishedTask finish_task; |
185 | 184 |
186 base::OneShotTimer<TestFinishedTask> t1; | 185 base::OneShotTimer<TestFinishedTask> t1; |
187 t1.Start(TimeDelta::FromMilliseconds(1000), &finish_task, | 186 t1.Start(TimeDelta::FromMilliseconds(1000), &finish_task, |
188 &TestFinishedTask::Run); | 187 &TestFinishedTask::Run); |
189 | 188 |
190 base::OneShotTimer<ResetIdleTask> t2; | 189 base::OneShotTimer<ResetIdleTask> t2; |
191 t2.Start(TimeDelta::FromMilliseconds(550), &reset_task, | 190 t2.Start(TimeDelta::FromMilliseconds(550), &reset_task, |
192 &ResetIdleTask::Run); | 191 &ResetIdleTask::Run); |
193 | 192 |
194 test_task.Start(); | 193 test_task.Start(); |
195 MessageLoop::current()->Run(); | 194 MessageLoop::current()->Run(); |
196 | 195 |
197 // In a perfect world, the idle_counter should be 9. However, | 196 // In a perfect world, the idle_counter should be 9. However, |
198 // since timers aren't guaranteed to fire perfectly, this can | 197 // since timers aren't guaranteed to fire perfectly, this can |
199 // be less. Just expect more than 5 and no more than 9. | 198 // be less. Just expect more than 5 and no more than 9. |
200 EXPECT_GT(test_task.get_idle_counter(), 5); | 199 EXPECT_GT(test_task.get_idle_counter(), 5); |
201 EXPECT_LE(test_task.get_idle_counter(), 9); | 200 EXPECT_LE(test_task.get_idle_counter(), 9); |
202 } | 201 } |
203 | 202 |
204 TEST_F(IdleTimerTest, RepeatNotIdle) { | 203 TEST_F(IdleTimerTest, RepeatNotIdle) { |
205 // Create an IdleTimer, which should fire repeatedly after 100ms. | 204 // Create an IdleTimer, which should fire repeatedly after 100ms. |
206 // Create a Quit timer which will fire after 1s. | 205 // Create a Quit timer which will fire after 1s. |
207 // Create a timer to reset idle every 50ms. | 206 // Create a timer to reset idle every 50ms. |
208 // Verify that we never fired. | 207 // Verify that we never fired. |
209 | 208 |
210 mock_idle_time = GetTickCount(); | 209 mock_timer_started = Time::Now(); |
211 TestIdleTask test_task(true); | 210 TestIdleTask test_task(true); |
212 | 211 |
213 TestFinishedTask finish_task; | 212 TestFinishedTask finish_task; |
214 ResetIdleTask reset_task; | 213 ResetIdleTask reset_task; |
215 | 214 |
216 base::OneShotTimer<TestFinishedTask> t; | 215 base::OneShotTimer<TestFinishedTask> t; |
217 t.Start(TimeDelta::FromMilliseconds(1000), &finish_task, | 216 t.Start(TimeDelta::FromMilliseconds(1000), &finish_task, |
218 &TestFinishedTask::Run); | 217 &TestFinishedTask::Run); |
219 | 218 |
220 base::RepeatingTimer<ResetIdleTask> reset_timer; | 219 base::RepeatingTimer<ResetIdleTask> reset_timer; |
221 reset_timer.Start(TimeDelta::FromMilliseconds(50), &reset_task, | 220 reset_timer.Start(TimeDelta::FromMilliseconds(50), &reset_task, |
222 &ResetIdleTask::Run); | 221 &ResetIdleTask::Run); |
223 | 222 |
224 test_task.Start(); | 223 test_task.Start(); |
225 MessageLoop::current()->Run(); | 224 MessageLoop::current()->Run(); |
226 | 225 |
227 reset_timer.Stop(); | 226 reset_timer.Stop(); |
228 | 227 |
229 EXPECT_EQ(test_task.get_idle_counter(), 0); | 228 EXPECT_EQ(test_task.get_idle_counter(), 0); |
230 } | 229 } |
231 | 230 |
| 231 } // namespace |
OLD | NEW |