OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_loop/message_pump_glib.h" | 5 #include "base/message_loop/message_pump_glib.h" |
6 | 6 |
7 #include <glib.h> | 7 #include <glib.h> |
8 #include <math.h> | 8 #include <math.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
19 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 namespace base { | 24 namespace base { |
24 namespace { | 25 namespace { |
25 | 26 |
26 // This class injects dummy "events" into the GLib loop. When "handled" these | 27 // This class injects dummy "events" into the GLib loop. When "handled" these |
27 // events can run tasks. This is intended to mock gtk events (the corresponding | 28 // events can run tasks. This is intended to mock gtk events (the corresponding |
28 // GLib source runs at the same priority). | 29 // GLib source runs at the same priority). |
29 class EventInjector { | 30 class EventInjector { |
30 public: | 31 public: |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 147 } |
147 | 148 |
148 // Checks how many events have been processed by the injector. | 149 // Checks how many events have been processed by the injector. |
149 void ExpectProcessedEvents(EventInjector* injector, int count) { | 150 void ExpectProcessedEvents(EventInjector* injector, int count) { |
150 EXPECT_EQ(injector->processed_events(), count); | 151 EXPECT_EQ(injector->processed_events(), count); |
151 } | 152 } |
152 | 153 |
153 // Posts a task on the current message loop. | 154 // Posts a task on the current message loop. |
154 void PostMessageLoopTask(const tracked_objects::Location& from_here, | 155 void PostMessageLoopTask(const tracked_objects::Location& from_here, |
155 const Closure& task) { | 156 const Closure& task) { |
156 MessageLoop::current()->PostTask(from_here, task); | 157 ThreadTaskRunnerHandle::Get()->PostTask(from_here, task); |
157 } | 158 } |
158 | 159 |
159 // Test fixture. | 160 // Test fixture. |
160 class MessagePumpGLibTest : public testing::Test { | 161 class MessagePumpGLibTest : public testing::Test { |
161 public: | 162 public: |
162 MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { } | 163 MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { } |
163 | 164 |
164 // Overridden from testing::Test: | 165 // Overridden from testing::Test: |
165 void SetUp() override { | 166 void SetUp() override { |
166 loop_ = new MessageLoop(MessageLoop::TYPE_UI); | 167 loop_ = new MessageLoop(MessageLoop::TYPE_UI); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 task_count_(kStartingTaskCount) { | 305 task_count_(kStartingTaskCount) { |
305 } | 306 } |
306 | 307 |
307 void FromTask() { | 308 void FromTask() { |
308 if (task_count_ > 0) { | 309 if (task_count_ > 0) { |
309 --task_count_; | 310 --task_count_; |
310 } | 311 } |
311 if (task_count_ == 0 && event_count_ == 0) { | 312 if (task_count_ == 0 && event_count_ == 0) { |
312 MessageLoop::current()->QuitWhenIdle(); | 313 MessageLoop::current()->QuitWhenIdle(); |
313 } else { | 314 } else { |
314 MessageLoop::current()->PostTask( | 315 ThreadTaskRunnerHandle::Get()->PostTask( |
315 FROM_HERE, Bind(&ConcurrentHelper::FromTask, this)); | 316 FROM_HERE, Bind(&ConcurrentHelper::FromTask, this)); |
316 } | 317 } |
317 } | 318 } |
318 | 319 |
319 void FromEvent() { | 320 void FromEvent() { |
320 if (event_count_ > 0) { | 321 if (event_count_ > 0) { |
321 --event_count_; | 322 --event_count_; |
322 } | 323 } |
323 if (task_count_ == 0 && event_count_ == 0) { | 324 if (task_count_ == 0 && event_count_ == 0) { |
324 MessageLoop::current()->QuitWhenIdle(); | 325 MessageLoop::current()->QuitWhenIdle(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 namespace { | 376 namespace { |
376 | 377 |
377 void AddEventsAndDrainGLib(EventInjector* injector) { | 378 void AddEventsAndDrainGLib(EventInjector* injector) { |
378 // Add a couple of dummy events | 379 // Add a couple of dummy events |
379 injector->AddDummyEvent(0); | 380 injector->AddDummyEvent(0); |
380 injector->AddDummyEvent(0); | 381 injector->AddDummyEvent(0); |
381 // Then add an event that will quit the main loop. | 382 // Then add an event that will quit the main loop. |
382 injector->AddEvent(0, MessageLoop::QuitWhenIdleClosure()); | 383 injector->AddEvent(0, MessageLoop::QuitWhenIdleClosure()); |
383 | 384 |
384 // Post a couple of dummy tasks | 385 // Post a couple of dummy tasks |
385 MessageLoop::current()->PostTask(FROM_HERE, Bind(&DoNothing)); | 386 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(&DoNothing)); |
386 MessageLoop::current()->PostTask(FROM_HERE, Bind(&DoNothing)); | 387 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, Bind(&DoNothing)); |
387 | 388 |
388 // Drain the events | 389 // Drain the events |
389 while (g_main_context_pending(NULL)) { | 390 while (g_main_context_pending(NULL)) { |
390 g_main_context_iteration(NULL, FALSE); | 391 g_main_context_iteration(NULL, FALSE); |
391 } | 392 } |
392 } | 393 } |
393 | 394 |
394 } // namespace | 395 } // namespace |
395 | 396 |
396 TEST_F(MessagePumpGLibTest, TestDrainingGLib) { | 397 TEST_F(MessagePumpGLibTest, TestDrainingGLib) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 void TestGLibLoopInternal(EventInjector* injector) { | 442 void TestGLibLoopInternal(EventInjector* injector) { |
442 // Allow tasks to be processed from 'native' event loops. | 443 // Allow tasks to be processed from 'native' event loops. |
443 MessageLoop::current()->SetNestableTasksAllowed(true); | 444 MessageLoop::current()->SetNestableTasksAllowed(true); |
444 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); | 445 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
445 | 446 |
446 int task_count = 0; | 447 int task_count = 0; |
447 // Add a couple of dummy events | 448 // Add a couple of dummy events |
448 injector->AddDummyEvent(0); | 449 injector->AddDummyEvent(0); |
449 injector->AddDummyEvent(0); | 450 injector->AddDummyEvent(0); |
450 // Post a couple of dummy tasks | 451 // Post a couple of dummy tasks |
451 MessageLoop::current()->PostTask( | 452 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
452 FROM_HERE, Bind(&IncrementInt, &task_count)); | 453 Bind(&IncrementInt, &task_count)); |
453 MessageLoop::current()->PostTask( | 454 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
454 FROM_HERE, Bind(&IncrementInt, &task_count)); | 455 Bind(&IncrementInt, &task_count)); |
455 // Delayed events | 456 // Delayed events |
456 injector->AddDummyEvent(10); | 457 injector->AddDummyEvent(10); |
457 injector->AddDummyEvent(10); | 458 injector->AddDummyEvent(10); |
458 // Delayed work | 459 // Delayed work |
459 MessageLoop::current()->PostDelayedTask( | 460 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
460 FROM_HERE, | 461 FROM_HERE, Bind(&IncrementInt, &task_count), |
461 Bind(&IncrementInt, &task_count), | |
462 TimeDelta::FromMilliseconds(30)); | 462 TimeDelta::FromMilliseconds(30)); |
463 MessageLoop::current()->PostDelayedTask( | 463 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
464 FROM_HERE, | 464 FROM_HERE, Bind(&GLibLoopRunner::Quit, runner.get()), |
465 Bind(&GLibLoopRunner::Quit, runner.get()), | |
466 TimeDelta::FromMilliseconds(40)); | 465 TimeDelta::FromMilliseconds(40)); |
467 | 466 |
468 // Run a nested, straight GLib message loop. | 467 // Run a nested, straight GLib message loop. |
469 runner->RunGLib(); | 468 runner->RunGLib(); |
470 | 469 |
471 ASSERT_EQ(3, task_count); | 470 ASSERT_EQ(3, task_count); |
472 EXPECT_EQ(4, injector->processed_events()); | 471 EXPECT_EQ(4, injector->processed_events()); |
473 MessageLoop::current()->QuitWhenIdle(); | 472 MessageLoop::current()->QuitWhenIdle(); |
474 } | 473 } |
475 | 474 |
476 void TestGtkLoopInternal(EventInjector* injector) { | 475 void TestGtkLoopInternal(EventInjector* injector) { |
477 // Allow tasks to be processed from 'native' event loops. | 476 // Allow tasks to be processed from 'native' event loops. |
478 MessageLoop::current()->SetNestableTasksAllowed(true); | 477 MessageLoop::current()->SetNestableTasksAllowed(true); |
479 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); | 478 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
480 | 479 |
481 int task_count = 0; | 480 int task_count = 0; |
482 // Add a couple of dummy events | 481 // Add a couple of dummy events |
483 injector->AddDummyEvent(0); | 482 injector->AddDummyEvent(0); |
484 injector->AddDummyEvent(0); | 483 injector->AddDummyEvent(0); |
485 // Post a couple of dummy tasks | 484 // Post a couple of dummy tasks |
486 MessageLoop::current()->PostTask( | 485 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
487 FROM_HERE, Bind(&IncrementInt, &task_count)); | 486 Bind(&IncrementInt, &task_count)); |
488 MessageLoop::current()->PostTask( | 487 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
489 FROM_HERE, Bind(&IncrementInt, &task_count)); | 488 Bind(&IncrementInt, &task_count)); |
490 // Delayed events | 489 // Delayed events |
491 injector->AddDummyEvent(10); | 490 injector->AddDummyEvent(10); |
492 injector->AddDummyEvent(10); | 491 injector->AddDummyEvent(10); |
493 // Delayed work | 492 // Delayed work |
494 MessageLoop::current()->PostDelayedTask( | 493 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
495 FROM_HERE, | 494 FROM_HERE, Bind(&IncrementInt, &task_count), |
496 Bind(&IncrementInt, &task_count), | |
497 TimeDelta::FromMilliseconds(30)); | 495 TimeDelta::FromMilliseconds(30)); |
498 MessageLoop::current()->PostDelayedTask( | 496 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
499 FROM_HERE, | 497 FROM_HERE, Bind(&GLibLoopRunner::Quit, runner.get()), |
500 Bind(&GLibLoopRunner::Quit, runner.get()), | |
501 TimeDelta::FromMilliseconds(40)); | 498 TimeDelta::FromMilliseconds(40)); |
502 | 499 |
503 // Run a nested, straight Gtk message loop. | 500 // Run a nested, straight Gtk message loop. |
504 runner->RunLoop(); | 501 runner->RunLoop(); |
505 | 502 |
506 ASSERT_EQ(3, task_count); | 503 ASSERT_EQ(3, task_count); |
507 EXPECT_EQ(4, injector->processed_events()); | 504 EXPECT_EQ(4, injector->processed_events()); |
508 MessageLoop::current()->QuitWhenIdle(); | 505 MessageLoop::current()->QuitWhenIdle(); |
509 } | 506 } |
510 | 507 |
(...skipping 15 matching lines...) Expand all Loading... |
526 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. | 523 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
527 // Note that in this case we don't make strong guarantees about niceness | 524 // Note that in this case we don't make strong guarantees about niceness |
528 // between events and posted tasks. | 525 // between events and posted tasks. |
529 loop()->PostTask( | 526 loop()->PostTask( |
530 FROM_HERE, | 527 FROM_HERE, |
531 Bind(&TestGtkLoopInternal, Unretained(injector()))); | 528 Bind(&TestGtkLoopInternal, Unretained(injector()))); |
532 loop()->Run(); | 529 loop()->Run(); |
533 } | 530 } |
534 | 531 |
535 } // namespace base | 532 } // namespace base |
OLD | NEW |