1 /* 2 * Copyright (C) 2020 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.controls.dagger 18 19 import androidx.test.ext.junit.runners.AndroidJUnit4 20 import androidx.test.filters.SmallTest 21 import com.android.internal.widget.LockPatternUtils 22 import com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED 23 import com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT 24 import com.android.systemui.SysuiTestCase 25 import com.android.systemui.controls.controller.ControlsController 26 import com.android.systemui.controls.controller.ControlsTileResourceConfiguration 27 import com.android.systemui.controls.management.ControlsListingController 28 import com.android.systemui.controls.settings.FakeControlsSettingsRepository 29 import com.android.systemui.controls.ui.ControlsUiController 30 import com.android.systemui.settings.UserTracker 31 import com.android.systemui.statusbar.policy.KeyguardStateController 32 import java.util.Optional 33 import org.junit.Assert.assertEquals 34 import org.junit.Assert.assertFalse 35 import org.junit.Assert.assertTrue 36 import org.junit.Before 37 import org.junit.Test 38 import org.junit.runner.RunWith 39 import org.mockito.Answers 40 import org.mockito.Mock 41 import org.mockito.Mockito 42 import org.mockito.Mockito.any 43 import org.mockito.Mockito.anyInt 44 import org.mockito.Mockito.`when` 45 import org.mockito.MockitoAnnotations 46 47 @SmallTest 48 @RunWith(AndroidJUnit4::class) 49 class ControlsComponentTest : SysuiTestCase() { 50 51 @Mock private lateinit var controller: ControlsController 52 @Mock private lateinit var uiController: ControlsUiController 53 @Mock private lateinit var listingController: ControlsListingController 54 @Mock private lateinit var keyguardStateController: KeyguardStateController 55 @Mock(answer = Answers.RETURNS_DEEP_STUBS) private lateinit var userTracker: UserTracker 56 @Mock private lateinit var lockPatternUtils: LockPatternUtils 57 @Mock 58 private lateinit var optionalControlsTileResourceConfiguration: 59 Optional<ControlsTileResourceConfiguration> 60 @Mock private lateinit var controlsTileResourceConfiguration: ControlsTileResourceConfiguration 61 62 private lateinit var controlsSettingsRepository: FakeControlsSettingsRepository 63 64 companion object { eqnull65 fun <T> eq(value: T): T = Mockito.eq(value) ?: value 66 } 67 68 @Before 69 fun setUp() { 70 MockitoAnnotations.initMocks(this) 71 72 controlsSettingsRepository = FakeControlsSettingsRepository() 73 74 `when`(userTracker.userHandle.identifier).thenReturn(0) 75 `when`(optionalControlsTileResourceConfiguration.orElse(any())) 76 .thenReturn(controlsTileResourceConfiguration) 77 } 78 79 @Test testFeatureEnablednull80 fun testFeatureEnabled() { 81 val component = setupComponent(true) 82 83 assertTrue(component.getControlsController().isPresent) 84 assertEquals(controller, component.getControlsController().get()) 85 assertTrue(component.getControlsUiController().isPresent) 86 assertEquals(uiController, component.getControlsUiController().get()) 87 assertTrue(component.getControlsListingController().isPresent) 88 assertEquals(listingController, component.getControlsListingController().get()) 89 } 90 91 @Test testFeatureDisablednull92 fun testFeatureDisabled() { 93 val component = setupComponent(false) 94 95 assertFalse(component.getControlsController().isPresent) 96 assertFalse(component.getControlsUiController().isPresent) 97 assertFalse(component.getControlsListingController().isPresent) 98 } 99 100 @Test testFeatureDisabledVisibilitynull101 fun testFeatureDisabledVisibility() { 102 val component = setupComponent(false) 103 104 assertEquals(ControlsComponent.Visibility.UNAVAILABLE, component.getVisibility()) 105 } 106 107 @Test testFeatureEnabledAfterBootVisibilitynull108 fun testFeatureEnabledAfterBootVisibility() { 109 `when`(lockPatternUtils.getStrongAuthForUser(anyInt())) 110 .thenReturn(STRONG_AUTH_REQUIRED_AFTER_BOOT) 111 val component = setupComponent(true) 112 113 assertEquals(ControlsComponent.Visibility.AVAILABLE_AFTER_UNLOCK, component.getVisibility()) 114 } 115 116 @Test testFeatureEnabledAndCannotShowOnLockScreenVisibilitynull117 fun testFeatureEnabledAndCannotShowOnLockScreenVisibility() { 118 `when`(lockPatternUtils.getStrongAuthForUser(anyInt())).thenReturn(STRONG_AUTH_NOT_REQUIRED) 119 `when`(keyguardStateController.isUnlocked()).thenReturn(false) 120 controlsSettingsRepository.setCanShowControlsInLockscreen(false) 121 val component = setupComponent(true) 122 123 assertEquals(ControlsComponent.Visibility.AVAILABLE_AFTER_UNLOCK, component.getVisibility()) 124 } 125 126 @Test testFeatureEnabledAndCanShowOnLockScreenVisibilitynull127 fun testFeatureEnabledAndCanShowOnLockScreenVisibility() { 128 `when`(lockPatternUtils.getStrongAuthForUser(anyInt())).thenReturn(STRONG_AUTH_NOT_REQUIRED) 129 `when`(keyguardStateController.isUnlocked()).thenReturn(false) 130 controlsSettingsRepository.setCanShowControlsInLockscreen(true) 131 val component = setupComponent(true) 132 133 assertEquals(ControlsComponent.Visibility.AVAILABLE, component.getVisibility()) 134 } 135 136 @Test testFeatureEnabledAndCanShowWhileUnlockedVisibilitynull137 fun testFeatureEnabledAndCanShowWhileUnlockedVisibility() { 138 controlsSettingsRepository.setCanShowControlsInLockscreen(false) 139 `when`(lockPatternUtils.getStrongAuthForUser(anyInt())).thenReturn(STRONG_AUTH_NOT_REQUIRED) 140 `when`(keyguardStateController.isUnlocked()).thenReturn(true) 141 val component = setupComponent(true) 142 143 assertEquals(ControlsComponent.Visibility.AVAILABLE, component.getVisibility()) 144 } 145 146 @Test testGetTileImageIdnull147 fun testGetTileImageId() { 148 val tileImageId = 0 149 150 `when`(controlsTileResourceConfiguration.getTileImageId()).thenReturn(tileImageId) 151 val component = setupComponent(true) 152 assertEquals(component.getTileImageId(), tileImageId) 153 } 154 155 @Test testGetTileTitleIdnull156 fun testGetTileTitleId() { 157 val tileTitleId = 0 158 159 `when`(controlsTileResourceConfiguration.getTileTitleId()).thenReturn(tileTitleId) 160 val component = setupComponent(true) 161 assertEquals(component.getTileTitleId(), tileTitleId) 162 } 163 164 @Test getPackageNamenull165 fun getPackageName() { 166 val packageName = "packageName" 167 `when`(controlsTileResourceConfiguration.getPackageName()).thenReturn(packageName) 168 val component = setupComponent(true) 169 assertEquals(component.getPackageName(), packageName) 170 } 171 setupComponentnull172 private fun setupComponent(enabled: Boolean): ControlsComponent { 173 return ControlsComponent( 174 enabled, 175 { controller }, 176 { uiController }, 177 { listingController }, 178 lockPatternUtils, 179 keyguardStateController, 180 userTracker, 181 controlsSettingsRepository, 182 optionalControlsTileResourceConfiguration 183 ) 184 } 185 } 186