| OLD | NEW | 
| (Empty) |  | 
 |    1 // Copyright 2013 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 "content/browser/renderer_host/input/input_router_unittest.h" | 
 |    6  | 
 |    7 #include "content/browser/renderer_host/input/input_router.h" | 
 |    8 #include "content/common/input_messages.h" | 
 |    9 #include "ui/base/keycodes/keyboard_codes.h" | 
 |   10  | 
 |   11 #if defined(OS_WIN) || defined(USE_AURA) | 
 |   12 #include "content/browser/renderer_host/ui_events_helper.h" | 
 |   13 #include "ui/base/events/event.h" | 
 |   14 #endif | 
 |   15  | 
 |   16 using WebKit::WebGestureEvent; | 
 |   17 using WebKit::WebInputEvent; | 
 |   18 using WebKit::WebMouseEvent; | 
 |   19 using WebKit::WebMouseWheelEvent; | 
 |   20 using WebKit::WebTouchEvent; | 
 |   21 using WebKit::WebTouchPoint; | 
 |   22  | 
 |   23 namespace content { | 
 |   24  | 
 |   25 InputRouterTest::InputRouterTest() {} | 
 |   26 InputRouterTest::~InputRouterTest() {} | 
 |   27  | 
 |   28 void InputRouterTest::SetUp() { | 
 |   29   browser_context_.reset(new TestBrowserContext()); | 
 |   30   process_.reset(new MockRenderProcessHost(browser_context_.get())); | 
 |   31   client_.reset(new MockInputRouterClient()); | 
 |   32   ack_handler_.reset(new MockInputAckHandler()); | 
 |   33   input_router_ = CreateInputRouter(process_.get(), | 
 |   34                                     client_.get(), | 
 |   35                                     ack_handler_.get(), | 
 |   36                                     MSG_ROUTING_NONE); | 
 |   37   client_->set_input_router(input_router_.get()); | 
 |   38   ack_handler_->set_input_router(input_router_.get()); | 
 |   39 } | 
 |   40  | 
 |   41 void InputRouterTest::TearDown() { | 
 |   42   // Process all pending tasks to avoid InputRouterTest::leaks. | 
 |   43   base::MessageLoop::current()->RunUntilIdle(); | 
 |   44  | 
 |   45   input_router_.reset(); | 
 |   46   client_.reset(); | 
 |   47   process_.reset(); | 
 |   48   browser_context_.reset(); | 
 |   49 } | 
 |   50  | 
 |   51 void InputRouterTest::SimulateKeyboardEvent(WebInputEvent::Type type) { | 
 |   52   NativeWebKeyboardEvent key_event; | 
 |   53   key_event.type = type; | 
 |   54   key_event.windowsKeyCode = ui::VKEY_L;  // non-null made up value. | 
 |   55   input_router_->SendKeyboardEvent(key_event, ui::LatencyInfo()); | 
 |   56   client_->ExpectSendCalled(true); | 
 |   57   EXPECT_EQ(type, client_->sent_key_event().type); | 
 |   58   EXPECT_EQ(key_event.windowsKeyCode, | 
 |   59             client_->sent_key_event().windowsKeyCode); | 
 |   60 } | 
 |   61  | 
 |   62 void InputRouterTest::SimulateWheelEvent(float dX, | 
 |   63                                          float dY, | 
 |   64                                          int modifiers, | 
 |   65                                          bool precise) { | 
 |   66   WebMouseWheelEvent wheel_event; | 
 |   67   wheel_event.type = WebInputEvent::MouseWheel; | 
 |   68   wheel_event.deltaX = dX; | 
 |   69   wheel_event.deltaY = dY; | 
 |   70   wheel_event.modifiers = modifiers; | 
 |   71   wheel_event.hasPreciseScrollingDeltas = precise; | 
 |   72   input_router_->SendWheelEvent( | 
 |   73       MouseWheelEventWithLatencyInfo(wheel_event, ui::LatencyInfo())); | 
 |   74   client_->ExpectSendCalled(true); | 
 |   75   EXPECT_EQ(wheel_event.type, client_->sent_wheel_event().event.type); | 
 |   76   EXPECT_EQ(dX, client_->sent_wheel_event().event.deltaX); | 
 |   77 } | 
 |   78  | 
 |   79 void InputRouterTest::SimulateMouseMove(int x, int y, int modifiers) { | 
 |   80   WebMouseEvent mouse_event; | 
 |   81   mouse_event.type = WebInputEvent::MouseMove; | 
 |   82   mouse_event.x = mouse_event.windowX = x; | 
 |   83   mouse_event.y = mouse_event.windowY = y; | 
 |   84   mouse_event.modifiers = modifiers; | 
 |   85   input_router_->SendMouseEvent( | 
 |   86       MouseEventWithLatencyInfo(mouse_event, ui::LatencyInfo())); | 
 |   87   client_->ExpectSendCalled(true); | 
 |   88   EXPECT_EQ(mouse_event.type, client_->sent_mouse_event().event.type); | 
 |   89   EXPECT_EQ(x, client_->sent_mouse_event().event.x); | 
 |   90 } | 
 |   91  | 
 |   92 void InputRouterTest::SimulateWheelEventWithPhase( | 
 |   93     WebMouseWheelEvent::Phase phase) { | 
 |   94   WebMouseWheelEvent wheel_event; | 
 |   95   wheel_event.type = WebInputEvent::MouseWheel; | 
 |   96   wheel_event.phase = phase; | 
 |   97   input_router_->SendWheelEvent( | 
 |   98       MouseWheelEventWithLatencyInfo(wheel_event, ui::LatencyInfo())); | 
 |   99   client_->ExpectSendCalled(true); | 
 |  100   EXPECT_EQ(wheel_event.type, client_->sent_wheel_event().event.type); | 
 |  101   EXPECT_EQ(phase, client_->sent_wheel_event().event.phase); | 
 |  102 } | 
 |  103  | 
 |  104 // Inject provided synthetic WebGestureEvent instance. | 
 |  105 void InputRouterTest::SimulateGestureEventCore(WebInputEvent::Type type, | 
 |  106                           WebGestureEvent::SourceDevice sourceDevice, | 
 |  107                           WebGestureEvent* gesture_event) { | 
 |  108   gesture_event->type = type; | 
 |  109   gesture_event->sourceDevice = sourceDevice; | 
 |  110   GestureEventWithLatencyInfo gesture_with_latency( | 
 |  111       *gesture_event, ui::LatencyInfo()); | 
 |  112   input_router_->SendGestureEvent(gesture_with_latency); | 
 |  113   client_->ExpectSendCalled(true); | 
 |  114   EXPECT_EQ(type, client_->sent_gesture_event().event.type); | 
 |  115   EXPECT_EQ(sourceDevice, client_->sent_gesture_event().event.sourceDevice); | 
 |  116 } | 
 |  117  | 
 |  118 // Inject simple synthetic WebGestureEvent instances. | 
 |  119 void InputRouterTest::SimulateGestureEvent(WebInputEvent::Type type, | 
 |  120                           WebGestureEvent::SourceDevice sourceDevice) { | 
 |  121   WebGestureEvent gesture_event; | 
 |  122   SimulateGestureEventCore(type, sourceDevice, &gesture_event); | 
 |  123 } | 
 |  124  | 
 |  125 void InputRouterTest::SimulateGestureScrollUpdateEvent(float dX, | 
 |  126                                                        float dY, | 
 |  127                                                        int modifiers) { | 
 |  128   WebGestureEvent gesture_event; | 
 |  129   gesture_event.data.scrollUpdate.deltaX = dX; | 
 |  130   gesture_event.data.scrollUpdate.deltaY = dY; | 
 |  131   gesture_event.modifiers = modifiers; | 
 |  132   SimulateGestureEventCore(WebInputEvent::GestureScrollUpdate, | 
 |  133                            WebGestureEvent::Touchscreen, &gesture_event); | 
 |  134 } | 
 |  135  | 
 |  136 void InputRouterTest::SimulateGesturePinchUpdateEvent(float scale, | 
 |  137                                                       float anchorX, | 
 |  138                                                       float anchorY, | 
 |  139                                                       int modifiers) { | 
 |  140   WebGestureEvent gesture_event; | 
 |  141   gesture_event.data.pinchUpdate.scale = scale; | 
 |  142   gesture_event.x = anchorX; | 
 |  143   gesture_event.y = anchorY; | 
 |  144   gesture_event.modifiers = modifiers; | 
 |  145   SimulateGestureEventCore(WebInputEvent::GesturePinchUpdate, | 
 |  146                            WebGestureEvent::Touchscreen, &gesture_event); | 
 |  147 } | 
 |  148  | 
 |  149 // Inject synthetic GestureFlingStart events. | 
 |  150 void InputRouterTest::SimulateGestureFlingStartEvent( | 
 |  151     float velocityX, | 
 |  152     float velocityY, | 
 |  153     WebGestureEvent::SourceDevice sourceDevice) { | 
 |  154   WebGestureEvent gesture_event; | 
 |  155   gesture_event.data.flingStart.velocityX = velocityX; | 
 |  156   gesture_event.data.flingStart.velocityY = velocityY; | 
 |  157   SimulateGestureEventCore(WebInputEvent::GestureFlingStart, sourceDevice, | 
 |  158                            &gesture_event); | 
 |  159 } | 
 |  160  | 
 |  161 void InputRouterTest::SimulateTouchEvent( | 
 |  162     int x, | 
 |  163     int y) { | 
 |  164   PressTouchPoint(x, y); | 
 |  165   SendTouchEvent(); | 
 |  166 } | 
 |  167  | 
 |  168 // Set the timestamp for the touch-event. | 
 |  169 void InputRouterTest::SetTouchTimestamp(base::TimeDelta timestamp) { | 
 |  170   touch_event_.timeStampSeconds = timestamp.InSecondsF(); | 
 |  171 } | 
 |  172  | 
 |  173 // Sends a touch event (irrespective of whether the page has a touch-event | 
 |  174 // handler or not). | 
 |  175 void InputRouterTest::SendTouchEvent() { | 
 |  176   input_router_->SendTouchEvent( | 
 |  177       TouchEventWithLatencyInfo(touch_event_, ui::LatencyInfo())); | 
 |  178  | 
 |  179   // Mark all the points as stationary. And remove the points that have been | 
 |  180   // released. | 
 |  181   int point = 0; | 
 |  182   for (unsigned int i = 0; i < touch_event_.touchesLength; ++i) { | 
 |  183     if (touch_event_.touches[i].state == WebTouchPoint::StateReleased) | 
 |  184       continue; | 
 |  185  | 
 |  186     touch_event_.touches[point] = touch_event_.touches[i]; | 
 |  187     touch_event_.touches[point].state = | 
 |  188         WebTouchPoint::StateStationary; | 
 |  189     ++point; | 
 |  190   } | 
 |  191   touch_event_.touchesLength = point; | 
 |  192   touch_event_.type = WebInputEvent::Undefined; | 
 |  193 } | 
 |  194  | 
 |  195 int InputRouterTest::PressTouchPoint(int x, int y) { | 
 |  196   if (touch_event_.touchesLength == touch_event_.touchesLengthCap) | 
 |  197     return -1; | 
 |  198   WebTouchPoint& point = | 
 |  199       touch_event_.touches[touch_event_.touchesLength]; | 
 |  200   point.id = touch_event_.touchesLength; | 
 |  201   point.position.x = point.screenPosition.x = x; | 
 |  202   point.position.y = point.screenPosition.y = y; | 
 |  203   point.state = WebTouchPoint::StatePressed; | 
 |  204   point.radiusX = point.radiusY = 1.f; | 
 |  205   ++touch_event_.touchesLength; | 
 |  206   touch_event_.type = WebInputEvent::TouchStart; | 
 |  207   return point.id; | 
 |  208 } | 
 |  209  | 
 |  210 void InputRouterTest::MoveTouchPoint(int index, int x, int y) { | 
 |  211   CHECK(index >= 0 && index < touch_event_.touchesLengthCap); | 
 |  212   WebTouchPoint& point = touch_event_.touches[index]; | 
 |  213   point.position.x = point.screenPosition.x = x; | 
 |  214   point.position.y = point.screenPosition.y = y; | 
 |  215   touch_event_.touches[index].state = WebTouchPoint::StateMoved; | 
 |  216   touch_event_.type = WebInputEvent::TouchMove; | 
 |  217 } | 
 |  218  | 
 |  219 void InputRouterTest::ReleaseTouchPoint(int index) { | 
 |  220   CHECK(index >= 0 && index < touch_event_.touchesLengthCap); | 
 |  221   touch_event_.touches[index].state = WebTouchPoint::StateReleased; | 
 |  222   touch_event_.type = WebInputEvent::TouchEnd; | 
 |  223 } | 
 |  224  | 
 |  225 }  // namespace content | 
| OLD | NEW |