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.keyboard.stickykeys.ui 18 19 import android.app.Dialog 20 import androidx.test.ext.junit.runners.AndroidJUnit4 21 import androidx.test.filters.SmallTest 22 import com.android.systemui.SysuiTestCase 23 import com.android.systemui.keyboard.data.repository.FakeStickyKeysRepository 24 import com.android.systemui.keyboard.data.repository.keyboardRepository 25 import com.android.systemui.keyboard.stickykeys.StickyKeysLogger 26 import com.android.systemui.keyboard.stickykeys.shared.model.Locked 27 import com.android.systemui.keyboard.stickykeys.shared.model.ModifierKey.SHIFT 28 import com.android.systemui.keyboard.stickykeys.ui.viewmodel.StickyKeysIndicatorViewModel 29 import com.android.systemui.kosmos.Kosmos 30 import com.android.systemui.util.mockito.any 31 import com.android.systemui.util.mockito.mock 32 import com.android.systemui.util.mockito.whenever 33 import kotlinx.coroutines.ExperimentalCoroutinesApi 34 import kotlinx.coroutines.test.StandardTestDispatcher 35 import kotlinx.coroutines.test.TestScope 36 import kotlinx.coroutines.test.runCurrent 37 import org.junit.Before 38 import org.junit.Test 39 import org.junit.runner.RunWith 40 import org.mockito.Mockito.verify 41 import org.mockito.Mockito.verifyZeroInteractions 42 43 @OptIn(ExperimentalCoroutinesApi::class) 44 @SmallTest 45 @RunWith(AndroidJUnit4::class) 46 class StickyKeysIndicatorCoordinatorTest : SysuiTestCase() { 47 48 private lateinit var coordinator: StickyKeysIndicatorCoordinator 49 private val testScope = TestScope(StandardTestDispatcher()) 50 private val stickyKeysRepository = FakeStickyKeysRepository() 51 private val dialog = mock<Dialog>() 52 53 @Before setupnull54 fun setup() { 55 val dialogFactory = mock<StickyKeyDialogFactory>() 56 whenever(dialogFactory.create(any())).thenReturn(dialog) 57 val keyboardRepository = Kosmos().keyboardRepository 58 val viewModel = 59 StickyKeysIndicatorViewModel( 60 stickyKeysRepository, 61 keyboardRepository, 62 testScope.backgroundScope 63 ) 64 coordinator = 65 StickyKeysIndicatorCoordinator( 66 testScope.backgroundScope, 67 dialogFactory, 68 viewModel, 69 mock<StickyKeysLogger>() 70 ) 71 coordinator.startListening() 72 keyboardRepository.setIsAnyKeyboardConnected(true) 73 } 74 75 @Test dialogIsShownWhenStickyKeysAreEmittednull76 fun dialogIsShownWhenStickyKeysAreEmitted() { 77 testScope.run { 78 verifyZeroInteractions(dialog) 79 80 stickyKeysRepository.setStickyKeys(linkedMapOf(SHIFT to Locked(true))) 81 runCurrent() 82 83 verify(dialog).show() 84 } 85 } 86 87 @Test dialogDisappearsWhenStickyKeysAreEmptynull88 fun dialogDisappearsWhenStickyKeysAreEmpty() { 89 testScope.run { 90 verifyZeroInteractions(dialog) 91 92 stickyKeysRepository.setStickyKeys(linkedMapOf(SHIFT to Locked(true))) 93 runCurrent() 94 stickyKeysRepository.setStickyKeys(linkedMapOf()) 95 runCurrent() 96 97 verify(dialog).dismiss() 98 } 99 } 100 } 101