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