1 /* 2 * Copyright (C) 2023 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 @file:OptIn(ExperimentalCoroutinesApi::class) 18 19 package com.android.systemui.accessibility.data.repository 20 21 import android.testing.AndroidTestingRunner 22 import android.view.accessibility.AccessibilityManager 23 import androidx.test.filters.SmallTest 24 import com.android.systemui.SysuiTestCase 25 import com.android.systemui.coroutines.collectLastValue 26 import com.android.systemui.util.mockito.whenever 27 import com.android.systemui.util.mockito.withArgCaptor 28 import com.google.common.truth.Truth.assertThat 29 import kotlinx.coroutines.ExperimentalCoroutinesApi 30 import kotlinx.coroutines.test.runCurrent 31 import kotlinx.coroutines.test.runTest 32 import org.junit.Rule 33 import org.junit.Test 34 import org.junit.runner.RunWith 35 import org.mockito.Mock 36 import org.mockito.Mockito.verify 37 import org.mockito.junit.MockitoJUnit 38 import org.mockito.junit.MockitoRule 39 40 @RunWith(AndroidTestingRunner::class) 41 @SmallTest 42 class AccessibilityRepositoryTest : SysuiTestCase() { 43 44 @Rule @JvmField val mockitoRule: MockitoRule = MockitoJUnit.rule() 45 46 // mocks 47 @Mock private lateinit var a11yManager: AccessibilityManager 48 49 // real impls <lambda>null50 private val underTest by lazy { AccessibilityRepository(a11yManager) } 51 52 @Test <lambda>null53 fun isTouchExplorationEnabled_reflectsA11yManager_initFalse() = runTest { 54 whenever(a11yManager.isTouchExplorationEnabled).thenReturn(false) 55 val isTouchExplorationEnabled by collectLastValue(underTest.isTouchExplorationEnabled) 56 assertThat(isTouchExplorationEnabled).isFalse() 57 } 58 59 @Test <lambda>null60 fun isTouchExplorationEnabled_reflectsA11yManager_initTrue() = runTest { 61 whenever(a11yManager.isTouchExplorationEnabled).thenReturn(true) 62 val isTouchExplorationEnabled by collectLastValue(underTest.isTouchExplorationEnabled) 63 assertThat(isTouchExplorationEnabled).isTrue() 64 } 65 66 @Test <lambda>null67 fun isTouchExplorationEnabled_reflectsA11yManager_changeTrue() = runTest { 68 whenever(a11yManager.isTouchExplorationEnabled).thenReturn(false) 69 val isTouchExplorationEnabled by collectLastValue(underTest.isTouchExplorationEnabled) 70 runCurrent() 71 withArgCaptor { verify(a11yManager).addTouchExplorationStateChangeListener(capture()) } 72 .onTouchExplorationStateChanged(/* enabled = */ true) 73 assertThat(isTouchExplorationEnabled).isTrue() 74 } 75 76 @Test isTouchExplorationEnabled_reflectsA11yManager_changeFalsenull77 fun isTouchExplorationEnabled_reflectsA11yManager_changeFalse() = runTest { 78 whenever(a11yManager.isTouchExplorationEnabled).thenReturn(true) 79 val isTouchExplorationEnabled by collectLastValue(underTest.isTouchExplorationEnabled) 80 runCurrent() 81 withArgCaptor { verify(a11yManager).addTouchExplorationStateChangeListener(capture()) } 82 .onTouchExplorationStateChanged(/* enabled = */ false) 83 assertThat(isTouchExplorationEnabled).isFalse() 84 } 85 } 86