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 android.testing.AndroidTestingRunner 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.settings.FakeControlsSettingsRepository 26 import com.android.systemui.controls.controller.ControlsController 27 import com.android.systemui.controls.controller.ControlsTileResourceConfiguration 28 import com.android.systemui.controls.management.ControlsListingController 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 dagger.Lazy 33 import java.util.Optional 34 import org.junit.Assert.assertEquals 35 import org.junit.Assert.assertFalse 36 import org.junit.Assert.assertTrue 37 import org.junit.Before 38 import org.junit.Test 39 import org.junit.runner.RunWith 40 import org.mockito.Answers 41 import org.mockito.Mock 42 import org.mockito.Mockito 43 import org.mockito.Mockito.anyInt 44 import org.mockito.Mockito.`when` 45 import org.mockito.Mockito.any 46 import org.mockito.MockitoAnnotations 47 48 @SmallTest 49 @RunWith(AndroidTestingRunner::class) 50 class ControlsComponentTest : SysuiTestCase() { 51 52 @Mock 53 private lateinit var controller: ControlsController 54 @Mock 55 private lateinit var uiController: ControlsUiController 56 @Mock 57 private lateinit var listingController: ControlsListingController 58 @Mock 59 private lateinit var keyguardStateController: KeyguardStateController 60 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 61 private lateinit var userTracker: UserTracker 62 @Mock 63 private lateinit var lockPatternUtils: LockPatternUtils 64 @Mock 65 private lateinit var optionalControlsTileResourceConfiguration: 66 Optional<ControlsTileResourceConfiguration> 67 @Mock 68 private lateinit var controlsTileResourceConfiguration: ControlsTileResourceConfiguration 69 70 private lateinit var controlsSettingsRepository: FakeControlsSettingsRepository 71 72 companion object { eqnull73 fun <T> eq(value: T): T = Mockito.eq(value) ?: value 74 } 75 76 @Before 77 fun setUp() { 78 MockitoAnnotations.initMocks(this) 79 80 controlsSettingsRepository = FakeControlsSettingsRepository() 81 82 `when`(userTracker.userHandle.identifier).thenReturn(0) 83 `when`(optionalControlsTileResourceConfiguration.orElse(any())) 84 .thenReturn(controlsTileResourceConfiguration) 85 } 86 87 @Test testFeatureEnablednull88 fun testFeatureEnabled() { 89 val component = setupComponent(true) 90 91 assertTrue(component.getControlsController().isPresent) 92 assertEquals(controller, component.getControlsController().get()) 93 assertTrue(component.getControlsUiController().isPresent) 94 assertEquals(uiController, component.getControlsUiController().get()) 95 assertTrue(component.getControlsListingController().isPresent) 96 assertEquals(listingController, component.getControlsListingController().get()) 97 } 98 99 @Test testFeatureDisablednull100 fun testFeatureDisabled() { 101 val component = setupComponent(false) 102 103 assertFalse(component.getControlsController().isPresent) 104 assertFalse(component.getControlsUiController().isPresent) 105 assertFalse(component.getControlsListingController().isPresent) 106 } 107 108 @Test testFeatureDisabledVisibilitynull109 fun testFeatureDisabledVisibility() { 110 val component = setupComponent(false) 111 112 assertEquals(ControlsComponent.Visibility.UNAVAILABLE, component.getVisibility()) 113 } 114 115 @Test testFeatureEnabledAfterBootVisibilitynull116 fun testFeatureEnabledAfterBootVisibility() { 117 `when`(lockPatternUtils.getStrongAuthForUser(anyInt())) 118 .thenReturn(STRONG_AUTH_REQUIRED_AFTER_BOOT) 119 val component = setupComponent(true) 120 121 assertEquals(ControlsComponent.Visibility.AVAILABLE_AFTER_UNLOCK, component.getVisibility()) 122 } 123 124 @Test testFeatureEnabledAndCannotShowOnLockScreenVisibilitynull125 fun testFeatureEnabledAndCannotShowOnLockScreenVisibility() { 126 `when`(lockPatternUtils.getStrongAuthForUser(anyInt())) 127 .thenReturn(STRONG_AUTH_NOT_REQUIRED) 128 `when`(keyguardStateController.isUnlocked()).thenReturn(false) 129 controlsSettingsRepository.setCanShowControlsInLockscreen(false) 130 val component = setupComponent(true) 131 132 assertEquals(ControlsComponent.Visibility.AVAILABLE_AFTER_UNLOCK, component.getVisibility()) 133 } 134 135 @Test testFeatureEnabledAndCanShowOnLockScreenVisibilitynull136 fun testFeatureEnabledAndCanShowOnLockScreenVisibility() { 137 `when`(lockPatternUtils.getStrongAuthForUser(anyInt())) 138 .thenReturn(STRONG_AUTH_NOT_REQUIRED) 139 `when`(keyguardStateController.isUnlocked()).thenReturn(false) 140 controlsSettingsRepository.setCanShowControlsInLockscreen(true) 141 val component = setupComponent(true) 142 143 assertEquals(ControlsComponent.Visibility.AVAILABLE, component.getVisibility()) 144 } 145 146 @Test testFeatureEnabledAndCanShowWhileUnlockedVisibilitynull147 fun testFeatureEnabledAndCanShowWhileUnlockedVisibility() { 148 controlsSettingsRepository.setCanShowControlsInLockscreen(false) 149 `when`(lockPatternUtils.getStrongAuthForUser(anyInt())) 150 .thenReturn(STRONG_AUTH_NOT_REQUIRED) 151 `when`(keyguardStateController.isUnlocked()).thenReturn(true) 152 val component = setupComponent(true) 153 154 assertEquals(ControlsComponent.Visibility.AVAILABLE, component.getVisibility()) 155 } 156 157 @Test testGetTileImageIdnull158 fun testGetTileImageId() { 159 val tileImageId = 0 160 161 `when`(controlsTileResourceConfiguration.getTileImageId()) 162 .thenReturn(tileImageId) 163 val component = setupComponent(true) 164 assertEquals(component.getTileImageId(), tileImageId) 165 } 166 167 @Test testGetTileTitleIdnull168 fun testGetTileTitleId() { 169 val tileTitleId = 0 170 171 `when`(controlsTileResourceConfiguration.getTileTitleId()) 172 .thenReturn(tileTitleId) 173 val component = setupComponent(true) 174 assertEquals(component.getTileTitleId(), tileTitleId) 175 } 176 setupComponentnull177 private fun setupComponent(enabled: Boolean): ControlsComponent { 178 return ControlsComponent( 179 enabled, 180 mContext, 181 Lazy { controller }, 182 Lazy { uiController }, 183 Lazy { listingController }, 184 lockPatternUtils, 185 keyguardStateController, 186 userTracker, 187 controlsSettingsRepository, 188 optionalControlsTileResourceConfiguration 189 ) 190 } 191 } 192