[Fix] Unable to show output device indicator after reboot

- To fix the NPE in MediaOutputIndicatorWorker
- To get Bluetooth device status in Slice, not in Worker
- To purify Worker to be an event receiver

Bug: 128945026
Test: make -j42 RunSettingsRoboTests
Change-Id: I9360202870c7a43a79641c5b84210c1bfad4b9a6
This commit is contained in:
timhypeng
2019-03-25 15:50:09 +08:00
committed by tim peng
parent dedeb2bd18
commit 9afd28fcb4
4 changed files with 173 additions and 231 deletions

View File

@@ -20,35 +20,51 @@ import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_INDIC
import android.annotation.ColorInt; import android.annotation.ColorInt;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.bluetooth.BluetoothDevice;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.core.graphics.drawable.IconCompat; import androidx.core.graphics.drawable.IconCompat;
import androidx.slice.Slice; import androidx.slice.Slice;
import androidx.slice.builders.ListBuilder; import androidx.slice.builders.ListBuilder;
import androidx.slice.builders.SliceAction; import androidx.slice.builders.SliceAction;
import com.android.internal.util.CollectionUtils;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.slices.CustomSliceable; import com.android.settings.slices.CustomSliceable;
import com.android.settings.slices.SliceBackgroundWorker; import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.media.MediaOutputSliceConstants; import com.android.settingslib.media.MediaOutputSliceConstants;
import java.util.ArrayList;
import java.util.List;
public class MediaOutputIndicatorSlice implements CustomSliceable { public class MediaOutputIndicatorSlice implements CustomSliceable {
private static final String TAG = "MediaOutputIndicatorSlice";
private Context mContext; private Context mContext;
@VisibleForTesting private LocalBluetoothManager mLocalBluetoothManager;
MediaOutputIndicatorWorker mWorker; private LocalBluetoothProfileManager mProfileManager;
public MediaOutputIndicatorSlice(Context context) { public MediaOutputIndicatorSlice(Context context) {
mContext = context; mContext = context;
mLocalBluetoothManager = com.android.settings.bluetooth.Utils.getLocalBtManager(context);
if (mLocalBluetoothManager == null) {
Log.e(TAG, "Bluetooth is not supported on this device");
return;
}
mProfileManager = mLocalBluetoothManager.getProfileManager();
} }
@Override @Override
public Slice getSlice() { public Slice getSlice() {
if (!getWorker().isVisible()) { if (!isVisible()) {
return null; return null;
} }
final IconCompat icon = IconCompat.createWithResource(mContext, final IconCompat icon = IconCompat.createWithResource(mContext,
@@ -66,18 +82,11 @@ public class MediaOutputIndicatorSlice implements CustomSliceable {
.setAccentColor(color) .setAccentColor(color)
.addRow(new ListBuilder.RowBuilder() .addRow(new ListBuilder.RowBuilder()
.setTitle(title) .setTitle(title)
.setSubtitle(getWorker().findActiveDeviceName()) .setSubtitle(findActiveDeviceName())
.setPrimaryAction(primarySliceAction)); .setPrimaryAction(primarySliceAction));
return listBuilder.build(); return listBuilder.build();
} }
private MediaOutputIndicatorWorker getWorker() {
if (mWorker == null) {
mWorker = (MediaOutputIndicatorWorker) SliceBackgroundWorker.getInstance(getUri());
}
return mWorker;
}
private Intent getMediaOutputSliceIntent() { private Intent getMediaOutputSliceIntent() {
final Intent intent = new Intent() final Intent intent = new Intent()
.setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT) .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
@@ -101,4 +110,65 @@ public class MediaOutputIndicatorSlice implements CustomSliceable {
public Class getBackgroundWorkerClass() { public Class getBackgroundWorkerClass() {
return MediaOutputIndicatorWorker.class; return MediaOutputIndicatorWorker.class;
} }
private boolean isVisible() {
// To decide Slice's visibility.
// return true if device is connected or previously connected, false for other cases.
return !CollectionUtils.isEmpty(getConnectableA2dpDevices())
|| !CollectionUtils.isEmpty(getConnectableHearingAidDevices());
}
private List<BluetoothDevice> getConnectableA2dpDevices() {
// Get A2dp devices on all states
// (STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING)
final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
if (a2dpProfile == null) {
return new ArrayList<>();
}
return a2dpProfile.getConnectableDevices();
}
private List<BluetoothDevice> getConnectableHearingAidDevices() {
// Get hearing aid profile devices on all states
// (STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING)
final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
if (hapProfile == null) {
return new ArrayList<>();
}
return hapProfile.getConnectableDevices();
}
private CharSequence findActiveDeviceName() {
// Return Hearing Aid device name if it is active
BluetoothDevice activeDevice = findActiveHearingAidDevice();
if (activeDevice != null) {
return activeDevice.getAliasName();
}
// Return A2DP device name if it is active
final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
if (a2dpProfile != null) {
activeDevice = a2dpProfile.getActiveDevice();
if (activeDevice != null) {
return activeDevice.getAliasName();
}
}
// No active device, return default summary
return mContext.getText(R.string.media_output_default_summary);
}
private BluetoothDevice findActiveHearingAidDevice() {
final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
if (hearingAidProfile == null) {
return null;
}
final List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
for (BluetoothDevice btDevice : activeDevices) {
if (btDevice != null) {
return btDevice;
}
}
return null;
}
} }

View File

@@ -16,26 +16,18 @@
package com.android.settings.media; package com.android.settings.media;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.util.Log; import android.util.Log;
import com.android.internal.util.CollectionUtils;
import com.android.settings.R;
import com.android.settings.bluetooth.Utils; import com.android.settings.bluetooth.Utils;
import com.android.settings.slices.SliceBackgroundWorker; import com.android.settings.slices.SliceBackgroundWorker;
import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.BluetoothCallback; import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/** /**
* Listener for background change from {@code BluetoothCallback} to update media output indicator. * Listener for background change from {@code BluetoothCallback} to update media output indicator.
@@ -45,7 +37,6 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
private static final String TAG = "MediaOutputIndicatorWorker"; private static final String TAG = "MediaOutputIndicatorWorker";
private LocalBluetoothManager mLocalBluetoothManager; private LocalBluetoothManager mLocalBluetoothManager;
private LocalBluetoothProfileManager mProfileManager;
public MediaOutputIndicatorWorker(Context context, Uri uri) { public MediaOutputIndicatorWorker(Context context, Uri uri) {
super(context, uri); super(context, uri);
@@ -53,12 +44,11 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
@Override @Override
protected void onSlicePinned() { protected void onSlicePinned() {
LocalBluetoothManager mLocalBluetoothManager = Utils.getLocalBtManager(getContext()); mLocalBluetoothManager = Utils.getLocalBtManager(getContext());
if (mLocalBluetoothManager == null) { if (mLocalBluetoothManager == null) {
Log.e(TAG, "Bluetooth is not supported on this device"); Log.e(TAG, "Bluetooth is not supported on this device");
return; return;
} }
mProfileManager = mLocalBluetoothManager.getProfileManager();
mLocalBluetoothManager.getEventManager().registerCallback(this); mLocalBluetoothManager.getEventManager().registerCallback(this);
} }
@@ -74,7 +64,6 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
@Override @Override
public void close() throws IOException { public void close() throws IOException {
mLocalBluetoothManager = null; mLocalBluetoothManager = null;
mProfileManager = null;
} }
@Override @Override
@@ -89,73 +78,4 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
notifySliceChange(); notifySliceChange();
} }
} }
/**
* To decide Slice's visibility.
*
* @return true if device is connected or previously connected, false for other cases.
*/
public boolean isVisible() {
return !CollectionUtils.isEmpty(getConnectableA2dpDevices())
|| !CollectionUtils.isEmpty(getConnectableHearingAidDevices());
}
private List<BluetoothDevice> getConnectableA2dpDevices() {
// get A2dp devices on all states
// (STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING)
final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
if (a2dpProfile == null) {
return new ArrayList<>();
}
return a2dpProfile.getConnectableDevices();
}
private List<BluetoothDevice> getConnectableHearingAidDevices() {
// get hearing aid profile devices on all states
// (STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING)
final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
if (hapProfile == null) {
return new ArrayList<>();
}
return hapProfile.getConnectableDevices();
}
/**
* Get active devices name.
*
* @return active Bluetooth device alias, or default summary if no active device.
*/
public CharSequence findActiveDeviceName() {
// Return Hearing Aid device name if it is active
BluetoothDevice activeDevice = findActiveHearingAidDevice();
if (activeDevice != null) {
return activeDevice.getAliasName();
}
// Return A2DP device name if it is active
final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
if (a2dpProfile != null) {
activeDevice = a2dpProfile.getActiveDevice();
if (activeDevice != null) {
return activeDevice.getAliasName();
}
}
// No active device, return default summary
return getContext().getText(R.string.media_output_default_summary);
}
private BluetoothDevice findActiveHearingAidDevice() {
final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
if (hearingAidProfile == null) {
return null;
}
final List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
for (BluetoothDevice btDevice : activeDevices) {
if (btDevice != null) {
return btDevice;
}
}
return null;
}
} }

View File

@@ -17,35 +17,29 @@
package com.android.settings.media; package com.android.settings.media;
import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_INDICATOR_SLICE_URI;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import androidx.slice.Slice; import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.SliceMetadata; import androidx.slice.SliceMetadata;
import androidx.slice.SliceProvider; import androidx.slice.SliceProvider;
import androidx.slice.core.SliceAction;
import androidx.slice.widget.SliceLiveData; import androidx.slice.widget.SliceLiveData;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.media.LocalMediaManager; import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.media.MediaDevice; import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.media.MediaOutputSliceConstants; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@@ -53,59 +47,108 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
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 java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class}) @Config(shadows = {ShadowBluetoothUtils.class})
@Ignore("b/129292771")
public class MediaOutputIndicatorSliceTest { public class MediaOutputIndicatorSliceTest {
private static final String TEST_DEVICE_NAME = "test_device_name"; private static final String TEST_A2DP_DEVICE_NAME = "Test_A2DP_BT_Device_NAME";
private static final int TEST_DEVICE_1_ICON = private static final String TEST_HAP_DEVICE_NAME = "Test_HAP_BT_Device_NAME";
com.android.internal.R.drawable.ic_bt_headphones_a2dp; private static final String TEST_A2DP_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
private static final String TEST_HAP_DEVICE_ADDRESS = "00:B2:B2:B2:B2:B2";
@Mock @Mock
private LocalMediaManager mLocalMediaManager; private A2dpProfile mA2dpProfile;
@Mock
private final List<MediaDevice> mDevices = new ArrayList<>(); private HearingAidProfile mHearingAidProfile;
@Mock
private LocalBluetoothManager mLocalBluetoothManager;
@Mock
private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice mA2dpDevice;
private BluetoothDevice mHapDevice;
private BluetoothManager mBluetoothManager;
private Context mContext; private Context mContext;
private List<BluetoothDevice> mDevicesList;
private MediaOutputIndicatorSlice mMediaOutputIndicatorSlice; private MediaOutputIndicatorSlice mMediaOutputIndicatorSlice;
private MediaOutputIndicatorWorker mMediaOutputIndicatorWorker;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
// Set-up specs for SliceMetadata. // Set-up specs for SliceMetadata.
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS); SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
// Setup Bluetooth environment
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager;
mBluetoothManager = new BluetoothManager(mContext);
mBluetoothAdapter = mBluetoothManager.getAdapter();
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
// Setup A2dp device
mA2dpDevice = spy(mBluetoothAdapter.getRemoteDevice(TEST_A2DP_DEVICE_ADDRESS));
when(mA2dpDevice.getName()).thenReturn(TEST_A2DP_DEVICE_NAME);
when(mA2dpDevice.isConnected()).thenReturn(true);
// Setup HearingAid device
mHapDevice = spy(mBluetoothAdapter.getRemoteDevice(TEST_HAP_DEVICE_ADDRESS));
when(mHapDevice.getName()).thenReturn(TEST_HAP_DEVICE_NAME);
when(mHapDevice.isConnected()).thenReturn(true);
mMediaOutputIndicatorSlice = new MediaOutputIndicatorSlice(mContext); mMediaOutputIndicatorSlice = new MediaOutputIndicatorSlice(mContext);
mMediaOutputIndicatorWorker = spy(new MediaOutputIndicatorWorker( mDevicesList = new ArrayList<>();
mContext, MEDIA_OUTPUT_INDICATOR_SLICE_URI));
mMediaOutputIndicatorSlice.mWorker = mMediaOutputIndicatorWorker;
} }
@Test @Test
public void getSlice_invisible_returnNull() { public void getSlice_noConnectableDevice_returnNull() {
when(mMediaOutputIndicatorWorker.isVisible()).thenReturn(false); mDevicesList.clear();
when(mA2dpProfile.getConnectableDevices()).thenReturn(mDevicesList);
assertThat(mMediaOutputIndicatorSlice.getSlice()).isNull(); assertThat(mMediaOutputIndicatorSlice.getSlice()).isNull();
} }
@Test @Test
public void getSlice_withActiveDevice_checkContent() { public void getSlice_noActiveDevice_verifyDefaultName() {
when(mMediaOutputIndicatorWorker.isVisible()).thenReturn(true); mDevicesList.add(mA2dpDevice);
when(mMediaOutputIndicatorWorker.findActiveDeviceName()).thenReturn(TEST_DEVICE_NAME); when(mA2dpProfile.getConnectableDevices()).thenReturn(mDevicesList);
when(mA2dpProfile.getActiveDevice()).thenReturn(null);
// Verify slice title and subtitle
final Slice mediaSlice = mMediaOutputIndicatorSlice.getSlice(); final Slice mediaSlice = mMediaOutputIndicatorSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, mediaSlice); final SliceMetadata metadata = SliceMetadata.from(mContext, mediaSlice);
// Verify slice title and subtitle
assertThat(metadata.getTitle()).isEqualTo(mContext.getText(R.string.media_output_title)); assertThat(metadata.getTitle()).isEqualTo(mContext.getText(R.string.media_output_title));
assertThat(metadata.getSubtitle()).isEqualTo(TEST_DEVICE_NAME); assertThat(metadata.getSubtitle()).isEqualTo(mContext.getText(
R.string.media_output_default_summary));
}
@Test
public void getSlice_A2dpDeviceActive_verifyName() {
mDevicesList.add(mA2dpDevice);
when(mA2dpProfile.getConnectableDevices()).thenReturn(mDevicesList);
when(mA2dpProfile.getActiveDevice()).thenReturn(mA2dpDevice);
final Slice mediaSlice = mMediaOutputIndicatorSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, mediaSlice);
assertThat(metadata.getTitle()).isEqualTo(mContext.getText(R.string.media_output_title));
assertThat(metadata.getSubtitle()).isEqualTo(TEST_A2DP_DEVICE_NAME);
}
@Test
public void getSlice_HADeviceActive_verifyName() {
mDevicesList.add(mHapDevice);
when(mHearingAidProfile.getConnectableDevices()).thenReturn(mDevicesList);
when(mHearingAidProfile.getActiveDevices()).thenReturn(mDevicesList);
// Verify slice title and subtitle
final Slice mediaSlice = mMediaOutputIndicatorSlice.getSlice();
final SliceMetadata metadata = SliceMetadata.from(mContext, mediaSlice);
assertThat(metadata.getTitle()).isEqualTo(mContext.getText(R.string.media_output_title));
assertThat(metadata.getSubtitle()).isEqualTo(TEST_HAP_DEVICE_NAME);
} }
} }

View File

@@ -16,28 +16,18 @@
package com.android.settings.media; package com.android.settings.media;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import com.android.settings.R;
import com.android.settings.bluetooth.Utils;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils; import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.BluetoothEventManager; import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@@ -45,119 +35,38 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowBluetoothDevice;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothUtils.class, @Config(shadows = {ShadowBluetoothUtils.class})
ShadowBluetoothDevice.class})
@Ignore("b/129292771")
public class MediaOutputIndicatorWorkerTest { public class MediaOutputIndicatorWorkerTest {
private static final String TEST_A2DP_DEVICE_NAME = "Test_A2DP_BT_Device_NAME";
private static final String TEST_HAP_DEVICE_NAME = "Test_HAP_BT_Device_NAME";
private static final String TEST_A2DP_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1";
private static final String TEST_HAP_DEVICE_ADDRESS = "00:B2:B2:B2:B2:B2";
private static final Uri URI = Uri.parse("content://com.android.settings.slices/test"); private static final Uri URI = Uri.parse("content://com.android.settings.slices/test");
@Mock
private A2dpProfile mA2dpProfile;
@Mock
private HearingAidProfile mHearingAidProfile;
@Mock
private LocalBluetoothManager mLocalManager;
@Mock @Mock
private BluetoothEventManager mBluetoothEventManager; private BluetoothEventManager mBluetoothEventManager;
@Mock @Mock
private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice mA2dpDevice;
private BluetoothDevice mHapDevice;
private BluetoothManager mBluetoothManager;
private Context mContext;
private List<BluetoothDevice> mDevicesList;
private LocalBluetoothManager mLocalBluetoothManager; private LocalBluetoothManager mLocalBluetoothManager;
private Context mContext;
private MediaOutputIndicatorWorker mMediaDeviceUpdateWorker; private MediaOutputIndicatorWorker mMediaDeviceUpdateWorker;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalManager; ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager;
mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager); when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
mBluetoothManager = new BluetoothManager(mContext);
mBluetoothAdapter = mBluetoothManager.getAdapter();
// Setup A2dp device
mA2dpDevice = spy(mBluetoothAdapter.getRemoteDevice(TEST_A2DP_DEVICE_ADDRESS));
when(mA2dpDevice.getName()).thenReturn(TEST_A2DP_DEVICE_NAME);
when(mA2dpDevice.isConnected()).thenReturn(true);
// Setup HearingAid device
mHapDevice = spy(mBluetoothAdapter.getRemoteDevice(TEST_HAP_DEVICE_ADDRESS));
when(mHapDevice.getName()).thenReturn(TEST_HAP_DEVICE_NAME);
when(mHapDevice.isConnected()).thenReturn(true);
mMediaDeviceUpdateWorker = new MediaOutputIndicatorWorker(mContext, URI); mMediaDeviceUpdateWorker = new MediaOutputIndicatorWorker(mContext, URI);
mDevicesList = new ArrayList<>();
} }
@Test @Test
public void isVisible_noConnectableDevice_returnFalse() { public void onSlicePinned_registerCallback() {
mDevicesList.clear(); mMediaDeviceUpdateWorker.onSlicePinned();
when(mA2dpProfile.getConnectableDevices()).thenReturn(mDevicesList); verify(mBluetoothEventManager).registerCallback(mMediaDeviceUpdateWorker);
assertThat(mMediaDeviceUpdateWorker.isVisible()).isFalse();
} }
@Test @Test
public void isVisible_withConnectableA2dpDevice_returnTrue() { public void onSliceUnpinned_unRegisterCallback() {
mDevicesList.clear(); mMediaDeviceUpdateWorker.onSlicePinned();
mDevicesList.add(mA2dpDevice); mMediaDeviceUpdateWorker.onSliceUnpinned();
when(mHearingAidProfile.getConnectableDevices()).thenReturn(mDevicesList); verify(mBluetoothEventManager).unregisterCallback(mMediaDeviceUpdateWorker);
assertThat(mMediaDeviceUpdateWorker.isVisible()).isTrue();
}
@Test
public void isVisible_withConnectableHADevice_returnTrue() {
mDevicesList.clear();
mDevicesList.add(mHapDevice);
when(mA2dpProfile.getConnectableDevices()).thenReturn(mDevicesList);
assertThat(mMediaDeviceUpdateWorker.isVisible()).isTrue();
}
@Test
public void findActiveDeviceName_A2dpDeviceActive_verifyName() {
when(mA2dpProfile.getActiveDevice()).thenReturn(mA2dpDevice);
assertThat(mMediaDeviceUpdateWorker.findActiveDeviceName())
.isEqualTo(mA2dpDevice.getAliasName());
}
@Test
public void findActiveDeviceName_HADeviceActive_verifyName() {
mDevicesList.add(mHapDevice);
when(mHearingAidProfile.getActiveDevices()).thenReturn(mDevicesList);
assertThat(mMediaDeviceUpdateWorker.findActiveDeviceName())
.isEqualTo(mHapDevice.getAliasName());
}
@Test
public void findActiveDeviceName_noActiveDevice_verifyDefaultName() {
when(mA2dpProfile.getActiveDevice()).thenReturn(null);
mDevicesList.clear();
when(mHearingAidProfile.getActiveDevices()).thenReturn(mDevicesList);
assertThat(mMediaDeviceUpdateWorker.findActiveDeviceName())
.isEqualTo(mContext.getText(R.string.media_output_default_summary));
} }
} }