1 /*
2  * Copyright (C) 2024 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 com.android.systemui.inputmethod.domain.interactor
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import androidx.test.filters.SmallTest
21 import com.android.systemui.SysuiTestCase
22 import com.android.systemui.inputmethod.data.model.InputMethodModel
23 import com.android.systemui.inputmethod.data.repository.fakeInputMethodRepository
24 import com.android.systemui.inputmethod.data.repository.inputMethodRepository
25 import com.android.systemui.kosmos.Kosmos
26 import com.android.systemui.kosmos.testScope
27 import com.google.common.truth.Truth.assertThat
28 import java.util.UUID
29 import kotlinx.coroutines.test.runTest
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @SmallTest
34 @RunWith(AndroidJUnit4::class)
35 class InputMethodInteractorTest : SysuiTestCase() {
36 
37     private val kosmos = Kosmos()
38     private val testScope = kosmos.testScope
39     private val fakeInputMethodRepository = kosmos.fakeInputMethodRepository
40 
41     private val underTest = InputMethodInteractor(repository = kosmos.inputMethodRepository)
42 
43     @Test
hasMultipleEnabledImesOrSubtypes_noImes_returnsFalsenull44     fun hasMultipleEnabledImesOrSubtypes_noImes_returnsFalse() =
45         testScope.runTest {
46             fakeInputMethodRepository.setEnabledInputMethods(USER_ID)
47 
48             assertThat(underTest.hasMultipleEnabledImesOrSubtypes(USER_ID)).isFalse()
49         }
50 
51     @Test
hasMultipleEnabledImesOrSubtypes_noMatches_returnsFalsenull52     fun hasMultipleEnabledImesOrSubtypes_noMatches_returnsFalse() =
53         testScope.runTest {
54             fakeInputMethodRepository.setEnabledInputMethods(
55                 USER_ID,
56                 createInputMethodWithSubtypes(auxiliarySubtypes = 1, nonAuxiliarySubtypes = 0),
57                 createInputMethodWithSubtypes(auxiliarySubtypes = 3, nonAuxiliarySubtypes = 0),
58             )
59 
60             assertThat(underTest.hasMultipleEnabledImesOrSubtypes(USER_ID)).isFalse()
61         }
62 
63     @Test
hasMultipleEnabledImesOrSubtypes_oneMatch_returnsFalsenull64     fun hasMultipleEnabledImesOrSubtypes_oneMatch_returnsFalse() =
65         testScope.runTest {
66             fakeInputMethodRepository.setEnabledInputMethods(
67                 USER_ID,
68                 createInputMethodWithSubtypes(auxiliarySubtypes = 0, nonAuxiliarySubtypes = 0),
69             )
70 
71             assertThat(underTest.hasMultipleEnabledImesOrSubtypes(USER_ID)).isFalse()
72         }
73 
74     @Test
hasMultipleEnabledImesOrSubtypes_twoMatches_returnsTruenull75     fun hasMultipleEnabledImesOrSubtypes_twoMatches_returnsTrue() =
76         testScope.runTest {
77             fakeInputMethodRepository.setEnabledInputMethods(
78                 USER_ID,
79                 createInputMethodWithSubtypes(auxiliarySubtypes = 0, nonAuxiliarySubtypes = 1),
80                 createInputMethodWithSubtypes(auxiliarySubtypes = 0, nonAuxiliarySubtypes = 0),
81             )
82 
83             assertThat(underTest.hasMultipleEnabledImesOrSubtypes(USER_ID)).isTrue()
84         }
85 
86     @Test
hasMultipleEnabledImesOrSubtypes_oneWithNonAux_returnsFalsenull87     fun hasMultipleEnabledImesOrSubtypes_oneWithNonAux_returnsFalse() =
88         testScope.runTest {
89             fakeInputMethodRepository.setEnabledInputMethods(
90                 USER_ID,
91                 createInputMethodWithSubtypes(auxiliarySubtypes = 0, nonAuxiliarySubtypes = 2),
92             )
93 
94             assertThat(underTest.hasMultipleEnabledImesOrSubtypes(USER_ID)).isFalse()
95         }
96 
97     @Test
hasMultipleEnabledImesOrSubtypes_twoWithAux_returnsFalsenull98     fun hasMultipleEnabledImesOrSubtypes_twoWithAux_returnsFalse() =
99         testScope.runTest {
100             fakeInputMethodRepository.setEnabledInputMethods(
101                 USER_ID,
102                 createInputMethodWithSubtypes(auxiliarySubtypes = 3, nonAuxiliarySubtypes = 0),
103                 createInputMethodWithSubtypes(auxiliarySubtypes = 5, nonAuxiliarySubtypes = 0),
104             )
105 
106             assertThat(underTest.hasMultipleEnabledImesOrSubtypes(USER_ID)).isFalse()
107         }
108 
109     @Test
hasMultipleEnabledImesOrSubtypes_selectedHasOneSubtype_returnsFalsenull110     fun hasMultipleEnabledImesOrSubtypes_selectedHasOneSubtype_returnsFalse() =
111         testScope.runTest {
112             fakeInputMethodRepository.selectedInputMethodSubtypes =
113                 listOf(InputMethodModel.Subtype(1, isAuxiliary = false))
114 
115             assertThat(underTest.hasMultipleEnabledImesOrSubtypes(USER_ID)).isFalse()
116         }
117 
118     @Test
hasMultipleEnabledImesOrSubtypes_selectedHasTwoSubtypes_returnsTruenull119     fun hasMultipleEnabledImesOrSubtypes_selectedHasTwoSubtypes_returnsTrue() =
120         testScope.runTest {
121             fakeInputMethodRepository.selectedInputMethodSubtypes =
122                 listOf(
123                     InputMethodModel.Subtype(subtypeId = 1, isAuxiliary = false),
124                     InputMethodModel.Subtype(subtypeId = 2, isAuxiliary = false),
125                 )
126 
127             assertThat(underTest.hasMultipleEnabledImesOrSubtypes(USER_ID)).isTrue()
128         }
129 
130     @Test
showImePicker_shownOnCorrectIdnull131     fun showImePicker_shownOnCorrectId() =
132         testScope.runTest {
133             val displayId = 7
134 
135             underTest.showInputMethodPicker(displayId, showAuxiliarySubtypes = false)
136 
137             assertThat(fakeInputMethodRepository.inputMethodPickerShownDisplayId)
138                 .isEqualTo(displayId)
139         }
140 
createInputMethodWithSubtypesnull141     private fun createInputMethodWithSubtypes(
142         auxiliarySubtypes: Int,
143         nonAuxiliarySubtypes: Int,
144     ): InputMethodModel {
145         return InputMethodModel(
146             userId = UUID.randomUUID().mostSignificantBits.toInt(),
147             imeId = UUID.randomUUID().toString(),
148             subtypes =
149                 List(auxiliarySubtypes + nonAuxiliarySubtypes) {
150                     InputMethodModel.Subtype(subtypeId = it, isAuxiliary = it < auxiliarySubtypes)
151                 }
152         )
153     }
154 
155     companion object {
156         private const val USER_ID = 100
157     }
158 }
159