| 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" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "ui/events/platform/platform_event_dispatcher.h" | 13 #include "ui/events/platform/platform_event_dispatcher.h" |
| 14 #include "ui/events/platform/platform_event_filter.h" |
| 14 #include "ui/events/platform/platform_event_observer.h" | 15 #include "ui/events/platform/platform_event_observer.h" |
| 15 #include "ui/events/platform/scoped_event_dispatcher.h" | 16 #include "ui/events/platform/scoped_event_dispatcher.h" |
| 16 | 17 |
| 17 namespace ui { | 18 namespace ui { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 scoped_ptr<PlatformEvent> CreatePlatformEvent() { | 22 scoped_ptr<PlatformEvent> CreatePlatformEvent() { |
| 22 scoped_ptr<PlatformEvent> event(new PlatformEvent()); | 23 scoped_ptr<PlatformEvent> event(new PlatformEvent()); |
| 23 memset(event.get(), 0, sizeof(PlatformEvent)); | 24 memset(event.get(), 0, sizeof(PlatformEvent)); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 130 |
| 130 virtual void DidProcessEvent(const PlatformEvent& event) OVERRIDE {} | 131 virtual void DidProcessEvent(const PlatformEvent& event) OVERRIDE {} |
| 131 | 132 |
| 132 private: | 133 private: |
| 133 int id_; | 134 int id_; |
| 134 std::vector<int>* list_; | 135 std::vector<int>* list_; |
| 135 | 136 |
| 136 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventObserver); | 137 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventObserver); |
| 137 }; | 138 }; |
| 138 | 139 |
| 140 class TestPlatformEventFilter : public PlatformEventFilter { |
| 141 public: |
| 142 TestPlatformEventFilter() { |
| 143 PlatformEventSource::GetInstance()->AddPlatformEventFilter(this); |
| 144 } |
| 145 |
| 146 virtual ~TestPlatformEventFilter() { |
| 147 PlatformEventSource::GetInstance()->RemovePlatformEventFilter(this); |
| 148 } |
| 149 |
| 150 virtual bool ShouldDispatchEvent(const PlatformEvent& event) OVERRIDE { |
| 151 return false; |
| 152 } |
| 153 |
| 154 private: |
| 155 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventFilter); |
| 156 }; |
| 157 |
| 139 class PlatformEventTest : public testing::Test { | 158 class PlatformEventTest : public testing::Test { |
| 140 public: | 159 public: |
| 141 PlatformEventTest() {} | 160 PlatformEventTest() {} |
| 142 virtual ~PlatformEventTest() {} | 161 virtual ~PlatformEventTest() {} |
| 143 | 162 |
| 144 TestPlatformEventSource* source() { return source_.get(); } | 163 TestPlatformEventSource* source() { return source_.get(); } |
| 145 | 164 |
| 146 protected: | 165 protected: |
| 147 // testing::Test: | 166 // testing::Test: |
| 148 virtual void SetUp() OVERRIDE { | 167 virtual void SetUp() OVERRIDE { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 ASSERT_EQ(1u, list_observer.size()); | 246 ASSERT_EQ(1u, list_observer.size()); |
| 228 EXPECT_EQ(31, list_observer[0]); | 247 EXPECT_EQ(31, list_observer[0]); |
| 229 } | 248 } |
| 230 | 249 |
| 231 list_observer.clear(); | 250 list_observer.clear(); |
| 232 event = CreatePlatformEvent(); | 251 event = CreatePlatformEvent(); |
| 233 source()->Dispatch(*event); | 252 source()->Dispatch(*event); |
| 234 EXPECT_EQ(0u, list_observer.size()); | 253 EXPECT_EQ(0u, list_observer.size()); |
| 235 } | 254 } |
| 236 | 255 |
| 256 // Tests that filtered events are not observed or dispatched. |
| 257 TEST_F(PlatformEventTest, FilterBasic) { |
| 258 std::vector<int> list; |
| 259 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 260 TestPlatformEventDispatcher dispatcher(1, &list); |
| 261 TestPlatformEventObserver observer(2, &list); |
| 262 source()->Dispatch(*event); |
| 263 EXPECT_EQ(2u, list.size()); |
| 264 list.clear(); |
| 265 { |
| 266 TestPlatformEventFilter filter; |
| 267 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| 268 source()->Dispatch(*event); |
| 269 ASSERT_EQ(0u, list.size()); |
| 270 list.clear(); |
| 271 } |
| 272 |
| 273 list.clear(); |
| 274 event = CreatePlatformEvent(); |
| 275 source()->Dispatch(*event); |
| 276 EXPECT_EQ(2u, list.size()); |
| 277 } |
| 278 |
| 237 // Tests that observers receive events in the correct order. | 279 // Tests that observers receive events in the correct order. |
| 238 TEST_F(PlatformEventTest, ObserverOrder) { | 280 TEST_F(PlatformEventTest, ObserverOrder) { |
| 239 std::vector<int> list_observer; | 281 std::vector<int> list_observer; |
| 240 const int sequence[] = {21, 3, 6, 45}; | 282 const int sequence[] = {21, 3, 6, 45}; |
| 241 ScopedVector<TestPlatformEventObserver> observers; | 283 ScopedVector<TestPlatformEventObserver> observers; |
| 242 for (size_t i = 0; i < arraysize(sequence); ++i) { | 284 for (size_t i = 0; i < arraysize(sequence); ++i) { |
| 243 observers.push_back( | 285 observers.push_back( |
| 244 new TestPlatformEventObserver(sequence[i], &list_observer)); | 286 new TestPlatformEventObserver(sequence[i], &list_observer)); |
| 245 } | 287 } |
| 246 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); | 288 scoped_ptr<PlatformEvent> event(CreatePlatformEvent()); |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 ASSERT_EQ(2u, list.size()); | 820 ASSERT_EQ(2u, list.size()); |
| 779 EXPECT_EQ(15, list[0]); | 821 EXPECT_EQ(15, list[0]); |
| 780 EXPECT_EQ(10, list[1]); | 822 EXPECT_EQ(10, list[1]); |
| 781 } | 823 } |
| 782 }; | 824 }; |
| 783 | 825 |
| 784 RUN_TEST_IN_MESSAGE_LOOP( | 826 RUN_TEST_IN_MESSAGE_LOOP( |
| 785 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) | 827 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) |
| 786 | 828 |
| 787 } // namespace ui | 829 } // namespace ui |
| OLD | NEW |