Fix after dis/reconnect HA, DUT will request to re-pair one side of HA

Root Cause: Another side of HA jumps to connecting state after pair-other-ear dialog appears.

Solution: Listen to onDeviceAttributesChanged() callback to know when
the sub device gets connected, then to dismiss dialog.

Bug: 236782832
Test: make RunSettingsRoboTests ROBOTEST_FILTER=HearingAidUtilsTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=HearingAidPairingDialogFragmentTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=AvailableMediaDeviceGroupControllerTest
Change-Id: I8dafefbb05e7cf1cadf37a7acbb2c5d66f2d0a78
This commit is contained in:
jasonwshsu
2022-08-31 01:39:20 +08:00
parent 6c5b859da2
commit 88f36dccc7
6 changed files with 162 additions and 23 deletions

View File

@@ -20,13 +20,24 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.bluetooth.Utils;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import org.junit.Before;
import org.junit.Rule;
@@ -38,27 +49,40 @@ import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
/** Tests for {@link HearingAidUtils}. */
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowAlertDialogCompat.class)
@Config(shadows = {ShadowAlertDialogCompat.class, ShadowBluetoothAdapter.class,
ShadowBluetoothUtils.class})
public class HearingAidUtilsTest {
@Rule
public final MockitoRule mockito = MockitoJUnit.rule();
private final Context mContext = ApplicationProvider.getApplicationContext();
private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
@Mock
private CachedBluetoothDevice mCachedBluetoothDevice;
@Mock
private CachedBluetoothDevice mSubCachedBluetoothDevice;
@Mock
private LocalBluetoothManager mLocalBluetoothManager;
@Mock
private CachedBluetoothDeviceManager mCachedDeviceManager;
private BluetoothDevice mBluetoothDevice;
private BluetoothAdapter mBluetoothAdapter;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private FragmentManager mFragmentManager;
@Before
public void setUp() {
setupEnvironment();
final FragmentActivity mActivity = Robolectric.setupActivity(FragmentActivity.class);
mFragmentManager = mActivity.getSupportFragmentManager();
ShadowAlertDialogCompat.reset();
when(mCachedBluetoothDevice.getAddress()).thenReturn(TEST_DEVICE_ADDRESS);
}
@Test
@@ -123,4 +147,16 @@ public class HearingAidUtilsTest {
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue();
}
private void setupEnvironment() {
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
mShadowBluetoothAdapter = Shadow.extract(mBluetoothAdapter);
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS);
mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
when(mCachedBluetoothDevice.getAddress()).thenReturn(TEST_DEVICE_ADDRESS);
}
}