Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / common / input / web_input_event_traits_unittest.cc
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 "content/common/input/web_input_event_traits.h"
6
7 #include <limits>
8
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/public/web/WebInputEvent.h"
11
12 using blink::WebGestureEvent;
13 using blink::WebInputEvent;
14 using blink::WebKeyboardEvent;
15 using blink::WebMouseEvent;
16 using blink::WebMouseWheelEvent;
17 using blink::WebTouchEvent;
18 using blink::WebTouchPoint;
19 using std::numeric_limits;
20
21 namespace content {
22 namespace {
23
24 class WebInputEventTraitsTest : public testing::Test {
25  protected:
26   static WebTouchPoint CreateTouchPoint(WebTouchPoint::State state, int id) {
27     WebTouchPoint touch;
28     touch.state = state;
29     touch.id = id;
30     return touch;
31   }
32
33   static WebTouchEvent CreateTouch(WebInputEvent::Type type) {
34     return CreateTouch(type, 1);
35   }
36
37   static WebTouchEvent CreateTouch(WebInputEvent::Type type,
38                                    unsigned touch_count) {
39     WebTouchEvent event;
40     event.touchesLength = touch_count;
41     event.type = type;
42     return event;
43   }
44
45   static WebGestureEvent CreateGesture(WebInputEvent::Type type,
46                                        float x,
47                                        float y) {
48     WebGestureEvent event;
49     event.type = type;
50     event.x = x;
51     event.y = y;
52     return event;
53   }
54 };
55
56 TEST_F(WebInputEventTraitsTest, TouchEventCoalescing) {
57   WebTouchEvent touch0 = CreateTouch(WebInputEvent::TouchStart);
58   WebTouchEvent touch1 = CreateTouch(WebInputEvent::TouchMove);
59
60   // Non touch-moves won't coalesce.
61   EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch0));
62
63   // Touches of different types won't coalesce.
64   EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1));
65
66   // Touch moves with idential touch lengths and touch ids should coalesce.
67   EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch1, touch1));
68
69   // Touch moves with different touch ids should not coalesce.
70   touch0 = CreateTouch(WebInputEvent::TouchMove);
71   touch1 = CreateTouch(WebInputEvent::TouchMove);
72   touch0.touches[0].id = 7;
73   EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1));
74   touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
75   touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
76   touch0.touches[0].id = 1;
77   touch1.touches[0].id = 0;
78   EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1));
79
80   // Touch moves with different touch lengths should not coalesce.
81   touch0 = CreateTouch(WebInputEvent::TouchMove, 1);
82   touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
83   EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1));
84
85   // Touch moves with identical touch ids in different orders should coalesce.
86   touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
87   touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
88   touch0.touches[0] = touch1.touches[1] =
89       CreateTouchPoint(WebTouchPoint::StateMoved, 1);
90   touch0.touches[1] = touch1.touches[0] =
91       CreateTouchPoint(WebTouchPoint::StateMoved, 0);
92   EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch0, touch1));
93
94   // Pointers with the same ID's should coalesce.
95   touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
96   touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
97   touch0.touches[0] = touch1.touches[1] =
98       CreateTouchPoint(WebTouchPoint::StateMoved, 1);
99   WebInputEventTraits::Coalesce(touch0, &touch1);
100   ASSERT_EQ(1, touch1.touches[0].id);
101   ASSERT_EQ(0, touch1.touches[1].id);
102   EXPECT_EQ(WebTouchPoint::StateUndefined, touch1.touches[1].state);
103   EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[0].state);
104
105   // Movement from now-stationary pointers should be preserved.
106   touch0 = touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
107   touch0.touches[0] = CreateTouchPoint(WebTouchPoint::StateMoved, 1);
108   touch1.touches[1] = CreateTouchPoint(WebTouchPoint::StateStationary, 1);
109   touch0.touches[1] = CreateTouchPoint(WebTouchPoint::StateStationary, 0);
110   touch1.touches[0] = CreateTouchPoint(WebTouchPoint::StateMoved, 0);
111   WebInputEventTraits::Coalesce(touch0, &touch1);
112   ASSERT_EQ(1, touch1.touches[0].id);
113   ASSERT_EQ(0, touch1.touches[1].id);
114   EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[0].state);
115   EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[1].state);
116 }
117
118 TEST_F(WebInputEventTraitsTest, PinchEventCoalescing) {
119   WebGestureEvent pinch0 =
120       CreateGesture(WebInputEvent::GesturePinchBegin, 1, 1);
121   WebGestureEvent pinch1 =
122       CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2);
123
124   // Only GesturePinchUpdate's coalesce.
125   EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch0));
126
127   // Pinch gestures of different types should not coalesce.
128   EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch1));
129
130   // Pinches with different focal points should not coalesce.
131   pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
132   pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2);
133   EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch1));
134   EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch0));
135
136   // Coalesced scales are multiplicative.
137   pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
138   pinch0.data.pinchUpdate.scale = 2.f;
139   pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
140   pinch1.data.pinchUpdate.scale = 3.f;
141   EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch0));
142   WebInputEventTraits::Coalesce(pinch0, &pinch1);
143   EXPECT_EQ(2.f * 3.f, pinch1.data.pinchUpdate.scale);
144
145   // Scales have a minimum value and can never reach 0.
146   ASSERT_GT(numeric_limits<float>::min(), 0);
147   pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
148   pinch0.data.pinchUpdate.scale = numeric_limits<float>::min() * 2.0f;
149   pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
150   pinch1.data.pinchUpdate.scale = numeric_limits<float>::min() * 5.0f;
151   EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch1));
152   WebInputEventTraits::Coalesce(pinch0, &pinch1);
153   EXPECT_EQ(numeric_limits<float>::min(), pinch1.data.pinchUpdate.scale);
154
155   // Scales have a maximum value and can never reach Infinity.
156   pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
157   pinch0.data.pinchUpdate.scale = numeric_limits<float>::max() / 2.0f;
158   pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
159   pinch1.data.pinchUpdate.scale = 10.0f;
160   EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch1));
161   WebInputEventTraits::Coalesce(pinch0, &pinch1);
162   EXPECT_EQ(numeric_limits<float>::max(), pinch1.data.pinchUpdate.scale);
163 }
164
165 // Very basic smoke test to ensure stringification doesn't explode.
166 TEST_F(WebInputEventTraitsTest, ToString) {
167   WebKeyboardEvent key;
168   key.type = WebInputEvent::RawKeyDown;
169   EXPECT_FALSE(WebInputEventTraits::ToString(key).empty());
170
171   WebMouseEvent mouse;
172   mouse.type = WebInputEvent::MouseMove;
173   EXPECT_FALSE(WebInputEventTraits::ToString(mouse).empty());
174
175   WebMouseWheelEvent mouse_wheel;
176   mouse_wheel.type = WebInputEvent::MouseWheel;
177   EXPECT_FALSE(WebInputEventTraits::ToString(mouse_wheel).empty());
178
179   WebGestureEvent gesture =
180       CreateGesture(WebInputEvent::GesturePinchBegin, 1, 1);
181   EXPECT_FALSE(WebInputEventTraits::ToString(gesture).empty());
182
183   WebTouchEvent touch = CreateTouch(WebInputEvent::TouchStart);
184   EXPECT_FALSE(WebInputEventTraits::ToString(touch).empty());
185 }
186
187 }  // namespace
188 }  // namespace content