| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 stop_stream_ = false; | 58 stop_stream_ = false; |
| 59 for (size_t count = 0; count < events.size(); ++count) { | 59 for (size_t count = 0; count < events.size(); ++count) { |
| 60 DispatchEvent(*events[count]); | 60 DispatchEvent(*events[count]); |
| 61 if (stop_stream_) | 61 if (stop_stream_) |
| 62 return count + 1; | 62 return count + 1; |
| 63 } | 63 } |
| 64 return events.size(); | 64 return events.size(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // PlatformEventSource: | 67 // PlatformEventSource: |
| 68 void StopCurrentEventStream() override { | 68 void StopCurrentEventStream() override { stop_stream_ = true; } |
| 69 stop_stream_ = true; | |
| 70 } | |
| 71 | 69 |
| 72 private: | 70 private: |
| 73 bool stop_stream_; | 71 bool stop_stream_; |
| 74 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); | 72 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); |
| 75 }; | 73 }; |
| 76 | 74 |
| 77 class TestPlatformEventDispatcher : public PlatformEventDispatcher { | 75 class TestPlatformEventDispatcher : public PlatformEventDispatcher { |
| 78 public: | 76 public: |
| 79 TestPlatformEventDispatcher(int id, std::vector<int>* list) | 77 TestPlatformEventDispatcher(int id, std::vector<int>* list) |
| 80 : id_(id), | 78 : id_(id), |
| 81 list_(list), | 79 list_(list), |
| 82 post_dispatch_action_(POST_DISPATCH_NONE), | 80 post_dispatch_action_(POST_DISPATCH_NONE) { |
| 83 stop_stream_(false) { | |
| 84 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 81 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); |
| 85 } | 82 } |
| 86 ~TestPlatformEventDispatcher() override { | 83 ~TestPlatformEventDispatcher() override { |
| 87 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 84 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); |
| 88 } | 85 } |
| 89 | 86 |
| 90 void set_post_dispatch_action(uint32_t action) { | 87 void set_post_dispatch_action(uint32_t action) { |
| 91 post_dispatch_action_ = action; | 88 post_dispatch_action_ = action; |
| 92 } | 89 } |
| 93 | 90 |
| 94 protected: | 91 protected: |
| 95 // PlatformEventDispatcher: | 92 // PlatformEventDispatcher: |
| 96 bool CanDispatchEvent(const PlatformEvent& event) override { | 93 bool CanDispatchEvent(const PlatformEvent& event) override { return true; } |
| 97 return true; | |
| 98 } | |
| 99 | 94 |
| 100 uint32_t DispatchEvent(const PlatformEvent& event) override { | 95 uint32_t DispatchEvent(const PlatformEvent& event) override { |
| 101 list_->push_back(id_); | 96 list_->push_back(id_); |
| 102 return post_dispatch_action_; | 97 return post_dispatch_action_; |
| 103 } | 98 } |
| 104 | 99 |
| 105 private: | 100 private: |
| 106 int id_; | 101 int id_; |
| 107 std::vector<int>* list_; | 102 std::vector<int>* list_; |
| 108 uint32_t post_dispatch_action_; | 103 uint32_t post_dispatch_action_; |
| 109 bool stop_stream_; | |
| 110 | 104 |
| 111 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); | 105 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventDispatcher); |
| 112 }; | 106 }; |
| 113 | 107 |
| 114 class TestPlatformEventObserver : public PlatformEventObserver { | 108 class TestPlatformEventObserver : public PlatformEventObserver { |
| 115 public: | 109 public: |
| 116 TestPlatformEventObserver(int id, std::vector<int>* list) | 110 TestPlatformEventObserver(int id, std::vector<int>* list) |
| 117 : id_(id), list_(list) { | 111 : id_(id), list_(list) { |
| 118 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); | 112 PlatformEventSource::GetInstance()->AddPlatformEventObserver(this); |
| 119 } | 113 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 138 | 132 |
| 139 class PlatformEventTest : public testing::Test { | 133 class PlatformEventTest : public testing::Test { |
| 140 public: | 134 public: |
| 141 PlatformEventTest() {} | 135 PlatformEventTest() {} |
| 142 ~PlatformEventTest() override {} | 136 ~PlatformEventTest() override {} |
| 143 | 137 |
| 144 TestPlatformEventSource* source() { return source_.get(); } | 138 TestPlatformEventSource* source() { return source_.get(); } |
| 145 | 139 |
| 146 protected: | 140 protected: |
| 147 // testing::Test: | 141 // testing::Test: |
| 148 void SetUp() override { | 142 void SetUp() override { source_.reset(new TestPlatformEventSource()); } |
| 149 source_.reset(new TestPlatformEventSource()); | |
| 150 } | |
| 151 | 143 |
| 152 private: | 144 private: |
| 153 scoped_ptr<TestPlatformEventSource> source_; | 145 scoped_ptr<TestPlatformEventSource> source_; |
| 154 | 146 |
| 155 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest); | 147 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest); |
| 156 }; | 148 }; |
| 157 | 149 |
| 158 // Tests that a dispatcher receives an event. | 150 // Tests that a dispatcher receives an event. |
| 159 TEST_F(PlatformEventTest, DispatcherBasic) { | 151 TEST_F(PlatformEventTest, DispatcherBasic) { |
| 160 std::vector<int> list_dispatcher; | 152 std::vector<int> list_dispatcher; |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { | 566 void SetScopedHandle(scoped_ptr<ScopedEventDispatcher> handler) { |
| 575 handler_ = handler.Pass(); | 567 handler_ = handler.Pass(); |
| 576 } | 568 } |
| 577 | 569 |
| 578 void set_callback(const base::Closure& callback) { | 570 void set_callback(const base::Closure& callback) { |
| 579 callback_ = callback; | 571 callback_ = callback; |
| 580 } | 572 } |
| 581 | 573 |
| 582 private: | 574 private: |
| 583 // PlatformEventDispatcher: | 575 // PlatformEventDispatcher: |
| 584 bool CanDispatchEvent(const PlatformEvent& event) override { | 576 bool CanDispatchEvent(const PlatformEvent& event) override { return true; } |
| 585 return true; | |
| 586 } | |
| 587 | 577 |
| 588 uint32_t DispatchEvent(const PlatformEvent& event) override { | 578 uint32_t DispatchEvent(const PlatformEvent& event) override { |
| 589 handler_.reset(); | 579 handler_.reset(); |
| 590 uint32_t action = TestPlatformEventDispatcher::DispatchEvent(event); | 580 uint32_t action = TestPlatformEventDispatcher::DispatchEvent(event); |
| 591 if (!callback_.is_null()) { | 581 if (!callback_.is_null()) { |
| 592 callback_.Run(); | 582 callback_.Run(); |
| 593 callback_ = base::Closure(); | 583 callback_ = base::Closure(); |
| 594 } | 584 } |
| 595 return action; | 585 return action; |
| 596 } | 586 } |
| 597 | 587 |
| 598 scoped_ptr<ScopedEventDispatcher> handler_; | 588 scoped_ptr<ScopedEventDispatcher> handler_; |
| 599 base::Closure callback_; | 589 base::Closure callback_; |
| 600 | 590 |
| 601 DISALLOW_COPY_AND_ASSIGN(DestroyScopedHandleDispatcher); | 591 DISALLOW_COPY_AND_ASSIGN(DestroyScopedHandleDispatcher); |
| 602 }; | 592 }; |
| 603 | 593 |
| 604 // Tests that resetting an overridden dispatcher causes the nested message-loop | 594 // Tests that resetting an overridden dispatcher causes the nested message-loop |
| 605 // iteration to stop and the rest of the events are dispatched in the next | 595 // iteration to stop and the rest of the events are dispatched in the next |
| 606 // iteration. | 596 // iteration. |
| 607 class DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration | 597 class DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration |
| 608 : public PlatformEventTestWithMessageLoop { | 598 : public PlatformEventTestWithMessageLoop { |
| 609 public: | 599 public: |
| 610 void NestedTask(std::vector<int>* list, | 600 void NestedTask(std::vector<int>* list, |
| 611 TestPlatformEventDispatcher* dispatcher) { | 601 TestPlatformEventDispatcher* dispatcher) { |
| 612 ScopedVector<PlatformEvent> events; | 602 ScopedVector<PlatformEvent> events; |
| 613 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 603 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 614 events.push_back(event.release()); | 604 events.push_back(event.Pass()); |
| 615 event = CreatePlatformEvent(); | 605 event = CreatePlatformEvent(); |
| 616 events.push_back(event.release()); | 606 events.push_back(event.Pass()); |
| 617 | 607 |
| 618 // Attempt to dispatch a couple of events. Dispatching the first event will | 608 // Attempt to dispatch a couple of events. Dispatching the first event will |
| 619 // have terminated the ScopedEventDispatcher object, which will terminate | 609 // have terminated the ScopedEventDispatcher object, which will terminate |
| 620 // the current iteration of the message-loop. | 610 // the current iteration of the message-loop. |
| 621 size_t count = source()->DispatchEventStream(events); | 611 size_t count = source()->DispatchEventStream(events); |
| 622 EXPECT_EQ(1u, count); | 612 EXPECT_EQ(1u, count); |
| 623 ASSERT_EQ(2u, list->size()); | 613 ASSERT_EQ(2u, list->size()); |
| 624 EXPECT_EQ(15, (*list)[0]); | 614 EXPECT_EQ(15, (*list)[0]); |
| 625 EXPECT_EQ(20, (*list)[1]); | 615 EXPECT_EQ(20, (*list)[1]); |
| 626 list->clear(); | 616 list->clear(); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 ASSERT_EQ(2u, list.size()); | 768 ASSERT_EQ(2u, list.size()); |
| 779 EXPECT_EQ(15, list[0]); | 769 EXPECT_EQ(15, list[0]); |
| 780 EXPECT_EQ(10, list[1]); | 770 EXPECT_EQ(10, list[1]); |
| 781 } | 771 } |
| 782 }; | 772 }; |
| 783 | 773 |
| 784 RUN_TEST_IN_MESSAGE_LOOP( | 774 RUN_TEST_IN_MESSAGE_LOOP( |
| 785 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) | 775 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
| 786 | 776 |
| 787 } // namespace ui | 777 } // namespace ui |
| OLD | NEW |