1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "AInputNativeTest"
18
19 #include <jni.h>
20 #include <jniAssert.h>
21 #include <math.h>
22 #include <array>
23 #include <cinttypes>
24 #include <string>
25
26 #include <android/input.h>
27 #include <android/log.h>
28 #include <nativehelper/JNIHelp.h>
29
30 namespace {
31
32 static struct MotionEventMethodId {
33 jmethodID getDownTime;
34 jmethodID getEventTime;
35 jmethodID getMetaState;
36 jmethodID getAction;
37 jmethodID getPointerCount;
38 jmethodID getRawX;
39 jmethodID getRawY;
40 jmethodID getActionButton;
41 jmethodID getClassification;
42 } gMotionEventMethodIds;
43
44 static struct KeyEventMethodId {
45 jmethodID getDownTime;
46 jmethodID getEventTime;
47 jmethodID getAction;
48 jmethodID getKeyCode;
49 } gKeyEventMethodIds;
50
51 static constexpr int64_t NS_PER_MS = 1000000LL;
52
nativeMotionEventTest(JNIEnv * env,jclass,jobject obj)53 void nativeMotionEventTest(JNIEnv *env, jclass /* clazz */, jobject obj) {
54 const AInputEvent *event = AMotionEvent_fromJava(env, obj);
55 jint action = env->CallIntMethod(obj, gMotionEventMethodIds.getAction);
56 jlong downTime = env->CallLongMethod(obj, gMotionEventMethodIds.getDownTime) * NS_PER_MS;
57 jlong eventTime = env->CallLongMethod(obj, gMotionEventMethodIds.getEventTime) * NS_PER_MS;
58 jint metaState = env->CallIntMethod(obj, gMotionEventMethodIds.getMetaState);
59 jint pointerCount = env->CallIntMethod(obj, gMotionEventMethodIds.getPointerCount);
60 jint actionButton = env->CallIntMethod(obj, gMotionEventMethodIds.getActionButton);
61 jint classification = env->CallIntMethod(obj, gMotionEventMethodIds.getClassification);
62
63 ASSERT(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION, "Wrong event type %d.",
64 AInputEvent_getType(event));
65
66 ASSERT(action == AMotionEvent_getAction(event), "Wrong action %d not equal to %d",
67 AMotionEvent_getAction(event), action);
68
69 ASSERT(downTime == AMotionEvent_getDownTime(event),
70 "Wrong downTime %" PRId64 " not equal to %" PRId64, AMotionEvent_getDownTime(event),
71 downTime);
72
73 ASSERT(eventTime == AMotionEvent_getEventTime(event),
74 "Wrong eventTime %" PRId64 " not equal to %" PRId64, AMotionEvent_getEventTime(event),
75 eventTime);
76
77 ASSERT(metaState == AMotionEvent_getMetaState(event), "Wrong metaState %d not equal to %d",
78 AMotionEvent_getMetaState(event), metaState);
79
80 ASSERT(AMotionEvent_getPointerCount(event) == pointerCount,
81 "Wrong pointer count %zu not equal to %d", AMotionEvent_getPointerCount(event),
82 pointerCount);
83
84 ASSERT(AMotionEvent_getActionButton(event) == actionButton,
85 "Wrong action button %d not equal to %d", AMotionEvent_getActionButton(event),
86 actionButton);
87
88 ASSERT(AMotionEvent_getClassification(event) == classification,
89 "Wrong classification %hhd not equal to %hhd", AMotionEvent_getClassification(event),
90 classification);
91
92 for (int i = 0; i < pointerCount; i++) {
93 jfloat rawX = env->CallFloatMethod(obj, gMotionEventMethodIds.getRawX, i);
94 jfloat rawY = env->CallFloatMethod(obj, gMotionEventMethodIds.getRawY, i);
95 ASSERT(fabs(rawX - AMotionEvent_getRawX(event, i)) == 0.0f, "Point X:%f not same as %f",
96 AMotionEvent_getRawX(event, i), rawX);
97
98 ASSERT(fabs(rawY - AMotionEvent_getRawY(event, i)) == 0.0f, "Point Y:%f not same as %f",
99 AMotionEvent_getRawY(event, i), rawY);
100 }
101 AInputEvent_release(event);
102 }
103
nativeKeyEventTest(JNIEnv * env,jclass,jobject obj)104 void nativeKeyEventTest(JNIEnv *env, jclass /* clazz */, jobject obj) {
105 const AInputEvent *event = AKeyEvent_fromJava(env, obj);
106 jint action = env->CallIntMethod(obj, gKeyEventMethodIds.getAction);
107 jlong downTime = env->CallLongMethod(obj, gKeyEventMethodIds.getDownTime) * NS_PER_MS;
108 jlong eventTime = env->CallLongMethod(obj, gKeyEventMethodIds.getEventTime) * NS_PER_MS;
109 jint keyCode = env->CallIntMethod(obj, gKeyEventMethodIds.getKeyCode);
110
111 ASSERT(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY, "Wrong event type %d.",
112 AInputEvent_getType(event));
113
114 ASSERT(action == AKeyEvent_getAction(event), "Wrong action %d not equal to %d",
115 AKeyEvent_getAction(event), action);
116
117 ASSERT(downTime == AKeyEvent_getDownTime(event),
118 "Wrong downTime %" PRId64 " not equal to %" PRId64, AKeyEvent_getDownTime(event),
119 downTime);
120
121 ASSERT(eventTime == AKeyEvent_getEventTime(event),
122 "Wrong eventTime %" PRId64 " not equal to %" PRId64, AKeyEvent_getEventTime(event),
123 eventTime);
124
125 ASSERT(keyCode == AKeyEvent_getKeyCode(event), "Wrong keyCode %d not equal to %d",
126 AKeyEvent_getAction(event), action);
127
128 AInputEvent_release(event);
129 }
130
131 const std::array<JNINativeMethod, 1> JNI_METHODS_MOTION = {{
132 {"nativeMotionEventTest", "(Landroid/view/MotionEvent;)V", (void *)nativeMotionEventTest},
133 }};
134
135 const std::array<JNINativeMethod, 1> JNI_METHODS_KEY = {{
136 {"nativeKeyEventTest", "(Landroid/view/KeyEvent;)V", (void *)nativeKeyEventTest},
137 }};
138
139 } // anonymous namespace
140
register_android_view_cts_AMotionEventNativeTest(JNIEnv * env)141 jint register_android_view_cts_AMotionEventNativeTest(JNIEnv *env) {
142 jclass clazz = env->FindClass("android/view/MotionEvent");
143 gMotionEventMethodIds.getAction = env->GetMethodID(clazz, "getAction", "()I");
144 gMotionEventMethodIds.getMetaState = env->GetMethodID(clazz, "getMetaState", "()I");
145 gMotionEventMethodIds.getDownTime = env->GetMethodID(clazz, "getDownTime", "()J");
146 gMotionEventMethodIds.getEventTime = env->GetMethodID(clazz, "getEventTime", "()J");
147 gMotionEventMethodIds.getPointerCount = env->GetMethodID(clazz, "getPointerCount", "()I");
148 gMotionEventMethodIds.getRawX = env->GetMethodID(clazz, "getRawX", "(I)F");
149 gMotionEventMethodIds.getRawY = env->GetMethodID(clazz, "getRawY", "(I)F");
150 gMotionEventMethodIds.getActionButton = env->GetMethodID(clazz, "getActionButton", "()I");
151 gMotionEventMethodIds.getClassification = env->GetMethodID(clazz, "getClassification", "()I");
152 jclass clazzTest = env->FindClass("android/view/cts/MotionEventTest");
153 return env->RegisterNatives(clazzTest, JNI_METHODS_MOTION.data(), JNI_METHODS_MOTION.size());
154 }
155
register_android_view_cts_AKeyEventNativeTest(JNIEnv * env)156 jint register_android_view_cts_AKeyEventNativeTest(JNIEnv *env) {
157 jclass clazz = env->FindClass("android/view/KeyEvent");
158 gKeyEventMethodIds.getAction = env->GetMethodID(clazz, "getAction", "()I");
159 gKeyEventMethodIds.getKeyCode = env->GetMethodID(clazz, "getKeyCode", "()I");
160 gKeyEventMethodIds.getDownTime = env->GetMethodID(clazz, "getDownTime", "()J");
161 gKeyEventMethodIds.getEventTime = env->GetMethodID(clazz, "getEventTime", "()J");
162 jclass clazzTest = env->FindClass("android/view/cts/KeyEventTest");
163 return env->RegisterNatives(clazzTest, JNI_METHODS_KEY.data(), JNI_METHODS_KEY.size());
164 }
165