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.SdkSandboxDeviceSupportedHostRule; 24 import android.platform.test.annotations.LargeTest; 25 26 import com.android.modules.utils.build.testing.DeviceSdkLevel; 27 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 28 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 29 30 import org.junit.After; 31 import org.junit.Before; 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @RunWith(DeviceJUnit4ClassRunner.class) 37 public class SdkSandboxMediaHostTest extends BaseHostJUnit4Test { 38 39 @Rule(order = 0) 40 public final SdkSandboxDeviceSupportedHostRule deviceSupportRule = 41 new SdkSandboxDeviceSupportedHostRule(this); 42 43 private static final String TEST_APP_PACKAGE_NAME = "com.android.sdksandbox.cts.app"; 44 private static final String TEST_APP_APK_NAME = "CtsSdkSandboxHostTestApp.apk"; 45 private static final int TIME_OUT_MS = 600_000; 46 private static final long WAIT_AFTER_REBOOT_MS = 10_000; 47 48 private DeviceSdkLevel mDeviceSdkLevel; 49 50 /** 51 * Runs the given phase of a test by calling into the device. Throws an exception if the test 52 * phase fails. 53 * 54 * <p>For example, <code>runPhase("testExample");</code> 55 */ runPhase(String phase)56 private void runPhase(String phase) throws Exception { 57 assertThat( 58 runDeviceTests( 59 TEST_APP_PACKAGE_NAME, 60 TEST_APP_PACKAGE_NAME + ".CtsSdkSandboxMediaTestApp", 61 phase)) 62 .isTrue(); 63 } 64 65 @Before setUp()66 public void setUp() throws Exception { 67 mDeviceSdkLevel = new DeviceSdkLevel(getDevice()); 68 uninstallPackage(TEST_APP_PACKAGE_NAME); 69 } 70 71 @After tearDown()72 public void tearDown() throws Exception { 73 uninstallPackage(TEST_APP_PACKAGE_NAME); 74 } 75 76 @Test testAudioFocus()77 public void testAudioFocus() throws Exception { 78 installPackage(TEST_APP_APK_NAME); 79 runPhase("testAudioFocus"); 80 } 81 82 /** 83 * Test that AppOps permissions for sandbox uids correctly initialised after device reboot. See 84 * b/335809734 for context. Test is meant for V+ devices only - audio focus after reboot was 85 * broken in 24Q2 (last U release) - see b/345381409#comment6 86 */ 87 @Test 88 @LargeTest // Reboot device testAudioFocus_AfterReboot()89 public void testAudioFocus_AfterReboot() throws Exception { 90 assumeTrue("Test is meant for V+ devices only", mDeviceSdkLevel.isDeviceAtLeastV()); 91 installPackage(TEST_APP_APK_NAME); 92 93 getDevice().reboot(); 94 getDevice().waitForBootComplete(TIME_OUT_MS); 95 // Allow 10 seconds after boot to avoid zygote contention. 96 Thread.sleep(WAIT_AFTER_REBOOT_MS); 97 98 // Explicitly update device config to ensure SDK Sandbox is enabled 99 getDevice().executeShellCommand("device_config put adservices disable_sdk_sandbox false"); 100 101 runPhase("testAudioFocus"); 102 } 103 } 104