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 */ 16 package com.android.settings.connecteddevice; 17 18 import static com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment.KEY_AVAILABLE_DEVICES; 19 import static com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment.KEY_CONNECTED_DEVICES; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 26 import android.content.Context; 27 import android.content.pm.PackageManager; 28 import android.provider.SearchIndexableResource; 29 30 import com.android.settings.R; 31 import com.android.settings.core.BasePreferenceController; 32 import com.android.settings.core.PreferenceControllerListHelper; 33 import com.android.settings.slices.SlicePreferenceController; 34 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 35 import com.android.settings.testutils.shadow.ShadowConnectivityManager; 36 import com.android.settings.testutils.shadow.ShadowUserManager; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.annotation.Config; 46 47 import java.util.List; 48 49 @RunWith(RobolectricTestRunner.class) 50 @Config(shadows = {ShadowUserManager.class, 51 ShadowConnectivityManager.class, ShadowBluetoothAdapter.class}) 52 public class ConnectedDeviceDashboardFragmentTest { 53 private static final String KEY_NEARBY_DEVICES = "bt_nearby_slice"; 54 55 @Mock 56 private PackageManager mPackageManager; 57 private Context mContext; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 63 mContext = spy(RuntimeEnvironment.application); 64 doReturn(mPackageManager).when(mContext).getPackageManager(); 65 doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); 66 } 67 68 @Test searchIndexProvider_shouldIndexResource()69 public void searchIndexProvider_shouldIndexResource() { 70 final List<SearchIndexableResource> indexRes = 71 ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER 72 .getXmlResourcesToIndex(mContext, true /* enabled */); 73 74 assertThat(indexRes).isNotNull(); 75 assertThat(indexRes.get(0).xmlResId).isEqualTo(R.xml.connected_devices); 76 } 77 78 @Test nonIndexableKeys_existInXmlLayout()79 public void nonIndexableKeys_existInXmlLayout() { 80 final List<String> niks = ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER 81 .getNonIndexableKeys(mContext); 82 83 assertThat(niks).containsExactly(KEY_CONNECTED_DEVICES, KEY_AVAILABLE_DEVICES, 84 KEY_NEARBY_DEVICES); 85 } 86 87 @Test getPreferenceControllers_containSlicePrefController()88 public void getPreferenceControllers_containSlicePrefController() { 89 final List<BasePreferenceController> controllers = 90 PreferenceControllerListHelper.getPreferenceControllersFromXml(mContext, 91 R.xml.connected_devices); 92 93 assertThat(controllers 94 .stream() 95 .filter(controller -> controller instanceof SlicePreferenceController) 96 .count()) 97 .isEqualTo(1); 98 } 99 } 100