- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / input_router_unittest.cc
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
10 using WebKit::WebGestureEvent;
11 using WebKit::WebInputEvent;
12 using WebKit::WebMouseEvent;
13 using WebKit::WebMouseWheelEvent;
14 using WebKit::WebTouchEvent;
15 using WebKit::WebTouchPoint;
16
17 namespace content {
18
19 InputRouterTest::InputRouterTest() {}
20 InputRouterTest::~InputRouterTest() {}
21
22 void InputRouterTest::SetUp() {
23   browser_context_.reset(new TestBrowserContext());
24   process_.reset(new MockRenderProcessHost(browser_context_.get()));
25   client_.reset(new MockInputRouterClient());
26   ack_handler_.reset(new MockInputAckHandler());
27   input_router_ = CreateInputRouter(process_.get(),
28                                     client_.get(),
29                                     ack_handler_.get(),
30                                     MSG_ROUTING_NONE);
31   client_->set_input_router(input_router_.get());
32   ack_handler_->set_input_router(input_router_.get());
33 }
34
35 void InputRouterTest::TearDown() {
36   // Process all pending tasks to avoid InputRouterTest::leaks.
37   base::MessageLoop::current()->RunUntilIdle();
38
39   input_router_.reset();
40   client_.reset();
41   process_.reset();
42   browser_context_.reset();
43 }
44
45 void InputRouterTest::SimulateKeyboardEvent(WebInputEvent::Type type,
46                                             bool is_shortcut) {
47   input_router_->SendKeyboardEvent(
48       SyntheticWebKeyboardEventBuilder::Build(type),
49       ui::LatencyInfo(),
50       is_shortcut);
51 }
52
53 void InputRouterTest::SimulateWheelEvent(float dX,
54                                          float dY,
55                                          int modifiers,
56                                          bool precise) {
57   input_router_->SendWheelEvent(
58       MouseWheelEventWithLatencyInfo(
59           SyntheticWebMouseWheelEventBuilder::Build(dX, dY, modifiers, precise),
60           ui::LatencyInfo()));
61 }
62
63 void InputRouterTest::SimulateMouseMove(int x, int y, int modifiers) {
64   input_router_->SendMouseEvent(
65       MouseEventWithLatencyInfo(SyntheticWebMouseEventBuilder::Build(
66                                     WebInputEvent::MouseMove, x, y, modifiers),
67                                 ui::LatencyInfo()));
68 }
69
70 void InputRouterTest::SimulateWheelEventWithPhase(
71     WebMouseWheelEvent::Phase phase) {
72   input_router_->SendWheelEvent(
73       MouseWheelEventWithLatencyInfo(
74           SyntheticWebMouseWheelEventBuilder::Build(phase), ui::LatencyInfo()));
75 }
76
77 // Inject provided synthetic WebGestureEvent instance.
78 void InputRouterTest::SimulateGestureEvent(
79     const WebGestureEvent& gesture) {
80   GestureEventWithLatencyInfo gesture_with_latency(gesture, ui::LatencyInfo());
81   input_router_->SendGestureEvent(gesture_with_latency);
82 }
83
84 // Inject simple synthetic WebGestureEvent instances.
85 void InputRouterTest::SimulateGestureEvent(
86     WebInputEvent::Type type,
87     WebGestureEvent::SourceDevice sourceDevice) {
88   SimulateGestureEvent(
89       SyntheticWebGestureEventBuilder::Build(type, sourceDevice));
90 }
91
92 void InputRouterTest::SimulateGestureScrollUpdateEvent(float dX,
93                                                        float dY,
94                                                        int modifiers) {
95   SimulateGestureEvent(
96       SyntheticWebGestureEventBuilder::BuildScrollUpdate(dX, dY, modifiers));
97 }
98
99 void InputRouterTest::SimulateGesturePinchUpdateEvent(float scale,
100                                                       float anchorX,
101                                                       float anchorY,
102                                                       int modifiers) {
103   SimulateGestureEvent(
104       SyntheticWebGestureEventBuilder::BuildPinchUpdate(scale,
105                                                         anchorX,
106                                                         anchorY,
107                                                         modifiers));
108 }
109
110 // Inject synthetic GestureFlingStart events.
111 void InputRouterTest::SimulateGestureFlingStartEvent(
112     float velocityX,
113     float velocityY,
114     WebGestureEvent::SourceDevice sourceDevice) {
115   SimulateGestureEvent(
116       SyntheticWebGestureEventBuilder::BuildFling(velocityX,
117                                                   velocityY,
118                                                   sourceDevice));
119 }
120
121 void InputRouterTest::SimulateTouchEvent(int x, int y) {
122   PressTouchPoint(x, y);
123   SendTouchEvent();
124 }
125
126 // Set the timestamp for the touch-event.
127 void InputRouterTest::SetTouchTimestamp(base::TimeDelta timestamp) {
128   touch_event_.SetTimestamp(timestamp);
129 }
130
131 // Sends a touch event (irrespective of whether the page has a touch-event
132 // handler or not).
133 void InputRouterTest::SendTouchEvent() {
134   input_router_->SendTouchEvent(
135       TouchEventWithLatencyInfo(touch_event_, ui::LatencyInfo()));
136   touch_event_.ResetPoints();
137 }
138
139 int InputRouterTest::PressTouchPoint(int x, int y) {
140   return touch_event_.PressPoint(x, y);
141 }
142
143 void InputRouterTest::MoveTouchPoint(int index, int x, int y) {
144   touch_event_.MovePoint(index, x, y);
145 }
146
147 void InputRouterTest::ReleaseTouchPoint(int index) {
148   touch_event_.ReleasePoint(index);
149 }
150
151 }  // namespace content