• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.junit.Assume.assumeFalse;
30 
31 import android.content.Intent;
32 import android.content.pm.PackageManager;
33 import android.os.Bundle;
34 
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 
37 import com.android.compatibility.common.util.UserHelper;
38 
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 
44 @RunWith(AndroidJUnit4.class)
45 public class MediaSessionEnumerationTests extends AppEnumerationTestsBase {
46 
47     private UserHelper mUserHelper = new UserHelper();
48 
49     @Before
onBefore()50     public void onBefore() {
51         // Skip tests for visible background users.
52         // TODO(b/380297485): Enable the tests back when notification listener fully supports
53         // visible background users.
54         assumeFalse(mUserHelper.isVisibleBackgroundUser());
55         setAllowGetActiveSession(TARGET_NO_API, true);
56     }
57 
58     @After
onAfter()59     public void onAfter() {
60         setAllowGetActiveSession(TARGET_NO_API, false);
61     }
62 
63     @Test
queriesPackage_isTrustedForMediaControl_canSeeTarget()64     public void queriesPackage_isTrustedForMediaControl_canSeeTarget()
65             throws Exception {
66         assertThat(isTrustedForMediaControl(QUERIES_PACKAGE, TARGET_NO_API), is(true));
67     }
68 
69     @Test
queriesNothing_isTrustedForMediaControl_cannotSeeTarget()70     public void queriesNothing_isTrustedForMediaControl_cannotSeeTarget()
71             throws Exception {
72         assertThat(isTrustedForMediaControl(QUERIES_NOTHING, TARGET_NO_API), is(false));
73     }
74 
isTrustedForMediaControl(String sourcePackageName, String targetPackageName)75     private boolean isTrustedForMediaControl(String sourcePackageName,
76             String targetPackageName) throws Exception {
77         final int targetUid = sPm.getPackageUid(
78                 targetPackageName, PackageManager.PackageInfoFlags.of(0));
79         final Bundle bundle = sendCommand(
80                 sourcePackageName,
81                 targetPackageName,
82                 targetUid,
83                 null /* intentExtra */,
84                 ACTION_MEDIA_SESSION_MANAGER_IS_TRUSTED_FOR_MEDIA_CONTROL,
85                 false /* waitForReady */)
86                 .await();
87         return bundle.getBoolean(Intent.EXTRA_RETURN_RESULT);
88     }
89 
setAllowGetActiveSession(String packageName, boolean allow)90     private static void setAllowGetActiveSession(String packageName, boolean allow) {
91         final StringBuilder cmd = new StringBuilder("cmd notification ");
92         if (allow) {
93             cmd.append("allow_listener ");
94         } else {
95             cmd.append("disallow_listener ");
96         }
97         cmd.append(packageName).append("/").append(ACTIVITY_CLASS_DUMMY_ACTIVITY).append(" ");
98         cmd.append(sContext.getUserId());
99         runShellCommand(cmd.toString());
100     }
101 }
102