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/gesture_event_stream_validator.h"
7 #include "content/common/input/synthetic_web_input_event_builders.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using blink::WebInputEvent
;
11 using blink::WebGestureEvent
;
16 const blink::WebGestureDevice kDefaultGestureDevice
=
17 blink::WebGestureDeviceTouchscreen
;
19 blink::WebGestureEvent
Build(WebInputEvent::Type type
) {
20 return SyntheticWebGestureEventBuilder::Build(type
, kDefaultGestureDevice
);
25 TEST(GestureEventStreamValidator
, ValidScroll
) {
26 GestureEventStreamValidator validator
;
27 std::string error_msg
;
28 WebGestureEvent event
;
30 event
= Build(WebInputEvent::GestureScrollBegin
);
31 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
32 EXPECT_TRUE(error_msg
.empty());
34 event
= Build(WebInputEvent::GestureScrollUpdate
);
35 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
36 EXPECT_TRUE(error_msg
.empty());
38 event
= Build(WebInputEvent::GestureScrollEnd
);
39 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
40 EXPECT_TRUE(error_msg
.empty());
43 TEST(GestureEventStreamValidator
, InvalidScroll
) {
44 GestureEventStreamValidator validator
;
45 std::string error_msg
;
46 WebGestureEvent event
;
48 // No preceding ScrollBegin.
49 event
= Build(WebInputEvent::GestureScrollUpdate
);
50 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
51 EXPECT_FALSE(error_msg
.empty());
53 // No preceding ScrollBegin.
54 event
= Build(WebInputEvent::GestureScrollEnd
);
55 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
56 EXPECT_FALSE(error_msg
.empty());
58 event
= Build(WebInputEvent::GestureScrollBegin
);
59 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
60 EXPECT_TRUE(error_msg
.empty());
63 event
= Build(WebInputEvent::GestureScrollBegin
);
64 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
65 EXPECT_FALSE(error_msg
.empty());
67 event
= Build(WebInputEvent::GestureScrollEnd
);
68 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
69 EXPECT_TRUE(error_msg
.empty());
71 // Scroll already ended.
72 event
= Build(WebInputEvent::GestureScrollEnd
);
73 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
74 EXPECT_FALSE(error_msg
.empty());
77 TEST(GestureEventStreamValidator
, ValidFling
) {
78 GestureEventStreamValidator validator
;
79 std::string error_msg
;
80 WebGestureEvent event
;
82 event
= Build(WebInputEvent::GestureScrollBegin
);
83 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
84 EXPECT_TRUE(error_msg
.empty());
86 event
= Build(WebInputEvent::GestureFlingStart
);
87 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
88 EXPECT_TRUE(error_msg
.empty());
91 TEST(GestureEventStreamValidator
, InvalidFling
) {
92 GestureEventStreamValidator validator
;
93 std::string error_msg
;
94 WebGestureEvent event
;
96 // No preceding ScrollBegin.
97 event
= Build(WebInputEvent::GestureFlingStart
);
98 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
99 EXPECT_FALSE(error_msg
.empty());
102 TEST(GestureEventStreamValidator
, ValidPinch
) {
103 GestureEventStreamValidator validator
;
104 std::string error_msg
;
105 WebGestureEvent event
;
107 event
= Build(WebInputEvent::GesturePinchBegin
);
108 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
109 EXPECT_TRUE(error_msg
.empty());
111 event
= Build(WebInputEvent::GesturePinchUpdate
);
112 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
113 EXPECT_TRUE(error_msg
.empty());
115 event
= Build(WebInputEvent::GesturePinchEnd
);
116 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
117 EXPECT_TRUE(error_msg
.empty());
120 TEST(GestureEventStreamValidator
, InvalidPinch
) {
121 GestureEventStreamValidator validator
;
122 std::string error_msg
;
123 WebGestureEvent event
;
125 // No preceding PinchBegin.
126 event
= Build(WebInputEvent::GesturePinchUpdate
);
127 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
128 EXPECT_FALSE(error_msg
.empty());
130 event
= Build(WebInputEvent::GesturePinchBegin
);
131 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
132 EXPECT_TRUE(error_msg
.empty());
134 // ScrollBegin while pinching.
135 event
= Build(WebInputEvent::GestureScrollBegin
);
136 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
137 EXPECT_FALSE(error_msg
.empty());
139 // ScrollEnd while pinching.
140 event
= Build(WebInputEvent::GestureScrollEnd
);
141 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
142 EXPECT_FALSE(error_msg
.empty());
144 // Pinch already begun.
145 event
= Build(WebInputEvent::GesturePinchBegin
);
146 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
147 EXPECT_FALSE(error_msg
.empty());
149 event
= Build(WebInputEvent::GesturePinchEnd
);
150 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
151 EXPECT_TRUE(error_msg
.empty());
153 // Pinch already ended.
154 event
= Build(WebInputEvent::GesturePinchEnd
);
155 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
156 EXPECT_FALSE(error_msg
.empty());
159 TEST(GestureEventStreamValidator
, ValidTap
) {
160 GestureEventStreamValidator validator
;
161 std::string error_msg
;
162 WebGestureEvent event
;
164 event
= Build(WebInputEvent::GestureTapDown
);
165 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
166 EXPECT_TRUE(error_msg
.empty());
168 event
= Build(WebInputEvent::GestureTapCancel
);
169 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
170 EXPECT_TRUE(error_msg
.empty());
172 event
= Build(WebInputEvent::GestureTapDown
);
173 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
174 EXPECT_TRUE(error_msg
.empty());
176 event
= Build(WebInputEvent::GestureTapUnconfirmed
);
177 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
178 EXPECT_TRUE(error_msg
.empty());
180 event
= Build(WebInputEvent::GestureTapCancel
);
181 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
182 EXPECT_TRUE(error_msg
.empty());
184 event
= Build(WebInputEvent::GestureTapDown
);
185 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
186 EXPECT_TRUE(error_msg
.empty());
188 event
= Build(WebInputEvent::GestureTap
);
189 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
190 EXPECT_TRUE(error_msg
.empty());
192 // DoubleTap does not require a TapDown (unlike Tap, TapUnconfirmed and
194 event
= Build(WebInputEvent::GestureDoubleTap
);
195 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
196 EXPECT_TRUE(error_msg
.empty());
199 TEST(GestureEventStreamValidator
, InvalidTap
) {
200 GestureEventStreamValidator validator
;
201 std::string error_msg
;
202 WebGestureEvent event
;
204 // No preceding TapDown.
205 event
= Build(WebInputEvent::GestureTapUnconfirmed
);
206 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
207 EXPECT_FALSE(error_msg
.empty());
209 event
= Build(WebInputEvent::GestureTapCancel
);
210 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
211 EXPECT_FALSE(error_msg
.empty());
213 event
= Build(WebInputEvent::GestureTap
);
214 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
215 EXPECT_FALSE(error_msg
.empty());
217 // TapDown already terminated.
218 event
= Build(WebInputEvent::GestureTapDown
);
219 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
220 EXPECT_TRUE(error_msg
.empty());
222 event
= Build(WebInputEvent::GestureDoubleTap
);
223 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
224 EXPECT_TRUE(error_msg
.empty());
226 event
= Build(WebInputEvent::GestureTapCancel
);
227 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
228 EXPECT_FALSE(error_msg
.empty());
230 // TapDown already terminated.
231 event
= Build(WebInputEvent::GestureTapDown
);
232 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
233 EXPECT_TRUE(error_msg
.empty());
235 event
= Build(WebInputEvent::GestureTap
);
236 EXPECT_TRUE(validator
.Validate(event
, &error_msg
));
237 EXPECT_TRUE(error_msg
.empty());
239 event
= Build(WebInputEvent::GestureTapCancel
);
240 EXPECT_FALSE(validator
.Validate(event
, &error_msg
));
241 EXPECT_FALSE(error_msg
.empty());
244 } // namespace content