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:
@@ -18,24 +18,38 @@ package com.android.settings.accessibility;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
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.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.bluetooth.BluetoothPairingDetail;
|
||||
import com.android.settings.bluetooth.HearingAidPairingDialogFragment;
|
||||
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;
|
||||
@@ -47,39 +61,57 @@ 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 HearingAidPairingDialogFragment}. */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = ShadowAlertDialogCompat.class)
|
||||
@Config(shadows = {ShadowAlertDialogCompat.class, ShadowBluetoothAdapter.class,
|
||||
ShadowBluetoothUtils.class})
|
||||
public class HearingAidPairingDialogFragmentTest {
|
||||
|
||||
@Rule
|
||||
public final MockitoRule mockito = MockitoJUnit.rule();
|
||||
|
||||
private static final String KEY_CACHED_DEVICE_SIDE = "cached_device_side";
|
||||
private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
|
||||
|
||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
||||
@Mock
|
||||
private CachedBluetoothDevice mCachedBluetoothDevice;
|
||||
@Mock
|
||||
private CachedBluetoothDevice mCachedSubBluetoothDevice;
|
||||
@Mock
|
||||
private LocalBluetoothManager mLocalBluetoothManager;
|
||||
@Mock
|
||||
private CachedBluetoothDeviceManager mCachedDeviceManager;
|
||||
private BluetoothAdapter mBluetoothAdapter;
|
||||
private FragmentActivity mActivity;
|
||||
private HearingAidPairingDialogFragment mFragment;
|
||||
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||
private BluetoothDevice mBluetoothDevice;
|
||||
private FragmentManager mFragmentManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mFragment = spy(HearingAidPairingDialogFragment.newInstance(mCachedBluetoothDevice));
|
||||
setupEnvironment();
|
||||
mFragment = spy(HearingAidPairingDialogFragment.newInstance(TEST_DEVICE_ADDRESS));
|
||||
mActivity = Robolectric.setupActivity(FragmentActivity.class);
|
||||
mFragmentManager = mActivity.getSupportFragmentManager();
|
||||
when(mFragment.getActivity()).thenReturn(mActivity);
|
||||
doReturn(mFragmentManager).when(mFragment).getParentFragmentManager();
|
||||
mFragment.onAttach(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newInstance_deviceSideRight_argumentSideRight() {
|
||||
when(mCachedBluetoothDevice.getDeviceSide()).thenReturn(
|
||||
HearingAidProfile.DeviceSide.SIDE_RIGHT);
|
||||
final AlertDialog dialog = (AlertDialog) mFragment.onCreateDialog(Bundle.EMPTY);
|
||||
dialog.show();
|
||||
|
||||
mFragment = HearingAidPairingDialogFragment.newInstance(mCachedBluetoothDevice);
|
||||
|
||||
final Bundle bundle = mFragment.getArguments();
|
||||
assertThat(bundle.getInt(KEY_CACHED_DEVICE_SIDE)).isEqualTo(
|
||||
HearingAidProfile.DeviceSide.SIDE_RIGHT);
|
||||
final String pairLeftString = mContext.getText(
|
||||
R.string.bluetooth_pair_other_ear_dialog_left_ear_positive_button).toString();
|
||||
assertThat(dialog.getButton(
|
||||
DialogInterface.BUTTON_POSITIVE).getText().toString()).isEqualTo(pairLeftString);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,4 +141,26 @@ public class HearingAidPairingDialogFragmentTest {
|
||||
assertThat(mFragment.getMetricsCategory()).isEqualTo(
|
||||
SettingsEnums.DIALOG_ACCESSIBILITY_HEARING_AID_PAIR_ANOTHER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceAttributesChanged_subHearingAidDeviceConnected_dialogDismiss() {
|
||||
when(mCachedSubBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true);
|
||||
when(mCachedBluetoothDevice.getSubDevice()).thenReturn(mCachedSubBluetoothDevice);
|
||||
|
||||
mFragment.onDeviceAttributesChanged();
|
||||
|
||||
verify(mFragment).dismiss();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user