blob: 1fb44a3caae130bb0352bc91d4bf2af46717e05c [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/message_loop.h"
6#include "base/task.h"
7#include "base/timer.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
dsh@google.com0f8dd262008-10-28 05:43:33 +090010using base::TimeDelta;
11
darin@google.com0b8a30c2008-08-29 05:50:12 +090012namespace {
13
14class OneShotTimerTester {
15 public:
16 OneShotTimerTester(bool* did_run) : did_run_(did_run) {
17 }
18 void Start() {
19 timer_.Start(TimeDelta::FromMilliseconds(10), this,
20 &OneShotTimerTester::Run);
21 }
22 private:
23 void Run() {
24 *did_run_ = true;
25 MessageLoop::current()->Quit();
26 }
27 bool* did_run_;
28 base::OneShotTimer<OneShotTimerTester> timer_;
29};
30
31class RepeatingTimerTester {
32 public:
33 RepeatingTimerTester(bool* did_run) : did_run_(did_run), counter_(10) {
34 }
35 void Start() {
36 timer_.Start(TimeDelta::FromMilliseconds(10), this,
37 &RepeatingTimerTester::Run);
38 }
39 private:
40 void Run() {
41 if (--counter_ == 0) {
42 *did_run_ = true;
43 MessageLoop::current()->Quit();
44 }
45 }
46 bool* did_run_;
47 int counter_;
48 base::RepeatingTimer<RepeatingTimerTester> timer_;
49};
50
darin@google.com0b8a30c2008-08-29 05:50:12 +090051void RunTest_OneShotTimer(MessageLoop::Type message_loop_type) {
52 MessageLoop loop(message_loop_type);
53
54 bool did_run = false;
55 OneShotTimerTester f(&did_run);
56 f.Start();
57
58 MessageLoop::current()->Run();
59
60 EXPECT_TRUE(did_run);
61}
62
63void RunTest_OneShotTimer_Cancel(MessageLoop::Type message_loop_type) {
64 MessageLoop loop(message_loop_type);
65
66 bool did_run_a = false;
67 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a);
68
69 // This should run before the timer expires.
70 MessageLoop::current()->DeleteSoon(FROM_HERE, a);
71
72 // Now start the timer.
73 a->Start();
74
75 bool did_run_b = false;
76 OneShotTimerTester b(&did_run_b);
77 b.Start();
78
79 MessageLoop::current()->Run();
80
81 EXPECT_FALSE(did_run_a);
82 EXPECT_TRUE(did_run_b);
83}
84
85void RunTest_RepeatingTimer(MessageLoop::Type message_loop_type) {
86 MessageLoop loop(message_loop_type);
87
88 bool did_run = false;
89 RepeatingTimerTester f(&did_run);
90 f.Start();
91
92 MessageLoop::current()->Run();
93
94 EXPECT_TRUE(did_run);
95}
96
97void RunTest_RepeatingTimer_Cancel(MessageLoop::Type message_loop_type) {
98 MessageLoop loop(message_loop_type);
99
100 bool did_run_a = false;
101 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a);
102
103 // This should run before the timer expires.
104 MessageLoop::current()->DeleteSoon(FROM_HERE, a);
105
106 // Now start the timer.
107 a->Start();
108
109 bool did_run_b = false;
110 RepeatingTimerTester b(&did_run_b);
111 b.Start();
112
113 MessageLoop::current()->Run();
114
115 EXPECT_FALSE(did_run_a);
116 EXPECT_TRUE(did_run_b);
117}
118
darin@google.comd936b5b2008-08-26 14:53:57 +0900119} // namespace
120
121//-----------------------------------------------------------------------------
122// Each test is run against each type of MessageLoop. That way we are sure
123// that timers work properly in all configurations.
124
darin@google.com0b8a30c2008-08-29 05:50:12 +0900125TEST(TimerTest, OneShotTimer) {
126 RunTest_OneShotTimer(MessageLoop::TYPE_DEFAULT);
127 RunTest_OneShotTimer(MessageLoop::TYPE_UI);
128 RunTest_OneShotTimer(MessageLoop::TYPE_IO);
129}
130
131TEST(TimerTest, OneShotTimer_Cancel) {
132 RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_DEFAULT);
133 RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_UI);
134 RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_IO);
135}
136
137TEST(TimerTest, RepeatingTimer) {
138 RunTest_RepeatingTimer(MessageLoop::TYPE_DEFAULT);
139 RunTest_RepeatingTimer(MessageLoop::TYPE_UI);
140 RunTest_RepeatingTimer(MessageLoop::TYPE_IO);
141}
142
143TEST(TimerTest, RepeatingTimer_Cancel) {
144 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_DEFAULT);
145 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_UI);
146 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_IO);
147}
mbelshe@google.come0efec82008-12-03 08:16:55 +0900148
149TEST(TimerTest, MessageLoopShutdown) {
150 // This test is designed to verify that shutdown of the
151 // message loop does not cause crashes if there were pending
152 // timers not yet fired. It may only trigger exceptions
153 // if debug heap checking (or purify) is enabled.
154 bool did_run = false;
155 {
156 OneShotTimerTester a(&did_run);
157 OneShotTimerTester b(&did_run);
158 OneShotTimerTester c(&did_run);
159 OneShotTimerTester d(&did_run);
160 {
161 MessageLoop loop(MessageLoop::TYPE_DEFAULT);
162 a.Start();
163 b.Start();
164 } // MessageLoop destructs by falling out of scope.
165 } // OneShotTimers destruct. SHOULD NOT CRASH, of course.
166
167 EXPECT_EQ(false, did_run);
168}