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 android.view.inputmethod.cts.util.MockTestActivityUtil.EXTRA_KEY_PRIVATE_IME_OPTIONS
25 import androidx.test.filters.MediumTest
26 import androidx.test.platform.app.InstrumentationRegistry
27 import androidx.test.runner.AndroidJUnit4
28 import com.android.cts.mockime.ImeEventStreamTestUtils.editorMatcher
29 import com.android.cts.mockime.ImeEventStreamTestUtils.expectEvent
30 import com.android.cts.mockime.ImeSettings
31 import com.android.cts.mockime.MockImeSession
32 import java.util.concurrent.TimeUnit
33 import org.junit.Assert.assertEquals
34 import org.junit.AssumptionViolatedException
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 
38 @MediumTest
39 @RunWith(AndroidJUnit4::class)
40 class InputMethodManagerTest : EndToEndImeTestBase() {
41 
42     val context: Context = InstrumentationRegistry.getInstrumentation().getContext()
43     val uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation()
44 
45     @Test
46     fun getInputMethodWindowVisibleHeight_returnsZeroIfNotFocused() {
47         val marker = getTestMarker()
48         val imm = context.getSystemService(InputMethodManager::class.java)!!
49         MockImeSession.create(context, uiAutomation, ImeSettings.Builder()).use { session ->
50             val stream = session.openEventStream()
51             MockTestActivityUtil.launchSync(
52                 context.getPackageManager().isInstantApp(),
53                 TIMEOUT,
54                 mapOf(EXTRA_KEY_PRIVATE_IME_OPTIONS to marker)
55             )
56                 .use {
57                     expectEvent(stream, editorMatcher("onStartInput", marker), TIMEOUT)
58                     session.callRequestShowSelf(0)
59                     expectImeVisible(TIMEOUT)
60                     assertEquals(
61                             "Only IME target UID may observe the visible height of the IME",
62                             0,
63                             imm.reflectivelyGetInputMethodWindowVisibleHeight()
64                     )
65                 }
66         }
67     }
68 
69     fun InputMethodManager.reflectivelyGetInputMethodWindowVisibleHeight() =
70         try {
71             InputMethodManager::class.java
72                     .getMethod("getInputMethodWindowVisibleHeight")
73                     .invoke(this) as Int
74         } catch (_: NoSuchMethodError) {
75             throw AssumptionViolatedException("getInputMethodWindowVisibleHeight doesn't exist")
76         }
77 
78     companion object {
79         val TIMEOUT = TimeUnit.SECONDS.toMillis(5)
80     }
81 }
82