| 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 <linux/input.h> | 5 #include <linux/input.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 "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/events/event.h" | 12 #include "ui/events/event.h" |
| 13 #include "ui/events/keycodes/keyboard_codes.h" | 13 #include "ui/events/keycodes/keyboard_codes.h" |
| 14 #include "ui/events/ozone/device/device_manager.h" |
| 14 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h" | 15 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h" |
| 15 #include "ui/events/ozone/evdev/event_converter_evdev_impl.h" | 16 #include "ui/events/ozone/evdev/event_converter_evdev_impl.h" |
| 17 #include "ui/events/ozone/evdev/event_factory_evdev.h" |
| 16 #include "ui/events/ozone/evdev/keyboard_evdev.h" | 18 #include "ui/events/ozone/evdev/keyboard_evdev.h" |
| 17 #include "ui/events/ozone/evdev/mouse_button_map_evdev.h" | |
| 18 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" | 19 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" |
| 19 | 20 |
| 20 namespace ui { | 21 namespace ui { |
| 21 | 22 |
| 22 const char kTestDevicePath[] = "/dev/input/test-device"; | 23 const char kTestDevicePath[] = "/dev/input/test-device"; |
| 23 | 24 |
| 24 class MockEventConverterEvdevImpl : public EventConverterEvdevImpl { | 25 class MockEventConverterEvdevImpl : public EventConverterEvdevImpl { |
| 25 public: | 26 public: |
| 26 MockEventConverterEvdevImpl(int fd, | 27 MockEventConverterEvdevImpl( |
| 27 EventModifiersEvdev* modifiers, | 28 int fd, |
| 28 MouseButtonMapEvdev* button_map, | 29 EventModifiersEvdev* modifiers, |
| 29 CursorDelegateEvdev* cursor, | 30 CursorDelegateEvdev* cursor, |
| 30 const KeyEventDispatchCallback& key_callback, | 31 const KeyEventDispatchCallback& key_callback, |
| 31 const EventDispatchCallback& callback) | 32 const MouseMoveEventDispatchCallback& mouse_move_callback, |
| 33 const MouseButtonEventDispatchCallback& mouse_button_callback) |
| 32 : EventConverterEvdevImpl(fd, | 34 : EventConverterEvdevImpl(fd, |
| 33 base::FilePath(kTestDevicePath), | 35 base::FilePath(kTestDevicePath), |
| 34 1, | 36 1, |
| 35 INPUT_DEVICE_UNKNOWN, | 37 INPUT_DEVICE_UNKNOWN, |
| 36 EventDeviceInfo(), | 38 EventDeviceInfo(), |
| 37 modifiers, | 39 modifiers, |
| 38 button_map, | |
| 39 cursor, | 40 cursor, |
| 40 key_callback, | 41 key_callback, |
| 41 callback) { | 42 mouse_move_callback, |
| 43 mouse_button_callback) { |
| 42 Start(); | 44 Start(); |
| 43 } | 45 } |
| 44 ~MockEventConverterEvdevImpl() override {} | 46 ~MockEventConverterEvdevImpl() override {} |
| 45 | 47 |
| 46 private: | 48 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(MockEventConverterEvdevImpl); | 49 DISALLOW_COPY_AND_ASSIGN(MockEventConverterEvdevImpl); |
| 48 }; | 50 }; |
| 49 | 51 |
| 50 class MockCursorEvdev : public CursorDelegateEvdev { | 52 class MockCursorEvdev : public CursorDelegateEvdev { |
| 51 public: | 53 public: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 70 } | 72 } |
| 71 gfx::PointF GetLocation() override { return cursor_location_; } | 73 gfx::PointF GetLocation() override { return cursor_location_; } |
| 72 | 74 |
| 73 private: | 75 private: |
| 74 // The location of the mock cursor. | 76 // The location of the mock cursor. |
| 75 gfx::PointF cursor_location_; | 77 gfx::PointF cursor_location_; |
| 76 | 78 |
| 77 DISALLOW_COPY_AND_ASSIGN(MockCursorEvdev); | 79 DISALLOW_COPY_AND_ASSIGN(MockCursorEvdev); |
| 78 }; | 80 }; |
| 79 | 81 |
| 82 class MockDeviceManager : public ui::DeviceManager { |
| 83 public: |
| 84 MockDeviceManager() {} |
| 85 ~MockDeviceManager() override {} |
| 86 |
| 87 // DeviceManager: |
| 88 void ScanDevices(DeviceEventObserver* observer) override {} |
| 89 void AddObserver(DeviceEventObserver* observer) override {} |
| 90 void RemoveObserver(DeviceEventObserver* observer) override {} |
| 91 }; |
| 92 |
| 93 class TestEventFactoryEvdev : public EventFactoryEvdev { |
| 94 public: |
| 95 TestEventFactoryEvdev(CursorDelegateEvdev* cursor, |
| 96 DeviceManager* device_manager, |
| 97 KeyboardLayoutEngine* keyboard_layout_engine, |
| 98 const EventDispatchCallback& callback) |
| 99 : EventFactoryEvdev(cursor, device_manager, keyboard_layout_engine), |
| 100 callback_(callback) {} |
| 101 ~TestEventFactoryEvdev() override {} |
| 102 |
| 103 private: |
| 104 void PostUiEvent(scoped_ptr<Event> event) override { |
| 105 callback_.Run(event.Pass()); |
| 106 } |
| 107 |
| 108 EventDispatchCallback callback_; |
| 109 }; |
| 110 |
| 80 } // namespace ui | 111 } // namespace ui |
| 81 | 112 |
| 82 // Test fixture. | 113 // Test fixture. |
| 83 class EventConverterEvdevImplTest : public testing::Test { | 114 class EventConverterEvdevImplTest : public testing::Test { |
| 84 public: | 115 public: |
| 85 EventConverterEvdevImplTest() {} | 116 EventConverterEvdevImplTest() {} |
| 86 | 117 |
| 87 // Overridden from testing::Test: | 118 // Overridden from testing::Test: |
| 88 void SetUp() override { | 119 void SetUp() override { |
| 89 // Set up pipe to satisfy message pump (unused). | 120 // Set up pipe to satisfy message pump (unused). |
| 90 int evdev_io[2]; | 121 int evdev_io[2]; |
| 91 if (pipe(evdev_io)) | 122 if (pipe(evdev_io)) |
| 92 PLOG(FATAL) << "failed pipe"; | 123 PLOG(FATAL) << "failed pipe"; |
| 93 events_in_ = evdev_io[0]; | 124 events_in_ = evdev_io[0]; |
| 94 events_out_ = evdev_io[1]; | 125 events_out_ = evdev_io[1]; |
| 95 | 126 |
| 96 cursor_.reset(new ui::MockCursorEvdev()); | 127 cursor_.reset(new ui::MockCursorEvdev()); |
| 97 modifiers_.reset(new ui::EventModifiersEvdev()); | 128 device_manager_.reset(new ui::MockDeviceManager()); |
| 98 button_map_.reset(new ui::MouseButtonMapEvdev()); | 129 event_factory_.reset(new ui::TestEventFactoryEvdev( |
| 130 cursor_.get(), device_manager_.get(), |
| 131 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine(), |
| 132 base::Bind(&EventConverterEvdevImplTest::DispatchEventForTest, |
| 133 base::Unretained(this)))); |
| 99 | 134 |
| 100 const ui::EventDispatchCallback callback = | |
| 101 base::Bind(&EventConverterEvdevImplTest::DispatchEventForTest, | |
| 102 base::Unretained(this)); | |
| 103 const ui::KeyEventDispatchCallback key_callback = | |
| 104 base::Bind(&EventConverterEvdevImplTest::DispatchKeyEventForTest, | |
| 105 base::Unretained(this)); | |
| 106 keyboard_.reset(new ui::KeyboardEvdev( | |
| 107 modifiers_.get(), | |
| 108 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine(), callback)); | |
| 109 device_.reset(new ui::MockEventConverterEvdevImpl( | 135 device_.reset(new ui::MockEventConverterEvdevImpl( |
| 110 events_in_, modifiers_.get(), button_map_.get(), cursor_.get(), | 136 events_in_, event_factory_->modifiers(), cursor_.get(), |
| 111 key_callback, callback)); | 137 base::Bind(&ui::EventFactoryEvdev::PostKeyEvent, |
| 138 base::Unretained(event_factory_.get())), |
| 139 base::Bind(&ui::EventFactoryEvdev::PostMouseMoveEvent, |
| 140 base::Unretained(event_factory_.get())), |
| 141 base::Bind(&ui::EventFactoryEvdev::PostMouseButtonEvent, |
| 142 base::Unretained(event_factory_.get())))); |
| 112 } | 143 } |
| 113 void TearDown() override { | 144 void TearDown() override { |
| 114 device_.reset(); | 145 device_.reset(); |
| 115 keyboard_.reset(); | |
| 116 modifiers_.reset(); | |
| 117 button_map_.reset(); | |
| 118 cursor_.reset(); | 146 cursor_.reset(); |
| 119 close(events_in_); | 147 close(events_in_); |
| 120 close(events_out_); | 148 close(events_out_); |
| 121 } | 149 } |
| 122 | 150 |
| 123 ui::MockCursorEvdev* cursor() { return cursor_.get(); } | 151 ui::MockCursorEvdev* cursor() { return cursor_.get(); } |
| 124 ui::MockEventConverterEvdevImpl* device() { return device_.get(); } | 152 ui::MockEventConverterEvdevImpl* device() { return device_.get(); } |
| 125 ui::EventModifiersEvdev* modifiers() { return modifiers_.get(); } | |
| 126 ui::MouseButtonMapEvdev* button_map() { return button_map_.get(); } | |
| 127 | 153 |
| 128 unsigned size() { return dispatched_events_.size(); } | 154 unsigned size() { return dispatched_events_.size(); } |
| 129 ui::KeyEvent* dispatched_event(unsigned index) { | 155 ui::KeyEvent* dispatched_event(unsigned index) { |
| 130 DCHECK_GT(dispatched_events_.size(), index); | 156 DCHECK_GT(dispatched_events_.size(), index); |
| 131 ui::Event* ev = dispatched_events_[index]; | 157 ui::Event* ev = dispatched_events_[index]; |
| 132 DCHECK(ev->IsKeyEvent()); | 158 DCHECK(ev->IsKeyEvent()); |
| 133 return static_cast<ui::KeyEvent*>(ev); | 159 return static_cast<ui::KeyEvent*>(ev); |
| 134 } | 160 } |
| 135 ui::MouseEvent* dispatched_mouse_event(unsigned index) { | 161 ui::MouseEvent* dispatched_mouse_event(unsigned index) { |
| 136 DCHECK_GT(dispatched_events_.size(), index); | 162 DCHECK_GT(dispatched_events_.size(), index); |
| 137 ui::Event* ev = dispatched_events_[index]; | 163 ui::Event* ev = dispatched_events_[index]; |
| 138 DCHECK(ev->IsMouseEvent()); | 164 DCHECK(ev->IsMouseEvent()); |
| 139 return static_cast<ui::MouseEvent*>(ev); | 165 return static_cast<ui::MouseEvent*>(ev); |
| 140 } | 166 } |
| 141 | 167 |
| 142 private: | 168 private: |
| 143 void DispatchEventForTest(scoped_ptr<ui::Event> event) { | 169 void DispatchEventForTest(scoped_ptr<ui::Event> event) { |
| 144 dispatched_events_.push_back(event.release()); | 170 dispatched_events_.push_back(event.release()); |
| 145 } | 171 } |
| 146 | 172 |
| 147 void DispatchKeyEventForTest(const ui::KeyEventParams& params) { | |
| 148 keyboard_->OnKeyChange(params.code, params.down); | |
| 149 } | |
| 150 | |
| 151 base::MessageLoopForUI ui_loop_; | 173 base::MessageLoopForUI ui_loop_; |
| 152 | 174 |
| 153 scoped_ptr<ui::MockCursorEvdev> cursor_; | 175 scoped_ptr<ui::MockCursorEvdev> cursor_; |
| 154 scoped_ptr<ui::EventModifiersEvdev> modifiers_; | 176 scoped_ptr<ui::DeviceManager> device_manager_; |
| 155 scoped_ptr<ui::MouseButtonMapEvdev> button_map_; | 177 scoped_ptr<ui::EventFactoryEvdev> event_factory_; |
| 156 scoped_ptr<ui::KeyboardEvdev> keyboard_; | |
| 157 scoped_ptr<ui::MockEventConverterEvdevImpl> device_; | 178 scoped_ptr<ui::MockEventConverterEvdevImpl> device_; |
| 158 | 179 |
| 159 ScopedVector<ui::Event> dispatched_events_; | 180 ScopedVector<ui::Event> dispatched_events_; |
| 160 | 181 |
| 161 int events_out_; | 182 int events_out_; |
| 162 int events_in_; | 183 int events_in_; |
| 163 | 184 |
| 164 DISALLOW_COPY_AND_ASSIGN(EventConverterEvdevImplTest); | 185 DISALLOW_COPY_AND_ASSIGN(EventConverterEvdevImplTest); |
| 165 }; | 186 }; |
| 166 | 187 |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 {{0, 0}, EV_KEY, BTN_TOUCH, 1}, | 485 {{0, 0}, EV_KEY, BTN_TOUCH, 1}, |
| 465 {{0, 0}, EV_SYN, SYN_REPORT, 0}, | 486 {{0, 0}, EV_SYN, SYN_REPORT, 0}, |
| 466 | 487 |
| 467 {{0, 0}, EV_KEY, BTN_TOUCH, 0}, | 488 {{0, 0}, EV_KEY, BTN_TOUCH, 0}, |
| 468 {{0, 0}, EV_SYN, SYN_REPORT, 0}, | 489 {{0, 0}, EV_SYN, SYN_REPORT, 0}, |
| 469 }; | 490 }; |
| 470 | 491 |
| 471 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue)); | 492 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue)); |
| 472 EXPECT_EQ(0u, size()); | 493 EXPECT_EQ(0u, size()); |
| 473 } | 494 } |
| OLD | NEW |