blob: 564de7d83f549eb09dfa186989b6096907acb1d1 [file] [log] [blame]
[email protected]ac4c6682012-01-04 00:57:391// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b44d5cc2009-06-15 10:30:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]59e69e742013-06-18 20:27:525#include "base/message_loop/message_pump_glib.h"
[email protected]b44d5cc2009-06-15 10:30:446
[email protected]d90efd52012-06-28 19:57:267#include <glib.h>
[email protected]23d35392009-06-15 19:53:088#include <math.h>
9
[email protected]b44d5cc2009-06-15 10:30:4410#include <algorithm>
11#include <vector>
[email protected]23d35392009-06-15 19:53:0812
[email protected]8f5a7e492012-01-01 02:14:4713#include "base/bind.h"
14#include "base/bind_helpers.h"
[email protected]763839e2011-12-09 23:06:0215#include "base/callback.h"
avi9b6f42932015-12-26 22:15:1416#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/ref_counted.h"
[email protected]495cad92013-07-18 08:12:4018#include "base/message_loop/message_loop.h"
[email protected]7ff48ca2013-02-06 16:56:1919#include "base/run_loop.h"
fdoray6ef45cf2016-08-25 15:36:3720#include "base/single_thread_task_runner.h"
[email protected]34b99632011-01-01 01:01:0621#include "base/threading/thread.h"
fdoray2df4a9e2016-07-18 23:47:1622#include "base/threading/thread_task_runner_handle.h"
[email protected]b44d5cc2009-06-15 10:30:4423#include "testing/gtest/include/gtest/gtest.h"
24
[email protected]7ff48ca2013-02-06 16:56:1925namespace base {
[email protected]b44d5cc2009-06-15 10:30:4426namespace {
27
28// This class injects dummy "events" into the GLib loop. When "handled" these
29// events can run tasks. This is intended to mock gtk events (the corresponding
30// GLib source runs at the same priority).
31class EventInjector {
32 public:
33 EventInjector() : processed_events_(0) {
34 source_ = static_cast<Source*>(g_source_new(&SourceFuncs, sizeof(Source)));
35 source_->injector = this;
36 g_source_attach(source_, NULL);
37 g_source_set_can_recurse(source_, TRUE);
38 }
39
40 ~EventInjector() {
41 g_source_destroy(source_);
42 g_source_unref(source_);
43 }
44
45 int HandlePrepare() {
46 // If the queue is empty, block.
47 if (events_.empty())
48 return -1;
[email protected]59e69e742013-06-18 20:27:5249 TimeDelta delta = events_[0].time - Time::NowFromSystemTime();
[email protected]b44d5cc2009-06-15 10:30:4450 return std::max(0, static_cast<int>(ceil(delta.InMillisecondsF())));
51 }
52
53 bool HandleCheck() {
54 if (events_.empty())
55 return false;
[email protected]59e69e742013-06-18 20:27:5256 return events_[0].time <= Time::NowFromSystemTime();
[email protected]b44d5cc2009-06-15 10:30:4457 }
58
59 void HandleDispatch() {
60 if (events_.empty())
61 return;
tzika8cc2202017-04-18 07:01:1562 Event event = std::move(events_[0]);
[email protected]b44d5cc2009-06-15 10:30:4463 events_.erase(events_.begin());
64 ++processed_events_;
[email protected]8f5a7e492012-01-01 02:14:4765 if (!event.callback.is_null())
tzika8cc2202017-04-18 07:01:1566 std::move(event.callback).Run();
[email protected]8f5a7e492012-01-01 02:14:4767 else if (!event.task.is_null())
tzika8cc2202017-04-18 07:01:1568 std::move(event.task).Run();
[email protected]b44d5cc2009-06-15 10:30:4469 }
70
[email protected]763839e2011-12-09 23:06:0271 // Adds an event to the queue. When "handled", executes |callback|.
[email protected]b44d5cc2009-06-15 10:30:4472 // delay_ms is relative to the last event if any, or to Now() otherwise.
tzika8cc2202017-04-18 07:01:1573 void AddEvent(int delay_ms, OnceClosure callback) {
74 AddEventHelper(delay_ms, std::move(callback), OnceClosure());
[email protected]763839e2011-12-09 23:06:0275 }
76
77 void AddDummyEvent(int delay_ms) {
tzika8cc2202017-04-18 07:01:1578 AddEventHelper(delay_ms, OnceClosure(), OnceClosure());
[email protected]763839e2011-12-09 23:06:0279 }
80
tzika8cc2202017-04-18 07:01:1581 void AddEventAsTask(int delay_ms, OnceClosure task) {
82 AddEventHelper(delay_ms, OnceClosure(), std::move(task));
[email protected]b44d5cc2009-06-15 10:30:4483 }
84
85 void Reset() {
86 processed_events_ = 0;
87 events_.clear();
88 }
89
90 int processed_events() const { return processed_events_; }
91
92 private:
93 struct Event {
[email protected]59e69e742013-06-18 20:27:5294 Time time;
tzika8cc2202017-04-18 07:01:1595 OnceClosure callback;
96 OnceClosure task;
[email protected]b44d5cc2009-06-15 10:30:4497 };
98
99 struct Source : public GSource {
100 EventInjector* injector;
101 };
102
tzika8cc2202017-04-18 07:01:15103 void AddEventHelper(int delay_ms, OnceClosure callback, OnceClosure task) {
[email protected]59e69e742013-06-18 20:27:52104 Time last_time;
[email protected]8f5a7e492012-01-01 02:14:47105 if (!events_.empty())
[email protected]763839e2011-12-09 23:06:02106 last_time = (events_.end()-1)->time;
[email protected]8f5a7e492012-01-01 02:14:47107 else
[email protected]59e69e742013-06-18 20:27:52108 last_time = Time::NowFromSystemTime();
[email protected]8f5a7e492012-01-01 02:14:47109
[email protected]59e69e742013-06-18 20:27:52110 Time future = last_time + TimeDelta::FromMilliseconds(delay_ms);
tzika8cc2202017-04-18 07:01:15111 EventInjector::Event event = {future, std::move(callback), std::move(task)};
112 events_.push_back(std::move(event));
[email protected]763839e2011-12-09 23:06:02113 }
114
[email protected]b44d5cc2009-06-15 10:30:44115 static gboolean Prepare(GSource* source, gint* timeout_ms) {
116 *timeout_ms = static_cast<Source*>(source)->injector->HandlePrepare();
117 return FALSE;
118 }
119
120 static gboolean Check(GSource* source) {
121 return static_cast<Source*>(source)->injector->HandleCheck();
122 }
123
124 static gboolean Dispatch(GSource* source,
125 GSourceFunc unused_func,
126 gpointer unused_data) {
127 static_cast<Source*>(source)->injector->HandleDispatch();
128 return TRUE;
129 }
130
131 Source* source_;
132 std::vector<Event> events_;
133 int processed_events_;
134 static GSourceFuncs SourceFuncs;
135 DISALLOW_COPY_AND_ASSIGN(EventInjector);
136};
137
138GSourceFuncs EventInjector::SourceFuncs = {
139 EventInjector::Prepare,
140 EventInjector::Check,
141 EventInjector::Dispatch,
142 NULL
143};
144
[email protected]b44d5cc2009-06-15 10:30:44145void IncrementInt(int *value) {
146 ++*value;
147}
148
149// Checks how many events have been processed by the injector.
150void ExpectProcessedEvents(EventInjector* injector, int count) {
151 EXPECT_EQ(injector->processed_events(), count);
152}
153
[email protected]b44d5cc2009-06-15 10:30:44154// Posts a task on the current message loop.
155void PostMessageLoopTask(const tracked_objects::Location& from_here,
tzika8cc2202017-04-18 07:01:15156 OnceClosure task) {
157 ThreadTaskRunnerHandle::Get()->PostTask(from_here, std::move(task));
[email protected]b44d5cc2009-06-15 10:30:44158}
159
160// Test fixture.
161class MessagePumpGLibTest : public testing::Test {
162 public:
163 MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { }
164
[email protected]c19003d2012-07-31 01:38:18165 // Overridden from testing::Test:
dcheng8aef37612014-12-23 02:56:47166 void SetUp() override {
[email protected]b44d5cc2009-06-15 10:30:44167 loop_ = new MessageLoop(MessageLoop::TYPE_UI);
168 injector_ = new EventInjector();
169 }
dcheng8aef37612014-12-23 02:56:47170 void TearDown() override {
[email protected]b44d5cc2009-06-15 10:30:44171 delete injector_;
172 injector_ = NULL;
173 delete loop_;
174 loop_ = NULL;
175 }
176
177 MessageLoop* loop() const { return loop_; }
178 EventInjector* injector() const { return injector_; }
179
180 private:
181 MessageLoop* loop_;
182 EventInjector* injector_;
183 DISALLOW_COPY_AND_ASSIGN(MessagePumpGLibTest);
184};
185
186} // namespace
187
[email protected]b44d5cc2009-06-15 10:30:44188TEST_F(MessagePumpGLibTest, TestQuit) {
189 // Checks that Quit works and that the basic infrastructure is working.
190
191 // Quit from a task
[email protected]7ff48ca2013-02-06 16:56:19192 RunLoop().RunUntilIdle();
[email protected]b44d5cc2009-06-15 10:30:44193 EXPECT_EQ(0, injector()->processed_events());
194
195 injector()->Reset();
196 // Quit from an event
[email protected]a085953f2013-02-04 23:40:00197 injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
fdoray6ef45cf2016-08-25 15:36:37198 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44199 EXPECT_EQ(1, injector()->processed_events());
200}
201
202TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) {
203 // Checks that tasks posted by events are executed before the next event if
204 // the posted task queue is empty.
205 // 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.
207 // If changes cause this test to fail, it is reasonable to change it, but
208 // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be
209 // changed accordingly, otherwise they can become flaky.
tzika8cc2202017-04-18 07:01:15210 injector()->AddEventAsTask(0, BindOnce(&DoNothing));
211 OnceClosure check_task =
212 BindOnce(&ExpectProcessedEvents, Unretained(injector()), 2);
213 OnceClosure posted_task =
214 BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task));
215 injector()->AddEventAsTask(0, std::move(posted_task));
216 injector()->AddEventAsTask(0, BindOnce(&DoNothing));
[email protected]a085953f2013-02-04 23:40:00217 injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
fdoray6ef45cf2016-08-25 15:36:37218 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44219 EXPECT_EQ(4, injector()->processed_events());
220
221 injector()->Reset();
tzika8cc2202017-04-18 07:01:15222 injector()->AddEventAsTask(0, BindOnce(&DoNothing));
223 check_task = BindOnce(&ExpectProcessedEvents, Unretained(injector()), 2);
224 posted_task =
225 BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task));
226 injector()->AddEventAsTask(0, std::move(posted_task));
227 injector()->AddEventAsTask(10, BindOnce(&DoNothing));
[email protected]a085953f2013-02-04 23:40:00228 injector()->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
fdoray6ef45cf2016-08-25 15:36:37229 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44230 EXPECT_EQ(4, injector()->processed_events());
231}
232
233TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) {
234 int task_count = 0;
235 // Tests that we process tasks while waiting for new events.
236 // The event queue is empty at first.
237 for (int i = 0; i < 10; ++i) {
fdoray6ef45cf2016-08-25 15:36:37238 loop()->task_runner()->PostTask(FROM_HERE,
tzik92b7a422017-04-11 15:00:44239 BindOnce(&IncrementInt, &task_count));
[email protected]b44d5cc2009-06-15 10:30:44240 }
241 // After all the previous tasks have executed, enqueue an event that will
242 // quit.
fdoray6ef45cf2016-08-25 15:36:37243 loop()->task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44244 FROM_HERE, BindOnce(&EventInjector::AddEvent, Unretained(injector()), 0,
245 MessageLoop::QuitWhenIdleClosure()));
fdoray6ef45cf2016-08-25 15:36:37246 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44247 ASSERT_EQ(10, task_count);
248 EXPECT_EQ(1, injector()->processed_events());
249
250 // Tests that we process delayed tasks while waiting for new events.
251 injector()->Reset();
252 task_count = 0;
253 for (int i = 0; i < 10; ++i) {
fdoray6ef45cf2016-08-25 15:36:37254 loop()->task_runner()->PostDelayedTask(FROM_HERE,
tzik92b7a422017-04-11 15:00:44255 BindOnce(&IncrementInt, &task_count),
fdoray6ef45cf2016-08-25 15:36:37256 TimeDelta::FromMilliseconds(10 * i));
[email protected]b44d5cc2009-06-15 10:30:44257 }
258 // After all the previous tasks have executed, enqueue an event that will
259 // quit.
260 // This relies on the fact that delayed tasks are executed in delay order.
261 // That is verified in message_loop_unittest.cc.
fdoray6ef45cf2016-08-25 15:36:37262 loop()->task_runner()->PostDelayedTask(
tzik92b7a422017-04-11 15:00:44263 FROM_HERE,
264 BindOnce(&EventInjector::AddEvent, Unretained(injector()), 10,
265 MessageLoop::QuitWhenIdleClosure()),
[email protected]59e69e742013-06-18 20:27:52266 TimeDelta::FromMilliseconds(150));
fdoray6ef45cf2016-08-25 15:36:37267 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44268 ASSERT_EQ(10, task_count);
269 EXPECT_EQ(1, injector()->processed_events());
270}
271
272TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) {
273 // Tests that we process events while waiting for work.
274 // The event queue is empty at first.
275 for (int i = 0; i < 10; ++i) {
[email protected]763839e2011-12-09 23:06:02276 injector()->AddDummyEvent(0);
[email protected]b44d5cc2009-06-15 10:30:44277 }
278 // 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
280 // that posted it has been handled, so we expect 11 at that point).
tzika8cc2202017-04-18 07:01:15281 OnceClosure check_task =
282 BindOnce(&ExpectProcessedEvents, Unretained(injector()), 11);
283 OnceClosure posted_task =
284 BindOnce(&PostMessageLoopTask, FROM_HERE, std::move(check_task));
285 injector()->AddEventAsTask(10, std::move(posted_task));
[email protected]b44d5cc2009-06-15 10:30:44286
287 // And then quit (relies on the condition tested by TestEventTaskInterleave).
[email protected]a085953f2013-02-04 23:40:00288 injector()->AddEvent(10, MessageLoop::QuitWhenIdleClosure());
fdoray6ef45cf2016-08-25 15:36:37289 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44290
291 EXPECT_EQ(12, injector()->processed_events());
292}
293
294namespace {
295
296// This class is a helper for the concurrent events / posted tasks test below.
297// It will quit the main loop once enough tasks and events have been processed,
298// while making sure there is always work to do and events in the queue.
[email protected]59e69e742013-06-18 20:27:52299class ConcurrentHelper : public RefCounted<ConcurrentHelper> {
[email protected]b44d5cc2009-06-15 10:30:44300 public:
[email protected]2fdc86a2010-01-26 23:08:02301 explicit ConcurrentHelper(EventInjector* injector)
[email protected]b44d5cc2009-06-15 10:30:44302 : injector_(injector),
303 event_count_(kStartingEventCount),
304 task_count_(kStartingTaskCount) {
305 }
306
307 void FromTask() {
308 if (task_count_ > 0) {
309 --task_count_;
310 }
311 if (task_count_ == 0 && event_count_ == 0) {
[email protected]91cae2592013-01-10 14:56:17312 MessageLoop::current()->QuitWhenIdle();
[email protected]b44d5cc2009-06-15 10:30:44313 } else {
fdoray2df4a9e2016-07-18 23:47:16314 ThreadTaskRunnerHandle::Get()->PostTask(
tzik92b7a422017-04-11 15:00:44315 FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, this));
[email protected]b44d5cc2009-06-15 10:30:44316 }
317 }
318
319 void FromEvent() {
320 if (event_count_ > 0) {
321 --event_count_;
322 }
323 if (task_count_ == 0 && event_count_ == 0) {
[email protected]91cae2592013-01-10 14:56:17324 MessageLoop::current()->QuitWhenIdle();
[email protected]b44d5cc2009-06-15 10:30:44325 } else {
tzika8cc2202017-04-18 07:01:15326 injector_->AddEventAsTask(0,
327 BindOnce(&ConcurrentHelper::FromEvent, this));
[email protected]b44d5cc2009-06-15 10:30:44328 }
329 }
330
331 int event_count() const { return event_count_; }
332 int task_count() const { return task_count_; }
333
334 private:
[email protected]59e69e742013-06-18 20:27:52335 friend class RefCounted<ConcurrentHelper>;
[email protected]877d55d2009-11-05 21:53:08336
337 ~ConcurrentHelper() {}
338
[email protected]b44d5cc2009-06-15 10:30:44339 static const int kStartingEventCount = 20;
340 static const int kStartingTaskCount = 20;
341
342 EventInjector* injector_;
343 int event_count_;
344 int task_count_;
345};
346
347} // namespace
348
349TEST_F(MessagePumpGLibTest, TestConcurrentEventPostedTask) {
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
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
[email protected]91cae2592013-01-10 14:56:17354 // to 0, and MessageLoop::QuitWhenIdle() will never be called.
[email protected]b44d5cc2009-06-15 10:30:44355 scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector());
356
357 // Add 2 events to the queue to make sure it is always full (when we remove
358 // the event before processing it).
tzika8cc2202017-04-18 07:01:15359 injector()->AddEventAsTask(0, BindOnce(&ConcurrentHelper::FromEvent, helper));
360 injector()->AddEventAsTask(0, BindOnce(&ConcurrentHelper::FromEvent, helper));
[email protected]b44d5cc2009-06-15 10:30:44361
362 // Similarly post 2 tasks.
fdoray6ef45cf2016-08-25 15:36:37363 loop()->task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44364 FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, helper));
fdoray6ef45cf2016-08-25 15:36:37365 loop()->task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44366 FROM_HERE, BindOnce(&ConcurrentHelper::FromTask, helper));
[email protected]b44d5cc2009-06-15 10:30:44367
fdoray6ef45cf2016-08-25 15:36:37368 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44369 EXPECT_EQ(0, helper->event_count());
370 EXPECT_EQ(0, helper->task_count());
371}
372
373namespace {
374
375void AddEventsAndDrainGLib(EventInjector* injector) {
376 // Add a couple of dummy events
[email protected]763839e2011-12-09 23:06:02377 injector->AddDummyEvent(0);
378 injector->AddDummyEvent(0);
[email protected]b44d5cc2009-06-15 10:30:44379 // Then add an event that will quit the main loop.
[email protected]a085953f2013-02-04 23:40:00380 injector->AddEvent(0, MessageLoop::QuitWhenIdleClosure());
[email protected]b44d5cc2009-06-15 10:30:44381
382 // Post a couple of dummy tasks
tzik92b7a422017-04-11 15:00:44383 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, BindOnce(&DoNothing));
384 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, BindOnce(&DoNothing));
[email protected]b44d5cc2009-06-15 10:30:44385
386 // Drain the events
387 while (g_main_context_pending(NULL)) {
388 g_main_context_iteration(NULL, FALSE);
389 }
390}
391
392} // namespace
393
394TEST_F(MessagePumpGLibTest, TestDrainingGLib) {
395 // Tests that draining events using GLib works.
fdoray6ef45cf2016-08-25 15:36:37396 loop()->task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44397 FROM_HERE, BindOnce(&AddEventsAndDrainGLib, Unretained(injector())));
fdoray6ef45cf2016-08-25 15:36:37398 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44399
400 EXPECT_EQ(3, injector()->processed_events());
401}
402
[email protected]b44d5cc2009-06-15 10:30:44403namespace {
404
405// Helper class that lets us run the GLib message loop.
[email protected]59e69e742013-06-18 20:27:52406class GLibLoopRunner : public RefCounted<GLibLoopRunner> {
[email protected]b44d5cc2009-06-15 10:30:44407 public:
408 GLibLoopRunner() : quit_(false) { }
409
410 void RunGLib() {
411 while (!quit_) {
412 g_main_context_iteration(NULL, TRUE);
413 }
414 }
415
[email protected]258dca42011-09-21 00:17:19416 void RunLoop() {
[email protected]258dca42011-09-21 00:17:19417 while (!quit_) {
418 g_main_context_iteration(NULL, TRUE);
419 }
[email protected]b44d5cc2009-06-15 10:30:44420 }
421
422 void Quit() {
423 quit_ = true;
424 }
425
426 void Reset() {
427 quit_ = false;
428 }
429
430 private:
[email protected]59e69e742013-06-18 20:27:52431 friend class RefCounted<GLibLoopRunner>;
[email protected]877d55d2009-11-05 21:53:08432
433 ~GLibLoopRunner() {}
434
[email protected]b44d5cc2009-06-15 10:30:44435 bool quit_;
436};
437
438void TestGLibLoopInternal(EventInjector* injector) {
439 // Allow tasks to be processed from 'native' event loops.
440 MessageLoop::current()->SetNestableTasksAllowed(true);
441 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner();
442
443 int task_count = 0;
444 // Add a couple of dummy events
[email protected]763839e2011-12-09 23:06:02445 injector->AddDummyEvent(0);
446 injector->AddDummyEvent(0);
[email protected]b44d5cc2009-06-15 10:30:44447 // Post a couple of dummy tasks
fdoray2df4a9e2016-07-18 23:47:16448 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
tzik92b7a422017-04-11 15:00:44449 BindOnce(&IncrementInt, &task_count));
fdoray2df4a9e2016-07-18 23:47:16450 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
tzik92b7a422017-04-11 15:00:44451 BindOnce(&IncrementInt, &task_count));
[email protected]b44d5cc2009-06-15 10:30:44452 // Delayed events
[email protected]763839e2011-12-09 23:06:02453 injector->AddDummyEvent(10);
454 injector->AddDummyEvent(10);
[email protected]b44d5cc2009-06-15 10:30:44455 // Delayed work
fdoray2df4a9e2016-07-18 23:47:16456 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzik92b7a422017-04-11 15:00:44457 FROM_HERE, BindOnce(&IncrementInt, &task_count),
[email protected]59e69e742013-06-18 20:27:52458 TimeDelta::FromMilliseconds(30));
fdoray2df4a9e2016-07-18 23:47:16459 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzik92b7a422017-04-11 15:00:44460 FROM_HERE, BindOnce(&GLibLoopRunner::Quit, runner),
[email protected]59e69e742013-06-18 20:27:52461 TimeDelta::FromMilliseconds(40));
[email protected]b44d5cc2009-06-15 10:30:44462
463 // Run a nested, straight GLib message loop.
464 runner->RunGLib();
465
466 ASSERT_EQ(3, task_count);
467 EXPECT_EQ(4, injector->processed_events());
[email protected]91cae2592013-01-10 14:56:17468 MessageLoop::current()->QuitWhenIdle();
[email protected]b44d5cc2009-06-15 10:30:44469}
470
471void TestGtkLoopInternal(EventInjector* injector) {
472 // Allow tasks to be processed from 'native' event loops.
473 MessageLoop::current()->SetNestableTasksAllowed(true);
474 scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner();
475
476 int task_count = 0;
477 // Add a couple of dummy events
[email protected]763839e2011-12-09 23:06:02478 injector->AddDummyEvent(0);
479 injector->AddDummyEvent(0);
[email protected]b44d5cc2009-06-15 10:30:44480 // Post a couple of dummy tasks
fdoray2df4a9e2016-07-18 23:47:16481 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
tzik92b7a422017-04-11 15:00:44482 BindOnce(&IncrementInt, &task_count));
fdoray2df4a9e2016-07-18 23:47:16483 ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
tzik92b7a422017-04-11 15:00:44484 BindOnce(&IncrementInt, &task_count));
[email protected]b44d5cc2009-06-15 10:30:44485 // Delayed events
[email protected]763839e2011-12-09 23:06:02486 injector->AddDummyEvent(10);
487 injector->AddDummyEvent(10);
[email protected]b44d5cc2009-06-15 10:30:44488 // Delayed work
fdoray2df4a9e2016-07-18 23:47:16489 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzik92b7a422017-04-11 15:00:44490 FROM_HERE, BindOnce(&IncrementInt, &task_count),
[email protected]59e69e742013-06-18 20:27:52491 TimeDelta::FromMilliseconds(30));
fdoray2df4a9e2016-07-18 23:47:16492 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzik92b7a422017-04-11 15:00:44493 FROM_HERE, BindOnce(&GLibLoopRunner::Quit, runner),
[email protected]59e69e742013-06-18 20:27:52494 TimeDelta::FromMilliseconds(40));
[email protected]b44d5cc2009-06-15 10:30:44495
496 // Run a nested, straight Gtk message loop.
[email protected]258dca42011-09-21 00:17:19497 runner->RunLoop();
[email protected]b44d5cc2009-06-15 10:30:44498
499 ASSERT_EQ(3, task_count);
500 EXPECT_EQ(4, injector->processed_events());
[email protected]91cae2592013-01-10 14:56:17501 MessageLoop::current()->QuitWhenIdle();
[email protected]b44d5cc2009-06-15 10:30:44502}
503
504} // namespace
505
506TEST_F(MessagePumpGLibTest, TestGLibLoop) {
[email protected]8f5a7e492012-01-01 02:14:47507 // Tests that events and posted tasks are correctly executed if the message
[email protected]b44d5cc2009-06-15 10:30:44508 // loop is not run by MessageLoop::Run() but by a straight GLib loop.
509 // Note that in this case we don't make strong guarantees about niceness
510 // between events and posted tasks.
fdoray6ef45cf2016-08-25 15:36:37511 loop()->task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44512 FROM_HERE, BindOnce(&TestGLibLoopInternal, Unretained(injector())));
fdoray6ef45cf2016-08-25 15:36:37513 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44514}
515
516TEST_F(MessagePumpGLibTest, TestGtkLoop) {
[email protected]8f5a7e492012-01-01 02:14:47517 // Tests that events and posted tasks are correctly executed if the message
[email protected]b44d5cc2009-06-15 10:30:44518 // loop is not run by MessageLoop::Run() but by a straight Gtk loop.
519 // Note that in this case we don't make strong guarantees about niceness
520 // between events and posted tasks.
fdoray6ef45cf2016-08-25 15:36:37521 loop()->task_runner()->PostTask(
tzik92b7a422017-04-11 15:00:44522 FROM_HERE, BindOnce(&TestGtkLoopInternal, Unretained(injector())));
fdoray6ef45cf2016-08-25 15:36:37523 RunLoop().Run();
[email protected]b44d5cc2009-06-15 10:30:44524}
[email protected]7ff48ca2013-02-06 16:56:19525
526} // namespace base