OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 aura::Env::DeleteInstance(); | 64 aura::Env::DeleteInstance(); |
65 screen_.reset(); | 65 screen_.reset(); |
66 #endif | 66 #endif |
67 } | 67 } |
68 | 68 |
69 virtual void ForwardGestureEvent( | 69 virtual void ForwardGestureEvent( |
70 const blink::WebGestureEvent& event) OVERRIDE { | 70 const blink::WebGestureEvent& event) OVERRIDE { |
71 forwarded_events_.push_back(event.type); | 71 forwarded_events_.push_back(event.type); |
72 } | 72 } |
73 | 73 |
74 virtual void ForwardTouchEvent( | 74 virtual void ForwardEmulatedTouchEvent( |
75 const blink::WebTouchEvent& event) OVERRIDE { | 75 const blink::WebTouchEvent& event) OVERRIDE { |
76 forwarded_events_.push_back(event.type); | 76 forwarded_events_.push_back(event.type); |
77 EXPECT_EQ(1U, event.touchesLength); | 77 EXPECT_EQ(1U, event.touchesLength); |
78 EXPECT_EQ(last_mouse_x_, event.touches[0].position.x); | 78 EXPECT_EQ(last_mouse_x_, event.touches[0].position.x); |
79 EXPECT_EQ(last_mouse_y_, event.touches[0].position.y); | 79 EXPECT_EQ(last_mouse_y_, event.touches[0].position.y); |
80 int expectedCancelable = event.type != WebInputEvent::TouchCancel; | 80 int expectedCancelable = event.type != WebInputEvent::TouchCancel; |
81 EXPECT_EQ(expectedCancelable, event.cancelable); | 81 EXPECT_EQ(expectedCancelable, event.cancelable); |
82 emulator()->HandleTouchEventAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); | 82 emulator()->HandleTouchEventAck( |
| 83 event, INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); |
83 } | 84 } |
84 | 85 |
85 virtual void SetCursor(const WebCursor& cursor) OVERRIDE {} | 86 virtual void SetCursor(const WebCursor& cursor) OVERRIDE {} |
86 | 87 |
87 protected: | 88 protected: |
88 TouchEmulator* emulator() const { | 89 TouchEmulator* emulator() const { |
89 return emulator_.get(); | 90 return emulator_.get(); |
90 } | 91 } |
91 | 92 |
92 int modifiers() const { | 93 int modifiers() const { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } | 175 } |
175 | 176 |
176 void MouseUp(int x, int y) { | 177 void MouseUp(int x, int y) { |
177 DCHECK(mouse_pressed_); | 178 DCHECK(mouse_pressed_); |
178 if (x != last_mouse_x_ || y != last_mouse_y_) | 179 if (x != last_mouse_x_ || y != last_mouse_y_) |
179 SendMouseEvent(WebInputEvent::MouseMove, x, y); | 180 SendMouseEvent(WebInputEvent::MouseMove, x, y); |
180 SendMouseEvent(WebInputEvent::MouseUp, x, y); | 181 SendMouseEvent(WebInputEvent::MouseUp, x, y); |
181 mouse_pressed_ = false; | 182 mouse_pressed_ = false; |
182 } | 183 } |
183 | 184 |
| 185 bool TouchStart(int x, int y, bool ack) { |
| 186 return SendTouchEvent( |
| 187 WebInputEvent::TouchStart, WebTouchPoint::StatePressed, x, y, ack); |
| 188 } |
| 189 |
| 190 bool TouchMove(int x, int y, bool ack) { |
| 191 return SendTouchEvent( |
| 192 WebInputEvent::TouchMove, WebTouchPoint::StateMoved, x, y, ack); |
| 193 } |
| 194 |
| 195 bool TouchEnd(int x, int y, bool ack) { |
| 196 return SendTouchEvent( |
| 197 WebInputEvent::TouchEnd, WebTouchPoint::StateReleased, x, y, ack); |
| 198 } |
| 199 |
| 200 bool SendTouchEvent(WebInputEvent::Type type, WebTouchPoint::State state, |
| 201 int x, int y, bool ack) { |
| 202 WebTouchEvent event; |
| 203 event.type = type; |
| 204 event.timeStampSeconds = GetNextEventTimeSeconds(); |
| 205 event.touchesLength = 1; |
| 206 event.touches[0].id = 0; |
| 207 event.touches[0].state = state; |
| 208 event.touches[0].position.x = x; |
| 209 event.touches[0].position.y = y; |
| 210 event.touches[0].screenPosition.x = x; |
| 211 event.touches[0].screenPosition.y = y; |
| 212 if (emulator()->HandleTouchEvent(event)) { |
| 213 // Touch event is not forwarded. |
| 214 return false; |
| 215 } |
| 216 |
| 217 if (ack) { |
| 218 // Can't send ack if there are some pending acks. |
| 219 DCHECK(!touch_events_to_ack_.size()); |
| 220 |
| 221 // Touch event is forwarded, ack should not be handled by emulator. |
| 222 EXPECT_FALSE(emulator()->HandleTouchEventAck( |
| 223 event, INPUT_EVENT_ACK_STATE_CONSUMED)); |
| 224 } else { |
| 225 touch_events_to_ack_.push_back(event); |
| 226 } |
| 227 return true; |
| 228 } |
| 229 |
| 230 void AckOldestTouchEvent() { |
| 231 DCHECK(touch_events_to_ack_.size()); |
| 232 WebTouchEvent event = touch_events_to_ack_[0]; |
| 233 touch_events_to_ack_.erase(touch_events_to_ack_.begin()); |
| 234 // Emulator should not handle ack from native stream. |
| 235 EXPECT_FALSE(emulator()->HandleTouchEventAck( |
| 236 event, INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS)); |
| 237 } |
| 238 |
184 private: | 239 private: |
185 scoped_ptr<TouchEmulator> emulator_; | 240 scoped_ptr<TouchEmulator> emulator_; |
186 std::vector<WebInputEvent::Type> forwarded_events_; | 241 std::vector<WebInputEvent::Type> forwarded_events_; |
187 #if defined(USE_AURA) | 242 #if defined(USE_AURA) |
188 scoped_ptr<gfx::Screen> screen_; | 243 scoped_ptr<gfx::Screen> screen_; |
189 #endif | 244 #endif |
190 double last_event_time_seconds_; | 245 double last_event_time_seconds_; |
191 double event_time_delta_seconds_; | 246 double event_time_delta_seconds_; |
192 bool shift_pressed_; | 247 bool shift_pressed_; |
193 bool mouse_pressed_; | 248 bool mouse_pressed_; |
194 int last_mouse_x_; | 249 int last_mouse_x_; |
195 int last_mouse_y_; | 250 int last_mouse_y_; |
| 251 std::vector<WebTouchEvent> touch_events_to_ack_; |
196 base::MessageLoopForUI message_loop_; | 252 base::MessageLoopForUI message_loop_; |
197 }; | 253 }; |
198 | 254 |
199 | 255 |
200 TEST_F(TouchEmulatorTest, NoTouches) { | 256 TEST_F(TouchEmulatorTest, NoTouches) { |
201 MouseMove(100, 200); | 257 MouseMove(100, 200); |
202 MouseMove(300, 300); | 258 MouseMove(300, 300); |
203 EXPECT_EQ("", ExpectedEvents()); | 259 EXPECT_EQ("", ExpectedEvents()); |
204 } | 260 } |
205 | 261 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 MouseDown(300, 200); | 395 MouseDown(300, 200); |
340 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); | 396 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
341 EXPECT_FALSE(SendMouseWheelEvent()); | 397 EXPECT_FALSE(SendMouseWheelEvent()); |
342 emulator()->Disable(); | 398 emulator()->Disable(); |
343 EXPECT_EQ("TouchCancel GestureTapCancel", ExpectedEvents()); | 399 EXPECT_EQ("TouchCancel GestureTapCancel", ExpectedEvents()); |
344 EXPECT_TRUE(SendMouseWheelEvent()); | 400 EXPECT_TRUE(SendMouseWheelEvent()); |
345 emulator()->Enable(true /* allow_pinch */); | 401 emulator()->Enable(true /* allow_pinch */); |
346 EXPECT_TRUE(SendMouseWheelEvent()); | 402 EXPECT_TRUE(SendMouseWheelEvent()); |
347 } | 403 } |
348 | 404 |
| 405 TEST_F(TouchEmulatorTest, MultipleTouchStreams) { |
| 406 // Native stream should be blocked while emulated is active. |
| 407 MouseMove(100, 200); |
| 408 EXPECT_EQ("", ExpectedEvents()); |
| 409 MouseDown(100, 200); |
| 410 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
| 411 EXPECT_FALSE(TouchStart(10, 10, true)); |
| 412 EXPECT_FALSE(TouchMove(20, 20, true)); |
| 413 MouseUp(200, 200); |
| 414 EXPECT_EQ( |
| 415 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate" |
| 416 " TouchEnd GestureScrollEnd", |
| 417 ExpectedEvents()); |
| 418 EXPECT_FALSE(TouchEnd(20, 20, true)); |
| 419 |
| 420 // Emulated stream should be blocked while native is active. |
| 421 EXPECT_TRUE(TouchStart(10, 10, true)); |
| 422 EXPECT_TRUE(TouchMove(20, 20, true)); |
| 423 MouseDown(300, 200); |
| 424 EXPECT_EQ("", ExpectedEvents()); |
| 425 MouseDrag(300, 300); |
| 426 EXPECT_EQ("", ExpectedEvents()); |
| 427 MouseUp(300, 300); |
| 428 EXPECT_EQ("", ExpectedEvents()); |
| 429 EXPECT_TRUE(TouchEnd(20, 20, true)); |
| 430 EXPECT_EQ("", ExpectedEvents()); |
| 431 |
| 432 // Late ack for TouchEnd should not mess things up. |
| 433 EXPECT_TRUE(TouchStart(10, 10, false)); |
| 434 EXPECT_TRUE(TouchMove(20, 20, false)); |
| 435 EXPECT_TRUE(TouchEnd(20, 20, false)); |
| 436 EXPECT_TRUE(TouchStart(30, 30, false)); |
| 437 AckOldestTouchEvent(); // TouchStart. |
| 438 AckOldestTouchEvent(); // TouchMove. |
| 439 AckOldestTouchEvent(); // TouchEnd. |
| 440 MouseDown(300, 200); |
| 441 EXPECT_EQ("", ExpectedEvents()); |
| 442 MouseDrag(300, 300); |
| 443 EXPECT_EQ("", ExpectedEvents()); |
| 444 MouseUp(300, 300); |
| 445 EXPECT_EQ("", ExpectedEvents()); |
| 446 AckOldestTouchEvent(); // TouchStart. |
| 447 MouseDown(300, 200); |
| 448 EXPECT_EQ("", ExpectedEvents()); |
| 449 EXPECT_TRUE(TouchMove(30, 40, true)); |
| 450 EXPECT_TRUE(TouchEnd(30, 40, true)); |
| 451 MouseUp(300, 200); |
| 452 EXPECT_EQ("", ExpectedEvents()); |
| 453 |
| 454 // Emulation should be back to normal. |
| 455 MouseDown(100, 200); |
| 456 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); |
| 457 MouseUp(200, 200); |
| 458 EXPECT_EQ( |
| 459 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate" |
| 460 " TouchEnd GestureScrollEnd", |
| 461 ExpectedEvents()); |
| 462 } |
| 463 |
349 } // namespace content | 464 } // namespace content |
OLD | NEW |