1/*
2 * Copyright (C) 2018 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.settings.connecteddevice;
17
18import static com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment
19        .KEY_AVAILABLE_DEVICES;
20import static com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment
21        .KEY_CONNECTED_DEVICES;
22
23import static com.google.common.truth.Truth.assertThat;
24
25import static org.mockito.Mockito.doReturn;
26import static org.mockito.Mockito.spy;
27
28import android.content.Context;
29import android.content.pm.PackageManager;
30import android.provider.SearchIndexableResource;
31
32import com.android.settings.R;
33import com.android.settings.testutils.SettingsRobolectricTestRunner;
34import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
35import com.android.settings.testutils.shadow.ShadowBluetoothPan;
36import com.android.settings.testutils.shadow.ShadowConnectivityManager;
37import com.android.settings.testutils.shadow.ShadowUserManager;
38
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.Mock;
43import org.mockito.MockitoAnnotations;
44import org.robolectric.RuntimeEnvironment;
45import org.robolectric.annotation.Config;
46
47import java.util.List;
48
49@RunWith(SettingsRobolectricTestRunner.class)
50@Config(shadows = {ShadowBluetoothPan.class, ShadowUserManager.class,
51        ShadowConnectivityManager.class, ShadowBluetoothAdapter.class})
52public class ConnectedDeviceDashboardFragmentTest {
53    @Mock
54    private PackageManager mPackageManager;
55    private Context mContext;
56
57    @Before
58    public void setUp() {
59        MockitoAnnotations.initMocks(this);
60
61        mContext = spy(RuntimeEnvironment.application);
62        doReturn(mPackageManager).when(mContext).getPackageManager();
63        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
64    }
65
66    @Test
67    public void testSearchIndexProvider_shouldIndexResource() {
68        final List<SearchIndexableResource> indexRes =
69                ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
70                        .getXmlResourcesToIndex(mContext, true /* enabled */);
71
72        assertThat(indexRes).isNotNull();
73        assertThat(indexRes.get(0).xmlResId).isEqualTo(R.xml.connected_devices);
74    }
75
76    @Test
77    public void testNonIndexableKeys_existInXmlLayout() {
78        final List<String> niks = ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
79                .getNonIndexableKeys(mContext);
80
81        assertThat(niks).containsExactly(KEY_CONNECTED_DEVICES, KEY_AVAILABLE_DEVICES);
82    }
83}
84