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 <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <linux/input.h> | 7 #include <linux/input.h> |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
15 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "ui/events/devices/device_data_manager.h" | 20 #include "ui/events/devices/device_data_manager.h" |
| 21 #include "ui/events/event_switches.h" |
20 #include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h" | 22 #include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h" |
| 23 #include "ui/events/ozone/evdev/touch_evdev_types.h" |
21 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" | 24 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" |
| 25 #include "ui/events/ozone/evdev/touch_noise/touch_noise_filter.h" |
| 26 #include "ui/events/ozone/evdev/touch_noise/touch_noise_finder.h" |
22 #include "ui/events/platform/platform_event_dispatcher.h" | 27 #include "ui/events/platform/platform_event_dispatcher.h" |
23 #include "ui/events/platform/platform_event_source.h" | 28 #include "ui/events/platform/platform_event_source.h" |
24 | 29 |
| 30 namespace ui { |
| 31 |
25 namespace { | 32 namespace { |
26 | 33 |
27 static int SetNonBlocking(int fd) { | 34 static int SetNonBlocking(int fd) { |
28 int flags = fcntl(fd, F_GETFL, 0); | 35 int flags = fcntl(fd, F_GETFL, 0); |
29 if (flags == -1) | 36 if (flags == -1) |
30 flags = 0; | 37 flags = 0; |
31 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); | 38 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
32 } | 39 } |
33 | 40 |
34 const char kTestDevicePath[] = "/dev/input/test-device"; | 41 const char kTestDevicePath[] = "/dev/input/test-device"; |
35 | 42 |
36 } // namespace | 43 } // namespace |
37 | 44 |
38 namespace ui { | |
39 | |
40 class MockTouchEventConverterEvdev : public TouchEventConverterEvdev { | 45 class MockTouchEventConverterEvdev : public TouchEventConverterEvdev { |
41 public: | 46 public: |
42 MockTouchEventConverterEvdev(int fd, | 47 MockTouchEventConverterEvdev(int fd, |
43 base::FilePath path, | 48 base::FilePath path, |
44 DeviceEventDispatcherEvdev* dispatcher); | 49 DeviceEventDispatcherEvdev* dispatcher); |
45 ~MockTouchEventConverterEvdev() override {} | 50 ~MockTouchEventConverterEvdev() override {} |
46 | 51 |
47 void ConfigureReadMock(struct input_event* queue, | 52 void ConfigureReadMock(struct input_event* queue, |
48 long read_this_many, | 53 long read_this_many, |
49 long queue_index); | 54 long queue_index); |
50 | 55 |
51 // Actually dispatch the event reader code. | 56 // Actually dispatch the event reader code. |
52 void ReadNow() { | 57 void ReadNow() { |
53 OnFileCanReadWithoutBlocking(read_pipe_); | 58 OnFileCanReadWithoutBlocking(read_pipe_); |
54 base::RunLoop().RunUntilIdle(); | 59 base::RunLoop().RunUntilIdle(); |
55 } | 60 } |
56 | 61 |
57 void Initialize(const EventDeviceInfo& device_info) override {} | 62 void Initialize(const EventDeviceInfo& device_info) override {} |
58 bool Reinitialize() override { return true; } | 63 bool Reinitialize() override { return true; } |
59 | 64 |
| 65 TouchNoiseFinder* touch_noise_finder() { return touch_noise_finder_.get(); } |
| 66 |
60 private: | 67 private: |
61 int read_pipe_; | 68 int read_pipe_; |
62 int write_pipe_; | 69 int write_pipe_; |
63 | 70 |
64 DISALLOW_COPY_AND_ASSIGN(MockTouchEventConverterEvdev); | 71 DISALLOW_COPY_AND_ASSIGN(MockTouchEventConverterEvdev); |
65 }; | 72 }; |
66 | 73 |
67 class MockDeviceEventDispatcherEvdev : public DeviceEventDispatcherEvdev { | 74 class MockDeviceEventDispatcherEvdev : public DeviceEventDispatcherEvdev { |
68 public: | 75 public: |
69 MockDeviceEventDispatcherEvdev( | 76 MockDeviceEventDispatcherEvdev( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 if (pipe(fds)) | 121 if (pipe(fds)) |
115 PLOG(FATAL) << "failed pipe"; | 122 PLOG(FATAL) << "failed pipe"; |
116 | 123 |
117 DCHECK(SetNonBlocking(fds[0]) == 0) | 124 DCHECK(SetNonBlocking(fds[0]) == 0) |
118 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; | 125 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; |
119 DCHECK(SetNonBlocking(fds[1]) == 0) | 126 DCHECK(SetNonBlocking(fds[1]) == 0) |
120 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; | 127 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; |
121 read_pipe_ = fds[0]; | 128 read_pipe_ = fds[0]; |
122 write_pipe_ = fds[1]; | 129 write_pipe_ = fds[1]; |
123 | 130 |
124 events_.resize(MAX_FINGERS); | 131 events_.resize(ui::kNumTouchEvdevSlots); |
| 132 for (size_t i = 0; i < events_.size(); ++i) |
| 133 events_[i].slot = i; |
125 } | 134 } |
126 | 135 |
127 void MockTouchEventConverterEvdev::ConfigureReadMock(struct input_event* queue, | 136 void MockTouchEventConverterEvdev::ConfigureReadMock(struct input_event* queue, |
128 long read_this_many, | 137 long read_this_many, |
129 long queue_index) { | 138 long queue_index) { |
130 int nwrite = HANDLE_EINTR(write(write_pipe_, | 139 int nwrite = HANDLE_EINTR(write(write_pipe_, |
131 queue + queue_index, | 140 queue + queue_index, |
132 sizeof(struct input_event) * read_this_many)); | 141 sizeof(struct input_event) * read_this_many)); |
133 DCHECK(nwrite == | 142 DCHECK(nwrite == |
134 static_cast<int>(sizeof(struct input_event) * read_this_many)) | 143 static_cast<int>(sizeof(struct input_event) * read_this_many)) |
135 << "write() failed, errno: " << errno; | 144 << "write() failed, errno: " << errno; |
136 } | 145 } |
137 | 146 |
138 } // namespace ui | |
139 | |
140 // Test fixture. | 147 // Test fixture. |
141 class TouchEventConverterEvdevTest : public testing::Test { | 148 class TouchEventConverterEvdevTest : public testing::Test { |
142 public: | 149 public: |
143 TouchEventConverterEvdevTest() {} | 150 TouchEventConverterEvdevTest() {} |
144 | 151 |
145 // Overridden from testing::Test: | 152 // Overridden from testing::Test: |
146 void SetUp() override { | 153 void SetUp() override { |
147 // Set up pipe to satisfy message pump (unused). | 154 // Set up pipe to satisfy message pump (unused). |
148 int evdev_io[2]; | 155 int evdev_io[2]; |
149 if (pipe(evdev_io)) | 156 if (pipe(evdev_io)) |
(...skipping 20 matching lines...) Expand all Loading... |
170 } | 177 } |
171 | 178 |
172 ui::MockTouchEventConverterEvdev* device() { return device_; } | 179 ui::MockTouchEventConverterEvdev* device() { return device_; } |
173 | 180 |
174 unsigned size() { return dispatched_events_.size(); } | 181 unsigned size() { return dispatched_events_.size(); } |
175 const ui::TouchEventParams& dispatched_event(unsigned index) { | 182 const ui::TouchEventParams& dispatched_event(unsigned index) { |
176 DCHECK_GT(dispatched_events_.size(), index); | 183 DCHECK_GT(dispatched_events_.size(), index); |
177 return dispatched_events_[index]; | 184 return dispatched_events_[index]; |
178 } | 185 } |
179 | 186 |
| 187 void ClearDispatchedEvents() { |
| 188 dispatched_events_.clear(); |
| 189 } |
| 190 |
180 private: | 191 private: |
181 base::MessageLoop* loop_; | 192 base::MessageLoop* loop_; |
182 ui::MockTouchEventConverterEvdev* device_; | 193 ui::MockTouchEventConverterEvdev* device_; |
183 scoped_ptr<ui::MockDeviceEventDispatcherEvdev> dispatcher_; | 194 scoped_ptr<ui::MockDeviceEventDispatcherEvdev> dispatcher_; |
184 | 195 |
185 int events_out_; | 196 int events_out_; |
186 int events_in_; | 197 int events_in_; |
187 | 198 |
188 void DispatchCallback(const ui::TouchEventParams& params) { | 199 void DispatchCallback(const ui::TouchEventParams& params) { |
189 dispatched_events_.push_back(params); | 200 dispatched_events_.push_back(params); |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 | 565 |
555 // crbug.com/446939 | 566 // crbug.com/446939 |
556 TEST_F(TouchEventConverterEvdevTest, CheckSlotLimit) { | 567 TEST_F(TouchEventConverterEvdevTest, CheckSlotLimit) { |
557 ui::MockTouchEventConverterEvdev* dev = device(); | 568 ui::MockTouchEventConverterEvdev* dev = device(); |
558 | 569 |
559 struct input_event mock_kernel_queue[] = { | 570 struct input_event mock_kernel_queue[] = { |
560 {{0, 0}, EV_ABS, ABS_MT_SLOT, 0}, | 571 {{0, 0}, EV_ABS, ABS_MT_SLOT, 0}, |
561 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 100}, | 572 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 100}, |
562 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 999}, | 573 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 999}, |
563 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 888}, | 574 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 888}, |
564 {{0, 0}, EV_ABS, ABS_MT_SLOT, ui::TouchEventConverterEvdev::MAX_FINGERS}, | 575 {{0, 0}, EV_ABS, ABS_MT_SLOT, ui::kNumTouchEvdevSlots}, |
565 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 200}, | 576 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 200}, |
566 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 777}, | 577 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 777}, |
567 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 666}, | 578 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 666}, |
568 {{0, 0}, EV_SYN, SYN_REPORT, 0}, | 579 {{0, 0}, EV_SYN, SYN_REPORT, 0}, |
569 }; | 580 }; |
570 | 581 |
571 // Check that one 1 event is generated | 582 // Check that one 1 event is generated |
572 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0); | 583 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0); |
573 dev->ReadNow(); | 584 dev->ReadNow(); |
574 EXPECT_EQ(1u, size()); | 585 EXPECT_EQ(1u, size()); |
575 } | 586 } |
| 587 |
| 588 namespace { |
| 589 |
| 590 // TouchNoiseFilter which considers all events of type |noise_event_type| as |
| 591 // noise. |
| 592 class EventTypeTouchNoiseFilter : public TouchNoiseFilter { |
| 593 public: |
| 594 explicit EventTypeTouchNoiseFilter(EventType noise_event_type) |
| 595 : noise_event_type_(noise_event_type) {} |
| 596 ~EventTypeTouchNoiseFilter() override {} |
| 597 |
| 598 // TouchNoiseFilter: |
| 599 void Filter(const std::vector<InProgressTouchEvdev>& touches, |
| 600 base::TimeDelta time, |
| 601 std::bitset<kNumTouchEvdevSlots>* slots_with_noise) override { |
| 602 for (const InProgressTouchEvdev& touch : touches) { |
| 603 if (touch.type == noise_event_type_) |
| 604 slots_with_noise->set(touch.slot); |
| 605 } |
| 606 } |
| 607 |
| 608 private: |
| 609 EventType noise_event_type_; |
| 610 |
| 611 DISALLOW_COPY_AND_ASSIGN(EventTypeTouchNoiseFilter); |
| 612 }; |
| 613 |
| 614 } // namespace |
| 615 |
| 616 class TouchEventConverterEvdevTouchNoiseTest |
| 617 : public TouchEventConverterEvdevTest { |
| 618 public: |
| 619 TouchEventConverterEvdevTouchNoiseTest() {} |
| 620 ~TouchEventConverterEvdevTouchNoiseTest() override {} |
| 621 |
| 622 // Makes the TouchNoiseFinder use |filter| and only |filter| to filter out |
| 623 // touch noise. |
| 624 void SetTouchNoiseFilter(scoped_ptr<TouchNoiseFilter> filter) { |
| 625 TouchNoiseFinder* finder = device()->touch_noise_finder(); |
| 626 finder->filters_.clear(); |
| 627 finder->filters_.push_back(filter.release()); |
| 628 } |
| 629 |
| 630 // TouchEventConverterEvdevTest: |
| 631 void SetUp() override { |
| 632 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 633 switches::kExtraTouchNoiseFiltering); |
| 634 TouchEventConverterEvdevTest::SetUp(); |
| 635 } |
| 636 |
| 637 private: |
| 638 DISALLOW_COPY_AND_ASSIGN(TouchEventConverterEvdevTouchNoiseTest); |
| 639 }; |
| 640 |
| 641 // Test that if TouchNoiseFinder identifies an event for an in-progress touch as |
| 642 // noise, that all subsequent events for that touch are converted to |
| 643 // ET_TOUCH_CANCELED. |
| 644 TEST_F(TouchEventConverterEvdevTouchNoiseTest, TouchNoiseFiltering) { |
| 645 struct input_event mock_kernel_queue[] = { |
| 646 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, |
| 647 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 40}, |
| 648 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 41}, |
| 649 {{0, 0}, EV_SYN, SYN_REPORT, 0}, |
| 650 |
| 651 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, |
| 652 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 43}, |
| 653 {{0, 0}, EV_SYN, SYN_REPORT, 0}, |
| 654 |
| 655 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, -1}, |
| 656 {{0, 0}, EV_SYN, SYN_REPORT, 0} |
| 657 }; |
| 658 |
| 659 MockTouchEventConverterEvdev* dev = device(); |
| 660 SetTouchNoiseFilter(scoped_ptr<ui::TouchNoiseFilter>( |
| 661 new EventTypeTouchNoiseFilter(ui::ET_TOUCH_PRESSED))); |
| 662 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0); |
| 663 dev->ReadNow(); |
| 664 ASSERT_EQ(3u, size()); |
| 665 EXPECT_EQ(ET_TOUCH_CANCELLED, dispatched_event(0).type); |
| 666 EXPECT_EQ(ET_TOUCH_CANCELLED, dispatched_event(1).type); |
| 667 EXPECT_EQ(ET_TOUCH_CANCELLED, dispatched_event(2).type); |
| 668 |
| 669 ClearDispatchedEvents(); |
| 670 SetTouchNoiseFilter(scoped_ptr<ui::TouchNoiseFilter>( |
| 671 new EventTypeTouchNoiseFilter(ui::ET_TOUCH_MOVED))); |
| 672 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0); |
| 673 dev->ReadNow(); |
| 674 ASSERT_EQ(3u, size()); |
| 675 ui::TouchEventParams event0 = dispatched_event(0); |
| 676 EXPECT_EQ(ET_TOUCH_PRESSED, event0.type); |
| 677 EXPECT_EQ(40, event0.location.x()); |
| 678 EXPECT_EQ(41, event0.location.y()); |
| 679 EXPECT_EQ(ET_TOUCH_CANCELLED, dispatched_event(1).type); |
| 680 EXPECT_EQ(ET_TOUCH_CANCELLED, dispatched_event(2).type); |
| 681 |
| 682 ClearDispatchedEvents(); |
| 683 SetTouchNoiseFilter(scoped_ptr<ui::TouchNoiseFilter>( |
| 684 new EventTypeTouchNoiseFilter(ui::ET_TOUCH_RELEASED))); |
| 685 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0); |
| 686 dev->ReadNow(); |
| 687 ASSERT_EQ(3u, size()); |
| 688 event0 = dispatched_event(0); |
| 689 EXPECT_EQ(ET_TOUCH_PRESSED, event0.type); |
| 690 EXPECT_EQ(40, event0.location.x()); |
| 691 EXPECT_EQ(41, event0.location.y()); |
| 692 ui::TouchEventParams event1 = dispatched_event(1); |
| 693 EXPECT_EQ(ET_TOUCH_MOVED, event1.type); |
| 694 EXPECT_EQ(42, event1.location.x()); |
| 695 EXPECT_EQ(43, event1.location.y()); |
| 696 EXPECT_EQ(ET_TOUCH_CANCELLED, dispatched_event(2).type); |
| 697 } |
| 698 |
| 699 } // namespace ui |
OLD | NEW |