1 /* 2 * Copyright (C) 2023 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.security; 18 19 import static android.app.settings.SettingsEnums.CONTENT_PROTECTION_PREFERENCE; 20 21 import static com.android.internal.R.string.config_defaultContentProtectionService; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.Mockito.doReturn; 26 import static org.mockito.Mockito.spy; 27 28 import android.content.ComponentName; 29 import android.content.Context; 30 import android.provider.DeviceConfig; 31 import android.provider.SearchIndexableResource; 32 import android.view.contentcapture.ContentCaptureManager; 33 34 import androidx.preference.PreferenceScreen; 35 36 import com.android.settings.R; 37 import com.android.settings.testutils.XmlTestUtils; 38 import com.android.settings.testutils.shadow.ShadowDashboardFragment; 39 import com.android.settings.testutils.shadow.ShadowDeviceConfig; 40 import com.android.settings.testutils.shadow.ShadowUtils; 41 42 import org.junit.After; 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.RuntimeEnvironment; 49 import org.robolectric.annotation.Config; 50 51 import java.util.List; 52 53 @RunWith(RobolectricTestRunner.class) 54 @Config( 55 shadows = { 56 ShadowDashboardFragment.class, 57 ShadowUtils.class, 58 ShadowDeviceConfig.class, 59 }) 60 public class ContentProtectionPreferenceFragmentTest { 61 private static final String PACKAGE_NAME = "com.test.package"; 62 63 private static final ComponentName COMPONENT_NAME = 64 new ComponentName(PACKAGE_NAME, "TestClass"); 65 66 private String mConfigDefaultContentProtectionService = COMPONENT_NAME.flattenToString(); 67 private ContentProtectionPreferenceFragment mFragment; 68 private Context mContext; 69 private PreferenceScreen mScreen; 70 71 @Before setUp()72 public void setUp() throws Exception { 73 MockitoAnnotations.initMocks(this); 74 75 mContext = spy(RuntimeEnvironment.application); 76 mFragment = spy(new ContentProtectionPreferenceFragment()); 77 mScreen = spy(new PreferenceScreen(mContext, /* attrs= */ null)); 78 79 doReturn(mContext).when(mFragment).getContext(); 80 doReturn(mScreen).when(mFragment).getPreferenceScreen(); 81 } 82 83 @After tearDown()84 public void tearDown() { 85 ShadowDeviceConfig.reset(); 86 } 87 88 @Test getMetricsCategory()89 public void getMetricsCategory() { 90 assertThat(mFragment.getMetricsCategory()).isEqualTo(CONTENT_PROTECTION_PREFERENCE); 91 } 92 93 @Test getPreferenceScreenResId()94 public void getPreferenceScreenResId() { 95 assertThat(mFragment.getPreferenceScreenResId()) 96 .isEqualTo(R.layout.content_protection_preference_fragment); 97 } 98 99 @Test getNonIndexableKeys_uiEnabled_existInXmlLayout()100 public void getNonIndexableKeys_uiEnabled_existInXmlLayout() { 101 DeviceConfig.setProperty( 102 DeviceConfig.NAMESPACE_CONTENT_CAPTURE, 103 ContentCaptureManager.DEVICE_CONFIG_PROPERTY_ENABLE_CONTENT_PROTECTION_RECEIVER, 104 "true", 105 /* makeDefault= */ false); 106 doReturn(mConfigDefaultContentProtectionService) 107 .when(mContext) 108 .getString(config_defaultContentProtectionService); 109 110 final List<String> nonIndexableKeys = 111 ContentProtectionPreferenceFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 112 mContext); 113 final List<String> allKeys = 114 XmlTestUtils.getKeysFromPreferenceXml( 115 mContext, R.layout.content_protection_preference_fragment); 116 final List<String> nonIndexableKeysExpected = 117 List.of( 118 "content_protection_preference_top_intro", 119 "content_protection_preference_subpage_illustration", 120 "content_protection_preference_user_consent_work_profile_switch"); 121 122 assertThat(allKeys).containsAtLeastElementsIn(nonIndexableKeys); 123 assertThat(nonIndexableKeys).isEqualTo(nonIndexableKeysExpected); 124 } 125 126 @Test getNonIndexableKeys_uiDisabled_notExisted()127 public void getNonIndexableKeys_uiDisabled_notExisted() { 128 DeviceConfig.setProperty( 129 DeviceConfig.NAMESPACE_CONTENT_CAPTURE, 130 ContentCaptureManager.DEVICE_CONFIG_PROPERTY_ENABLE_CONTENT_PROTECTION_RECEIVER, 131 "false", 132 /* makeDefault= */ false); 133 134 final List<String> nonIndexableKeys = 135 ContentProtectionPreferenceFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 136 mContext); 137 final List<String> allKeys = 138 XmlTestUtils.getKeysFromPreferenceXml( 139 mContext, R.layout.content_protection_preference_fragment); 140 141 assertThat(nonIndexableKeys).isEqualTo(allKeys); 142 } 143 144 @Test searchIndexProvider_shouldIndexResource()145 public void searchIndexProvider_shouldIndexResource() { 146 final List<SearchIndexableResource> indexRes = 147 ContentProtectionPreferenceFragment.SEARCH_INDEX_DATA_PROVIDER 148 .getXmlResourcesToIndex(mContext, /* enabled= */ true); 149 150 assertThat(indexRes).isNotNull(); 151 assertThat(indexRes).isNotEmpty(); 152 assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId()); 153 } 154 155 @Test isPageSearchEnabled_uiDisabled_returnsFalse()156 public void isPageSearchEnabled_uiDisabled_returnsFalse() { 157 boolean isSearchEnabled = 158 mFragment.SEARCH_INDEX_DATA_PROVIDER.isPageSearchEnabled(mContext); 159 160 assertThat(isSearchEnabled).isFalse(); 161 } 162 163 @Test isPageSearchEnabled_uiEnabled_returnsTrue()164 public void isPageSearchEnabled_uiEnabled_returnsTrue() { 165 DeviceConfig.setProperty( 166 DeviceConfig.NAMESPACE_CONTENT_CAPTURE, 167 ContentCaptureManager.DEVICE_CONFIG_PROPERTY_ENABLE_CONTENT_PROTECTION_RECEIVER, 168 "true", 169 /* makeDefault= */ false); 170 doReturn(mConfigDefaultContentProtectionService) 171 .when(mContext) 172 .getString(config_defaultContentProtectionService); 173 174 boolean isSearchEnabled = 175 mFragment.SEARCH_INDEX_DATA_PROVIDER.isPageSearchEnabled(mContext); 176 177 assertThat(isSearchEnabled).isTrue(); 178 } 179 } 180