1 /* <lambda>null2 * Copyright (C) 2022 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.inputmethod.cts.sdk32 18 19 import android.content.Context 20 import android.view.inputmethod.InputMethodManager 21 import android.view.inputmethod.cts.util.EndToEndImeTestBase 22 import android.view.inputmethod.cts.util.InputMethodVisibilityVerifier.expectImeVisible 23 import android.view.inputmethod.cts.util.MockTestActivityUtil 24 import androidx.test.filters.MediumTest 25 import androidx.test.platform.app.InstrumentationRegistry 26 import androidx.test.runner.AndroidJUnit4 27 import com.android.cts.mockime.ImeSettings 28 import com.android.cts.mockime.MockImeSession 29 import org.junit.Assert.assertEquals 30 import org.junit.AssumptionViolatedException 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 import java.util.concurrent.TimeUnit 34 35 @MediumTest 36 @RunWith(AndroidJUnit4::class) 37 class InputMethodManagerTest : EndToEndImeTestBase() { 38 39 val context: Context = InstrumentationRegistry.getInstrumentation().getContext() 40 val uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation() 41 42 @Test 43 fun getInputMethodWindowVisibleHeight_returnsZeroIfNotFocused() { 44 val imm = context.getSystemService(InputMethodManager::class.java)!! 45 MockImeSession.create(context, uiAutomation, ImeSettings.Builder()).use { session -> 46 MockTestActivityUtil.launchSync( 47 context.getPackageManager().isInstantApp(), TIMEOUT).use { 48 session.callRequestShowSelf(0) 49 expectImeVisible(TIMEOUT) 50 assertEquals( 51 "Only IME target UID may observe the visible height of the IME", 52 0, 53 imm.reflectivelyGetInputMethodWindowVisibleHeight() 54 ) 55 } 56 } 57 } 58 59 fun InputMethodManager.reflectivelyGetInputMethodWindowVisibleHeight() = 60 try { 61 InputMethodManager::class.java 62 .getMethod("getInputMethodWindowVisibleHeight") 63 .invoke(this) as Int 64 } catch (_: NoSuchMethodError) { 65 throw AssumptionViolatedException("getInputMethodWindowVisibleHeight doesn't exist") 66 } 67 68 companion object { 69 val TIMEOUT = TimeUnit.SECONDS.toMillis(5) 70 } 71 } 72