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.safetycenter.SafetyCenterManagerWrapper;
35 import com.android.settings.testutils.FakeFeatureFactory;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 
44 @RunWith(AndroidJUnit4.class)
45 public class TopLevelSecurityEntryPreferenceControllerTest {
46 
47     private static final String PREFERENCE_KEY = "top_level_security";
48     private static final String ALTERNATIVE_FRAGMENT_CLASSNAME = "AlternativeFragmentClassname";
49 
50     private TopLevelSecurityEntryPreferenceController mTopLevelSecurityEntryPreferenceController;
51     private Preference mPreference;
52     private FakeFeatureFactory mFeatureFactory;
53     private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider;
54 
55     @Mock
56     private Context mContext;
57     @Mock
58     private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mFeatureFactory = FakeFeatureFactory.setupForTest();
64         mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider();
65         SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper;
66 
67         mPreference = new Preference(ApplicationProvider.getApplicationContext());
68         mPreference.setKey(PREFERENCE_KEY);
69 
70         doNothing().when(mContext).startActivity(any(Intent.class));
71         mTopLevelSecurityEntryPreferenceController =
72                 new TopLevelSecurityEntryPreferenceController(mContext, PREFERENCE_KEY);
73     }
74 
75     @Test
handlePreferenceTreeClick_forDifferentPreferenceKey_isNotHandled()76     public void handlePreferenceTreeClick_forDifferentPreferenceKey_isNotHandled() {
77         Preference preference = new Preference(ApplicationProvider.getApplicationContext());
78         preference.setKey("some_other_preference");
79 
80         boolean preferenceHandled =
81                 mTopLevelSecurityEntryPreferenceController.handlePreferenceTreeClick(preference);
82 
83         assertThat(preferenceHandled).isFalse();
84     }
85 
86     @Test
handlePreferenceTreeClick_withAlternativeFragment_launchesAlternativeFragment()87     public void handlePreferenceTreeClick_withAlternativeFragment_launchesAlternativeFragment() {
88         when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
89                 .thenReturn(true);
90         when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname())
91                 .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME);
92         final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
93 
94         boolean preferenceHandled =
95                 mTopLevelSecurityEntryPreferenceController.handlePreferenceTreeClick(mPreference);
96 
97         assertThat(preferenceHandled).isTrue();
98         verify(mContext).startActivity(intentCaptor.capture());
99         assertThat(intentCaptor.getValue().getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
100                 .isEqualTo(ALTERNATIVE_FRAGMENT_CLASSNAME);
101     }
102 
103     @Test
handlePreferenceTreeClick_withDisabledAlternative_isNotHandled()104     public void handlePreferenceTreeClick_withDisabledAlternative_isNotHandled() {
105         when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
106                 .thenReturn(false);
107         when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname())
108                 .thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME);
109 
110         boolean preferenceHandled =
111                 mTopLevelSecurityEntryPreferenceController.handlePreferenceTreeClick(mPreference);
112 
113         assertThat(preferenceHandled).isFalse();
114     }
115 
116     @Test
handlePreferenceTreeClick_withoutAlternativeFragmentName_isNotHandled()117     public void handlePreferenceTreeClick_withoutAlternativeFragmentName_isNotHandled() {
118         when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
119                 .thenReturn(true);
120         when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname())
121                 .thenReturn(null);
122 
123         boolean preferenceHandled =
124                 mTopLevelSecurityEntryPreferenceController.handlePreferenceTreeClick(mPreference);
125 
126         assertThat(preferenceHandled).isFalse();
127     }
128 
129     @Test
getAvailabilityStatus_whenSafetyCenterEnabled_returnsUnavailable()130     public void getAvailabilityStatus_whenSafetyCenterEnabled_returnsUnavailable() {
131         when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(true);
132 
133         assertThat(mTopLevelSecurityEntryPreferenceController.getAvailabilityStatus())
134                 .isEqualTo(TopLevelSecurityEntryPreferenceController.CONDITIONALLY_UNAVAILABLE);
135     }
136 
137     @Test
getAvailabilityStatus_whenSafetyCenterDisabled_returnsAvailable()138     public void getAvailabilityStatus_whenSafetyCenterDisabled_returnsAvailable() {
139         when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(false);
140 
141         assertThat(mTopLevelSecurityEntryPreferenceController.getAvailabilityStatus())
142                 .isEqualTo(TopLevelSecurityEntryPreferenceController.AVAILABLE);
143     }
144 }
145