[email protected] | ac4c668 | 2012-01-04 00:57:39 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/message_pump_glib.h" |
| 6 | |
[email protected] | d90efd5 | 2012-06-28 19:57:26 | [diff] [blame^] | 7 | #include <glib.h> |
[email protected] | 23d3539 | 2009-06-15 19:53:08 | [diff] [blame] | 8 | #include <math.h> |
| 9 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <vector> |
[email protected] | 23d3539 | 2009-06-15 19:53:08 | [diff] [blame] | 12 | |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 13 | #include "base/bind.h" |
| 14 | #include "base/bind_helpers.h" |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 15 | #include "base/callback.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 17 | #include "base/message_loop.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 18 | #include "base/threading/thread.h" |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
[email protected] | a13283cc | 2012-04-05 00:21:22 | [diff] [blame] | 21 | #if defined(TOOLKIT_GTK) |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 22 | #include <gtk/gtk.h> |
| 23 | #endif |
| 24 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // This class injects dummy "events" into the GLib loop. When "handled" these |
| 28 | // events can run tasks. This is intended to mock gtk events (the corresponding |
| 29 | // GLib source runs at the same priority). |
| 30 | class EventInjector { |
| 31 | public: |
| 32 | EventInjector() : processed_events_(0) { |
| 33 | source_ = static_cast<Source*>(g_source_new(&SourceFuncs, sizeof(Source))); |
| 34 | source_->injector = this; |
| 35 | g_source_attach(source_, NULL); |
| 36 | g_source_set_can_recurse(source_, TRUE); |
| 37 | } |
| 38 | |
| 39 | ~EventInjector() { |
| 40 | g_source_destroy(source_); |
| 41 | g_source_unref(source_); |
| 42 | } |
| 43 | |
| 44 | int HandlePrepare() { |
| 45 | // If the queue is empty, block. |
| 46 | if (events_.empty()) |
| 47 | return -1; |
| 48 | base::TimeDelta delta = events_[0].time - base::Time::NowFromSystemTime(); |
| 49 | return std::max(0, static_cast<int>(ceil(delta.InMillisecondsF()))); |
| 50 | } |
| 51 | |
| 52 | bool HandleCheck() { |
| 53 | if (events_.empty()) |
| 54 | return false; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 55 | return events_[0].time <= base::Time::NowFromSystemTime(); |
| 56 | } |
| 57 | |
| 58 | void HandleDispatch() { |
| 59 | if (events_.empty()) |
| 60 | return; |
| 61 | Event event = events_[0]; |
| 62 | events_.erase(events_.begin()); |
| 63 | ++processed_events_; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 64 | if (!event.callback.is_null()) |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 65 | event.callback.Run(); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 66 | else if (!event.task.is_null()) |
| 67 | event.task.Run(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 70 | // Adds an event to the queue. When "handled", executes |callback|. |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 71 | // delay_ms is relative to the last event if any, or to Now() otherwise. |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 72 | void AddEvent(int delay_ms, const base::Closure& callback) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 73 | AddEventHelper(delay_ms, callback, base::Closure()); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void AddDummyEvent(int delay_ms) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 77 | AddEventHelper(delay_ms, base::Closure(), base::Closure()); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 80 | void AddEventAsTask(int delay_ms, const base::Closure& task) { |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 81 | AddEventHelper(delay_ms, base::Closure(), task); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void Reset() { |
| 85 | processed_events_ = 0; |
| 86 | events_.clear(); |
| 87 | } |
| 88 | |
| 89 | int processed_events() const { return processed_events_; } |
| 90 | |
| 91 | private: |
| 92 | struct Event { |
| 93 | base::Time time; |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 94 | base::Closure callback; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 95 | base::Closure task; |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | struct Source : public GSource { |
| 99 | EventInjector* injector; |
| 100 | }; |
| 101 | |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 102 | void AddEventHelper( |
| 103 | int delay_ms, const base::Closure& callback, const base::Closure& task) { |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 104 | base::Time last_time; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 105 | if (!events_.empty()) |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 106 | last_time = (events_.end()-1)->time; |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 107 | else |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 108 | last_time = base::Time::NowFromSystemTime(); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 109 | |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 110 | base::Time future = last_time + base::TimeDelta::FromMilliseconds(delay_ms); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 111 | EventInjector::Event event = {future, callback, task}; |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 112 | events_.push_back(event); |
| 113 | } |
| 114 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 115 | 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 | |
| 138 | GSourceFuncs EventInjector::SourceFuncs = { |
| 139 | EventInjector::Prepare, |
| 140 | EventInjector::Check, |
| 141 | EventInjector::Dispatch, |
| 142 | NULL |
| 143 | }; |
| 144 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 145 | void IncrementInt(int *value) { |
| 146 | ++*value; |
| 147 | } |
| 148 | |
| 149 | // Checks how many events have been processed by the injector. |
| 150 | void ExpectProcessedEvents(EventInjector* injector, int count) { |
| 151 | EXPECT_EQ(injector->processed_events(), count); |
| 152 | } |
| 153 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 154 | // Posts a task on the current message loop. |
| 155 | void PostMessageLoopTask(const tracked_objects::Location& from_here, |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 156 | const base::Closure& task) { |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 157 | MessageLoop::current()->PostTask(from_here, task); |
| 158 | } |
| 159 | |
| 160 | // Test fixture. |
| 161 | class MessagePumpGLibTest : public testing::Test { |
| 162 | public: |
| 163 | MessagePumpGLibTest() : loop_(NULL), injector_(NULL) { } |
| 164 | |
| 165 | virtual void SetUp() { |
| 166 | loop_ = new MessageLoop(MessageLoop::TYPE_UI); |
| 167 | injector_ = new EventInjector(); |
| 168 | } |
| 169 | |
| 170 | virtual void TearDown() { |
| 171 | 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] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 188 | TEST_F(MessagePumpGLibTest, TestQuit) { |
| 189 | // Checks that Quit works and that the basic infrastructure is working. |
| 190 | |
| 191 | // Quit from a task |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 192 | loop()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 193 | loop()->Run(); |
| 194 | EXPECT_EQ(0, injector()->processed_events()); |
| 195 | |
| 196 | injector()->Reset(); |
| 197 | // Quit from an event |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 198 | injector()->AddEvent(0, MessageLoop::QuitClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 199 | loop()->Run(); |
| 200 | EXPECT_EQ(1, injector()->processed_events()); |
| 201 | } |
| 202 | |
| 203 | TEST_F(MessagePumpGLibTest, TestEventTaskInterleave) { |
| 204 | // Checks that tasks posted by events are executed before the next event if |
| 205 | // the posted task queue is empty. |
| 206 | // MessageLoop doesn't make strong guarantees that it is the case, but the |
| 207 | // current implementation ensures it and the tests below rely on it. |
| 208 | // If changes cause this test to fail, it is reasonable to change it, but |
| 209 | // TestWorkWhileWaitingForEvents and TestEventsWhileWaitingForWork have to be |
| 210 | // changed accordingly, otherwise they can become flaky. |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 211 | injector()->AddEventAsTask(0, base::Bind(&base::DoNothing)); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 212 | base::Closure check_task = |
| 213 | base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 2); |
| 214 | base::Closure posted_task = |
| 215 | base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 216 | injector()->AddEventAsTask(0, posted_task); |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 217 | injector()->AddEventAsTask(0, base::Bind(&base::DoNothing)); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 218 | injector()->AddEvent(0, MessageLoop::QuitClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 219 | loop()->Run(); |
| 220 | EXPECT_EQ(4, injector()->processed_events()); |
| 221 | |
| 222 | injector()->Reset(); |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 223 | injector()->AddEventAsTask(0, base::Bind(&base::DoNothing)); |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 224 | check_task = |
| 225 | base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 2); |
| 226 | posted_task = base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 227 | injector()->AddEventAsTask(0, posted_task); |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 228 | injector()->AddEventAsTask(10, base::Bind(&base::DoNothing)); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 229 | injector()->AddEvent(0, MessageLoop::QuitClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 230 | loop()->Run(); |
| 231 | EXPECT_EQ(4, injector()->processed_events()); |
| 232 | } |
| 233 | |
| 234 | TEST_F(MessagePumpGLibTest, TestWorkWhileWaitingForEvents) { |
| 235 | int task_count = 0; |
| 236 | // Tests that we process tasks while waiting for new events. |
| 237 | // The event queue is empty at first. |
| 238 | for (int i = 0; i < 10; ++i) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 239 | loop()->PostTask(FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 240 | } |
| 241 | // After all the previous tasks have executed, enqueue an event that will |
| 242 | // quit. |
| 243 | loop()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 244 | FROM_HERE, |
| 245 | base::Bind(&EventInjector::AddEvent, base::Unretained(injector()), 0, |
| 246 | MessageLoop::QuitClosure())); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 247 | loop()->Run(); |
| 248 | ASSERT_EQ(10, task_count); |
| 249 | EXPECT_EQ(1, injector()->processed_events()); |
| 250 | |
| 251 | // Tests that we process delayed tasks while waiting for new events. |
| 252 | injector()->Reset(); |
| 253 | task_count = 0; |
| 254 | for (int i = 0; i < 10; ++i) { |
| 255 | loop()->PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 256 | FROM_HERE, |
| 257 | base::Bind(&IncrementInt, &task_count), |
| 258 | base::TimeDelta::FromMilliseconds(10*i)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 259 | } |
| 260 | // After all the previous tasks have executed, enqueue an event that will |
| 261 | // quit. |
| 262 | // This relies on the fact that delayed tasks are executed in delay order. |
| 263 | // That is verified in message_loop_unittest.cc. |
| 264 | loop()->PostDelayedTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 265 | FROM_HERE, |
| 266 | base::Bind(&EventInjector::AddEvent, base::Unretained(injector()), 10, |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 267 | MessageLoop::QuitClosure()), |
| 268 | base::TimeDelta::FromMilliseconds(150)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 269 | loop()->Run(); |
| 270 | ASSERT_EQ(10, task_count); |
| 271 | EXPECT_EQ(1, injector()->processed_events()); |
| 272 | } |
| 273 | |
| 274 | TEST_F(MessagePumpGLibTest, TestEventsWhileWaitingForWork) { |
| 275 | // Tests that we process events while waiting for work. |
| 276 | // The event queue is empty at first. |
| 277 | for (int i = 0; i < 10; ++i) { |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 278 | injector()->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 279 | } |
| 280 | // After all the events have been processed, post a task that will check that |
| 281 | // the events have been processed (note: the task executes after the event |
| 282 | // that posted it has been handled, so we expect 11 at that point). |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 283 | base::Closure check_task = |
| 284 | base::Bind(&ExpectProcessedEvents, base::Unretained(injector()), 11); |
| 285 | base::Closure posted_task = |
| 286 | base::Bind(&PostMessageLoopTask, FROM_HERE, check_task); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 287 | injector()->AddEventAsTask(10, posted_task); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 288 | |
| 289 | // And then quit (relies on the condition tested by TestEventTaskInterleave). |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 290 | injector()->AddEvent(10, MessageLoop::QuitClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 291 | loop()->Run(); |
| 292 | |
| 293 | EXPECT_EQ(12, injector()->processed_events()); |
| 294 | } |
| 295 | |
| 296 | namespace { |
| 297 | |
| 298 | // This class is a helper for the concurrent events / posted tasks test below. |
| 299 | // It will quit the main loop once enough tasks and events have been processed, |
| 300 | // while making sure there is always work to do and events in the queue. |
| 301 | class ConcurrentHelper : public base::RefCounted<ConcurrentHelper> { |
| 302 | public: |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 303 | explicit ConcurrentHelper(EventInjector* injector) |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 304 | : injector_(injector), |
| 305 | event_count_(kStartingEventCount), |
| 306 | task_count_(kStartingTaskCount) { |
| 307 | } |
| 308 | |
| 309 | void FromTask() { |
| 310 | if (task_count_ > 0) { |
| 311 | --task_count_; |
| 312 | } |
| 313 | if (task_count_ == 0 && event_count_ == 0) { |
| 314 | MessageLoop::current()->Quit(); |
| 315 | } else { |
| 316 | MessageLoop::current()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 317 | FROM_HERE, base::Bind(&ConcurrentHelper::FromTask, this)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | void FromEvent() { |
| 322 | if (event_count_ > 0) { |
| 323 | --event_count_; |
| 324 | } |
| 325 | if (task_count_ == 0 && event_count_ == 0) { |
| 326 | MessageLoop::current()->Quit(); |
| 327 | } else { |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 328 | injector_->AddEventAsTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 329 | 0, base::Bind(&ConcurrentHelper::FromEvent, this)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | int event_count() const { return event_count_; } |
| 334 | int task_count() const { return task_count_; } |
| 335 | |
| 336 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 337 | friend class base::RefCounted<ConcurrentHelper>; |
| 338 | |
| 339 | ~ConcurrentHelper() {} |
| 340 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 341 | static const int kStartingEventCount = 20; |
| 342 | static const int kStartingTaskCount = 20; |
| 343 | |
| 344 | EventInjector* injector_; |
| 345 | int event_count_; |
| 346 | int task_count_; |
| 347 | }; |
| 348 | |
| 349 | } // namespace |
| 350 | |
| 351 | TEST_F(MessagePumpGLibTest, TestConcurrentEventPostedTask) { |
| 352 | // Tests that posted tasks don't starve events, nor the opposite. |
| 353 | // We use the helper class above. We keep both event and posted task queues |
| 354 | // full, the helper verifies that both tasks and events get processed. |
| 355 | // If that is not the case, either event_count_ or task_count_ will not get |
| 356 | // to 0, and MessageLoop::Quit() will never be called. |
| 357 | scoped_refptr<ConcurrentHelper> helper = new ConcurrentHelper(injector()); |
| 358 | |
| 359 | // Add 2 events to the queue to make sure it is always full (when we remove |
| 360 | // the event before processing it). |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 361 | injector()->AddEventAsTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 362 | 0, base::Bind(&ConcurrentHelper::FromEvent, helper.get())); |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 363 | injector()->AddEventAsTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 364 | 0, base::Bind(&ConcurrentHelper::FromEvent, helper.get())); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 365 | |
| 366 | // Similarly post 2 tasks. |
| 367 | loop()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 368 | FROM_HERE, base::Bind(&ConcurrentHelper::FromTask, helper.get())); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 369 | loop()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 370 | FROM_HERE, base::Bind(&ConcurrentHelper::FromTask, helper.get())); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 371 | |
| 372 | loop()->Run(); |
| 373 | EXPECT_EQ(0, helper->event_count()); |
| 374 | EXPECT_EQ(0, helper->task_count()); |
| 375 | } |
| 376 | |
| 377 | namespace { |
| 378 | |
| 379 | void AddEventsAndDrainGLib(EventInjector* injector) { |
| 380 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 381 | injector->AddDummyEvent(0); |
| 382 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 383 | // Then add an event that will quit the main loop. |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 384 | injector->AddEvent(0, MessageLoop::QuitClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 385 | |
| 386 | // Post a couple of dummy tasks |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 387 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&base::DoNothing)); |
| 388 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&base::DoNothing)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 389 | |
| 390 | // Drain the events |
| 391 | while (g_main_context_pending(NULL)) { |
| 392 | g_main_context_iteration(NULL, FALSE); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | } // namespace |
| 397 | |
| 398 | TEST_F(MessagePumpGLibTest, TestDrainingGLib) { |
| 399 | // Tests that draining events using GLib works. |
| 400 | loop()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 401 | FROM_HERE, |
| 402 | base::Bind(&AddEventsAndDrainGLib, base::Unretained(injector()))); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 403 | loop()->Run(); |
| 404 | |
| 405 | EXPECT_EQ(3, injector()->processed_events()); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | namespace { |
| 410 | |
[email protected] | a13283cc | 2012-04-05 00:21:22 | [diff] [blame] | 411 | #if defined(TOOLKIT_GTK) |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 412 | void AddEventsAndDrainGtk(EventInjector* injector) { |
| 413 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 414 | injector->AddDummyEvent(0); |
| 415 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 416 | // Then add an event that will quit the main loop. |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 417 | injector->AddEvent(0, MessageLoop::QuitClosure()); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 418 | |
| 419 | // Post a couple of dummy tasks |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 420 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&base::DoNothing)); |
| 421 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&base::DoNothing)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 422 | |
| 423 | // Drain the events |
| 424 | while (gtk_events_pending()) { |
| 425 | gtk_main_iteration(); |
| 426 | } |
| 427 | } |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 428 | #endif |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 429 | |
| 430 | } // namespace |
| 431 | |
[email protected] | a13283cc | 2012-04-05 00:21:22 | [diff] [blame] | 432 | #if defined(TOOLKIT_GTK) |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 433 | TEST_F(MessagePumpGLibTest, TestDrainingGtk) { |
| 434 | // Tests that draining events using Gtk works. |
| 435 | loop()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 436 | FROM_HERE, |
| 437 | base::Bind(&AddEventsAndDrainGtk, base::Unretained(injector()))); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 438 | loop()->Run(); |
| 439 | |
| 440 | EXPECT_EQ(3, injector()->processed_events()); |
| 441 | } |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 442 | #endif |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 443 | |
| 444 | namespace { |
| 445 | |
| 446 | // Helper class that lets us run the GLib message loop. |
| 447 | class GLibLoopRunner : public base::RefCounted<GLibLoopRunner> { |
| 448 | public: |
| 449 | GLibLoopRunner() : quit_(false) { } |
| 450 | |
| 451 | void RunGLib() { |
| 452 | while (!quit_) { |
| 453 | g_main_context_iteration(NULL, TRUE); |
| 454 | } |
| 455 | } |
| 456 | |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 457 | void RunLoop() { |
[email protected] | a13283cc | 2012-04-05 00:21:22 | [diff] [blame] | 458 | #if defined(TOOLKIT_GTK) |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 459 | while (!quit_) { |
| 460 | gtk_main_iteration(); |
| 461 | } |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 462 | #else |
| 463 | while (!quit_) { |
| 464 | g_main_context_iteration(NULL, TRUE); |
| 465 | } |
| 466 | #endif |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void Quit() { |
| 470 | quit_ = true; |
| 471 | } |
| 472 | |
| 473 | void Reset() { |
| 474 | quit_ = false; |
| 475 | } |
| 476 | |
| 477 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 478 | friend class base::RefCounted<GLibLoopRunner>; |
| 479 | |
| 480 | ~GLibLoopRunner() {} |
| 481 | |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 482 | bool quit_; |
| 483 | }; |
| 484 | |
| 485 | void TestGLibLoopInternal(EventInjector* injector) { |
| 486 | // Allow tasks to be processed from 'native' event loops. |
| 487 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 488 | scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
| 489 | |
| 490 | int task_count = 0; |
| 491 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 492 | injector->AddDummyEvent(0); |
| 493 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 494 | // Post a couple of dummy tasks |
| 495 | MessageLoop::current()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 496 | FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 497 | MessageLoop::current()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 498 | FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 499 | // Delayed events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 500 | injector->AddDummyEvent(10); |
| 501 | injector->AddDummyEvent(10); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 502 | // Delayed work |
| 503 | MessageLoop::current()->PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 504 | FROM_HERE, |
| 505 | base::Bind(&IncrementInt, &task_count), |
| 506 | base::TimeDelta::FromMilliseconds(30)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 507 | MessageLoop::current()->PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 508 | FROM_HERE, |
| 509 | base::Bind(&GLibLoopRunner::Quit, runner.get()), |
| 510 | base::TimeDelta::FromMilliseconds(40)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 511 | |
| 512 | // Run a nested, straight GLib message loop. |
| 513 | runner->RunGLib(); |
| 514 | |
| 515 | ASSERT_EQ(3, task_count); |
| 516 | EXPECT_EQ(4, injector->processed_events()); |
| 517 | MessageLoop::current()->Quit(); |
| 518 | } |
| 519 | |
| 520 | void TestGtkLoopInternal(EventInjector* injector) { |
| 521 | // Allow tasks to be processed from 'native' event loops. |
| 522 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 523 | scoped_refptr<GLibLoopRunner> runner = new GLibLoopRunner(); |
| 524 | |
| 525 | int task_count = 0; |
| 526 | // Add a couple of dummy events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 527 | injector->AddDummyEvent(0); |
| 528 | injector->AddDummyEvent(0); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 529 | // Post a couple of dummy tasks |
| 530 | MessageLoop::current()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 531 | FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 532 | MessageLoop::current()->PostTask( |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 533 | FROM_HERE, base::Bind(&IncrementInt, &task_count)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 534 | // Delayed events |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 535 | injector->AddDummyEvent(10); |
| 536 | injector->AddDummyEvent(10); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 537 | // Delayed work |
| 538 | MessageLoop::current()->PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 539 | FROM_HERE, |
| 540 | base::Bind(&IncrementInt, &task_count), |
| 541 | base::TimeDelta::FromMilliseconds(30)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 542 | MessageLoop::current()->PostDelayedTask( |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 543 | FROM_HERE, |
| 544 | base::Bind(&GLibLoopRunner::Quit, runner.get()), |
| 545 | base::TimeDelta::FromMilliseconds(40)); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 546 | |
| 547 | // Run a nested, straight Gtk message loop. |
[email protected] | 258dca4 | 2011-09-21 00:17:19 | [diff] [blame] | 548 | runner->RunLoop(); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 549 | |
| 550 | ASSERT_EQ(3, task_count); |
| 551 | EXPECT_EQ(4, injector->processed_events()); |
| 552 | MessageLoop::current()->Quit(); |
| 553 | } |
| 554 | |
| 555 | } // namespace |
| 556 | |
| 557 | TEST_F(MessagePumpGLibTest, TestGLibLoop) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 558 | // Tests that events and posted tasks are correctly executed if the message |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 559 | // loop is not run by MessageLoop::Run() but by a straight GLib loop. |
| 560 | // Note that in this case we don't make strong guarantees about niceness |
| 561 | // between events and posted tasks. |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 562 | loop()->PostTask( |
| 563 | FROM_HERE, |
| 564 | base::Bind(&TestGLibLoopInternal, base::Unretained(injector()))); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 565 | loop()->Run(); |
| 566 | } |
| 567 | |
| 568 | TEST_F(MessagePumpGLibTest, TestGtkLoop) { |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 569 | // Tests that events and posted tasks are correctly executed if the message |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 570 | // loop is not run by MessageLoop::Run() but by a straight Gtk loop. |
| 571 | // Note that in this case we don't make strong guarantees about niceness |
| 572 | // between events and posted tasks. |
[email protected] | 8f5a7e49 | 2012-01-01 02:14:47 | [diff] [blame] | 573 | loop()->PostTask( |
| 574 | FROM_HERE, |
| 575 | base::Bind(&TestGtkLoopInternal, base::Unretained(injector()))); |
[email protected] | b44d5cc | 2009-06-15 10:30:44 | [diff] [blame] | 576 | loop()->Run(); |
| 577 | } |