OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <vector> | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/time/time.h" | |
11 #include "content/browser/renderer_host/input/touch_emulator.h" | |
12 #include "content/browser/renderer_host/input/touch_emulator_client.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "ui/events/gesture_detection/gesture_config_helper.h" | |
15 | |
16 using blink::WebGestureEvent; | |
17 using blink::WebInputEvent; | |
18 using blink::WebKeyboardEvent; | |
19 using blink::WebMouseEvent; | |
20 using blink::WebTouchEvent; | |
21 using blink::WebTouchPoint; | |
22 | |
23 namespace content { | |
24 | |
25 class TouchEmulatorTest : public testing::Test, | |
26 public TouchEmulatorClient { | |
27 public: | |
28 TouchEmulatorTest() | |
29 : shift_pressed_(false), | |
30 mouse_pressed_(false), | |
31 last_mouse_x_(-1), | |
32 last_mouse_y_(-1), | |
33 scrolling_(false), | |
34 pinching_(false) {} | |
35 | |
36 virtual ~TouchEmulatorTest() {} | |
37 | |
38 // testing::Test | |
39 virtual void SetUp() OVERRIDE { | |
40 ui::GestureProvider::Config config; | |
41 config.gesture_detector_config = ui::DefaultGestureDetectorConfig(); | |
42 config.scale_gesture_detector_config = | |
43 ui::DefaultScaleGestureDetectorConfig(); | |
44 config.snap_scroll_controller_config.screen_width_pixels = 800; | |
45 config.snap_scroll_controller_config.screen_height_pixels = 600; | |
46 config.snap_scroll_controller_config.device_scale_factor = 1.f; | |
47 emulator_.reset(new TouchEmulator(this, config)); | |
48 } | |
49 | |
50 virtual void TearDown() OVERRIDE { | |
51 // Process all pending tasks to avoid leaks. | |
52 std::string expected = pinching_ ? "PINCH_END SCROLL_END" : | |
53 (scrolling_ ? "SCROLL_END" : ""); | |
54 emulator_.reset(); | |
55 Expect(expected); | |
56 } | |
57 | |
58 virtual void ForwardGestureEvent( | |
59 const blink::WebGestureEvent& event) OVERRIDE { | |
60 if (event.type == WebInputEvent::GestureScrollBegin) | |
61 scrolling_ = true; | |
62 if (event.type == WebInputEvent::GestureScrollEnd) | |
63 scrolling_ = false; | |
64 if (event.type == WebInputEvent::GestureFlingStart) | |
65 scrolling_ = false; | |
66 if (event.type == WebInputEvent::GesturePinchBegin) | |
67 pinching_ = true; | |
68 if (event.type == WebInputEvent::GesturePinchEnd) | |
69 pinching_ = false; | |
70 forwarded_events_.push_back(event.type); | |
71 } | |
72 | |
73 virtual void ForwardTouchEventWithLatencyInfo( | |
74 const blink::WebTouchEvent& event, | |
75 const ui::LatencyInfo& latency_info) OVERRIDE { | |
76 forwarded_events_.push_back(event.type); | |
77 emulator()->HandleTouchEventAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); | |
78 } | |
79 | |
80 virtual void SetCursor(const WebCursor& cursor) OVERRIDE {} | |
81 | |
82 protected: | |
83 TouchEmulator* emulator() const { | |
84 return emulator_.get(); | |
85 } | |
86 | |
87 void DestroyEmulator() { | |
jdduke (slow)
2014/04/07 18:07:26
Nit: This isn't used.
dgozman
2014/04/08 16:56:49
Done.
| |
88 emulator_.reset(); | |
89 } | |
90 | |
91 int modifiers() const { | |
92 return shift_pressed_ ? WebInputEvent::ShiftKey : 0; | |
93 } | |
94 | |
95 std::string EventTypeToString(WebInputEvent::Type type) { | |
96 switch (type) { | |
97 case WebInputEvent::TouchStart: | |
jdduke (slow)
2014/04/07 18:07:26
You can use |WebInputEventTraits::GetName(type)| i
dgozman
2014/04/08 16:56:49
Nice! Done.
| |
98 return "TOUCH_START"; | |
99 case WebInputEvent::TouchMove: | |
100 return "TOUCH_MOVE"; | |
101 case WebInputEvent::TouchEnd: | |
102 return "TOUCH_END"; | |
103 case WebInputEvent::TouchCancel: | |
104 return "TOUCH_CANCEL"; | |
105 case WebInputEvent::GestureScrollBegin: | |
106 return "SCROLL_BEGIN"; | |
107 case WebInputEvent::GestureScrollUpdate: | |
108 return "SCROLL_UPDATE"; | |
109 case WebInputEvent::GestureScrollEnd: | |
110 return "SCROLL_END"; | |
111 case WebInputEvent::GestureFlingStart: | |
112 return "FLING_START"; | |
113 case WebInputEvent::GestureFlingCancel: | |
114 return "FLING_CANCEL"; | |
115 case WebInputEvent::GesturePinchBegin: | |
116 return "PINCH_BEGIN"; | |
117 case WebInputEvent::GesturePinchUpdate: | |
118 return "PINCH_UPDATE"; | |
119 case WebInputEvent::GesturePinchEnd: | |
120 return "PINCH_END"; | |
121 case WebInputEvent::GestureShowPress: | |
122 return "SHOW_PRESS"; | |
123 case WebInputEvent::GestureTap: | |
124 return "TAP"; | |
125 case WebInputEvent::GestureTapUnconfirmed: | |
126 return "TAP_UNCONFIRMED"; | |
127 case WebInputEvent::GestureTapDown: | |
128 return "TAP_DOWN"; | |
129 case WebInputEvent::GestureTapCancel: | |
130 return "TAP_CANCEL"; | |
131 default: | |
132 return "UNKNOWN"; | |
133 } | |
134 } | |
135 | |
136 void Expect(std::string events) { | |
137 std::string forwarded; | |
138 for (size_t i = 0; i < forwarded_events_.size(); ++i) { | |
139 if (i != 0) | |
140 forwarded += " "; | |
141 forwarded += EventTypeToString(forwarded_events_[i]); | |
142 } | |
143 forwarded_events_.clear(); | |
144 EXPECT_EQ(events, forwarded); | |
145 } | |
146 | |
147 void SendKeyboardEvent() { | |
148 WebKeyboardEvent event; | |
149 event.timeStampSeconds = base::Time::Now().ToDoubleT(); | |
150 event.type = WebInputEvent::KeyDown; | |
151 event.modifiers = modifiers(); | |
152 emulator()->HandleKeyboardEvent(event); | |
153 event.type = WebInputEvent::KeyUp; | |
154 event.modifiers = modifiers(); | |
155 emulator()->HandleKeyboardEvent(event); | |
156 } | |
157 | |
158 void PressShift() { | |
159 DCHECK(!shift_pressed_); | |
160 shift_pressed_ = true; | |
161 SendKeyboardEvent(); | |
162 } | |
163 | |
164 void ReleaseShift() { | |
165 DCHECK(shift_pressed_); | |
166 shift_pressed_ = false; | |
167 SendKeyboardEvent(); | |
168 } | |
169 | |
170 void SendMouseEvent(WebInputEvent::Type type, int x, int y) { | |
171 WebMouseEvent event; | |
172 event.timeStampSeconds = base::Time::Now().ToDoubleT(); | |
173 event.type = type; | |
174 event.button = mouse_pressed_ ? WebMouseEvent::ButtonLeft : | |
175 WebMouseEvent::ButtonNone; | |
176 event.modifiers = modifiers(); | |
177 last_mouse_x_ = x; | |
178 last_mouse_y_ = y; | |
179 event.x = event.windowX = event.globalX = x; | |
180 event.y = event.windowY = event.globalY = y; | |
181 emulator()->HandleMouseEvent(event); | |
182 } | |
183 | |
184 void MouseDown(int x, int y) { | |
185 DCHECK(!mouse_pressed_); | |
186 if (x != last_mouse_x_ || y != last_mouse_y_) | |
187 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
188 mouse_pressed_ = true; | |
189 SendMouseEvent(WebInputEvent::MouseDown, x, y); | |
190 } | |
191 | |
192 void MouseDrag(int x, int y) { | |
193 DCHECK(mouse_pressed_); | |
194 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
195 } | |
196 | |
197 void MouseMove(int x, int y) { | |
198 DCHECK(!mouse_pressed_); | |
199 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
200 } | |
201 | |
202 void MouseUp(int x, int y) { | |
203 DCHECK(mouse_pressed_); | |
204 if (x != last_mouse_x_ || y != last_mouse_y_) | |
205 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
206 SendMouseEvent(WebInputEvent::MouseUp, x, y); | |
207 mouse_pressed_ = false; | |
208 } | |
209 | |
210 private: | |
211 scoped_ptr<TouchEmulator> emulator_; | |
212 std::vector<WebInputEvent::Type> forwarded_events_; | |
213 bool shift_pressed_; | |
214 bool mouse_pressed_; | |
215 int last_mouse_x_; | |
216 int last_mouse_y_; | |
217 bool scrolling_; | |
218 bool pinching_; | |
219 base::MessageLoopForUI message_loop_; | |
220 }; | |
221 | |
tdresser
2014/04/07 18:44:04
I'd be more comfortable if these tests looked at g
dgozman
2014/04/08 16:56:49
I'm not sure how to test this. Looks like properly
tdresser
2014/04/08 18:39:15
ForwardGestureEvent could record the most recent W
jdduke (slow)
2014/04/08 19:14:11
I think event orderings are more important than ch
tdresser
2014/04/08 19:21:23
Good call - I'd be perfectly happy with a single t
| |
222 | |
223 TEST_F(TouchEmulatorTest, NoTouches) { | |
224 MouseMove(100, 200); | |
225 MouseMove(300, 300); | |
226 Expect(""); | |
jdduke (slow)
2014/04/07 18:07:26
Will |EXPECT_EQ(ExpectedEvents(), "")| not work? I
dgozman
2014/04/08 16:56:49
You are right, fixed.
| |
227 } | |
228 | |
229 TEST_F(TouchEmulatorTest, Touch) { | |
230 MouseMove(100, 200); | |
231 Expect(""); | |
232 MouseDown(100, 200); | |
233 MouseUp(200, 200); | |
234 Expect("TOUCH_START TAP_DOWN TOUCH_MOVE TAP_CANCEL" | |
235 " SCROLL_BEGIN SCROLL_UPDATE TOUCH_END FLING_START"); | |
236 } | |
237 | |
238 TEST_F(TouchEmulatorTest, MultipleTouches) { | |
239 MouseMove(100, 200); | |
240 Expect(""); | |
241 MouseDown(100, 200); | |
242 MouseUp(200, 200); | |
243 Expect("TOUCH_START TAP_DOWN TOUCH_MOVE TAP_CANCEL" | |
244 " SCROLL_BEGIN SCROLL_UPDATE TOUCH_END FLING_START"); | |
245 MouseMove(300, 200); | |
246 MouseMove(200, 200); | |
247 Expect(""); | |
248 MouseDown(300, 200); | |
249 Expect("TOUCH_START FLING_CANCEL TAP_DOWN"); | |
250 MouseDrag(300, 300); | |
251 Expect("TOUCH_MOVE TAP_CANCEL SCROLL_BEGIN SCROLL_UPDATE"); | |
252 MouseDrag(300, 400); | |
253 Expect("TOUCH_MOVE SCROLL_UPDATE"); | |
254 MouseUp(300, 500); | |
255 Expect("TOUCH_MOVE SCROLL_UPDATE TOUCH_END SCROLL_END"); | |
256 } | |
257 | |
258 TEST_F(TouchEmulatorTest, Pinch) { | |
259 MouseDown(100, 200); | |
260 MouseDrag(200, 200); | |
261 PressShift(); | |
262 MouseDrag(300, 200); | |
263 Expect("TOUCH_START TAP_DOWN TOUCH_MOVE TAP_CANCEL" | |
264 " SCROLL_BEGIN SCROLL_UPDATE TOUCH_MOVE PINCH_BEGIN"); | |
265 ReleaseShift(); | |
266 MouseDrag(400, 200); | |
267 MouseUp(400, 200); | |
268 Expect("TOUCH_MOVE PINCH_END SCROLL_UPDATE TOUCH_END SCROLL_END"); | |
269 } | |
270 | |
271 TEST_F(TouchEmulatorTest, DestroyWhilePinch) { | |
272 MouseDown(100, 200); | |
273 MouseDrag(200, 200); | |
274 PressShift(); | |
275 MouseDrag(300, 200); | |
276 Expect("TOUCH_START TAP_DOWN TOUCH_MOVE TAP_CANCEL" | |
277 " SCROLL_BEGIN SCROLL_UPDATE TOUCH_MOVE PINCH_BEGIN"); | |
jdduke (slow)
2014/04/07 18:07:26
Hmm, did you want to add some more code here?
dgozman
2014/04/08 16:56:49
At first, I planned to call DestroyEmulator here.
| |
278 } | |
279 | |
280 } // namespace content | |
OLD | NEW |