[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / input / touch_event_stream_validator_unittest.cc
blob5e3b767fb1d09364de01342fc5736bc0e349ed71
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
3 // found in the LICENSE file.
5 #include "content/common/input/touch_event_stream_validator.h"
7 #include "content/common/input/synthetic_web_input_event_builders.h"
8 #include "content/common/input/web_touch_event_traits.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using blink::WebInputEvent;
12 using blink::WebTouchEvent;
13 using blink::WebTouchPoint;
15 namespace content {
17 TEST(TouchEventStreamValidator, ValidTouchStream) {
18 TouchEventStreamValidator validator;
19 SyntheticWebTouchEvent event;
20 std::string error_msg;
22 event.PressPoint(0, 1);
23 EXPECT_TRUE(validator.Validate(event, &error_msg));
24 EXPECT_TRUE(error_msg.empty());
25 event.ResetPoints();
27 event.PressPoint(1, 0);
28 EXPECT_TRUE(validator.Validate(event, &error_msg));
29 EXPECT_TRUE(error_msg.empty());
30 event.ResetPoints();
32 event.MovePoint(1, 1, 1);
33 EXPECT_TRUE(validator.Validate(event, &error_msg));
34 EXPECT_TRUE(error_msg.empty());
35 event.ResetPoints();
37 event.ReleasePoint(1);
38 EXPECT_TRUE(validator.Validate(event, &error_msg));
39 EXPECT_TRUE(error_msg.empty());
40 event.ResetPoints();
42 event.MovePoint(0, -1, 0);
43 EXPECT_TRUE(validator.Validate(event, &error_msg));
44 EXPECT_TRUE(error_msg.empty());
45 event.ResetPoints();
47 event.CancelPoint(0);
48 EXPECT_TRUE(validator.Validate(event, &error_msg));
49 EXPECT_TRUE(error_msg.empty());
50 event.ResetPoints();
52 event.PressPoint(-1, -1);
53 EXPECT_TRUE(validator.Validate(event, &error_msg));
54 EXPECT_TRUE(error_msg.empty());
57 TEST(TouchEventStreamValidator, ResetOnNewTouchStream) {
58 TouchEventStreamValidator validator;
59 SyntheticWebTouchEvent event;
60 std::string error_msg;
62 event.PressPoint(0, 1);
63 EXPECT_TRUE(validator.Validate(event, &error_msg));
64 EXPECT_TRUE(error_msg.empty());
65 event.ResetPoints();
67 event.CancelPoint(0);
68 event.ResetPoints();
69 event.PressPoint(1, 0);
70 EXPECT_TRUE(validator.Validate(event, &error_msg));
71 EXPECT_TRUE(error_msg.empty());
74 TEST(TouchEventStreamValidator, MissedTouchStart) {
75 TouchEventStreamValidator validator;
76 SyntheticWebTouchEvent event;
77 std::string error_msg;
79 event.PressPoint(0, 1);
80 EXPECT_TRUE(validator.Validate(event, &error_msg));
81 EXPECT_TRUE(error_msg.empty());
83 event.PressPoint(1, 0);
84 event.ResetPoints();
85 event.PressPoint(1, 1);
86 EXPECT_FALSE(validator.Validate(event, &error_msg));
87 EXPECT_FALSE(error_msg.empty());
90 TEST(TouchEventStreamValidator, MissedTouchEnd) {
91 TouchEventStreamValidator validator;
92 SyntheticWebTouchEvent event;
93 std::string error_msg;
95 event.PressPoint(0, 1);
96 EXPECT_TRUE(validator.Validate(event, &error_msg));
97 EXPECT_TRUE(error_msg.empty());
98 event.ResetPoints();
100 event.PressPoint(0, 1);
101 EXPECT_TRUE(validator.Validate(event, &error_msg));
102 EXPECT_TRUE(error_msg.empty());
103 event.ResetPoints();
105 event.ReleasePoint(1);
106 event.ResetPoints();
107 event.PressPoint(1, 1);
108 EXPECT_FALSE(validator.Validate(event, &error_msg));
109 EXPECT_FALSE(error_msg.empty());
112 TEST(TouchEventStreamValidator, EmptyEvent) {
113 TouchEventStreamValidator validator;
114 WebTouchEvent event;
115 std::string error_msg;
117 EXPECT_FALSE(validator.Validate(event, &error_msg));
118 EXPECT_FALSE(error_msg.empty());
121 TEST(TouchEventStreamValidator, InvalidEventType) {
122 TouchEventStreamValidator validator;
123 WebTouchEvent event;
124 std::string error_msg;
126 event.type = WebInputEvent::GestureScrollBegin;
127 event.touchesLength = 1;
128 event.touches[0].state = WebTouchPoint::StatePressed;
130 EXPECT_FALSE(validator.Validate(event, &error_msg));
131 EXPECT_FALSE(error_msg.empty());
134 TEST(TouchEventStreamValidator, InvalidPointStates) {
135 TouchEventStreamValidator validator;
136 std::string error_msg;
138 WebInputEvent::Type kTouchTypes[4] = {
139 WebInputEvent::TouchStart, WebInputEvent::TouchMove,
140 WebInputEvent::TouchEnd, WebInputEvent::TouchCancel,
143 WebTouchPoint::State kValidTouchPointStatesForType[4] = {
144 WebTouchPoint::StatePressed, WebTouchPoint::StateMoved,
145 WebTouchPoint::StateReleased, WebTouchPoint::StateCancelled,
148 SyntheticWebTouchEvent start;
149 start.PressPoint(0, 0);
150 for (size_t i = 0; i < 4; ++i) {
151 // Always start with a touchstart to reset the stream validation.
152 EXPECT_TRUE(validator.Validate(start, &error_msg));
153 EXPECT_TRUE(error_msg.empty());
155 WebTouchEvent event;
156 event.touchesLength = 1;
157 event.type = kTouchTypes[i];
158 for (size_t j = WebTouchPoint::StateUndefined;
159 j <= WebTouchPoint::StateCancelled;
160 ++j) {
161 event.touches[0].state = static_cast<WebTouchPoint::State>(j);
162 if (event.touches[0].state == kValidTouchPointStatesForType[i]) {
163 EXPECT_TRUE(validator.Validate(event, &error_msg));
164 EXPECT_TRUE(error_msg.empty());
165 } else {
166 EXPECT_FALSE(validator.Validate(event, &error_msg));
167 EXPECT_FALSE(error_msg.empty());
173 } // namespace content