1 /*
2  * Copyright (C) 2024 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.sdksandbox.cts.host;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assume.assumeTrue;
22 
23 import android.app.sdksandbox.hosttestutils.DeviceSupportHostUtils;
24 import android.platform.test.annotations.LargeTest;
25 
26 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
27 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
28 
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 @RunWith(DeviceJUnit4ClassRunner.class)
35 public class SdkSandboxMediaHostTest extends BaseHostJUnit4Test {
36 
37     private static final String TEST_APP_PACKAGE_NAME = "com.android.sdksandbox.cts.app";
38     private static final String TEST_APP_APK_NAME = "CtsSdkSandboxHostTestApp.apk";
39     public static final int TIME_OUT = 600_000;
40 
41     private final DeviceSupportHostUtils mDeviceSupportUtils = new DeviceSupportHostUtils(this);
42 
43     /**
44      * Runs the given phase of a test by calling into the device. Throws an exception if the test
45      * phase fails.
46      *
47      * <p>For example, <code>runPhase("testExample");</code>
48      */
runPhase(String phase)49     private void runPhase(String phase) throws Exception {
50         assertThat(
51                         runDeviceTests(
52                                 TEST_APP_PACKAGE_NAME,
53                                 TEST_APP_PACKAGE_NAME + ".CtsSdkSandboxMediaTestApp",
54                                 phase))
55                 .isTrue();
56     }
57 
58     @Before
setUp()59     public void setUp() throws Exception {
60         assumeTrue("Device supports SdkSandbox", mDeviceSupportUtils.isSdkSandboxSupported());
61         uninstallPackage(TEST_APP_PACKAGE_NAME);
62     }
63 
64     @After
tearDown()65     public void tearDown() throws Exception {
66         uninstallPackage(TEST_APP_PACKAGE_NAME);
67     }
68 
69     @Test
testAudioFocus()70     public void testAudioFocus() throws Exception {
71         installPackage(TEST_APP_APK_NAME);
72         runPhase("testAudioFocus");
73     }
74 
75     /**
76      * Test that AppOps permissions for sandbox uids correctly initialised after device reboot. See
77      * b/335809734 for context.
78      */
79     @Test
80     @LargeTest // Reboot device
testAudioFocus_AfterReboot()81     public void testAudioFocus_AfterReboot() throws Exception {
82         installPackage(TEST_APP_APK_NAME);
83 
84         getDevice().reboot();
85         getDevice().waitForBootComplete(TIME_OUT);
86         // Explicitly update device config to ensure SDK Sandbox is enabled
87         getDevice().executeShellCommand("device_config put adservices disable_sdk_sandbox false");
88 
89         runPhase("testAudioFocus");
90     }
91 }
92