Clear media switcher items when in phone call

- update test case

Bug: 132385707
Test: make -j42 RunSettingsRoboTests
Change-Id: I2bbd35e869e8ab5596d280d04fddc98f05629190
This commit is contained in:
hughchen
2019-05-09 11:27:25 +08:00
parent a6bebb793e
commit 095c5be448
2 changed files with 37 additions and 9 deletions

View File

@@ -24,6 +24,7 @@ import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
@@ -76,14 +77,8 @@ public class MediaOutputSlice implements CustomSliceable {
final ListBuilder listBuilder = new ListBuilder(mContext, getUri(), ListBuilder.INFINITY)
.setAccentColor(COLOR_NOT_TINTED);
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (!adapter.isEnabled()) {
Log.d(TAG, "getSlice() Bluetooth is off");
return listBuilder.build();
}
if (getWorker() == null) {
Log.d(TAG, "getSlice() Can not get worker through uri!");
if (!isVisible()) {
Log.d(TAG, "getSlice() is not visible");
return listBuilder.build();
}
@@ -195,4 +190,19 @@ public class MediaOutputSlice implements CustomSliceable {
public Class getBackgroundWorkerClass() {
return MediaDeviceUpdateWorker.class;
}
private boolean isVisible() {
// To decide Slice's visibility.
// Return true if
// 1. phone is not in ongoing call mode
// 2. worker is not null
// 3. Bluetooth is enabled
final TelephonyManager telephonyManager =
(TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
return telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE
&& adapter.isEnabled()
&& getWorker() != null;
}
}

View File

@@ -34,6 +34,7 @@ import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.telephony.TelephonyManager;
import androidx.slice.Slice;
import androidx.slice.SliceMetadata;
@@ -55,12 +56,13 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowTelephonyManager;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class})
@Config(shadows = {ShadowBluetoothAdapter.class, ShadowTelephonyManager.class})
public class MediaOutputSliceTest {
private static final String TEST_PACKAGE_NAME = "com.fake.android.music";
@@ -80,17 +82,21 @@ public class MediaOutputSliceTest {
private MediaOutputSlice mMediaOutputSlice;
private MediaDeviceUpdateWorker mMediaDeviceUpdateWorker;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private ShadowTelephonyManager mShadowTelephonyManager;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mShadowTelephonyManager = Shadow.extract(mContext.getSystemService(
Context.TELEPHONY_SERVICE));
// Set-up specs for SliceMetadata.
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
// Setup BluetoothAdapter
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowTelephonyManager.setCallState(TelephonyManager.CALL_STATE_IDLE);
mMediaOutputSlice = new MediaOutputSlice(mContext);
mMediaDeviceUpdateWorker = new MediaDeviceUpdateWorker(mContext, MEDIA_OUTPUT_SLICE_URI);
@@ -124,6 +130,18 @@ public class MediaOutputSliceTest {
assertThat(rows).isEqualTo(0);
}
@Test
public void getSlice_callStateRinging_shouldReturnZeroRow() {
mShadowTelephonyManager.setCallState(TelephonyManager.CALL_STATE_RINGING);
final Slice slice = mMediaOutputSlice.getSlice();
final int rows = SliceQuery.findAll(slice, FORMAT_SLICE, HINT_LIST_ITEM,
null /* nonHints */).size();
assertThat(rows).isEqualTo(0);
}
@Test
public void getSlice_shouldHaveActiveDeviceName() {
mDevices.clear();