OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/events/ozone/evdev/touch_noise/touch_noise_finder.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 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" |
| 15 |
| 16 namespace ui { |
| 17 |
| 18 class TouchNoiseFinderTest : public testing::Test { |
| 19 public: |
| 20 struct TouchEntry { |
| 21 int time_ms; |
| 22 size_t slot; |
| 23 bool touching; |
| 24 gfx::PointF location; |
| 25 bool expect_noise; |
| 26 }; |
| 27 |
| 28 TouchNoiseFinderTest() {} |
| 29 ~TouchNoiseFinderTest() override {} |
| 30 |
| 31 bool FilterAndCheck(const TouchEntry entries[], size_t count) { |
| 32 std::vector<InProgressTouchEvdev> touches; |
| 33 size_t start_index = 0u; |
| 34 bool was_touching = false; |
| 35 for (size_t i = 0; i < count; ++i) { |
| 36 const TouchEntry& entry = entries[i]; |
| 37 |
| 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; |
| 44 touch.touching = entry.touching; |
| 45 |
| 46 if (i == count - 1 || entry.time_ms != entries[i + 1].time_ms) { |
| 47 touch_noise_finder_->HandleTouches( |
| 48 touches, base::TimeDelta::FromMilliseconds(entry.time_ms)); |
| 49 |
| 50 for (size_t j = 0; j < touches.size(); ++j) { |
| 51 bool expect_noise = entries[j + start_index].expect_noise; |
| 52 size_t slot = touches[j].slot; |
| 53 if (touch_noise_finder_->SlotHasNoise(slot) != expect_noise) |
| 54 LOG(ERROR) << base::StringPrintf( |
| 55 "Incorrect filtering at %dms for slot %lu", entry.time_ms, |
| 56 slot); |
| 57 return false; |
| 58 } |
| 59 |
| 60 start_index = i + 1; |
| 61 touches.clear(); |
| 62 } |
| 63 |
| 64 was_touching = entry.touching; |
| 65 } |
| 66 |
| 67 return true; |
| 68 } |
| 69 |
| 70 private: |
| 71 // testing::Test: |
| 72 void SetUp() override { |
| 73 touch_noise_finder_.reset(new TouchNoiseFinder); |
| 74 } |
| 75 |
| 76 scoped_ptr<TouchNoiseFinder> touch_noise_finder_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(TouchNoiseFinderTest); |
| 79 }; |
| 80 |
| 81 // Test that taps which are far apart in quick succession are considered noise. |
| 82 TEST_F(TouchNoiseFinderTest, FarApartTaps) { |
| 83 const TouchEntry kTestData[] = { |
| 84 {10, 1, true, gfx::PointF(10, 10), false}, |
| 85 {20, 1, true, gfx::PointF(10, 11), false}, |
| 86 {30, 1, true, gfx::PointF(10, 12), false}, |
| 87 {30, 2, true, gfx::PointF(2500, 1000), true}, |
| 88 {40, 1, true, gfx::PointF(10, 13), true}, |
| 89 {40, 2, true, gfx::PointF(2500, 1001), true}, |
| 90 {50, 1, true, gfx::PointF(10, 14), true}, |
| 91 {50, 2, false, gfx::PointF(2500, 1002), true}, |
| 92 {60, 1, false, gfx::PointF(10, 15), true}}; |
| 93 EXPECT_TRUE(FilterAndCheck(kTestData, arraysize(kTestData))); |
| 94 } |
| 95 |
| 96 // Test that taps which are far apart but do not occur in quick succession are |
| 97 // not considered noise. |
| 98 TEST_F(TouchNoiseFinderTest, FarApartTapsSlow) { |
| 99 const TouchEntry kTestData[] = { |
| 100 {1000, 1, true, gfx::PointF(10, 10), false}, |
| 101 {1500, 1, true, gfx::PointF(10, 11), false}, |
| 102 {2000, 1, true, gfx::PointF(10, 12), false}, |
| 103 {2500, 1, true, gfx::PointF(10, 13), false}, |
| 104 {2500, 2, true, gfx::PointF(2500, 1000), false}, |
| 105 {3000, 1, true, gfx::PointF(10, 14), false}, |
| 106 {3000, 2, false, gfx::PointF(2500, 1001), false}, |
| 107 {3500, 1, false, gfx::PointF(10, 15), false}}; |
| 108 EXPECT_TRUE(FilterAndCheck(kTestData, arraysize(kTestData))); |
| 109 } |
| 110 |
| 111 // Test that touches which are horizontally aligned are considered noise. |
| 112 TEST_F(TouchNoiseFinderTest, HorizontallyAligned) { |
| 113 const TouchEntry kTestData[] = { |
| 114 {10, 1, true, gfx::PointF(10, 10), false}, |
| 115 {20, 1, true, gfx::PointF(10, 10), false}, |
| 116 {20, 2, true, gfx::PointF(10, 25), true}, |
| 117 {30, 1, false, gfx::PointF(10, 10), false}, |
| 118 {30, 2, true, gfx::PointF(10, 25), true}, |
| 119 {40, 2, false, gfx::PointF(10, 25), true}}; |
| 120 EXPECT_TRUE(FilterAndCheck(kTestData, arraysize(kTestData))); |
| 121 } |
| 122 |
| 123 // Test that touches in the same position are considered noise. |
| 124 TEST_F(TouchNoiseFinderTest, SamePosition) { |
| 125 const TouchEntry kTestData[] = { |
| 126 {1000, 1, true, gfx::PointF(10, 10), false}, |
| 127 {1500, 1, false, gfx::PointF(10, 10), false}, |
| 128 {2000, 1, true, gfx::PointF(10, 10), false}, |
| 129 {2500, 1, false, gfx::PointF(10, 10), false}, |
| 130 {3000, 1, true, gfx::PointF(50, 50), false}, |
| 131 {3500, 1, true, gfx::PointF(50, 51), false}, |
| 132 {3500, 2, true, gfx::PointF(10, 10), true}, |
| 133 {4000, 1, false, gfx::PointF(50, 52), false}, |
| 134 {4000, 2, false, gfx::PointF(10, 10), true}, |
| 135 {4500, 1, true, gfx::PointF(10, 10), true}, |
| 136 {5000, 1, false, gfx::PointF(10, 10), true}}; |
| 137 EXPECT_TRUE(FilterAndCheck(kTestData, arraysize(kTestData))); |
| 138 } |
| 139 |
| 140 } // namespace ui |
OLD | NEW |