xref: /aosp_15_r20/cts/tests/input/src/android/input/cts/KeyboardLayoutTest.kt (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * 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.input.cts
18 
19 import android.Manifest
20 import android.content.BroadcastReceiver
21 import android.content.Context
22 import android.content.Intent
23 import android.hardware.input.InputManager
24 import androidx.test.ext.junit.rules.ActivityScenarioRule
25 import androidx.test.ext.junit.runners.AndroidJUnit4
26 import androidx.test.filters.MediumTest
27 import androidx.test.platform.app.InstrumentationRegistry
28 import com.android.compatibility.common.util.PollingCheck
29 import com.android.compatibility.common.util.SystemUtil
30 import org.junit.Assert.assertEquals
31 import org.junit.Assert.assertNotEquals
32 import org.junit.Assert.fail
33 import org.junit.Before
34 import org.junit.Rule
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 
38 /**
39  * Read and parse <keyboard-layout> attributes into KeyboardLayout
40  */
41 @MediumTest
42 @RunWith(AndroidJUnit4::class)
43 class KeyboardLayoutTest {
44 
45     @get:Rule
46     val rule = ActivityScenarioRule<CaptureEventActivity>(CaptureEventActivity::class.java)
47 
48     private lateinit var activity: CaptureEventActivity
49     private lateinit var inputManager: InputManager
50 
51     @Before
setUpnull52     fun setUp() {
53         rule.getScenario().onActivity {
54             inputManager = it.getSystemService(InputManager::class.java)
55             activity = it
56         }
57         PollingCheck.waitFor { activity.hasWindowFocus() }
58     }
59 
60     @Test
testKeyboardLayoutType_CorrectlyInitializednull61     fun testKeyboardLayoutType_CorrectlyInitialized() {
62         val englishQwertyLayoutDesc = getKeyboardLayoutDescriptor("english_us_qwerty")
63         val englishUndefinedLayoutDesc = getKeyboardLayoutDescriptor("english_us_undefined")
64 
65         assertNotEquals(
66             "English qwerty layout should not be empty",
67             "",
68             englishQwertyLayoutDesc!!
69         )
70         assertNotEquals(
71             "English undefined layout should not be empty",
72             "",
73             englishUndefinedLayoutDesc!!
74         )
75 
76         assertEquals(
77             "Layout type should be qwerty",
78             "qwerty",
79             getKeyboardLayoutTypeForLayoutDescriptor(englishQwertyLayoutDesc)
80         )
81         assertEquals(
82             "Layout type should be undefined",
83             "undefined",
84             getKeyboardLayoutTypeForLayoutDescriptor(englishUndefinedLayoutDesc)
85         )
86     }
87 
88     /**
89      * Returns the first matching keyboard layout id that matches the provided language.
90      *
91      * @param language The language to query for.
92      * @return The first matching keyboard layout or an empty string if none was found.
93      */
getKeyboardLayoutDescriptornull94     private fun getKeyboardLayoutDescriptor(language: String): String? {
95         return SystemUtil.runWithShellPermissionIdentity<String>({
96             for (kl in inputManager.keyboardLayoutDescriptors) {
97                 if (kl.endsWith(language)) {
98                     return@runWithShellPermissionIdentity kl
99                 }
100             }
101             fail("Failed to get keyboard layout for language $language")
102             ""
103         }, Manifest.permission.INTERACT_ACROSS_USERS)
104     }
105 
106     /**
107      * @return the layout type for layout with provided layout descriptor
108      */
getKeyboardLayoutTypeForLayoutDescriptornull109     private fun getKeyboardLayoutTypeForLayoutDescriptor(descriptor: String): String? {
110         return SystemUtil.runWithShellPermissionIdentity<String>({
111             inputManager.getKeyboardLayoutTypeForLayoutDescriptor(descriptor)
112         }, Manifest.permission.INTERACT_ACROSS_USERS)
113     }
114 
115     class CtsKeyboardLayoutProvider : BroadcastReceiver() {
onReceivenull116         override fun onReceive(context: Context, intent: Intent) {
117             // Nothing to do at this time.
118         }
119     }
120 }
121