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.Mockito.when; 22 23 import android.content.Intent; 24 25 import androidx.test.ext.junit.runners.AndroidJUnit4; 26 import androidx.test.platform.app.InstrumentationRegistry; 27 28 import com.android.settings.Settings; 29 import com.android.settings.SettingsActivity; 30 import com.android.settings.testutils.FakeFeatureFactory; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.MockitoAnnotations; 36 37 @RunWith(AndroidJUnit4.class) 38 public class SecurityDashboardActivityTest { 39 private static final String ALTERNATIVE_FRAGMENT_CLASSNAME = "AlternativeFragmentClassname"; 40 private static final String DEFAULT_FRAGMENT_CLASSNAME = "DefaultFragmentClassname"; 41 42 private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider; 43 private Settings.SecurityDashboardActivity mActivity; 44 private Intent mDefaultIntent; 45 46 @Before setUp()47 public void setUp() { 48 MockitoAnnotations.initMocks(this); 49 FakeFeatureFactory mFeatureFactory = FakeFeatureFactory.setupForTest(); 50 mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider(); 51 mDefaultIntent = new Intent(); 52 mDefaultIntent.setAction(android.provider.Settings.ACTION_SECURITY_SETTINGS); 53 mDefaultIntent.setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(), 54 Settings.SecurityDashboardActivity.class); 55 mDefaultIntent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT_CLASSNAME); 56 InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { 57 try { 58 mActivity = 59 (Settings.SecurityDashboardActivity) InstrumentationRegistry 60 .getInstrumentation().newActivity( 61 getClass().getClassLoader(), 62 Settings.SecurityDashboardActivity.class.getName(), 63 mDefaultIntent); 64 } catch (Exception e) { 65 throw new RuntimeException(e); // nothing to do 66 } 67 }); 68 } 69 70 @Test noAvailableAlternativeFragmentAvailable_defaultFragmentSet()71 public void noAvailableAlternativeFragmentAvailable_defaultFragmentSet() { 72 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 73 .thenReturn(false); 74 75 assertThat(mActivity.getInitialFragmentName(mDefaultIntent)) 76 .isEqualTo(DEFAULT_FRAGMENT_CLASSNAME); 77 } 78 79 @Test alternativeFragmentAvailable_alternativeFragmentSet()80 public void alternativeFragmentAvailable_alternativeFragmentSet() { 81 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 82 .thenReturn(true); 83 when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname()) 84 .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME); 85 86 assertThat(mActivity.getInitialFragmentName(mDefaultIntent)) 87 .isEqualTo(ALTERNATIVE_FRAGMENT_CLASSNAME); 88 } 89 90 @Test noAvailableAlternativeFragmentAvailable_alternativeFragmentNotValid()91 public void noAvailableAlternativeFragmentAvailable_alternativeFragmentNotValid() { 92 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 93 .thenReturn(false); 94 95 assertThat(mActivity.isValidFragment(ALTERNATIVE_FRAGMENT_CLASSNAME)).isFalse(); 96 } 97 98 @Test alternativeFragmentAvailable_alternativeFragmentIsValid()99 public void alternativeFragmentAvailable_alternativeFragmentIsValid() { 100 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 101 .thenReturn(true); 102 when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname()) 103 .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME); 104 105 assertThat(mActivity.isValidFragment(ALTERNATIVE_FRAGMENT_CLASSNAME)).isTrue(); 106 } 107 } 108