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_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> |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 DISALLOW_COPY_AND_ASSIGN(EventInjector); | 134 DISALLOW_COPY_AND_ASSIGN(EventInjector); |
135 }; | 135 }; |
136 | 136 |
137 GSourceFuncs EventInjector::SourceFuncs = { | 137 GSourceFuncs EventInjector::SourceFuncs = { |
138 EventInjector::Prepare, | 138 EventInjector::Prepare, |
139 EventInjector::Check, | 139 EventInjector::Check, |
140 EventInjector::Dispatch, | 140 EventInjector::Dispatch, |
141 NULL | 141 NULL |
142 }; | 142 }; |
143 | 143 |
144 // Does nothing. This function can be called from a task. | |
145 void DoNothing() { | |
146 } | |
147 | |
148 void IncrementInt(int *value) { | 144 void IncrementInt(int *value) { |
149 ++*value; | 145 ++*value; |
150 } | 146 } |
151 | 147 |
152 // Checks how many events have been processed by the injector. | 148 // Checks how many events have been processed by the injector. |
153 void ExpectProcessedEvents(EventInjector* injector, int count) { | 149 void ExpectProcessedEvents(EventInjector* injector, int count) { |
154 EXPECT_EQ(injector->processed_events(), count); | 150 EXPECT_EQ(injector->processed_events(), count); |
155 } | 151 } |
156 | 152 |
157 // Posts a task on the current message loop. | 153 // Posts a task on the current message loop. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 } | 200 } |
205 | 201 |
206 TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { | 202 TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { |
207 // Checks that tasks posted by events are executed before the next event if | 203 // Checks that tasks posted by events are executed before the next event if |
208 // the posted task queue is empty. | 204 // the posted task queue is empty. |
209 // MessageLoop doesn't make strong guarantees that it is the case, but the | 205 // MessageLoop doesn't make strong guarantees that it is the case, but the |
210 // current implementation ensures it and the tests below rely on it. | 206 // current implementation ensures it and the tests below rely on it. |
211 // If changes cause this test to fail, it is reasonable to change it, but | 207 // If changes cause this test to fail, it is reasonable to change it, but |
212 // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be | 208 // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be |
213 // changed accordingly, otherwise they can become flaky. | 209 // changed accordingly, otherwise they can become flaky. |
214 injector()->AddEventAsTask(0, base::Bind(&DoNothing)); | 210 injector()->AddEventAsTask(0, base::Bind(&base::DoNothing)); |
215 base::Closure check_task = | 211 base::Closure check_task = |
216 base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 2); | 212 base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 2); |
217 base::Closure posted_task = | 213 base::Closure posted_task = |
218 base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); | 214 base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); |
219 injector()->AddEventAsTask(0, posted_task); | 215 injector()->AddEventAsTask(0, posted_task); |
220 injector()->AddEventAsTask(0, base::Bind(&DoNothing)); | 216 injector()->AddEventAsTask(0, base::Bind(&base::DoNothing)); |
221 injector()->AddEvent(0, MessageLoop::QuitClosure()); | 217 injector()->AddEvent(0, MessageLoop::QuitClosure()); |
222 loop()->Run(); | 218 loop()->Run(); |
223 EXPECT_EQ(4, injector()->processed_events()); | 219 EXPECT_EQ(4, injector()->processed_events()); |
224 | 220 |
225 injector()->Reset(); | 221 injector()->Reset(); |
226 injector()->AddEventAsTask(0, base::Bind(&DoNothing)); | 222 injector()->AddEventAsTask(0, base::Bind(&base::DoNothing)); |
227 check_task = | 223 check_task = |
228 base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 2); | 224 base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 2); |
229 posted_task = base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); | 225 posted_task = base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); |
230 injector()->AddEventAsTask(0, posted_task); | 226 injector()->AddEventAsTask(0, posted_task); |
231 injector()->AddEventAsTask(10, base::Bind(&DoNothing)); | 227 injector()->AddEventAsTask(10, base::Bind(&base::DoNothing)); |
232 injector()->AddEvent(0, MessageLoop::QuitClosure()); | 228 injector()->AddEvent(0, MessageLoop::QuitClosure()); |
233 loop()->Run(); | 229 loop()->Run(); |
234 EXPECT_EQ(4, injector()->processed_events()); | 230 EXPECT_EQ(4, injector()->processed_events()); |
235 } | 231 } |
236 | 232 |
237 TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { | 233 TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { |
238 int task_count = 0; | 234 int task_count = 0; |
239 // Tests that we process tasks while waiting for new events. | 235 // Tests that we process tasks while waiting for new events. |
240 // The event queue is empty at first. | 236 // The event queue is empty at first. |
241 for (int i = 0; i < 10; ++i) { | 237 for (int i = 0; i < 10; ++i) { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 namespace { | 373 namespace { |
378 | 374 |
379 void AddEventsAndDrainGLib(EventInjector* injector) { | 375 void AddEventsAndDrainGLib(EventInjector* injector) { |
380 // Add a couple of dummy events | 376 // Add a couple of dummy events |
381 injector->AddDummyEvent(0); | 377 injector->AddDummyEvent(0); |
382 injector->AddDummyEvent(0); | 378 injector->AddDummyEvent(0); |
383 // Then add an event that will quit the main loop. | 379 // Then add an event that will quit the main loop. |
384 injector->AddEvent(0, MessageLoop::QuitClosure()); | 380 injector->AddEvent(0, MessageLoop::QuitClosure()); |
385 | 381 |
386 // Post a couple of dummy tasks | 382 // Post a couple of dummy tasks |
387 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DoNothing)); | 383 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&base::DoNothing)); |
388 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DoNothing)); | 384 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&base::DoNothing)); |
389 | 385 |
390 // Drain the events | 386 // Drain the events |
391 while (g_main_context_pending(NULL)) { | 387 while (g_main_context_pending(NULL)) { |
392 g_main_context_iteration(NULL, FALSE); | 388 g_main_context_iteration(NULL, FALSE); |
393 } | 389 } |
394 } | 390 } |
395 | 391 |
396 } // namespace | 392 } // namespace |
397 | 393 |
398 TEST_F(MessagePumpGLibTest, TestDrainingGLib) { | 394 TEST_F(MessagePumpGLibTest, TestDrainingGLib) { |
(...skipping 11 matching lines...) Expand all Loading... |
410 | 406 |
411 #if defined(TOOLKIT_USES_GTK) | 407 #if defined(TOOLKIT_USES_GTK) |
412 void AddEventsAndDrainGtk(EventInjector* injector) { | 408 void AddEventsAndDrainGtk(EventInjector* injector) { |
413 // Add a couple of dummy events | 409 // Add a couple of dummy events |
414 injector->AddDummyEvent(0); | 410 injector->AddDummyEvent(0); |
415 injector->AddDummyEvent(0); | 411 injector->AddDummyEvent(0); |
416 // Then add an event that will quit the main loop. | 412 // Then add an event that will quit the main loop. |
417 injector->AddEvent(0, MessageLoop::QuitClosure()); | 413 injector->AddEvent(0, MessageLoop::QuitClosure()); |
418 | 414 |
419 // Post a couple of dummy tasks | 415 // Post a couple of dummy tasks |
420 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DoNothing)); | 416 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&base::DoNothing)); |
421 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&DoNothing)); | 417 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&base::DoNothing)); |
422 | 418 |
423 // Drain the events | 419 // Drain the events |
424 while (gtk_events_pending()) { | 420 while (gtk_events_pending()) { |
425 gtk_main_iteration(); | 421 gtk_main_iteration(); |
426 } | 422 } |
427 } | 423 } |
428 #endif | 424 #endif |
429 | 425 |
430 } // namespace | 426 } // namespace |
431 | 427 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 TEST_F(MessagePumpGLibTest, TestGtkLoop) { | 556 TEST_F(MessagePumpGLibTest, TestGtkLoop) { |
561 // Tests that events and posted tasks are correctly executed if the message | 557 // Tests that events and posted tasks are correctly executed if the message |
562 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. | 558 // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
563 // Note that in this case we don't make strong guarantees about niceness | 559 // Note that in this case we don't make strong guarantees about niceness |
564 // between events and posted tasks. | 560 // between events and posted tasks. |
565 loop()->PostTask( | 561 loop()->PostTask( |
566 FROM_HERE, | 562 FROM_HERE, |
567 base::Bind(&TestGtkLoopInternal, base::Unretained(injector()))); | 563 base::Bind(&TestGtkLoopInternal, base::Unretained(injector()))); |
568 loop()->Run(); | 564 loop()->Run(); |
569 } | 565 } |
OLD | NEW |