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.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.Intent; 28 29 import androidx.preference.Preference; 30 import androidx.test.core.app.ApplicationProvider; 31 import androidx.test.ext.junit.runners.AndroidJUnit4; 32 33 import com.android.settings.SettingsActivity; 34 import com.android.settings.testutils.FakeFeatureFactory; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.ArgumentCaptor; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 43 @RunWith(AndroidJUnit4.class) 44 public class TopLevelSecurityEntryPreferenceControllerTest { 45 46 private static final String PREFERENCE_KEY = "top_level_security"; 47 private static final String ALTERNATIVE_FRAGMENT_CLASSNAME = "AlternativeFragmentClassname"; 48 49 private TopLevelSecurityEntryPreferenceController mTopLevelSecurityEntryPreferenceController; 50 private Preference mPreference; 51 private FakeFeatureFactory mFeatureFactory; 52 private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider; 53 54 @Mock 55 private Context mContext; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mFeatureFactory = FakeFeatureFactory.setupForTest(); 61 mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider(); 62 63 mPreference = new Preference(ApplicationProvider.getApplicationContext()); 64 mPreference.setKey(PREFERENCE_KEY); 65 66 doNothing().when(mContext).startActivity(any(Intent.class)); 67 mTopLevelSecurityEntryPreferenceController = 68 new TopLevelSecurityEntryPreferenceController(mContext, PREFERENCE_KEY); 69 } 70 71 @Test handlePreferenceTreeClick_forDifferentPreferenceKey_isNotHandled()72 public void handlePreferenceTreeClick_forDifferentPreferenceKey_isNotHandled() { 73 Preference preference = new Preference(ApplicationProvider.getApplicationContext()); 74 preference.setKey("some_other_preference"); 75 76 boolean preferenceHandled = 77 mTopLevelSecurityEntryPreferenceController.handlePreferenceTreeClick(preference); 78 79 assertThat(preferenceHandled).isFalse(); 80 } 81 82 @Test handlePreferenceTreeClick_withAlternativeFragment_launchesAlternativeFragment()83 public void handlePreferenceTreeClick_withAlternativeFragment_launchesAlternativeFragment() { 84 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 85 .thenReturn(true); 86 when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname()) 87 .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME); 88 final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 89 90 boolean preferenceHandled = 91 mTopLevelSecurityEntryPreferenceController.handlePreferenceTreeClick(mPreference); 92 93 assertThat(preferenceHandled).isTrue(); 94 verify(mContext).startActivity(intentCaptor.capture()); 95 assertThat(intentCaptor.getValue().getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)) 96 .isEqualTo(ALTERNATIVE_FRAGMENT_CLASSNAME); 97 } 98 99 @Test handlePreferenceTreeClick_withDisabledAlternative_isNotHandled()100 public void handlePreferenceTreeClick_withDisabledAlternative_isNotHandled() { 101 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 102 .thenReturn(false); 103 when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname()) 104 .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME); 105 106 boolean preferenceHandled = 107 mTopLevelSecurityEntryPreferenceController.handlePreferenceTreeClick(mPreference); 108 109 assertThat(preferenceHandled).isFalse(); 110 } 111 112 @Test handlePreferenceTreeClick_withoutAlternativeFragmentName_isNotHandled()113 public void handlePreferenceTreeClick_withoutAlternativeFragmentName_isNotHandled() { 114 when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) 115 .thenReturn(true); 116 when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname()) 117 .thenReturn(null); 118 119 boolean preferenceHandled = 120 mTopLevelSecurityEntryPreferenceController.handlePreferenceTreeClick(mPreference); 121 122 assertThat(preferenceHandled).isFalse(); 123 } 124 } 125