SystemMR2Test: Adopt shell permission for testing @SystemApi

Bug: 183428114
Test: Passed CTS
Change-Id: Ib19a15cdc0f75be7f36ff8f9deddf4a9d9676a3e
diff --git a/tests/tests/media/src/android/media/cts/MediaRouter2Test.java b/tests/tests/media/src/android/media/cts/MediaRouter2Test.java
index 07136d6..d8de395 100644
--- a/tests/tests/media/src/android/media/cts/MediaRouter2Test.java
+++ b/tests/tests/media/src/android/media/cts/MediaRouter2Test.java
@@ -1042,6 +1042,12 @@
     }
 
     @Test
+    public void testGettingSystemMediaRouter2WithoutPermissionThrowsSecurityException() {
+        assertThrows(SecurityException.class,
+                () -> MediaRouter2.getInstance(mContext, mContext.getPackageName()));
+    }
+
+    @Test
     public void markCallbacksAsTested() {
         // Due to CTS coverage tool's bug, it doesn't count the callback methods as tested even if
         // we have tests for them. This method just directly calls those methods so that the tool
diff --git a/tests/tests/media/src/android/media/cts/SystemMediaRouter2Test.java b/tests/tests/media/src/android/media/cts/SystemMediaRouter2Test.java
index 8e09880..8de3f5d 100644
--- a/tests/tests/media/src/android/media/cts/SystemMediaRouter2Test.java
+++ b/tests/tests/media/src/android/media/cts/SystemMediaRouter2Test.java
@@ -37,6 +37,8 @@
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
+import android.Manifest;
+import android.app.UiAutomation;
 import android.content.Context;
 import android.media.AudioManager;
 import android.media.MediaRoute2Info;
@@ -77,6 +79,8 @@
 @NonMediaMainlineTest
 public class SystemMediaRouter2Test {
     private static final String TAG = "SystemMR2Test";
+
+    UiAutomation mUiAutomation;
     Context mContext;
     private MediaRouter2 mSystemRouter2ForCts;
     private MediaRouter2 mAppRouter2;
@@ -107,6 +111,9 @@
     @Before
     public void setUp() throws Exception {
         mContext = InstrumentationRegistry.getTargetContext();
+        mUiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
+        mUiAutomation.adoptShellPermissionIdentity(Manifest.permission.MODIFY_AUDIO_ROUTING);
+
         mExecutor = Executors.newSingleThreadExecutor();
         mAudioManager = (AudioManager) mContext.getSystemService(AUDIO_SERVICE);
         MediaRouter2TestActivity.startActivity(mContext);
@@ -154,6 +161,8 @@
         releaseAllSessions();
         // unregister callbacks
         clearCallbacks();
+
+        mUiAutomation.dropShellPermissionIdentity();
     }
 
     @Test