1 /* 2 * Copyright (C) 2022 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.safetycenter; 18 19 import static android.safetycenter.SafetyEvent.SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.ArgumentMatchers.eq; 26 import static org.mockito.Mockito.never; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.content.Context; 31 import android.content.Intent; 32 import android.safetycenter.SafetyEvent; 33 import android.safetycenter.SafetySourceData; 34 import android.safetycenter.SafetySourceIssue; 35 import android.safetycenter.SafetySourceStatus; 36 import android.safetycenter.SafetySourceStatus.IconAction; 37 38 import androidx.test.core.app.ApplicationProvider; 39 import androidx.test.ext.junit.runners.AndroidJUnit4; 40 41 import com.android.internal.widget.LockPatternUtils; 42 import com.android.settings.security.ScreenLockPreferenceDetailsUtils; 43 import com.android.settings.testutils.FakeFeatureFactory; 44 import com.android.settings.testutils.ResourcesUtils; 45 46 import org.junit.After; 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.mockito.ArgumentCaptor; 51 import org.mockito.Mock; 52 import org.mockito.MockitoAnnotations; 53 54 @RunWith(AndroidJUnit4.class) 55 public class LockScreenSafetySourceTest { 56 57 private static final String SUMMARY = "summary"; 58 private static final String FAKE_ACTION_OPEN_SUB_SETTING = "open_sub_setting"; 59 private static final String EXTRA_DESTINATION = "destination"; 60 private static final String FAKE_CHOOSE_LOCK_GENERIC_FRAGMENT = "choose_lock_generic"; 61 private static final String FAKE_SCREEN_LOCK_SETTINGS = "screen_lock_settings"; 62 private static final SafetyEvent EVENT_SOURCE_STATE_CHANGED = 63 new SafetyEvent.Builder(SAFETY_EVENT_TYPE_SOURCE_STATE_CHANGED).build(); 64 65 private Context mApplicationContext; 66 67 @Mock private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper; 68 69 @Mock private ScreenLockPreferenceDetailsUtils mScreenLockPreferenceDetailsUtils; 70 71 @Mock private LockPatternUtils mLockPatternUtils; 72 73 @Before setUp()74 public void setUp() { 75 MockitoAnnotations.initMocks(this); 76 mApplicationContext = ApplicationProvider.getApplicationContext(); 77 SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper; 78 final FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest(); 79 when(featureFactory.securityFeatureProvider.getLockPatternUtils(mApplicationContext)) 80 .thenReturn(mLockPatternUtils); 81 } 82 83 @After tearDown()84 public void tearDown() { 85 SafetyCenterManagerWrapper.sInstance = null; 86 } 87 88 @Test setSafetySourceData_whenScreenLockEnabled_safetyCenterDisabled_doesNotSetData()89 public void setSafetySourceData_whenScreenLockEnabled_safetyCenterDisabled_doesNotSetData() { 90 whenScreenLockIsEnabled(); 91 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(false); 92 when(mScreenLockPreferenceDetailsUtils.isAvailable()).thenReturn(true); 93 94 LockScreenSafetySource.setSafetySourceData( 95 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 96 97 verify(mSafetyCenterManagerWrapper, never()) 98 .setSafetySourceData(any(), any(), any(), any()); 99 } 100 101 @Test setSafetySourceData_whenScreenLockIsDisabled_setsNullData()102 public void setSafetySourceData_whenScreenLockIsDisabled_setsNullData() { 103 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 104 when(mScreenLockPreferenceDetailsUtils.isAvailable()).thenReturn(false); 105 106 LockScreenSafetySource.setSafetySourceData( 107 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 108 109 verify(mSafetyCenterManagerWrapper) 110 .setSafetySourceData( 111 any(), eq(LockScreenSafetySource.SAFETY_SOURCE_ID), eq(null), any()); 112 } 113 114 @Test setSafetySourceData_setsDataForLockscreenSafetySource()115 public void setSafetySourceData_setsDataForLockscreenSafetySource() { 116 whenScreenLockIsEnabled(); 117 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 118 119 LockScreenSafetySource.setSafetySourceData( 120 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 121 122 verify(mSafetyCenterManagerWrapper) 123 .setSafetySourceData( 124 any(), eq(LockScreenSafetySource.SAFETY_SOURCE_ID), any(), any()); 125 } 126 127 @Test setSafetySourceData_setsDataWithCorrectSafetyEvent()128 public void setSafetySourceData_setsDataWithCorrectSafetyEvent() { 129 whenScreenLockIsEnabled(); 130 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 131 132 LockScreenSafetySource.setSafetySourceData( 133 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 134 135 verify(mSafetyCenterManagerWrapper) 136 .setSafetySourceData(any(), any(), any(), eq(EVENT_SOURCE_STATE_CHANGED)); 137 } 138 139 @Test setSafetySourceData_whenScreenLockIsEnabled_setData()140 public void setSafetySourceData_whenScreenLockIsEnabled_setData() { 141 whenScreenLockIsEnabled(); 142 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 143 144 LockScreenSafetySource.setSafetySourceData( 145 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 146 147 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 148 verify(mSafetyCenterManagerWrapper) 149 .setSafetySourceData(any(), any(), captor.capture(), any()); 150 SafetySourceData safetySourceData = captor.getValue(); 151 SafetySourceStatus safetySourceStatus = safetySourceData.getStatus(); 152 153 assertThat(safetySourceStatus.getTitle().toString()) 154 .isEqualTo( 155 ResourcesUtils.getResourcesString( 156 mApplicationContext, "unlock_set_unlock_launch_picker_title")); 157 assertThat(safetySourceStatus.getSummary().toString()).isEqualTo(SUMMARY); 158 assertThat(safetySourceStatus.getPendingIntent().getIntent()).isNotNull(); 159 assertThat(safetySourceStatus.getPendingIntent().getIntent().getAction()) 160 .isEqualTo(FAKE_ACTION_OPEN_SUB_SETTING); 161 assertThat( 162 safetySourceStatus 163 .getPendingIntent() 164 .getIntent() 165 .getStringExtra(EXTRA_DESTINATION)) 166 .isEqualTo(FAKE_CHOOSE_LOCK_GENERIC_FRAGMENT); 167 } 168 169 @Test setSafetySourceData_whenPwdQualIsNotMan_whenLockPattIsSec_setStatusLevelInfo()170 public void setSafetySourceData_whenPwdQualIsNotMan_whenLockPattIsSec_setStatusLevelInfo() { 171 whenScreenLockIsEnabled(); 172 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 173 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(true); 174 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 175 .thenReturn(false); 176 177 LockScreenSafetySource.setSafetySourceData( 178 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 179 180 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 181 verify(mSafetyCenterManagerWrapper) 182 .setSafetySourceData( 183 any(), 184 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 185 captor.capture(), 186 any()); 187 SafetySourceData safetySourceData = captor.getValue(); 188 SafetySourceStatus safetySourceStatus = safetySourceData.getStatus(); 189 190 assertThat(safetySourceStatus.getSeverityLevel()) 191 .isEqualTo(SafetySourceData.SEVERITY_LEVEL_INFORMATION); 192 } 193 194 @Test setSafetySourceData_whenPwdQualIsNotMan_whenLockPattIsNotSec_setStatusLevelRec()195 public void setSafetySourceData_whenPwdQualIsNotMan_whenLockPattIsNotSec_setStatusLevelRec() { 196 whenScreenLockIsEnabled(); 197 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 198 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(false); 199 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 200 .thenReturn(false); 201 202 LockScreenSafetySource.setSafetySourceData( 203 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 204 205 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 206 verify(mSafetyCenterManagerWrapper) 207 .setSafetySourceData( 208 any(), 209 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 210 captor.capture(), 211 any()); 212 SafetySourceData safetySourceData = captor.getValue(); 213 SafetySourceStatus safetySourceStatus = safetySourceData.getStatus(); 214 215 assertThat(safetySourceStatus.getSeverityLevel()) 216 .isEqualTo(SafetySourceData.SEVERITY_LEVEL_RECOMMENDATION); 217 } 218 219 @Test setSafetySourceData_whenPwdQualIsMan_whenLockPattIsSec_setStatusLevelUnsp()220 public void setSafetySourceData_whenPwdQualIsMan_whenLockPattIsSec_setStatusLevelUnsp() { 221 whenScreenLockIsEnabled(); 222 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 223 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(true); 224 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 225 .thenReturn(true); 226 227 LockScreenSafetySource.setSafetySourceData( 228 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 229 230 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 231 verify(mSafetyCenterManagerWrapper) 232 .setSafetySourceData( 233 any(), 234 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 235 captor.capture(), 236 any()); 237 SafetySourceData safetySourceData = captor.getValue(); 238 SafetySourceStatus safetySourceStatus = safetySourceData.getStatus(); 239 240 assertThat(safetySourceStatus.getSeverityLevel()) 241 .isEqualTo(SafetySourceData.SEVERITY_LEVEL_UNSPECIFIED); 242 } 243 244 @Test setSafetySourceData_whenPwdQualIsMan_whenLockPattIsNotSec_setStatusLevelUnsp()245 public void setSafetySourceData_whenPwdQualIsMan_whenLockPattIsNotSec_setStatusLevelUnsp() { 246 whenScreenLockIsEnabled(); 247 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 248 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(false); 249 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 250 .thenReturn(true); 251 252 LockScreenSafetySource.setSafetySourceData( 253 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 254 255 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 256 verify(mSafetyCenterManagerWrapper) 257 .setSafetySourceData( 258 any(), 259 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 260 captor.capture(), 261 any()); 262 SafetySourceData safetySourceData = captor.getValue(); 263 SafetySourceStatus safetySourceStatus = safetySourceData.getStatus(); 264 265 assertThat(safetySourceStatus.getSeverityLevel()) 266 .isEqualTo(SafetySourceData.SEVERITY_LEVEL_UNSPECIFIED); 267 } 268 269 @Test setSafetySourceData_whenPwdQualIsNotMan_whenLockPattIsSec_doesNotSetIssues()270 public void setSafetySourceData_whenPwdQualIsNotMan_whenLockPattIsSec_doesNotSetIssues() { 271 whenScreenLockIsEnabled(); 272 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 273 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(true); 274 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 275 .thenReturn(false); 276 277 LockScreenSafetySource.setSafetySourceData( 278 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 279 280 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 281 verify(mSafetyCenterManagerWrapper) 282 .setSafetySourceData(any(), any(), captor.capture(), any()); 283 SafetySourceData safetySourceData = captor.getValue(); 284 285 assertThat(safetySourceData.getIssues()).isEmpty(); 286 } 287 288 @Test setSafetySourceData_whenPwdQualIsNotMan_whenLockPattIsNotSec_setsIssue()289 public void setSafetySourceData_whenPwdQualIsNotMan_whenLockPattIsNotSec_setsIssue() { 290 whenScreenLockIsEnabled(); 291 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 292 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(false); 293 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 294 .thenReturn(false); 295 296 LockScreenSafetySource.setSafetySourceData( 297 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 298 299 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 300 verify(mSafetyCenterManagerWrapper) 301 .setSafetySourceData( 302 any(), 303 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 304 captor.capture(), 305 any()); 306 SafetySourceData safetySourceData = captor.getValue(); 307 308 assertThat(safetySourceData.getIssues()).hasSize(1); 309 SafetySourceIssue issue = safetySourceData.getIssues().get(0); 310 assertThat(issue.getId()).isEqualTo(LockScreenSafetySource.NO_SCREEN_LOCK_ISSUE_ID); 311 assertThat(issue.getTitle().toString()) 312 .isEqualTo( 313 ResourcesUtils.getResourcesString( 314 mApplicationContext, "no_screen_lock_issue_title")); 315 assertThat(issue.getSummary().toString()) 316 .isEqualTo( 317 ResourcesUtils.getResourcesString( 318 mApplicationContext, "no_screen_lock_issue_summary")); 319 assertThat(issue.getSeverityLevel()) 320 .isEqualTo(SafetySourceData.SEVERITY_LEVEL_RECOMMENDATION); 321 assertThat(issue.getIssueTypeId()) 322 .isEqualTo(LockScreenSafetySource.NO_SCREEN_LOCK_ISSUE_TYPE_ID); 323 assertThat(issue.getIssueCategory()).isEqualTo(SafetySourceIssue.ISSUE_CATEGORY_DEVICE); 324 assertThat(issue.getActions()).hasSize(1); 325 SafetySourceIssue.Action action = issue.getActions().get(0); 326 assertThat(action.getId()).isEqualTo(LockScreenSafetySource.SET_SCREEN_LOCK_ACTION_ID); 327 assertThat(action.getLabel().toString()) 328 .isEqualTo( 329 ResourcesUtils.getResourcesString( 330 mApplicationContext, "no_screen_lock_issue_action_label")); 331 assertThat(action.getPendingIntent().getIntent().getAction()) 332 .isEqualTo(FAKE_ACTION_OPEN_SUB_SETTING); 333 assertThat(action.getPendingIntent().getIntent().getStringExtra(EXTRA_DESTINATION)) 334 .isEqualTo(FAKE_CHOOSE_LOCK_GENERIC_FRAGMENT); 335 } 336 337 @Test setSafetySourceData_whenPwdQualIsMan_whenLockPattIsSec_doesNotSetIssues()338 public void setSafetySourceData_whenPwdQualIsMan_whenLockPattIsSec_doesNotSetIssues() { 339 whenScreenLockIsEnabled(); 340 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 341 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(true); 342 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 343 .thenReturn(true); 344 345 LockScreenSafetySource.setSafetySourceData( 346 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 347 348 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 349 verify(mSafetyCenterManagerWrapper) 350 .setSafetySourceData(any(), any(), captor.capture(), any()); 351 SafetySourceData safetySourceData = captor.getValue(); 352 353 assertThat(safetySourceData.getIssues()).isEmpty(); 354 } 355 356 @Test setSafetySourceData_whenPwdQualIsMan_whenLockPattIsNotSec_doesNotSetIssues()357 public void setSafetySourceData_whenPwdQualIsMan_whenLockPattIsNotSec_doesNotSetIssues() { 358 whenScreenLockIsEnabled(); 359 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 360 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(false); 361 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 362 .thenReturn(true); 363 364 LockScreenSafetySource.setSafetySourceData( 365 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 366 367 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 368 verify(mSafetyCenterManagerWrapper) 369 .setSafetySourceData( 370 any(), 371 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 372 captor.capture(), 373 any()); 374 SafetySourceData safetySourceData = captor.getValue(); 375 376 assertThat(safetySourceData.getIssues()).isEmpty(); 377 } 378 379 @Test setSafetySourceData_whenPasswordQualityIsManaged_setDisabled()380 public void setSafetySourceData_whenPasswordQualityIsManaged_setDisabled() { 381 whenScreenLockIsEnabled(); 382 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 383 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 384 .thenReturn(true); 385 386 LockScreenSafetySource.setSafetySourceData( 387 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 388 389 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 390 verify(mSafetyCenterManagerWrapper) 391 .setSafetySourceData( 392 any(), 393 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 394 captor.capture(), 395 any()); 396 SafetySourceData safetySourceData = captor.getValue(); 397 SafetySourceStatus safetySourceStatus = safetySourceData.getStatus(); 398 399 assertThat(safetySourceStatus.isEnabled()).isFalse(); 400 assertThat(safetySourceStatus.getPendingIntent()).isNull(); 401 assertThat(safetySourceStatus.getIconAction()).isNull(); 402 assertThat(safetySourceStatus.getSeverityLevel()) 403 .isEqualTo(SafetySourceData.SEVERITY_LEVEL_UNSPECIFIED); 404 assertThat(safetySourceStatus.getSummary().toString()) 405 .isEqualTo( 406 ResourcesUtils.getResourcesString( 407 mApplicationContext, "disabled_by_policy_title")); 408 } 409 410 @Test setSafetySourceData_whenPasswordQualityIsNotManaged_setEnabled()411 public void setSafetySourceData_whenPasswordQualityIsNotManaged_setEnabled() { 412 whenScreenLockIsEnabled(); 413 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 414 when(mScreenLockPreferenceDetailsUtils.isPasswordQualityManaged(anyInt(), any())) 415 .thenReturn(false); 416 when(mScreenLockPreferenceDetailsUtils.isLockPatternSecure()).thenReturn(true); 417 when(mScreenLockPreferenceDetailsUtils.shouldShowGearMenu()).thenReturn(true); 418 419 LockScreenSafetySource.setSafetySourceData( 420 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 421 422 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 423 verify(mSafetyCenterManagerWrapper) 424 .setSafetySourceData( 425 any(), 426 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 427 captor.capture(), 428 any()); 429 SafetySourceData safetySourceData = captor.getValue(); 430 SafetySourceStatus safetySourceStatus = safetySourceData.getStatus(); 431 432 assertThat(safetySourceStatus.isEnabled()).isTrue(); 433 assertThat(safetySourceStatus.getPendingIntent()).isNotNull(); 434 assertThat(safetySourceStatus.getIconAction()).isNotNull(); 435 assertThat(safetySourceStatus.getSeverityLevel()) 436 .isEqualTo(SafetySourceData.SEVERITY_LEVEL_INFORMATION); 437 assertThat(safetySourceStatus.getSummary().toString()).isEqualTo(SUMMARY); 438 } 439 440 @Test setSafetySourceData_whenShouldShowGearMenu_setGearMenuActionIcon()441 public void setSafetySourceData_whenShouldShowGearMenu_setGearMenuActionIcon() { 442 whenScreenLockIsEnabled(); 443 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 444 when(mScreenLockPreferenceDetailsUtils.shouldShowGearMenu()).thenReturn(true); 445 446 LockScreenSafetySource.setSafetySourceData( 447 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 448 449 final ArgumentCaptor<SafetySourceData> captor = 450 ArgumentCaptor.forClass(SafetySourceData.class); 451 verify(mSafetyCenterManagerWrapper) 452 .setSafetySourceData( 453 any(), 454 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 455 captor.capture(), 456 any()); 457 final IconAction iconAction = captor.getValue().getStatus().getIconAction(); 458 459 assertThat(iconAction.getIconType()).isEqualTo(IconAction.ICON_TYPE_GEAR); 460 assertThat(iconAction.getPendingIntent().getIntent().getAction()) 461 .isEqualTo(FAKE_ACTION_OPEN_SUB_SETTING); 462 assertThat(iconAction.getPendingIntent().getIntent().getStringExtra(EXTRA_DESTINATION)) 463 .isEqualTo(FAKE_SCREEN_LOCK_SETTINGS); 464 } 465 466 @Test setSafetySourceData_whenShouldNotShowGearMenu_doesNotSetGearMenuActionIcon()467 public void setSafetySourceData_whenShouldNotShowGearMenu_doesNotSetGearMenuActionIcon() { 468 whenScreenLockIsEnabled(); 469 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 470 when(mScreenLockPreferenceDetailsUtils.shouldShowGearMenu()).thenReturn(false); 471 472 LockScreenSafetySource.setSafetySourceData( 473 mApplicationContext, mScreenLockPreferenceDetailsUtils, EVENT_SOURCE_STATE_CHANGED); 474 475 ArgumentCaptor<SafetySourceData> captor = ArgumentCaptor.forClass(SafetySourceData.class); 476 verify(mSafetyCenterManagerWrapper) 477 .setSafetySourceData( 478 any(), 479 eq(LockScreenSafetySource.SAFETY_SOURCE_ID), 480 captor.capture(), 481 any()); 482 SafetySourceData safetySourceData = captor.getValue(); 483 SafetySourceStatus safetySourceStatus = safetySourceData.getStatus(); 484 485 assertThat(safetySourceStatus.getIconAction()).isNull(); 486 } 487 488 @Test onLockScreenChange_whenSafetyCenterEnabled_setsLockscreenAndBiometricData()489 public void onLockScreenChange_whenSafetyCenterEnabled_setsLockscreenAndBiometricData() { 490 whenScreenLockIsEnabled(); 491 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(true); 492 493 LockScreenSafetySource.onLockScreenChange(mApplicationContext); 494 495 verify(mSafetyCenterManagerWrapper) 496 .setSafetySourceData( 497 any(), eq(LockScreenSafetySource.SAFETY_SOURCE_ID), any(), any()); 498 verify(mSafetyCenterManagerWrapper) 499 .setSafetySourceData( 500 any(), eq(BiometricsSafetySource.SAFETY_SOURCE_ID), any(), any()); 501 } 502 503 @Test onLockScreenChange_whenSafetyCenterDisabled_doesNotSetData()504 public void onLockScreenChange_whenSafetyCenterDisabled_doesNotSetData() { 505 whenScreenLockIsEnabled(); 506 when(mSafetyCenterManagerWrapper.isEnabled(mApplicationContext)).thenReturn(false); 507 508 LockScreenSafetySource.onLockScreenChange(mApplicationContext); 509 510 verify(mSafetyCenterManagerWrapper, never()) 511 .setSafetySourceData(any(), any(), any(), any()); 512 } 513 whenScreenLockIsEnabled()514 private void whenScreenLockIsEnabled() { 515 when(mScreenLockPreferenceDetailsUtils.isAvailable()).thenReturn(true); 516 when(mScreenLockPreferenceDetailsUtils.getSummary(anyInt())).thenReturn(SUMMARY); 517 518 Intent launchChooseLockGenericFragment = new Intent(FAKE_ACTION_OPEN_SUB_SETTING); 519 launchChooseLockGenericFragment.putExtra( 520 EXTRA_DESTINATION, FAKE_CHOOSE_LOCK_GENERIC_FRAGMENT); 521 when(mScreenLockPreferenceDetailsUtils.getLaunchChooseLockGenericFragmentIntent(anyInt())) 522 .thenReturn(launchChooseLockGenericFragment); 523 524 Intent launchScreenLockSettings = new Intent(FAKE_ACTION_OPEN_SUB_SETTING); 525 launchScreenLockSettings.putExtra(EXTRA_DESTINATION, FAKE_SCREEN_LOCK_SETTINGS); 526 when(mScreenLockPreferenceDetailsUtils.getLaunchScreenLockSettingsIntent(anyInt())) 527 .thenReturn(launchScreenLockSettings); 528 } 529 } 530