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