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 last_event_time_seconds_ = |
| 35 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| 36 event_time_delta_seconds_ = 0.1; |
| 37 } |
| 38 |
| 39 virtual ~TouchEmulatorTest() {} |
| 40 |
| 41 // testing::Test |
| 42 virtual void SetUp() OVERRIDE { |
| 43 ui::GestureProvider::Config config; |
| 44 config.gesture_detector_config = ui::DefaultGestureDetectorConfig(); |
| 45 config.scale_gesture_detector_config = |
| 46 ui::DefaultScaleGestureDetectorConfig(); |
| 47 config.snap_scroll_controller_config.screen_width_pixels = 800; |
| 48 config.snap_scroll_controller_config.screen_height_pixels = 600; |
| 49 config.snap_scroll_controller_config.device_scale_factor = 1.f; |
| 50 config.gesture_begin_end_types_enabled = false; |
| 51 emulator_.reset(new TouchEmulator(this, config)); |
| 52 emulator_->Enable(true /* allow_pinch */); |
| 53 } |
| 54 |
| 55 virtual void TearDown() OVERRIDE { |
| 56 emulator_->Disable(); |
| 57 EXPECT_EQ("", ExpectedEvents()); |
| 58 } |
| 59 |
| 60 virtual void ForwardGestureEvent( |
| 61 const blink::WebGestureEvent& event) OVERRIDE { |
| 62 forwarded_events_.push_back(event.type); |
| 63 } |
| 64 |
| 65 virtual void ForwardTouchEvent( |
| 66 const blink::WebTouchEvent& event) OVERRIDE { |
| 67 forwarded_events_.push_back(event.type); |
| 68 EXPECT_EQ(1U, event.touchesLength); |
| 69 EXPECT_EQ(last_mouse_x_, event.touches[0].position.x); |
| 70 EXPECT_EQ(last_mouse_y_, event.touches[0].position.y); |
| 71 emulator()->HandleTouchEventAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); |
| 72 } |
| 73 |
| 74 virtual void SetCursor(const WebCursor& cursor) OVERRIDE {} |
| 75 |
| 76 protected: |
| 77 TouchEmulator* emulator() const { |
| 78 return emulator_.get(); |
| 79 } |
| 80 |
| 81 int modifiers() const { |
| 82 return shift_pressed_ ? WebInputEvent::ShiftKey : 0; |
| 83 } |
| 84 |
| 85 std::string ExpectedEvents() { |
| 86 std::string result; |
| 87 for (size_t i = 0; i < forwarded_events_.size(); ++i) { |
| 88 if (i != 0) |
| 89 result += " "; |
| 90 result += WebInputEventTraits::GetName(forwarded_events_[i]); |
| 91 } |
| 92 forwarded_events_.clear(); |
| 93 return result; |
| 94 } |
| 95 |
| 96 double GetNextEventTimeSeconds() { |
| 97 last_event_time_seconds_ += event_time_delta_seconds_; |
| 98 return last_event_time_seconds_; |
| 99 } |
| 100 |
| 101 void set_event_time_delta_seconds_(double delta) { |
| 102 event_time_delta_seconds_ = delta; |
| 103 } |
| 104 |
| 105 void SendKeyboardEvent(WebInputEvent::Type type) { |
| 106 WebKeyboardEvent event; |
| 107 event.timeStampSeconds = GetNextEventTimeSeconds(); |
| 108 event.type = type; |
| 109 event.modifiers = modifiers(); |
| 110 emulator()->HandleKeyboardEvent(event); |
| 111 } |
| 112 |
| 113 void PressShift() { |
| 114 DCHECK(!shift_pressed_); |
| 115 shift_pressed_ = true; |
| 116 SendKeyboardEvent(WebInputEvent::KeyDown); |
| 117 } |
| 118 |
| 119 void ReleaseShift() { |
| 120 DCHECK(shift_pressed_); |
| 121 shift_pressed_ = false; |
| 122 SendKeyboardEvent(WebInputEvent::KeyUp); |
| 123 } |
| 124 |
| 125 void SendMouseEvent(WebInputEvent::Type type, int x, int y) { |
| 126 WebMouseEvent event; |
| 127 event.timeStampSeconds = GetNextEventTimeSeconds(); |
| 128 event.type = type; |
| 129 event.button = mouse_pressed_ ? WebMouseEvent::ButtonLeft : |
| 130 WebMouseEvent::ButtonNone; |
| 131 event.modifiers = modifiers(); |
| 132 last_mouse_x_ = x; |
| 133 last_mouse_y_ = y; |
| 134 event.x = event.windowX = event.globalX = x; |
| 135 event.y = event.windowY = event.globalY = y; |
| 136 emulator()->HandleMouseEvent(event); |
| 137 } |
| 138 |
| 139 void MouseDown(int x, int y) { |
| 140 DCHECK(!mouse_pressed_); |
| 141 if (x != last_mouse_x_ || y != last_mouse_y_) |
| 142 SendMouseEvent(WebInputEvent::MouseMove, x, y); |
| 143 mouse_pressed_ = true; |
| 144 SendMouseEvent(WebInputEvent::MouseDown, x, y); |
| 145 } |
| 146 |
| 147 void MouseDrag(int x, int y) { |
| 148 DCHECK(mouse_pressed_); |
| 149 SendMouseEvent(WebInputEvent::MouseMove, x, y); |
| 150 } |
| 151 |
| 152 void MouseMove(int x, int y) { |
| 153 DCHECK(!mouse_pressed_); |
| 154 SendMouseEvent(WebInputEvent::MouseMove, x, y); |
| 155 } |
| 156 |
| 157 void MouseUp(int x, int y) { |
| 158 DCHECK(mouse_pressed_); |
| 159 if (x != last_mouse_x_ || y != last_mouse_y_) |
| 160 SendMouseEvent(WebInputEvent::MouseMove, x, y); |
| 161 SendMouseEvent(WebInputEvent::MouseUp, x, y); |
| 162 mouse_pressed_ = false; |
| 163 } |
| 164 |
| 165 private: |
| 166 scoped_ptr<TouchEmulator> emulator_; |
| 167 std::vector<WebInputEvent::Type> forwarded_events_; |
| 168 double last_event_time_seconds_; |
| 169 double event_time_delta_seconds_; |
| 170 bool shift_pressed_; |
| 171 bool mouse_pressed_; |
| 172 int last_mouse_x_; |
| 173 int last_mouse_y_; |
| 174 base::MessageLoopForUI message_loop_; |
| 175 }; |
| 176 |
| 177 |
| 178 TEST_F(TouchEmulatorTest, NoTouches) { |
| 179 MouseMove(100, 200); |
| 180 MouseMove(300, 300); |
| 181 EXPECT_EQ("", ExpectedEvents()); |
| 182 } |
| 183 |
| 184 TEST_F(TouchEmulatorTest, Touch) { |
| 185 MouseMove(100, 200); |
| 186 EXPECT_EQ("", ExpectedEvents()); |
| 187 MouseDown(100, 200); |
| 188 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
| 189 MouseUp(200, 200); |
| 190 EXPECT_EQ( |
| 191 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate" |
| 192 " TouchEnd GestureScrollEnd", |
| 193 ExpectedEvents()); |
| 194 } |
| 195 |
| 196 TEST_F(TouchEmulatorTest, MultipleTouches) { |
| 197 MouseMove(100, 200); |
| 198 EXPECT_EQ("", ExpectedEvents()); |
| 199 MouseDown(100, 200); |
| 200 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
| 201 MouseUp(200, 200); |
| 202 EXPECT_EQ( |
| 203 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate" |
| 204 " TouchEnd GestureScrollEnd", |
| 205 ExpectedEvents()); |
| 206 MouseMove(300, 200); |
| 207 MouseMove(200, 200); |
| 208 EXPECT_EQ("", ExpectedEvents()); |
| 209 MouseDown(300, 200); |
| 210 EXPECT_EQ("TouchStart 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 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
| 226 MouseDrag(200, 200); |
| 227 EXPECT_EQ( |
| 228 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", |
| 229 ExpectedEvents()); |
| 230 PressShift(); |
| 231 EXPECT_EQ("", ExpectedEvents()); |
| 232 MouseDrag(300, 200); |
| 233 EXPECT_EQ("TouchMove GesturePinchBegin", ExpectedEvents()); |
| 234 ReleaseShift(); |
| 235 EXPECT_EQ("", ExpectedEvents()); |
| 236 MouseDrag(400, 200); |
| 237 EXPECT_EQ( |
| 238 "TouchMove GesturePinchEnd GestureScrollUpdate", |
| 239 ExpectedEvents()); |
| 240 MouseUp(400, 200); |
| 241 EXPECT_EQ("TouchEnd GestureScrollEnd", ExpectedEvents()); |
| 242 } |
| 243 |
| 244 TEST_F(TouchEmulatorTest, DisableAndReenable) { |
| 245 MouseDown(100, 200); |
| 246 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
| 247 MouseDrag(200, 200); |
| 248 EXPECT_EQ( |
| 249 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", |
| 250 ExpectedEvents()); |
| 251 PressShift(); |
| 252 MouseDrag(300, 200); |
| 253 EXPECT_EQ("TouchMove GesturePinchBegin", ExpectedEvents()); |
| 254 |
| 255 // Disable while pinch is in progress. |
| 256 emulator()->Disable(); |
| 257 EXPECT_EQ("TouchCancel GesturePinchEnd GestureScrollEnd", ExpectedEvents()); |
| 258 MouseUp(300, 200); |
| 259 ReleaseShift(); |
| 260 MouseMove(300, 300); |
| 261 EXPECT_EQ("", ExpectedEvents()); |
| 262 |
| 263 emulator()->Enable(true /* allow_pinch */); |
| 264 MouseDown(300, 300); |
| 265 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
| 266 MouseDrag(300, 400); |
| 267 EXPECT_EQ( |
| 268 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", |
| 269 ExpectedEvents()); |
| 270 |
| 271 // Disable while scroll is in progress. |
| 272 emulator()->Disable(); |
| 273 EXPECT_EQ("TouchCancel GestureScrollEnd", ExpectedEvents()); |
| 274 } |
| 275 |
| 276 TEST_F(TouchEmulatorTest, MouseMovesDropped) { |
| 277 MouseMove(100, 200); |
| 278 EXPECT_EQ("", ExpectedEvents()); |
| 279 MouseDown(100, 200); |
| 280 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
| 281 |
| 282 // Mouse move after mouse down is never dropped. |
| 283 set_event_time_delta_seconds_(0.001); |
| 284 MouseDrag(200, 200); |
| 285 EXPECT_EQ( |
| 286 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", |
| 287 ExpectedEvents()); |
| 288 |
| 289 // The following mouse moves are dropped. |
| 290 MouseDrag(300, 200); |
| 291 EXPECT_EQ("", ExpectedEvents()); |
| 292 MouseDrag(350, 200); |
| 293 EXPECT_EQ("", ExpectedEvents()); |
| 294 |
| 295 // Dispatching again. |
| 296 set_event_time_delta_seconds_(0.1); |
| 297 MouseDrag(400, 200); |
| 298 EXPECT_EQ( |
| 299 "TouchMove GestureScrollUpdate", |
| 300 ExpectedEvents()); |
| 301 MouseUp(400, 200); |
| 302 EXPECT_EQ( |
| 303 "TouchEnd GestureScrollEnd", |
| 304 ExpectedEvents()); |
| 305 } |
| 306 |
| 307 } // namespace content |
OLD | NEW |