• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 
18 #include <android/input.h>
19 #include <jni.h>
20 #include <jniAssert.h>
21 
22 #include <array>
23 #include <thread>
24 
25 #define LOG_TAG "InputQueueTest"
26 
waitForEvent(JNIEnv * env,jclass,jobject inputQueue)27 bool waitForEvent(JNIEnv *env, jclass /* clazz */, jobject inputQueue) {
28     constexpr size_t NUM_TRIES = 5;
29     for (size_t i = 0; i < NUM_TRIES; i++) {
30         AInputQueue *nativeQueue = AInputQueue_fromJava(env, inputQueue);
31         if (nativeQueue != nullptr) {
32             int32_t numEvents = AInputQueue_hasEvents(nativeQueue);
33             if (numEvents > 0) {
34                 return true;
35             }
36         }
37         std::this_thread::sleep_for(std::chrono::milliseconds(100));
38     }
39     return false;
40 }
41 
inputQueueTest(JNIEnv * env,jclass,jobject inputQueue)42 void inputQueueTest(JNIEnv *env, jclass /* clazz */, jobject inputQueue) {
43     AInputQueue *nativeQueue = AInputQueue_fromJava(env, inputQueue);
44     ASSERT(nativeQueue != nullptr, "Native input queue not returned");
45     AInputEvent *event = nullptr;
46     ASSERT(AInputQueue_getEvent(nativeQueue, &event) >= 0, "getEvent did not succeed");
47     ASSERT(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION, "Wrong event type");
48     ASSERT(AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_DOWN, "Wrong action");
49     AInputQueue_finishEvent(nativeQueue, event, true);
50 }
51 
52 const std::array<JNINativeMethod, 2> JNI_METHODS = {{
53         {"waitForEvent", "(Landroid/view/InputQueue;)Z", (void *)waitForEvent},
54         {"inputQueueTest", "(Landroid/view/InputQueue;)V", (void *)inputQueueTest},
55 }};
56 
register_android_view_cts_InputQueueTest(JNIEnv * env)57 jint register_android_view_cts_InputQueueTest(JNIEnv *env) {
58     jclass clazzTest = env->FindClass("android/view/cts/InputQueueTest");
59     return env->RegisterNatives(clazzTest, JNI_METHODS.data(), JNI_METHODS.size());
60 }
61