Chromium Code Reviews| Index: ui/events/ozone/evdev/touch_noise/touch_noise_remover_unittest.cc |
| diff --git a/ui/events/ozone/evdev/touch_noise/touch_noise_remover_unittest.cc b/ui/events/ozone/evdev/touch_noise/touch_noise_remover_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..75a6ff35679e1f69e3c3d7fbdd73e5d8f7d15273 |
| --- /dev/null |
| +++ b/ui/events/ozone/evdev/touch_noise/touch_noise_remover_unittest.cc |
| @@ -0,0 +1,144 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/events/ozone/evdev/touch_noise/touch_noise_remover.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h" |
| + |
| +namespace ui { |
| + |
| +namespace { |
| + |
| +bool TouchEventParamsEquals(const TouchEventParams& p1, |
| + const TouchEventParams& p2) { |
| + return p1.device_id == p2.device_id && p1.touch_id == p2.touch_id && |
| + p1.tracking_id == p2.tracking_id && p1.type == p2.type && |
| + p1.location == p2.location && p1.radii == p2.radii && |
| + p1.pressure == p2.pressure && p1.timestamp == p2.timestamp; |
| +} |
| + |
| +} // namespace |
| + |
| +class TouchNoiseRemoverTest : public testing::Test { |
| + public: |
| + struct TouchEntry { |
| + int time_ms; |
| + int touch_id; |
| + ui::EventType type; |
| + gfx::PointF location; |
| + bool should_be_canceled; |
| + }; |
| + |
| + TouchNoiseRemoverTest() {} |
| + ~TouchNoiseRemoverTest() override {} |
| + |
| + bool FilterAndCheck(const TouchEntry entries[], size_t count) { |
| + std::vector<TouchEventParams> params; |
| + std::vector<TouchEventParams> expected_params; |
| + for (size_t i = 0; i < count; ++i) { |
| + const TouchEntry& entry = entries[i]; |
| + int entry_time_ms = entry.time_ms; |
| + params.push_back( |
| + TouchEventParams(0, entry.touch_id, entry.touch_id, entry.type, |
| + entry.location, gfx::Vector2dF(), 0.0f, |
| + base::TimeDelta::FromMilliseconds(entry_time_ms))); |
| + if (!entry.should_be_canceled) |
| + expected_params.push_back(params.back()); |
| + |
| + if (i == count - 1 || entry_time_ms != entries[i + 1].time_ms) { |
| + touch_noise_remover_->RemoveNoise( |
| + ¶ms, base::TimeDelta::FromMilliseconds(entry_time_ms)); |
| + if (params.size() != expected_params.size() || |
| + !std::equal(params.begin(), params.end(), expected_params.begin(), |
| + TouchEventParamsEquals)) { |
| + LOG(ERROR) << base::StringPrintf("Incorrect filtering at %dms", |
| + entry_time_ms); |
| + return false; |
| + } |
| + |
| + params.clear(); |
| + expected_params.clear(); |
| + } |
| + } |
| + |
| + return true; |
| + } |
| + |
| + private: |
| + // testing::Test: |
| + void SetUp() override { |
| + touch_noise_remover_.reset(new TouchNoiseRemover); |
| + } |
| + |
| + scoped_ptr<TouchNoiseRemover> touch_noise_remover_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TouchNoiseRemoverTest); |
| +}; |
| + |
| +// Test that taps which are far apart in quick succession are considered noise. |
| +TEST_F(TouchNoiseRemoverTest, FarApartTaps) { |
| + const TouchEntry kTestData[] = { |
| + {10, 1, ui::ET_TOUCH_PRESSED, gfx::PointF(10, 10), false}, |
| + {20, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 11), false}, |
| + {30, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 12), false}, |
| + {30, 2, ui::ET_TOUCH_PRESSED, gfx::PointF(2500, 1000), true}, |
| + {40, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 13), true}, |
| + {40, 2, ui::ET_TOUCH_MOVED, gfx::PointF(2500, 1001), true}, |
| + {50, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 14), true}, |
| + {50, 2, ui::ET_TOUCH_RELEASED, gfx::PointF(2500, 1002), true}, |
| + {60, 1, ui::ET_TOUCH_RELEASED, gfx::PointF(10, 15), true}}; |
| + EXPECT_TRUE(FilterAndCheck(kTestData, arraysize(kTestData))); |
| +} |
| + |
| +// Test that taps which are far apart but do not occur in quick succession are |
| +// not considered noise. |
| +TEST_F(TouchNoiseRemoverTest, FarApartTapsSlow) { |
| + const TouchEntry kTestData[] = { |
| + {1000, 1, ui::ET_TOUCH_PRESSED, gfx::PointF(10, 10), false}, |
| + {1500, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 11), false}, |
| + {2000, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 12), false}, |
| + {2500, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 13), false}, |
| + {2500, 2, ui::ET_TOUCH_PRESSED, gfx::PointF(2500, 1000), false}, |
| + {3000, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 14), false}, |
| + {3000, 2, ui::ET_TOUCH_RELEASED, gfx::PointF(2500, 1001), false}, |
| + {3500, 1, ui::ET_TOUCH_RELEASED, gfx::PointF(10, 15), false}}; |
| + EXPECT_TRUE(FilterAndCheck(kTestData, arraysize(kTestData))); |
| +} |
| + |
| +// Test that touches which are horizontally aligned are considered noise. |
| +TEST_F(TouchNoiseRemoverTest, HorizontallyAligned) { |
| + const TouchEntry kTestData[] = { |
| + {10, 1, ui::ET_TOUCH_PRESSED, gfx::PointF(10, 10), false}, |
| + {20, 1, ui::ET_TOUCH_MOVED, gfx::PointF(10, 10), false}, |
| + {20, 2, ui::ET_TOUCH_PRESSED, gfx::PointF(10, 25), true}, |
| + {30, 1, ui::ET_TOUCH_RELEASED, gfx::PointF(10, 10), false}, |
| + {30, 2, ui::ET_TOUCH_MOVED, gfx::PointF(10, 25), true}, |
| + {40, 2, ui::ET_TOUCH_RELEASED, gfx::PointF(10, 25), true}}; |
| + EXPECT_TRUE(FilterAndCheck(kTestData, arraysize(kTestData))); |
| +} |
| + |
| +// Test that touches in the same position are considered noise. |
| +TEST_F(TouchNoiseRemoverTest, SamePosition) { |
| + const TouchEntry kTestData[] = { |
| + {1000, 1, ui::ET_TOUCH_PRESSED, gfx::PointF(10, 10), false}, |
| + {1500, 1, ui::ET_TOUCH_RELEASED, gfx::PointF(10, 10), false}, |
| + {2000, 1, ui::ET_TOUCH_PRESSED, gfx::PointF(10, 10), false}, |
| + {2500, 1, ui::ET_TOUCH_RELEASED, gfx::PointF(10, 10), false}, |
| + {3000, 1, ui::ET_TOUCH_PRESSED, gfx::PointF(50, 50), false}, |
| + {3500, 1, ui::ET_TOUCH_MOVED, gfx::PointF(50, 51), false}, |
| + {3500, 2, ui::ET_TOUCH_PRESSED, gfx::PointF(10, 10), true}, |
| + {4000, 1, ui::ET_TOUCH_RELEASED, gfx::PointF(50, 52), false}, |
| + {4000, 2, ui::ET_TOUCH_RELEASED, gfx::PointF(10, 10), true}, |
| + {4500, 1, ui::ET_TOUCH_PRESSED, gfx::PointF(10, 10), true}, |
| + {5000, 1, ui::ET_TOUCH_RELEASED, gfx::PointF(10, 10), true}}; |
| + EXPECT_TRUE(FilterAndCheck(kTestData, arraysize(kTestData))); |
| +} |
|
flackr
2015/03/10 05:23:40
Thanks for adding unit tests! The cancel behavior
|
| + |
| +} // namespace ui |