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 "content/browser/renderer_host/input/motion_event_android.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/events/event_constants.h"
11 #include "ui/events/test/motion_event_test_utils.h"
13 using ui::MotionEvent
;
17 const float kPixToDip
= 0.5f
;
19 int kAndroidActionDown
= AMOTION_EVENT_ACTION_DOWN
;
20 int kAndroidActionPointerDown
= AMOTION_EVENT_ACTION_POINTER_DOWN
;
21 int kAndroidAltKeyDown
= AMETA_ALT_ON
;
23 // Corresponds to TOOL_TYPE_FINGER, see
24 // developer.android.com/reference/android/view/MotionEvent.html
26 int kAndroidToolTypeFinger
= 1;
28 // Corresponds to BUTTON_PRIMARY, see
29 // developer.android.com/reference/android/view/MotionEvent.html#BUTTON_PRIMARY.
30 int kAndroidButtonPrimary
= 1;
34 // Note that these tests avoid creating a Java instance of the MotionEvent, as
35 // we're primarily testing caching behavior, and the code necessary to
36 // construct a Java-backed MotionEvent itself adds unnecessary complexity.
37 TEST(MotionEventAndroidTest
, Constructor
) {
38 int event_time_ms
= 5;
39 base::TimeTicks event_time
=
40 base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms
);
41 MotionEventAndroid::Pointer
p0(
42 1, 13.7f
, -7.13f
, 5.3f
, 1.2f
, 0.1f
, kAndroidToolTypeFinger
);
43 MotionEventAndroid::Pointer
p1(
44 2, -13.7f
, 7.13f
, 3.5f
, 12.1f
, -0.1f
, kAndroidToolTypeFinger
);
45 float raw_offset
= -3.f
;
46 int pointer_count
= 2;
48 int action_index
= -1;
49 MotionEventAndroid
event(kPixToDip
,
50 base::android::AttachCurrentThread(),
57 kAndroidButtonPrimary
,
64 EXPECT_EQ(MotionEvent::ACTION_DOWN
, event
.GetAction());
65 EXPECT_EQ(event_time
, event
.GetEventTime());
66 EXPECT_EQ(p0
.pos_x_pixels
* kPixToDip
, event
.GetX(0));
67 EXPECT_EQ(p0
.pos_y_pixels
* kPixToDip
, event
.GetY(0));
68 EXPECT_EQ(p1
.pos_x_pixels
* kPixToDip
, event
.GetX(1));
69 EXPECT_EQ(p1
.pos_y_pixels
* kPixToDip
, event
.GetY(1));
70 EXPECT_FLOAT_EQ((p0
.pos_x_pixels
+ raw_offset
) * kPixToDip
, event
.GetRawX(0));
71 EXPECT_FLOAT_EQ((p0
.pos_y_pixels
- raw_offset
) * kPixToDip
, event
.GetRawY(0));
72 EXPECT_FLOAT_EQ((p1
.pos_x_pixels
+ raw_offset
) * kPixToDip
, event
.GetRawX(1));
73 EXPECT_FLOAT_EQ((p1
.pos_y_pixels
- raw_offset
) * kPixToDip
, event
.GetRawY(1));
74 EXPECT_EQ(p0
.touch_major_pixels
* kPixToDip
, event
.GetTouchMajor(0));
75 EXPECT_EQ(p1
.touch_major_pixels
* kPixToDip
, event
.GetTouchMajor(1));
76 EXPECT_EQ(p0
.touch_minor_pixels
* kPixToDip
, event
.GetTouchMinor(0));
77 EXPECT_EQ(p1
.touch_minor_pixels
* kPixToDip
, event
.GetTouchMinor(1));
78 EXPECT_EQ(p0
.orientation_rad
, event
.GetOrientation(0));
79 EXPECT_EQ(p1
.orientation_rad
, event
.GetOrientation(1));
80 EXPECT_EQ(p0
.id
, event
.GetPointerId(0));
81 EXPECT_EQ(p1
.id
, event
.GetPointerId(1));
82 EXPECT_EQ(MotionEvent::TOOL_TYPE_FINGER
, event
.GetToolType(0));
83 EXPECT_EQ(MotionEvent::TOOL_TYPE_FINGER
, event
.GetToolType(1));
84 EXPECT_EQ(MotionEvent::BUTTON_PRIMARY
, event
.GetButtonState());
85 EXPECT_EQ(ui::EF_ALT_DOWN
, event
.GetFlags());
86 EXPECT_EQ(static_cast<size_t>(pointer_count
), event
.GetPointerCount());
87 EXPECT_EQ(static_cast<size_t>(history_size
), event
.GetHistorySize());
90 TEST(MotionEventAndroidTest
, Clone
) {
91 const int pointer_count
= 1;
92 MotionEventAndroid::Pointer
p0(
93 1, 13.7f
, -7.13f
, 5.3f
, 1.2f
, 0.1f
, kAndroidToolTypeFinger
);
94 MotionEventAndroid::Pointer
p1(0, 0, 0, 0, 0, 0, 0);
95 MotionEventAndroid
event(kPixToDip
,
96 base::android::AttachCurrentThread(),
110 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
111 EXPECT_EQ(ui::test::ToString(event
), ui::test::ToString(*clone
));
114 TEST(MotionEventAndroidTest
, Cancel
) {
115 const int event_time_ms
= 5;
116 const int pointer_count
= 1;
117 MotionEventAndroid::Pointer
p0(
118 1, 13.7f
, -7.13f
, 5.3f
, 1.2f
, 0.1f
, kAndroidToolTypeFinger
);
119 MotionEventAndroid::Pointer
p1(0, 0, 0, 0, 0, 0, 0);
120 MotionEventAndroid
event(kPixToDip
,
121 base::android::AttachCurrentThread(),
135 scoped_ptr
<MotionEvent
> cancel_event
= event
.Cancel();
136 EXPECT_EQ(MotionEvent::ACTION_CANCEL
, cancel_event
->GetAction());
138 base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms
),
139 cancel_event
->GetEventTime());
140 EXPECT_EQ(p0
.pos_x_pixels
* kPixToDip
, cancel_event
->GetX(0));
141 EXPECT_EQ(p0
.pos_y_pixels
* kPixToDip
, cancel_event
->GetY(0));
142 EXPECT_EQ(static_cast<size_t>(pointer_count
),
143 cancel_event
->GetPointerCount());
144 EXPECT_EQ(0U, cancel_event
->GetHistorySize());
147 TEST(MotionEventAndroidTest
, InvalidOrientationsSanitized
) {
148 int pointer_count
= 2;
149 float orientation0
= 1e10f
;
150 float orientation1
= std::numeric_limits
<float>::quiet_NaN();
151 MotionEventAndroid::Pointer
p0(0, 0, 0, 0, 0, orientation0
, 0);
152 MotionEventAndroid::Pointer
p1(1, 0, 0, 0, 0, orientation1
, 0);
153 MotionEventAndroid
event(kPixToDip
,
154 base::android::AttachCurrentThread(),
168 EXPECT_EQ(0.f
, event
.GetOrientation(0));
169 EXPECT_EQ(0.f
, event
.GetOrientation(1));
172 TEST(MotionEventAndroidTest
, NonEmptyHistoryForNonMoveEventsSanitized
) {
173 int pointer_count
= 1;
174 size_t history_size
= 5;
175 MotionEventAndroid::Pointer
p0(0, 0, 0, 0, 0, 0, 0);
176 MotionEventAndroid::Pointer
p1(0, 0, 0, 0, 0, 0, 0);
177 MotionEventAndroid
event(kPixToDip
,
178 base::android::AttachCurrentThread(),
192 EXPECT_EQ(0U, event
.GetHistorySize());
195 TEST(MotionEventAndroidTest
, ActionIndexForPointerDown
) {
196 MotionEventAndroid::Pointer
p0(
197 1, 13.7f
, -7.13f
, 5.3f
, 1.2f
, 0.1f
, kAndroidToolTypeFinger
);
198 MotionEventAndroid::Pointer
p1(
199 2, -13.7f
, 7.13f
, 3.5f
, 12.1f
, -0.1f
, kAndroidToolTypeFinger
);
200 int pointer_count
= 2;
201 int history_size
= 0;
202 int action_index
= 1;
203 MotionEventAndroid
event(kPixToDip
,
204 base::android::AttachCurrentThread(),
207 kAndroidActionPointerDown
,
218 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, event
.GetAction());
219 EXPECT_EQ(action_index
, event
.GetActionIndex());
222 } // namespace content