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
17package com.android.settings.security;
18
19import static com.android.settings.security.EncryptionAndCredential.SEARCH_INDEX_DATA_PROVIDER;
20import static com.google.common.truth.Truth.assertThat;
21import static org.mockito.Mockito.when;
22
23import android.app.admin.DevicePolicyManager;
24import android.content.Context;
25import android.os.UserManager;
26import android.provider.SearchIndexableResource;
27
28import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
29import com.android.settings.search.BaseSearchIndexProvider;
30import com.android.settings.testutils.SettingsRobolectricTestRunner;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37import org.robolectric.shadows.ShadowApplication;
38
39import java.util.ArrayList;
40import java.util.List;
41
42@RunWith(SettingsRobolectricTestRunner.class)
43public class EncryptionAndCredentialTest {
44
45    @Mock
46    private UserManager mUserManager;
47    @Mock
48    private DevicePolicyManager mDevicePolicyManager;
49
50    private Context mContext;
51
52    @Before
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
62    public void getMetricsCategory_shouldReturnEncryptionAndCredential() {
63        EncryptionAndCredential fragment = new EncryptionAndCredential();
64        assertThat(fragment.getMetricsCategory()).isEqualTo(MetricsEvent.ENCRYPTION_AND_CREDENTIAL);
65    }
66
67    @Test
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