| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "ui/events/platform/platform_event_source.h" | 5 #include "ui/events/platform/platform_event_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 private: | 72 private: |
| 73 bool stop_stream_; | 73 bool stop_stream_; |
| 74 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); | 74 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 class TestPlatformEventDispatcher : public PlatformEventDispatcher { | 77 class TestPlatformEventDispatcher : public PlatformEventDispatcher { |
| 78 public: | 78 public: |
| 79 TestPlatformEventDispatcher(int id, std::vector<int>* list) | 79 TestPlatformEventDispatcher(int id, std::vector<int>* list) |
| 80 : id_(id), | 80 : id_(id), |
| 81 list_(list), | 81 list_(list), |
| 82 post_dispatch_action_(POST_DISPATCH_NONE), | 82 post_dispatch_action_(kPostDispatchNone), |
| 83 stop_stream_(false) { | 83 stop_stream_(false) { |
| 84 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 84 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); |
| 85 } | 85 } |
| 86 virtual ~TestPlatformEventDispatcher() { | 86 virtual ~TestPlatformEventDispatcher() { |
| 87 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 87 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void set_post_dispatch_action(uint32_t action) { | 90 void set_post_dispatch_action(uint32_t action) { |
| 91 post_dispatch_action_ = action; | 91 post_dispatch_action_ = action; |
| 92 } | 92 } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 TestPlatformEventDispatcher first(12, &list_dispatcher); | 199 TestPlatformEventDispatcher first(12, &list_dispatcher); |
| 200 TestPlatformEventDispatcher second(23, &list_dispatcher); | 200 TestPlatformEventDispatcher second(23, &list_dispatcher); |
| 201 | 201 |
| 202 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 202 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 203 source()->Dispatch(*event); | 203 source()->Dispatch(*event); |
| 204 ASSERT_EQ(2u, list_dispatcher.size()); | 204 ASSERT_EQ(2u, list_dispatcher.size()); |
| 205 EXPECT_EQ(12, list_dispatcher[0]); | 205 EXPECT_EQ(12, list_dispatcher[0]); |
| 206 EXPECT_EQ(23, list_dispatcher[1]); | 206 EXPECT_EQ(23, list_dispatcher[1]); |
| 207 list_dispatcher.clear(); | 207 list_dispatcher.clear(); |
| 208 | 208 |
| 209 first.set_post_dispatch_action(POST_DISPATCH_STOP_PROPAGATION); | 209 first.set_post_dispatch_action(kPostDispatchStopPropagation); |
| 210 event = CreatePlatformEvent(); | 210 event = CreatePlatformEvent(); |
| 211 source()->Dispatch(*event); | 211 source()->Dispatch(*event); |
| 212 ASSERT_EQ(1u, list_dispatcher.size()); | 212 ASSERT_EQ(1u, list_dispatcher.size()); |
| 213 EXPECT_EQ(12, list_dispatcher[0]); | 213 EXPECT_EQ(12, list_dispatcher[0]); |
| 214 } | 214 } |
| 215 | 215 |
| 216 // Tests that observers receive events. | 216 // Tests that observers receive events. |
| 217 TEST_F(PlatformEventTest, ObserverBasic) { | 217 TEST_F(PlatformEventTest, ObserverBasic) { |
| 218 std::vector<int> list_observer; | 218 std::vector<int> list_observer; |
| 219 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 219 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 // Tests that an overridden dispatcher can request that the default dispatchers | 289 // Tests that an overridden dispatcher can request that the default dispatchers |
| 290 // can dispatch the events. | 290 // can dispatch the events. |
| 291 TEST_F(PlatformEventTest, OverriddenDispatcherInvokeDefaultDispatcher) { | 291 TEST_F(PlatformEventTest, OverriddenDispatcherInvokeDefaultDispatcher) { |
| 292 std::vector<int> list; | 292 std::vector<int> list; |
| 293 TestPlatformEventDispatcher dispatcher(10, &list); | 293 TestPlatformEventDispatcher dispatcher(10, &list); |
| 294 TestPlatformEventObserver observer(15, &list); | 294 TestPlatformEventObserver observer(15, &list); |
| 295 TestPlatformEventDispatcher overriding_dispatcher(20, &list); | 295 TestPlatformEventDispatcher overriding_dispatcher(20, &list); |
| 296 source()->RemovePlatformEventDispatcher(&overriding_dispatcher); | 296 source()->RemovePlatformEventDispatcher(&overriding_dispatcher); |
| 297 scoped_ptr<ScopedEventDispatcher> handle = | 297 scoped_ptr<ScopedEventDispatcher> handle = |
| 298 source()->OverrideDispatcher(&overriding_dispatcher); | 298 source()->OverrideDispatcher(&overriding_dispatcher); |
| 299 overriding_dispatcher.set_post_dispatch_action(POST_DISPATCH_PERFORM_DEFAULT); | 299 overriding_dispatcher.set_post_dispatch_action(kPostDispatchPerformDefault); |
| 300 | 300 |
| 301 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 301 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 302 source()->Dispatch(*event); | 302 source()->Dispatch(*event); |
| 303 // First the observer, then the overriding dispatcher, then the default | 303 // First the observer, then the overriding dispatcher, then the default |
| 304 // dispatcher. | 304 // dispatcher. |
| 305 ASSERT_EQ(3u, list.size()); | 305 ASSERT_EQ(3u, list.size()); |
| 306 EXPECT_EQ(15, list[0]); | 306 EXPECT_EQ(15, list[0]); |
| 307 EXPECT_EQ(20, list[1]); | 307 EXPECT_EQ(20, list[1]); |
| 308 EXPECT_EQ(10, list[2]); | 308 EXPECT_EQ(10, list[2]); |
| 309 list.clear(); | 309 list.clear(); |
| 310 | 310 |
| 311 // Install a second overriding dispatcher. | 311 // Install a second overriding dispatcher. |
| 312 TestPlatformEventDispatcher second_overriding(50, &list); | 312 TestPlatformEventDispatcher second_overriding(50, &list); |
| 313 source()->RemovePlatformEventDispatcher(&second_overriding); | 313 source()->RemovePlatformEventDispatcher(&second_overriding); |
| 314 scoped_ptr<ScopedEventDispatcher> second_override_handle = | 314 scoped_ptr<ScopedEventDispatcher> second_override_handle = |
| 315 source()->OverrideDispatcher(&second_overriding); | 315 source()->OverrideDispatcher(&second_overriding); |
| 316 source()->Dispatch(*event); | 316 source()->Dispatch(*event); |
| 317 ASSERT_EQ(2u, list.size()); | 317 ASSERT_EQ(2u, list.size()); |
| 318 EXPECT_EQ(15, list[0]); | 318 EXPECT_EQ(15, list[0]); |
| 319 EXPECT_EQ(50, list[1]); | 319 EXPECT_EQ(50, list[1]); |
| 320 list.clear(); | 320 list.clear(); |
| 321 | 321 |
| 322 second_overriding.set_post_dispatch_action(POST_DISPATCH_PERFORM_DEFAULT); | 322 second_overriding.set_post_dispatch_action(kPostDispatchPerformDefault); |
| 323 source()->Dispatch(*event); | 323 source()->Dispatch(*event); |
| 324 // First the observer, then the second overriding dispatcher, then the default | 324 // First the observer, then the second overriding dispatcher, then the default |
| 325 // dispatcher. | 325 // dispatcher. |
| 326 ASSERT_EQ(3u, list.size()); | 326 ASSERT_EQ(3u, list.size()); |
| 327 EXPECT_EQ(15, list[0]); | 327 EXPECT_EQ(15, list[0]); |
| 328 EXPECT_EQ(50, list[1]); | 328 EXPECT_EQ(50, list[1]); |
| 329 EXPECT_EQ(10, list[2]); | 329 EXPECT_EQ(10, list[2]); |
| 330 } | 330 } |
| 331 | 331 |
| 332 // Runs a callback during an event dispatch. | 332 // Runs a callback during an event dispatch. |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 712 scoped_ptr<ScopedEventDispatcher> second_override_handle = | 712 scoped_ptr<ScopedEventDispatcher> second_override_handle = |
| 713 source()->OverrideDispatcher(&second_overriding); | 713 source()->OverrideDispatcher(&second_overriding); |
| 714 | 714 |
| 715 source()->Dispatch(*event); | 715 source()->Dispatch(*event); |
| 716 ASSERT_EQ(2u, list->size()); | 716 ASSERT_EQ(2u, list->size()); |
| 717 EXPECT_EQ(15, (*list)[0]); | 717 EXPECT_EQ(15, (*list)[0]); |
| 718 EXPECT_EQ(70, (*list)[1]); | 718 EXPECT_EQ(70, (*list)[1]); |
| 719 list->clear(); | 719 list->clear(); |
| 720 | 720 |
| 721 second_overriding.SetScopedHandle(second_override_handle.Pass()); | 721 second_overriding.SetScopedHandle(second_override_handle.Pass()); |
| 722 second_overriding.set_post_dispatch_action(POST_DISPATCH_NONE); | 722 second_overriding.set_post_dispatch_action(kPostDispatchNone); |
| 723 base::RunLoop run_loop; | 723 base::RunLoop run_loop; |
| 724 second_overriding.set_callback(run_loop.QuitClosure()); | 724 second_overriding.set_callback(run_loop.QuitClosure()); |
| 725 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 725 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 726 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); | 726 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); |
| 727 loop->PostTask( | 727 loop->PostTask( |
| 728 FROM_HERE, | 728 FROM_HERE, |
| 729 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), | 729 base::Bind(base::IgnoreResult(&TestPlatformEventSource::Dispatch), |
| 730 base::Unretained(source()), | 730 base::Unretained(source()), |
| 731 *event)); | 731 *event)); |
| 732 run_loop.Run(); | 732 run_loop.Run(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 ASSERT_EQ(2u, list.size()); | 778 ASSERT_EQ(2u, list.size()); |
| 779 EXPECT_EQ(15, list[0]); | 779 EXPECT_EQ(15, list[0]); |
| 780 EXPECT_EQ(10, list[1]); | 780 EXPECT_EQ(10, list[1]); |
| 781 } | 781 } |
| 782 }; | 782 }; |
| 783 | 783 |
| 784 RUN_TEST_IN_MESSAGE_LOOP( | 784 RUN_TEST_IN_MESSAGE_LOOP( |
| 785 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) | 785 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
| 786 | 786 |
| 787 } // namespace ui | 787 } // namespace ui |
| OLD | NEW |