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 */
16package com.android.phone;
17
18import static com.google.common.truth.Truth.assertThat;
19
20import static org.mockito.Mockito.when;
21
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.pm.ApplicationInfo;
25import android.content.pm.ProviderInfo;
26import android.content.res.Resources;
27import android.database.Cursor;
28import android.os.UserManager;
29import android.provider.SearchIndexablesContract;
30import android.provider.Settings;
31import android.support.test.runner.AndroidJUnit4;
32import android.telephony.euicc.EuiccManager;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
39
40/** Unit tests for {@link PhoneSearchIndexablesProvider}. */
41@RunWith(AndroidJUnit4.class)
42public final class PhoneSearchIndexablesProviderTest {
43    private PhoneSearchIndexablesTestProvider mProvider;
44    @Mock private ApplicationInfo mApplicationInfo;
45    @Mock private Context mContext;
46    @Mock private Resources mResources;
47    @Mock private UserManager mUserManager;
48    @Mock private EuiccManager mEuiccManager;
49    @Mock private ContentResolver mCr;
50
51    private class PhoneSearchIndexablesTestProvider extends PhoneSearchIndexablesProvider {
52        private boolean mIsEuiccSettingsHidden = false;
53        private boolean mIsEnhanced4gLteHidden = false;
54
55        @Override boolean isEuiccSettingsHidden() {
56            return mIsEuiccSettingsHidden;
57        }
58
59        @Override boolean isEnhanced4gLteHidden() {
60            return mIsEnhanced4gLteHidden;
61        }
62
63        public void setIsEuiccSettingsHidden(boolean isEuiccSettingsHidden) {
64            mIsEuiccSettingsHidden = isEuiccSettingsHidden;
65        }
66
67        public void setIsEnhanced4gLteHidden(boolean isEnhanced4gLteHidden) {
68            mIsEnhanced4gLteHidden = isEnhanced4gLteHidden;
69        }
70    }
71
72    @Before
73    public void setUp() {
74        MockitoAnnotations.initMocks(this);
75        when(mResources.getString(com.android.phone.R.string.carrier_settings_euicc))
76                .thenReturn("");
77        when(mResources.getString(com.android.phone.R.string.keywords_carrier_settings_euicc))
78                .thenReturn("");
79
80        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
81        when(mContext.getSystemService(Context.EUICC_SERVICE)).thenReturn(mEuiccManager);
82        when(mContext.getResources()).thenReturn(mResources);
83        when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
84        when(mContext.getPackageName()).thenReturn("PhoneSearchIndexablesProviderTest");
85        when(mContext.getContentResolver()).thenReturn(mCr);
86        when(mCr.getPackageName()).thenReturn("com.android.phone");
87
88        final ProviderInfo providerInfo = new ProviderInfo();
89        providerInfo.authority = Settings.AUTHORITY;
90        providerInfo.exported = true;
91        providerInfo.grantUriPermissions = true;
92        providerInfo.readPermission = android.Manifest.permission.READ_SEARCH_INDEXABLES;
93        mProvider = new PhoneSearchIndexablesTestProvider();
94        mProvider.attachInfo(mContext, providerInfo);
95    }
96
97    @Test
98    public void testQueryRawData() {
99        when(mUserManager.isAdminUser()).thenReturn(true);
100        when(mEuiccManager.isEnabled()).thenReturn(true);
101        Settings.Global.putInt(mCr, Settings.Global.EUICC_PROVISIONED, 1);
102        Settings.Global.getInt(mCr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
103
104        Cursor cursor = mProvider.queryRawData(SearchIndexablesContract.INDEXABLES_RAW_COLUMNS);
105        assertThat(cursor.getColumnNames()).isEqualTo(
106                SearchIndexablesContract.INDEXABLES_RAW_COLUMNS);
107        assertThat(cursor.getCount()).isEqualTo(1);
108        cursor.moveToNext();
109        assertThat(cursor.getString(SearchIndexablesContract.COLUMN_INDEX_RAW_KEY))
110                .isEqualTo("esim_list_profile");
111    }
112
113    @Test
114    public void testQueryNonIndexableKeys() {
115        mProvider.setIsEnhanced4gLteHidden(false /* isEnhanced4gLteHidden */);
116        mProvider.setIsEuiccSettingsHidden(false /* isEuiccSettingsHiden */);
117        when(mUserManager.isAdminUser()).thenReturn(false);
118        Cursor cursor1 = mProvider.queryNonIndexableKeys(
119                SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
120        assertThat(cursor1.getColumnNames()).isEqualTo(
121                SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
122        assertThat(cursor1.getCount()).isEqualTo(11);
123
124        when(mUserManager.isAdminUser()).thenReturn(true);
125        Cursor cursor2 = mProvider
126                .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
127        assertThat(cursor2.getCount()).isEqualTo(1);
128
129        mProvider.setIsEuiccSettingsHidden(true /* isEuiccSettingsHidden */);
130        Cursor cursor3 = mProvider
131                .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
132        assertThat(cursor3.getCount()).isEqualTo(2);
133
134        mProvider.setIsEnhanced4gLteHidden(true /* isEnhanced4gLteHidden */);
135        Cursor cursor4 = mProvider
136                .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
137        assertThat(cursor4.getCount()).isEqualTo(3);
138    }
139}
140