1 /*
2  * Copyright (C) 2016 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.enterprise;
18 
19 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_DEFAULT;
20 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.admin.DevicePolicyManager;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.os.Bundle;
31 import android.provider.SearchIndexableResource;
32 import android.widget.FrameLayout;
33 
34 import androidx.appcompat.app.AppCompatActivity;
35 import androidx.test.core.app.ApplicationProvider;
36 
37 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
38 import com.android.settings.R;
39 import com.android.settings.testutils.FakeFeatureFactory;
40 import com.android.settingslib.core.AbstractPreferenceController;
41 import com.android.settingslib.drawer.CategoryKey;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.Robolectric;
49 import org.robolectric.RobolectricTestRunner;
50 import org.robolectric.android.controller.ActivityController;
51 
52 import java.util.List;
53 
54 @RunWith(RobolectricTestRunner.class)
55 public class EnterprisePrivacySettingsTest extends AbsBasePrivacySettingsPreference {
56     private static final ComponentName DEVICE_OWNER_COMPONENT =
57             new ComponentName("com.android.foo", "bar");
58 
59     @Mock
60     private DevicePolicyManager mDevicePolicyManager;
61     private FakeFeatureFactory mFeatureFactory;
62     private EnterprisePrivacySettings mSettings;
63     private Context mContext;
64     private TestActivity mActivity;
65 
66     @Before
setUp()67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69         mContext = ApplicationProvider.getApplicationContext();
70         mFeatureFactory = FakeFeatureFactory.setupForTest();
71         mSettings = new EnterprisePrivacySettings();
72         mSettings.mPrivacySettingsPreference = new PrivacySettingsEnterprisePreference(mContext);
73 
74         ActivityController<TestActivity> controller = Robolectric.buildActivity(
75                 TestActivity.class).create();
76         mActivity = controller.get();
77 
78         mActivity
79                 .getSupportFragmentManager()
80                 .beginTransaction()
81                 .add(TestActivity.CONTAINER_VIEW_ID, mSettings)
82                 .commit();
83         controller.start();
84     }
85 
86     @Test
verifyConstants()87     public void verifyConstants() {
88         assertThat(mSettings.getMetricsCategory())
89                 .isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS);
90         assertThat(mSettings.getLogTag()).isEqualTo("EnterprisePrivacySettings");
91         assertThat(mSettings.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ENTERPRISE_PRIVACY);
92         assertThat(mSettings.getPreferenceScreenResId())
93                 .isEqualTo(R.xml.enterprise_privacy_settings);
94     }
95 
96     @Test
isPageEnabled_hasDeviceOwner_returnsTrue()97     public void isPageEnabled_hasDeviceOwner_returnsTrue() {
98         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner())
99                 .thenReturn(true);
100 
101         assertThat(EnterprisePrivacySettings.isPageEnabled(mContext))
102                 .isTrue();
103     }
104 
105     @Test
isPageEnabled_noDeviceOwner_returnsFalse()106     public void isPageEnabled_noDeviceOwner_returnsFalse() {
107         when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
108         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner())
109                 .thenReturn(false);
110 
111         assertThat(EnterprisePrivacySettings.isPageEnabled(mContext))
112                 .isFalse();
113     }
114 
115     @Test
getPreferenceControllers_returnsEnterprisePreferenceControllers()116     public void getPreferenceControllers_returnsEnterprisePreferenceControllers() {
117         final List<AbstractPreferenceController> privacyControllers =
118                 mSettings.createPreferenceControllers(mContext);
119 
120         verifyEnterprisePreferenceControllers(privacyControllers);
121     }
122 
123     @Test
124     public void
getSearchIndexProviderPreferenceControllers_returnsEnterpriseSearchIndexPreferenceControllers()125             getSearchIndexProviderPreferenceControllers_returnsEnterpriseSearchIndexPreferenceControllers() {
126         Context context = spy(ApplicationProvider.getApplicationContext());
127         setupPrivacyPreference(context, DEVICE_OWNER_TYPE_DEFAULT);
128 
129         final List<AbstractPreferenceController> controllers =
130             EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
131                 .getPreferenceControllers(context);
132 
133         verifyEnterprisePreferenceControllers(controllers);
134     }
135 
136     @Test
137     public void
getSearchIndexProviderPreferenceControllers_returnsFinancedSearchIndexPreferenceControllers()138             getSearchIndexProviderPreferenceControllers_returnsFinancedSearchIndexPreferenceControllers() {
139         Context context = spy(ApplicationProvider.getApplicationContext());
140         setupPrivacyPreference(context, DEVICE_OWNER_TYPE_FINANCED);
141 
142         final List<AbstractPreferenceController> controllers =
143                 EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
144                         .getPreferenceControllers(context);
145 
146         verifyFinancedPreferenceControllers(controllers);
147     }
148 
149     @Test
getXmlResourcesToIndex_returnsEnterpriseXmlResources()150     public void getXmlResourcesToIndex_returnsEnterpriseXmlResources() {
151         Context context = spy(ApplicationProvider.getApplicationContext());
152         setupPrivacyPreference(context, DEVICE_OWNER_TYPE_DEFAULT);
153 
154         final List<SearchIndexableResource> searchIndexableResources =
155                 EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
156                         .getXmlResourcesToIndex(context, true);
157 
158         verifyEnterpriseSearchIndexableResources(searchIndexableResources);
159     }
160 
161     @Test
getXmlResourcesToIndex_returnsFinancedXmlResources()162     public void getXmlResourcesToIndex_returnsFinancedXmlResources() {
163         Context context = spy(ApplicationProvider.getApplicationContext());
164         setupPrivacyPreference(context, DEVICE_OWNER_TYPE_FINANCED);
165 
166         final List<SearchIndexableResource> searchIndexableResources =
167                 EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
168                         .getXmlResourcesToIndex(context, true);
169 
170         verifyFinancedSearchIndexableResources(searchIndexableResources);
171     }
172 
173     @Test
onCreate_enterprisePrivacyPreference_updatesTitle()174     public void onCreate_enterprisePrivacyPreference_updatesTitle() {
175         mSettings.onCreate(new Bundle());
176 
177         assertThat(mActivity.getTitle())
178                 .isEqualTo(mContext.getText(R.string.enterprise_privacy_settings));
179     }
180 
181     @Test
onCreate_financedPrivacyPreference_doesNotUpdateTitle()182     public void onCreate_financedPrivacyPreference_doesNotUpdateTitle() {
183         mSettings.mPrivacySettingsPreference = new PrivacySettingsFinancedPreference(mContext);
184 
185         mSettings.onCreate(new Bundle());
186 
187         assertThat(mActivity.getTitle())
188                 .isEqualTo(mContext.getText(R.string.financed_privacy_settings));
189     }
190 
setupPrivacyPreference(Context context, int deviceOwnerType)191     private void setupPrivacyPreference(Context context, int deviceOwnerType) {
192         when(context.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
193         when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
194         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser())
195                 .thenReturn(DEVICE_OWNER_COMPONENT);
196         when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT))
197                 .thenReturn(deviceOwnerType);
198     }
199 
200     private static final class TestActivity extends AppCompatActivity {
201 
202         private static final int CONTAINER_VIEW_ID = 1234;
203 
204         @Override
onCreate(Bundle bundle)205         protected void onCreate(Bundle bundle) {
206             super.onCreate(bundle);
207 
208             FrameLayout frameLayout = new FrameLayout(this);
209             frameLayout.setId(CONTAINER_VIEW_ID);
210 
211             // Need to set the Theme.AppCompat theme (or descendant) with this activity, otherwise
212             // a {@link IllegalStateException} is thrown when setting the content view.
213             setTheme(androidx.appcompat.R.style.Theme_AppCompat_Light);
214             setContentView(frameLayout);
215         }
216     }
217 }
218