Index: content/browser/renderer_host/input/touch_emulator_unittest.cc |
diff --git a/content/browser/renderer_host/input/touch_emulator_unittest.cc b/content/browser/renderer_host/input/touch_emulator_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3968dbc08401017aea6e5b223dd60304e4257ac6 |
--- /dev/null |
+++ b/content/browser/renderer_host/input/touch_emulator_unittest.cc |
@@ -0,0 +1,251 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <vector> |
+ |
+#include "base/basictypes.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/time/time.h" |
+#include "content/browser/renderer_host/input/touch_emulator.h" |
+#include "content/browser/renderer_host/input/touch_emulator_client.h" |
+#include "content/common/input/web_input_event_traits.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/events/gesture_detection/gesture_config_helper.h" |
+ |
+using blink::WebGestureEvent; |
+using blink::WebInputEvent; |
+using blink::WebKeyboardEvent; |
+using blink::WebMouseEvent; |
+using blink::WebTouchEvent; |
+using blink::WebTouchPoint; |
+ |
+namespace content { |
+ |
+class TouchEmulatorTest : public testing::Test, |
+ public TouchEmulatorClient { |
+ public: |
+ TouchEmulatorTest() |
+ : shift_pressed_(false), |
+ mouse_pressed_(false), |
+ last_mouse_x_(-1), |
+ last_mouse_y_(-1), |
+ scrolling_(false), |
+ pinching_(false) {} |
+ |
+ virtual ~TouchEmulatorTest() {} |
+ |
+ // testing::Test |
+ virtual void SetUp() OVERRIDE { |
+ ui::GestureProvider::Config config; |
+ config.gesture_detector_config = ui::DefaultGestureDetectorConfig(); |
+ config.scale_gesture_detector_config = |
+ ui::DefaultScaleGestureDetectorConfig(); |
+ config.snap_scroll_controller_config.screen_width_pixels = 800; |
+ config.snap_scroll_controller_config.screen_height_pixels = 600; |
+ config.snap_scroll_controller_config.device_scale_factor = 1.f; |
+ config.gesture_begin_end_types_enabled = false; |
+ emulator_.reset(new TouchEmulator(this, config)); |
+ emulator_->Enable(); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ std::string expected = pinching_ ? |
+ "TouchCancel GesturePinchEnd GestureScrollEnd" : |
+ (scrolling_ ? "TouchCancel GestureScrollEnd" : ""); |
+ emulator_->Disable(); |
+ EXPECT_EQ(expected, ExpectedEvents()); |
+ } |
+ |
+ virtual void ForwardGestureEvent( |
+ const blink::WebGestureEvent& event) OVERRIDE { |
+ if (event.type == WebInputEvent::GestureScrollBegin) |
+ scrolling_ = true; |
+ if (event.type == WebInputEvent::GestureScrollEnd) |
+ scrolling_ = false; |
+ if (event.type == WebInputEvent::GestureFlingStart) |
+ scrolling_ = false; |
+ if (event.type == WebInputEvent::GesturePinchBegin) |
+ pinching_ = true; |
+ if (event.type == WebInputEvent::GesturePinchEnd) |
+ pinching_ = false; |
+ forwarded_events_.push_back(event.type); |
+ } |
+ |
+ virtual void ForwardTouchEvent( |
+ const blink::WebTouchEvent& event) OVERRIDE { |
+ forwarded_events_.push_back(event.type); |
+ emulator()->HandleTouchEventAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); |
+ } |
+ |
+ virtual void SetCursor(const WebCursor& cursor) OVERRIDE {} |
+ |
+ protected: |
+ TouchEmulator* emulator() const { |
+ return emulator_.get(); |
+ } |
+ |
+ int modifiers() const { |
+ return shift_pressed_ ? WebInputEvent::ShiftKey : 0; |
+ } |
+ |
+ std::string ExpectedEvents() { |
+ std::string result; |
+ for (size_t i = 0; i < forwarded_events_.size(); ++i) { |
+ if (i != 0) |
+ result += " "; |
+ result += WebInputEventTraits::GetName(forwarded_events_[i]); |
+ } |
+ forwarded_events_.clear(); |
+ return result; |
+ } |
+ |
+ void SendKeyboardEvent() { |
+ WebKeyboardEvent event; |
+ event.timeStampSeconds = base::Time::Now().ToDoubleT(); |
+ event.type = WebInputEvent::KeyDown; |
+ event.modifiers = modifiers(); |
+ emulator()->HandleKeyboardEvent(event); |
+ event.type = WebInputEvent::KeyUp; // !!!!! |
+ event.modifiers = modifiers(); |
+ emulator()->HandleKeyboardEvent(event); |
+ } |
+ |
+ void PressShift() { |
+ DCHECK(!shift_pressed_); |
+ shift_pressed_ = true; |
+ SendKeyboardEvent(); |
+ } |
+ |
+ void ReleaseShift() { |
+ DCHECK(shift_pressed_); |
+ shift_pressed_ = false; |
+ SendKeyboardEvent(); |
+ } |
+ |
+ void SendMouseEvent(WebInputEvent::Type type, int x, int y) { |
+ WebMouseEvent event; |
+ event.timeStampSeconds = base::Time::Now().ToDoubleT(); |
+ event.type = type; |
+ event.button = mouse_pressed_ ? WebMouseEvent::ButtonLeft : |
+ WebMouseEvent::ButtonNone; |
+ event.modifiers = modifiers(); |
+ last_mouse_x_ = x; |
+ last_mouse_y_ = y; |
+ event.x = event.windowX = event.globalX = x; |
+ event.y = event.windowY = event.globalY = y; |
+ emulator()->HandleMouseEvent(event); |
jdduke (slow)
2014/04/08 19:14:11
One cheap addition here would be recording the tou
dgozman
2014/04/09 17:00:05
I've added the similar check to |ForwardTouchEvent
|
+ } |
+ |
+ void MouseDown(int x, int y) { |
+ DCHECK(!mouse_pressed_); |
+ if (x != last_mouse_x_ || y != last_mouse_y_) |
+ SendMouseEvent(WebInputEvent::MouseMove, x, y); |
+ mouse_pressed_ = true; |
+ SendMouseEvent(WebInputEvent::MouseDown, x, y); |
+ } |
+ |
+ void MouseDrag(int x, int y) { |
+ DCHECK(mouse_pressed_); |
+ SendMouseEvent(WebInputEvent::MouseMove, x, y); |
+ } |
+ |
+ void MouseMove(int x, int y) { |
+ DCHECK(!mouse_pressed_); |
+ SendMouseEvent(WebInputEvent::MouseMove, x, y); |
+ } |
+ |
+ void MouseUp(int x, int y) { |
+ DCHECK(mouse_pressed_); |
+ if (x != last_mouse_x_ || y != last_mouse_y_) |
+ SendMouseEvent(WebInputEvent::MouseMove, x, y); |
+ SendMouseEvent(WebInputEvent::MouseUp, x, y); |
+ mouse_pressed_ = false; |
+ } |
+ |
+ private: |
+ scoped_ptr<TouchEmulator> emulator_; |
+ std::vector<WebInputEvent::Type> forwarded_events_; |
+ bool shift_pressed_; |
+ bool mouse_pressed_; |
+ int last_mouse_x_; |
+ int last_mouse_y_; |
+ bool scrolling_; |
+ bool pinching_; |
+ base::MessageLoopForUI message_loop_; |
+}; |
+ |
+ |
+TEST_F(TouchEmulatorTest, NoTouches) { |
+ MouseMove(100, 200); |
+ MouseMove(300, 300); |
+ EXPECT_EQ("", ExpectedEvents()); |
+} |
+ |
+TEST_F(TouchEmulatorTest, Touch) { |
+ MouseMove(100, 200); |
+ EXPECT_EQ("", ExpectedEvents()); |
+ MouseDown(100, 200); |
+ MouseUp(200, 200); |
+ EXPECT_EQ( |
+ "TouchStart GestureTapDown TouchMove GestureTapCancel" |
+ " GestureScrollBegin GestureScrollUpdate TouchEnd GestureFlingStart", |
+ ExpectedEvents()); |
+} |
+ |
+TEST_F(TouchEmulatorTest, MultipleTouches) { |
+ MouseMove(100, 200); |
+ EXPECT_EQ("", ExpectedEvents()); |
+ MouseDown(100, 200); |
+ MouseUp(200, 200); |
+ EXPECT_EQ( |
+ "TouchStart GestureTapDown TouchMove GestureTapCancel" |
+ " GestureScrollBegin GestureScrollUpdate TouchEnd GestureFlingStart", |
+ ExpectedEvents()); |
+ MouseMove(300, 200); |
+ MouseMove(200, 200); |
+ EXPECT_EQ("", ExpectedEvents()); |
+ MouseDown(300, 200); |
+ EXPECT_EQ("TouchStart GestureFlingCancel GestureTapDown", ExpectedEvents()); |
+ MouseDrag(300, 300); |
+ EXPECT_EQ( |
+ "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate", |
+ ExpectedEvents()); |
+ MouseDrag(300, 400); |
+ EXPECT_EQ("TouchMove GestureScrollUpdate", ExpectedEvents()); |
+ MouseUp(300, 500); |
+ EXPECT_EQ( |
+ "TouchMove GestureScrollUpdate TouchEnd GestureScrollEnd", |
+ ExpectedEvents()); |
+} |
+ |
+TEST_F(TouchEmulatorTest, Pinch) { |
+ MouseDown(100, 200); |
+ MouseDrag(200, 200); |
+ PressShift(); |
+ MouseDrag(300, 200); |
+ EXPECT_EQ( |
+ "TouchStart GestureTapDown TouchMove GestureTapCancel" |
+ " GestureScrollBegin GestureScrollUpdate TouchMove GesturePinchBegin", |
+ ExpectedEvents()); |
+ ReleaseShift(); |
+ MouseDrag(400, 200); |
+ MouseUp(400, 200); |
+ EXPECT_EQ( |
+ "TouchMove GesturePinchEnd GestureScrollUpdate TouchEnd GestureScrollEnd", |
+ ExpectedEvents()); |
+} |
+ |
+TEST_F(TouchEmulatorTest, DestroyWhilePinch) { |
jdduke (slow)
2014/04/08 19:14:11
Maybe change this test to a very basic enable/disa
dgozman
2014/04/09 17:00:05
I previously assumed that emulator destructor (and
|
+ MouseDown(100, 200); |
+ MouseDrag(200, 200); |
+ PressShift(); |
+ MouseDrag(300, 200); |
+ EXPECT_EQ( |
+ "TouchStart GestureTapDown TouchMove GestureTapCancel" |
+ " GestureScrollBegin GestureScrollUpdate TouchMove GesturePinchBegin", |
+ ExpectedEvents()); |
+} |
+ |
+} // namespace content |