1 package org.robolectric.shadows; 2 3 import static android.media.MediaRouter.ROUTE_TYPE_LIVE_AUDIO; 4 import static android.media.MediaRouter.ROUTE_TYPE_LIVE_VIDEO; 5 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; 6 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2; 7 import static android.os.Build.VERSION_CODES.N; 8 import static com.google.common.truth.Truth.assertThat; 9 import static org.robolectric.Shadows.shadowOf; 10 11 import android.content.Context; 12 import android.media.MediaRouter; 13 import android.media.MediaRouter.RouteInfo; 14 import androidx.test.core.app.ApplicationProvider; 15 import androidx.test.ext.junit.runners.AndroidJUnit4; 16 import org.junit.Before; 17 import org.junit.Test; 18 import org.junit.runner.RunWith; 19 import org.robolectric.RuntimeEnvironment; 20 import org.robolectric.annotation.Config; 21 22 /** Tests for {@link ShadowMediaRouter}. */ 23 @RunWith(AndroidJUnit4.class) 24 public final class ShadowMediaRouterTest { 25 private MediaRouter mediaRouter; 26 27 @Before 28 public void setUp() throws Exception { 29 mediaRouter = 30 (MediaRouter) 31 ApplicationProvider.getApplicationContext() 32 .getSystemService(Context.MEDIA_ROUTER_SERVICE); 33 } 34 35 @Test 36 public void testAddBluetoothRoute_additionalRouteAvailable() { 37 shadowOf(mediaRouter).addBluetoothRoute(); 38 assertThat(mediaRouter.getRouteCount()).isEqualTo(2); 39 } 40 41 @Test 42 public void testAddBluetoothRoute_bluetoothRouteSelected() { 43 shadowOf(mediaRouter).addBluetoothRoute(); 44 RouteInfo bluetoothRoute = mediaRouter.getRouteAt(1); 45 assertThat(mediaRouter.getSelectedRoute(ROUTE_TYPE_LIVE_AUDIO)).isEqualTo(bluetoothRoute); 46 } 47 48 @Test 49 public void testAddBluetoothRoute_checkBluetoothRouteProperties() { 50 shadowOf(mediaRouter).addBluetoothRoute(); 51 RouteInfo bluetoothRoute = mediaRouter.getRouteAt(1); 52 assertThat(bluetoothRoute.getName()).isEqualTo(ShadowMediaRouter.BLUETOOTH_DEVICE_NAME); 53 } 54 55 @Test 56 @Config(minSdk = JELLY_BEAN_MR2) 57 public void testAddBluetoothRoute_checkBluetoothRouteProperties_apiJbMr2() { 58 shadowOf(mediaRouter).addBluetoothRoute(); 59 RouteInfo bluetoothRoute = mediaRouter.getRouteAt(1); 60 assertThat(bluetoothRoute.getDescription()).isEqualTo("Bluetooth audio"); 61 } 62 63 @Test 64 @Config(minSdk = N) 65 public void testAddBluetoothRoute_checkBluetoothRouteProperties_apiN() { 66 shadowOf(mediaRouter).addBluetoothRoute(); 67 RouteInfo bluetoothRoute = mediaRouter.getRouteAt(1); 68 assertThat(bluetoothRoute.getDeviceType()).isEqualTo(RouteInfo.DEVICE_TYPE_BLUETOOTH); 69 } 70 71 @Test 72 public void testSelectBluetoothRoute_getsSetAsSelectedRoute() { 73 // Although this isn't something faked out by the shadow we should ensure that the Bluetooth 74 // route can be selected after it's been added. 75 shadowOf(mediaRouter).addBluetoothRoute(); 76 RouteInfo bluetoothRoute = mediaRouter.getRouteAt(1); 77 78 mediaRouter.selectRoute(ROUTE_TYPE_LIVE_AUDIO, bluetoothRoute); 79 80 assertThat(mediaRouter.getSelectedRoute(ROUTE_TYPE_LIVE_AUDIO)).isEqualTo(bluetoothRoute); 81 } 82 83 @Test 84 public void testRemoveBluetoothRoute_whenBluetoothSelected_defaultRouteAvailableAndSelected() { 85 shadowOf(mediaRouter).addBluetoothRoute(); 86 87 shadowOf(mediaRouter).removeBluetoothRoute(); 88 89 assertThat(mediaRouter.getRouteCount()).isEqualTo(1); 90 assertThat(mediaRouter.getSelectedRoute(ROUTE_TYPE_LIVE_AUDIO)).isEqualTo(getDefaultRoute()); 91 } 92 93 @Test 94 public void testRemoveBluetoothRoute_whenDefaultSelected_defaultRouteAvailableAndSelected() { 95 shadowOf(mediaRouter).addBluetoothRoute(); 96 RouteInfo bluetoothRoute = mediaRouter.getRouteAt(1); 97 mediaRouter.selectRoute(ROUTE_TYPE_LIVE_AUDIO, bluetoothRoute); 98 99 shadowOf(mediaRouter).removeBluetoothRoute(); 100 101 assertThat(mediaRouter.getRouteCount()).isEqualTo(1); 102 assertThat(mediaRouter.getSelectedRoute(ROUTE_TYPE_LIVE_AUDIO)).isEqualTo(getDefaultRoute()); 103 } 104 105 @Test 106 public void testIsBluetoothRouteSelected_bluetoothRouteNotAdded_returnsFalse() { 107 assertThat(shadowOf(mediaRouter).isBluetoothRouteSelected(ROUTE_TYPE_LIVE_AUDIO)).isFalse(); 108 } 109 110 // Pre-API 18, non-user routes weren't able to be selected. 111 @Test 112 @Config(minSdk = JELLY_BEAN_MR2) 113 public void testIsBluetoothRouteSelected_bluetoothRouteAddedButNotSelected_returnsFalse() { 114 shadowOf(mediaRouter).addBluetoothRoute(); 115 mediaRouter.selectRoute(ROUTE_TYPE_LIVE_AUDIO, getDefaultRoute()); 116 assertThat(shadowOf(mediaRouter).isBluetoothRouteSelected(ROUTE_TYPE_LIVE_AUDIO)).isFalse(); 117 } 118 119 @Test 120 @Config(minSdk = JELLY_BEAN_MR1) 121 public void testIsBluetoothRouteSelected_bluetoothRouteSelectedForDifferentType_returnsFalse() { 122 shadowOf(mediaRouter).addBluetoothRoute(); 123 RouteInfo bluetoothRoute = mediaRouter.getRouteAt(1); 124 125 // Select the Bluetooth route for AUDIO and the default route for AUDIO. 126 mediaRouter.selectRoute(ROUTE_TYPE_LIVE_AUDIO, bluetoothRoute); 127 mediaRouter.selectRoute(ROUTE_TYPE_LIVE_VIDEO, getDefaultRoute()); 128 129 assertThat(shadowOf(mediaRouter).isBluetoothRouteSelected(ROUTE_TYPE_LIVE_VIDEO)).isFalse(); 130 } 131 132 @Test 133 public void testIsBluetoothRouteSelected_bluetoothRouteSelected_returnsTrue() { 134 shadowOf(mediaRouter).addBluetoothRoute(); 135 RouteInfo bluetoothRoute = mediaRouter.getRouteAt(1); 136 mediaRouter.selectRoute(ROUTE_TYPE_LIVE_AUDIO, bluetoothRoute); 137 assertThat(shadowOf(mediaRouter).isBluetoothRouteSelected(ROUTE_TYPE_LIVE_AUDIO)).isTrue(); 138 } 139 140 private RouteInfo getDefaultRoute() { 141 if (RuntimeEnvironment.getApiLevel() >= JELLY_BEAN_MR2) { 142 return mediaRouter.getDefaultRoute(); 143 } 144 return mediaRouter.getRouteAt(0); 145 } 146 } 147