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/bind.h" |
| 13 #include "base/bind_helpers.h" |
12 #include "base/callback.h" | 14 #include "base/callback.h" |
13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
14 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
15 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 #if defined(TOOLKIT_USES_GTK) | 20 #if defined(TOOLKIT_USES_GTK) |
19 #include <gtk/gtk.h> | 21 #include <gtk/gtk.h> |
20 #endif | 22 #endif |
21 | 23 |
(...skipping 29 matching lines...) Expand all Loading... |
51 return false; | 53 return false; |
52 return events_[0].time <= base::Time::NowFromSystemTime(); | 54 return events_[0].time <= base::Time::NowFromSystemTime(); |
53 } | 55 } |
54 | 56 |
55 void HandleDispatch() { | 57 void HandleDispatch() { |
56 if (events_.empty()) | 58 if (events_.empty()) |
57 return; | 59 return; |
58 Event event = events_[0]; | 60 Event event = events_[0]; |
59 events_.erase(events_.begin()); | 61 events_.erase(events_.begin()); |
60 ++processed_events_; | 62 ++processed_events_; |
61 if (!event.callback.is_null()) { | 63 if (!event.callback.is_null()) |
62 event.callback.Run(); | 64 event.callback.Run(); |
63 } else if (event.task) { | 65 else if (!event.task.is_null()) |
64 event.task->Run(); | 66 event.task.Run(); |
65 delete event.task; | |
66 } | |
67 } | 67 } |
68 | 68 |
69 // Adds an event to the queue. When "handled", executes |callback|. | 69 // Adds an event to the queue. When "handled", executes |callback|. |
70 // 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. |
71 void AddEvent(int delay_ms, const base::Closure& callback) { | 71 void AddEvent(int delay_ms, const base::Closure& callback) { |
72 AddEventHelper(delay_ms, callback, NULL); | 72 AddEventHelper(delay_ms, callback, base::Closure()); |
73 } | 73 } |
74 | 74 |
75 void AddDummyEvent(int delay_ms) { | 75 void AddDummyEvent(int delay_ms) { |
76 AddEventHelper(delay_ms, base::Closure(), NULL); | 76 AddEventHelper(delay_ms, base::Closure(), base::Closure()); |
77 } | 77 } |
78 | 78 |
79 void AddEventAsTask(int delay_ms, Task* task) { | 79 void AddEventAsTask(int delay_ms, const base::Closure& task) { |
80 AddEventHelper(delay_ms, base::Closure(), task); | 80 AddEventHelper(delay_ms, base::Closure(), task); |
81 } | 81 } |
82 | 82 |
83 void Reset() { | 83 void Reset() { |
84 processed_events_ = 0; | 84 processed_events_ = 0; |
85 events_.clear(); | 85 events_.clear(); |
86 } | 86 } |
87 | 87 |
88 int processed_events() const { return processed_events_; } | 88 int processed_events() const { return processed_events_; } |
89 | 89 |
90 private: | 90 private: |
91 struct Event { | 91 struct Event { |
92 base::Time time; | 92 base::Time time; |
93 base::Closure callback; | 93 base::Closure callback; |
94 Task* task; | 94 base::Closure task; |
95 }; | 95 }; |
96 | 96 |
97 struct Source : public GSource { | 97 struct Source : public GSource { |
98 EventInjector* injector; | 98 EventInjector* injector; |
99 }; | 99 }; |
100 | 100 |
101 void AddEventHelper(int delay_ms, const base::Closure& callback, Task* task) { | 101 void AddEventHelper( |
| 102 int delay_ms, const base::Closure& callback, const base::Closure& task) { |
102 base::Time last_time; | 103 base::Time last_time; |
103 if (!events_.empty()) { | 104 if (!events_.empty()) |
104 last_time = (events_.end()-1)->time; | 105 last_time = (events_.end()-1)->time; |
105 } else { | 106 else |
106 last_time = base::Time::NowFromSystemTime(); | 107 last_time = base::Time::NowFromSystemTime(); |
107 } | 108 |
108 base::Time future = last_time + base::TimeDelta::FromMilliseconds(delay_ms); | 109 base::Time future = last_time + base::TimeDelta::FromMilliseconds(delay_ms); |
109 EventInjector::Event event = { future, callback, task }; | 110 EventInjector::Event event = {future, callback, task}; |
110 events_.push_back(event); | 111 events_.push_back(event); |
111 } | 112 } |
112 | 113 |
113 static gboolean Prepare(GSource* source, gint* timeout_ms) { | 114 static gboolean Prepare(GSource* source, gint* timeout_ms) { |
114 *timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare(); | 115 *timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare(); |
115 return FALSE; | 116 return FALSE; |
116 } | 117 } |
117 | 118 |
118 static gboolean Check(GSource* source) { | 119 static gboolean Check(GSource* source) { |
119 return static_cast<Source*>(source)->injector->HandleCheck(); | 120 return static_cast<Source*>(source)->injector->HandleCheck(); |
(...skipping 28 matching lines...) Expand all Loading... |
148 ++*value; | 149 ++*value; |
149 } | 150 } |
150 | 151 |
151 // Checks how many events have been processed by the injector. | 152 // Checks how many events have been processed by the injector. |
152 void ExpectProcessedEvents(EventInjector* injector, int count) { | 153 void ExpectProcessedEvents(EventInjector* injector, int count) { |
153 EXPECT_EQ(injector->processed_events(), count); | 154 EXPECT_EQ(injector->processed_events(), count); |
154 } | 155 } |
155 | 156 |
156 // Posts a task on the current message loop. | 157 // Posts a task on the current message loop. |
157 void PostMessageLoopTask(const tracked_objects::Location& from_here, | 158 void PostMessageLoopTask(const tracked_objects::Location& from_here, |
158 Task* task) { | 159 const base::Closure& task) { |
159 MessageLoop::current()->PostTask(from_here, task); | 160 MessageLoop::current()->PostTask(from_here, task); |
160 } | 161 } |
161 | 162 |
162 // Test fixture. | 163 // Test fixture. |
163 class MessagePumpGLibTest : public testing::Test { | 164 class MessagePumpGLibTest : public testing::Test { |
164 public: | 165 public: |
165 MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { } | 166 MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { } |
166 | 167 |
167 virtual void SetUp() { | 168 virtual void SetUp() { |
168 loop_ = new MessageLoop(MessageLoop::TYPE_UI); | 169 loop_ = new MessageLoop(MessageLoop::TYPE_UI); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } | 207 } |
207 | 208 |
208 TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { | 209 TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { |
209 // Checks that tasks posted by events are executed before the next event if | 210 // Checks that tasks posted by events are executed before the next event if |
210 // the posted task queue is empty. | 211 // the posted task queue is empty. |
211 // MessageLoop doesn't make strong guarantees that it is the case, but the | 212 // MessageLoop doesn't make strong guarantees that it is the case, but the |
212 // current implementation ensures it and the tests below rely on it. | 213 // current implementation ensures it and the tests below rely on it. |
213 // If changes cause this test to fail, it is reasonable to change it, but | 214 // If changes cause this test to fail, it is reasonable to change it, but |
214 // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be | 215 // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be |
215 // changed accordingly, otherwise they can become flaky. | 216 // changed accordingly, otherwise they can become flaky. |
216 injector()->AddEventAsTask(0, NewRunnableFunction(DoNothing)); | 217 injector()->AddEventAsTask(0, base::Bind(&DoNothing)); |
217 Task* check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 2); | 218 base::Closure check_task = |
218 Task* posted_task = NewRunnableFunction(PostMessageLoopTask, | 219 base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 2); |
219 FROM_HERE, check_task); | 220 base::Closure posted_task = |
| 221 base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); |
220 injector()->AddEventAsTask(0, posted_task); | 222 injector()->AddEventAsTask(0, posted_task); |
221 injector()->AddEventAsTask(0, NewRunnableFunction(DoNothing)); | 223 injector()->AddEventAsTask(0, base::Bind(&DoNothing)); |
222 injector()->AddEvent(0, MessageLoop::QuitClosure()); | 224 injector()->AddEvent(0, MessageLoop::QuitClosure()); |
223 loop()->Run(); | 225 loop()->Run(); |
224 EXPECT_EQ(4, injector()->processed_events()); | 226 EXPECT_EQ(4, injector()->processed_events()); |
225 | 227 |
226 injector()->Reset(); | 228 injector()->Reset(); |
227 injector()->AddEventAsTask(0, NewRunnableFunction(DoNothing)); | 229 injector()->AddEventAsTask(0, base::Bind(&DoNothing)); |
228 check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 2); | 230 check_task = |
229 posted_task = NewRunnableFunction(PostMessageLoopTask, FROM_HERE, check_task); | 231 base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 2); |
| 232 posted_task = base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); |
230 injector()->AddEventAsTask(0, posted_task); | 233 injector()->AddEventAsTask(0, posted_task); |
231 injector()->AddEventAsTask(10, NewRunnableFunction(DoNothing)); | 234 injector()->AddEventAsTask(10, base::Bind(&DoNothing)); |
232 injector()->AddEvent(0, MessageLoop::QuitClosure()); | 235 injector()->AddEvent(0, MessageLoop::QuitClosure()); |
233 loop()->Run(); | 236 loop()->Run(); |
234 EXPECT_EQ(4, injector()->processed_events()); | 237 EXPECT_EQ(4, injector()->processed_events()); |
235 } | 238 } |
236 | 239 |
237 TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { | 240 TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { |
238 int task_count = 0; | 241 int task_count = 0; |
239 // Tests that we process tasks while waiting for new events. | 242 // Tests that we process tasks while waiting for new events. |
240 // The event queue is empty at first. | 243 // The event queue is empty at first. |
241 for (int i = 0; i < 10; ++i) { | 244 for (int i = 0; i < 10; ++i) { |
242 loop()->PostTask(FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 245 loop()->PostTask(FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
243 } | 246 } |
244 // After all the previous tasks have executed, enqueue an event that will | 247 // After all the previous tasks have executed, enqueue an event that will |
245 // quit. | 248 // quit. |
246 loop()->PostTask( | 249 loop()->PostTask( |
247 FROM_HERE, NewRunnableMethod(injector(), &EventInjector::AddEvent, | 250 FROM_HERE, |
248 0, MessageLoop::QuitClosure())); | 251 base::Bind(&EventInjector::AddEvent, base::Unretained(injector()), 0, |
| 252 MessageLoop::QuitClosure())); |
249 loop()->Run(); | 253 loop()->Run(); |
250 ASSERT_EQ(10, task_count); | 254 ASSERT_EQ(10, task_count); |
251 EXPECT_EQ(1, injector()->processed_events()); | 255 EXPECT_EQ(1, injector()->processed_events()); |
252 | 256 |
253 // Tests that we process delayed tasks while waiting for new events. | 257 // Tests that we process delayed tasks while waiting for new events. |
254 injector()->Reset(); | 258 injector()->Reset(); |
255 task_count = 0; | 259 task_count = 0; |
256 for (int i = 0; i < 10; ++i) { | 260 for (int i = 0; i < 10; ++i) { |
257 loop()->PostDelayedTask( | 261 loop()->PostDelayedTask( |
258 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 10*i); | 262 FROM_HERE, base::Bind(&IncrementInt, &task_count), 10*i); |
259 } | 263 } |
260 // After all the previous tasks have executed, enqueue an event that will | 264 // After all the previous tasks have executed, enqueue an event that will |
261 // quit. | 265 // quit. |
262 // This relies on the fact that delayed tasks are executed in delay order. | 266 // This relies on the fact that delayed tasks are executed in delay order. |
263 // That is verified in message_loop_unittest.cc. | 267 // That is verified in message_loop_unittest.cc. |
264 loop()->PostDelayedTask( | 268 loop()->PostDelayedTask( |
265 FROM_HERE, NewRunnableMethod(injector(), &EventInjector::AddEvent, | 269 FROM_HERE, |
266 10, MessageLoop::QuitClosure()), 150); | 270 base::Bind(&EventInjector::AddEvent, base::Unretained(injector()), 10, |
| 271 MessageLoop::QuitClosure()), 150); |
267 loop()->Run(); | 272 loop()->Run(); |
268 ASSERT_EQ(10, task_count); | 273 ASSERT_EQ(10, task_count); |
269 EXPECT_EQ(1, injector()->processed_events()); | 274 EXPECT_EQ(1, injector()->processed_events()); |
270 } | 275 } |
271 | 276 |
272 TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) { | 277 TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) { |
273 // Tests that we process events while waiting for work. | 278 // Tests that we process events while waiting for work. |
274 // The event queue is empty at first. | 279 // The event queue is empty at first. |
275 for (int i = 0; i < 10; ++i) { | 280 for (int i = 0; i < 10; ++i) { |
276 injector()->AddDummyEvent(0); | 281 injector()->AddDummyEvent(0); |
277 } | 282 } |
278 // After all the events have been processed, post a task that will check that | 283 // After all the events have been processed, post a task that will check that |
279 // the events have been processed (note: the task executes after the event | 284 // the events have been processed (note: the task executes after the event |
280 // that posted it has been handled, so we expect 11 at that point). | 285 // that posted it has been handled, so we expect 11 at that point). |
281 Task* check_task = NewRunnableFunction(ExpectProcessedEvents, injector(), 11); | 286 base::Closure check_task = |
282 Task* posted_task = NewRunnableFunction(PostMessageLoopTask, | 287 base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 11); |
283 FROM_HERE, check_task); | 288 base::Closure posted_task = |
| 289 base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); |
284 injector()->AddEventAsTask(10, posted_task); | 290 injector()->AddEventAsTask(10, posted_task); |
285 | 291 |
286 // And then quit (relies on the condition tested by TestEventTaskInterleave). | 292 // And then quit (relies on the condition tested by TestEventTaskInterleave). |
287 injector()->AddEvent(10, MessageLoop::QuitClosure()); | 293 injector()->AddEvent(10, MessageLoop::QuitClosure()); |
288 loop()->Run(); | 294 loop()->Run(); |
289 | 295 |
290 EXPECT_EQ(12, injector()->processed_events()); | 296 EXPECT_EQ(12, injector()->processed_events()); |
291 } | 297 } |
292 | 298 |
293 namespace { | 299 namespace { |
(...skipping 10 matching lines...) Expand all Loading... |
304 } | 310 } |
305 | 311 |
306 void FromTask() { | 312 void FromTask() { |
307 if (task_count_ > 0) { | 313 if (task_count_ > 0) { |
308 --task_count_; | 314 --task_count_; |
309 } | 315 } |
310 if (task_count_ == 0 && event_count_ == 0) { | 316 if (task_count_ == 0 && event_count_ == 0) { |
311 MessageLoop::current()->Quit(); | 317 MessageLoop::current()->Quit(); |
312 } else { | 318 } else { |
313 MessageLoop::current()->PostTask( | 319 MessageLoop::current()->PostTask( |
314 FROM_HERE, NewRunnableMethod(this, &ConcurrentHelper::FromTask)); | 320 FROM_HERE, base::Bind(&ConcurrentHelper::FromTask, this)); |
315 } | 321 } |
316 } | 322 } |
317 | 323 |
318 void FromEvent() { | 324 void FromEvent() { |
319 if (event_count_ > 0) { | 325 if (event_count_ > 0) { |
320 --event_count_; | 326 --event_count_; |
321 } | 327 } |
322 if (task_count_ == 0 && event_count_ == 0) { | 328 if (task_count_ == 0 && event_count_ == 0) { |
323 MessageLoop::current()->Quit(); | 329 MessageLoop::current()->Quit(); |
324 } else { | 330 } else { |
325 injector_->AddEventAsTask( | 331 injector_->AddEventAsTask( |
326 0, NewRunnableMethod(this, &ConcurrentHelper::FromEvent)); | 332 0, base::Bind(&ConcurrentHelper::FromEvent, this)); |
327 } | 333 } |
328 } | 334 } |
329 | 335 |
330 int event_count() const { return event_count_; } | 336 int event_count() const { return event_count_; } |
331 int task_count() const { return task_count_; } | 337 int task_count() const { return task_count_; } |
332 | 338 |
333 private: | 339 private: |
334 friend class base::RefCounted<ConcurrentHelper>; | 340 friend class base::RefCounted<ConcurrentHelper>; |
335 | 341 |
336 ~ConcurrentHelper() {} | 342 ~ConcurrentHelper() {} |
(...skipping 12 matching lines...) Expand all Loading... |
349 // Tests that posted tasks don't starve events, nor the opposite. | 355 // Tests that posted tasks don't starve events, nor the opposite. |
350 // We use the helper class above. We keep both event and posted task queues | 356 // We use the helper class above. We keep both event and posted task queues |
351 // full, the helper verifies that both tasks and events get processed. | 357 // full, the helper verifies that both tasks and events get processed. |
352 // If that is not the case, either event_count_ or task_count_ will not get | 358 // If that is not the case, either event_count_ or task_count_ will not get |
353 // to 0, and MessageLoop::Quit() will never be called. | 359 // to 0, and MessageLoop::Quit() will never be called. |
354 scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector()); | 360 scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector()); |
355 | 361 |
356 // Add 2 events to the queue to make sure it is always full (when we remove | 362 // Add 2 events to the queue to make sure it is always full (when we remove |
357 // the event before processing it). | 363 // the event before processing it). |
358 injector()->AddEventAsTask( | 364 injector()->AddEventAsTask( |
359 0, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromEvent)); | 365 0, base::Bind(&ConcurrentHelper::FromEvent, helper.get())); |
360 injector()->AddEventAsTask( | 366 injector()->AddEventAsTask( |
361 0, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromEvent)); | 367 0, base::Bind(&ConcurrentHelper::FromEvent, helper.get())); |
362 | 368 |
363 // Similarly post 2 tasks. | 369 // Similarly post 2 tasks. |
364 loop()->PostTask( | 370 loop()->PostTask( |
365 FROM_HERE, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromTask)); | 371 FROM_HERE, base::Bind(&ConcurrentHelper::FromTask, helper.get())); |
366 loop()->PostTask( | 372 loop()->PostTask( |
367 FROM_HERE, NewRunnableMethod(helper.get(), &ConcurrentHelper::FromTask)); | 373 FROM_HERE, base::Bind(&ConcurrentHelper::FromTask, helper.get())); |
368 | 374 |
369 loop()->Run(); | 375 loop()->Run(); |
370 EXPECT_EQ(0, helper->event_count()); | 376 EXPECT_EQ(0, helper->event_count()); |
371 EXPECT_EQ(0, helper->task_count()); | 377 EXPECT_EQ(0, helper->task_count()); |
372 } | 378 } |
373 | 379 |
374 namespace { | 380 namespace { |
375 | 381 |
376 void AddEventsAndDrainGLib(EventInjector* injector) { | 382 void AddEventsAndDrainGLib(EventInjector* injector) { |
377 // Add a couple of dummy events | 383 // Add a couple of dummy events |
378 injector->AddDummyEvent(0); | 384 injector->AddDummyEvent(0); |
379 injector->AddDummyEvent(0); | 385 injector->AddDummyEvent(0); |
380 // Then add an event that will quit the main loop. | 386 // Then add an event that will quit the main loop. |
381 injector->AddEvent(0, MessageLoop::QuitClosure()); | 387 injector->AddEvent(0, MessageLoop::QuitClosure()); |
382 | 388 |
383 // Post a couple of dummy tasks | 389 // Post a couple of dummy tasks |
384 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); | 390 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DoNothing)); |
385 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); | 391 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DoNothing)); |
386 | 392 |
387 // Drain the events | 393 // Drain the events |
388 while (g_main_context_pending(NULL)) { | 394 while (g_main_context_pending(NULL)) { |
389 g_main_context_iteration(NULL, FALSE); | 395 g_main_context_iteration(NULL, FALSE); |
390 } | 396 } |
391 } | 397 } |
392 | 398 |
393 } // namespace | 399 } // namespace |
394 | 400 |
395 TEST_F(MessagePumpGLibTest, TestDrainingGLib) { | 401 TEST_F(MessagePumpGLibTest, TestDrainingGLib) { |
396 // Tests that draining events using GLib works. | 402 // Tests that draining events using GLib works. |
397 loop()->PostTask( | 403 loop()->PostTask( |
398 FROM_HERE, NewRunnableFunction(AddEventsAndDrainGLib, injector())); | 404 FROM_HERE, |
| 405 base::Bind(&AddEventsAndDrainGLib, base::Unretained(injector()))); |
399 loop()->Run(); | 406 loop()->Run(); |
400 | 407 |
401 EXPECT_EQ(3, injector()->processed_events()); | 408 EXPECT_EQ(3, injector()->processed_events()); |
402 } | 409 } |
403 | 410 |
404 | 411 |
405 namespace { | 412 namespace { |
406 | 413 |
407 #if defined(TOOLKIT_USES_GTK) | 414 #if defined(TOOLKIT_USES_GTK) |
408 void AddEventsAndDrainGtk(EventInjector* injector) { | 415 void AddEventsAndDrainGtk(EventInjector* injector) { |
409 // Add a couple of dummy events | 416 // Add a couple of dummy events |
410 injector->AddDummyEvent(0); | 417 injector->AddDummyEvent(0); |
411 injector->AddDummyEvent(0); | 418 injector->AddDummyEvent(0); |
412 // Then add an event that will quit the main loop. | 419 // Then add an event that will quit the main loop. |
413 injector->AddEvent(0, MessageLoop::QuitClosure()); | 420 injector->AddEvent(0, MessageLoop::QuitClosure()); |
414 | 421 |
415 // Post a couple of dummy tasks | 422 // Post a couple of dummy tasks |
416 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); | 423 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DoNothing)); |
417 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(DoNothing)); | 424 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DoNothing)); |
418 | 425 |
419 // Drain the events | 426 // Drain the events |
420 while (gtk_events_pending()) { | 427 while (gtk_events_pending()) { |
421 gtk_main_iteration(); | 428 gtk_main_iteration(); |
422 } | 429 } |
423 } | 430 } |
424 #endif | 431 #endif |
425 | 432 |
426 } // namespace | 433 } // namespace |
427 | 434 |
428 #if defined(TOOLKIT_USES_GTK) | 435 #if defined(TOOLKIT_USES_GTK) |
429 TEST_F(MessagePumpGLibTest, TestDrainingGtk) { | 436 TEST_F(MessagePumpGLibTest, TestDrainingGtk) { |
430 // Tests that draining events using Gtk works. | 437 // Tests that draining events using Gtk works. |
431 loop()->PostTask( | 438 loop()->PostTask( |
432 FROM_HERE, NewRunnableFunction(AddEventsAndDrainGtk, injector())); | 439 FROM_HERE, |
| 440 base::Bind(&AddEventsAndDrainGtk, base::Unretained(injector()))); |
433 loop()->Run(); | 441 loop()->Run(); |
434 | 442 |
435 EXPECT_EQ(3, injector()->processed_events()); | 443 EXPECT_EQ(3, injector()->processed_events()); |
436 } | 444 } |
437 #endif | 445 #endif |
438 | 446 |
439 namespace { | 447 namespace { |
440 | 448 |
441 // Helper class that lets us run the GLib message loop. | 449 // Helper class that lets us run the GLib message loop. |
442 class GLibLoopRunner : public base::RefCounted<GLibLoopRunner> { | 450 class GLibLoopRunner : public base::RefCounted<GLibLoopRunner> { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 // Allow tasks to be processed from 'native' event loops. | 489 // Allow tasks to be processed from 'native' event loops. |
482 MessageLoop::current()->SetNestableTasksAllowed(true); | 490 MessageLoop::current()->SetNestableTasksAllowed(true); |
483 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); | 491 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
484 | 492 |
485 int task_count = 0; | 493 int task_count = 0; |
486 // Add a couple of dummy events | 494 // Add a couple of dummy events |
487 injector->AddDummyEvent(0); | 495 injector->AddDummyEvent(0); |
488 injector->AddDummyEvent(0); | 496 injector->AddDummyEvent(0); |
489 // Post a couple of dummy tasks | 497 // Post a couple of dummy tasks |
490 MessageLoop::current()->PostTask( | 498 MessageLoop::current()->PostTask( |
491 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 499 FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
492 MessageLoop::current()->PostTask( | 500 MessageLoop::current()->PostTask( |
493 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 501 FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
494 // Delayed events | 502 // Delayed events |
495 injector->AddDummyEvent(10); | 503 injector->AddDummyEvent(10); |
496 injector->AddDummyEvent(10); | 504 injector->AddDummyEvent(10); |
497 // Delayed work | 505 // Delayed work |
498 MessageLoop::current()->PostDelayedTask( | 506 MessageLoop::current()->PostDelayedTask( |
499 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 30); | 507 FROM_HERE, base::Bind(&IncrementInt, &task_count), 30); |
500 MessageLoop::current()->PostDelayedTask( | 508 MessageLoop::current()->PostDelayedTask( |
501 FROM_HERE, NewRunnableMethod(runner.get(), &GLibLoopRunner::Quit), 40); | 509 FROM_HERE, base::Bind(&GLibLoopRunner::Quit, runner.get()), 40); |
502 | 510 |
503 // Run a nested, straight GLib message loop. | 511 // Run a nested, straight GLib message loop. |
504 runner->RunGLib(); | 512 runner->RunGLib(); |
505 | 513 |
506 ASSERT_EQ(3, task_count); | 514 ASSERT_EQ(3, task_count); |
507 EXPECT_EQ(4, injector->processed_events()); | 515 EXPECT_EQ(4, injector->processed_events()); |
508 MessageLoop::current()->Quit(); | 516 MessageLoop::current()->Quit(); |
509 } | 517 } |
510 | 518 |
511 void TestGtkLoopInternal(EventInjector* injector) { | 519 void TestGtkLoopInternal(EventInjector* injector) { |
512 // Allow tasks to be processed from 'native' event loops. | 520 // Allow tasks to be processed from 'native' event loops. |
513 MessageLoop::current()->SetNestableTasksAllowed(true); | 521 MessageLoop::current()->SetNestableTasksAllowed(true); |
514 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); | 522 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
515 | 523 |
516 int task_count = 0; | 524 int task_count = 0; |
517 // Add a couple of dummy events | 525 // Add a couple of dummy events |
518 injector->AddDummyEvent(0); | 526 injector->AddDummyEvent(0); |
519 injector->AddDummyEvent(0); | 527 injector->AddDummyEvent(0); |
520 // Post a couple of dummy tasks | 528 // Post a couple of dummy tasks |
521 MessageLoop::current()->PostTask( | 529 MessageLoop::current()->PostTask( |
522 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 530 FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
523 MessageLoop::current()->PostTask( | 531 MessageLoop::current()->PostTask( |
524 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count)); | 532 FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
525 // Delayed events | 533 // Delayed events |
526 injector->AddDummyEvent(10); | 534 injector->AddDummyEvent(10); |
527 injector->AddDummyEvent(10); | 535 injector->AddDummyEvent(10); |
528 // Delayed work | 536 // Delayed work |
529 MessageLoop::current()->PostDelayedTask( | 537 MessageLoop::current()->PostDelayedTask( |
530 FROM_HERE, NewRunnableFunction(IncrementInt, &task_count), 30); | 538 FROM_HERE, base::Bind(&IncrementInt, &task_count), 30); |
531 MessageLoop::current()->PostDelayedTask( | 539 MessageLoop::current()->PostDelayedTask( |
532 FROM_HERE, NewRunnableMethod(runner.get(), &GLibLoopRunner::Quit), 40); | 540 FROM_HERE, base::Bind(&GLibLoopRunner::Quit, runner.get()), 40); |
533 | 541 |
534 // Run a nested, straight Gtk message loop. | 542 // Run a nested, straight Gtk message loop. |
535 runner->RunLoop(); | 543 runner->RunLoop(); |
536 | 544 |
537 ASSERT_EQ(3, task_count); | 545 ASSERT_EQ(3, task_count); |
538 EXPECT_EQ(4, injector->processed_events()); | 546 EXPECT_EQ(4, injector->processed_events()); |
539 MessageLoop::current()->Quit(); | 547 MessageLoop::current()->Quit(); |
540 } | 548 } |
541 | 549 |
542 } // namespace | 550 } // namespace |
543 | 551 |
544 TEST_F(MessagePumpGLibTest, TestGLibLoop) { | 552 TEST_F(MessagePumpGLibTest, TestGLibLoop) { |
545 // Tests that events and posted tasks are correctly exectuted if the message | 553 // Tests that events and posted tasks are correctly executed if the message |
546 // loop is not run by MessageLoop::Run() but by a straight GLib loop. | 554 // loop is not run by MessageLoop::Run() but by a straight GLib loop. |
547 // Note that in this case we don't make strong guarantees about niceness | 555 // Note that in this case we don't make strong guarantees about niceness |
548 // between events and posted tasks. | 556 // between events and posted tasks. |
549 loop()->PostTask(FROM_HERE, | 557 loop()->PostTask( |
550 NewRunnableFunction(TestGLibLoopInternal, injector())); | 558 FROM_HERE, |
| 559 base::Bind(&TestGLibLoopInternal, base::Unretained(injector()))); |
551 loop()->Run(); | 560 loop()->Run(); |
552 } | 561 } |
553 | 562 |
554 TEST_F(MessagePumpGLibTest, TestGtkLoop) { | 563 TEST_F(MessagePumpGLibTest, TestGtkLoop) { |
555 // Tests that events and posted tasks are correctly exectuted if the message | 564 // Tests that events and posted tasks are correctly executed if the message |
556 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. | 565 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
557 // Note that in this case we don't make strong guarantees about niceness | 566 // Note that in this case we don't make strong guarantees about niceness |
558 // between events and posted tasks. | 567 // between events and posted tasks. |
559 loop()->PostTask(FROM_HERE, | 568 loop()->PostTask( |
560 NewRunnableFunction(TestGtkLoopInternal, injector())); | 569 FROM_HERE, |
| 570 base::Bind(&TestGtkLoopInternal, base::Unretained(injector()))); |
561 loop()->Run(); | 571 loop()->Run(); |
562 } | 572 } |
OLD | NEW |