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 
obtainNativeMotionEventCopyFromJava(JNIEnv * env,jclass,jobject obj)104 void obtainNativeMotionEventCopyFromJava(JNIEnv *env, jclass /* clazz */, jobject obj) {
105     const AInputEvent *event = AMotionEvent_fromJava(env, obj);
106     AInputEvent_release(event);
107 }
108 
obtainMotionEventCopyFromNative(JNIEnv * env,jclass,jobject obj)109 jobject obtainMotionEventCopyFromNative(JNIEnv *env, jclass /* clazz */, jobject obj) {
110     const AInputEvent *event = AMotionEvent_fromJava(env, obj);
111     jobject motionEvent = AInputEvent_toJava(env, event);
112     AInputEvent_release(event);
113     return motionEvent;
114 }
115 
obtainKeyEventCopyFromNative(JNIEnv * env,jclass,jobject obj)116 jobject obtainKeyEventCopyFromNative(JNIEnv *env, jclass /* clazz */, jobject obj) {
117     const AInputEvent *event = AKeyEvent_fromJava(env, obj);
118     jobject keyEvent = AInputEvent_toJava(env, event);
119     AInputEvent_release(event);
120     return keyEvent;
121 }
122 
nativeKeyEventTest(JNIEnv * env,jclass,jobject obj)123 void nativeKeyEventTest(JNIEnv *env, jclass /* clazz */, jobject obj) {
124     const AInputEvent *event = AKeyEvent_fromJava(env, obj);
125     jint action = env->CallIntMethod(obj, gKeyEventMethodIds.getAction);
126     jlong downTime = env->CallLongMethod(obj, gKeyEventMethodIds.getDownTime) * NS_PER_MS;
127     jlong eventTime = env->CallLongMethod(obj, gKeyEventMethodIds.getEventTime) * NS_PER_MS;
128     jint keyCode = env->CallIntMethod(obj, gKeyEventMethodIds.getKeyCode);
129 
130     ASSERT(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY, "Wrong event type %d.",
131            AInputEvent_getType(event));
132 
133     ASSERT(action == AKeyEvent_getAction(event), "Wrong action %d not equal to %d",
134            AKeyEvent_getAction(event), action);
135 
136     ASSERT(downTime == AKeyEvent_getDownTime(event),
137            "Wrong downTime %" PRId64 " not equal to %" PRId64, AKeyEvent_getDownTime(event),
138            downTime);
139 
140     ASSERT(eventTime == AKeyEvent_getEventTime(event),
141            "Wrong eventTime %" PRId64 " not equal to %" PRId64, AKeyEvent_getEventTime(event),
142            eventTime);
143 
144     ASSERT(keyCode == AKeyEvent_getKeyCode(event), "Wrong keyCode %d not equal to %d",
145            AKeyEvent_getAction(event), action);
146 
147     AInputEvent_release(event);
148 }
149 
obtainNativeKeyEventCopyFromJava(JNIEnv * env,jclass,jobject obj)150 void obtainNativeKeyEventCopyFromJava(JNIEnv *env, jclass /* clazz */, jobject obj) {
151     const AInputEvent *event = AKeyEvent_fromJava(env, obj);
152     AInputEvent_release(event);
153 }
154 
155 const std::array<JNINativeMethod, 3> JNI_METHODS_MOTION = {{
156         {"nativeMotionEventTest", "(Landroid/view/MotionEvent;)V", (void *)nativeMotionEventTest},
157         {"obtainMotionEventCopyFromNative",
158          "(Landroid/view/MotionEvent;)Landroid/view/MotionEvent;",
159          (jobject)obtainMotionEventCopyFromNative},
160         {"obtainNativeMotionEventCopyFromJava", "(Landroid/view/MotionEvent;)V",
161          (void *)obtainNativeMotionEventCopyFromJava},
162 }};
163 
164 const std::array<JNINativeMethod, 3> JNI_METHODS_KEY = {{
165         {"nativeKeyEventTest", "(Landroid/view/KeyEvent;)V", (void *)nativeKeyEventTest},
166         {"obtainKeyEventCopyFromNative", "(Landroid/view/KeyEvent;)Landroid/view/KeyEvent;",
167          (jobject)obtainKeyEventCopyFromNative},
168         {"obtainNativeKeyEventCopyFromJava", "(Landroid/view/KeyEvent;)V",
169          (void *)obtainNativeKeyEventCopyFromJava},
170 }};
171 
172 } // anonymous namespace
173 
register_android_view_cts_AMotionEventNativeTest(JNIEnv * env)174 jint register_android_view_cts_AMotionEventNativeTest(JNIEnv *env) {
175     jclass clazz = env->FindClass("android/view/MotionEvent");
176     gMotionEventMethodIds.getAction = env->GetMethodID(clazz, "getAction", "()I");
177     gMotionEventMethodIds.getMetaState = env->GetMethodID(clazz, "getMetaState", "()I");
178     gMotionEventMethodIds.getDownTime = env->GetMethodID(clazz, "getDownTime", "()J");
179     gMotionEventMethodIds.getEventTime = env->GetMethodID(clazz, "getEventTime", "()J");
180     gMotionEventMethodIds.getPointerCount = env->GetMethodID(clazz, "getPointerCount", "()I");
181     gMotionEventMethodIds.getRawX = env->GetMethodID(clazz, "getRawX", "(I)F");
182     gMotionEventMethodIds.getRawY = env->GetMethodID(clazz, "getRawY", "(I)F");
183     gMotionEventMethodIds.getActionButton = env->GetMethodID(clazz, "getActionButton", "()I");
184     gMotionEventMethodIds.getClassification = env->GetMethodID(clazz, "getClassification", "()I");
185     jclass clazzTest = env->FindClass("android/view/cts/MotionEventTest");
186     return env->RegisterNatives(clazzTest, JNI_METHODS_MOTION.data(), JNI_METHODS_MOTION.size());
187 }
188 
register_android_view_cts_AKeyEventNativeTest(JNIEnv * env)189 jint register_android_view_cts_AKeyEventNativeTest(JNIEnv *env) {
190     jclass clazz = env->FindClass("android/view/KeyEvent");
191     gKeyEventMethodIds.getAction = env->GetMethodID(clazz, "getAction", "()I");
192     gKeyEventMethodIds.getKeyCode = env->GetMethodID(clazz, "getKeyCode", "()I");
193     gKeyEventMethodIds.getDownTime = env->GetMethodID(clazz, "getDownTime", "()J");
194     gKeyEventMethodIds.getEventTime = env->GetMethodID(clazz, "getEventTime", "()J");
195     jclass clazzTest = env->FindClass("android/view/cts/KeyEventTest");
196     return env->RegisterNatives(clazzTest, JNI_METHODS_KEY.data(), JNI_METHODS_KEY.size());
197 }
198