handoff: Fix the origin so that it correctly reflects the sender.
[chromium-blink-merge.git] / content / browser / renderer_host / input / motion_event_android_unittest.cc
blob20b9a5258af60d26668376ba3c8735b4504dd717
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.
5 #include <android/input.h>
7 #include "base/android/jni_android.h"
8 #include "base/float_util.h"
9 #include "content/browser/renderer_host/input/motion_event_android.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/events/event_constants.h"
12 #include "ui/events/test/motion_event_test_utils.h"
14 using ui::MotionEvent;
16 namespace content {
17 namespace {
18 const float kPixToDip = 0.5f;
20 int kAndroidActionDown = AMOTION_EVENT_ACTION_DOWN;
21 int kAndroidActionPointerDown = AMOTION_EVENT_ACTION_POINTER_DOWN;
22 int kAndroidAltKeyDown = AMETA_ALT_ON;
24 // Corresponds to TOOL_TYPE_FINGER, see
25 // developer.android.com/reference/android/view/MotionEvent.html
26 // #TOOL_TYPE_FINGER.
27 int kAndroidToolTypeFinger = 1;
29 // Corresponds to BUTTON_PRIMARY, see
30 // developer.android.com/reference/android/view/MotionEvent.html#BUTTON_PRIMARY.
31 int kAndroidButtonPrimary = 1;
33 } // namespace
35 // Note that these tests avoid creating a Java instance of the MotionEvent, as
36 // we're primarily testing caching behavior, and the code necessary to
37 // construct a Java-backed MotionEvent itself adds unnecessary complexity.
38 TEST(MotionEventAndroidTest, Constructor) {
39 int event_time_ms = 5;
40 base::TimeTicks event_time =
41 base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms);
42 MotionEventAndroid::Pointer p0(
43 1, 13.7f, -7.13f, 5.3f, 1.2f, 0.1f, kAndroidToolTypeFinger);
44 MotionEventAndroid::Pointer p1(
45 2, -13.7f, 7.13f, 3.5f, 12.1f, -0.1f, kAndroidToolTypeFinger);
46 float raw_offset = -3.f;
47 int pointer_count = 2;
48 int history_size = 0;
49 int action_index = -1;
50 MotionEventAndroid event(kPixToDip,
51 base::android::AttachCurrentThread(),
52 nullptr,
53 event_time_ms,
54 kAndroidActionDown,
55 pointer_count,
56 history_size,
57 action_index,
58 kAndroidButtonPrimary,
59 kAndroidAltKeyDown,
60 raw_offset,
61 -raw_offset,
62 p0,
63 p1);
65 EXPECT_EQ(MotionEvent::ACTION_DOWN, event.GetAction());
66 EXPECT_EQ(event_time, event.GetEventTime());
67 EXPECT_EQ(p0.pos_x_pixels * kPixToDip, event.GetX(0));
68 EXPECT_EQ(p0.pos_y_pixels * kPixToDip, event.GetY(0));
69 EXPECT_EQ(p1.pos_x_pixels * kPixToDip, event.GetX(1));
70 EXPECT_EQ(p1.pos_y_pixels * kPixToDip, event.GetY(1));
71 EXPECT_FLOAT_EQ((p0.pos_x_pixels + raw_offset) * kPixToDip, event.GetRawX(0));
72 EXPECT_FLOAT_EQ((p0.pos_y_pixels - raw_offset) * kPixToDip, event.GetRawY(0));
73 EXPECT_FLOAT_EQ((p1.pos_x_pixels + raw_offset) * kPixToDip, event.GetRawX(1));
74 EXPECT_FLOAT_EQ((p1.pos_y_pixels - raw_offset) * kPixToDip, event.GetRawY(1));
75 EXPECT_EQ(p0.touch_major_pixels * kPixToDip, event.GetTouchMajor(0));
76 EXPECT_EQ(p1.touch_major_pixels * kPixToDip, event.GetTouchMajor(1));
77 EXPECT_EQ(p0.touch_minor_pixels * kPixToDip, event.GetTouchMinor(0));
78 EXPECT_EQ(p1.touch_minor_pixels * kPixToDip, event.GetTouchMinor(1));
79 EXPECT_EQ(p0.orientation_rad, event.GetOrientation(0));
80 EXPECT_EQ(p1.orientation_rad, event.GetOrientation(1));
81 EXPECT_EQ(p0.id, event.GetPointerId(0));
82 EXPECT_EQ(p1.id, event.GetPointerId(1));
83 EXPECT_EQ(MotionEvent::TOOL_TYPE_FINGER, event.GetToolType(0));
84 EXPECT_EQ(MotionEvent::TOOL_TYPE_FINGER, event.GetToolType(1));
85 EXPECT_EQ(MotionEvent::BUTTON_PRIMARY, event.GetButtonState());
86 EXPECT_EQ(ui::EF_ALT_DOWN, event.GetFlags());
87 EXPECT_EQ(static_cast<size_t>(pointer_count), event.GetPointerCount());
88 EXPECT_EQ(static_cast<size_t>(history_size), event.GetHistorySize());
91 TEST(MotionEventAndroidTest, Clone) {
92 const int pointer_count = 1;
93 MotionEventAndroid::Pointer p0(
94 1, 13.7f, -7.13f, 5.3f, 1.2f, 0.1f, kAndroidToolTypeFinger);
95 MotionEventAndroid::Pointer p1(0, 0, 0, 0, 0, 0, 0);
96 MotionEventAndroid event(kPixToDip,
97 base::android::AttachCurrentThread(),
98 nullptr,
100 kAndroidActionDown,
101 pointer_count,
109 p1);
111 scoped_ptr<MotionEvent> clone = event.Clone();
112 EXPECT_EQ(ui::test::ToString(event), ui::test::ToString(*clone));
115 TEST(MotionEventAndroidTest, Cancel) {
116 const int event_time_ms = 5;
117 const int pointer_count = 1;
118 MotionEventAndroid::Pointer p0(
119 1, 13.7f, -7.13f, 5.3f, 1.2f, 0.1f, kAndroidToolTypeFinger);
120 MotionEventAndroid::Pointer p1(0, 0, 0, 0, 0, 0, 0);
121 MotionEventAndroid event(kPixToDip,
122 base::android::AttachCurrentThread(),
123 nullptr,
124 event_time_ms,
125 kAndroidActionDown,
126 pointer_count,
134 p1);
136 scoped_ptr<MotionEvent> cancel_event = event.Cancel();
137 EXPECT_EQ(MotionEvent::ACTION_CANCEL, cancel_event->GetAction());
138 EXPECT_EQ(
139 base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms),
140 cancel_event->GetEventTime());
141 EXPECT_EQ(p0.pos_x_pixels * kPixToDip, cancel_event->GetX(0));
142 EXPECT_EQ(p0.pos_y_pixels * kPixToDip, cancel_event->GetY(0));
143 EXPECT_EQ(static_cast<size_t>(pointer_count),
144 cancel_event->GetPointerCount());
145 EXPECT_EQ(0U, cancel_event->GetHistorySize());
148 TEST(MotionEventAndroidTest, InvalidOrientationsSanitized) {
149 int pointer_count = 2;
150 float orientation0 = 1e10f;
151 float orientation1 = std::numeric_limits<float>::quiet_NaN();
152 MotionEventAndroid::Pointer p0(0, 0, 0, 0, 0, orientation0, 0);
153 MotionEventAndroid::Pointer p1(1, 0, 0, 0, 0, orientation1, 0);
154 MotionEventAndroid event(kPixToDip,
155 base::android::AttachCurrentThread(),
156 nullptr,
158 kAndroidActionDown,
159 pointer_count,
167 p1);
169 EXPECT_EQ(0.f, event.GetOrientation(0));
170 EXPECT_EQ(0.f, event.GetOrientation(1));
173 TEST(MotionEventAndroidTest, NonEmptyHistoryForNonMoveEventsSanitized) {
174 int pointer_count = 1;
175 size_t history_size = 5;
176 MotionEventAndroid::Pointer p0(0, 0, 0, 0, 0, 0, 0);
177 MotionEventAndroid::Pointer p1(0, 0, 0, 0, 0, 0, 0);
178 MotionEventAndroid event(kPixToDip,
179 base::android::AttachCurrentThread(),
180 nullptr,
182 kAndroidActionDown,
183 pointer_count,
184 history_size,
191 p1);
193 EXPECT_EQ(0U, event.GetHistorySize());
196 TEST(MotionEventAndroidTest, ActionIndexForPointerDown) {
197 MotionEventAndroid::Pointer p0(
198 1, 13.7f, -7.13f, 5.3f, 1.2f, 0.1f, kAndroidToolTypeFinger);
199 MotionEventAndroid::Pointer p1(
200 2, -13.7f, 7.13f, 3.5f, 12.1f, -0.1f, kAndroidToolTypeFinger);
201 int pointer_count = 2;
202 int history_size = 0;
203 int action_index = 1;
204 MotionEventAndroid event(kPixToDip,
205 base::android::AttachCurrentThread(),
206 nullptr,
208 kAndroidActionPointerDown,
209 pointer_count,
210 history_size,
211 action_index,
217 p1);
219 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, event.GetAction());
220 EXPECT_EQ(action_index, event.GetActionIndex());
223 } // namespace content