1 /*
2  * Copyright (C) 2021 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.appsearch.cts;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
22 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
23 
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import javax.annotation.Nonnull;
30 
31 /**
32  * Test to cover instant app behavior with AppSearch.
33  *
34  * <p>This test is split into two distinct parts: The first part is the test-apps that runs on the
35  * device and interactive with AppSearch. This class is the second part that runs on the host and
36  * triggers tests in the first part for different users.
37  *
38  * <p>To trigger a device test, call runDeviceTestAsUser with a specific the test name and specific
39  * user.
40  *
41  * <p>Unlock your device when testing locally.
42  */
43 @RunWith(DeviceJUnit4ClassRunner.class)
44 public class AppSearchInstantAppTest extends BaseHostJUnit4Test {
45     private static final String TARGET_APK_A = "CtsAppSearchHostTestHelperA.apk";
46     private static final String TARGET_PKG_A = "android.appsearch.app.helper_a";
47     private static final String TEST_CLASS_A = TARGET_PKG_A + ".AppSearchInstantAppTest";
48     private static final long DEFAULT_INSTRUMENTATION_TIMEOUT_MS = 600_000; // 10min
49 
50     private int mPrimaryUserId;
51 
runDeviceTestAsUserInPkgA(@onnull String testMethod, int userId)52     private void runDeviceTestAsUserInPkgA(@Nonnull String testMethod, int userId)
53             throws Exception {
54         assertWithMessage(testMethod + " failed").that(
55                 runDeviceTests(getDevice(), TARGET_PKG_A, TEST_CLASS_A, testMethod, userId,
56                         DEFAULT_INSTRUMENTATION_TIMEOUT_MS)).isTrue();
57     }
58 
59     @Before
setUp()60     public void setUp() throws Exception {
61         mPrimaryUserId = getDevice().getPrimaryUserId();
62         uninstallPackage(TARGET_PKG_A);
63         installPackageAsUser(TARGET_APK_A, /* grantPermission= */true, mPrimaryUserId, "--instant");
64     }
65 
66     @After
tearDown()67     public void tearDown() throws Exception {
68         uninstallPackage(TARGET_PKG_A);
69     }
70 
71     @Test
testInstantAppDoesntHaveAccess()72     public void testInstantAppDoesntHaveAccess() throws Exception {
73         runDeviceTestAsUserInPkgA("testInstantAppDoesntHaveAccess", mPrimaryUserId);
74     }
75 }
76