1 /* 2 * Copyright (C) 2022 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 17 package com.android.settings.bluetooth; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.when; 22 23 import android.bluetooth.BluetoothDevice; 24 25 import com.android.settings.R; 26 import com.android.settings.applications.SpacePreference; 27 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 28 import com.android.settingslib.bluetooth.HearingAidInfo; 29 import com.android.settingslib.widget.ButtonPreference; 30 31 import org.junit.Rule; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.junit.MockitoJUnit; 36 import org.mockito.junit.MockitoRule; 37 import org.robolectric.RobolectricTestRunner; 38 39 import java.util.HashSet; 40 import java.util.Set; 41 42 /** Tests for {@link BluetoothDetailsPairOtherController}. */ 43 @RunWith(RobolectricTestRunner.class) 44 public class BluetoothDetailsPairOtherControllerTest extends BluetoothDetailsControllerTestBase { 45 @Rule 46 public final MockitoRule mockito = MockitoJUnit.rule(); 47 48 @Mock 49 private CachedBluetoothDevice mSubCachedDevice; 50 private BluetoothDetailsPairOtherController mController; 51 private ButtonPreference mPreference; 52 private SpacePreference mSpacePreference; 53 54 @Override setUp()55 public void setUp() { 56 super.setUp(); 57 58 mController = new BluetoothDetailsPairOtherController(mContext, mFragment, mCachedDevice, 59 mLifecycle); 60 mPreference = new ButtonPreference(mContext); 61 mSpacePreference = new SpacePreference(mContext, null); 62 mPreference.setKey(mController.getPreferenceKey()); 63 mSpacePreference.setKey(BluetoothDetailsPairOtherController.KEY_SPACE); 64 mScreen.addPreference(mPreference); 65 mScreen.addPreference(mSpacePreference); 66 } 67 68 /** Test the pair other side button title during initialization. */ 69 @Test init_deviceIsLeftSide_showPairRightSideTitle()70 public void init_deviceIsLeftSide_showPairRightSideTitle() { 71 when(mCachedDevice.getDeviceSide()).thenReturn(HearingAidInfo.DeviceSide.SIDE_LEFT); 72 73 mController.init(mScreen); 74 75 assertThat(mPreference.getTitle().toString()).isEqualTo( 76 mContext.getString(R.string.bluetooth_pair_right_ear_button)); 77 } 78 79 /** Test the pair other side button title during initialization. */ 80 @Test init_deviceIsRightSide_showPairLeftSideTitle()81 public void init_deviceIsRightSide_showPairLeftSideTitle() { 82 when(mCachedDevice.getDeviceSide()).thenReturn(HearingAidInfo.DeviceSide.SIDE_RIGHT); 83 84 mController.init(mScreen); 85 86 assertThat(mPreference.getTitle().toString()).isEqualTo( 87 mContext.getString(R.string.bluetooth_pair_left_ear_button)); 88 } 89 90 /** Test the pair other side button visibility during initialization. */ 91 @Test init_deviceIsNotConnectedHearingAid_preferenceIsNotVisible()92 public void init_deviceIsNotConnectedHearingAid_preferenceIsNotVisible() { 93 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(false); 94 95 mController.init(mScreen); 96 97 assertThat(mPreference.isVisible()).isFalse(); 98 assertThat(mSpacePreference.isVisible()).isFalse(); 99 } 100 101 /** 102 * Test if the controller is available. 103 * Conditions: 104 * 1. The device is not a connected hearing aid 105 * Expected result: 106 * The controller is not available. No need to show pair other side hint for 107 * non-hearing aid device or not connected device. 108 */ 109 @Test isAvailable_deviceIsNotConnectedHearingAid_notAvailable()110 public void isAvailable_deviceIsNotConnectedHearingAid_notAvailable() { 111 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(false); 112 113 assertThat(mController.isAvailable()).isFalse(); 114 } 115 116 /** 117 * Test if the controller is available. 118 * Conditions: 119 * 1. Monaural hearing aid 120 * Expected result: 121 * The controller is not available. No need to show pair other side hint for 122 * monaural device. 123 */ 124 @Test isAvailable_deviceIsConnectedHearingAid_isMonaural_notAvailable()125 public void isAvailable_deviceIsConnectedHearingAid_isMonaural_notAvailable() { 126 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); 127 when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidInfo.DeviceMode.MODE_MONAURAL); 128 129 assertThat(mController.isAvailable()).isFalse(); 130 } 131 132 /** 133 * Test if the controller is available. 134 * Conditions: 135 * 1. Binaural hearing aids 136 * 2. Sub device is added 137 * 3. Sub device is bonded 138 * Expected result: 139 * The controller is not available. Both sides are already paired. 140 */ 141 @Test isAvailable_deviceIsConnectedHearingAid_subDeviceIsBonded_notAvailable()142 public void isAvailable_deviceIsConnectedHearingAid_subDeviceIsBonded_notAvailable() { 143 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); 144 when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidInfo.DeviceMode.MODE_BINAURAL); 145 when(mSubCachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 146 when(mCachedDevice.getSubDevice()).thenReturn(mSubCachedDevice); 147 148 assertThat(mController.isAvailable()).isFalse(); 149 } 150 151 /** 152 * Test if the controller is available. 153 * Conditions: 154 * 1. Binaural hearing aids 155 * 2. Sub device is added 156 * 3. Sub device is not bonded 157 * Expected result: 158 * The controller is available. Need to show the hint to pair the other side. 159 */ 160 @Test isAvailable_deviceIsConnectedHearingAid_subDeviceIsNotBonded_available()161 public void isAvailable_deviceIsConnectedHearingAid_subDeviceIsNotBonded_available() { 162 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); 163 when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidInfo.DeviceMode.MODE_BINAURAL); 164 when(mSubCachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE); 165 when(mCachedDevice.getSubDevice()).thenReturn(mSubCachedDevice); 166 167 assertThat(mController.isAvailable()).isTrue(); 168 } 169 170 /** 171 * Test if the controller is available. 172 * Conditions: 173 * 1. Binaural hearing aids 174 * 2. Member device is added 175 * 3. Member device is bonded 176 * Expected result: 177 * The controller is not available. Both sides are already paired. 178 */ 179 @Test isAvailable_deviceIsConnectedHearingAid_memberDeviceIsBonded_notAvailable()180 public void isAvailable_deviceIsConnectedHearingAid_memberDeviceIsBonded_notAvailable() { 181 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); 182 when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidInfo.DeviceMode.MODE_BINAURAL); 183 when(mSubCachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 184 when(mCachedDevice.getMemberDevice()).thenReturn(Set.of(mSubCachedDevice)); 185 186 assertThat(mController.isAvailable()).isFalse(); 187 } 188 189 /** 190 * Test if the controller is available. 191 * Conditions: 192 * 1. Binaural hearing aids 193 * 2. Member device is added 194 * 3. Member device is not bonded 195 * Expected result: 196 * The controller is available. Need to show the hint to pair the other side. 197 */ 198 @Test isAvailable_deviceIsConnectedHearingAid_memberDeviceIsNotBonded_available()199 public void isAvailable_deviceIsConnectedHearingAid_memberDeviceIsNotBonded_available() { 200 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); 201 when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidInfo.DeviceMode.MODE_BINAURAL); 202 when(mSubCachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE); 203 when(mCachedDevice.getMemberDevice()).thenReturn(Set.of(mSubCachedDevice)); 204 205 assertThat(mController.isAvailable()).isTrue(); 206 } 207 208 /** 209 * Test if the controller is available. 210 * Conditions: 211 * 1. Binaural hearing aids 212 * 2. No sub device is added 213 * 2. No member device is added 214 * Expected result: 215 * The controller is available. Need to show the hint to pair the other side. 216 */ 217 @Test isAvailable_deviceIsConnectedHearingAid_otherDeviceIsNotExist_available()218 public void isAvailable_deviceIsConnectedHearingAid_otherDeviceIsNotExist_available() { 219 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); 220 when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidInfo.DeviceMode.MODE_BINAURAL); 221 when(mCachedDevice.getSubDevice()).thenReturn(null); 222 when(mCachedDevice.getMemberDevice()).thenReturn(new HashSet<>()); 223 224 assertThat(mController.isAvailable()).isTrue(); 225 } 226 227 /** Test the pair other side button title after refreshing. */ 228 @Test refresh_deviceIsRightSide_showPairLeftSideTitle()229 public void refresh_deviceIsRightSide_showPairLeftSideTitle() { 230 when(mCachedDevice.getDeviceSide()).thenReturn(HearingAidInfo.DeviceSide.SIDE_RIGHT); 231 mController.init(mScreen); 232 233 mController.refresh(); 234 235 assertThat(mPreference.getTitle().toString()).isEqualTo( 236 mContext.getString(R.string.bluetooth_pair_left_ear_button)); 237 } 238 239 /** Test the pair other side button visibility after refreshing. */ 240 @Test refresh_deviceIsNotConnectedHearingAid_preferenceIsNotVisible()241 public void refresh_deviceIsNotConnectedHearingAid_preferenceIsNotVisible() { 242 when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(false); 243 mController.init(mScreen); 244 245 mController.refresh(); 246 247 assertThat(mPreference.isVisible()).isFalse(); 248 assertThat(mSpacePreference.isVisible()).isFalse(); 249 } 250 } 251