OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_pump_glib.h" | 5 #include "base/message_pump_glib.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
| 12 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 #if defined(TOOLKIT_USES_GTK) | 18 #if defined(TOOLKIT_USES_GTK) |
18 #include <gtk/gtk.h> | 19 #include <gtk/gtk.h> |
19 #endif | 20 #endif |
20 | 21 |
21 namespace { | 22 namespace { |
(...skipping 28 matching lines...) Expand all Loading... |
50 return false; | 51 return false; |
51 return events_[0].time <= base::Time::NowFromSystemTime(); | 52 return events_[0].time <= base::Time::NowFromSystemTime(); |
52 } | 53 } |
53 | 54 |
54 void HandleDispatch() { | 55 void HandleDispatch() { |
55 if (events_.empty()) | 56 if (events_.empty()) |
56 return; | 57 return; |
57 Event event = events_[0]; | 58 Event event = events_[0]; |
58 events_.erase(events_.begin()); | 59 events_.erase(events_.begin()); |
59 ++processed_events_; | 60 ++processed_events_; |
60 if (event.task) { | 61 if (!event.callback.is_null()) { |
| 62 event.callback.Run(); |
| 63 } else if (event.task) { |
61 event.task->Run(); | 64 event.task->Run(); |
62 delete event.task; | 65 delete event.task; |
63 } | 66 } |
64 } | 67 } |
65 | 68 |
66 // Adds an event to the queue. When "handled", executes |task|. | 69 // Adds an event to the queue. When "handled", executes |callback|. |
67 // delay_ms is relative to the last event if any, or to Now() otherwise. | 70 // delay_ms is relative to the last event if any, or to Now() otherwise. |
68 void AddEvent(int delay_ms, Task* task) { | 71 void AddEvent(int delay_ms, const base::Closure& callback) { |
69 base::Time last_time; | 72 AddEventHelper(delay_ms, callback, NULL); |
70 if (!events_.empty()) { | 73 } |
71 last_time = (events_.end()-1)->time; | 74 |
72 } else { | 75 void AddDummyEvent(int delay_ms) { |
73 last_time = base::Time::NowFromSystemTime(); | 76 AddEventHelper(delay_ms, base::Closure(), NULL); |
74 } | 77 } |
75 base::Time future = last_time + base::TimeDelta::FromMilliseconds(delay_ms); | 78 |
76 EventInjector::Event event = { future, task }; | 79 void AddEventAsTask(int delay_ms, Task* task) { |
77 events_.push_back(event); | 80 AddEventHelper(delay_ms, base::Closure(), task); |
78 } | 81 } |
79 | 82 |
80 void Reset() { | 83 void Reset() { |
81 processed_events_ = 0; | 84 processed_events_ = 0; |
82 events_.clear(); | 85 events_.clear(); |
83 } | 86 } |
84 | 87 |
85 int processed_events() const { return processed_events_; } | 88 int processed_events() const { return processed_events_; } |
86 | 89 |
87 private: | 90 private: |
88 struct Event { | 91 struct Event { |
89 base::Time time; | 92 base::Time time; |
| 93 base::Closure callback; |
90 Task* task; | 94 Task* task; |
91 }; | 95 }; |
92 | 96 |
93 struct Source : public GSource { | 97 struct Source : public GSource { |
94 EventInjector* injector; | 98 EventInjector* injector; |
95 }; | 99 }; |
96 | 100 |
| 101 void AddEventHelper(int delay_ms, const base::Closure& callback, Task* task) { |
| 102 base::Time last_time; |
| 103 if (!events_.empty()) { |
| 104 last_time = (events_.end()-1)->time; |
| 105 } else { |
| 106 last_time = base::Time::NowFromSystemTime(); |
| 107 } |
| 108 base::Time future = last_time + base::TimeDelta::FromMilliseconds(delay_ms); |
| 109 EventInjector::Event event = { future, callback, task }; |
| 110 events_.push_back(event); |
| 111 } |
| 112 |
97 static gboolean Prepare(GSource* source, gint* timeout_ms) { | 113 static gboolean Prepare(GSource* source, gint* timeout_ms) { |
98 *timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare(); | 114 *timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare(); |
99 return FALSE; | 115 return FALSE; |
100 } | 116 } |
101 | 117 |
102 static gboolean Check(GSource* source) { | 118 static gboolean Check(GSource* source) { |
103 return static_cast<Source*>(source)->injector->HandleCheck(); | 119 return static_cast<Source*>(source)->injector->HandleCheck(); |
104 } | 120 } |
105 | 121 |
106 static gboolean Dispatch(GSource* source, | 122 static gboolean Dispatch(GSource* source, |
(...skipping 23 matching lines...) Expand all Loading... |
130 | 146 |
131 void IncrementInt(int *value) { | 147 void IncrementInt(int *value) { |
132 ++*value; | 148 ++*value; |
133 } | 149 } |
134 | 150 |
135 // Checks how many events have been processed by the injector. | 151 // Checks how many events have been processed by the injector. |
136 void ExpectProcessedEvents(EventInjector* injector, int count) { | 152 void ExpectProcessedEvents(EventInjector* injector, int count) { |
137 EXPECT_EQ(injector->processed_events(), count); | 153 EXPECT_EQ(injector->processed_events(), count); |
138 } | 154 } |
139 | 155 |
140 // Quits the current message loop. | |
141 void QuitMessageLoop() { | |
142 MessageLoop::current()->Quit(); | |
143 } | |
144 | |
145 // Returns a new task that quits the main loop. | |
146 Task* NewQuitTask() { | |
147 return NewRunnableFunction(QuitMessageLoop); | |
148 } | |
149 | |
150 // Posts a task on the current message loop. | 156 // Posts a task on the current message loop. |
151 void PostMessageLoopTask(const tracked_objects::Location& from_here, | 157 void PostMessageLoopTask(const tracked_objects::Location& from_here, |
152 Task* task) { | 158 Task* task) { |
153 MessageLoop::current()->PostTask(from_here, task); | 159 MessageLoop::current()->PostTask(from_here, task); |
154 } | 160 } |
155 | 161 |
156 // Test fixture. | 162 // Test fixture. |
157 class MessagePumpGLibTest : public testing::Test { | 163 class MessagePumpGLibTest : public testing::Test { |
158 public: | 164 public: |
159 MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { } | 165 MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { } |
(...skipping 21 matching lines...) Expand all Loading... |
181 | 187 |
182 } // namespace | 188 } // namespace |
183 | 189 |
184 // EventInjector is expected to always live longer than the runnable methods. | 190 // EventInjector is expected to always live longer than the runnable methods. |
185 DISABLE_RUNNABLE_METHOD_REFCOUNT(EventInjector); | 191 DISABLE_RUNNABLE_METHOD_REFCOUNT(EventInjector); |
186 | 192 |
187 TEST_F(MessagePumpGLibTest, TestQuit) { | 193 TEST_F(MessagePumpGLibTest, TestQuit) { |
188 // Checks that Quit works and that the basic infrastructure is working. | 194 // Checks that Quit works and that the basic infrastructure is working. |
189 | 195 |
190 // Quit from a task | 196 // Quit from a task |
191 loop()->PostTask(FROM_HERE, NewQuitTask()); | 197 loop()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
192 loop()->Run(); | 198 loop()->Run(); |
193 EXPECT_EQ(0, injector()->processed_events()); | 199 EXPECT_EQ(0, injector()->processed_events()); |
194 | 200 |
195 injector()->Reset(); | 201 injector()->Reset(); |
196 // Quit from an event | 202 // Quit from an event |
197 injector()->AddEvent(0, NewQuitTask()); | 203 injector()->AddEvent(0, MessageLoop::QuitClosure()); |
198 loop()->Run(); | 204 loop()->Run(); |
199 EXPECT_EQ(1, injector()->processed_events()); | 205 EXPECT_EQ(1, injector()->processed_events()); |
200 } | 206 } |
201 | 207 |
202 TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { | 208 TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { |
203 // Checks that tasks posted by events are executed before the next event if | 209 // Checks that tasks posted by events are executed before the next event if |
204 // the posted task queue is empty. | 210 // the posted task queue is empty. |
205 // MessageLoop doesn't make strong guarantees that it is the case, but the | 211 // MessageLoop doesn't make strong guarantees that it is the case, but the |
206 // current implementation ensures it and the tests below rely on it. | 212 // current implementation ensures it and the tests below rely on it. |
207 // If changes cause this test to fail, it is reasonable to change it, but | 213 // If changes cause this test to fail, it is reasonable to change it, but |
208 // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be | 214 // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be |
209 // changed accordingly, otherwise they can become flaky. | 215 // changed accordingly, otherwise they can become flaky. |
210 injector()->AddEvent(0, NewRunnableFunction(DoNothing)); | 216 injector()->AddEventAsTask(0, NewRunnableFunction(DoNothing)); |
211 Task* check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 2); | 217 Task* check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 2); |
212 Task* posted_task = NewRunnableFunction(PostMessageLoopTask, | 218 Task* posted_task = NewRunnableFunction(PostMessageLoopTask, |
213 FROM_HERE, check_task); | 219 FROM_HERE, check_task); |
214 injector()->AddEvent(0, posted_task); | 220 injector()->AddEventAsTask(0, posted_task); |
215 injector()->AddEvent(0, NewRunnableFunction(DoNothing)); | 221 injector()->AddEventAsTask(0, NewRunnableFunction(DoNothing)); |
216 injector()->AddEvent(0, NewQuitTask()); | 222 injector()->AddEvent(0, MessageLoop::QuitClosure()); |
217 loop()->Run(); | 223 loop()->Run(); |
218 EXPECT_EQ(4, injector()->processed_events()); | 224 EXPECT_EQ(4, injector()->processed_events()); |
219 | 225 |
220 injector()->Reset(); | 226 injector()->Reset(); |
221 injector()->AddEvent(0, NewRunnableFunction(DoNothing)); | 227 injector()->AddEventAsTask(0, NewRunnableFunction(DoNothing)); |
222 check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 2); | 228 check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 2); |
223 posted_task = NewRunnableFunction(PostMessageLoopTask, FROM_HERE, check_task); | 229 posted_task = NewRunnableFunction(PostMessageLoopTask, FROM_HERE, check_task); |
224 injector()->AddEvent(0, posted_task); | 230 injector()->AddEventAsTask(0, posted_task); |
225 injector()->AddEvent(10, NewRunnableFunction(DoNothing)); | 231 injector()->AddEventAsTask(10, NewRunnableFunction(DoNothing)); |
226 injector()->AddEvent(0, NewQuitTask()); | 232 injector()->AddEvent(0, MessageLoop::QuitClosure()); |
227 loop()->Run(); | 233 loop()->Run(); |
228 EXPECT_EQ(4, injector()->processed_events()); | 234 EXPECT_EQ(4, injector()->processed_events()); |
229 } | 235 } |
230 | 236 |
231 TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { | 237 TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { |
232 int task_count = 0; | 238 int task_count = 0; |
233 // Tests that we process tasks while waiting for new events. | 239 // Tests that we process tasks while waiting for new events. |
234 // The event queue is empty at first. | 240 // The event queue is empty at first. |
235 for (int i = 0; i < 10; ++i) { | 241 for (int i = 0; i < 10; ++i) { |
236 loop()->PostTask(FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 242 loop()->PostTask(FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); |
237 } | 243 } |
238 // After all the previous tasks have executed, enqueue an event that will | 244 // After all the previous tasks have executed, enqueue an event that will |
239 // quit. | 245 // quit. |
240 loop()->PostTask( | 246 loop()->PostTask( |
241 FROM_HERE, NewRunnableMethod(injector(), &EventInjector::AddEvent, | 247 FROM_HERE, NewRunnableMethod(injector(), &EventInjector::AddEvent, |
242 0, NewQuitTask())); | 248 0, MessageLoop::QuitClosure())); |
243 loop()->Run(); | 249 loop()->Run(); |
244 ASSERT_EQ(10, task_count); | 250 ASSERT_EQ(10, task_count); |
245 EXPECT_EQ(1, injector()->processed_events()); | 251 EXPECT_EQ(1, injector()->processed_events()); |
246 | 252 |
247 // Tests that we process delayed tasks while waiting for new events. | 253 // Tests that we process delayed tasks while waiting for new events. |
248 injector()->Reset(); | 254 injector()->Reset(); |
249 task_count = 0; | 255 task_count = 0; |
250 for (int i = 0; i < 10; ++i) { | 256 for (int i = 0; i < 10; ++i) { |
251 loop()->PostDelayedTask( | 257 loop()->PostDelayedTask( |
252 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 10*i); | 258 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 10*i); |
253 } | 259 } |
254 // After all the previous tasks have executed, enqueue an event that will | 260 // After all the previous tasks have executed, enqueue an event that will |
255 // quit. | 261 // quit. |
256 // This relies on the fact that delayed tasks are executed in delay order. | 262 // This relies on the fact that delayed tasks are executed in delay order. |
257 // That is verified in message_loop_unittest.cc. | 263 // That is verified in message_loop_unittest.cc. |
258 loop()->PostDelayedTask( | 264 loop()->PostDelayedTask( |
259 FROM_HERE, NewRunnableMethod(injector(), &EventInjector::AddEvent, | 265 FROM_HERE, NewRunnableMethod(injector(), &EventInjector::AddEvent, |
260 10, NewQuitTask()), 150); | 266 10, MessageLoop::QuitClosure()), 150); |
261 loop()->Run(); | 267 loop()->Run(); |
262 ASSERT_EQ(10, task_count); | 268 ASSERT_EQ(10, task_count); |
263 EXPECT_EQ(1, injector()->processed_events()); | 269 EXPECT_EQ(1, injector()->processed_events()); |
264 } | 270 } |
265 | 271 |
266 TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) { | 272 TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) { |
267 // Tests that we process events while waiting for work. | 273 // Tests that we process events while waiting for work. |
268 // The event queue is empty at first. | 274 // The event queue is empty at first. |
269 for (int i = 0; i < 10; ++i) { | 275 for (int i = 0; i < 10; ++i) { |
270 injector()->AddEvent(0, NULL); | 276 injector()->AddDummyEvent(0); |
271 } | 277 } |
272 // After all the events have been processed, post a task that will check that | 278 // After all the events have been processed, post a task that will check that |
273 // the events have been processed (note: the task executes after the event | 279 // the events have been processed (note: the task executes after the event |
274 // that posted it has been handled, so we expect 11 at that point). | 280 // that posted it has been handled, so we expect 11 at that point). |
275 Task* check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 11); | 281 Task* check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 11); |
276 Task* posted_task = NewRunnableFunction(PostMessageLoopTask, | 282 Task* posted_task = NewRunnableFunction(PostMessageLoopTask, |
277 FROM_HERE, check_task); | 283 FROM_HERE, check_task); |
278 injector()->AddEvent(10, posted_task); | 284 injector()->AddEventAsTask(10, posted_task); |
279 | 285 |
280 // And then quit (relies on the condition tested by TestEventTaskInterleave). | 286 // And then quit (relies on the condition tested by TestEventTaskInterleave). |
281 injector()->AddEvent(10, NewQuitTask()); | 287 injector()->AddEvent(10, MessageLoop::QuitClosure()); |
282 loop()->Run(); | 288 loop()->Run(); |
283 | 289 |
284 EXPECT_EQ(12, injector()->processed_events()); | 290 EXPECT_EQ(12, injector()->processed_events()); |
285 } | 291 } |
286 | 292 |
287 namespace { | 293 namespace { |
288 | 294 |
289 // This class is a helper for the concurrent events / posted tasks test below. | 295 // This class is a helper for the concurrent events / posted tasks test below. |
290 // It will quit the main loop once enough tasks and events have been processed, | 296 // It will quit the main loop once enough tasks and events have been processed, |
291 // while making sure there is always work to do and events in the queue. | 297 // while making sure there is always work to do and events in the queue. |
(...skipping 17 matching lines...) Expand all Loading... |
309 } | 315 } |
310 } | 316 } |
311 | 317 |
312 void FromEvent() { | 318 void FromEvent() { |
313 if (event_count_ > 0) { | 319 if (event_count_ > 0) { |
314 --event_count_; | 320 --event_count_; |
315 } | 321 } |
316 if (task_count_ == 0 && event_count_ == 0) { | 322 if (task_count_ == 0 && event_count_ == 0) { |
317 MessageLoop::current()->Quit(); | 323 MessageLoop::current()->Quit(); |
318 } else { | 324 } else { |
319 injector_->AddEvent( | 325 injector_->AddEventAsTask( |
320 0, NewRunnableMethod(this, &ConcurrentHelper::FromEvent)); | 326 0, NewRunnableMethod(this, &ConcurrentHelper::FromEvent)); |
321 } | 327 } |
322 } | 328 } |
323 | 329 |
324 int event_count() const { return event_count_; } | 330 int event_count() const { return event_count_; } |
325 int task_count() const { return task_count_; } | 331 int task_count() const { return task_count_; } |
326 | 332 |
327 private: | 333 private: |
328 friend class base::RefCounted<ConcurrentHelper>; | 334 friend class base::RefCounted<ConcurrentHelper>; |
329 | 335 |
(...skipping 12 matching lines...) Expand all Loading... |
342 TEST_F(MessagePumpGLibTest, TestConcurrentEventPostedTask) { | 348 TEST_F(MessagePumpGLibTest, TestConcurrentEventPostedTask) { |
343 // Tests that posted tasks don't starve events, nor the opposite. | 349 // Tests that posted tasks don't starve events, nor the opposite. |
344 // We use the helper class above. We keep both event and posted task queues | 350 // We use the helper class above. We keep both event and posted task queues |
345 // full, the helper verifies that both tasks and events get processed. | 351 // full, the helper verifies that both tasks and events get processed. |
346 // If that is not the case, either event_count_ or task_count_ will not get | 352 // If that is not the case, either event_count_ or task_count_ will not get |
347 // to 0, and MessageLoop::Quit() will never be called. | 353 // to 0, and MessageLoop::Quit() will never be called. |
348 scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector()); | 354 scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector()); |
349 | 355 |
350 // Add 2 events to the queue to make sure it is always full (when we remove | 356 // Add 2 events to the queue to make sure it is always full (when we remove |
351 // the event before processing it). | 357 // the event before processing it). |
352 injector()->AddEvent( | 358 injector()->AddEventAsTask( |
353 0, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromEvent)); | 359 0, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromEvent)); |
354 injector()->AddEvent( | 360 injector()->AddEventAsTask( |
355 0, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromEvent)); | 361 0, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromEvent)); |
356 | 362 |
357 // Similarly post 2 tasks. | 363 // Similarly post 2 tasks. |
358 loop()->PostTask( | 364 loop()->PostTask( |
359 FROM_HERE, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromTask)); | 365 FROM_HERE, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromTask)); |
360 loop()->PostTask( | 366 loop()->PostTask( |
361 FROM_HERE, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromTask)); | 367 FROM_HERE, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromTask)); |
362 | 368 |
363 loop()->Run(); | 369 loop()->Run(); |
364 EXPECT_EQ(0, helper->event_count()); | 370 EXPECT_EQ(0, helper->event_count()); |
365 EXPECT_EQ(0, helper->task_count()); | 371 EXPECT_EQ(0, helper->task_count()); |
366 } | 372 } |
367 | 373 |
368 namespace { | 374 namespace { |
369 | 375 |
370 void AddEventsAndDrainGLib(EventInjector* injector) { | 376 void AddEventsAndDrainGLib(EventInjector* injector) { |
371 // Add a couple of dummy events | 377 // Add a couple of dummy events |
372 injector->AddEvent(0, NULL); | 378 injector->AddDummyEvent(0); |
373 injector->AddEvent(0, NULL); | 379 injector->AddDummyEvent(0); |
374 // Then add an event that will quit the main loop. | 380 // Then add an event that will quit the main loop. |
375 injector->AddEvent(0, NewQuitTask()); | 381 injector->AddEvent(0, MessageLoop::QuitClosure()); |
376 | 382 |
377 // Post a couple of dummy tasks | 383 // Post a couple of dummy tasks |
378 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); | 384 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); |
379 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); | 385 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); |
380 | 386 |
381 // Drain the events | 387 // Drain the events |
382 while (g_main_context_pending(NULL)) { | 388 while (g_main_context_pending(NULL)) { |
383 g_main_context_iteration(NULL, FALSE); | 389 g_main_context_iteration(NULL, FALSE); |
384 } | 390 } |
385 } | 391 } |
386 | 392 |
387 } // namespace | 393 } // namespace |
388 | 394 |
389 TEST_F(MessagePumpGLibTest, TestDrainingGLib) { | 395 TEST_F(MessagePumpGLibTest, TestDrainingGLib) { |
390 // Tests that draining events using GLib works. | 396 // Tests that draining events using GLib works. |
391 loop()->PostTask( | 397 loop()->PostTask( |
392 FROM_HERE, NewRunnableFunction(AddEventsAndDrainGLib, injector())); | 398 FROM_HERE, NewRunnableFunction(AddEventsAndDrainGLib, injector())); |
393 loop()->Run(); | 399 loop()->Run(); |
394 | 400 |
395 EXPECT_EQ(3, injector()->processed_events()); | 401 EXPECT_EQ(3, injector()->processed_events()); |
396 } | 402 } |
397 | 403 |
398 | 404 |
399 namespace { | 405 namespace { |
400 | 406 |
401 #if defined(TOOLKIT_USES_GTK) | 407 #if defined(TOOLKIT_USES_GTK) |
402 void AddEventsAndDrainGtk(EventInjector* injector) { | 408 void AddEventsAndDrainGtk(EventInjector* injector) { |
403 // Add a couple of dummy events | 409 // Add a couple of dummy events |
404 injector->AddEvent(0, NULL); | 410 injector->AddDummyEvent(0); |
405 injector->AddEvent(0, NULL); | 411 injector->AddDummyEvent(0); |
406 // Then add an event that will quit the main loop. | 412 // Then add an event that will quit the main loop. |
407 injector->AddEvent(0, NewQuitTask()); | 413 injector->AddEvent(0, MessageLoop::QuitClosure()); |
408 | 414 |
409 // Post a couple of dummy tasks | 415 // Post a couple of dummy tasks |
410 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); | 416 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); |
411 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); | 417 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); |
412 | 418 |
413 // Drain the events | 419 // Drain the events |
414 while (gtk_events_pending()) { | 420 while (gtk_events_pending()) { |
415 gtk_main_iteration(); | 421 gtk_main_iteration(); |
416 } | 422 } |
417 } | 423 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 bool quit_; | 477 bool quit_; |
472 }; | 478 }; |
473 | 479 |
474 void TestGLibLoopInternal(EventInjector* injector) { | 480 void TestGLibLoopInternal(EventInjector* injector) { |
475 // Allow tasks to be processed from 'native' event loops. | 481 // Allow tasks to be processed from 'native' event loops. |
476 MessageLoop::current()->SetNestableTasksAllowed(true); | 482 MessageLoop::current()->SetNestableTasksAllowed(true); |
477 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); | 483 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
478 | 484 |
479 int task_count = 0; | 485 int task_count = 0; |
480 // Add a couple of dummy events | 486 // Add a couple of dummy events |
481 injector->AddEvent(0, NULL); | 487 injector->AddDummyEvent(0); |
482 injector->AddEvent(0, NULL); | 488 injector->AddDummyEvent(0); |
483 // Post a couple of dummy tasks | 489 // Post a couple of dummy tasks |
484 MessageLoop::current()->PostTask( | 490 MessageLoop::current()->PostTask( |
485 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 491 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); |
486 MessageLoop::current()->PostTask( | 492 MessageLoop::current()->PostTask( |
487 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 493 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); |
488 // Delayed events | 494 // Delayed events |
489 injector->AddEvent(10, NULL); | 495 injector->AddDummyEvent(10); |
490 injector->AddEvent(10, NULL); | 496 injector->AddDummyEvent(10); |
491 // Delayed work | 497 // Delayed work |
492 MessageLoop::current()->PostDelayedTask( | 498 MessageLoop::current()->PostDelayedTask( |
493 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 30); | 499 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 30); |
494 MessageLoop::current()->PostDelayedTask( | 500 MessageLoop::current()->PostDelayedTask( |
495 FROM_HERE, NewRunnableMethod(runner.get(), &GLibLoopRunner::Quit), 40); | 501 FROM_HERE, NewRunnableMethod(runner.get(), &GLibLoopRunner::Quit), 40); |
496 | 502 |
497 // Run a nested, straight GLib message loop. | 503 // Run a nested, straight GLib message loop. |
498 runner->RunGLib(); | 504 runner->RunGLib(); |
499 | 505 |
500 ASSERT_EQ(3, task_count); | 506 ASSERT_EQ(3, task_count); |
501 EXPECT_EQ(4, injector->processed_events()); | 507 EXPECT_EQ(4, injector->processed_events()); |
502 MessageLoop::current()->Quit(); | 508 MessageLoop::current()->Quit(); |
503 } | 509 } |
504 | 510 |
505 void TestGtkLoopInternal(EventInjector* injector) { | 511 void TestGtkLoopInternal(EventInjector* injector) { |
506 // Allow tasks to be processed from 'native' event loops. | 512 // Allow tasks to be processed from 'native' event loops. |
507 MessageLoop::current()->SetNestableTasksAllowed(true); | 513 MessageLoop::current()->SetNestableTasksAllowed(true); |
508 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); | 514 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
509 | 515 |
510 int task_count = 0; | 516 int task_count = 0; |
511 // Add a couple of dummy events | 517 // Add a couple of dummy events |
512 injector->AddEvent(0, NULL); | 518 injector->AddDummyEvent(0); |
513 injector->AddEvent(0, NULL); | 519 injector->AddDummyEvent(0); |
514 // Post a couple of dummy tasks | 520 // Post a couple of dummy tasks |
515 MessageLoop::current()->PostTask( | 521 MessageLoop::current()->PostTask( |
516 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 522 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); |
517 MessageLoop::current()->PostTask( | 523 MessageLoop::current()->PostTask( |
518 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 524 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); |
519 // Delayed events | 525 // Delayed events |
520 injector->AddEvent(10, NULL); | 526 injector->AddDummyEvent(10); |
521 injector->AddEvent(10, NULL); | 527 injector->AddDummyEvent(10); |
522 // Delayed work | 528 // Delayed work |
523 MessageLoop::current()->PostDelayedTask( | 529 MessageLoop::current()->PostDelayedTask( |
524 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 30); | 530 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 30); |
525 MessageLoop::current()->PostDelayedTask( | 531 MessageLoop::current()->PostDelayedTask( |
526 FROM_HERE, NewRunnableMethod(runner.get(), &GLibLoopRunner::Quit), 40); | 532 FROM_HERE, NewRunnableMethod(runner.get(), &GLibLoopRunner::Quit), 40); |
527 | 533 |
528 // Run a nested, straight Gtk message loop. | 534 // Run a nested, straight Gtk message loop. |
529 runner->RunLoop(); | 535 runner->RunLoop(); |
530 | 536 |
531 ASSERT_EQ(3, task_count); | 537 ASSERT_EQ(3, task_count); |
(...skipping 15 matching lines...) Expand all Loading... |
547 | 553 |
548 TEST_F(MessagePumpGLibTest, TestGtkLoop) { | 554 TEST_F(MessagePumpGLibTest, TestGtkLoop) { |
549 // Tests that events and posted tasks are correctly exectuted if the message | 555 // Tests that events and posted tasks are correctly exectuted if the message |
550 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. | 556 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
551 // Note that in this case we don't make strong guarantees about niceness | 557 // Note that in this case we don't make strong guarantees about niceness |
552 // between events and posted tasks. | 558 // between events and posted tasks. |
553 loop()->PostTask(FROM_HERE, | 559 loop()->PostTask(FROM_HERE, |
554 NewRunnableFunction(TestGtkLoopInternal, injector())); | 560 NewRunnableFunction(TestGtkLoopInternal, injector())); |
555 loop()->Run(); | 561 loop()->Run(); |
556 } | 562 } |
OLD | NEW |