Fix test failure of AvailableMediaDeviceGroupControllerTest

Prevent test failing by avoid using ShadowFragment in the test. This
will cause `androidx.fragment.app.SuperNotCalledException` when creating
HearingAidPairingDialogFragment.

Bug: 299597695
Test: atest AvailableMediaDeviceGroupControllerTest
Change-Id: I99b5213cb78aad97fcb7e44e1babcf6c7f11636e
This commit is contained in:
Angela Wang
2023-09-08 11:35:03 +00:00
parent fde9937b04
commit bb1fe04afb
2 changed files with 9 additions and 11 deletions

View File

@@ -134,6 +134,11 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle
AvailableMediaDeviceGroupController.this, fragment.getMetricsCategory()); AvailableMediaDeviceGroupController.this, fragment.getMetricsCategory());
} }
@VisibleForTesting
public void setFragmentManager(FragmentManager fragmentManager) {
mFragmentManager = fragmentManager;
}
@VisibleForTesting @VisibleForTesting
public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) { public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) {
mBluetoothDeviceUpdater = bluetoothDeviceUpdater; mBluetoothDeviceUpdater = bluetoothDeviceUpdater;

View File

@@ -17,9 +17,7 @@ package com.android.settings.connecteddevice;
import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE; import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
@@ -44,7 +42,6 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater; import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
import com.android.settings.bluetooth.Utils; import com.android.settings.bluetooth.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowAudioManager; import com.android.settings.testutils.shadow.ShadowAudioManager;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
@@ -74,15 +71,13 @@ import org.robolectric.annotation.Config;
ShadowAudioManager.class, ShadowAudioManager.class,
ShadowBluetoothAdapter.class, ShadowBluetoothAdapter.class,
ShadowBluetoothUtils.class, ShadowBluetoothUtils.class,
com.android.settings.testutils.shadow.ShadowFragment.class, ShadowAlertDialogCompat.class,
}) })
public class AvailableMediaDeviceGroupControllerTest { public class AvailableMediaDeviceGroupControllerTest {
private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1"; private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
private static final String PREFERENCE_KEY_1 = "pref_key_1"; private static final String PREFERENCE_KEY_1 = "pref_key_1";
@Mock
private DashboardFragment mDashboardFragment;
@Mock @Mock
private AvailableMediaBluetoothDeviceUpdater mAvailableMediaBluetoothDeviceUpdater; private AvailableMediaBluetoothDeviceUpdater mAvailableMediaBluetoothDeviceUpdater;
@Mock @Mock
@@ -119,11 +114,8 @@ public class AvailableMediaDeviceGroupControllerTest {
Robolectric.buildActivity(FragmentActivity.class)).get(); Robolectric.buildActivity(FragmentActivity.class)).get();
when(mPreferenceGroup.getPreferenceManager()).thenReturn(mPreferenceManager); when(mPreferenceGroup.getPreferenceManager()).thenReturn(mPreferenceManager);
doReturn(mContext).when(mDashboardFragment).getContext();
doReturn(mPackageManager).when(mContext).getPackageManager(); doReturn(mPackageManager).when(mContext).getPackageManager();
doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
when(mDashboardFragment.getParentFragmentManager()).thenReturn(
mActivity.getSupportFragmentManager());
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager; ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager;
mLocalBluetoothManager = Utils.getLocalBtManager(mContext); mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
@@ -138,6 +130,8 @@ public class AvailableMediaDeviceGroupControllerTest {
new AvailableMediaDeviceGroupController(mContext)); new AvailableMediaDeviceGroupController(mContext));
mAvailableMediaDeviceGroupController. mAvailableMediaDeviceGroupController.
setBluetoothDeviceUpdater(mAvailableMediaBluetoothDeviceUpdater); setBluetoothDeviceUpdater(mAvailableMediaBluetoothDeviceUpdater);
mAvailableMediaDeviceGroupController.setFragmentManager(
mActivity.getSupportFragmentManager());
mAvailableMediaDeviceGroupController.mPreferenceGroup = mPreferenceGroup; mAvailableMediaDeviceGroupController.mPreferenceGroup = mPreferenceGroup;
} }
@@ -251,18 +245,17 @@ public class AvailableMediaDeviceGroupControllerTest {
} }
@Test @Test
@Config(shadows = ShadowAlertDialogCompat.class)
public void onActiveDeviceChanged_hearingAidProfile_launchHearingAidPairingDialog() { public void onActiveDeviceChanged_hearingAidProfile_launchHearingAidPairingDialog() {
when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true);
when(mCachedBluetoothDevice.getDeviceMode()).thenReturn( when(mCachedBluetoothDevice.getDeviceMode()).thenReturn(
HearingAidInfo.DeviceMode.MODE_BINAURAL); HearingAidInfo.DeviceMode.MODE_BINAURAL);
when(mCachedBluetoothDevice.getDeviceSide()).thenReturn( when(mCachedBluetoothDevice.getDeviceSide()).thenReturn(
HearingAidInfo.DeviceSide.SIDE_LEFT); HearingAidInfo.DeviceSide.SIDE_LEFT);
mAvailableMediaDeviceGroupController.init(mDashboardFragment);
mAvailableMediaDeviceGroupController.onActiveDeviceChanged(mCachedBluetoothDevice, mAvailableMediaDeviceGroupController.onActiveDeviceChanged(mCachedBluetoothDevice,
BluetoothProfile.HEARING_AID); BluetoothProfile.HEARING_AID);
shadowMainLooper().idle(); shadowMainLooper().idle();
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog.isShowing()).isTrue(); assertThat(dialog.isShowing()).isTrue();
} }