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