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