1 /* 2 * Copyright (C) 2022 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 android.appenumeration.cts; 18 19 import static android.appenumeration.cts.Constants.ACTION_MEDIA_SESSION_MANAGER_IS_TRUSTED_FOR_MEDIA_CONTROL; 20 import static android.appenumeration.cts.Constants.ACTIVITY_CLASS_DUMMY_ACTIVITY; 21 import static android.appenumeration.cts.Constants.QUERIES_NOTHING; 22 import static android.appenumeration.cts.Constants.QUERIES_PACKAGE; 23 import static android.appenumeration.cts.Constants.TARGET_NO_API; 24 25 import static com.android.compatibility.common.util.ShellUtils.runShellCommand; 26 27 import static org.hamcrest.MatcherAssert.assertThat; 28 import static org.hamcrest.core.Is.is; 29 30 import android.content.Intent; 31 import android.content.pm.PackageManager; 32 import android.os.Bundle; 33 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 @RunWith(AndroidJUnit4.class) 42 public class MediaSessionEnumerationTests extends AppEnumerationTestsBase { 43 44 @Before onBefore()45 public void onBefore() { 46 setAllowGetActiveSession(TARGET_NO_API, true); 47 } 48 49 @After onAfter()50 public void onAfter() { 51 setAllowGetActiveSession(TARGET_NO_API, false); 52 } 53 54 @Test queriesPackage_isTrustedForMediaControl_canSeeTarget()55 public void queriesPackage_isTrustedForMediaControl_canSeeTarget() 56 throws Exception { 57 assertThat(isTrustedForMediaControl(QUERIES_PACKAGE, TARGET_NO_API), is(true)); 58 } 59 60 @Test queriesNothing_isTrustedForMediaControl_cannotSeeTarget()61 public void queriesNothing_isTrustedForMediaControl_cannotSeeTarget() 62 throws Exception { 63 assertThat(isTrustedForMediaControl(QUERIES_NOTHING, TARGET_NO_API), is(false)); 64 } 65 isTrustedForMediaControl(String sourcePackageName, String targetPackageName)66 private boolean isTrustedForMediaControl(String sourcePackageName, 67 String targetPackageName) throws Exception { 68 final int targetUid = sPm.getPackageUid( 69 targetPackageName, PackageManager.PackageInfoFlags.of(0)); 70 final Bundle bundle = sendCommand( 71 sourcePackageName, 72 targetPackageName, 73 targetUid, 74 null /* intentExtra */, 75 ACTION_MEDIA_SESSION_MANAGER_IS_TRUSTED_FOR_MEDIA_CONTROL, 76 false /* waitForReady */) 77 .await(); 78 return bundle.getBoolean(Intent.EXTRA_RETURN_RESULT); 79 } 80 setAllowGetActiveSession(String packageName, boolean allow)81 private static void setAllowGetActiveSession(String packageName, boolean allow) { 82 final StringBuilder cmd = new StringBuilder("cmd notification "); 83 if (allow) { 84 cmd.append("allow_listener "); 85 } else { 86 cmd.append("disallow_listener "); 87 } 88 cmd.append(packageName).append("/").append(ACTIVITY_CLASS_DUMMY_ACTIVITY).append(" "); 89 runShellCommand(cmd.toString()); 90 } 91 } 92