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 } gMotionEventMethodIds;
41
42 static struct KeyEventMethodId {
43 jmethodID getDownTime;
44 jmethodID getEventTime;
45 jmethodID getAction;
46 jmethodID getKeyCode;
47 } gKeyEventMethodIds;
48
49 static constexpr int64_t NS_PER_MS = 1000000LL;
50
nativeMotionEventTest(JNIEnv * env,jclass,jobject obj)51 void nativeMotionEventTest(JNIEnv *env, jclass /* clazz */, jobject obj) {
52 const AInputEvent *event = AMotionEvent_fromJava(env, obj);
53 jint action = env->CallIntMethod(obj, gMotionEventMethodIds.getAction);
54 jlong downTime = env->CallLongMethod(obj, gMotionEventMethodIds.getDownTime) * NS_PER_MS;
55 jlong eventTime = env->CallLongMethod(obj, gMotionEventMethodIds.getEventTime) * NS_PER_MS;
56 jint metaState = env->CallIntMethod(obj, gMotionEventMethodIds.getMetaState);
57 jint pointerCount = env->CallIntMethod(obj, gMotionEventMethodIds.getPointerCount);
58
59 ASSERT(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION, "Wrong event type %d.",
60 AInputEvent_getType(event));
61
62 ASSERT(action == AMotionEvent_getAction(event), "Wrong action %d not equal to %d",
63 AMotionEvent_getAction(event), action);
64
65 ASSERT(downTime == AMotionEvent_getDownTime(event),
66 "Wrong downTime %" PRId64 " not equal to %" PRId64, AMotionEvent_getDownTime(event),
67 downTime);
68
69 ASSERT(eventTime == AMotionEvent_getEventTime(event),
70 "Wrong eventTime %" PRId64 " not equal to %" PRId64, AMotionEvent_getEventTime(event),
71 eventTime);
72
73 ASSERT(metaState == AMotionEvent_getMetaState(event), "Wrong metaState %d not equal to %d",
74 AMotionEvent_getMetaState(event), metaState);
75
76 ASSERT(AMotionEvent_getPointerCount(event) == pointerCount,
77 "Wrong pointer count %zu not equal to %d", AMotionEvent_getPointerCount(event),
78 pointerCount);
79
80 for (int i = 0; i < pointerCount; i++) {
81 jfloat rawX = env->CallFloatMethod(obj, gMotionEventMethodIds.getRawX, i);
82 jfloat rawY = env->CallFloatMethod(obj, gMotionEventMethodIds.getRawY, i);
83 ASSERT(fabs(rawX - AMotionEvent_getRawX(event, i)) == 0.0f, "Point X:%f not same as %f",
84 AMotionEvent_getRawX(event, i), rawX);
85
86 ASSERT(fabs(rawY - AMotionEvent_getRawY(event, i)) == 0.0f, "Point Y:%f not same as %f",
87 AMotionEvent_getRawY(event, i), rawY);
88 }
89 AInputEvent_release(event);
90 }
91
nativeKeyEventTest(JNIEnv * env,jclass,jobject obj)92 void nativeKeyEventTest(JNIEnv *env, jclass /* clazz */, jobject obj) {
93 const AInputEvent *event = AKeyEvent_fromJava(env, obj);
94 jint action = env->CallIntMethod(obj, gKeyEventMethodIds.getAction);
95 jlong downTime = env->CallLongMethod(obj, gKeyEventMethodIds.getDownTime) * NS_PER_MS;
96 jlong eventTime = env->CallLongMethod(obj, gKeyEventMethodIds.getEventTime) * NS_PER_MS;
97 jint keyCode = env->CallIntMethod(obj, gKeyEventMethodIds.getKeyCode);
98
99 ASSERT(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY, "Wrong event type %d.",
100 AInputEvent_getType(event));
101
102 ASSERT(action == AKeyEvent_getAction(event), "Wrong action %d not equal to %d",
103 AKeyEvent_getAction(event), action);
104
105 ASSERT(downTime == AKeyEvent_getDownTime(event),
106 "Wrong downTime %" PRId64 " not equal to %" PRId64, AKeyEvent_getDownTime(event),
107 downTime);
108
109 ASSERT(eventTime == AKeyEvent_getEventTime(event),
110 "Wrong eventTime %" PRId64 " not equal to %" PRId64, AKeyEvent_getEventTime(event),
111 eventTime);
112
113 ASSERT(keyCode == AKeyEvent_getKeyCode(event), "Wrong keyCode %d not equal to %d",
114 AKeyEvent_getAction(event), action);
115
116 AInputEvent_release(event);
117 }
118
119 const std::array<JNINativeMethod, 1> JNI_METHODS_MOTION = {{
120 {"nativeMotionEventTest", "(Landroid/view/MotionEvent;)V", (void *)nativeMotionEventTest},
121 }};
122
123 const std::array<JNINativeMethod, 1> JNI_METHODS_KEY = {{
124 {"nativeKeyEventTest", "(Landroid/view/KeyEvent;)V", (void *)nativeKeyEventTest},
125 }};
126
127 } // anonymous namespace
128
register_android_view_cts_AMotionEventNativeTest(JNIEnv * env)129 jint register_android_view_cts_AMotionEventNativeTest(JNIEnv *env) {
130 jclass clazz = env->FindClass("android/view/MotionEvent");
131 gMotionEventMethodIds.getAction = env->GetMethodID(clazz, "getAction", "()I");
132 gMotionEventMethodIds.getMetaState = env->GetMethodID(clazz, "getMetaState", "()I");
133 gMotionEventMethodIds.getDownTime = env->GetMethodID(clazz, "getDownTime", "()J");
134 gMotionEventMethodIds.getEventTime = env->GetMethodID(clazz, "getEventTime", "()J");
135 gMotionEventMethodIds.getPointerCount = env->GetMethodID(clazz, "getPointerCount", "()I");
136 gMotionEventMethodIds.getRawX = env->GetMethodID(clazz, "getRawX", "(I)F");
137 gMotionEventMethodIds.getRawY = env->GetMethodID(clazz, "getRawY", "(I)F");
138 jclass clazzTest = env->FindClass("android/view/cts/MotionEventTest");
139 return env->RegisterNatives(clazzTest, JNI_METHODS_MOTION.data(), JNI_METHODS_MOTION.size());
140 }
141
register_android_view_cts_AKeyEventNativeTest(JNIEnv * env)142 jint register_android_view_cts_AKeyEventNativeTest(JNIEnv *env) {
143 jclass clazz = env->FindClass("android/view/KeyEvent");
144 gKeyEventMethodIds.getAction = env->GetMethodID(clazz, "getAction", "()I");
145 gKeyEventMethodIds.getKeyCode = env->GetMethodID(clazz, "getKeyCode", "()I");
146 gKeyEventMethodIds.getDownTime = env->GetMethodID(clazz, "getDownTime", "()J");
147 gKeyEventMethodIds.getEventTime = env->GetMethodID(clazz, "getEventTime", "()J");
148 jclass clazzTest = env->FindClass("android/view/cts/KeyEventTest");
149 return env->RegisterNatives(clazzTest, JNI_METHODS_KEY.data(), JNI_METHODS_KEY.size());
150 }
151