fix: 首次提交

This commit is contained in:
2024-12-09 11:25:23 +08:00
parent d0c01071e9
commit 2c2109a5f3
4741 changed files with 290641 additions and 0 deletions

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.media;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class BluetoothMediaDeviceTest {
private static final String TEST_ADDRESS = "11:22:33:44:55:66";
@Mock
private CachedBluetoothDevice mDevice;
private Context mContext;
private BluetoothMediaDevice mBluetoothMediaDevice;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
when(mDevice.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(true);
when(mDevice.isActiveDevice(BluetoothProfile.HEARING_AID)).thenReturn(true);
when(mDevice.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true);
mBluetoothMediaDevice = new BluetoothMediaDevice(mContext, mDevice, null, null);
}
@Test
public void isCachedBluetoothDeviceConnected_deviceConnected_returnTrue() {
when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mDevice.isConnected()).thenReturn(true);
assertThat(mBluetoothMediaDevice.isConnected()).isTrue();
}
@Test
public void isCachedBluetoothDeviceConnected_deviceNotConnected_returnFalse() {
when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mDevice.isConnected()).thenReturn(false);
assertThat(mBluetoothMediaDevice.isConnected()).isFalse();
}
@Test
public void isFastPairDevice_isUntetheredHeadset_returnTrue() {
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
when(mDevice.getDevice()).thenReturn(bluetoothDevice);
final String value = "True";
final byte[] bytes = value.getBytes();
when(bluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
.thenReturn(bytes);
assertThat(mBluetoothMediaDevice.isFastPairDevice()).isTrue();
}
@Test
public void isFastPairDevice_isNotUntetheredHeadset_returnFalse() {
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
when(mDevice.getDevice()).thenReturn(bluetoothDevice);
final String value = "asjdaioshfaio";
final byte[] bytes = value.getBytes();
when(bluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
.thenReturn(bytes);
assertThat(mBluetoothMediaDevice.isFastPairDevice()).isFalse();
}
@Test
public void getIcon_isNotFastPairDevice_drawableTypeIsNotBitmapDrawable() {
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
when(mDevice.getDevice()).thenReturn(bluetoothDevice);
final String value = "False";
final byte[] bytes = value.getBytes();
when(bluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
.thenReturn(bytes);
assertThat(mBluetoothMediaDevice.getIcon() instanceof BitmapDrawable).isFalse();
}
@Test
public void getId_returnsCachedBluetoothDeviceAddress() {
when(mDevice.getAddress()).thenReturn(TEST_ADDRESS);
assertThat(mBluetoothMediaDevice.getId()).isEqualTo(TEST_ADDRESS);
}
}

View File

@@ -0,0 +1,328 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.media;
import static com.google.common.truth.Truth.assertThat;
import android.media.AudioDeviceInfo;
import android.media.MediaRoute2Info;
import android.platform.test.flag.junit.SetFlagsRule;
import com.android.settingslib.R;
import com.android.settingslib.media.flags.Flags;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class DeviceIconUtilTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
@Before
public void setup() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_TV_MEDIA_OUTPUT_DIALOG);
}
@Test
public void getIconResIdFromMediaRouteType_usbDevice_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_USB_DEVICE))
.isEqualTo(R.drawable.ic_headphone);
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_USB_DEVICE))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromMediaRouteType_usbHeadset_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_USB_HEADSET))
.isEqualTo(R.drawable.ic_headphone);
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_USB_HEADSET))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromMediaRouteType_usbAccessory_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_USB_ACCESSORY))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromMediaRouteType_tv_usbAccessory_isUsb() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_USB_ACCESSORY))
.isEqualTo(R.drawable.ic_usb);
}
@Test
public void getIconResIdFromMediaRouteType_dock_isDock() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_DOCK))
.isEqualTo(R.drawable.ic_dock_device);
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_DOCK))
.isEqualTo(R.drawable.ic_dock_device);
}
@Test
public void getIconResIdFromMediaRouteType_hdmi() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_HDMI))
.isEqualTo(R.drawable.ic_external_display);
}
@Test
public void getIconResIdFromMediaRouteType_tv_hdmi_isTv() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_HDMI))
.isEqualTo(R.drawable.ic_tv);
}
@Test
public void getIconResIdFromMediaRouteType_hdmiArc_isExternalDisplay() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_HDMI_ARC))
.isEqualTo(R.drawable.ic_external_display);
}
@Test
public void getIconResIdFromMediaRouteType_tv_hdmiArc_isHdmi() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_HDMI_ARC))
.isEqualTo(R.drawable.ic_hdmi);
}
@Test
public void getIconResIdFromMediaRouteType_hdmiEarc_isExternalDisplay() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_HDMI_EARC))
.isEqualTo(R.drawable.ic_external_display);
}
@Test
public void getIconResIdFromMediaRouteType_tv_hdmiEarc_isHdmi() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_HDMI_EARC))
.isEqualTo(R.drawable.ic_hdmi);
}
@Test
public void getIconResIdFromMediaRouteType_wiredHeadset_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_WIRED_HEADSET))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromMediaRouteType_tv_wiredHeadset_isWiredDevice() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_WIRED_HEADSET))
.isEqualTo(R.drawable.ic_wired_device);
}
@Test
public void getIconResIdFromMediaRouteType_wiredHeadphones_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_WIRED_HEADPHONES))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromMediaRouteType_tv_wiredHeadphones_isWiredDevice() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_WIRED_HEADPHONES))
.isEqualTo(R.drawable.ic_wired_device);
}
@Test
public void getIconResIdFromMediaRouteType_builtinSpeaker_isSmartphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_BUILTIN_SPEAKER))
.isEqualTo(R.drawable.ic_smartphone);
}
@Test
public void getIconResIdFromMediaRouteType_tv_builtinSpeaker_isTv() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_BUILTIN_SPEAKER))
.isEqualTo(R.drawable.ic_tv);
}
@Test
public void getIconResIdFromMediaRouteType_unsupportedType_isSmartphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_UNKNOWN))
.isEqualTo(R.drawable.ic_smartphone);
}
@Test
public void getIconResIdFromMediaRouteType_tv_unsupportedType_isSpeaker() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromMediaRouteType(MediaRoute2Info.TYPE_UNKNOWN))
.isEqualTo(R.drawable.ic_media_speaker_device);
}
@Test
public void getIconResIdFromAudioDeviceType_usbDevice_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_USB_DEVICE))
.isEqualTo(R.drawable.ic_headphone);
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_USB_DEVICE))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromAudioDeviceType_usbHeadset_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_USB_HEADSET))
.isEqualTo(R.drawable.ic_headphone);
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_USB_HEADSET))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromAudioDeviceType_usbAccessory_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_USB_ACCESSORY))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromAudioDeviceType_tv_usbAccessory_isUsb() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_USB_ACCESSORY))
.isEqualTo(R.drawable.ic_usb);
}
@Test
public void getIconResIdFromAudioDeviceType_dock_isDock() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_DOCK))
.isEqualTo(R.drawable.ic_dock_device);
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_DOCK))
.isEqualTo(R.drawable.ic_dock_device);
}
@Test
public void getIconResIdFromAudioDeviceType_hdmi_isExternalDisplay() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_HDMI))
.isEqualTo(R.drawable.ic_external_display);
}
@Test
public void getIconResIdFromAudioDeviceType_tv_hdmi_isTv() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_HDMI))
.isEqualTo(R.drawable.ic_tv);
}
@Test
public void getIconResIdFromAudioDeviceType_hdmiArc_isExternalDisplay() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_HDMI_ARC))
.isEqualTo(R.drawable.ic_external_display);
}
@Test
public void getIconResIdFromAudioDeviceType_hdmiArc_isHdmi() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_HDMI_ARC))
.isEqualTo(R.drawable.ic_hdmi);
}
@Test
public void getIconResIdFromAudioDeviceType_hdmiEarc_isExternalDisplay() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_HDMI_EARC))
.isEqualTo(R.drawable.ic_external_display);
}
@Test
public void getIconResIdFromAudioDeviceType_tv_hdmiEarc() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_HDMI_EARC))
.isEqualTo(R.drawable.ic_hdmi);
}
@Test
public void getIconResIdFromAudioDeviceType_wiredHeadset_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_WIRED_HEADSET))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromAudioDeviceType_tv_wiredHeadset_isWiredDevice() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_WIRED_HEADSET))
.isEqualTo(R.drawable.ic_wired_device);
}
@Test
public void getIconResIdFromAudioDeviceType_wiredHeadphones_isHeadphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_WIRED_HEADPHONES))
.isEqualTo(R.drawable.ic_headphone);
}
@Test
public void getIconResIdFromAudioDeviceType_tv_wiredHeadphones_isWiredDevice() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_WIRED_HEADPHONES))
.isEqualTo(R.drawable.ic_wired_device);
}
@Test
public void getIconResIdFromAudioDeviceType_builtinSpeaker_isSmartphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER))
.isEqualTo(R.drawable.ic_smartphone);
}
@Test
public void getIconResIdFromAudioDeviceType_tv_builtinSpeaker_isTv() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER))
.isEqualTo(R.drawable.ic_tv);
}
@Test
public void getIconResIdFromAudioDeviceType_unsupportedType_isSmartphone() {
assertThat(new DeviceIconUtil(/* isTv */ false)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_UNKNOWN))
.isEqualTo(R.drawable.ic_smartphone);
}
@Test
public void getIconResIdFromAudioDeviceType_tv_unsupportedType_isSpeaker() {
assertThat(new DeviceIconUtil(/* isTv */ true)
.getIconResIdFromAudioDeviceType(AudioDeviceInfo.TYPE_UNKNOWN))
.isEqualTo(R.drawable.ic_media_speaker_device);
}
}

View File

@@ -0,0 +1,151 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.media;
import static android.media.MediaRoute2Info.TYPE_GROUP;
import static android.media.MediaRoute2Info.TYPE_REMOTE_CAR;
import static android.media.MediaRoute2Info.TYPE_REMOTE_COMPUTER;
import static android.media.MediaRoute2Info.TYPE_REMOTE_GAME_CONSOLE;
import static android.media.MediaRoute2Info.TYPE_REMOTE_SMARTPHONE;
import static android.media.MediaRoute2Info.TYPE_REMOTE_SMARTWATCH;
import static android.media.MediaRoute2Info.TYPE_REMOTE_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_REMOTE_TABLET;
import static android.media.MediaRoute2Info.TYPE_REMOTE_TABLET_DOCKED;
import static android.media.MediaRoute2Info.TYPE_REMOTE_TV;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.media.MediaRoute2Info;
import android.media.MediaRouter2Manager;
import com.android.settingslib.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class InfoMediaDeviceTest {
private static final String TEST_PACKAGE_NAME = "com.test.packagename";
private static final String TEST_ID = "test_id";
private static final String TEST_NAME = "test_name";
@Mock
private MediaRouter2Manager mRouterManager;
@Mock
private MediaRoute2Info mRouteInfo;
private Context mContext;
private InfoMediaDevice mInfoMediaDevice;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mInfoMediaDevice = new InfoMediaDevice(mContext, mRouteInfo);
}
@Test
public void getName_shouldReturnName() {
when(mRouteInfo.getName()).thenReturn(TEST_NAME);
assertThat(mInfoMediaDevice.getName()).isEqualTo(TEST_NAME);
}
@Test
public void getSummary_clientPackageNameIsNull_returnNull() {
when(mRouteInfo.getClientPackageName()).thenReturn(null);
assertThat(mInfoMediaDevice.getSummary()).isEqualTo(null);
}
@Test
public void getSummary_clientPackageNameIsNotNull_returnActive() {
when(mRouteInfo.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
assertThat(mInfoMediaDevice.getSummary())
.isEqualTo(mContext.getString(R.string.bluetooth_active_no_battery_level));
}
@Test
public void getId_shouldReturnId() {
when(mRouteInfo.getId()).thenReturn(TEST_ID);
assertThat(mInfoMediaDevice.getId()).isEqualTo(TEST_ID);
}
@Test
public void getDrawableResId_returnCorrectResId() {
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_TV);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_media_display_device);
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_media_speaker_device);
when(mRouteInfo.getType()).thenReturn(TYPE_GROUP);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_media_group_device);
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_TABLET);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_media_tablet);
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_TABLET_DOCKED);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_dock_device);
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_COMPUTER);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_media_computer);
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_GAME_CONSOLE);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_media_game_console);
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_CAR);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_media_car);
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_SMARTWATCH);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(
R.drawable.ic_media_smartwatch);
when(mRouteInfo.getType()).thenReturn(TYPE_REMOTE_SMARTPHONE);
assertThat(mInfoMediaDevice.getDrawableResIdByType()).isEqualTo(R.drawable.ic_smartphone);
}
}

View File

@@ -0,0 +1,880 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.media;
import static android.media.MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_REMOTE_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_REMOTE_TV;
import static android.media.MediaRoute2Info.TYPE_USB_DEVICE;
import static android.media.MediaRoute2Info.TYPE_WIRED_HEADSET;
import static android.media.MediaRoute2ProviderService.REASON_NETWORK_ERROR;
import static android.media.MediaRoute2ProviderService.REASON_UNKNOWN_ERROR;
import static com.android.settingslib.media.LocalMediaManager.MediaDeviceState.STATE_SELECTED;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Context;
import android.media.MediaRoute2Info;
import android.media.MediaRouter2Manager;
import android.media.RouteListingPreference;
import android.media.RoutingSessionInfo;
import android.media.session.MediaSessionManager;
import android.os.Build;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.testutils.shadow.ShadowRouter2Manager;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowRouter2Manager.class})
public class InfoMediaManagerTest {
private static final String TEST_PACKAGE_NAME = "com.test.packagename";
private static final String TEST_PACKAGE_NAME_2 = "com.test.packagename2";
private static final String TEST_ID = "test_id";
private static final String TEST_ID_1 = "test_id_1";
private static final String TEST_ID_2 = "test_id_2";
private static final String TEST_ID_3 = "test_id_3";
private static final String TEST_ID_4 = "test_id_4";
private static final String TEST_NAME = "test_name";
private static final String TEST_DUPLICATED_ID_1 = "test_duplicated_id_1";
private static final String TEST_DUPLICATED_ID_2 = "test_duplicated_id_2";
private static final String TEST_DUPLICATED_ID_3 = "test_duplicated_id_3";
@Mock
private MediaRouter2Manager mRouterManager;
@Mock
private LocalBluetoothManager mLocalBluetoothManager;
@Mock
private MediaManager.MediaDeviceCallback mCallback;
@Mock
private MediaSessionManager mMediaSessionManager;
@Mock
private ComponentName mComponentName;
private ManagerInfoMediaManager mInfoMediaManager;
private Context mContext;
private ShadowRouter2Manager mShadowRouter2Manager;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mMediaSessionManager).when(mContext).getSystemService(
Context.MEDIA_SESSION_SERVICE);
mInfoMediaManager =
new ManagerInfoMediaManager(
mContext, TEST_PACKAGE_NAME, null, mLocalBluetoothManager);
mShadowRouter2Manager = ShadowRouter2Manager.getShadow();
mInfoMediaManager.mRouterManager = MediaRouter2Manager.getInstance(mContext);
}
@Test
public void stopScan_notStartFirst_notCallsUnregister() {
mInfoMediaManager.mRouterManager = mRouterManager;
mInfoMediaManager.stopScan();
verify(mRouterManager, never()).unregisterScanRequest();
}
@Test
public void stopScan_startFirst_callsUnregister() {
RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
mInfoMediaManager.mRouterManager = mRouterManager;
// Since test is running in Robolectric, return a fake session to avoid NPE.
when(mRouterManager.getRoutingSessions(anyString())).thenReturn(List.of(sessionInfo));
mInfoMediaManager.startScan();
mInfoMediaManager.stopScan();
verify(mRouterManager).unregisterScanRequest();
}
@Test
public void onRouteAdded_getAvailableRoutes_shouldAddMediaDevice() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
final List<String> selectedRoutes = new ArrayList<>();
selectedRoutes.add(TEST_ID);
when(sessionInfo.getSelectedRoutes()).thenReturn(selectedRoutes);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getDeduplicationIds()).thenReturn(Set.of());
final List<MediaRoute2Info> routes = new ArrayList<>();
routes.add(info);
mShadowRouter2Manager.setTransferableRoutes(routes);
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
assertThat(mediaDevice).isNull();
mInfoMediaManager.mMediaRouterCallback.onRoutesUpdated();
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice);
assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size());
}
@Test
public void onSessionReleased_shouldUpdateConnectedDevice() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo1 = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo1);
final RoutingSessionInfo sessionInfo2 = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo2);
final List<String> selectedRoutesSession1 = new ArrayList<>();
selectedRoutesSession1.add(TEST_ID_1);
when(sessionInfo1.getSelectedRoutes()).thenReturn(selectedRoutesSession1);
final List<String> selectedRoutesSession2 = new ArrayList<>();
selectedRoutesSession2.add(TEST_ID_2);
when(sessionInfo2.getSelectedRoutes()).thenReturn(selectedRoutesSession2);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
final MediaRoute2Info info1 = mock(MediaRoute2Info.class);
when(info1.getId()).thenReturn(TEST_ID_1);
when(info1.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
final MediaRoute2Info info2 = mock(MediaRoute2Info.class);
when(info2.getId()).thenReturn(TEST_ID_2);
when(info2.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
final List<MediaRoute2Info> routes = new ArrayList<>();
routes.add(info1);
routes.add(info2);
mShadowRouter2Manager.setAllRoutes(routes);
mShadowRouter2Manager.setTransferableRoutes(routes);
final MediaDevice mediaDevice1 = mInfoMediaManager.findMediaDevice(TEST_ID_1);
assertThat(mediaDevice1).isNull();
final MediaDevice mediaDevice2 = mInfoMediaManager.findMediaDevice(TEST_ID_2);
assertThat(mediaDevice2).isNull();
mInfoMediaManager.mMediaRouterCallback.onRoutesUpdated();
final MediaDevice infoDevice1 = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice1.getId()).isEqualTo(TEST_ID_1);
final MediaDevice infoDevice2 = mInfoMediaManager.mMediaDevices.get(1);
assertThat(infoDevice2.getId()).isEqualTo(TEST_ID_2);
// The active routing session is the last one in the list, which maps to infoDevice2.
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice2);
routingSessionInfos.remove(sessionInfo2);
mInfoMediaManager.mMediaRouterCallback.onSessionReleased(sessionInfo2);
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice1);
}
@Test
public void onPreferredFeaturesChanged_samePackageName_shouldAddMediaDevice() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
final List<String> selectedRoutes = new ArrayList<>();
selectedRoutes.add(TEST_ID);
when(sessionInfo.getSelectedRoutes()).thenReturn(selectedRoutes);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getDeduplicationIds()).thenReturn(Set.of());
final List<MediaRoute2Info> routes = new ArrayList<>();
routes.add(info);
mShadowRouter2Manager.setTransferableRoutes(routes);
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
assertThat(mediaDevice).isNull();
mInfoMediaManager.mMediaRouterCallback.onPreferredFeaturesChanged(TEST_PACKAGE_NAME, null);
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice);
assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size());
}
@Test
public void onPreferredFeaturesChanged_differentPackageName_doNothing() {
mInfoMediaManager.mMediaRouterCallback.onPreferredFeaturesChanged("com.fake.play", null);
assertThat(mInfoMediaManager.mMediaDevices).hasSize(0);
}
@Test
public void onRoutesChanged_getAvailableRoutes_shouldAddMediaDevice() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
final List<String> selectedRoutes = new ArrayList<>();
selectedRoutes.add(TEST_ID);
when(sessionInfo.getSelectedRoutes()).thenReturn(selectedRoutes);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getDeduplicationIds()).thenReturn(Set.of());
final List<MediaRoute2Info> routes = new ArrayList<>();
routes.add(info);
mShadowRouter2Manager.setTransferableRoutes(routes);
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
assertThat(mediaDevice).isNull();
mInfoMediaManager.mMediaRouterCallback.onRoutesUpdated();
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice);
assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size());
}
@Test
public void onRoutesChanged_getAvailableRoutes_shouldFilterDevice() {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT",
Build.VERSION_CODES.UPSIDE_DOWN_CAKE);
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
final List<String> selectedRoutes = new ArrayList<>();
selectedRoutes.add(TEST_ID);
when(sessionInfo.getSelectedRoutes()).thenReturn(selectedRoutes);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
mShadowRouter2Manager.setTransferableRoutes(getRoutesListWithDuplicatedIds());
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
assertThat(mediaDevice).isNull();
mInfoMediaManager.mMediaRouterCallback.onRoutesUpdated();
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice);
assertThat(mInfoMediaManager.mMediaDevices).hasSize(2);
}
@Test
public void onRouteChanged_getAvailableRoutesWithPreferenceListExit_ordersRoutes() {
RouteListingPreference routeListingPreference = setUpPreferenceList(TEST_PACKAGE_NAME);
setUpSelectedRoutes(TEST_PACKAGE_NAME);
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
when(mRouterManager.getRoutingSessions(TEST_PACKAGE_NAME)).thenReturn(routingSessionInfos);
when(sessionInfo.getSelectedRoutes()).thenReturn(ImmutableList.of(TEST_ID));
setAvailableRoutesList(TEST_PACKAGE_NAME);
mInfoMediaManager.mRouterManager = mRouterManager;
mInfoMediaManager.mMediaRouterCallback.onRouteListingPreferenceUpdated(TEST_PACKAGE_NAME,
routeListingPreference);
mInfoMediaManager.mMediaRouterCallback.onRoutesUpdated();
assertThat(mInfoMediaManager.mMediaDevices).hasSize(4);
assertThat(mInfoMediaManager.mMediaDevices.get(0).getId()).isEqualTo(TEST_ID);
assertThat(mInfoMediaManager.mMediaDevices.get(1).getId()).isEqualTo(TEST_ID_1);
assertThat(mInfoMediaManager.mMediaDevices.get(2).getId()).isEqualTo(TEST_ID_4);
assertThat(mInfoMediaManager.mMediaDevices.get(2).isSuggestedDevice()).isTrue();
assertThat(mInfoMediaManager.mMediaDevices.get(3).getId()).isEqualTo(TEST_ID_3);
}
@Test
public void onRouteChanged_preferenceListUpdateWithDifferentPkg_notOrdersRoutes() {
RouteListingPreference routeListingPreference = setUpPreferenceList(TEST_PACKAGE_NAME_2);
setUpSelectedRoutes(TEST_PACKAGE_NAME);
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
when(mRouterManager.getRoutingSessions(TEST_PACKAGE_NAME)).thenReturn(routingSessionInfos);
when(sessionInfo.getSelectedRoutes()).thenReturn(ImmutableList.of(TEST_ID));
setAvailableRoutesList(TEST_PACKAGE_NAME);
mInfoMediaManager.mRouterManager = mRouterManager;
mInfoMediaManager.mMediaRouterCallback.onRouteListingPreferenceUpdated(TEST_PACKAGE_NAME_2,
routeListingPreference);
mInfoMediaManager.mMediaRouterCallback.onRoutesUpdated();
assertThat(mInfoMediaManager.mMediaDevices).hasSize(1);
assertThat(mInfoMediaManager.mMediaDevices.get(0).getId()).isEqualTo(TEST_ID);
}
private RouteListingPreference setUpPreferenceList(String packageName) {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT",
Build.VERSION_CODES.UPSIDE_DOWN_CAKE);
final List<RouteListingPreference.Item> preferenceItemList = new ArrayList<>();
RouteListingPreference.Item item1 =
new RouteListingPreference.Item.Builder(TEST_ID_4)
.setFlags(RouteListingPreference.Item.FLAG_SUGGESTED)
.build();
RouteListingPreference.Item item2 = new RouteListingPreference.Item.Builder(
TEST_ID_3).build();
preferenceItemList.add(item1);
preferenceItemList.add(item2);
RouteListingPreference routeListingPreference =
new RouteListingPreference.Builder().setItems(
preferenceItemList).setUseSystemOrdering(false).build();
when(mRouterManager.getRouteListingPreference(packageName))
.thenReturn(routeListingPreference);
return routeListingPreference;
}
private void setUpSelectedRoutes(String packageName) {
final List<MediaRoute2Info> selectedRoutes = new ArrayList<>();
final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
when(info.getClientPackageName()).thenReturn(packageName);
when(info.isSystemRoute()).thenReturn(true);
selectedRoutes.add(info);
when(mRouterManager.getSelectedRoutes(any())).thenReturn(selectedRoutes);
}
private List<MediaRoute2Info> setAvailableRoutesList(String packageName) {
final List<MediaRoute2Info> availableRoutes = new ArrayList<>();
final MediaRoute2Info availableInfo1 = mock(MediaRoute2Info.class);
when(availableInfo1.getId()).thenReturn(TEST_ID_2);
when(availableInfo1.getClientPackageName()).thenReturn(packageName);
when(availableInfo1.getType()).thenReturn(TYPE_REMOTE_TV);
availableRoutes.add(availableInfo1);
final MediaRoute2Info availableInfo2 = mock(MediaRoute2Info.class);
when(availableInfo2.getId()).thenReturn(TEST_ID_3);
when(availableInfo2.getClientPackageName()).thenReturn(packageName);
availableRoutes.add(availableInfo2);
final MediaRoute2Info availableInfo3 = mock(MediaRoute2Info.class);
when(availableInfo3.getId()).thenReturn(TEST_ID_4);
when(availableInfo3.getClientPackageName()).thenReturn(packageName);
availableRoutes.add(availableInfo3);
final MediaRoute2Info availableInfo4 = mock(MediaRoute2Info.class);
when(availableInfo4.getId()).thenReturn(TEST_ID_1);
when(availableInfo4.isSystemRoute()).thenReturn(true);
when(availableInfo4.getClientPackageName()).thenReturn(packageName);
availableRoutes.add(availableInfo4);
when(mRouterManager.getAvailableRoutes(packageName)).thenReturn(availableRoutes);
return availableRoutes;
}
@Test
public void hasPreferenceRouteListing_oldSdkVersion_returnsFalse() {
assertThat(mInfoMediaManager.preferRouteListingOrdering()).isFalse();
}
@Test
public void hasPreferenceRouteListing_newSdkVersionWithPreferenceExist_returnsTrue() {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT",
Build.VERSION_CODES.UPSIDE_DOWN_CAKE);
when(mRouterManager.getRouteListingPreference(any())).thenReturn(
new RouteListingPreference.Builder().setItems(
ImmutableList.of()).setUseSystemOrdering(false).build());
mInfoMediaManager.mRouterManager = mRouterManager;
assertThat(mInfoMediaManager.preferRouteListingOrdering()).isTrue();
}
@Test
public void hasPreferenceRouteListing_newSdkVersionWithPreferenceNotExist_returnsFalse() {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT",
Build.VERSION_CODES.UPSIDE_DOWN_CAKE);
when(mRouterManager.getRouteListingPreference(any())).thenReturn(null);
assertThat(mInfoMediaManager.preferRouteListingOrdering()).isFalse();
}
@Test
public void getInAppOnlyItemRoutingReceiver_oldSdkVersion_returnsNull() {
assertThat(mInfoMediaManager.getLinkedItemComponentName()).isNull();
}
@Test
public void getInAppOnlyItemRoutingReceiver_newSdkVersionWithReceiverExist_returns() {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT",
Build.VERSION_CODES.UPSIDE_DOWN_CAKE);
when(mRouterManager.getRouteListingPreference(any())).thenReturn(
new RouteListingPreference.Builder().setItems(
ImmutableList.of()).setUseSystemOrdering(
false).setLinkedItemComponentName(mComponentName).build());
mInfoMediaManager.mRouterManager = mRouterManager;
assertThat(mInfoMediaManager.getLinkedItemComponentName()).isEqualTo(mComponentName);
}
private List<MediaRoute2Info> getRoutesListWithDuplicatedIds() {
final List<MediaRoute2Info> routes = new ArrayList<>();
final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.isSystemRoute()).thenReturn(true);
when(info.getDeduplicationIds()).thenReturn(
Set.of(TEST_DUPLICATED_ID_1, TEST_DUPLICATED_ID_2));
routes.add(info);
final MediaRoute2Info info1 = mock(MediaRoute2Info.class);
when(info1.getId()).thenReturn(TEST_ID_1);
when(info1.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info1.isSystemRoute()).thenReturn(true);
when(info1.getDeduplicationIds()).thenReturn(Set.of(TEST_DUPLICATED_ID_3));
routes.add(info1);
final MediaRoute2Info info2 = mock(MediaRoute2Info.class);
when(info2.getId()).thenReturn(TEST_ID_2);
when(info2.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info2.isSystemRoute()).thenReturn(true);
when(info2.getDeduplicationIds()).thenReturn(Set.of(TEST_DUPLICATED_ID_3));
routes.add(info2);
final MediaRoute2Info info3 = mock(MediaRoute2Info.class);
when(info3.getId()).thenReturn(TEST_ID_3);
when(info3.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info3.isSystemRoute()).thenReturn(true);
when(info3.getDeduplicationIds()).thenReturn(Set.of(TEST_DUPLICATED_ID_1));
routes.add(info3);
final MediaRoute2Info info4 = mock(MediaRoute2Info.class);
when(info4.getId()).thenReturn(TEST_ID_4);
when(info4.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info4.isSystemRoute()).thenReturn(true);
when(info4.getDeduplicationIds()).thenReturn(Set.of(TEST_DUPLICATED_ID_2));
routes.add(info4);
return routes;
}
@Test
public void onRoutesRemoved_getAvailableRoutes_shouldAddMediaDevice() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
final List<String> selectedRoutes = new ArrayList<>();
selectedRoutes.add(TEST_ID);
when(sessionInfo.getSelectedRoutes()).thenReturn(selectedRoutes);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getDeduplicationIds()).thenReturn(Set.of());
final List<MediaRoute2Info> routes = new ArrayList<>();
routes.add(info);
mShadowRouter2Manager.setTransferableRoutes(routes);
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
assertThat(mediaDevice).isNull();
mInfoMediaManager.mMediaRouterCallback.onRoutesUpdated();
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice);
assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size());
}
@Test
public void addDeviceToPlayMedia_containSelectableRoutes_returnTrue() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
final MediaRoute2Info route2Info = mock(MediaRoute2Info.class);
final MediaDevice device = new InfoMediaDevice(mContext, route2Info);
final List<String> list = new ArrayList<>();
list.add(TEST_ID);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getSelectableRoutes()).thenReturn(list);
when(route2Info.getId()).thenReturn(TEST_ID);
when(route2Info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
assertThat(mInfoMediaManager.addDeviceToPlayMedia(device)).isTrue();
}
@Test
public void addDeviceToPlayMedia_notContainSelectableRoutes_returnFalse() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
final MediaRoute2Info route2Info = mock(MediaRoute2Info.class);
final MediaDevice device = new InfoMediaDevice(mContext, route2Info);
final List<String> list = new ArrayList<>();
list.add("fake_id");
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getSelectableRoutes()).thenReturn(list);
when(route2Info.getId()).thenReturn(TEST_ID);
when(route2Info.getName()).thenReturn(TEST_NAME);
when(route2Info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
assertThat(mInfoMediaManager.addDeviceToPlayMedia(device)).isFalse();
}
@Test
public void removeDeviceFromMedia_containSelectedRoutes_returnTrue() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
final MediaRoute2Info route2Info = mock(MediaRoute2Info.class);
final MediaDevice device = new InfoMediaDevice(mContext, route2Info);
final List<String> list = new ArrayList<>();
list.add(TEST_ID);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getSelectedRoutes()).thenReturn(list);
when(route2Info.getId()).thenReturn(TEST_ID);
when(route2Info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
assertThat(mInfoMediaManager.removeDeviceFromPlayMedia(device)).isTrue();
}
@Test
public void removeDeviceFromMedia_notContainSelectedRoutes_returnFalse() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
final MediaRoute2Info route2Info = mock(MediaRoute2Info.class);
final MediaDevice device = new InfoMediaDevice(mContext, route2Info);
final List<String> list = new ArrayList<>();
list.add("fake_id");
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getSelectedRoutes()).thenReturn(list);
when(route2Info.getId()).thenReturn(TEST_ID);
when(route2Info.getName()).thenReturn(TEST_NAME);
when(route2Info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
assertThat(mInfoMediaManager.removeDeviceFromPlayMedia(device)).isFalse();
}
@Test
public void getSelectableMediaDevice_notContainPackageName_returnEmpty() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn("com.fake.packagename");
assertThat(mInfoMediaManager.getSelectableMediaDevices()).isEmpty();
}
@Test
public void getDeselectableMediaDevice_checkList() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
final List<MediaRoute2Info> mediaRoute2Infos = new ArrayList<>();
final MediaRoute2Info mediaRoute2Info = mock(MediaRoute2Info.class);
mediaRoute2Infos.add(mediaRoute2Info);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
mShadowRouter2Manager.setDeselectableRoutes(mediaRoute2Infos);
when(mediaRoute2Info.getName()).thenReturn(TEST_NAME);
when(mediaRoute2Info.getId()).thenReturn(TEST_ID);
final List<MediaDevice> mediaDevices = mInfoMediaManager.getDeselectableMediaDevices();
assertThat(mediaDevices.size()).isEqualTo(1);
assertThat(mediaDevices.get(0).getName()).isEqualTo(TEST_NAME);
}
@Test
public void adjustSessionVolume_routingSessionInfoIsNull_noCrash() {
mInfoMediaManager.adjustSessionVolume(null, 10);
}
@Test
public void getSessionVolumeMax_containPackageName_returnMaxVolume() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
mInfoMediaManager.getSessionVolumeMax();
verify(info).getVolumeMax();
}
@Test
public void getSessionVolume_containPackageName_returnMaxVolume() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
mInfoMediaManager.getSessionVolume();
verify(info).getVolume();
}
@Test
public void getRemoteSessions_returnsRemoteSessions() {
final List<RoutingSessionInfo> infos = new ArrayList<>();
infos.add(mock(RoutingSessionInfo.class));
mShadowRouter2Manager.setRemoteSessions(infos);
assertThat(mInfoMediaManager.getRemoteSessions()).containsExactlyElementsIn(infos);
}
@Test
public void releaseSession_removeSuccessfully_returnTrue() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
assertThat(mInfoMediaManager.releaseSession()).isTrue();
}
@Test
public void getSessionName_containPackageName_returnName() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
routingSessionInfos.add(info);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.getName()).thenReturn(TEST_NAME);
assertThat(mInfoMediaManager.getSessionName()).isEqualTo(TEST_NAME);
}
@Test
public void onTransferFailed_notDispatchOnRequestFailed() {
mInfoMediaManager.registerCallback(mCallback);
mInfoMediaManager.mMediaRouterCallback.onTransferFailed(null, null);
verify(mCallback, never()).onRequestFailed(REASON_UNKNOWN_ERROR);
}
@Test
public void onRequestFailed_shouldDispatchOnRequestFailed() {
mInfoMediaManager.registerCallback(mCallback);
mInfoMediaManager.mMediaRouterCallback.onRequestFailed(REASON_NETWORK_ERROR);
verify(mCallback).onRequestFailed(REASON_NETWORK_ERROR);
}
@Test
public void onTransferred_getAvailableRoutes_shouldAddMediaDevice() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
final List<String> selectedRoutes = new ArrayList<>();
selectedRoutes.add(TEST_ID);
when(sessionInfo.getSelectedRoutes()).thenReturn(selectedRoutes);
mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
final MediaRoute2Info info = mock(MediaRoute2Info.class);
mInfoMediaManager.registerCallback(mCallback);
when(info.getDeduplicationIds()).thenReturn(Set.of());
when(info.getId()).thenReturn(TEST_ID);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
final List<MediaRoute2Info> routes = new ArrayList<>();
routes.add(info);
mShadowRouter2Manager.setTransferableRoutes(routes);
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
assertThat(mediaDevice).isNull();
mInfoMediaManager.mMediaRouterCallback.onTransferred(sessionInfo, sessionInfo);
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(infoDevice);
assertThat(mInfoMediaManager.mMediaDevices).hasSize(routes.size());
verify(mCallback).onConnectedDeviceChanged(TEST_ID);
}
@Test
public void onSessionUpdated_shouldDispatchDeviceListAdded() {
final MediaRoute2Info info = mock(MediaRoute2Info.class);
when(info.getId()).thenReturn(TEST_ID);
when(info.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(info.isSystemRoute()).thenReturn(true);
final List<MediaRoute2Info> routes = new ArrayList<>();
routes.add(info);
mShadowRouter2Manager.setAllRoutes(routes);
mInfoMediaManager.registerCallback(mCallback);
mInfoMediaManager.mMediaRouterCallback.onSessionUpdated(mock(RoutingSessionInfo.class));
verify(mCallback).onDeviceListAdded(any());
}
@Test
public void addMediaDevice_verifyDeviceTypeCanCorrespondToMediaDevice() {
final MediaRoute2Info route2Info = mock(MediaRoute2Info.class);
final CachedBluetoothDeviceManager cachedBluetoothDeviceManager =
mock(CachedBluetoothDeviceManager.class);
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
when(route2Info.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
when(route2Info.getId()).thenReturn(TEST_ID);
mInfoMediaManager.addMediaDevice(route2Info);
assertThat(mInfoMediaManager.mMediaDevices.get(0) instanceof InfoMediaDevice).isTrue();
when(route2Info.getType()).thenReturn(TYPE_USB_DEVICE);
when(route2Info.getId()).thenReturn(TEST_ID);
mInfoMediaManager.mMediaDevices.clear();
mInfoMediaManager.addMediaDevice(route2Info);
assertThat(mInfoMediaManager.mMediaDevices.get(0) instanceof PhoneMediaDevice).isTrue();
when(route2Info.getType()).thenReturn(TYPE_WIRED_HEADSET);
when(route2Info.getId()).thenReturn(TEST_ID);
mInfoMediaManager.mMediaDevices.clear();
mInfoMediaManager.addMediaDevice(route2Info);
assertThat(mInfoMediaManager.mMediaDevices.get(0) instanceof PhoneMediaDevice).isTrue();
when(route2Info.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
when(route2Info.getAddress()).thenReturn("00:00:00:00:00:00");
when(route2Info.getId()).thenReturn(TEST_ID);
when(mLocalBluetoothManager.getCachedDeviceManager())
.thenReturn(cachedBluetoothDeviceManager);
when(cachedBluetoothDeviceManager.findDevice(any(BluetoothDevice.class)))
.thenReturn(cachedDevice);
mInfoMediaManager.mMediaDevices.clear();
mInfoMediaManager.addMediaDevice(route2Info);
assertThat(mInfoMediaManager.mMediaDevices.get(0) instanceof BluetoothMediaDevice).isTrue();
when(route2Info.getType()).thenReturn(TYPE_BUILTIN_SPEAKER);
mInfoMediaManager.mMediaDevices.clear();
mInfoMediaManager.addMediaDevice(route2Info);
assertThat(mInfoMediaManager.mMediaDevices.get(0) instanceof PhoneMediaDevice).isTrue();
}
@Test
public void addMediaDevice_cachedBluetoothDeviceIsNull_shouldNotAdded() {
final MediaRoute2Info route2Info = mock(MediaRoute2Info.class);
final CachedBluetoothDeviceManager cachedBluetoothDeviceManager =
mock(CachedBluetoothDeviceManager.class);
when(route2Info.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
when(route2Info.getAddress()).thenReturn("00:00:00:00:00:00");
when(mLocalBluetoothManager.getCachedDeviceManager())
.thenReturn(cachedBluetoothDeviceManager);
when(cachedBluetoothDeviceManager.findDevice(any(BluetoothDevice.class)))
.thenReturn(null);
mInfoMediaManager.mMediaDevices.clear();
mInfoMediaManager.addMediaDevice(route2Info);
assertThat(mInfoMediaManager.mMediaDevices.size()).isEqualTo(0);
}
@Test
public void addMediaDevice_deviceIncludedInSelectedDevices_shouldSetAsCurrentConnected() {
final MediaRoute2Info route2Info = mock(MediaRoute2Info.class);
final CachedBluetoothDeviceManager cachedBluetoothDeviceManager =
mock(CachedBluetoothDeviceManager.class);
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
routingSessionInfos.add(sessionInfo);
when(mRouterManager.getRoutingSessions(TEST_PACKAGE_NAME)).thenReturn(routingSessionInfos);
when(sessionInfo.getSelectedRoutes()).thenReturn(ImmutableList.of(TEST_ID));
when(route2Info.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
when(route2Info.getAddress()).thenReturn("00:00:00:00:00:00");
when(route2Info.getId()).thenReturn(TEST_ID);
when(mLocalBluetoothManager.getCachedDeviceManager())
.thenReturn(cachedBluetoothDeviceManager);
when(cachedBluetoothDeviceManager.findDevice(any(BluetoothDevice.class)))
.thenReturn(cachedDevice);
mInfoMediaManager.mRouterManager = mRouterManager;
mInfoMediaManager.mMediaDevices.clear();
mInfoMediaManager.addMediaDevice(route2Info);
MediaDevice device = mInfoMediaManager.mMediaDevices.get(0);
assertThat(device instanceof BluetoothMediaDevice).isTrue();
assertThat(device.getState()).isEqualTo(STATE_SELECTED);
assertThat(mInfoMediaManager.getCurrentConnectedDevice()).isEqualTo(device);
}
}

View File

@@ -0,0 +1,608 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.media;
import static android.media.MediaRoute2ProviderService.REASON_UNKNOWN_ERROR;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.media.AudioDeviceAttributes;
import android.media.AudioManager;
import android.media.AudioSystem;
import android.media.MediaRoute2Info;
import android.media.MediaRouter2Manager;
import android.media.RoutingSessionInfo;
import com.android.settingslib.bluetooth.A2dpProfile;
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 com.android.settingslib.bluetooth.LocalBluetoothProfile;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class})
public class LocalMediaManagerTest {
private static final String TEST_DEVICE_NAME_1 = "device_name_1";
private static final String TEST_DEVICE_NAME_2 = "device_name_2";
private static final String TEST_DEVICE_ID_1 = "device_id_1";
private static final String TEST_DEVICE_ID_2 = "device_id_2";
private static final String TEST_DEVICE_ID_3 = "device_id_3";
private static final String TEST_CURRENT_DEVICE_ID = "currentDevice_id";
private static final String TEST_PACKAGE_NAME = "com.test.playmusic";
private static final String TEST_SESSION_ID = "session_id";
private static final String TEST_ADDRESS = "00:01:02:03:04:05";
@Mock
private InfoMediaManager mInfoMediaManager;
@Mock
private LocalBluetoothManager mLocalBluetoothManager;
@Mock
private LocalMediaManager.DeviceCallback mCallback;
@Mock
private HearingAidProfile mHapProfile;
@Mock
private A2dpProfile mA2dpProfile;
@Mock
private LocalBluetoothProfileManager mLocalProfileManager;
@Mock
private MediaRouter2Manager mMediaRouter2Manager;
@Mock
private MediaRoute2Info mRouteInfo1;
@Mock
private MediaRoute2Info mRouteInfo2;
@Mock
private AudioManager mAudioManager;
private Context mContext;
private LocalMediaManager mLocalMediaManager;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private InfoMediaDevice mInfoMediaDevice1;
private InfoMediaDevice mInfoMediaDevice2;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
final List<BluetoothDevice> bluetoothDevices = new ArrayList<>();
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(bluetoothDevices);
when(mRouteInfo1.getName()).thenReturn(TEST_DEVICE_NAME_1);
when(mRouteInfo1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(mRouteInfo2.getName()).thenReturn(TEST_DEVICE_NAME_2);
when(mRouteInfo2.getId()).thenReturn(TEST_DEVICE_ID_2);
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalProfileManager);
when(mLocalProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
when(mLocalProfileManager.getHearingAidProfile()).thenReturn(mHapProfile);
mInfoMediaDevice1 = spy(new InfoMediaDevice(mContext, mRouteInfo1));
mInfoMediaDevice2 = new InfoMediaDevice(mContext, mRouteInfo2);
mLocalMediaManager = new LocalMediaManager(mContext, mLocalBluetoothManager,
mInfoMediaManager, "com.test.packagename");
mLocalMediaManager.mAudioManager = mAudioManager;
}
@Test
public void startScan_mediaDevicesListShouldBeClear() {
final MediaDevice device = mock(MediaDevice.class);
mLocalMediaManager.mMediaDevices.add(device);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(1);
mLocalMediaManager.startScan();
assertThat(mLocalMediaManager.mMediaDevices).isEmpty();
}
@Test
public void connectDevice_deviceNotEqualCurrentConnectedDevice_connectDevice() {
final MediaDevice currentDevice = mock(MediaDevice.class);
final MediaDevice device = mock(MediaDevice.class);
mLocalMediaManager.mMediaDevices.add(currentDevice);
mLocalMediaManager.mMediaDevices.add(device);
mLocalMediaManager.mCurrentConnectedDevice = currentDevice;
when(device.getId()).thenReturn(TEST_DEVICE_ID_1);
when(currentDevice.getId()).thenReturn(TEST_CURRENT_DEVICE_ID);
mLocalMediaManager.registerCallback(mCallback);
assertThat(mLocalMediaManager.connectDevice(device)).isTrue();
verify(mInfoMediaManager).connectToDevice(device);
}
@Test
public void connectDevice_deviceNotEqualCurrentConnectedDevice_isConnectingState() {
mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice1);
mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice2);
mLocalMediaManager.mCurrentConnectedDevice = mInfoMediaDevice1;
mLocalMediaManager.registerCallback(mCallback);
assertThat(mLocalMediaManager.connectDevice(mInfoMediaDevice2)).isTrue();
assertThat(mInfoMediaDevice2.getState()).isEqualTo(LocalMediaManager.MediaDeviceState
.STATE_CONNECTING);
}
@Test
public void connectDevice_deviceEqualCurrentConnectedDevice_notConnectingState() {
mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice1);
mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice2);
mLocalMediaManager.mCurrentConnectedDevice = mInfoMediaDevice1;
mLocalMediaManager.registerCallback(mCallback);
assertThat(mLocalMediaManager.connectDevice(mInfoMediaDevice1)).isFalse();
assertThat(mInfoMediaDevice1.getState()).isNotEqualTo(LocalMediaManager.MediaDeviceState
.STATE_CONNECTING);
}
@Test
public void connectDevice_bluetoothDeviceNotConnected_connectBluetoothDevice() {
final MediaDevice device = mock(BluetoothMediaDevice.class);
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
mLocalMediaManager.mMediaDevices.add(device);
when(device.getId()).thenReturn(TEST_DEVICE_ID_1);
when(((BluetoothMediaDevice) device).getCachedDevice()).thenReturn(cachedDevice);
when(cachedDevice.isConnected()).thenReturn(false);
when(cachedDevice.isBusy()).thenReturn(false);
mLocalMediaManager.registerCallback(mCallback);
assertThat(mLocalMediaManager.connectDevice(device)).isTrue();
verify(cachedDevice).connect();
}
@Test
public void getMediaDeviceById_idExist_shouldReturnMediaDevice() {
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(device2);
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(device2.getId()).thenReturn(TEST_DEVICE_ID_2);
MediaDevice device = mLocalMediaManager.getMediaDeviceById(TEST_DEVICE_ID_2);
assertThat(device.getId()).isEqualTo(TEST_DEVICE_ID_2);
}
@Test
public void getMediaDeviceById_idNotExist_shouldReturnNull() {
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(device2);
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(device2.getId()).thenReturn(TEST_DEVICE_ID_2);
MediaDevice device = mLocalMediaManager.getMediaDeviceById(TEST_CURRENT_DEVICE_ID);
assertThat(device).isNull();
}
@Test
public void getMediaDeviceById_idIsNull_shouldReturnNull() {
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(device2);
when(device1.getId()).thenReturn(null);
when(device2.getId()).thenReturn(null);
MediaDevice device = mLocalMediaManager.getMediaDeviceById(TEST_CURRENT_DEVICE_ID);
assertThat(device).isNull();
}
@Test
public void onDeviceListAdded_addDevicesList() {
final List<MediaDevice> devices = new ArrayList<>();
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
devices.add(device1);
devices.add(device2);
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(device2.getId()).thenReturn(TEST_DEVICE_ID_2);
assertThat(mLocalMediaManager.mMediaDevices).isEmpty();
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(2);
verify(mCallback).onDeviceListUpdate(any());
}
@Test
public void onDeviceListAdded_addDeviceList() {
final List<MediaDevice> devices = new ArrayList<>();
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
final MediaDevice device3 = mock(MediaDevice.class);
devices.add(device1);
devices.add(device2);
mLocalMediaManager.mMediaDevices.add(device3);
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(device2.getId()).thenReturn(TEST_DEVICE_ID_2);
when(device3.getId()).thenReturn(TEST_DEVICE_ID_3);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(1);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(2);
verify(mCallback).onDeviceListUpdate(any());
}
@Test
public void onDeviceListRemoved_removeAll() {
final List<MediaDevice> devices = new ArrayList<>();
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
devices.add(device1);
devices.add(device2);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(device2);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(2);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback
.onDeviceListRemoved(mLocalMediaManager.mMediaDevices);
assertThat(mLocalMediaManager.mMediaDevices).isEmpty();
verify(mCallback).onDeviceListUpdate(any());
}
@Test
public void onDeviceListRemoved_phoneDeviceNotLastDeviceAfterRemoveDeviceList_removeList() {
final List<MediaDevice> devices = new ArrayList<>();
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
final MediaDevice device3 = mock(MediaDevice.class);
devices.add(device1);
devices.add(device3);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(device2);
mLocalMediaManager.mMediaDevices.add(device3);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(3);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onDeviceListRemoved(devices);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(1);
verify(mCallback).onDeviceListUpdate(any());
}
@Test
public void onConnectedDeviceChanged_connectedAndCurrentDeviceAreDifferent_notifyThemChanged() {
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(device2);
mLocalMediaManager.mCurrentConnectedDevice = device1;
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(device2.getId()).thenReturn(TEST_DEVICE_ID_2);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onConnectedDeviceChanged(TEST_DEVICE_ID_2);
assertThat(mLocalMediaManager.getCurrentConnectedDevice()).isEqualTo(device2);
verify(mCallback).onSelectedDeviceStateChanged(device2,
LocalMediaManager.MediaDeviceState.STATE_CONNECTED);
}
@Test
public void onConnectedDeviceChanged_connectedAndCurrentDeviceAreSame_doNothing() {
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(device2);
mLocalMediaManager.mCurrentConnectedDevice = device1;
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(device2.getId()).thenReturn(TEST_DEVICE_ID_2);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onConnectedDeviceChanged(TEST_DEVICE_ID_1);
verify(mCallback, never()).onDeviceAttributesChanged();
}
@Test
public void onConnectedDeviceChanged_isConnectedState() {
mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice1);
mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice2);
mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_DISCONNECTED);
assertThat(mInfoMediaDevice1.getState()).isEqualTo(LocalMediaManager.MediaDeviceState
.STATE_DISCONNECTED);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onConnectedDeviceChanged(TEST_DEVICE_ID_1);
assertThat(mInfoMediaDevice1.getState()).isEqualTo(LocalMediaManager.MediaDeviceState
.STATE_CONNECTED);
}
@Test
public void onConnectedDeviceChanged_nullConnectedDevice_noException() {
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onConnectedDeviceChanged(TEST_DEVICE_ID_2);
}
@Test
public void onDeviceAttributesChanged_failingTransferring_shouldResetState() {
final MediaDevice currentDevice = mock(MediaDevice.class);
final MediaDevice device = mock(BluetoothMediaDevice.class);
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
mLocalMediaManager.mMediaDevices.add(device);
mLocalMediaManager.mMediaDevices.add(currentDevice);
when(device.getId()).thenReturn(TEST_DEVICE_ID_1);
when(currentDevice.getId()).thenReturn(TEST_CURRENT_DEVICE_ID);
when(((BluetoothMediaDevice) device).getCachedDevice()).thenReturn(cachedDevice);
when(cachedDevice.isConnected()).thenReturn(false);
when(cachedDevice.isBusy()).thenReturn(false);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.connectDevice(device);
mLocalMediaManager.mDeviceAttributeChangeCallback.onDeviceAttributesChanged();
verify(device).setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTING_FAILED);
}
@Test
public void onRequestFailed_checkDevicesState() {
mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice1);
mLocalMediaManager.mMediaDevices.add(mInfoMediaDevice2);
mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTING);
mInfoMediaDevice2.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTED);
assertThat(mInfoMediaDevice1.getState()).isEqualTo(LocalMediaManager.MediaDeviceState
.STATE_CONNECTING);
assertThat(mInfoMediaDevice2.getState()).isEqualTo(LocalMediaManager.MediaDeviceState
.STATE_CONNECTED);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onRequestFailed(REASON_UNKNOWN_ERROR);
assertThat(mInfoMediaDevice1.getState()).isEqualTo(LocalMediaManager.MediaDeviceState
.STATE_CONNECTING_FAILED);
assertThat(mInfoMediaDevice2.getState()).isEqualTo(LocalMediaManager.MediaDeviceState
.STATE_CONNECTED);
}
@Test
public void onDeviceAttributesChanged_shouldBeCalled() {
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mDeviceAttributeChangeCallback.onDeviceAttributesChanged();
verify(mCallback).onDeviceAttributesChanged();
}
@Test
public void getActiveMediaSession_verifyCorrectSession() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
when(info.getId()).thenReturn(TEST_SESSION_ID);
routingSessionInfos.add(info);
when(mInfoMediaManager.getRemoteSessions()).thenReturn(routingSessionInfos);
assertThat(mLocalMediaManager.getRemoteRoutingSessions().get(0).getId())
.matches(TEST_SESSION_ID);
}
@Test
public void onDeviceListAdded_haveMutingExpectedDevice_addMutingExpectedDevice() {
final List<MediaDevice> devices = new ArrayList<>();
final MediaDevice device1 = mock(MediaDevice.class);
devices.add(device1);
final List<LocalBluetoothProfile> profiles = new ArrayList<>();
final A2dpProfile a2dpProfile = mock(A2dpProfile.class);
profiles.add(a2dpProfile);
final List<BluetoothDevice> bluetoothDevices = new ArrayList<>();
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
final CachedBluetoothDeviceManager cachedManager = mock(CachedBluetoothDeviceManager.class);
bluetoothDevices.add(bluetoothDevice);
mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(bluetoothDevices);
AudioDeviceAttributes audioDeviceAttributes = new AudioDeviceAttributes(
AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, TEST_ADDRESS);
when(mAudioManager.getMutingExpectedDevice()).thenReturn(audioDeviceAttributes);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(cachedManager);
when(cachedManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice);
when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(cachedDevice.isConnected()).thenReturn(false);
when(cachedDevice.getConnectableProfiles()).thenReturn(profiles);
when(cachedDevice.getDevice()).thenReturn(bluetoothDevice);
when(cachedDevice.getAddress()).thenReturn(TEST_ADDRESS);
when(mA2dpProfile.getActiveDevice()).thenReturn(bluetoothDevice);
when(mHapProfile.getActiveDevices()).thenReturn(new ArrayList<>());
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(device1.getDeviceType()).thenReturn(MediaDevice.MediaDeviceType.TYPE_PHONE_DEVICE);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(0);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(2);
verify(mCallback).onDeviceListUpdate(any());
}
@Test
public void onDeviceListAdded_transferToDisconnectedBluetooth_verifyConnectDevice() {
final List<MediaDevice> devices = new ArrayList<>();
final MediaDevice currentDevice = mock(MediaDevice.class);
final MediaDevice device = mock(BluetoothMediaDevice.class);
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
mLocalMediaManager.mMediaDevices.add(device);
mLocalMediaManager.mMediaDevices.add(currentDevice);
when(device.getId()).thenReturn(TEST_DEVICE_ID_1);
when(currentDevice.getId()).thenReturn(TEST_CURRENT_DEVICE_ID);
when(((BluetoothMediaDevice) device).getCachedDevice()).thenReturn(cachedDevice);
when(cachedDevice.isConnected()).thenReturn(false);
when(cachedDevice.isBusy()).thenReturn(false);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.connectDevice(device);
verify(cachedDevice).connect();
when(device.isConnected()).thenReturn(true);
mLocalMediaManager.mCurrentConnectedDevice = currentDevice;
devices.add(mInfoMediaDevice1);
devices.add(currentDevice);
mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices);
verify(mInfoMediaManager).connectToDevice(mInfoMediaDevice1);
}
@Test
public void onRequestFailed_shouldDispatchOnRequestFailed() {
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onRequestFailed(1);
verify(mCallback).onRequestFailed(1);
}
@Test
public void onDeviceListAdded_bluetoothAdapterIsNull_noDisconnectedDeviceAdded() {
final List<MediaDevice> devices = new ArrayList<>();
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
final MediaDevice device3 = mock(MediaDevice.class);
devices.add(device1);
devices.add(device2);
mLocalMediaManager.mMediaDevices.add(device3);
mShadowBluetoothAdapter = null;
assertThat(mLocalMediaManager.mMediaDevices).hasSize(1);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(2);
verify(mCallback).onDeviceListUpdate(any());
}
@Test
public void adjustSessionVolume_verifyCorrectSessionVolume() {
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
when(info.getId()).thenReturn(TEST_SESSION_ID);
routingSessionInfos.add(info);
when(mInfoMediaManager.getRoutingSessionById(TEST_SESSION_ID)).thenReturn(info);
mLocalMediaManager.adjustSessionVolume(TEST_SESSION_ID, 10);
verify(mInfoMediaManager).adjustSessionVolume(info, 10);
}
@Test
public void updateCurrentConnectedDevice_bluetoothDeviceIsActive_returnBluetoothDevice() {
final BluetoothMediaDevice device1 = mock(BluetoothMediaDevice.class);
final BluetoothMediaDevice device2 = mock(BluetoothMediaDevice.class);
final PhoneMediaDevice phoneDevice = mock(PhoneMediaDevice.class);
final CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
final BluetoothDevice bluetoothDevice1 = mock(BluetoothDevice.class);
final BluetoothDevice bluetoothDevice2 = mock(BluetoothDevice.class);
when(mA2dpProfile.getActiveDevice()).thenReturn(bluetoothDevice2);
when(mHapProfile.getActiveDevices()).thenReturn(new ArrayList<>());
when(device1.getCachedDevice()).thenReturn(cachedDevice1);
when(device2.getCachedDevice()).thenReturn(cachedDevice2);
when(cachedDevice1.getDevice()).thenReturn(bluetoothDevice1);
when(cachedDevice2.getDevice()).thenReturn(bluetoothDevice2);
when(cachedDevice1.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
when(cachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(true);
when(device1.isConnected()).thenReturn(true);
when(device2.isConnected()).thenReturn(true);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(phoneDevice);
mLocalMediaManager.mMediaDevices.add(device2);
assertThat(mLocalMediaManager.updateCurrentConnectedDevice()).isEqualTo(device2);
}
@Test
public void updateCurrentConnectedDevice_phoneDeviceIsActive_returnPhoneDevice() {
final BluetoothMediaDevice device1 = mock(BluetoothMediaDevice.class);
final BluetoothMediaDevice device2 = mock(BluetoothMediaDevice.class);
final PhoneMediaDevice phoneDevice = mock(PhoneMediaDevice.class);
final CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
final BluetoothDevice bluetoothDevice1 = mock(BluetoothDevice.class);
final BluetoothDevice bluetoothDevice2 = mock(BluetoothDevice.class);
when(mA2dpProfile.getActiveDevice()).thenReturn(null);
when(mHapProfile.getActiveDevices()).thenReturn(new ArrayList<>());
when(device1.getCachedDevice()).thenReturn(cachedDevice1);
when(device2.getCachedDevice()).thenReturn(cachedDevice2);
when(cachedDevice1.getDevice()).thenReturn(bluetoothDevice1);
when(cachedDevice2.getDevice()).thenReturn(bluetoothDevice2);
when(cachedDevice1.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
when(cachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
when(device1.isConnected()).thenReturn(true);
when(device2.isConnected()).thenReturn(true);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(phoneDevice);
mLocalMediaManager.mMediaDevices.add(device2);
assertThat(mLocalMediaManager.updateCurrentConnectedDevice()).isEqualTo(phoneDevice);
}
}

View File

@@ -0,0 +1,510 @@
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.media;
import static android.media.MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_REMOTE_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES;
import static android.media.RouteListingPreference.Item.SELECTION_BEHAVIOR_GO_TO_APP;
import static com.android.settingslib.media.MediaDevice.SelectionBehavior.SELECTION_BEHAVIOR_TRANSFER;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.media.MediaRoute2Info;
import android.media.NearbyDevice;
import android.media.RouteListingPreference;
import android.os.Parcel;
import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class MediaDeviceTest {
private static final Comparator<MediaDevice> COMPARATOR = Comparator.naturalOrder();
private static final String DEVICE_ADDRESS_1 = "AA:BB:CC:DD:EE:11";
private static final String DEVICE_ADDRESS_2 = "AA:BB:CC:DD:EE:22";
private static final String DEVICE_ADDRESS_3 = "AA:BB:CC:DD:EE:33";
private static final String DEVICE_NAME_1 = "TestName_1";
private static final String DEVICE_NAME_2 = "TestName_2";
private static final String DEVICE_NAME_3 = "TestName_3";
private static final String ROUTER_ID_1 = "RouterId_1";
private static final String ROUTER_ID_2 = "RouterId_2";
private static final String ROUTER_ID_3 = "RouterId_3";
private static final String TEST_PACKAGE_NAME = "com.test.playmusic";
private final BluetoothClass mHeadreeClass =
createBtClass(BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES);
private final BluetoothClass mCarkitClass =
createBtClass(BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO);
@Mock
private BluetoothDevice mDevice1;
@Mock
private BluetoothDevice mDevice2;
@Mock
private BluetoothDevice mDevice3;
@Mock
private CachedBluetoothDevice mCachedDevice1;
@Mock
private CachedBluetoothDevice mCachedDevice2;
@Mock
private CachedBluetoothDevice mCachedDevice3;
@Mock
private LocalBluetoothManager mLocalBluetoothManager;
@Mock
private MediaRoute2Info mRouteInfo1;
@Mock
private MediaRoute2Info mRouteInfo2;
@Mock
private MediaRoute2Info mRouteInfo3;
@Mock
private MediaRoute2Info mBluetoothRouteInfo1;
@Mock
private MediaRoute2Info mBluetoothRouteInfo2;
@Mock
private MediaRoute2Info mBluetoothRouteInfo3;
@Mock
private MediaRoute2Info mPhoneRouteInfo;
@Mock
private LocalBluetoothProfileManager mProfileManager;
@Mock
private HearingAidProfile mHapProfile;
@Mock
private A2dpProfile mA2dpProfile;
@Mock
private BluetoothDevice mDevice;
private RouteListingPreference.Item mItem;
private BluetoothMediaDevice mBluetoothMediaDevice1;
private BluetoothMediaDevice mBluetoothMediaDevice2;
private BluetoothMediaDevice mBluetoothMediaDevice3;
private Context mContext;
private InfoMediaDevice mInfoMediaDevice1;
private InfoMediaDevice mInfoMediaDevice2;
private InfoMediaDevice mInfoMediaDevice3;
private List<MediaDevice> mMediaDevices = new ArrayList<>();
private PhoneMediaDevice mPhoneMediaDevice;
private BluetoothClass createBtClass(int deviceClass) {
Parcel p = Parcel.obtain();
p.writeInt(deviceClass);
p.setDataPosition(0); // reset position of parcel before passing to constructor
BluetoothClass bluetoothClass = BluetoothClass.CREATOR.createFromParcel(p);
p.recycle();
return bluetoothClass;
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
when(mCachedDevice1.getAddress()).thenReturn(DEVICE_ADDRESS_1);
when(mCachedDevice2.getAddress()).thenReturn(DEVICE_ADDRESS_2);
when(mCachedDevice3.getAddress()).thenReturn(DEVICE_ADDRESS_3);
when(mCachedDevice1.getName()).thenReturn(DEVICE_NAME_1);
when(mCachedDevice2.getName()).thenReturn(DEVICE_NAME_2);
when(mCachedDevice3.getName()).thenReturn(DEVICE_NAME_3);
when(mCachedDevice1.getDevice()).thenReturn(mDevice1);
when(mCachedDevice2.getDevice()).thenReturn(mDevice2);
when(mCachedDevice3.getDevice()).thenReturn(mDevice3);
when(mCachedDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mCachedDevice1.isConnected()).thenReturn(true);
when(mCachedDevice2.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mCachedDevice2.isConnected()).thenReturn(true);
when(mCachedDevice3.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mCachedDevice3.isConnected()).thenReturn(true);
when(mBluetoothRouteInfo1.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
when(mBluetoothRouteInfo2.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
when(mBluetoothRouteInfo3.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
when(mRouteInfo1.getId()).thenReturn(ROUTER_ID_1);
when(mRouteInfo2.getId()).thenReturn(ROUTER_ID_2);
when(mRouteInfo3.getId()).thenReturn(ROUTER_ID_3);
when(mRouteInfo1.getName()).thenReturn(DEVICE_NAME_1);
when(mRouteInfo2.getName()).thenReturn(DEVICE_NAME_2);
when(mRouteInfo3.getName()).thenReturn(DEVICE_NAME_3);
when(mRouteInfo1.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
when(mRouteInfo2.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
when(mRouteInfo3.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
when(mPhoneRouteInfo.getType()).thenReturn(TYPE_BUILTIN_SPEAKER);
when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
when(mProfileManager.getHearingAidProfile()).thenReturn(mHapProfile);
when(mA2dpProfile.getActiveDevice()).thenReturn(mDevice);
mBluetoothMediaDevice1 =
new BluetoothMediaDevice(
mContext, mCachedDevice1, mBluetoothRouteInfo1);
mBluetoothMediaDevice2 =
new BluetoothMediaDevice(
mContext, mCachedDevice2, mBluetoothRouteInfo2);
mBluetoothMediaDevice3 =
new BluetoothMediaDevice(
mContext, mCachedDevice3, mBluetoothRouteInfo3);
mInfoMediaDevice1 = new InfoMediaDevice(mContext, mRouteInfo1);
mInfoMediaDevice2 = new InfoMediaDevice(mContext, mRouteInfo2);
mInfoMediaDevice3 = new InfoMediaDevice(mContext, mRouteInfo3);
mPhoneMediaDevice = new PhoneMediaDevice(mContext, mPhoneRouteInfo);
}
@Test
public void compareTo_carKit_nonCarKitBluetooth_carKitFirst() {
when(mDevice1.getBluetoothClass()).thenReturn(mHeadreeClass);
when(mDevice2.getBluetoothClass()).thenReturn(mCarkitClass);
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice2);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
}
@Test
public void compareTo_differentRange_sortWithRange() {
mBluetoothMediaDevice1.setRangeZone(NearbyDevice.RANGE_FAR);
mBluetoothMediaDevice2.setRangeZone(NearbyDevice.RANGE_CLOSE);
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice2);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
}
@Test
public void compareTo_carKit_info_carKitFirst() {
when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
mMediaDevices.add(mInfoMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
}
@Test
public void compareTo_carKit_phone_phoneFirst() {
when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mPhoneMediaDevice);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice);
}
@Test
public void compareTo_carKitIsDisConnected_nonCarKitBluetooth_nonCarKitBluetoothFirst() {
when(mDevice1.getBluetoothClass()).thenReturn(mHeadreeClass);
when(mDevice2.getBluetoothClass()).thenReturn(mCarkitClass);
when(mCachedDevice2.isConnected()).thenReturn(false);
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice2);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
}
@Test
public void compareTo_lastSelected_others_lastSelectedFirst() {
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice2);
mBluetoothMediaDevice2.setConnectedRecord();
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
}
@Test
public void compareTo_connectionRecord_sortByRecord() {
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice2);
mBluetoothMediaDevice1.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
// Reset last selected record
ConnectionRecordManager.getInstance().setConnectionRecord(mContext, null, 0);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice2);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice1);
}
@Test
public void compareTo_sortByRecord_connectedDeviceFirst() {
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice2);
when(mCachedDevice2.isConnected()).thenReturn(false);
mBluetoothMediaDevice1.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
// Reset last selected record
ConnectionRecordManager.getInstance().setConnectionRecord(mContext, null, 0);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice2);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice2);
}
@Test
public void compareTo_info_bluetooth_bluetoothFirst() {
mMediaDevices.add(mInfoMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
}
@Test
public void compareTo_bluetooth_phone_phoneFirst() {
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mPhoneMediaDevice);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice);
}
@Test
public void compareTo_bluetooth_wiredHeadset_wiredHeadsetFirst() {
final MediaRoute2Info phoneRouteInfo = mock(MediaRoute2Info.class);
when(phoneRouteInfo.getType()).thenReturn(TYPE_WIRED_HEADPHONES);
final PhoneMediaDevice phoneMediaDevice =
new PhoneMediaDevice(mContext, phoneRouteInfo);
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(phoneMediaDevice);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(phoneMediaDevice);
}
@Test
public void compareTo_info_wiredHeadset_wiredHeadsetFirst() {
final MediaRoute2Info phoneRouteInfo = mock(MediaRoute2Info.class);
when(phoneRouteInfo.getType()).thenReturn(TYPE_WIRED_HEADPHONES);
final PhoneMediaDevice phoneMediaDevice =
new PhoneMediaDevice(mContext, phoneRouteInfo);
mMediaDevices.add(mInfoMediaDevice1);
mMediaDevices.add(phoneMediaDevice);
assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(phoneMediaDevice);
}
@Test
public void compareTo_twoInfo_sortByAlphabet() {
mMediaDevices.add(mInfoMediaDevice2);
mMediaDevices.add(mInfoMediaDevice1);
assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice2);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
}
@Test
public void compareTo_twoBluetooth_sortByAlphabet() {
mMediaDevices.add(mBluetoothMediaDevice2);
mMediaDevices.add(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
}
@Test
public void compareTo_sortByAlphabet_connectDeviceFirst() {
mMediaDevices.add(mBluetoothMediaDevice2);
mMediaDevices.add(mBluetoothMediaDevice1);
when(mCachedDevice1.isConnected()).thenReturn(false);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice2);
}
// 1.mInfoMediaDevice1: Last Selected device
// 2.mBluetoothMediaDevice1: CarKit device
// 3.mInfoMediaDevice2: * 2 times usage
// 4.mInfoMediaDevice3: * 1 time usage
// 5.mBluetoothMediaDevice2: * 2 times usage
// 6.mBluetoothMediaDevice3: * 1 time usage
// 7.mPhoneMediaDevice: * 0 time usage
// Order: 7 -> 2 -> 5 -> 6 -> 1 -> 3 -> 4
@Test
public void compareTo_mixedDevices_carKitFirst() {
when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
when(mDevice2.getBluetoothClass()).thenReturn(mHeadreeClass);
when(mDevice3.getBluetoothClass()).thenReturn(mHeadreeClass);
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice2);
mMediaDevices.add(mBluetoothMediaDevice3);
mMediaDevices.add(mInfoMediaDevice1);
mMediaDevices.add(mInfoMediaDevice2);
mMediaDevices.add(mInfoMediaDevice3);
mMediaDevices.add(mPhoneMediaDevice);
mBluetoothMediaDevice3.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
mInfoMediaDevice3.setConnectedRecord();
mInfoMediaDevice2.setConnectedRecord();
mInfoMediaDevice2.setConnectedRecord();
mInfoMediaDevice1.setConnectedRecord();
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice);
assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(2)).isEqualTo(mBluetoothMediaDevice2);
assertThat(mMediaDevices.get(3)).isEqualTo(mBluetoothMediaDevice3);
assertThat(mMediaDevices.get(4)).isEqualTo(mInfoMediaDevice1);
assertThat(mMediaDevices.get(5)).isEqualTo(mInfoMediaDevice2);
assertThat(mMediaDevices.get(6)).isEqualTo(mInfoMediaDevice3);
}
// 1.mInfoMediaDevice1: Last Selected device
// 2.mBluetoothMediaDevice1: CarKit device not connected
// 3.mInfoMediaDevice2: * 2 times usage
// 4.mInfoMediaDevice3: * 1 time usage
// 5.mBluetoothMediaDevice2: * 4 times usage not connected
// 6.mBluetoothMediaDevice3: * 1 time usage
// 7.mPhoneMediaDevice: * 0 time usage
// Order: 7 -> 6 -> 1 -> 3 -> 4 -> 2 -> 5
@Test
public void compareTo_mixedDevices_connectDeviceFirst() {
when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
when(mDevice2.getBluetoothClass()).thenReturn(mHeadreeClass);
when(mDevice3.getBluetoothClass()).thenReturn(mHeadreeClass);
when(mCachedDevice1.isConnected()).thenReturn(false);
when(mCachedDevice2.isConnected()).thenReturn(false);
mMediaDevices.add(mBluetoothMediaDevice1);
mMediaDevices.add(mBluetoothMediaDevice2);
mMediaDevices.add(mBluetoothMediaDevice3);
mMediaDevices.add(mInfoMediaDevice1);
mMediaDevices.add(mInfoMediaDevice2);
mMediaDevices.add(mInfoMediaDevice3);
mMediaDevices.add(mPhoneMediaDevice);
mBluetoothMediaDevice3.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
mBluetoothMediaDevice2.setConnectedRecord();
mInfoMediaDevice3.setConnectedRecord();
mInfoMediaDevice2.setConnectedRecord();
mInfoMediaDevice2.setConnectedRecord();
mInfoMediaDevice1.setConnectedRecord();
Collections.sort(mMediaDevices, COMPARATOR);
assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice);
assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice3);
assertThat(mMediaDevices.get(2)).isEqualTo(mInfoMediaDevice1);
assertThat(mMediaDevices.get(3)).isEqualTo(mInfoMediaDevice2);
assertThat(mMediaDevices.get(4)).isEqualTo(mInfoMediaDevice3);
assertThat(mMediaDevices.get(5)).isEqualTo(mBluetoothMediaDevice1);
assertThat(mMediaDevices.get(6)).isEqualTo(mBluetoothMediaDevice2);
}
@Test
public void getClientPackageName_returnPackageName() {
when(mRouteInfo1.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME);
assertThat(mInfoMediaDevice1.getClientPackageName()).isEqualTo(TEST_PACKAGE_NAME);
}
@Test
public void setState_verifyGetState() {
mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTED);
assertThat(mInfoMediaDevice1.getState()).isEqualTo(
LocalMediaManager.MediaDeviceState.STATE_CONNECTED);
mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTING);
assertThat(mInfoMediaDevice1.getState()).isEqualTo(
LocalMediaManager.MediaDeviceState.STATE_CONNECTING);
mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_DISCONNECTED);
assertThat(mInfoMediaDevice1.getState()).isEqualTo(
LocalMediaManager.MediaDeviceState.STATE_DISCONNECTED);
mInfoMediaDevice1.setState(LocalMediaManager.MediaDeviceState.STATE_CONNECTING_FAILED);
assertThat(mInfoMediaDevice1.getState()).isEqualTo(
LocalMediaManager.MediaDeviceState.STATE_CONNECTING_FAILED);
}
@Test
public void getFeatures_noRouteInfo_returnEmptyList() {
mBluetoothMediaDevice1 =
new BluetoothMediaDevice(
mContext, mCachedDevice1, /* MediaRoute2Info */ null);
assertThat(mBluetoothMediaDevice1.getFeatures().size()).isEqualTo(0);
}
@Test
public void getSelectionBehavior_setItemWithSelectionBehaviorOnSystemRoute_returnTransfer() {
mItem = new RouteListingPreference.Item.Builder(DEVICE_ADDRESS_1)
.setSelectionBehavior(SELECTION_BEHAVIOR_GO_TO_APP)
.build();
mBluetoothMediaDevice1 =
new BluetoothMediaDevice(
mContext,
mCachedDevice1,
null /* MediaRoute2Info */,
mItem);
mPhoneMediaDevice =
new PhoneMediaDevice(mContext, mPhoneRouteInfo, mItem);
assertThat(mBluetoothMediaDevice1.getSelectionBehavior()).isEqualTo(
SELECTION_BEHAVIOR_TRANSFER);
assertThat(mPhoneMediaDevice.getSelectionBehavior()).isEqualTo(
SELECTION_BEHAVIOR_TRANSFER);
}
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.media;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class MediaManagerTest {
private static final String TEST_ID = "test_id";
@Mock
private MediaManager.MediaDeviceCallback mCallback;
@Mock
private MediaDevice mDevice;
private MediaManager mMediaManager;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
when(mDevice.getId()).thenReturn(TEST_ID);
mMediaManager = new MediaManager(mContext, null) {};
}
@Test
public void dispatchDeviceListAdded_registerCallback_shouldDispatchCallback() {
mMediaManager.registerCallback(mCallback);
mMediaManager.dispatchDeviceListAdded();
verify(mCallback).onDeviceListAdded(any());
}
@Test
public void dispatchDeviceListRemoved_registerCallback_shouldDispatchCallback() {
mMediaManager.registerCallback(mCallback);
mMediaManager.dispatchDeviceListRemoved(mMediaManager.mMediaDevices);
verify(mCallback).onDeviceListRemoved(mMediaManager.mMediaDevices);
}
@Test
public void dispatchActiveDeviceChanged_registerCallback_shouldDispatchCallback() {
mMediaManager.registerCallback(mCallback);
mMediaManager.dispatchConnectedDeviceChanged(TEST_ID);
verify(mCallback).onConnectedDeviceChanged(TEST_ID);
}
@Test
public void findMediaDevice_idExist_shouldReturnMediaDevice() {
mMediaManager.mMediaDevices.add(mDevice);
final MediaDevice device = mMediaManager.findMediaDevice(TEST_ID);
assertThat(device.getId()).isEqualTo(mDevice.getId());
}
@Test
public void findMediaDevice_idNotExist_shouldReturnNull() {
mMediaManager.mMediaDevices.add(mDevice);
final MediaDevice device = mMediaManager.findMediaDevice("123");
assertThat(device).isNull();
}
@Test
public void dispatchOnRequestFailed_registerCallback_shouldDispatchCallback() {
mMediaManager.registerCallback(mCallback);
mMediaManager.dispatchOnRequestFailed(1);
verify(mCallback).onRequestFailed(1);
}
}

View File

@@ -0,0 +1,148 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.media;
import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_USB_DEVICE;
import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES;
import static android.media.MediaRoute2Info.TYPE_WIRED_HEADSET;
import static com.android.settingslib.media.PhoneMediaDevice.PHONE_ID;
import static com.android.settingslib.media.PhoneMediaDevice.USB_HEADSET_ID;
import static com.android.settingslib.media.PhoneMediaDevice.WIRED_HEADSET_ID;
import static com.android.settingslib.media.PhoneMediaDevice.getMediaTransferThisDeviceName;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.media.MediaRoute2Info;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import com.android.media.flags.Flags;
import com.android.settingslib.R;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class PhoneMediaDeviceTest {
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
@Mock
private MediaRoute2Info mInfo;
private Context mContext;
private PhoneMediaDevice mPhoneMediaDevice;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPhoneMediaDevice = new PhoneMediaDevice(mContext, mInfo, null);
}
@Test
public void updateSummary_isActiveIsTrue_returnActiveString() {
mPhoneMediaDevice.updateSummary(true);
assertThat(mPhoneMediaDevice.getSummary())
.isEqualTo(mContext.getString(R.string.bluetooth_active_no_battery_level));
}
@Test
public void updateSummary_notActive_returnEmpty() {
mPhoneMediaDevice.updateSummary(false);
assertThat(mPhoneMediaDevice.getSummary()).isEmpty();
}
@Test
public void getDrawableResId_returnCorrectResId() {
when(mInfo.getType()).thenReturn(TYPE_WIRED_HEADPHONES);
assertThat(mPhoneMediaDevice.getDrawableResId()).isEqualTo(R.drawable.ic_headphone);
when(mInfo.getType()).thenReturn(TYPE_WIRED_HEADSET);
assertThat(mPhoneMediaDevice.getDrawableResId()).isEqualTo(R.drawable.ic_headphone);
when(mInfo.getType()).thenReturn(TYPE_BUILTIN_SPEAKER);
assertThat(mPhoneMediaDevice.getDrawableResId()).isEqualTo(R.drawable.ic_smartphone);
}
@Test
public void getName_returnCorrectName() {
final String deviceName = "test_name";
when(mInfo.getType()).thenReturn(TYPE_WIRED_HEADPHONES);
when(mInfo.getName()).thenReturn(deviceName);
assertThat(mPhoneMediaDevice.getName())
.isEqualTo(mContext.getString(R.string.media_transfer_wired_usb_device_name));
when(mInfo.getType()).thenReturn(TYPE_USB_DEVICE);
assertThat(mPhoneMediaDevice.getName())
.isEqualTo(mContext.getString(R.string.media_transfer_wired_usb_device_name));
when(mInfo.getType()).thenReturn(TYPE_BUILTIN_SPEAKER);
assertThat(mPhoneMediaDevice.getName())
.isEqualTo(getMediaTransferThisDeviceName(mContext));
}
@EnableFlags(Flags.FLAG_ENABLE_AUDIO_POLICIES_DEVICE_AND_BLUETOOTH_CONTROLLER)
@Test
public void getId_whenAdvancedWiredRoutingEnabled_returnCorrectId() {
String fakeId = "foo";
when(mInfo.getId()).thenReturn(fakeId);
assertThat(mPhoneMediaDevice.getId()).isEqualTo(fakeId);
}
@DisableFlags(Flags.FLAG_ENABLE_AUDIO_POLICIES_DEVICE_AND_BLUETOOTH_CONTROLLER)
@Test
public void getId_whenAdvancedWiredRoutingDisabled_returnCorrectId() {
when(mInfo.getType()).thenReturn(TYPE_WIRED_HEADPHONES);
assertThat(mPhoneMediaDevice.getId())
.isEqualTo(WIRED_HEADSET_ID);
when(mInfo.getType()).thenReturn(TYPE_USB_DEVICE);
assertThat(mPhoneMediaDevice.getId())
.isEqualTo(USB_HEADSET_ID);
when(mInfo.getType()).thenReturn(TYPE_BUILTIN_SPEAKER);
assertThat(mPhoneMediaDevice.getId())
.isEqualTo(PHONE_ID);
}
}