OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 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 "ui/events/platform/platform_event_source.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "ui/events/platform/platform_event_dispatcher.h" |
| 14 #include "ui/events/platform/platform_event_observer.h" |
| 15 #include "ui/events/platform/scoped_event_dispatcher.h" |
| 16 |
| 17 namespace ui { |
| 18 |
| 19 namespace { |
| 20 |
| 21 scoped_ptr<PlatformEvent> CreatePlatformEvent() { |
| 22 scoped_ptr<PlatformEvent> event(new PlatformEvent()); |
| 23 memset(event.get(), 0, sizeof(PlatformEvent)); |
| 24 return event.Pass(); |
| 25 } |
| 26 |
| 27 template <typename T> |
| 28 void DestroyScopedPtr(scoped_ptr<T> object) {} |
| 29 |
| 30 } // namespace |
| 31 |
| 32 class TestPlatformEventSource : public PlatformEventSource { |
| 33 public: |
| 34 TestPlatformEventSource() {} |
| 35 virtual ~TestPlatformEventSource() {} |
| 36 |
| 37 uint32_t Dispatch(const PlatformEvent& event) { return DispatchEvent(event); } |
| 38 |
| 39 // Dispatches the stream of events, and returns the number of events that are |
| 40 // dispatched before it is requested to stop. |
| 41 size_t DispatchEventStream(const ScopedVector<PlatformEvent>& events) { |
| 42 for (size_t count = 0; count < events.size(); ++count) { |
| 43 uint32_t action = DispatchEvent(*events[count]); |
| 44 if (action & POST_DISPATCH_QUIT_LOOP) |
| 45 return count + 1; |
| 46 } |
| 47 return events.size(); |
| 48 } |
| 49 |
| 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); |
| 52 }; |
| 53 |
| 54 class TestPlatformEventDispatcher : public PlatformEventDispatcher { |
| 55 public: |
| 56 TestPlatformEventDispatcher(int id, std::vector<int>* list) |
| 57 : id_(id), list_(list), post_dispatch_action_(POST_DISPATCH_NONE) { |
| 58 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); |
| 59 } |
| 60 virtual ~TestPlatformEventDispatcher() { |
| 61 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); |
| 62 } |
| 63 |
| 64 void set_post_dispatch_action(uint32_t action) { |
| 65 post_dispatch_action_ = action; |
| 66 } |
| 67 |
| 68 protected: |
| 69 // PlatformEventDispatcher: |
| 70 virtual bool CanDispatchEvent(const PlatformEvent& event) OVERRIDE { |
| 71 return true; |
| 72 } |
| 73 |
| 74 virtual uint32_t DispatchEvent(const PlatformEvent& event) OVERRIDE { |
| 75 list_->push_back(id_); |
| 76 return post_dispatch_action_; |
| 77 } |
| 78 |
| 79 private: |
| 80 int id_; |
| 81 std::vector<int>* list_; |
| 82 uint32_t post_dispatch_action_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); |
| 85 }; |
| 86 |
| 87 class TestPlatformEventObserver : public PlatformEventObserver { |
| 88 public: |
| 89 TestPlatformEventObserver(int id, std::vector<int>* list) |
| 90 : id_(id), list_(list), consume_event_(false) { |
| 91 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); |
| 92 } |
| 93 virtual ~TestPlatformEventObserver() { |
| 94 PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this); |
| 95 } |
| 96 |
| 97 void set_consume_event(bool consume) { consume_event_ = consume; } |
| 98 |
| 99 protected: |
| 100 // PlatformEventObserver: |
| 101 virtual EventStatus WillProcessEvent(const PlatformEvent& event) OVERRIDE { |
| 102 list_->push_back(id_); |
| 103 return consume_event_ ? EVENT_STATUS_HANDLED : EVENT_STATUS_CONTINUE; |
| 104 } |
| 105 |
| 106 virtual void DidProcessEvent(const PlatformEvent& event) OVERRIDE {} |
| 107 |
| 108 private: |
| 109 int id_; |
| 110 std::vector<int>* list_; |
| 111 bool consume_event_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventObserver); |
| 114 }; |
| 115 |
| 116 class PlatformEventTest : public testing::Test { |
| 117 public: |
| 118 PlatformEventTest() {} |
| 119 virtual ~PlatformEventTest() {} |
| 120 |
| 121 TestPlatformEventSource* source() { return source_.get(); } |
| 122 |
| 123 protected: |
| 124 // testing::Test: |
| 125 virtual void SetUp() OVERRIDE { |
| 126 source_.reset(new TestPlatformEventSource()); |
| 127 } |
| 128 |
| 129 private: |
| 130 scoped_ptr<TestPlatformEventSource> source_; |
| 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest); |
| 133 }; |
| 134 |
| 135 // Tests that a dispatcher receives an event. |
| 136 TEST_F(PlatformEventTest, DispatcherBasic) { |
| 137 std::vector<int> list_dispatcher; |
| 138 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 139 source()->Dispatch(*event); |
| 140 EXPECT_EQ(0u, list_dispatcher.size()); |
| 141 { |
| 142 TestPlatformEventDispatcher dispatcher(1, &list_dispatcher); |
| 143 |
| 144 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 145 source()->Dispatch(*event); |
| 146 ASSERT_EQ(1u, list_dispatcher.size()); |
| 147 EXPECT_EQ(1, list_dispatcher[0]); |
| 148 } |
| 149 |
| 150 list_dispatcher.clear(); |
| 151 event = CreatePlatformEvent(); |
| 152 source()->Dispatch(*event); |
| 153 EXPECT_EQ(0u, list_dispatcher.size()); |
| 154 } |
| 155 |
| 156 // Tests that dispatchers receive events in the correct order. |
| 157 TEST_F(PlatformEventTest, DispatcherOrder) { |
| 158 std::vector<int> list_dispatcher; |
| 159 int sequence[] = {21, 3, 6, 45}; |
| 160 ScopedVector<TestPlatformEventDispatcher> dispatchers; |
| 161 for (size_t i = 0; i < arraysize(sequence); ++i) { |
| 162 dispatchers.push_back( |
| 163 new TestPlatformEventDispatcher(sequence[i], &list_dispatcher)); |
| 164 } |
| 165 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 166 source()->Dispatch(*event); |
| 167 ASSERT_EQ(arraysize(sequence), list_dispatcher.size()); |
| 168 EXPECT_EQ(std::vector<int>(sequence, sequence + arraysize(sequence)), |
| 169 list_dispatcher); |
| 170 } |
| 171 |
| 172 // Tests that if a dispatcher consumes the event, the subsequent dispatchers do |
| 173 // not receive the event. |
| 174 TEST_F(PlatformEventTest, DispatcherConsumesEventToStopDispatch) { |
| 175 std::vector<int> list_dispatcher; |
| 176 TestPlatformEventDispatcher first(12, &list_dispatcher); |
| 177 TestPlatformEventDispatcher second(23, &list_dispatcher); |
| 178 |
| 179 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 180 source()->Dispatch(*event); |
| 181 ASSERT_EQ(2u, list_dispatcher.size()); |
| 182 EXPECT_EQ(12, list_dispatcher[0]); |
| 183 EXPECT_EQ(23, list_dispatcher[1]); |
| 184 list_dispatcher.clear(); |
| 185 |
| 186 first.set_post_dispatch_action(POST_DISPATCH_STOP_PROPAGATION); |
| 187 event = CreatePlatformEvent(); |
| 188 source()->Dispatch(*event); |
| 189 ASSERT_EQ(1u, list_dispatcher.size()); |
| 190 EXPECT_EQ(12, list_dispatcher[0]); |
| 191 } |
| 192 |
| 193 // Tests that observers receive events. |
| 194 TEST_F(PlatformEventTest, ObserverBasic) { |
| 195 std::vector<int> list_observer; |
| 196 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 197 source()->Dispatch(*event); |
| 198 EXPECT_EQ(0u, list_observer.size()); |
| 199 { |
| 200 TestPlatformEventObserver observer(31, &list_observer); |
| 201 |
| 202 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 203 source()->Dispatch(*event); |
| 204 ASSERT_EQ(1u, list_observer.size()); |
| 205 EXPECT_EQ(31, list_observer[0]); |
| 206 } |
| 207 |
| 208 list_observer.clear(); |
| 209 event = CreatePlatformEvent(); |
| 210 source()->Dispatch(*event); |
| 211 EXPECT_EQ(0u, list_observer.size()); |
| 212 } |
| 213 |
| 214 // Tests that observers receive events in the correct order. |
| 215 TEST_F(PlatformEventTest, ObserverOrder) { |
| 216 std::vector<int> list_observer; |
| 217 const int sequence[] = {21, 3, 6, 45}; |
| 218 ScopedVector<TestPlatformEventObserver> observers; |
| 219 for (size_t i = 0; i < arraysize(sequence); ++i) { |
| 220 observers.push_back( |
| 221 new TestPlatformEventObserver(sequence[i], &list_observer)); |
| 222 } |
| 223 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 224 source()->Dispatch(*event); |
| 225 ASSERT_EQ(arraysize(sequence), list_observer.size()); |
| 226 EXPECT_EQ(std::vector<int>(sequence, sequence + arraysize(sequence)), |
| 227 list_observer); |
| 228 } |
| 229 |
| 230 // Tests that observers and dispatchers receive events in the correct order. |
| 231 TEST_F(PlatformEventTest, DispatcherAndObserverOrder) { |
| 232 std::vector<int> list; |
| 233 TestPlatformEventDispatcher first_d(12, &list); |
| 234 TestPlatformEventObserver first_o(10, &list); |
| 235 TestPlatformEventDispatcher second_d(23, &list); |
| 236 TestPlatformEventObserver second_o(20, &list); |
| 237 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 238 source()->Dispatch(*event); |
| 239 const int expected[] = {10, 20, 12, 23}; |
| 240 EXPECT_EQ(std::vector<int>(expected, expected + arraysize(expected)), list); |
| 241 } |
| 242 |
| 243 // Tests that an observer can consume an event and stop its dispatch. |
| 244 TEST_F(PlatformEventTest, ObserverConsumesEventToStopDispatch) { |
| 245 std::vector<int> list; |
| 246 TestPlatformEventDispatcher first_d(12, &list); |
| 247 TestPlatformEventObserver first_o(10, &list); |
| 248 TestPlatformEventDispatcher second_d(23, &list); |
| 249 TestPlatformEventObserver second_o(20, &list); |
| 250 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 251 source()->Dispatch(*event); |
| 252 const int expected[] = {10, 20, 12, 23}; |
| 253 EXPECT_EQ(std::vector<int>(expected, expected + arraysize(expected)), list); |
| 254 |
| 255 list.clear(); |
| 256 first_o.set_consume_event(true); |
| 257 event = CreatePlatformEvent(); |
| 258 source()->Dispatch(*event); |
| 259 ASSERT_EQ(1u, list.size()); |
| 260 EXPECT_EQ(10, list[0]); |
| 261 } |
| 262 |
| 263 // Tests that an overridden dispatcher receives events before the default |
| 264 // dispatchers. |
| 265 TEST_F(PlatformEventTest, OverriddenDispatcherBasic) { |
| 266 std::vector<int> list; |
| 267 TestPlatformEventDispatcher dispatcher(10, &list); |
| 268 TestPlatformEventObserver observer(15, &list); |
| 269 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 270 source()->Dispatch(*event); |
| 271 ASSERT_EQ(2u, list.size()); |
| 272 EXPECT_EQ(15, list[0]); |
| 273 EXPECT_EQ(10, list[1]); |
| 274 list.clear(); |
| 275 |
| 276 TestPlatformEventDispatcher overriding_dispatcher(20, &list); |
| 277 source()->RemovePlatformEventDispatcher(&overriding_dispatcher); |
| 278 scoped_ptr<ScopedEventDispatcher> handle = |
| 279 source()->OverrideDispatcher(&overriding_dispatcher); |
| 280 source()->Dispatch(*event); |
| 281 ASSERT_EQ(2u, list.size()); |
| 282 EXPECT_EQ(15, list[0]); |
| 283 EXPECT_EQ(20, list[1]); |
| 284 } |
| 285 |
| 286 // Tests that an overridden dispatcher can request that the default dispatchers |
| 287 // can dispatch the events. |
| 288 TEST_F(PlatformEventTest, OverriddenDispatcherInvokeDefaultDispatcher) { |
| 289 std::vector<int> list; |
| 290 TestPlatformEventDispatcher dispatcher(10, &list); |
| 291 TestPlatformEventObserver observer(15, &list); |
| 292 TestPlatformEventDispatcher overriding_dispatcher(20, &list); |
| 293 source()->RemovePlatformEventDispatcher(&overriding_dispatcher); |
| 294 scoped_ptr<ScopedEventDispatcher> handle = |
| 295 source()->OverrideDispatcher(&overriding_dispatcher); |
| 296 overriding_dispatcher.set_post_dispatch_action(POST_DISPATCH_PERFORM_DEFAULT); |
| 297 |
| 298 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 299 source()->Dispatch(*event); |
| 300 // First the observer, then the overriding dispatcher, then the default |
| 301 // dispatcher. |
| 302 ASSERT_EQ(3u, list.size()); |
| 303 EXPECT_EQ(15, list[0]); |
| 304 EXPECT_EQ(20, list[1]); |
| 305 EXPECT_EQ(10, list[2]); |
| 306 list.clear(); |
| 307 |
| 308 // Install a second overriding dispatcher. |
| 309 TestPlatformEventDispatcher second_overriding(50, &list); |
| 310 source()->RemovePlatformEventDispatcher(&second_overriding); |
| 311 scoped_ptr<ScopedEventDispatcher> second_override_handle = |
| 312 source()->OverrideDispatcher(&second_overriding); |
| 313 source()->Dispatch(*event); |
| 314 ASSERT_EQ(2u, list.size()); |
| 315 EXPECT_EQ(15, list[0]); |
| 316 EXPECT_EQ(50, list[1]); |
| 317 list.clear(); |
| 318 |
| 319 second_overriding.set_post_dispatch_action(POST_DISPATCH_PERFORM_DEFAULT); |
| 320 source()->Dispatch(*event); |
| 321 // First the observer, then the second overriding dispatcher, then the default |
| 322 // dispatcher. |
| 323 ASSERT_EQ(3u, list.size()); |
| 324 EXPECT_EQ(15, list[0]); |
| 325 EXPECT_EQ(50, list[1]); |
| 326 EXPECT_EQ(10, list[2]); |
| 327 } |
| 328 |
| 329 // Provides mechanism for running tests from inside an active message-loop. |
| 330 class PlatformEventTestWithMessageLoop : public PlatformEventTest { |
| 331 public: |
| 332 PlatformEventTestWithMessageLoop() {} |
| 333 virtual ~PlatformEventTestWithMessageLoop() {} |
| 334 |
| 335 void Run() { |
| 336 message_loop_.PostTask( |
| 337 FROM_HERE, |
| 338 base::Bind(&PlatformEventTestWithMessageLoop::RunTest, |
| 339 base::Unretained(this))); |
| 340 message_loop_.Run(); |
| 341 } |
| 342 |
| 343 protected: |
| 344 void RunTest() { |
| 345 RunTestImpl(); |
| 346 message_loop_.Quit(); |
| 347 } |
| 348 |
| 349 virtual void RunTestImpl() = 0; |
| 350 |
| 351 private: |
| 352 base::MessageLoopForUI message_loop_; |
| 353 |
| 354 DISALLOW_COPY_AND_ASSIGN(PlatformEventTestWithMessageLoop); |
| 355 }; |
| 356 |
| 357 #define RUN_TEST_IN_MESSAGE_LOOP(name) \ |
| 358 TEST_F(name, Run) { Run(); } |
| 359 |
| 360 // Tests that a ScopedEventDispatcher restores the previous dispatcher when |
| 361 // destroyed. |
| 362 class ScopedDispatcherRestoresAfterDestroy |
| 363 : public PlatformEventTestWithMessageLoop { |
| 364 public: |
| 365 // PlatformEventTestWithMessageLoop: |
| 366 virtual void RunTestImpl() OVERRIDE { |
| 367 std::vector<int> list; |
| 368 TestPlatformEventDispatcher dispatcher(10, &list); |
| 369 TestPlatformEventObserver observer(15, &list); |
| 370 |
| 371 TestPlatformEventDispatcher first_overriding(20, &list); |
| 372 source()->RemovePlatformEventDispatcher(&first_overriding); |
| 373 scoped_ptr<ScopedEventDispatcher> first_override_handle = |
| 374 source()->OverrideDispatcher(&first_overriding); |
| 375 |
| 376 // Install a second overriding dispatcher. |
| 377 TestPlatformEventDispatcher second_overriding(50, &list); |
| 378 source()->RemovePlatformEventDispatcher(&second_overriding); |
| 379 scoped_ptr<ScopedEventDispatcher> second_override_handle = |
| 380 source()->OverrideDispatcher(&second_overriding); |
| 381 |
| 382 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 383 source()->Dispatch(*event); |
| 384 ASSERT_EQ(2u, list.size()); |
| 385 EXPECT_EQ(15, list[0]); |
| 386 EXPECT_EQ(50, list[1]); |
| 387 list.clear(); |
| 388 |
| 389 second_override_handle.reset(); |
| 390 source()->Dispatch(*event); |
| 391 ASSERT_EQ(2u, list.size()); |
| 392 EXPECT_EQ(15, list[0]); |
| 393 EXPECT_EQ(20, list[1]); |
| 394 } |
| 395 }; |
| 396 |
| 397 RUN_TEST_IN_MESSAGE_LOOP(ScopedDispatcherRestoresAfterDestroy) |
| 398 |
| 399 // This dispatcher destroys the handle to the ScopedEventDispatcher when |
| 400 // dispatching an event. |
| 401 class DestroyScopedHandleDispatcher : public TestPlatformEventDispatcher { |
| 402 public: |
| 403 DestroyScopedHandleDispatcher(int id, std::vector<int>* list) |
| 404 : TestPlatformEventDispatcher(id, list) {} |
| 405 virtual ~DestroyScopedHandleDispatcher() {} |
| 406 |
| 407 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { |
| 408 handler_ = handler.Pass(); |
| 409 } |
| 410 |
| 411 private: |
| 412 // PlatformEventDispatcher: |
| 413 virtual bool CanDispatchEvent(const PlatformEvent& event) OVERRIDE { |
| 414 return true; |
| 415 } |
| 416 |
| 417 virtual uint32_t DispatchEvent(const PlatformEvent& event) OVERRIDE { |
| 418 handler_.reset(); |
| 419 return TestPlatformEventDispatcher::DispatchEvent(event); |
| 420 } |
| 421 |
| 422 scoped_ptr<ScopedEventDispatcher> handler_; |
| 423 |
| 424 DISALLOW_COPY_AND_ASSIGN(DestroyScopedHandleDispatcher); |
| 425 }; |
| 426 |
| 427 // Tests that resetting an overridden dispatcher causes the nested message-loop |
| 428 // iteration to stop and the rest of the events are dispatched in the next |
| 429 // iteration. |
| 430 class DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration |
| 431 : public PlatformEventTestWithMessageLoop { |
| 432 public: |
| 433 void NestedTask(std::vector<int>* list, |
| 434 TestPlatformEventDispatcher* dispatcher) { |
| 435 ScopedVector<PlatformEvent> events; |
| 436 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 437 events.push_back(event.release()); |
| 438 event = CreatePlatformEvent(); |
| 439 events.push_back(event.release()); |
| 440 |
| 441 // Attempt to dispatch a couple of events. Dispatching the first event will |
| 442 // have terminated the ScopedEventDispatcher object, which will terminate |
| 443 // the current iteration of the message-loop. |
| 444 size_t count = source()->DispatchEventStream(events); |
| 445 EXPECT_EQ(1u, count); |
| 446 ASSERT_EQ(2u, list->size()); |
| 447 EXPECT_EQ(15, (*list)[0]); |
| 448 EXPECT_EQ(20, (*list)[1]); |
| 449 list->clear(); |
| 450 |
| 451 ASSERT_LT(count, events.size()); |
| 452 events.erase(events.begin(), events.begin() + count); |
| 453 |
| 454 count = source()->DispatchEventStream(events); |
| 455 EXPECT_EQ(1u, count); |
| 456 ASSERT_EQ(2u, list->size()); |
| 457 EXPECT_EQ(15, (*list)[0]); |
| 458 EXPECT_EQ(10, (*list)[1]); |
| 459 list->clear(); |
| 460 |
| 461 // Terminate the message-loop. |
| 462 base::MessageLoopForUI::current()->QuitNow(); |
| 463 } |
| 464 |
| 465 // PlatformEventTestWithMessageLoop: |
| 466 virtual void RunTestImpl() OVERRIDE { |
| 467 std::vector<int> list; |
| 468 TestPlatformEventDispatcher dispatcher(10, &list); |
| 469 TestPlatformEventObserver observer(15, &list); |
| 470 |
| 471 DestroyScopedHandleDispatcher overriding(20, &list); |
| 472 source()->RemovePlatformEventDispatcher(&overriding); |
| 473 scoped_ptr<ScopedEventDispatcher> override_handle = |
| 474 source()->OverrideDispatcher(&overriding); |
| 475 |
| 476 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 477 source()->Dispatch(*event); |
| 478 ASSERT_EQ(2u, list.size()); |
| 479 EXPECT_EQ(15, list[0]); |
| 480 EXPECT_EQ(20, list[1]); |
| 481 list.clear(); |
| 482 |
| 483 overriding.SetScopedHandle(override_handle.Pass()); |
| 484 base::RunLoop run_loop; |
| 485 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 486 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
| 487 loop->PostTask( |
| 488 FROM_HERE, |
| 489 base::Bind( |
| 490 &DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration:: |
| 491 NestedTask, |
| 492 base::Unretained(this), |
| 493 base::Unretained(&list), |
| 494 base::Unretained(&overriding))); |
| 495 run_loop.Run(); |
| 496 |
| 497 // Dispatching the event should now reach the default dispatcher. |
| 498 source()->Dispatch(*event); |
| 499 ASSERT_EQ(2u, list.size()); |
| 500 EXPECT_EQ(15, list[0]); |
| 501 EXPECT_EQ(10, list[1]); |
| 502 } |
| 503 }; |
| 504 |
| 505 RUN_TEST_IN_MESSAGE_LOOP( |
| 506 DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration) |
| 507 |
| 508 // Tests that resetting an overridden dispatcher, and installing another |
| 509 // overridden dispatcher before the nested message-loop completely unwinds |
| 510 // function correctly. |
| 511 class ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration |
| 512 : public PlatformEventTestWithMessageLoop { |
| 513 public: |
| 514 void NestedTask(scoped_ptr<ScopedEventDispatcher> dispatch_handle, |
| 515 std::vector<int>* list) { |
| 516 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 517 source()->Dispatch(*event); |
| 518 ASSERT_EQ(2u, list->size()); |
| 519 EXPECT_EQ(15, (*list)[0]); |
| 520 EXPECT_EQ(20, (*list)[1]); |
| 521 list->clear(); |
| 522 |
| 523 // Reset the override dispatcher. This should restore the default |
| 524 // dispatcher. |
| 525 dispatch_handle.reset(); |
| 526 source()->Dispatch(*event); |
| 527 ASSERT_EQ(2u, list->size()); |
| 528 EXPECT_EQ(15, (*list)[0]); |
| 529 EXPECT_EQ(10, (*list)[1]); |
| 530 list->clear(); |
| 531 |
| 532 // Install another override-dispatcher. |
| 533 DestroyScopedHandleDispatcher second_overriding(70, list); |
| 534 source()->RemovePlatformEventDispatcher(&second_overriding); |
| 535 scoped_ptr<ScopedEventDispatcher> second_override_handle = |
| 536 source()->OverrideDispatcher(&second_overriding); |
| 537 |
| 538 source()->Dispatch(*event); |
| 539 ASSERT_EQ(2u, list->size()); |
| 540 EXPECT_EQ(15, (*list)[0]); |
| 541 EXPECT_EQ(70, (*list)[1]); |
| 542 list->clear(); |
| 543 |
| 544 second_overriding.SetScopedHandle(second_override_handle.Pass()); |
| 545 second_overriding.set_post_dispatch_action(POST_DISPATCH_QUIT_LOOP); |
| 546 base::RunLoop run_loop; |
| 547 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 548 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
| 549 loop->PostTask( |
| 550 FROM_HERE, |
| 551 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), |
| 552 base::Unretained(source()), |
| 553 *event)); |
| 554 run_loop.Run(); |
| 555 ASSERT_EQ(2u, list->size()); |
| 556 EXPECT_EQ(15, (*list)[0]); |
| 557 EXPECT_EQ(70, (*list)[1]); |
| 558 list->clear(); |
| 559 |
| 560 // Terminate the message-loop. |
| 561 base::MessageLoopForUI::current()->QuitNow(); |
| 562 } |
| 563 |
| 564 // PlatformEventTestWithMessageLoop: |
| 565 virtual void RunTestImpl() OVERRIDE { |
| 566 std::vector<int> list; |
| 567 TestPlatformEventDispatcher dispatcher(10, &list); |
| 568 TestPlatformEventObserver observer(15, &list); |
| 569 |
| 570 TestPlatformEventDispatcher overriding(20, &list); |
| 571 source()->RemovePlatformEventDispatcher(&overriding); |
| 572 scoped_ptr<ScopedEventDispatcher> override_handle = |
| 573 source()->OverrideDispatcher(&overriding); |
| 574 |
| 575 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 576 source()->Dispatch(*event); |
| 577 ASSERT_EQ(2u, list.size()); |
| 578 EXPECT_EQ(15, list[0]); |
| 579 EXPECT_EQ(20, list[1]); |
| 580 list.clear(); |
| 581 |
| 582 // Start a nested message-loop, and destroy |override_handle| in the nested |
| 583 // loop. That should terminate the nested loop, restore the previous |
| 584 // dispatchers, and return control to this function. |
| 585 base::RunLoop run_loop; |
| 586 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 587 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
| 588 loop->PostTask( |
| 589 FROM_HERE, |
| 590 base::Bind( |
| 591 &ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration:: |
| 592 NestedTask, |
| 593 base::Unretained(this), |
| 594 base::Passed(&override_handle), |
| 595 base::Unretained(&list))); |
| 596 run_loop.Run(); |
| 597 |
| 598 // Dispatching the event should now reach the default dispatcher. |
| 599 source()->Dispatch(*event); |
| 600 ASSERT_EQ(2u, list.size()); |
| 601 EXPECT_EQ(15, list[0]); |
| 602 EXPECT_EQ(10, list[1]); |
| 603 } |
| 604 }; |
| 605 |
| 606 RUN_TEST_IN_MESSAGE_LOOP( |
| 607 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
| 608 |
| 609 } // namespace ui |
OLD | NEW |