1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/ozone/evdev/touch_noise/touch_noise_finder.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/stringprintf.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/events/ozone/evdev/touch_evdev_types.h"
14 #include "ui/gfx/geometry/point_f.h"
18 class TouchNoiseFinderTest
: public testing::Test
{
28 TouchNoiseFinderTest() {}
29 ~TouchNoiseFinderTest() override
{}
31 bool FilterAndCheck(const TouchEntry entries
[], size_t count
) {
32 std::vector
<InProgressTouchEvdev
> touches
;
33 size_t start_index
= 0u;
34 std::bitset
<kNumTouchEvdevSlots
> was_touching
;
35 for (size_t i
= 0; i
< count
; ++i
) {
36 const TouchEntry
& entry
= entries
[i
];
38 InProgressTouchEvdev touch
;
39 touch
.x
= entry
.location
.x();
40 touch
.y
= entry
.location
.y();
41 touch
.tracking_id
= entry
.slot
;
42 touch
.slot
= entry
.slot
;
43 touch
.was_touching
= was_touching
.test(touch
.slot
);
44 touch
.touching
= entry
.touching
;
45 touches
.push_back(touch
);
47 if (i
== count
- 1 || entry
.time_ms
!= entries
[i
+ 1].time_ms
) {
48 touch_noise_finder_
->HandleTouches(
49 touches
, base::TimeDelta::FromMilliseconds(entry
.time_ms
));
51 for (size_t j
= 0; j
< touches
.size(); ++j
) {
52 bool expect_noise
= entries
[j
+ start_index
].expect_noise
;
53 size_t slot
= touches
[j
].slot
;
54 if (touch_noise_finder_
->SlotHasNoise(slot
) != expect_noise
) {
55 LOG(ERROR
) << base::StringPrintf(
56 "Incorrect filtering at %dms for slot %zu", entry
.time_ms
,
66 was_touching
.set(entry
.slot
, entry
.touching
);
74 void SetUp() override
{
75 touch_noise_finder_
.reset(new TouchNoiseFinder
);
78 scoped_ptr
<TouchNoiseFinder
> touch_noise_finder_
;
80 DISALLOW_COPY_AND_ASSIGN(TouchNoiseFinderTest
);
83 // Test that taps which are far apart in quick succession are considered noise.
84 TEST_F(TouchNoiseFinderTest
, FarApartTaps
) {
85 const TouchEntry kTestData
[] = {
86 {10, 1, true, gfx::PointF(10, 10), false},
87 {20, 1, true, gfx::PointF(10, 11), false},
88 {30, 1, true, gfx::PointF(10, 12), false},
89 {30, 2, true, gfx::PointF(2500, 1000), true},
90 {40, 1, true, gfx::PointF(10, 13), true},
91 {40, 2, true, gfx::PointF(2500, 1001), true},
92 {50, 1, true, gfx::PointF(10, 14), true},
93 {50, 2, false, gfx::PointF(2500, 1002), true},
94 {60, 1, false, gfx::PointF(10, 15), true}};
95 EXPECT_TRUE(FilterAndCheck(kTestData
, arraysize(kTestData
)));
98 // Test that taps which are far apart but do not occur in quick succession are
99 // not considered noise.
100 TEST_F(TouchNoiseFinderTest
, FarApartTapsSlow
) {
101 const TouchEntry kTestData
[] = {
102 {1000, 1, true, gfx::PointF(10, 10), false},
103 {1500, 1, true, gfx::PointF(10, 11), false},
104 {2000, 1, true, gfx::PointF(10, 12), false},
105 {2500, 1, true, gfx::PointF(10, 13), false},
106 {2500, 2, true, gfx::PointF(2500, 1000), false},
107 {3000, 1, true, gfx::PointF(10, 14), false},
108 {3000, 2, false, gfx::PointF(2500, 1001), false},
109 {3500, 1, false, gfx::PointF(10, 15), false}};
110 EXPECT_TRUE(FilterAndCheck(kTestData
, arraysize(kTestData
)));
113 // Test that touches which are horizontally aligned are considered noise.
114 TEST_F(TouchNoiseFinderTest
, HorizontallyAligned
) {
115 const TouchEntry kTestData
[] = {
116 {10, 1, true, gfx::PointF(10, 10), false},
117 {20, 1, true, gfx::PointF(10, 10), false},
118 {20, 2, true, gfx::PointF(10, 25), true},
119 {30, 1, false, gfx::PointF(10, 10), false},
120 {30, 2, true, gfx::PointF(10, 25), true},
121 {40, 2, false, gfx::PointF(10, 25), true}};
122 EXPECT_TRUE(FilterAndCheck(kTestData
, arraysize(kTestData
)));
125 // Test that touches in the same position are considered noise.
126 TEST_F(TouchNoiseFinderTest
, SamePosition
) {
127 const TouchEntry kTestData
[] = {
128 {1000, 1, true, gfx::PointF(10, 10), false},
129 {1500, 1, false, gfx::PointF(10, 10), false},
130 {2000, 1, true, gfx::PointF(10, 10), false},
131 {2500, 1, false, gfx::PointF(10, 10), false},
132 {3000, 1, true, gfx::PointF(50, 50), false},
133 {3500, 1, true, gfx::PointF(50, 51), false},
134 {3500, 2, true, gfx::PointF(10, 10), true},
135 {4000, 1, false, gfx::PointF(50, 52), false},
136 {4000, 2, false, gfx::PointF(10, 10), true},
137 {4500, 1, true, gfx::PointF(10, 10), true},
138 {5000, 1, false, gfx::PointF(10, 10), true}};
139 EXPECT_TRUE(FilterAndCheck(kTestData
, arraysize(kTestData
)));
142 // Test that a multi-second touch is considered noise.
143 TEST_F(TouchNoiseFinderTest
, MultiSecondTouch
) {
144 const TouchEntry kTestData
[] = {
145 {1000, 1, true, gfx::PointF(10, 10), false},
146 {2000, 1, true, gfx::PointF(10, 11), false},
147 {3000, 1, true, gfx::PointF(10, 10), false},
148 {4000, 1, true, gfx::PointF(10, 11), true},
149 {5000, 1, true, gfx::PointF(10, 10), true},
150 {6000, 1, true, gfx::PointF(10, 11), true}};
151 EXPECT_TRUE(FilterAndCheck(kTestData
, arraysize(kTestData
)));