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.ArgumentMatchers.any; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.pm.PackageManager; 30 import android.platform.test.flag.junit.CheckFlagsRule; 31 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 32 import android.platform.test.flag.junit.SetFlagsRule; 33 import android.provider.SearchIndexableResource; 34 35 import com.android.settings.R; 36 import com.android.settings.connecteddevice.fastpair.FastPairDeviceUpdater; 37 import com.android.settings.core.BasePreferenceController; 38 import com.android.settings.core.PreferenceControllerListHelper; 39 import com.android.settings.slices.SlicePreferenceController; 40 import com.android.settings.testutils.FakeFeatureFactory; 41 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 42 import com.android.settings.testutils.shadow.ShadowConnectivityManager; 43 import com.android.settings.testutils.shadow.ShadowUserManager; 44 45 import org.junit.Before; 46 import org.junit.Rule; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.mockito.Mock; 50 import org.mockito.MockitoAnnotations; 51 import org.robolectric.RobolectricTestRunner; 52 import org.robolectric.RuntimeEnvironment; 53 import org.robolectric.annotation.Config; 54 55 import java.util.List; 56 57 @RunWith(RobolectricTestRunner.class) 58 @Config( 59 shadows = { 60 ShadowUserManager.class, 61 ShadowConnectivityManager.class, 62 ShadowBluetoothAdapter.class 63 }) 64 public class ConnectedDeviceDashboardFragmentTest { 65 @Rule 66 public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); 67 68 @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 69 70 private static final String KEY_NEARBY_DEVICES = "bt_nearby_slice"; 71 private static final String KEY_DISCOVERABLE_FOOTER = "discoverable_footer"; 72 private static final String KEY_SAVED_DEVICE_SEE_ALL = "previously_connected_devices_see_all"; 73 private static final String KEY_FAST_PAIR_DEVICE_SEE_ALL = "fast_pair_devices_see_all"; 74 private static final String KEY_AUDIO_SHARING_DEVICES = "audio_sharing_device_list"; 75 private static final String KEY_AUDIO_SHARING_SETTINGS = 76 "connected_device_audio_sharing_settings"; 77 private static final String KEY_ADD_BT_DEVICES = "add_bt_devices"; 78 private static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; 79 private static final String SYSTEMUI_PACKAGE_NAME = "com.android.systemui"; 80 private static final String SLICE_ACTION = "com.android.settings.SEARCH_RESULT_TRAMPOLINE"; 81 private static final String TEST_APP_NAME = "com.testapp.settings"; 82 private static final String TEST_ACTION = "com.testapp.settings.ACTION_START"; 83 84 @Mock private PackageManager mPackageManager; 85 @Mock private FastPairDeviceUpdater mFastPairDeviceUpdater; 86 private Context mContext; 87 private ConnectedDeviceDashboardFragment mFragment; 88 private FakeFeatureFactory mFeatureFactory; 89 90 @Before setUp()91 public void setUp() { 92 MockitoAnnotations.initMocks(this); 93 94 mContext = spy(RuntimeEnvironment.application); 95 mFragment = new ConnectedDeviceDashboardFragment(); 96 mSetFlagsRule.enableFlags(com.android.settingslib.flags.Flags.FLAG_ENABLE_LE_AUDIO_SHARING); 97 mFeatureFactory = FakeFeatureFactory.setupForTest(); 98 when(mFeatureFactory 99 .getFastPairFeatureProvider() 100 .getFastPairDeviceUpdater( 101 any(Context.class), any(DevicePreferenceCallback.class))) 102 .thenReturn(mFastPairDeviceUpdater); 103 doReturn(mPackageManager).when(mContext).getPackageManager(); 104 doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); 105 } 106 107 @Test searchIndexProvider_shouldIndexResource()108 public void searchIndexProvider_shouldIndexResource() { 109 final List<SearchIndexableResource> indexRes = 110 ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex( 111 mContext, true /* enabled */); 112 113 assertThat(indexRes).isNotNull(); 114 assertThat(indexRes.get(0).xmlResId).isEqualTo(R.xml.connected_devices); 115 } 116 117 @Test nonIndexableKeys_existInXmlLayout()118 public void nonIndexableKeys_existInXmlLayout() { 119 final List<String> niks = 120 ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 121 mContext); 122 123 assertThat(niks) 124 .containsExactly( 125 KEY_CONNECTED_DEVICES, 126 KEY_AVAILABLE_DEVICES, 127 KEY_NEARBY_DEVICES, 128 KEY_DISCOVERABLE_FOOTER, 129 KEY_SAVED_DEVICE_SEE_ALL, 130 KEY_FAST_PAIR_DEVICE_SEE_ALL, 131 KEY_AUDIO_SHARING_DEVICES, 132 KEY_AUDIO_SHARING_SETTINGS); 133 } 134 135 @Test isAlwaysDiscoverable_callingAppIsNotFromSystemApp_returnsFalse()136 public void isAlwaysDiscoverable_callingAppIsNotFromSystemApp_returnsFalse() { 137 assertThat(mFragment.isAlwaysDiscoverable(TEST_APP_NAME, TEST_ACTION)).isFalse(); 138 } 139 140 @Test isAlwaysDiscoverable_callingAppIsFromSettings_returnsTrue()141 public void isAlwaysDiscoverable_callingAppIsFromSettings_returnsTrue() { 142 assertThat(mFragment.isAlwaysDiscoverable(SETTINGS_PACKAGE_NAME, TEST_ACTION)).isTrue(); 143 } 144 145 @Test isAlwaysDiscoverable_callingAppIsFromSystemUI_returnsTrue()146 public void isAlwaysDiscoverable_callingAppIsFromSystemUI_returnsTrue() { 147 assertThat(mFragment.isAlwaysDiscoverable(SYSTEMUI_PACKAGE_NAME, TEST_ACTION)).isTrue(); 148 } 149 150 @Test isAlwaysDiscoverable_actionIsFromSlice_returnsFalse()151 public void isAlwaysDiscoverable_actionIsFromSlice_returnsFalse() { 152 assertThat(mFragment.isAlwaysDiscoverable(SYSTEMUI_PACKAGE_NAME, SLICE_ACTION)).isFalse(); 153 } 154 155 @Test getPreferenceControllers_containSlicePrefController()156 public void getPreferenceControllers_containSlicePrefController() { 157 final List<BasePreferenceController> controllers = 158 PreferenceControllerListHelper.getPreferenceControllersFromXml( 159 mContext, R.xml.connected_devices); 160 161 assertThat( 162 controllers.stream() 163 .filter( 164 controller -> 165 controller instanceof SlicePreferenceController) 166 .count()) 167 .isEqualTo(1); 168 } 169 } 170