1 /* 2 * Copyright (C) 2023 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.tests.sdksandbox.host; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.sdksandbox.hosttestutils.SdkSandboxDeviceSupportedHostRule; 22 23 import com.android.tradefed.device.DeviceNotAvailableException; 24 import com.android.tradefed.targetprep.TargetSetupError; 25 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 26 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 27 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 @RunWith(DeviceJUnit4ClassRunner.class) 35 public class SdkSandboxMetricsHostTest extends BaseHostJUnit4Test { 36 37 @Rule(order = 0) 38 public final SdkSandboxDeviceSupportedHostRule deviceSupportRule = 39 new SdkSandboxDeviceSupportedHostRule(this); 40 41 private static final String TEST_APP_PACKAGE = "com.android.tests.sdksandbox"; 42 private static final String TEST_APP_APK = "SdkSandboxMetricsTestApp.apk"; 43 private static final String TEST_APP_CLASS_NAME = "SdkSandboxMetricsTestApp"; 44 private static final String SECONDARY_TEST_APP_PACKAGE = 45 "com.android.tests.sdksandbox.secondary"; 46 private static final String SECONDARY_TEST_APP_APK = "SdkSandboxMetricsSecondaryTestApp.apk"; 47 private static final String SECONDARY_TEST_APP_CLASS_NAME = "SdkSandboxMetricsSecondaryTestApp"; 48 49 @Before setUp()50 public void setUp() throws DeviceNotAvailableException, TargetSetupError { 51 installPackage(TEST_APP_APK); 52 installPackage(SECONDARY_TEST_APP_APK); 53 } 54 55 @After tearDown()56 public void tearDown() throws DeviceNotAvailableException { 57 uninstallPackage(TEST_APP_PACKAGE); 58 uninstallPackage(SECONDARY_TEST_APP_PACKAGE); 59 } 60 61 @Test testSdkCanAccessSdkSandboxExitReasons()62 public void testSdkCanAccessSdkSandboxExitReasons() throws Exception { 63 runPhase(TEST_APP_PACKAGE, TEST_APP_CLASS_NAME, "testSdkCanAccessSdkSandboxExitReasons"); 64 } 65 66 @Test testSdkCannotAccessOtherSdkSandboxExitReasons()67 public void testSdkCannotAccessOtherSdkSandboxExitReasons() throws Exception { 68 runPhase( 69 SECONDARY_TEST_APP_PACKAGE, 70 SECONDARY_TEST_APP_CLASS_NAME, 71 "startAndCrashSdkSandbox"); 72 runPhase( 73 TEST_APP_PACKAGE, 74 TEST_APP_CLASS_NAME, 75 "testSdkCannotAccessExitReasonsFromOtherSdkSandboxUids"); 76 } 77 78 @Test testAppWithDumpPermissionCanAccessSdkSandboxExitReasons()79 public void testAppWithDumpPermissionCanAccessSdkSandboxExitReasons() throws Exception { 80 runPhase( 81 TEST_APP_PACKAGE, 82 TEST_APP_CLASS_NAME, 83 "testAppWithDumpPermissionCanAccessSdkSandboxExitReasons"); 84 } 85 86 @Test testSdkSandboxCrashGeneratesDropboxReport()87 public void testSdkSandboxCrashGeneratesDropboxReport() throws Exception { 88 runPhase(TEST_APP_PACKAGE, TEST_APP_CLASS_NAME, "testCrashSandboxGeneratesDropboxReport"); 89 } 90 91 /** 92 * Runs the given phase of a test by calling into the device. Throws an exception if the test 93 * phase fails. 94 * 95 * <p>For example, <code>runPhase("testExample");</code> 96 */ runPhase(String packageName, String className, String phase)97 private void runPhase(String packageName, String className, String phase) throws Exception { 98 assertThat(runDeviceTests(packageName, packageName + "." + className, phase)).isTrue(); 99 } 100 } 101