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