1 /* 2 * Copyright (C) 2017 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 com.android.settings.security.EncryptionAndCredential.SEARCH_INDEX_DATA_PROVIDER; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Mockito.when; 22 23 import android.app.admin.DevicePolicyManager; 24 import android.content.Context; 25 import android.os.UserManager; 26 import android.provider.SearchIndexableResource; 27 28 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 29 import com.android.settings.search.BaseSearchIndexProvider; 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.shadows.ShadowApplication; 38 39 import java.util.ArrayList; 40 import java.util.List; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 public class EncryptionAndCredentialTest { 44 45 @Mock 46 private UserManager mUserManager; 47 @Mock 48 private DevicePolicyManager mDevicePolicyManager; 49 50 private Context mContext; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 ShadowApplication application = ShadowApplication.getInstance(); 56 application.setSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager); 57 application.setSystemService(Context.USER_SERVICE, mUserManager); 58 mContext = application.getApplicationContext(); 59 } 60 61 @Test getMetricsCategory_shouldReturnEncryptionAndCredential()62 public void getMetricsCategory_shouldReturnEncryptionAndCredential() { 63 EncryptionAndCredential fragment = new EncryptionAndCredential(); 64 assertThat(fragment.getMetricsCategory()).isEqualTo(MetricsEvent.ENCRYPTION_AND_CREDENTIAL); 65 } 66 67 @Test getNonIndexableKeys_pageIsDisabled_shouldReturnAllKeysAsNonIndexable()68 public void getNonIndexableKeys_pageIsDisabled_shouldReturnAllKeysAsNonIndexable() { 69 when(mUserManager.isAdminUser()).thenReturn(false); 70 71 final List<SearchIndexableResource> index = 72 SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(mContext, true /* enabled */); 73 final List<String> expectedKeys = new ArrayList<>(); 74 for (SearchIndexableResource res : index) { 75 expectedKeys.addAll(((BaseSearchIndexProvider) SEARCH_INDEX_DATA_PROVIDER) 76 .getNonIndexableKeysFromXml(mContext, res.xmlResId)); 77 } 78 final List<String> keys = SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext); 79 80 assertThat(keys).containsExactlyElementsIn(expectedKeys); 81 } 82 } 83