Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: base/idletimer_unittest.cc

Issue 9261: Update idle timer test to use longer intervals.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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::Time; 9 using base::Time;
10 using base::TimeDelta; 10 using base::TimeDelta;
11 using base::IdleTimer; 11 using base::IdleTimer;
12 12
13
14 // If the timers fire too quickly, it can be tricky to make timer tests
15 // reliable on all buildbots. This constant sets a minimum timer delta where
16 // we expect that we should be able to reliably count timers without problems
17 // due to slight clock/scheduling variances.
18 const int kSafeTestIntervalMs = 500;
19
13 namespace { 20 namespace {
14 21
15 // We Mock the GetLastInputInfo function to return 22 // We Mock the GetLastInputInfo function to return
16 // the time stored here. 23 // the time stored here.
17 static Time mock_timer_started; 24 static Time mock_timer_started;
18 25
19 bool MockIdleTimeSource(int32 *milliseconds_interval_since_last_event) { 26 bool MockIdleTimeSource(int32 *milliseconds_interval_since_last_event) {
20 TimeDelta delta = Time::Now() - mock_timer_started; 27 TimeDelta delta = Time::Now() - mock_timer_started;
21 *milliseconds_interval_since_last_event = 28 *milliseconds_interval_since_last_event =
22 static_cast<int32>(delta.InMilliseconds()); 29 static_cast<int32>(delta.InMilliseconds());
23 return true; 30 return true;
24 } 31 }
25 32
26 // TestIdle task fires after 100ms of idle time. 33 // TestIdle task fires after 100ms of idle time.
27 class TestIdleTask : public IdleTimer { 34 class TestIdleTask : public IdleTimer {
28 public: 35 public:
29 TestIdleTask(bool repeat) 36 TestIdleTask(bool repeat)
30 : IdleTimer(TimeDelta::FromMilliseconds(100), repeat), 37 : IdleTimer(TimeDelta::FromMilliseconds(kSafeTestIntervalMs), repeat),
31 idle_counter_(0) { 38 idle_counter_(0) {
32 set_idle_time_source(MockIdleTimeSource); 39 set_idle_time_source(MockIdleTimeSource);
33 } 40 }
34 41
35 int get_idle_counter() { return idle_counter_; } 42 int get_idle_counter() { return idle_counter_; }
36 43
37 virtual void OnIdle() { 44 virtual void OnIdle() {
38 idle_counter_++; 45 idle_counter_++;
39 } 46 }
40 47
(...skipping 24 matching lines...) Expand all
65 // IdleTimer requires a UI message loop on the current thread. 72 // IdleTimer requires a UI message loop on the current thread.
66 MessageLoopForUI message_loop_; 73 MessageLoopForUI message_loop_;
67 }; 74 };
68 75
69 /////////////////////////////////////////////////////////////////////////////// 76 ///////////////////////////////////////////////////////////////////////////////
70 // NoRepeat tests: 77 // NoRepeat tests:
71 // A non-repeating idle timer will fire once on idle, and 78 // A non-repeating idle timer will fire once on idle, and
72 // then will not fire again unless it goes non-idle first. 79 // then will not fire again unless it goes non-idle first.
73 80
74 TEST_F(IdleTimerTest, NoRepeatIdle) { 81 TEST_F(IdleTimerTest, NoRepeatIdle) {
75 // Create an IdleTimer, which should fire once after 100ms. 82 // Create an IdleTimer, which should fire once after 500ms.
76 // Create a Quit timer which will fire after 1s. 83 // Create a Quit timer which will fire after 1s.
77 // Verify that we fired exactly once. 84 // Verify that we fired exactly once.
78 85
79 mock_timer_started = Time::Now(); 86 mock_timer_started = Time::Now();
80 TestIdleTask test_task(false); 87 TestIdleTask test_task(false);
81 88
82 TestFinishedTask finish_task; 89 TestFinishedTask finish_task;
83 base::OneShotTimer<TestFinishedTask> timer; 90 base::OneShotTimer<TestFinishedTask> timer;
84 timer.Start(TimeDelta::FromSeconds(1), &finish_task, &TestFinishedTask::Run); 91 timer.Start(TimeDelta::FromMilliseconds(2 * kSafeTestIntervalMs),
92 &finish_task, &TestFinishedTask::Run);
85 93
86 test_task.Start(); 94 test_task.Start();
87 MessageLoop::current()->Run(); 95 MessageLoop::current()->Run();
88 96
89 EXPECT_EQ(test_task.get_idle_counter(), 1); 97 EXPECT_EQ(test_task.get_idle_counter(), 1);
90 } 98 }
91 99
92 TEST_F(IdleTimerTest, NoRepeatFlipIdleOnce) { 100 TEST_F(IdleTimerTest, NoRepeatFlipIdleOnce) {
93 // Create an IdleTimer, which should fire once after 100ms. 101 // Create an IdleTimer, which should fire once after 500ms.
94 // Create a Quit timer which will fire after 1s. 102 // Create a Quit timer which will fire after 5s.
95 // Create a timer to reset once, idle after 500ms. 103 // Create a timer to reset once, idle after 2s.
96 // Verify that we fired exactly twice. 104 // Verify that we fired exactly twice.
97 105
98 mock_timer_started = Time::Now(); 106 mock_timer_started = Time::Now();
99 TestIdleTask test_task(false); 107 TestIdleTask test_task(false);
100 108
101 TestFinishedTask finish_task; 109 TestFinishedTask finish_task;
102 ResetIdleTask reset_task; 110 ResetIdleTask reset_task;
103 111
104 base::OneShotTimer<TestFinishedTask> t1; 112 base::OneShotTimer<TestFinishedTask> t1;
105 t1.Start(TimeDelta::FromMilliseconds(1000), &finish_task, 113 t1.Start(TimeDelta::FromMilliseconds(10 * kSafeTestIntervalMs), &finish_task,
106 &TestFinishedTask::Run); 114 &TestFinishedTask::Run);
107 115
108 base::OneShotTimer<ResetIdleTask> t2; 116 base::OneShotTimer<ResetIdleTask> t2;
109 t2.Start(TimeDelta::FromMilliseconds(500), &reset_task, 117 t2.Start(TimeDelta::FromMilliseconds(4 * kSafeTestIntervalMs), &reset_task,
110 &ResetIdleTask::Run); 118 &ResetIdleTask::Run);
111 119
112 test_task.Start(); 120 test_task.Start();
113 MessageLoop::current()->Run(); 121 MessageLoop::current()->Run();
114 122
115 EXPECT_EQ(test_task.get_idle_counter(), 2); 123 EXPECT_EQ(test_task.get_idle_counter(), 2);
116 } 124 }
117 125
118 TEST_F(IdleTimerTest, NoRepeatNotIdle) { 126 TEST_F(IdleTimerTest, NoRepeatNotIdle) {
119 // Create an IdleTimer, which should fire once after 100ms. 127 // Create an IdleTimer, which should fire once after 500ms.
120 // Create a Quit timer which will fire after 1s. 128 // Create a Quit timer which will fire after 5s.
121 // Create a timer to reset idle every 50ms. 129 // Create a timer to reset idle every 50ms.
122 // Verify that we never fired. 130 // Verify that we never fired.
123 131
124 mock_timer_started = Time::Now(); 132 mock_timer_started = Time::Now();
125 TestIdleTask test_task(false); 133 TestIdleTask test_task(false);
126 134
127 TestFinishedTask finish_task; 135 TestFinishedTask finish_task;
128 ResetIdleTask reset_task; 136 ResetIdleTask reset_task;
129 137
130 base::OneShotTimer<TestFinishedTask> t; 138 base::OneShotTimer<TestFinishedTask> t;
131 t.Start(TimeDelta::FromMilliseconds(1000), &finish_task, 139 t.Start(TimeDelta::FromMilliseconds(10 * kSafeTestIntervalMs), &finish_task,
132 &TestFinishedTask::Run); 140 &TestFinishedTask::Run);
133 141
134 base::RepeatingTimer<ResetIdleTask> reset_timer; 142 base::RepeatingTimer<ResetIdleTask> reset_timer;
135 reset_timer.Start(TimeDelta::FromMilliseconds(50), &reset_task, 143 reset_timer.Start(TimeDelta::FromMilliseconds(50), &reset_task,
136 &ResetIdleTask::Run); 144 &ResetIdleTask::Run);
137 145
138 test_task.Start(); 146 test_task.Start();
139 147
140 MessageLoop::current()->Run(); 148 MessageLoop::current()->Run();
141 149
142 reset_timer.Stop(); 150 reset_timer.Stop();
143 151
144 EXPECT_EQ(test_task.get_idle_counter(), 0); 152 EXPECT_EQ(test_task.get_idle_counter(), 0);
145 } 153 }
146 154
147 /////////////////////////////////////////////////////////////////////////////// 155 ///////////////////////////////////////////////////////////////////////////////
148 // Repeat tests: 156 // Repeat tests:
149 // A repeating idle timer will fire repeatedly on each interval, as long 157 // A repeating idle timer will fire repeatedly on each interval, as long
150 // as it has been idle. So, if the machine remains idle, it will continue 158 // as it has been idle. So, if the machine remains idle, it will continue
151 // firing over and over. 159 // firing over and over.
152 160
153 TEST_F(IdleTimerTest, Repeat) { 161 TEST_F(IdleTimerTest, Repeat) {
154 // Create an IdleTimer, which should fire repeatedly after 100ms. 162 // Create an IdleTimer, which should fire repeatedly after 500ms.
155 // Create a Quit timer which will fire after 1.05s. 163 // Create a Quit timer which will fire after 1.5s.
156 // Verify that we fired 10 times. 164 // Verify that we fired 2-3 times.
157 mock_timer_started = Time::Now(); 165 mock_timer_started = Time::Now();
158 TestIdleTask test_task(true); 166 TestIdleTask test_task(true);
159 167
160 TestFinishedTask finish_task; 168 TestFinishedTask finish_task;
161 169
162 base::OneShotTimer<TestFinishedTask> t; 170 base::OneShotTimer<TestFinishedTask> t;
163 t.Start(TimeDelta::FromMilliseconds(1050), &finish_task, 171 t.Start(TimeDelta::FromMilliseconds(kSafeTestIntervalMs * 3), &finish_task,
164 &TestFinishedTask::Run); 172 &TestFinishedTask::Run);
165 173
166 test_task.Start(); 174 test_task.Start();
167 MessageLoop::current()->Run(); 175 MessageLoop::current()->Run();
168 176
169 // In a perfect world, the idle_counter should be 10. However, 177 // In a perfect world, the idle_counter should be 2. However,
170 // since timers aren't guaranteed to fire perfectly, this can 178 // due to timer 'slop', accept 2 or 3.
171 // be less. Just expect more than 5 and no more than 10. 179 EXPECT_GE(test_task.get_idle_counter(), 2);
172 EXPECT_GT(test_task.get_idle_counter(), 5); 180 EXPECT_LE(test_task.get_idle_counter(), 3);
173 EXPECT_LE(test_task.get_idle_counter(), 10);
174 } 181 }
175 182
176 // TODO(darin): http://code.google.com/p/chromium/issues/detail?id=3780 183 // TODO(darin): http://code.google.com/p/chromium/issues/detail?id=3780
177 TEST_F(IdleTimerTest, DISABLED_RepeatIdleReset) { 184 TEST_F(IdleTimerTest, RepeatIdleReset) {
178 // Create an IdleTimer, which should fire repeatedly after 100ms. 185 // Create an IdleTimer, which should fire repeatedly after 500ms.
179 // Create a Quit timer which will fire after 1s. 186 // Create a Quit timer which will fire after 5s.
180 // Create a reset timer, which fires after 550ms 187 // Create a reset timer, which fires after 2500ms
181 // Verify that we fired 9 times. 188 // Verify that we fired 8-10 times.
182 mock_timer_started = Time::Now(); 189 mock_timer_started = Time::Now();
183 TestIdleTask test_task(true); 190 TestIdleTask test_task(true);
184 191
185 ResetIdleTask reset_task; 192 ResetIdleTask reset_task;
186 TestFinishedTask finish_task; 193 TestFinishedTask finish_task;
187 194
188 base::OneShotTimer<TestFinishedTask> t1; 195 base::OneShotTimer<TestFinishedTask> t1;
189 t1.Start(TimeDelta::FromMilliseconds(1000), &finish_task, 196 t1.Start(TimeDelta::FromMilliseconds(10 * kSafeTestIntervalMs), &finish_task,
190 &TestFinishedTask::Run); 197 &TestFinishedTask::Run);
191 198
192 base::OneShotTimer<ResetIdleTask> t2; 199 base::OneShotTimer<ResetIdleTask> t2;
193 t2.Start(TimeDelta::FromMilliseconds(550), &reset_task, 200 t2.Start(TimeDelta::FromMilliseconds(5 * kSafeTestIntervalMs), &reset_task,
194 &ResetIdleTask::Run); 201 &ResetIdleTask::Run);
195 202
196 test_task.Start(); 203 test_task.Start();
197 MessageLoop::current()->Run(); 204 MessageLoop::current()->Run();
198 205
199 // In a perfect world, the idle_counter should be 9. However, 206 // In a perfect world, the idle_counter should be 9. However,
200 // since timers aren't guaranteed to fire perfectly, this can 207 // since timers aren't guaranteed to fire perfectly, this can
201 // be less. Just expect more than 5 and no more than 9. 208 // be less. Accept 8-10.
202 EXPECT_GT(test_task.get_idle_counter(), 5); 209 EXPECT_GE(test_task.get_idle_counter(), 8);
203 EXPECT_LE(test_task.get_idle_counter(), 9); 210 EXPECT_LE(test_task.get_idle_counter(), 10);
204 } 211 }
205 212
206 TEST_F(IdleTimerTest, RepeatNotIdle) { 213 TEST_F(IdleTimerTest, RepeatNotIdle) {
207 // Create an IdleTimer, which should fire repeatedly after 100ms. 214 // Create an IdleTimer, which should fire repeatedly after 500ms.
208 // Create a Quit timer which will fire after 1s. 215 // Create a Quit timer which will fire after 4s.
209 // Create a timer to reset idle every 50ms. 216 // Create a timer to reset idle every 50ms.
210 // Verify that we never fired. 217 // Verify that we never fired.
211 218
212 mock_timer_started = Time::Now(); 219 mock_timer_started = Time::Now();
213 TestIdleTask test_task(true); 220 TestIdleTask test_task(true);
214 221
215 TestFinishedTask finish_task; 222 TestFinishedTask finish_task;
216 ResetIdleTask reset_task; 223 ResetIdleTask reset_task;
217 224
218 base::OneShotTimer<TestFinishedTask> t; 225 base::OneShotTimer<TestFinishedTask> t;
219 t.Start(TimeDelta::FromMilliseconds(1000), &finish_task, 226 t.Start(TimeDelta::FromMilliseconds(8 * kSafeTestIntervalMs), &finish_task,
220 &TestFinishedTask::Run); 227 &TestFinishedTask::Run);
221 228
222 base::RepeatingTimer<ResetIdleTask> reset_timer; 229 base::RepeatingTimer<ResetIdleTask> reset_timer;
223 reset_timer.Start(TimeDelta::FromMilliseconds(50), &reset_task, 230 reset_timer.Start(TimeDelta::FromMilliseconds(50), &reset_task,
224 &ResetIdleTask::Run); 231 &ResetIdleTask::Run);
225 232
226 test_task.Start(); 233 test_task.Start();
227 MessageLoop::current()->Run(); 234 MessageLoop::current()->Run();
228 235
229 reset_timer.Stop(); 236 reset_timer.Stop();
230 237
231 EXPECT_EQ(test_task.get_idle_counter(), 0); 238 EXPECT_EQ(test_task.get_idle_counter(), 0);
232 } 239 }
233 240
234 } // namespace 241 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698