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 "base/android/jni_android.h"
6 #include "content/browser/renderer_host/input/motion_event_android.h"
7 #include "testing/gtest/include/gtest/gtest.h"
13 const float kPixToDip
= 0.5f
;
15 // Corresponds to ACTION_DOWN, see
16 // developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN.
17 int kAndroidActionDown
= 0;
19 // Corresponds to TOOL_TYPE_FINGER, see
20 // developer.android.com/reference/android/view/MotionEvent.html
22 int kAndroidToolTypeFinger
= 1;
24 // Corresponds to BUTTON_PRIMARY, see
25 // developer.android.com/reference/android/view/MotionEvent.html#BUTTON_PRIMARY.
26 int kAndroidButtonPrimary
= 1;
30 TEST(MotionEventAndroidTest
, Constructor
) {
31 int event_time_ms
= 5;
32 base::TimeTicks event_time
=
33 base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms
);
38 float raw_offset
= 10.1f
;
39 float touch_major0
= 5.3f
;
40 float touch_major1
= 3.5f
;
43 int pointer_count
= 2;
45 int action_index
= -1;
46 base::android::ScopedJavaLocalRef
<jobject
> base_event_obj
=
47 MotionEventAndroid::Obtain(
48 event_time
, event_time
, MotionEvent::ACTION_DOWN
, x0
, y0
);
49 ASSERT_TRUE(base_event_obj
.obj());
51 MotionEventAndroid
event(kPixToDip
,
52 base::android::AttachCurrentThread(),
69 kAndroidToolTypeFinger
,
70 kAndroidToolTypeFinger
,
71 kAndroidButtonPrimary
);
73 EXPECT_EQ(MotionEvent::ACTION_DOWN
, event
.GetAction());
74 EXPECT_EQ(event_time
, event
.GetEventTime());
75 EXPECT_EQ(x0
* kPixToDip
, event
.GetX(0));
76 EXPECT_EQ(y0
* kPixToDip
, event
.GetY(0));
77 EXPECT_EQ(x1
* kPixToDip
, event
.GetX(1));
78 EXPECT_EQ(y1
* kPixToDip
, event
.GetY(1));
79 EXPECT_FLOAT_EQ((x0
+ raw_offset
) * kPixToDip
, event
.GetRawX(0));
80 EXPECT_FLOAT_EQ((y0
- raw_offset
) * kPixToDip
, event
.GetRawY(0));
81 EXPECT_FLOAT_EQ((x1
+ raw_offset
) * kPixToDip
, event
.GetRawX(1));
82 EXPECT_FLOAT_EQ((y1
- raw_offset
) * kPixToDip
, event
.GetRawY(1));
83 EXPECT_EQ(touch_major0
* kPixToDip
, event
.GetTouchMajor(0));
84 EXPECT_EQ(touch_major1
* kPixToDip
, event
.GetTouchMajor(1));
85 EXPECT_EQ(p0
, event
.GetPointerId(0));
86 EXPECT_EQ(p1
, event
.GetPointerId(1));
87 EXPECT_EQ(MotionEvent::TOOL_TYPE_FINGER
, event
.GetToolType(0));
88 EXPECT_EQ(MotionEvent::TOOL_TYPE_FINGER
, event
.GetToolType(1));
89 EXPECT_EQ(MotionEvent::BUTTON_PRIMARY
, event
.GetButtonState());
90 EXPECT_EQ(static_cast<size_t>(pointer_count
), event
.GetPointerCount());
91 EXPECT_EQ(static_cast<size_t>(history_size
), event
.GetHistorySize());
92 EXPECT_EQ(action_index
, event
.GetActionIndex());
95 TEST(MotionEventAndroidTest
, Clone
) {
96 int event_time_ms
= 5;
97 base::TimeTicks event_time
=
98 base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms
);
101 float touch_major
= 5.3f
;
102 int pointer_count
= 1;
104 base::android::ScopedJavaLocalRef
<jobject
> event_obj
=
105 MotionEventAndroid::Obtain(
106 event_time
, event_time
, MotionEvent::ACTION_DOWN
, x
, y
);
107 ASSERT_TRUE(event_obj
.obj());
109 MotionEventAndroid
event(kPixToDip
,
110 base::android::AttachCurrentThread(),
131 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
132 EXPECT_EQ(event
, *clone
);
135 TEST(MotionEventAndroidTest
, Cancel
) {
136 int event_time_ms
= 5;
137 base::TimeTicks event_time
=
138 base::TimeTicks() + base::TimeDelta::FromMilliseconds(event_time_ms
);
139 int pointer_count
= 1;
142 base::android::ScopedJavaLocalRef
<jobject
> event_obj
=
143 MotionEventAndroid::Obtain(
144 event_time
, event_time
, MotionEvent::ACTION_DOWN
, x
, y
);
145 ASSERT_TRUE(event_obj
.obj());
147 MotionEventAndroid
event(kPixToDip
,
148 base::android::AttachCurrentThread(),
169 scoped_ptr
<MotionEvent
> cancel_event
= event
.Cancel();
170 EXPECT_EQ(MotionEvent::ACTION_CANCEL
, cancel_event
->GetAction());
171 EXPECT_EQ(event_time
, cancel_event
->GetEventTime());
172 EXPECT_EQ(x
* kPixToDip
, cancel_event
->GetX(0));
173 EXPECT_EQ(y
* kPixToDip
, cancel_event
->GetY(0));
174 EXPECT_EQ(static_cast<size_t>(pointer_count
),
175 cancel_event
->GetPointerCount());
176 EXPECT_EQ(0U, cancel_event
->GetHistorySize());
179 } // namespace content