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 package com.android.settings.bluetooth 18 19 import androidx.activity.ComponentActivity 20 import com.android.settings.R 21 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat 22 import com.google.common.truth.Truth.assertThat 23 import org.junit.After 24 import org.junit.Before 25 import org.junit.Test 26 import org.junit.runner.RunWith 27 import org.mockito.Mockito.spy 28 import org.robolectric.RobolectricTestRunner 29 import org.robolectric.android.controller.ActivityController 30 import org.robolectric.annotation.Config 31 import org.mockito.Mockito.`when` as whenever 32 33 @RunWith(RobolectricTestRunner::class) 34 @Config(shadows = [ShadowAlertDialogCompat::class]) 35 class RequestPermissionHelperTest { 36 private lateinit var activityController: ActivityController<ComponentActivity> 37 38 @Before setUpnull39 fun setUp() { 40 activityController = 41 ActivityController.of(ComponentActivity()).create().start().postCreate(null).resume() 42 } 43 44 @After tearDownnull45 fun tearDown() { 46 activityController.pause().stop().destroy() 47 } 48 49 @Test requestEnable_withAppLabelAndNoTimeout_hasCorrectMessagenull50 fun requestEnable_withAppLabelAndNoTimeout_hasCorrectMessage() { 51 val activity = activityController.get() 52 53 RequestPermissionHelper.requestEnable( 54 context = activity, 55 appLabel = "App Label", 56 timeout = -1, 57 onAllow = {}, 58 onDeny = {}, 59 )!!.show() 60 61 assertLatestMessageIs("App Label wants to turn on Bluetooth") 62 } 63 64 @Test requestEnable_withAppLabelAndZeroTimeout_hasCorrectMessagenull65 fun requestEnable_withAppLabelAndZeroTimeout_hasCorrectMessage() { 66 val activity = activityController.get() 67 68 RequestPermissionHelper.requestEnable( 69 context = activity, 70 appLabel = "App Label", 71 timeout = 0, 72 onAllow = {}, 73 onDeny = {}, 74 )!!.show() 75 76 assertLatestMessageIs( 77 "App Label wants to turn on Bluetooth and make your phone visible to other devices. " + 78 "You can change this later in Bluetooth settings." 79 ) 80 } 81 82 @Test requestEnable_withAppLabelAndNormalTimeout_hasCorrectMessagenull83 fun requestEnable_withAppLabelAndNormalTimeout_hasCorrectMessage() { 84 val activity = activityController.get() 85 86 RequestPermissionHelper.requestEnable( 87 context = activity, 88 appLabel = "App Label", 89 timeout = 120, 90 onAllow = {}, 91 onDeny = {}, 92 )!!.show() 93 94 assertLatestMessageIs( 95 "App Label wants to turn on Bluetooth and make your phone visible to other devices " + 96 "for 120 seconds." 97 ) 98 } 99 100 @Test requestEnable_withNoAppLabelAndNoTimeout_hasCorrectMessagenull101 fun requestEnable_withNoAppLabelAndNoTimeout_hasCorrectMessage() { 102 val activity = activityController.get() 103 104 RequestPermissionHelper.requestEnable( 105 context = activity, 106 appLabel = null, 107 timeout = -1, 108 onAllow = {}, 109 onDeny = {}, 110 )!!.show() 111 112 assertLatestMessageIs("An app wants to turn on Bluetooth") 113 } 114 115 @Test requestEnable_withNoAppLabelAndZeroTimeout_hasCorrectMessagenull116 fun requestEnable_withNoAppLabelAndZeroTimeout_hasCorrectMessage() { 117 val activity = activityController.get() 118 119 RequestPermissionHelper.requestEnable( 120 context = activity, 121 appLabel = null, 122 timeout = 0, 123 onAllow = {}, 124 onDeny = {}, 125 )!!.show() 126 127 assertLatestMessageIs( 128 "An app wants to turn on Bluetooth and make your phone visible to other devices. " + 129 "You can change this later in Bluetooth settings." 130 ) 131 } 132 133 @Test requestEnable_withNoAppLabelAndNormalTimeout_hasCorrectMessagenull134 fun requestEnable_withNoAppLabelAndNormalTimeout_hasCorrectMessage() { 135 val activity = activityController.get() 136 137 RequestPermissionHelper.requestEnable( 138 context = activity, 139 appLabel = null, 140 timeout = 120, 141 onAllow = {}, 142 onDeny = {}, 143 )!!.show() 144 145 assertLatestMessageIs( 146 "An app wants to turn on Bluetooth and make your phone visible to other devices for " + 147 "120 seconds." 148 ) 149 } 150 151 @Test requestEnable_whenAutoConfirm_onAllowIsCallednull152 fun requestEnable_whenAutoConfirm_onAllowIsCalled() { 153 val activity = getActivityWith(autoConfirm = true) 154 var onAllowCalled = false 155 156 RequestPermissionHelper.requestEnable( 157 context = activity, 158 appLabel = null, 159 timeout = -1, 160 onAllow = { onAllowCalled = true }, 161 onDeny = {}, 162 ) 163 164 assertThat(onAllowCalled).isTrue() 165 } 166 167 @Test requestEnable_whenNotAutoConfirm_onAllowIsNotCalledWhenRequestnull168 fun requestEnable_whenNotAutoConfirm_onAllowIsNotCalledWhenRequest() { 169 val activity = getActivityWith(autoConfirm = false) 170 var onAllowCalled = false 171 172 RequestPermissionHelper.requestEnable( 173 context = activity, 174 appLabel = null, 175 timeout = -1, 176 onAllow = { onAllowCalled = true }, 177 onDeny = {}, 178 ) 179 180 assertThat(onAllowCalled).isFalse() 181 } 182 183 @Test requestDisable_withAppLabel_hasCorrectMessagenull184 fun requestDisable_withAppLabel_hasCorrectMessage() { 185 val activity = activityController.get() 186 187 RequestPermissionHelper.requestDisable( 188 context = activity, 189 appLabel = "App Label", 190 onAllow = {}, 191 onDeny = {}, 192 )!!.show() 193 194 assertLatestMessageIs("App Label wants to turn off Bluetooth") 195 } 196 197 @Test requestDisable_withNoAppLabel_hasCorrectMessagenull198 fun requestDisable_withNoAppLabel_hasCorrectMessage() { 199 val activity = activityController.get() 200 201 RequestPermissionHelper.requestDisable( 202 context = activity, 203 appLabel = null, 204 onAllow = {}, 205 onDeny = {}, 206 )!!.show() 207 208 assertLatestMessageIs("An app wants to turn off Bluetooth") 209 } 210 211 @Test requestDisable_whenAutoConfirm_onAllowIsCallednull212 fun requestDisable_whenAutoConfirm_onAllowIsCalled() { 213 val activity = getActivityWith(autoConfirm = true) 214 var onAllowCalled = false 215 216 RequestPermissionHelper.requestDisable( 217 context = activity, 218 appLabel = null, 219 onAllow = { onAllowCalled = true }, 220 onDeny = {}, 221 ) 222 223 assertThat(onAllowCalled).isTrue() 224 } 225 226 @Test requestDisable_whenNotAutoConfirm_onAllowIsNotCalledWhenRequestnull227 fun requestDisable_whenNotAutoConfirm_onAllowIsNotCalledWhenRequest() { 228 val activity = getActivityWith(autoConfirm = false) 229 var onAllowCalled = false 230 231 RequestPermissionHelper.requestDisable( 232 context = activity, 233 appLabel = null, 234 onAllow = { onAllowCalled = true }, 235 onDeny = {}, 236 ) 237 238 assertThat(onAllowCalled).isFalse() 239 } 240 getActivityWithnull241 private fun getActivityWith(autoConfirm: Boolean): ComponentActivity { 242 val activity = spy(activityController.get()) 243 val spyResources = spy(activity.resources) 244 whenever(activity.resources).thenReturn(spyResources) 245 whenever(spyResources.getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) 246 .thenReturn(autoConfirm) 247 return activity 248 } 249 assertLatestMessageIsnull250 private fun assertLatestMessageIs(message: String) { 251 val dialog = ShadowAlertDialogCompat.getLatestAlertDialog() 252 val shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog) 253 assertThat(shadowDialog.message.toString()).isEqualTo(message) 254 } 255 } 256