1 /* 2 * Copyright (C) 2021 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.security; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.doNothing; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.times; 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.content.pm.PackageManager; 33 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 import androidx.test.platform.app.InstrumentationRegistry; 36 37 import com.android.settings.Settings; 38 import com.android.settings.SettingsActivity; 39 import com.android.settings.safetycenter.SafetyCenterManagerWrapper; 40 import com.android.settings.testutils.FakeFeatureFactory; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.ArgumentCaptor; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 49 @RunWith(AndroidJUnit4.class) 50 public class SecurityDashboardActivityTest { 51 private static final String ALTERNATIVE_FRAGMENT_CLASSNAME = "AlternativeFragmentClassname"; 52 private static final String DEFAULT_FRAGMENT_CLASSNAME = "DefaultFragmentClassname"; 53 54 private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider; 55 private Settings.SecurityDashboardActivity mActivity; 56 private Intent mDefaultIntent; 57 58 @Mock 59 private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper; 60 61 @Before setUp()62 public void setUp() { 63 MockitoAnnotations.initMocks(this); 64 FakeFeatureFactory mFeatureFactory = FakeFeatureFactory.setupForTest(); 65 mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider(); 66 SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper; 67 68 mDefaultIntent = new Intent(); 69 mDefaultIntent.setAction(android.provider.Settings.ACTION_SECURITY_SETTINGS); 70 mDefaultIntent.setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(), 71 Settings.SecurityDashboardActivity.class); 72 mDefaultIntent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT_CLASSNAME); 73 InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { 74 try { 75 mActivity = 76 spy((Settings.SecurityDashboardActivity) InstrumentationRegistry 77 .getInstrumentation().newActivity( 78 getClass().getClassLoader(), 79 Settings.SecurityDashboardActivity.class.getName(), 80 mDefaultIntent)); 81 } catch (Exception e) { 82 throw new RuntimeException(e); // nothing to do 83 } 84 }); 85 doNothing().when(mActivity).startActivity(any(Intent.class)); 86 87 PackageManager pm = mock(PackageManager.class); 88 doReturn(pm).when(mActivity).getPackageManager(); 89 doReturn("com.android.permissioncontroller").when(pm).getPermissionControllerPackageName(); 90 } 91 92 @Test noAvailableAlternativeFragmentAvailable_defaultFragmentSet()93 public void noAvailableAlternativeFragmentAvailable_defaultFragmentSet() { 94 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 95 .thenReturn(false); 96 97 assertThat(mActivity.getInitialFragmentName(mDefaultIntent)) 98 .isEqualTo(DEFAULT_FRAGMENT_CLASSNAME); 99 } 100 101 @Test alternativeFragmentAvailable_alternativeFragmentSet()102 public void alternativeFragmentAvailable_alternativeFragmentSet() { 103 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 104 .thenReturn(true); 105 when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname()) 106 .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME); 107 108 assertThat(mActivity.getInitialFragmentName(mDefaultIntent)) 109 .isEqualTo(ALTERNATIVE_FRAGMENT_CLASSNAME); 110 } 111 112 @Test noAvailableAlternativeFragmentAvailable_alternativeFragmentNotValid()113 public void noAvailableAlternativeFragmentAvailable_alternativeFragmentNotValid() { 114 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 115 .thenReturn(false); 116 117 assertThat(mActivity.isValidFragment(ALTERNATIVE_FRAGMENT_CLASSNAME)).isFalse(); 118 } 119 120 @Test alternativeFragmentAvailable_alternativeFragmentIsValid()121 public void alternativeFragmentAvailable_alternativeFragmentIsValid() { 122 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 123 .thenReturn(true); 124 when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname()) 125 .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME); 126 127 assertThat(mActivity.isValidFragment(ALTERNATIVE_FRAGMENT_CLASSNAME)).isTrue(); 128 } 129 130 @Test onCreate_whenSafetyCenterEnabled_redirectsToSafetyCenter()131 public void onCreate_whenSafetyCenterEnabled_redirectsToSafetyCenter() { 132 when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(true); 133 final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 134 135 mActivity.handleSafetyCenterRedirection(); 136 137 verify(mActivity).startActivity(intentCaptor.capture()); 138 assertThat(intentCaptor.getValue().getAction()).isEqualTo(Intent.ACTION_SAFETY_CENTER); 139 } 140 141 @Test onCreate_whenSafetyCenterDisabled_doesntRedirectToSafetyCenter()142 public void onCreate_whenSafetyCenterDisabled_doesntRedirectToSafetyCenter() { 143 when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(false); 144 145 mActivity.handleSafetyCenterRedirection(); 146 147 verify(mActivity, times(0)).startActivity(any()); 148 } 149 } 150