Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: ui/events/ozone/evdev/touch_event_converter_evdev_unittest.cc

Issue 991533002: Port Chromium OS touch noise filtering to Chromium (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 120
114 if (pipe(fds)) 121 if (pipe(fds))
115 PLOG(FATAL) << "failed pipe"; 122 PLOG(FATAL) << "failed pipe";
116 123
117 EXPECT_FALSE(SetNonBlocking(fds[0]) || SetNonBlocking(fds[1])) 124 EXPECT_FALSE(SetNonBlocking(fds[0]) || SetNonBlocking(fds[1]))
118 << "failed to set non-blocking: " << strerror(errno); 125 << "failed to set non-blocking: " << strerror(errno);
119 126
120 read_pipe_ = fds[0]; 127 read_pipe_ = fds[0];
121 write_pipe_ = fds[1]; 128 write_pipe_ = fds[1];
122 129
123 events_.resize(MAX_FINGERS); 130 events_.resize(ui::kNumTouchEvdevSlots);
131 for (size_t i = 0; i < events_.size(); ++i)
132 events_[i].slot = i;
124 } 133 }
125 134
126 void MockTouchEventConverterEvdev::ConfigureReadMock(struct input_event* queue, 135 void MockTouchEventConverterEvdev::ConfigureReadMock(struct input_event* queue,
127 long read_this_many, 136 long read_this_many,
128 long queue_index) { 137 long queue_index) {
129 int nwrite = HANDLE_EINTR(write(write_pipe_, 138 int nwrite = HANDLE_EINTR(write(write_pipe_,
130 queue + queue_index, 139 queue + queue_index,
131 sizeof(struct input_event) * read_this_many)); 140 sizeof(struct input_event) * read_this_many));
132 DCHECK(nwrite == 141 DCHECK(nwrite ==
133 static_cast<int>(sizeof(struct input_event) * read_this_many)) 142 static_cast<int>(sizeof(struct input_event) * read_this_many))
134 << "write() failed, errno: " << errno; 143 << "write() failed, errno: " << errno;
135 } 144 }
136 145
137 } // namespace ui
138
139 // Test fixture. 146 // Test fixture.
140 class TouchEventConverterEvdevTest : public testing::Test { 147 class TouchEventConverterEvdevTest : public testing::Test {
141 public: 148 public:
142 TouchEventConverterEvdevTest() {} 149 TouchEventConverterEvdevTest() {}
143 150
144 // Overridden from testing::Test: 151 // Overridden from testing::Test:
145 void SetUp() override { 152 void SetUp() override {
146 // Set up pipe to satisfy message pump (unused). 153 // Set up pipe to satisfy message pump (unused).
147 int evdev_io[2]; 154 int evdev_io[2];
148 if (pipe(evdev_io)) 155 if (pipe(evdev_io))
(...skipping 20 matching lines...) Expand all
169 } 176 }
170 177
171 ui::MockTouchEventConverterEvdev* device() { return device_; } 178 ui::MockTouchEventConverterEvdev* device() { return device_; }
172 179
173 unsigned size() { return dispatched_events_.size(); } 180 unsigned size() { return dispatched_events_.size(); }
174 const ui::TouchEventParams& dispatched_event(unsigned index) { 181 const ui::TouchEventParams& dispatched_event(unsigned index) {
175 DCHECK_GT(dispatched_events_.size(), index); 182 DCHECK_GT(dispatched_events_.size(), index);
176 return dispatched_events_[index]; 183 return dispatched_events_[index];
177 } 184 }
178 185
186 void ClearDispatchedEvents() {
187 dispatched_events_.clear();
188 }
189
179 private: 190 private:
180 base::MessageLoop* loop_; 191 base::MessageLoop* loop_;
181 ui::MockTouchEventConverterEvdev* device_; 192 ui::MockTouchEventConverterEvdev* device_;
182 scoped_ptr<ui::MockDeviceEventDispatcherEvdev> dispatcher_; 193 scoped_ptr<ui::MockDeviceEventDispatcherEvdev> dispatcher_;
183 194
184 int events_out_; 195 int events_out_;
185 int events_in_; 196 int events_in_;
186 197
187 void DispatchCallback(const ui::TouchEventParams& params) { 198 void DispatchCallback(const ui::TouchEventParams& params) {
188 dispatched_events_.push_back(params); 199 dispatched_events_.push_back(params);
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 564
554 // crbug.com/446939 565 // crbug.com/446939
555 TEST_F(TouchEventConverterEvdevTest, CheckSlotLimit) { 566 TEST_F(TouchEventConverterEvdevTest, CheckSlotLimit) {
556 ui::MockTouchEventConverterEvdev* dev = device(); 567 ui::MockTouchEventConverterEvdev* dev = device();
557 568
558 struct input_event mock_kernel_queue[] = { 569 struct input_event mock_kernel_queue[] = {
559 {{0, 0}, EV_ABS, ABS_MT_SLOT, 0}, 570 {{0, 0}, EV_ABS, ABS_MT_SLOT, 0},
560 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 100}, 571 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 100},
561 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 999}, 572 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 999},
562 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 888}, 573 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 888},
563 {{0, 0}, EV_ABS, ABS_MT_SLOT, ui::TouchEventConverterEvdev::MAX_FINGERS}, 574 {{0, 0}, EV_ABS, ABS_MT_SLOT, ui::kNumTouchEvdevSlots},
564 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 200}, 575 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 200},
565 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 777}, 576 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 777},
566 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 666}, 577 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 666},
567 {{0, 0}, EV_SYN, SYN_REPORT, 0}, 578 {{0, 0}, EV_SYN, SYN_REPORT, 0},
568 }; 579 };
569 580
570 // Check that one 1 event is generated 581 // Check that one 1 event is generated
571 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0); 582 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0);
572 dev->ReadNow(); 583 dev->ReadNow();
573 EXPECT_EQ(1u, size()); 584 EXPECT_EQ(1u, size());
574 } 585 }
586
587 namespace {
588
589 // TouchNoiseFilter which:
590 // - Considers all events of type |noise_event_type| as noise.
591 // - Keeps track of the events that it receives.
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 EventType event_type = EventTypeFromTouch(touch);
604 ++counts_[event_type];
605 if (event_type == noise_event_type_)
606 slots_with_noise->set(touch.slot);
607 }
608 }
609
610 // Returns the number of received events of |type|.
611 size_t num_events(EventType type) const {
612 std::map<EventType, size_t>::const_iterator it = counts_.find(type);
613 return it == counts_.end() ? 0u : it->second;
614 }
615
616 private:
617 EventType EventTypeFromTouch(const InProgressTouchEvdev& touch) const {
618 if (touch.touching)
619 return touch.was_touching ? ET_TOUCH_MOVED : ET_TOUCH_PRESSED;
620 return touch.was_touching ? ET_TOUCH_RELEASED : ET_UNKNOWN;
621 }
622
623 EventType noise_event_type_;
624 std::map<EventType, size_t> counts_;
625
626 DISALLOW_COPY_AND_ASSIGN(EventTypeTouchNoiseFilter);
627 };
628
629 } // namespace
630
631 class TouchEventConverterEvdevTouchNoiseTest
632 : public TouchEventConverterEvdevTest {
633 public:
634 TouchEventConverterEvdevTouchNoiseTest() {}
635 ~TouchEventConverterEvdevTouchNoiseTest() override {}
636
637 // Makes the TouchNoiseFinder use |filter| and only |filter| to filter out
638 // touch noise.
639 void SetTouchNoiseFilter(scoped_ptr<TouchNoiseFilter> filter) {
640 TouchNoiseFinder* finder = device()->touch_noise_finder();
641 finder->filters_.clear();
642 finder->filters_.push_back(filter.release());
643 }
644
645 // Returns the first of TouchNoiseFinder's filters.
646 ui::TouchNoiseFilter* first_filter() {
647 TouchNoiseFinder* finder = device()->touch_noise_finder();
648 return finder->filters_.empty() ? nullptr : *finder->filters_.begin();
649 }
650
651 // TouchEventConverterEvdevTest:
652 void SetUp() override {
653 base::CommandLine::ForCurrentProcess()->AppendSwitch(
654 switches::kExtraTouchNoiseFiltering);
655 TouchEventConverterEvdevTest::SetUp();
656 }
657
658 private:
659 DISALLOW_COPY_AND_ASSIGN(TouchEventConverterEvdevTouchNoiseTest);
660 };
661
662 // Test that if TouchNoiseFinder identifies an event for an in-progress touch as
663 // noise, that the event is converted to ET_TOUCH_CANCELLED and that all
664 // subsequent events for the in-progress touch are cancelled.
665 TEST_F(TouchEventConverterEvdevTouchNoiseTest, TouchNoiseFiltering) {
666 struct input_event mock_kernel_queue[] = {
667 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684},
668 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 40},
669 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 41},
670 {{0, 0}, EV_SYN, SYN_REPORT, 0},
671
672 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42},
673 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 43},
674 {{0, 0}, EV_SYN, SYN_REPORT, 0},
675
676 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, -1},
677 {{0, 0}, EV_SYN, SYN_REPORT, 0}
678 };
679
680 MockTouchEventConverterEvdev* dev = device();
681 SetTouchNoiseFilter(scoped_ptr<TouchNoiseFilter>(
682 new EventTypeTouchNoiseFilter(ET_TOUCH_PRESSED)));
683 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0);
684 dev->ReadNow();
685 ASSERT_EQ(0u, size());
686
687 ClearDispatchedEvents();
688 SetTouchNoiseFilter(scoped_ptr<TouchNoiseFilter>(
689 new EventTypeTouchNoiseFilter(ET_TOUCH_MOVED)));
690 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0);
691 dev->ReadNow();
692 ASSERT_EQ(2u, size());
693 TouchEventParams event0 = dispatched_event(0);
694 EXPECT_EQ(ET_TOUCH_PRESSED, event0.type);
695 EXPECT_EQ(40, event0.location.x());
696 EXPECT_EQ(41, event0.location.y());
697 EXPECT_EQ(ET_TOUCH_CANCELLED, dispatched_event(1).type);
698
699 ClearDispatchedEvents();
700 SetTouchNoiseFilter(scoped_ptr<TouchNoiseFilter>(
701 new EventTypeTouchNoiseFilter(ET_TOUCH_RELEASED)));
702 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0);
703 dev->ReadNow();
704 ASSERT_EQ(3u, size());
705 event0 = dispatched_event(0);
706 EXPECT_EQ(ET_TOUCH_PRESSED, event0.type);
707 EXPECT_EQ(40, event0.location.x());
708 EXPECT_EQ(41, event0.location.y());
709 TouchEventParams event1 = dispatched_event(1);
710 EXPECT_EQ(ET_TOUCH_MOVED, event1.type);
711 EXPECT_EQ(42, event1.location.x());
712 EXPECT_EQ(43, event1.location.y());
713 EXPECT_EQ(ET_TOUCH_CANCELLED, dispatched_event(2).type);
714 }
715
716 // Test that TouchEventConverterEvdev keeps sending events to
717 // TouchNoiseFinder after the touch is canceled.
718 TEST_F(TouchEventConverterEvdevTouchNoiseTest,
719 DoNotSendTouchCancelsToTouchNoiseFinder) {
720 struct input_event mock_kernel_queue[] = {
721 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684},
722 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 40},
723 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 41},
724 {{0, 0}, EV_SYN, SYN_REPORT, 0},
725
726 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42},
727 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 43},
728 {{0, 0}, EV_SYN, SYN_REPORT, 0},
729
730 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 43},
731 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 44},
732 {{0, 0}, EV_SYN, SYN_REPORT, 0},
733
734 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, -1},
735 {{0, 0}, EV_SYN, SYN_REPORT, 0}
736 };
737
738 MockTouchEventConverterEvdev* dev = device();
739 SetTouchNoiseFilter(scoped_ptr<TouchNoiseFilter>(
740 new EventTypeTouchNoiseFilter(ET_TOUCH_PRESSED)));
741 dev->ConfigureReadMock(mock_kernel_queue, arraysize(mock_kernel_queue), 0);
742 dev->ReadNow();
743 ASSERT_EQ(0u, size());
744
745 EventTypeTouchNoiseFilter* filter =
746 static_cast<EventTypeTouchNoiseFilter*>(first_filter());
747 EXPECT_EQ(1u, filter->num_events(ET_TOUCH_PRESSED));
748 EXPECT_EQ(2u, filter->num_events(ET_TOUCH_MOVED));
749 EXPECT_EQ(1u, filter->num_events(ET_TOUCH_RELEASED));
750 }
751
752 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698