• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cts.util.EndToEndImeTestBase
21 import android.view.inputmethod.cts.util.InputMethodVisibilityVerifier.expectImeVisible
22 import android.view.inputmethod.cts.util.MockTestActivityUtil
23 import android.view.inputmethod.cts.util.MockTestActivityUtil.EXTRA_KEY_PRIVATE_IME_OPTIONS
24 import android.view.inputmethod.InputMethodManager
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.runner.RunWith
36 import org.junit.Test
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 = createUniqueMarker()
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(), TIMEOUT,
53                     mapOf(EXTRA_KEY_PRIVATE_IME_OPTIONS to marker)
54             )
55                 .use {
56                     expectEvent(stream, editorMatcher("onStartInput", marker), TIMEOUT)
57                     session.callRequestShowSelf(0)
58                     expectImeVisible(TIMEOUT)
59                     assertEquals(
60                             "Only IME target UID may observe the visible height of the IME",
61                             0,
62                             imm.reflectivelyGetInputMethodWindowVisibleHeight()
63                     )
64                 }
65         }
66     }
67 
68     fun InputMethodManager.reflectivelyGetInputMethodWindowVisibleHeight() =
69         try {
70             InputMethodManager::class.java
71                     .getMethod("getInputMethodWindowVisibleHeight")
72                     .invoke(this) as Int
73         } catch (_: NoSuchMethodError) {
74             throw AssumptionViolatedException("getInputMethodWindowVisibleHeight doesn't exist")
75         }
76 
77     companion object {
78         val TIMEOUT = TimeUnit.SECONDS.toMillis(5)
79     }
80 }
81