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 "content/common/input/web_input_event_traits.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "ui/events/gesture_detection/gesture_config_helper.h" | |
16 | |
17 using blink::WebGestureEvent; | |
18 using blink::WebInputEvent; | |
19 using blink::WebKeyboardEvent; | |
20 using blink::WebMouseEvent; | |
21 using blink::WebTouchEvent; | |
22 using blink::WebTouchPoint; | |
23 | |
24 namespace content { | |
25 | |
26 class TouchEmulatorTest : public testing::Test, | |
27 public TouchEmulatorClient { | |
28 public: | |
29 TouchEmulatorTest() | |
30 : shift_pressed_(false), | |
31 mouse_pressed_(false), | |
32 last_mouse_x_(-1), | |
33 last_mouse_y_(-1), | |
34 scrolling_(false), | |
35 pinching_(false) {} | |
36 | |
37 virtual ~TouchEmulatorTest() {} | |
38 | |
39 // testing::Test | |
40 virtual void SetUp() OVERRIDE { | |
41 ui::GestureProvider::Config config; | |
42 config.gesture_detector_config = ui::DefaultGestureDetectorConfig(); | |
43 config.scale_gesture_detector_config = | |
44 ui::DefaultScaleGestureDetectorConfig(); | |
45 config.snap_scroll_controller_config.screen_width_pixels = 800; | |
46 config.snap_scroll_controller_config.screen_height_pixels = 600; | |
47 config.snap_scroll_controller_config.device_scale_factor = 1.f; | |
48 config.gesture_begin_end_types_enabled = false; | |
49 emulator_.reset(new TouchEmulator(this, config)); | |
50 emulator_->Enable(); | |
51 } | |
52 | |
53 virtual void TearDown() OVERRIDE { | |
54 std::string expected = pinching_ ? | |
55 "TouchCancel GesturePinchEnd GestureScrollEnd" : | |
56 (scrolling_ ? "TouchCancel GestureScrollEnd" : ""); | |
57 emulator_->Disable(); | |
58 EXPECT_EQ(expected, ExpectedEvents()); | |
59 } | |
60 | |
61 virtual void ForwardGestureEvent( | |
62 const blink::WebGestureEvent& event) OVERRIDE { | |
63 if (event.type == WebInputEvent::GestureScrollBegin) | |
64 scrolling_ = true; | |
65 if (event.type == WebInputEvent::GestureScrollEnd) | |
66 scrolling_ = false; | |
67 if (event.type == WebInputEvent::GestureFlingStart) | |
68 scrolling_ = false; | |
69 if (event.type == WebInputEvent::GesturePinchBegin) | |
70 pinching_ = true; | |
71 if (event.type == WebInputEvent::GesturePinchEnd) | |
72 pinching_ = false; | |
73 forwarded_events_.push_back(event.type); | |
74 } | |
75 | |
76 virtual void ForwardTouchEvent( | |
77 const blink::WebTouchEvent& event) OVERRIDE { | |
78 forwarded_events_.push_back(event.type); | |
79 emulator()->HandleTouchEventAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); | |
80 } | |
81 | |
82 virtual void SetCursor(const WebCursor& cursor) OVERRIDE {} | |
83 | |
84 protected: | |
85 TouchEmulator* emulator() const { | |
86 return emulator_.get(); | |
87 } | |
88 | |
89 int modifiers() const { | |
90 return shift_pressed_ ? WebInputEvent::ShiftKey : 0; | |
91 } | |
92 | |
93 std::string ExpectedEvents() { | |
94 std::string result; | |
95 for (size_t i = 0; i < forwarded_events_.size(); ++i) { | |
96 if (i != 0) | |
97 result += " "; | |
98 result += WebInputEventTraits::GetName(forwarded_events_[i]); | |
99 } | |
100 forwarded_events_.clear(); | |
101 return result; | |
102 } | |
103 | |
104 void SendKeyboardEvent() { | |
105 WebKeyboardEvent event; | |
106 event.timeStampSeconds = base::Time::Now().ToDoubleT(); | |
107 event.type = WebInputEvent::KeyDown; | |
108 event.modifiers = modifiers(); | |
109 emulator()->HandleKeyboardEvent(event); | |
110 event.type = WebInputEvent::KeyUp; // !!!!! | |
111 event.modifiers = modifiers(); | |
112 emulator()->HandleKeyboardEvent(event); | |
113 } | |
114 | |
115 void PressShift() { | |
116 DCHECK(!shift_pressed_); | |
117 shift_pressed_ = true; | |
118 SendKeyboardEvent(); | |
119 } | |
120 | |
121 void ReleaseShift() { | |
122 DCHECK(shift_pressed_); | |
123 shift_pressed_ = false; | |
124 SendKeyboardEvent(); | |
125 } | |
126 | |
127 void SendMouseEvent(WebInputEvent::Type type, int x, int y) { | |
128 WebMouseEvent event; | |
129 event.timeStampSeconds = base::Time::Now().ToDoubleT(); | |
130 event.type = type; | |
131 event.button = mouse_pressed_ ? WebMouseEvent::ButtonLeft : | |
132 WebMouseEvent::ButtonNone; | |
133 event.modifiers = modifiers(); | |
134 last_mouse_x_ = x; | |
135 last_mouse_y_ = y; | |
136 event.x = event.windowX = event.globalX = x; | |
137 event.y = event.windowY = event.globalY = y; | |
138 emulator()->HandleMouseEvent(event); | |
jdduke (slow)
2014/04/08 19:14:11
One cheap addition here would be recording the tou
dgozman
2014/04/09 17:00:05
I've added the similar check to |ForwardTouchEvent
| |
139 } | |
140 | |
141 void MouseDown(int x, int y) { | |
142 DCHECK(!mouse_pressed_); | |
143 if (x != last_mouse_x_ || y != last_mouse_y_) | |
144 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
145 mouse_pressed_ = true; | |
146 SendMouseEvent(WebInputEvent::MouseDown, x, y); | |
147 } | |
148 | |
149 void MouseDrag(int x, int y) { | |
150 DCHECK(mouse_pressed_); | |
151 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
152 } | |
153 | |
154 void MouseMove(int x, int y) { | |
155 DCHECK(!mouse_pressed_); | |
156 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
157 } | |
158 | |
159 void MouseUp(int x, int y) { | |
160 DCHECK(mouse_pressed_); | |
161 if (x != last_mouse_x_ || y != last_mouse_y_) | |
162 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
163 SendMouseEvent(WebInputEvent::MouseUp, x, y); | |
164 mouse_pressed_ = false; | |
165 } | |
166 | |
167 private: | |
168 scoped_ptr<TouchEmulator> emulator_; | |
169 std::vector<WebInputEvent::Type> forwarded_events_; | |
170 bool shift_pressed_; | |
171 bool mouse_pressed_; | |
172 int last_mouse_x_; | |
173 int last_mouse_y_; | |
174 bool scrolling_; | |
175 bool pinching_; | |
176 base::MessageLoopForUI message_loop_; | |
177 }; | |
178 | |
179 | |
180 TEST_F(TouchEmulatorTest, NoTouches) { | |
181 MouseMove(100, 200); | |
182 MouseMove(300, 300); | |
183 EXPECT_EQ("", ExpectedEvents()); | |
184 } | |
185 | |
186 TEST_F(TouchEmulatorTest, Touch) { | |
187 MouseMove(100, 200); | |
188 EXPECT_EQ("", ExpectedEvents()); | |
189 MouseDown(100, 200); | |
190 MouseUp(200, 200); | |
191 EXPECT_EQ( | |
192 "TouchStart GestureTapDown TouchMove GestureTapCancel" | |
193 " GestureScrollBegin GestureScrollUpdate TouchEnd GestureFlingStart", | |
194 ExpectedEvents()); | |
195 } | |
196 | |
197 TEST_F(TouchEmulatorTest, MultipleTouches) { | |
198 MouseMove(100, 200); | |
199 EXPECT_EQ("", ExpectedEvents()); | |
200 MouseDown(100, 200); | |
201 MouseUp(200, 200); | |
202 EXPECT_EQ( | |
203 "TouchStart GestureTapDown TouchMove GestureTapCancel" | |
204 " GestureScrollBegin GestureScrollUpdate TouchEnd GestureFlingStart", | |
205 ExpectedEvents()); | |
206 MouseMove(300, 200); | |
207 MouseMove(200, 200); | |
208 EXPECT_EQ("", ExpectedEvents()); | |
209 MouseDown(300, 200); | |
210 EXPECT_EQ("TouchStart GestureFlingCancel GestureTapDown", ExpectedEvents()); | |
211 MouseDrag(300, 300); | |
212 EXPECT_EQ( | |
213 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", | |
214 ExpectedEvents()); | |
215 MouseDrag(300, 400); | |
216 EXPECT_EQ("TouchMove GestureScrollUpdate", ExpectedEvents()); | |
217 MouseUp(300, 500); | |
218 EXPECT_EQ( | |
219 "TouchMove GestureScrollUpdate TouchEnd GestureScrollEnd", | |
220 ExpectedEvents()); | |
221 } | |
222 | |
223 TEST_F(TouchEmulatorTest, Pinch) { | |
224 MouseDown(100, 200); | |
225 MouseDrag(200, 200); | |
226 PressShift(); | |
227 MouseDrag(300, 200); | |
228 EXPECT_EQ( | |
229 "TouchStart GestureTapDown TouchMove GestureTapCancel" | |
230 " GestureScrollBegin GestureScrollUpdate TouchMove GesturePinchBegin", | |
231 ExpectedEvents()); | |
232 ReleaseShift(); | |
233 MouseDrag(400, 200); | |
234 MouseUp(400, 200); | |
235 EXPECT_EQ( | |
236 "TouchMove GesturePinchEnd GestureScrollUpdate TouchEnd GestureScrollEnd", | |
237 ExpectedEvents()); | |
238 } | |
239 | |
240 TEST_F(TouchEmulatorTest, DestroyWhilePinch) { | |
jdduke (slow)
2014/04/08 19:14:11
Maybe change this test to a very basic enable/disa
dgozman
2014/04/09 17:00:05
I previously assumed that emulator destructor (and
| |
241 MouseDown(100, 200); | |
242 MouseDrag(200, 200); | |
243 PressShift(); | |
244 MouseDrag(300, 200); | |
245 EXPECT_EQ( | |
246 "TouchStart GestureTapDown TouchMove GestureTapCancel" | |
247 " GestureScrollBegin GestureScrollUpdate TouchMove GesturePinchBegin", | |
248 ExpectedEvents()); | |
249 } | |
250 | |
251 } // namespace content | |
OLD | NEW |