Handle Bluetooth is not supported on emulator
- Disable audio swicher while Bluetooth feature is not supported - Error handle before using LocalBluetoothManager in the constructor Bug: 80491267 Test: make RunSettingsRoboTests ROBOTEST_FILTER="AudioOutputSwitchPreferenceControllerTest" -j42 Change-Id: I971f31cd08dd0a2778548f6d1d675f279d92ef8e
This commit is contained in:
@@ -29,6 +29,7 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.media.AudioDeviceCallback;
|
import android.media.AudioDeviceCallback;
|
||||||
import android.media.AudioDeviceInfo;
|
import android.media.AudioDeviceInfo;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
@@ -113,6 +114,10 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
|
|||||||
Log.w(TAG, "Error getting LocalBluetoothManager.", e);
|
Log.w(TAG, "Error getting LocalBluetoothManager.", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (mLocalBluetoothManager == null) {
|
||||||
|
Log.e(TAG, "Bluetooth is not supported on this device");
|
||||||
|
return;
|
||||||
|
}
|
||||||
mLocalBluetoothManager.setForegroundActivity(mContext);
|
mLocalBluetoothManager.setForegroundActivity(mContext);
|
||||||
mProfileManager = mLocalBluetoothManager.getProfileManager();
|
mProfileManager = mLocalBluetoothManager.getProfileManager();
|
||||||
}
|
}
|
||||||
@@ -123,7 +128,8 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public final int getAvailabilityStatus() {
|
public final int getAvailabilityStatus() {
|
||||||
return FeatureFlagUtils.isEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS)
|
return FeatureFlagUtils.isEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS) &&
|
||||||
|
mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
|
||||||
? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
|
? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -42,6 +42,7 @@ import static org.robolectric.Shadows.shadowOf;
|
|||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothManager;
|
import android.bluetooth.BluetoothManager;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
@@ -72,7 +73,9 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.shadows.ShadowBluetoothDevice;
|
import org.robolectric.shadows.ShadowBluetoothDevice;
|
||||||
|
import org.robolectric.shadows.ShadowPackageManager;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -122,6 +125,7 @@ public class AudioOutputSwitchPreferenceControllerTest {
|
|||||||
private List<BluetoothDevice> mProfileConnectedDevices;
|
private List<BluetoothDevice> mProfileConnectedDevices;
|
||||||
private List<BluetoothDevice> mHearingAidActiveDevices;
|
private List<BluetoothDevice> mHearingAidActiveDevices;
|
||||||
private List<BluetoothDevice> mEmptyDevices;
|
private List<BluetoothDevice> mEmptyDevices;
|
||||||
|
private ShadowPackageManager mPackageManager;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -139,6 +143,8 @@ public class AudioOutputSwitchPreferenceControllerTest {
|
|||||||
when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
|
when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
|
||||||
when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
|
when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
|
||||||
when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
|
when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
|
||||||
|
mPackageManager = Shadow.extract(mContext.getPackageManager());
|
||||||
|
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
|
||||||
|
|
||||||
mBluetoothManager = new BluetoothManager(mContext);
|
mBluetoothManager = new BluetoothManager(mContext);
|
||||||
mBluetoothAdapter = mBluetoothManager.getAdapter();
|
mBluetoothAdapter = mBluetoothManager.getAdapter();
|
||||||
@@ -174,13 +180,46 @@ public class AudioOutputSwitchPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAvailabilityStatus_byDefault_isAvailable() {
|
public void constructor_notSupportBluetooth_shouldReturnBeforeUsingLocalBluetoothManager() {
|
||||||
|
ShadowBluetoothUtils.reset();
|
||||||
|
mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
|
||||||
|
|
||||||
|
AudioSwitchPreferenceController controller = new AudioSwitchPreferenceControllerTestable(
|
||||||
|
mContext, TEST_KEY);
|
||||||
|
|
||||||
|
assertThat(mLocalBluetoothManager).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAvailabilityStatus_disableFlagNoBluetoothFeature_returnUnavailable() {
|
||||||
|
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
|
||||||
|
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, false);
|
||||||
|
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAvailabilityStatus_disableFlagWithBluetoothFeature_returnUnavailable() {
|
||||||
|
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
|
||||||
|
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
|
||||||
|
|
||||||
|
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAvailabilityStatus_enableFlagWithBluetoothFeature_returnAvailable() {
|
||||||
|
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, true);
|
||||||
|
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
|
||||||
|
|
||||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAvailabilityStatus_whenNotVisible_isDisable() {
|
public void getAvailabilityStatus_enableFlagNoBluetoothFeature_returnUnavailable() {
|
||||||
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
|
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, true);
|
||||||
|
mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, false);
|
||||||
|
|
||||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user