blob: 613adad150304d24d8a634f26cb6ed19854994f3 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include "base/idle_timer.h"
6#include "base/message_loop.h"
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace {
10 class IdleTimerTest : public testing::Test {
11 };
12};
13
14// We Mock the GetLastInputInfo function to return
15// the time stored here.
16static DWORD mock_idle_time = GetTickCount();
17
18BOOL __stdcall MockGetLastInputInfoFunction(PLASTINPUTINFO plii) {
19 DCHECK(plii->cbSize == sizeof(LASTINPUTINFO));
20 plii->dwTime = mock_idle_time;
21 return TRUE;
22}
23
24// TestIdle task fires after 100ms of idle time.
25class TestIdleTask : public IdleTimerTask {
26 public:
27 TestIdleTask(bool repeat)
28 : IdleTimerTask(TimeDelta::FromMilliseconds(100), repeat),
29 idle_counter_(0) {
30 set_last_input_info_fn(MockGetLastInputInfoFunction);
31 }
32
33 int get_idle_counter() { return idle_counter_; }
34
35 virtual void OnIdle() {
36 idle_counter_++;
37 }
38
39 private:
40 int idle_counter_;
41};
42
43// A task to help us quit the test.
44class TestFinishedTask : public Task {
45 public:
46 TestFinishedTask() {}
47 void Run() {
48 MessageLoop::current()->Quit();
49 }
50};
51
52// A timer which resets the idle clock.
53class ResetIdleTask : public Task {
54 public:
55 ResetIdleTask() {}
56 void Run() {
57 mock_idle_time = GetTickCount();
58 }
59};
60
61///////////////////////////////////////////////////////////////////////////////
62// NoRepeat tests:
63// A non-repeating idle timer will fire once on idle, and
64// then will not fire again unless it goes non-idle first.
65
66TEST(IdleTimerTest, NoRepeatIdle) {
67 // Create an IdleTimer, which should fire once after 100ms.
68 // Create a Quit timer which will fire after 1s.
69 // Verify that we fired exactly once.
70
71 mock_idle_time = GetTickCount();
72 TestIdleTask test_task(false);
73 TestFinishedTask finish_task;
74 MessageLoop* loop = MessageLoop::current();
75 Timer* t = loop->timer_manager()->StartTimer(1000, &finish_task, false);
76 test_task.Start();
77 loop->Run();
78
79 EXPECT_EQ(test_task.get_idle_counter(), 1);
80 delete t;
81}
82
83TEST(IdleTimerTest, NoRepeatFlipIdleOnce) {
84 // Create an IdleTimer, which should fire once after 100ms.
85 // Create a Quit timer which will fire after 1s.
86 // Create a timer to reset once, idle after 500ms.
87 // Verify that we fired exactly twice.
88
89 mock_idle_time = GetTickCount();
90 TestIdleTask test_task(false);
91 TestFinishedTask finish_task;
92 ResetIdleTask reset_task;
93 MessageLoop* loop = MessageLoop::current();
94 Timer* t1 = loop->timer_manager()->StartTimer(1000, &finish_task, false);
95 Timer* t2 = loop->timer_manager()->StartTimer(500, &reset_task, false);
96 test_task.Start();
97 loop->Run();
98
99 EXPECT_EQ(test_task.get_idle_counter(), 2);
100 delete t1;
101 delete t2;
102}
103
104TEST(IdleTimerTest, NoRepeatNotIdle) {
105 // Create an IdleTimer, which should fire once after 100ms.
106 // Create a Quit timer which will fire after 1s.
107 // Create a timer to reset idle every 50ms.
108 // Verify that we never fired.
109
110 mock_idle_time = GetTickCount();
111 TestIdleTask test_task(false);
112 TestFinishedTask finish_task;
113 ResetIdleTask reset_task;
114 MessageLoop* loop = MessageLoop::current();
115 Timer* t = loop->timer_manager()->StartTimer(1000, &finish_task, false);
116 Timer* reset_timer = loop->timer_manager()->StartTimer(50, &reset_task, true);
117 test_task.Start();
118 loop->Run();
119 loop->timer_manager()->StopTimer(reset_timer);
120
121 EXPECT_EQ(test_task.get_idle_counter(), 0);
122 delete t;
123 delete reset_timer;
124}
125
126///////////////////////////////////////////////////////////////////////////////
127// Repeat tests:
128// A repeating idle timer will fire repeatedly on each interval, as long
129// as it has been idle. So, if the machine remains idle, it will continue
130// firing over and over.
131
132TEST(IdleTimerTest, Repeat) {
133 // Create an IdleTimer, which should fire repeatedly after 100ms.
134 // Create a Quit timer which will fire after 1.05s.
135 // Verify that we fired 10 times.
136 mock_idle_time = GetTickCount();
137 TestIdleTask test_task(true);
138 TestFinishedTask finish_task;
139 MessageLoop* loop = MessageLoop::current();
140 Timer* t = loop->timer_manager()->StartTimer(1050, &finish_task, false);
141 test_task.Start();
142 loop->Run();
143
144 // In a perfect world, the idle_counter should be 10. However,
145 // since timers aren't guaranteed to fire perfectly, this can
146 // be less. Just expect more than 5 and no more than 10.
147 EXPECT_GT(test_task.get_idle_counter(), 5);
148 EXPECT_LE(test_task.get_idle_counter(), 10);
149 delete t;
150}
151
152TEST(IdleTimerTest, RepeatIdleReset) {
153 // Create an IdleTimer, which should fire repeatedly after 100ms.
154 // Create a Quit timer which will fire after 1s.
155 // Create a reset timer, which fires after 550ms
156 // Verify that we fired 9 times.
157 mock_idle_time = GetTickCount();
158 TestIdleTask test_task(true);
159 ResetIdleTask reset_task;
160 TestFinishedTask finish_task;
161 MessageLoop* loop = MessageLoop::current();
162 Timer* t1 = loop->timer_manager()->StartTimer(1000, &finish_task, false);
163 Timer* t2 = loop->timer_manager()->StartTimer(550, &reset_task, false);
164 test_task.Start();
165 loop->Run();
166
167 // In a perfect world, the idle_counter should be 9. However,
168 // since timers aren't guaranteed to fire perfectly, this can
169 // be less. Just expect more than 5 and no more than 9.
170 EXPECT_GT(test_task.get_idle_counter(), 5);
171 EXPECT_LE(test_task.get_idle_counter(), 9);
172 delete t1;
173 delete t2;
174}
175
176TEST(IdleTimerTest, RepeatNotIdle) {
177 // Create an IdleTimer, which should fire repeatedly after 100ms.
178 // Create a Quit timer which will fire after 1s.
179 // Create a timer to reset idle every 50ms.
180 // Verify that we never fired.
181
182 mock_idle_time = GetTickCount();
183 TestIdleTask test_task(true);
184 TestFinishedTask finish_task;
185 ResetIdleTask reset_task;
186 MessageLoop* loop = MessageLoop::current();
187 Timer* t1 = loop->timer_manager()->StartTimer(1000, &finish_task, false);
188 Timer* reset_timer = loop->timer_manager()->StartTimer(50, &reset_task, true);
189 test_task.Start();
190 loop->Run();
191 loop->timer_manager()->StopTimer(reset_timer);
192
193 EXPECT_EQ(test_task.get_idle_counter(), 0);
194 delete t1;
195 delete reset_timer;
196}
license.botf003cfe2008-08-24 09:55:55 +0900197