| 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> |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 // Tests that posted tasks don't starve events, nor the opposite. | 350 // Tests that posted tasks don't starve events, nor the opposite. |
| 351 // We use the helper class above. We keep both event and posted task queues | 351 // We use the helper class above. We keep both event and posted task queues |
| 352 // full, the helper verifies that both tasks and events get processed. | 352 // full, the helper verifies that both tasks and events get processed. |
| 353 // If that is not the case, either event_count_ or task_count_ will not get | 353 // If that is not the case, either event_count_ or task_count_ will not get |
| 354 // to 0, and MessageLoop::QuitWhenIdle() will never be called. | 354 // to 0, and MessageLoop::QuitWhenIdle() will never be called. |
| 355 scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector()); | 355 scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector()); |
| 356 | 356 |
| 357 // Add 2 events to the queue to make sure it is always full (when we remove | 357 // Add 2 events to the queue to make sure it is always full (when we remove |
| 358 // the event before processing it). | 358 // the event before processing it). |
| 359 injector()->AddEventAsTask( | 359 injector()->AddEventAsTask( |
| 360 0, Bind(&ConcurrentHelper::FromEvent, helper.get())); | 360 0, Bind(&ConcurrentHelper::FromEvent, helper)); |
| 361 injector()->AddEventAsTask( | 361 injector()->AddEventAsTask( |
| 362 0, Bind(&ConcurrentHelper::FromEvent, helper.get())); | 362 0, Bind(&ConcurrentHelper::FromEvent, helper)); |
| 363 | 363 |
| 364 // Similarly post 2 tasks. | 364 // Similarly post 2 tasks. |
| 365 loop()->task_runner()->PostTask( | 365 loop()->task_runner()->PostTask( |
| 366 FROM_HERE, Bind(&ConcurrentHelper::FromTask, helper.get())); | 366 FROM_HERE, Bind(&ConcurrentHelper::FromTask, helper)); |
| 367 loop()->task_runner()->PostTask( | 367 loop()->task_runner()->PostTask( |
| 368 FROM_HERE, Bind(&ConcurrentHelper::FromTask, helper.get())); | 368 FROM_HERE, Bind(&ConcurrentHelper::FromTask, helper)); |
| 369 | 369 |
| 370 RunLoop().Run(); | 370 RunLoop().Run(); |
| 371 EXPECT_EQ(0, helper->event_count()); | 371 EXPECT_EQ(0, helper->event_count()); |
| 372 EXPECT_EQ(0, helper->task_count()); | 372 EXPECT_EQ(0, helper->task_count()); |
| 373 } | 373 } |
| 374 | 374 |
| 375 namespace { | 375 namespace { |
| 376 | 376 |
| 377 void AddEventsAndDrainGLib(EventInjector* injector) { | 377 void AddEventsAndDrainGLib(EventInjector* injector) { |
| 378 // Add a couple of dummy events | 378 // Add a couple of dummy events |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 452 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 453 Bind(&IncrementInt, &task_count)); | 453 Bind(&IncrementInt, &task_count)); |
| 454 // Delayed events | 454 // Delayed events |
| 455 injector->AddDummyEvent(10); | 455 injector->AddDummyEvent(10); |
| 456 injector->AddDummyEvent(10); | 456 injector->AddDummyEvent(10); |
| 457 // Delayed work | 457 // Delayed work |
| 458 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 458 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 459 FROM_HERE, Bind(&IncrementInt, &task_count), | 459 FROM_HERE, Bind(&IncrementInt, &task_count), |
| 460 TimeDelta::FromMilliseconds(30)); | 460 TimeDelta::FromMilliseconds(30)); |
| 461 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 461 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 462 FROM_HERE, Bind(&GLibLoopRunner::Quit, runner.get()), | 462 FROM_HERE, Bind(&GLibLoopRunner::Quit, runner), |
| 463 TimeDelta::FromMilliseconds(40)); | 463 TimeDelta::FromMilliseconds(40)); |
| 464 | 464 |
| 465 // Run a nested, straight GLib message loop. | 465 // Run a nested, straight GLib message loop. |
| 466 runner->RunGLib(); | 466 runner->RunGLib(); |
| 467 | 467 |
| 468 ASSERT_EQ(3, task_count); | 468 ASSERT_EQ(3, task_count); |
| 469 EXPECT_EQ(4, injector->processed_events()); | 469 EXPECT_EQ(4, injector->processed_events()); |
| 470 MessageLoop::current()->QuitWhenIdle(); | 470 MessageLoop::current()->QuitWhenIdle(); |
| 471 } | 471 } |
| 472 | 472 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 485 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 485 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 486 Bind(&IncrementInt, &task_count)); | 486 Bind(&IncrementInt, &task_count)); |
| 487 // Delayed events | 487 // Delayed events |
| 488 injector->AddDummyEvent(10); | 488 injector->AddDummyEvent(10); |
| 489 injector->AddDummyEvent(10); | 489 injector->AddDummyEvent(10); |
| 490 // Delayed work | 490 // Delayed work |
| 491 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 491 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 492 FROM_HERE, Bind(&IncrementInt, &task_count), | 492 FROM_HERE, Bind(&IncrementInt, &task_count), |
| 493 TimeDelta::FromMilliseconds(30)); | 493 TimeDelta::FromMilliseconds(30)); |
| 494 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 494 ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 495 FROM_HERE, Bind(&GLibLoopRunner::Quit, runner.get()), | 495 FROM_HERE, Bind(&GLibLoopRunner::Quit, runner), |
| 496 TimeDelta::FromMilliseconds(40)); | 496 TimeDelta::FromMilliseconds(40)); |
| 497 | 497 |
| 498 // Run a nested, straight Gtk message loop. | 498 // Run a nested, straight Gtk message loop. |
| 499 runner->RunLoop(); | 499 runner->RunLoop(); |
| 500 | 500 |
| 501 ASSERT_EQ(3, task_count); | 501 ASSERT_EQ(3, task_count); |
| 502 EXPECT_EQ(4, injector->processed_events()); | 502 EXPECT_EQ(4, injector->processed_events()); |
| 503 MessageLoop::current()->QuitWhenIdle(); | 503 MessageLoop::current()->QuitWhenIdle(); |
| 504 } | 504 } |
| 505 | 505 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 519 // Tests that events and posted tasks are correctly executed if the message | 519 // Tests that events and posted tasks are correctly executed if the message |
| 520 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. | 520 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
| 521 // Note that in this case we don't make strong guarantees about niceness | 521 // Note that in this case we don't make strong guarantees about niceness |
| 522 // between events and posted tasks. | 522 // between events and posted tasks. |
| 523 loop()->task_runner()->PostTask( | 523 loop()->task_runner()->PostTask( |
| 524 FROM_HERE, Bind(&TestGtkLoopInternal, Unretained(injector()))); | 524 FROM_HERE, Bind(&TestGtkLoopInternal, Unretained(injector()))); |
| 525 RunLoop().Run(); | 525 RunLoop().Run(); |
| 526 } | 526 } |
| 527 | 527 |
| 528 } // namespace base | 528 } // namespace base |
| OLD | NEW |