| 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 #if defined(USE_AURA) | |
| 18 #include "ui/aura/env.h" | |
| 19 #include "ui/aura/test/test_screen.h" | |
| 20 #endif | |
| 21 | |
| 22 using blink::WebGestureEvent; | |
| 23 using blink::WebInputEvent; | |
| 24 using blink::WebKeyboardEvent; | |
| 25 using blink::WebMouseEvent; | |
| 26 using blink::WebTouchEvent; | |
| 27 using blink::WebTouchPoint; | |
| 28 | |
| 29 namespace content { | |
| 30 | |
| 31 class TouchEmulatorTest : public testing::Test, | |
| 32 public TouchEmulatorClient { | |
| 33 public: | |
| 34 TouchEmulatorTest() | |
| 35 : shift_pressed_(false), | |
| 36 mouse_pressed_(false), | |
| 37 last_mouse_x_(-1), | |
| 38 last_mouse_y_(-1) { | |
| 39 last_event_time_seconds_ = | |
| 40 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | |
| 41 event_time_delta_seconds_ = 0.1; | |
| 42 } | |
| 43 | |
| 44 virtual ~TouchEmulatorTest() {} | |
| 45 | |
| 46 // testing::Test | |
| 47 virtual void SetUp() OVERRIDE { | |
| 48 #if defined(USE_AURA) | |
| 49 aura::Env::CreateInstance(); | |
| 50 screen_.reset(aura::TestScreen::Create()); | |
| 51 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); | |
| 52 #endif | |
| 53 | |
| 54 emulator_.reset(new TouchEmulator(this)); | |
| 55 emulator_->Enable(true /* allow_pinch */); | |
| 56 } | |
| 57 | |
| 58 virtual void TearDown() OVERRIDE { | |
| 59 emulator_->Disable(); | |
| 60 EXPECT_EQ("", ExpectedEvents()); | |
| 61 | |
| 62 #if defined(USE_AURA) | |
| 63 aura::Env::DeleteInstance(); | |
| 64 screen_.reset(); | |
| 65 #endif | |
| 66 } | |
| 67 | |
| 68 virtual void ForwardGestureEvent( | |
| 69 const blink::WebGestureEvent& event) OVERRIDE { | |
| 70 forwarded_events_.push_back(event.type); | |
| 71 } | |
| 72 | |
| 73 virtual void ForwardTouchEvent( | |
| 74 const blink::WebTouchEvent& event) OVERRIDE { | |
| 75 forwarded_events_.push_back(event.type); | |
| 76 EXPECT_EQ(1U, event.touchesLength); | |
| 77 EXPECT_EQ(last_mouse_x_, event.touches[0].position.x); | |
| 78 EXPECT_EQ(last_mouse_y_, event.touches[0].position.y); | |
| 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 double GetNextEventTimeSeconds() { | |
| 105 last_event_time_seconds_ += event_time_delta_seconds_; | |
| 106 return last_event_time_seconds_; | |
| 107 } | |
| 108 | |
| 109 void set_event_time_delta_seconds_(double delta) { | |
| 110 event_time_delta_seconds_ = delta; | |
| 111 } | |
| 112 | |
| 113 void SendKeyboardEvent(WebInputEvent::Type type) { | |
| 114 WebKeyboardEvent event; | |
| 115 event.timeStampSeconds = GetNextEventTimeSeconds(); | |
| 116 event.type = type; | |
| 117 event.modifiers = modifiers(); | |
| 118 emulator()->HandleKeyboardEvent(event); | |
| 119 } | |
| 120 | |
| 121 void PressShift() { | |
| 122 DCHECK(!shift_pressed_); | |
| 123 shift_pressed_ = true; | |
| 124 SendKeyboardEvent(WebInputEvent::KeyDown); | |
| 125 } | |
| 126 | |
| 127 void ReleaseShift() { | |
| 128 DCHECK(shift_pressed_); | |
| 129 shift_pressed_ = false; | |
| 130 SendKeyboardEvent(WebInputEvent::KeyUp); | |
| 131 } | |
| 132 | |
| 133 void SendMouseEvent(WebInputEvent::Type type, int x, int y) { | |
| 134 WebMouseEvent event; | |
| 135 event.timeStampSeconds = GetNextEventTimeSeconds(); | |
| 136 event.type = type; | |
| 137 event.button = mouse_pressed_ ? WebMouseEvent::ButtonLeft : | |
| 138 WebMouseEvent::ButtonNone; | |
| 139 event.modifiers = modifiers(); | |
| 140 last_mouse_x_ = x; | |
| 141 last_mouse_y_ = y; | |
| 142 event.x = event.windowX = event.globalX = x; | |
| 143 event.y = event.windowY = event.globalY = y; | |
| 144 emulator()->HandleMouseEvent(event); | |
| 145 } | |
| 146 | |
| 147 void MouseDown(int x, int y) { | |
| 148 DCHECK(!mouse_pressed_); | |
| 149 if (x != last_mouse_x_ || y != last_mouse_y_) | |
| 150 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
| 151 mouse_pressed_ = true; | |
| 152 SendMouseEvent(WebInputEvent::MouseDown, x, y); | |
| 153 } | |
| 154 | |
| 155 void MouseDrag(int x, int y) { | |
| 156 DCHECK(mouse_pressed_); | |
| 157 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
| 158 } | |
| 159 | |
| 160 void MouseMove(int x, int y) { | |
| 161 DCHECK(!mouse_pressed_); | |
| 162 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
| 163 } | |
| 164 | |
| 165 void MouseUp(int x, int y) { | |
| 166 DCHECK(mouse_pressed_); | |
| 167 if (x != last_mouse_x_ || y != last_mouse_y_) | |
| 168 SendMouseEvent(WebInputEvent::MouseMove, x, y); | |
| 169 SendMouseEvent(WebInputEvent::MouseUp, x, y); | |
| 170 mouse_pressed_ = false; | |
| 171 } | |
| 172 | |
| 173 private: | |
| 174 scoped_ptr<TouchEmulator> emulator_; | |
| 175 std::vector<WebInputEvent::Type> forwarded_events_; | |
| 176 #if defined(USE_AURA) | |
| 177 scoped_ptr<gfx::Screen> screen_; | |
| 178 #endif | |
| 179 double last_event_time_seconds_; | |
| 180 double event_time_delta_seconds_; | |
| 181 bool shift_pressed_; | |
| 182 bool mouse_pressed_; | |
| 183 int last_mouse_x_; | |
| 184 int last_mouse_y_; | |
| 185 base::MessageLoopForUI message_loop_; | |
| 186 }; | |
| 187 | |
| 188 | |
| 189 TEST_F(TouchEmulatorTest, NoTouches) { | |
| 190 MouseMove(100, 200); | |
| 191 MouseMove(300, 300); | |
| 192 EXPECT_EQ("", ExpectedEvents()); | |
| 193 } | |
| 194 | |
| 195 TEST_F(TouchEmulatorTest, Touch) { | |
| 196 MouseMove(100, 200); | |
| 197 EXPECT_EQ("", ExpectedEvents()); | |
| 198 MouseDown(100, 200); | |
| 199 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); | |
| 200 MouseUp(200, 200); | |
| 201 EXPECT_EQ( | |
| 202 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate" | |
| 203 " TouchEnd GestureScrollEnd", | |
| 204 ExpectedEvents()); | |
| 205 } | |
| 206 | |
| 207 TEST_F(TouchEmulatorTest, MultipleTouches) { | |
| 208 MouseMove(100, 200); | |
| 209 EXPECT_EQ("", ExpectedEvents()); | |
| 210 MouseDown(100, 200); | |
| 211 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); | |
| 212 MouseUp(200, 200); | |
| 213 EXPECT_EQ( | |
| 214 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate" | |
| 215 " TouchEnd GestureScrollEnd", | |
| 216 ExpectedEvents()); | |
| 217 MouseMove(300, 200); | |
| 218 MouseMove(200, 200); | |
| 219 EXPECT_EQ("", ExpectedEvents()); | |
| 220 MouseDown(300, 200); | |
| 221 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); | |
| 222 MouseDrag(300, 300); | |
| 223 EXPECT_EQ( | |
| 224 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", | |
| 225 ExpectedEvents()); | |
| 226 MouseDrag(300, 400); | |
| 227 EXPECT_EQ("TouchMove GestureScrollUpdate", ExpectedEvents()); | |
| 228 MouseUp(300, 500); | |
| 229 EXPECT_EQ( | |
| 230 "TouchMove GestureScrollUpdate TouchEnd GestureScrollEnd", | |
| 231 ExpectedEvents()); | |
| 232 } | |
| 233 | |
| 234 TEST_F(TouchEmulatorTest, Pinch) { | |
| 235 MouseDown(100, 200); | |
| 236 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); | |
| 237 MouseDrag(200, 200); | |
| 238 EXPECT_EQ( | |
| 239 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", | |
| 240 ExpectedEvents()); | |
| 241 PressShift(); | |
| 242 EXPECT_EQ("", ExpectedEvents()); | |
| 243 MouseDrag(300, 200); | |
| 244 EXPECT_EQ("TouchMove GesturePinchBegin", ExpectedEvents()); | |
| 245 ReleaseShift(); | |
| 246 EXPECT_EQ("", ExpectedEvents()); | |
| 247 MouseDrag(400, 200); | |
| 248 EXPECT_EQ( | |
| 249 "TouchMove GesturePinchEnd GestureScrollUpdate", | |
| 250 ExpectedEvents()); | |
| 251 MouseUp(400, 200); | |
| 252 EXPECT_EQ("TouchEnd GestureScrollEnd", ExpectedEvents()); | |
| 253 } | |
| 254 | |
| 255 TEST_F(TouchEmulatorTest, DisableAndReenable) { | |
| 256 MouseDown(100, 200); | |
| 257 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); | |
| 258 MouseDrag(200, 200); | |
| 259 EXPECT_EQ( | |
| 260 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", | |
| 261 ExpectedEvents()); | |
| 262 PressShift(); | |
| 263 MouseDrag(300, 200); | |
| 264 EXPECT_EQ("TouchMove GesturePinchBegin", ExpectedEvents()); | |
| 265 | |
| 266 // Disable while pinch is in progress. | |
| 267 emulator()->Disable(); | |
| 268 EXPECT_EQ("TouchCancel GesturePinchEnd GestureScrollEnd", ExpectedEvents()); | |
| 269 MouseUp(300, 200); | |
| 270 ReleaseShift(); | |
| 271 MouseMove(300, 300); | |
| 272 EXPECT_EQ("", ExpectedEvents()); | |
| 273 | |
| 274 emulator()->Enable(true /* allow_pinch */); | |
| 275 MouseDown(300, 300); | |
| 276 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); | |
| 277 MouseDrag(300, 400); | |
| 278 EXPECT_EQ( | |
| 279 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", | |
| 280 ExpectedEvents()); | |
| 281 | |
| 282 // Disable while scroll is in progress. | |
| 283 emulator()->Disable(); | |
| 284 EXPECT_EQ("TouchCancel GestureScrollEnd", ExpectedEvents()); | |
| 285 } | |
| 286 | |
| 287 TEST_F(TouchEmulatorTest, MouseMovesDropped) { | |
| 288 MouseMove(100, 200); | |
| 289 EXPECT_EQ("", ExpectedEvents()); | |
| 290 MouseDown(100, 200); | |
| 291 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); | |
| 292 | |
| 293 // Mouse move after mouse down is never dropped. | |
| 294 set_event_time_delta_seconds_(0.001); | |
| 295 MouseDrag(200, 200); | |
| 296 EXPECT_EQ( | |
| 297 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", | |
| 298 ExpectedEvents()); | |
| 299 | |
| 300 // The following mouse moves are dropped. | |
| 301 MouseDrag(300, 200); | |
| 302 EXPECT_EQ("", ExpectedEvents()); | |
| 303 MouseDrag(350, 200); | |
| 304 EXPECT_EQ("", ExpectedEvents()); | |
| 305 | |
| 306 // Dispatching again. | |
| 307 set_event_time_delta_seconds_(0.1); | |
| 308 MouseDrag(400, 200); | |
| 309 EXPECT_EQ( | |
| 310 "TouchMove GestureScrollUpdate", | |
| 311 ExpectedEvents()); | |
| 312 MouseUp(400, 200); | |
| 313 EXPECT_EQ( | |
| 314 "TouchEnd GestureScrollEnd", | |
| 315 ExpectedEvents()); | |
| 316 } | |
| 317 | |
| 318 } // namespace content | |
| OLD | NEW |