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 } |
| 51 |
| 52 virtual void TearDown() OVERRIDE { |
| 53 std::string expected = pinching_ ? "GesturePinchEnd GestureScrollEnd" : |
| 54 (scrolling_ ? "GestureScrollEnd" : ""); |
| 55 emulator_.reset(); |
| 56 EXPECT_EQ(expected, ExpectedEvents()); |
| 57 } |
| 58 |
| 59 virtual void ForwardGestureEvent( |
| 60 const blink::WebGestureEvent& event) OVERRIDE { |
| 61 if (event.type == WebInputEvent::GestureScrollBegin) |
| 62 scrolling_ = true; |
| 63 if (event.type == WebInputEvent::GestureScrollEnd) |
| 64 scrolling_ = false; |
| 65 if (event.type == WebInputEvent::GestureFlingStart) |
| 66 scrolling_ = false; |
| 67 if (event.type == WebInputEvent::GesturePinchBegin) |
| 68 pinching_ = true; |
| 69 if (event.type == WebInputEvent::GesturePinchEnd) |
| 70 pinching_ = false; |
| 71 forwarded_events_.push_back(event.type); |
| 72 } |
| 73 |
| 74 virtual void ForwardTouchEvent( |
| 75 const blink::WebTouchEvent& event) 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 int modifiers() const { |
| 88 return shift_pressed_ ? WebInputEvent::ShiftKey : 0; |
| 89 } |
| 90 |
| 91 std::string ExpectedEvents() { |
| 92 std::string result; |
| 93 for (size_t i = 0; i < forwarded_events_.size(); ++i) { |
| 94 if (i != 0) |
| 95 result += " "; |
| 96 result += WebInputEventTraits::GetName(forwarded_events_[i]); |
| 97 } |
| 98 forwarded_events_.clear(); |
| 99 return result; |
| 100 } |
| 101 |
| 102 void SendKeyboardEvent() { |
| 103 WebKeyboardEvent event; |
| 104 event.timeStampSeconds = base::Time::Now().ToDoubleT(); |
| 105 event.type = WebInputEvent::KeyDown; |
| 106 event.modifiers = modifiers(); |
| 107 emulator()->HandleKeyboardEvent(event); |
| 108 event.type = WebInputEvent::KeyUp; |
| 109 event.modifiers = modifiers(); |
| 110 emulator()->HandleKeyboardEvent(event); |
| 111 } |
| 112 |
| 113 void PressShift() { |
| 114 DCHECK(!shift_pressed_); |
| 115 shift_pressed_ = true; |
| 116 SendKeyboardEvent(); |
| 117 } |
| 118 |
| 119 void ReleaseShift() { |
| 120 DCHECK(shift_pressed_); |
| 121 shift_pressed_ = false; |
| 122 SendKeyboardEvent(); |
| 123 } |
| 124 |
| 125 void SendMouseEvent(WebInputEvent::Type type, int x, int y) { |
| 126 WebMouseEvent event; |
| 127 event.timeStampSeconds = base::Time::Now().ToDoubleT(); |
| 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 bool shift_pressed_; |
| 169 bool mouse_pressed_; |
| 170 int last_mouse_x_; |
| 171 int last_mouse_y_; |
| 172 bool scrolling_; |
| 173 bool pinching_; |
| 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 MouseUp(200, 200); |
| 189 EXPECT_EQ( |
| 190 "TouchStart GestureTapDown TouchMove GestureTapCancel" |
| 191 " GestureScrollBegin GestureScrollUpdate TouchEnd GestureFlingStart", |
| 192 ExpectedEvents()); |
| 193 } |
| 194 |
| 195 TEST_F(TouchEmulatorTest, MultipleTouches) { |
| 196 MouseMove(100, 200); |
| 197 EXPECT_EQ("", ExpectedEvents()); |
| 198 MouseDown(100, 200); |
| 199 MouseUp(200, 200); |
| 200 EXPECT_EQ( |
| 201 "TouchStart GestureTapDown TouchMove GestureTapCancel" |
| 202 " GestureScrollBegin GestureScrollUpdate TouchEnd GestureFlingStart", |
| 203 ExpectedEvents()); |
| 204 MouseMove(300, 200); |
| 205 MouseMove(200, 200); |
| 206 EXPECT_EQ("", ExpectedEvents()); |
| 207 MouseDown(300, 200); |
| 208 EXPECT_EQ("TouchStart GestureFlingCancel GestureTapDown", ExpectedEvents()); |
| 209 MouseDrag(300, 300); |
| 210 EXPECT_EQ( |
| 211 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", |
| 212 ExpectedEvents()); |
| 213 MouseDrag(300, 400); |
| 214 EXPECT_EQ("TouchMove GestureScrollUpdate", ExpectedEvents()); |
| 215 MouseUp(300, 500); |
| 216 EXPECT_EQ( |
| 217 "TouchMove GestureScrollUpdate TouchEnd GestureScrollEnd", |
| 218 ExpectedEvents()); |
| 219 } |
| 220 |
| 221 TEST_F(TouchEmulatorTest, Pinch) { |
| 222 MouseDown(100, 200); |
| 223 MouseDrag(200, 200); |
| 224 PressShift(); |
| 225 MouseDrag(300, 200); |
| 226 EXPECT_EQ( |
| 227 "TouchStart GestureTapDown TouchMove GestureTapCancel" |
| 228 " GestureScrollBegin GestureScrollUpdate TouchMove GesturePinchBegin", |
| 229 ExpectedEvents()); |
| 230 ReleaseShift(); |
| 231 MouseDrag(400, 200); |
| 232 MouseUp(400, 200); |
| 233 EXPECT_EQ( |
| 234 "TouchMove GesturePinchEnd GestureScrollUpdate TouchEnd GestureScrollEnd", |
| 235 ExpectedEvents()); |
| 236 } |
| 237 |
| 238 TEST_F(TouchEmulatorTest, DestroyWhilePinch) { |
| 239 MouseDown(100, 200); |
| 240 MouseDrag(200, 200); |
| 241 PressShift(); |
| 242 MouseDrag(300, 200); |
| 243 EXPECT_EQ( |
| 244 "TouchStart GestureTapDown TouchMove GestureTapCancel" |
| 245 " GestureScrollBegin GestureScrollUpdate TouchMove GesturePinchBegin", |
| 246 ExpectedEvents()); |
| 247 } |
| 248 |
| 249 } // namespace content |
OLD | NEW |