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.privacy; 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.spy; 24 import static org.mockito.Mockito.times; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.Intent; 30 31 import androidx.test.ext.junit.runners.AndroidJUnit4; 32 import androidx.test.platform.app.InstrumentationRegistry; 33 34 import com.android.settings.Settings; 35 import com.android.settings.SettingsActivity; 36 import com.android.settings.safetycenter.SafetyCenterManagerWrapper; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.ArgumentCaptor; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 45 @RunWith(AndroidJUnit4.class) 46 public class PrivacyDashboardActivityTest { 47 private static final String DEFAULT_FRAGMENT_CLASSNAME = "DefaultFragmentClassname"; 48 @Mock 49 private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper; 50 private Settings.PrivacyDashboardActivity mActivity; 51 private static final String ACTION_PRIVACY_ADVANCED_SETTINGS = 52 "android.settings.PRIVACY_ADVANCED_SETTINGS"; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper; 58 } 59 60 @Test onCreate_whenSafetyCenterEnabled_redirectsToSafetyCenter()61 public void onCreate_whenSafetyCenterEnabled_redirectsToSafetyCenter() throws Exception { 62 startActivityUsingIntent(android.provider.Settings.ACTION_PRIVACY_SETTINGS); 63 when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(true); 64 final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 65 mActivity.handleSafetyCenterRedirection(); 66 verify(mActivity).startActivity(intentCaptor.capture()); 67 assertThat(intentCaptor.getValue().getAction()).isEqualTo(Intent.ACTION_SAFETY_CENTER); 68 } 69 70 @Test onCreateWithAdvancedIntent_whenSafetyCenterEnabled_doesntRedirectToSafetyCenter()71 public void onCreateWithAdvancedIntent_whenSafetyCenterEnabled_doesntRedirectToSafetyCenter() 72 throws Exception { 73 startActivityUsingIntent(ACTION_PRIVACY_ADVANCED_SETTINGS); 74 when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(true); 75 final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 76 mActivity.handleSafetyCenterRedirection(); 77 verify(mActivity, times(0)).startActivity(any()); 78 } 79 80 @Test onCreate_whenSafetyCenterDisabled_doesntRedirectToSafetyCenter()81 public void onCreate_whenSafetyCenterDisabled_doesntRedirectToSafetyCenter() throws Exception { 82 startActivityUsingIntent(android.provider.Settings.ACTION_PRIVACY_SETTINGS); 83 when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(false); 84 mActivity.handleSafetyCenterRedirection(); 85 verify(mActivity, times(0)).startActivity(any()); 86 } 87 88 @Test onCreateWithAdvancedIntent_whenSafetyCenterDisabled_doesntRedirectToSafetyCenter()89 public void onCreateWithAdvancedIntent_whenSafetyCenterDisabled_doesntRedirectToSafetyCenter() 90 throws Exception { 91 startActivityUsingIntent(ACTION_PRIVACY_ADVANCED_SETTINGS); 92 when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(true); 93 final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 94 mActivity.handleSafetyCenterRedirection(); 95 verify(mActivity, times(0)).startActivity(any()); 96 } 97 startActivityUsingIntent(String intentAction)98 private void startActivityUsingIntent(String intentAction) throws Exception { 99 MockitoAnnotations.initMocks(this); 100 SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper; 101 final Intent intent = new Intent(); 102 intent.setAction(intentAction); 103 intent.setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(), 104 Settings.PrivacyDashboardActivity.class); 105 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT_CLASSNAME); 106 InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { 107 try { 108 Settings.PrivacyDashboardActivity activity = 109 (Settings.PrivacyDashboardActivity) InstrumentationRegistry 110 .getInstrumentation().newActivity( 111 getClass().getClassLoader(), 112 Settings.PrivacyDashboardActivity.class.getName(), 113 intent); 114 activity.setIntent(intent); 115 mActivity = spy(activity); 116 } catch (Exception e) { 117 throw new RuntimeException(e); // nothing to do 118 } 119 }); 120 doNothing().when(mActivity).startActivity(any(Intent.class)); 121 } 122 } 123