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 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.anyBoolean;
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.provider.SearchIndexableResource;
30 
31 import androidx.test.core.app.ApplicationProvider;
32 
33 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
34 import com.android.settings.R;
35 import com.android.settings.testutils.FakeFeatureFactory;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 import com.android.settingslib.drawer.CategoryKey;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.robolectric.RobolectricTestRunner;
45 
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class EnterprisePrivacySettingsTest extends BasePrivacySettingsPreferenceTest {
51     private static final ComponentName DEVICE_OWNER_COMPONENT =
52             new ComponentName("com.android.foo", "bar");
53 
54     @Mock
55     private DevicePolicyManager mDevicePolicyManager;
56     @Mock
57     private PrivacySettingsPreference mPrivacySettingsPreference;
58     private FakeFeatureFactory mFeatureFactory;
59     private EnterprisePrivacySettings mSettings;
60 
61     @Override
62     @Before
setUp()63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65         mContext = spy(ApplicationProvider.getApplicationContext());
66         mFeatureFactory = FakeFeatureFactory.setupForTest();
67         mSettings = new EnterprisePrivacySettings();
68         mSettings.mPrivacySettingsPreference = mPrivacySettingsPreference;
69 
70         when(mContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDevicePolicyManager);
71         when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
72         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser())
73                 .thenReturn(DEVICE_OWNER_COMPONENT);
74         when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT))
75                 .thenReturn(DEVICE_OWNER_TYPE_DEFAULT);
76     }
77 
78     @Test
verifyConstants()79     public void verifyConstants() {
80         when(mPrivacySettingsPreference.getPreferenceScreenResId())
81                 .thenReturn(R.xml.enterprise_privacy_settings);
82 
83         assertThat(mSettings.getMetricsCategory())
84                 .isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS);
85         assertThat(mSettings.getLogTag()).isEqualTo("EnterprisePrivacySettings");
86         assertThat(mSettings.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ENTERPRISE_PRIVACY);
87         assertThat(mSettings.getPreferenceScreenResId())
88                 .isEqualTo(R.xml.enterprise_privacy_settings);
89     }
90 
91     @Test
isPageEnabled_hasDeviceOwner_shouldReturnTrue()92     public void isPageEnabled_hasDeviceOwner_shouldReturnTrue() {
93         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner())
94                 .thenReturn(true);
95 
96         assertThat(EnterprisePrivacySettings.isPageEnabled(mContext))
97                 .isTrue();
98     }
99 
100     @Test
isPageEnabled_noDeviceOwner_shouldReturnFalse()101     public void isPageEnabled_noDeviceOwner_shouldReturnFalse() {
102         when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
103         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner())
104                 .thenReturn(false);
105 
106         assertThat(EnterprisePrivacySettings.isPageEnabled(mContext))
107                 .isFalse();
108     }
109 
110     @Test
getPreferenceControllers()111     public void getPreferenceControllers() {
112         final List<AbstractPreferenceController> controllers = new ArrayList<>();
113         controllers.add(new NetworkLogsPreferenceController(mContext));
114         when(mPrivacySettingsPreference.createPreferenceControllers(anyBoolean()))
115                 .thenReturn(controllers);
116 
117         final List<AbstractPreferenceController> privacyControllers =
118                 mSettings.createPreferenceControllers(mContext);
119 
120         assertThat(privacyControllers).isNotNull();
121         assertThat(privacyControllers.size()).isEqualTo(1);
122         assertThat(controllers.get(0)).isInstanceOf(NetworkLogsPreferenceController.class);
123     }
124 
125     @Test
126     public void
getSearchIndexProviderPreferenceControllers_returnsEnterpriseSearchIndexPreferenceControllers()127             getSearchIndexProviderPreferenceControllers_returnsEnterpriseSearchIndexPreferenceControllers() {
128         final List<AbstractPreferenceController> controllers =
129             EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
130                 .getPreferenceControllers(mContext);
131 
132         verifyEnterprisePreferenceControllers(controllers);
133     }
134 
135     @Test
getXmlResourcesToIndex_returnsEnterpriseXmlResources()136     public void getXmlResourcesToIndex_returnsEnterpriseXmlResources() {
137         final List<SearchIndexableResource> searchIndexableResources =
138                 EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER
139                         .getXmlResourcesToIndex(mContext, true);
140 
141         verifyEnterpriseSearchIndexableResources(searchIndexableResources);
142     }
143 }
144