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 package android.view.cts; 18 19 import static org.junit.Assert.assertTrue; 20 21 import android.app.Instrumentation; 22 import android.os.SystemClock; 23 import android.view.InputQueue; 24 import android.view.MotionEvent; 25 import android.view.Window; 26 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.filters.SmallTest; 29 import androidx.test.rule.ActivityTestRule; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 /** 37 * Test {@link AInputQueue}. 38 */ 39 @SmallTest 40 @RunWith(AndroidJUnit4.class) 41 public class InputQueueTest { 42 private static final String LOG_TAG = InputQueueTest.class.getSimpleName(); 43 static { 44 System.loadLibrary("ctsview_jni"); 45 } 46 47 private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation(); 48 waitForEvent(InputQueue inputQueue)49 private static native boolean waitForEvent(InputQueue inputQueue); inputQueueTest(InputQueue inputQueue)50 private static native void inputQueueTest(InputQueue inputQueue); 51 52 @Rule 53 public ActivityTestRule<InputQueueCtsActivity> mTestActivityRule = 54 new ActivityTestRule<>(InputQueueCtsActivity.class); 55 56 @Test testNativeInputQueue()57 public void testNativeInputQueue() throws Throwable { 58 InputQueueCtsActivity activity = mTestActivityRule.getActivity(); 59 Window window = activity.getWindow(); 60 InputQueue inputQueue = activity.getInputQueue(); 61 62 // An event is created Java-side. 63 long now = SystemClock.uptimeMillis(); 64 MotionEvent event = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, 0, 0, 0); 65 window.injectInputEvent(event); 66 67 assertTrue("Timed out waiting for event", waitForEvent(inputQueue)); 68 69 inputQueueTest(inputQueue); // Check the injected event is received on the native side. 70 } 71 } 72