1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mediaroutertest; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.hardware.display.DisplayManagerGlobal; 22 import android.media.MediaRouter; 23 import android.view.Display; 24 import android.view.DisplayAddress; 25 import android.view.DisplayAdjustments; 26 import android.view.DisplayInfo; 27 28 import androidx.test.filters.SmallTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.Before; 32 import org.junit.BeforeClass; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @RunWith(AndroidJUnit4.class) 37 @SmallTest 38 public class MediaRouteInfoTest { 39 private TestableRouteInfo mRouteInfo; 40 private static Display sWifiDisplay; 41 private static Display sExternalDisplay; 42 private static Display sInternalDisplay; 43 private static final String FAKE_MAC_ADDRESS = "fake MAC address"; 44 45 @BeforeClass setUpOnce()46 public static void setUpOnce() { 47 final DisplayManagerGlobal global = DisplayManagerGlobal.getInstance(); 48 final DisplayInfo wifiInfo = new DisplayInfo(); 49 wifiInfo.flags = Display.FLAG_PRESENTATION; 50 wifiInfo.type = Display.TYPE_WIFI; 51 wifiInfo.address = DisplayAddress.fromMacAddress(FAKE_MAC_ADDRESS); 52 sWifiDisplay = new Display(global, 2, wifiInfo, new DisplayAdjustments()); 53 54 final DisplayInfo externalInfo = new DisplayInfo(); 55 externalInfo.flags = Display.FLAG_PRESENTATION; 56 externalInfo.type = Display.TYPE_EXTERNAL; 57 sExternalDisplay = new Display(global, 3, externalInfo, new DisplayAdjustments()); 58 59 final DisplayInfo internalInfo = new DisplayInfo(); 60 internalInfo.flags = Display.FLAG_PRESENTATION; 61 internalInfo.type = Display.TYPE_INTERNAL; 62 sInternalDisplay = new Display(global, 4, internalInfo, new DisplayAdjustments()); 63 } 64 65 @Before setUp()66 public void setUp() { 67 mRouteInfo = new TestableRouteInfo(); 68 } 69 70 @Test testGetPresentationDisplay_notLiveVideo()71 public void testGetPresentationDisplay_notLiveVideo() { 72 mRouteInfo.setPresentationDisplays(sWifiDisplay); 73 mRouteInfo.mSupportedType = MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY; 74 75 mRouteInfo.updatePresentationDisplay(); 76 77 assertThat(mRouteInfo.getPresentationDisplay()).isNull(); 78 } 79 80 @Test testGetPresentationDisplay_includesLiveVideo()81 public void testGetPresentationDisplay_includesLiveVideo() { 82 mRouteInfo.setPresentationDisplays(sWifiDisplay); 83 mRouteInfo.mSupportedType |= MediaRouter.ROUTE_TYPE_LIVE_AUDIO; 84 85 mRouteInfo.updatePresentationDisplay(); 86 87 assertThat(mRouteInfo.getPresentationDisplay()).isEqualTo(sWifiDisplay); 88 } 89 90 @Test testGetPresentationDisplay_noPresentationDisplay()91 public void testGetPresentationDisplay_noPresentationDisplay() { 92 mRouteInfo.updatePresentationDisplay(); 93 94 assertThat(mRouteInfo.getPresentationDisplay()).isNull(); 95 } 96 97 @Test testGetPresentationDisplay_wifiDisplayOnly()98 public void testGetPresentationDisplay_wifiDisplayOnly() { 99 mRouteInfo.setPresentationDisplays(sWifiDisplay); 100 101 mRouteInfo.updatePresentationDisplay(); 102 103 assertThat(mRouteInfo.getPresentationDisplay()).isEqualTo(sWifiDisplay); 104 } 105 106 @Test testGetPresentationDisplay_externalDisplayOnly()107 public void testGetPresentationDisplay_externalDisplayOnly() { 108 mRouteInfo.setPresentationDisplays(sExternalDisplay); 109 110 mRouteInfo.updatePresentationDisplay(); 111 112 assertThat(mRouteInfo.getPresentationDisplay()).isEqualTo(sExternalDisplay); 113 } 114 115 @Test testGetPresentationDisplay_internalDisplayOnly()116 public void testGetPresentationDisplay_internalDisplayOnly() { 117 mRouteInfo.setPresentationDisplays(sInternalDisplay); 118 119 mRouteInfo.updatePresentationDisplay(); 120 121 assertThat(mRouteInfo.getPresentationDisplay()).isEqualTo(sInternalDisplay); 122 } 123 124 @Test testGetPresentationDisplay_addressNotMatch()125 public void testGetPresentationDisplay_addressNotMatch() { 126 mRouteInfo.setPresentationDisplays(sWifiDisplay); 127 mRouteInfo.mDeviceAddress = "Not match"; 128 129 mRouteInfo.updatePresentationDisplay(); 130 131 assertThat(mRouteInfo.getPresentationDisplay()).isNull(); 132 } 133 134 @Test testGetPresentationDisplay_containsWifiAndExternalDisplays_returnWifiDisplay()135 public void testGetPresentationDisplay_containsWifiAndExternalDisplays_returnWifiDisplay() { 136 mRouteInfo.setPresentationDisplays(sExternalDisplay, sWifiDisplay); 137 138 mRouteInfo.updatePresentationDisplay(); 139 140 assertThat(mRouteInfo.getPresentationDisplay()).isEqualTo(sWifiDisplay); 141 } 142 143 @Test testGetPresentationDisplay_containsExternalAndInternalDisplays_returnExternal()144 public void testGetPresentationDisplay_containsExternalAndInternalDisplays_returnExternal() { 145 mRouteInfo.setPresentationDisplays(sExternalDisplay, sInternalDisplay); 146 147 mRouteInfo.updatePresentationDisplay(); 148 149 assertThat(mRouteInfo.getPresentationDisplay()).isEqualTo(sExternalDisplay); 150 } 151 152 @Test testGetPresentationDisplay_containsAllDisplays_returnWifiDisplay()153 public void testGetPresentationDisplay_containsAllDisplays_returnWifiDisplay() { 154 mRouteInfo.setPresentationDisplays(sExternalDisplay, sInternalDisplay, sWifiDisplay); 155 156 mRouteInfo.updatePresentationDisplay(); 157 158 assertThat(mRouteInfo.getPresentationDisplay()).isEqualTo(sWifiDisplay); 159 } 160 161 private static class TestableRouteInfo extends MediaRouter.RouteInfo { 162 private Display[] mDisplays = new Display[0]; 163 private int mSupportedType = MediaRouter.ROUTE_TYPE_LIVE_VIDEO; 164 private String mDeviceAddress = FAKE_MAC_ADDRESS; 165 private MediaRouter.RouteInfo mDefaultRouteInfo = null; 166 TestableRouteInfo()167 private TestableRouteInfo() { 168 super(null); 169 } 170 setPresentationDisplays(Display ....displays)171 private void setPresentationDisplays(Display ...displays) { 172 mDisplays = new Display[displays.length]; 173 System.arraycopy(displays, 0, mDisplays, 0, displays.length); 174 } 175 176 @Override getAllPresentationDisplays()177 public Display[] getAllPresentationDisplays() { 178 return mDisplays; 179 } 180 181 @Override getSupportedTypes()182 public int getSupportedTypes() { 183 return mSupportedType; 184 } 185 186 @Override getDeviceAddress()187 public String getDeviceAddress() { 188 return mDeviceAddress; 189 } 190 191 @Override getDefaultAudioVideo()192 public MediaRouter.RouteInfo getDefaultAudioVideo() { 193 return mDefaultRouteInfo; 194 } 195 } 196 } 197