• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.device.ITestDevice;
22 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
23 import com.android.tradefed.testtype.junit4.DeviceTestRunOptions;
24 
25 import java.util.Map;
26 
27 import javax.annotation.Nonnull;
28 
29 public abstract class AppSearchHostTestBase extends BaseHostJUnit4Test {
30     protected static final String TARGET_APK_A = "CtsAppSearchHostTestHelperA.apk";
31     protected static final String TARGET_PKG_A = "android.appsearch.app.helper_a";
32     protected static final String TEST_CLASS_A = TARGET_PKG_A + ".AppSearchDeviceTest";
33     protected static final String TEST_STORAGE_AUGMENTER_CLASS_A =
34             TARGET_PKG_A + ".AppSearchStorageAugmenterDeviceTest";
35     protected static final String TEST_CONTACTS_INDEXER_CLASS_A =
36             TARGET_PKG_A + ".ContactsIndexerDeviceTest";
37     protected static final String TEST_ENTERPRISE_CONTACTS_CLASS_A =
38             TARGET_PKG_A + ".EnterpriseContactsDeviceTest";
39     protected static final String TARGET_APK_B = "CtsAppSearchHostTestHelperB.apk";
40     protected static final String TARGET_PKG_B = "android.appsearch.app.helper_b";
41     protected static final String TEST_CLASS_B = TARGET_PKG_B + ".AppSearchDeviceTest";
42 
43     protected static final String USER_ID_KEY = "userId";
44 
45     protected static final long DEFAULT_INSTRUMENTATION_TIMEOUT_MS = 600_000; // 10min
46 
runDeviceTestAsUserInPkgA(@onnull String testMethod, int userId)47     protected void runDeviceTestAsUserInPkgA(@Nonnull String testMethod, int userId)
48             throws Exception {
49         assertWithMessage(testMethod + " failed").that(
50                 runDeviceTests(getDevice(), TARGET_PKG_A, TEST_CLASS_A, testMethod, userId,
51                         DEFAULT_INSTRUMENTATION_TIMEOUT_MS)).isTrue();
52     }
53 
runDeviceTestAsUserInPkgA(@onnull String testMethod, int userId, @Nonnull Map<String, String> args)54     protected void runDeviceTestAsUserInPkgA(@Nonnull String testMethod, int userId,
55             @Nonnull Map<String, String> args) throws Exception {
56         DeviceTestRunOptions deviceTestRunOptions = new DeviceTestRunOptions(TARGET_PKG_A)
57                 .setTestClassName(TEST_CLASS_A)
58                 .setTestMethodName(testMethod)
59                 .setMaxInstrumentationTimeoutMs(DEFAULT_INSTRUMENTATION_TIMEOUT_MS)
60                 .setUserId(userId);
61         for (Map.Entry<String, String> entry : args.entrySet()) {
62             deviceTestRunOptions.addInstrumentationArg(entry.getKey(), entry.getValue());
63         }
64         assertWithMessage(testMethod + " failed").that(
65                 runDeviceTests(deviceTestRunOptions)).isTrue();
66     }
67 
runStorageAugmenterDeviceTestAsUserInPkgA(@onnull String testMethod, int userId)68     protected void runStorageAugmenterDeviceTestAsUserInPkgA(@Nonnull String testMethod, int userId)
69             throws Exception {
70         assertWithMessage(testMethod + " failed").that(
71                 runDeviceTests(getDevice(), TARGET_PKG_A, TEST_STORAGE_AUGMENTER_CLASS_A,
72                         testMethod, userId, DEFAULT_INSTRUMENTATION_TIMEOUT_MS)).isTrue();
73     }
74 
runContactsIndexerDeviceTestAsUserInPkgA(@onnull String testMethod, int userId, @Nonnull Map<String, String> args)75     protected void runContactsIndexerDeviceTestAsUserInPkgA(@Nonnull String testMethod, int userId,
76             @Nonnull Map<String, String> args) throws Exception {
77         DeviceTestRunOptions deviceTestRunOptions = new DeviceTestRunOptions(TARGET_PKG_A)
78                 .setTestClassName(TEST_CONTACTS_INDEXER_CLASS_A)
79                 .setTestMethodName(testMethod)
80                 .setMaxInstrumentationTimeoutMs(DEFAULT_INSTRUMENTATION_TIMEOUT_MS)
81                 .setUserId(userId);
82         for (Map.Entry<String, String> entry : args.entrySet()) {
83             deviceTestRunOptions.addInstrumentationArg(entry.getKey(), entry.getValue());
84         }
85         assertWithMessage(testMethod + " failed").that(
86                 runDeviceTests(deviceTestRunOptions)).isTrue();
87     }
88 
runEnterpriseContactsDeviceTestAsUserInPkgA(@onnull String testMethod, int userId, @Nonnull Map<String, String> args)89     protected void runEnterpriseContactsDeviceTestAsUserInPkgA(@Nonnull String testMethod,
90             int userId, @Nonnull Map<String, String> args) throws Exception {
91         DeviceTestRunOptions deviceTestRunOptions = new DeviceTestRunOptions(TARGET_PKG_A)
92                 .setTestClassName(TEST_ENTERPRISE_CONTACTS_CLASS_A)
93                 .setTestMethodName(testMethod)
94                 .setMaxInstrumentationTimeoutMs(DEFAULT_INSTRUMENTATION_TIMEOUT_MS)
95                 .setUserId(userId);
96         for (Map.Entry<String, String> entry : args.entrySet()) {
97             deviceTestRunOptions.addInstrumentationArg(entry.getKey(), entry.getValue());
98         }
99         assertWithMessage(testMethod + " failed").that(
100                 runDeviceTests(deviceTestRunOptions)).isTrue();
101     }
102 
runDeviceTestAsUserInPkgB(@onnull String testMethod, int userId)103     protected void runDeviceTestAsUserInPkgB(@Nonnull String testMethod, int userId)
104             throws Exception {
105         assertWithMessage(testMethod + " failed").that(
106                 runDeviceTests(getDevice(), TARGET_PKG_B, TEST_CLASS_B, testMethod, userId,
107                         DEFAULT_INSTRUMENTATION_TIMEOUT_MS)).isTrue();
108     }
109 
rebootAndWaitUntilReady()110     protected void rebootAndWaitUntilReady() throws Exception {
111         rebootAndWaitUntilReady(getDevice());
112     }
113 
rebootAndWaitUntilReady(ITestDevice device)114     protected static void rebootAndWaitUntilReady(ITestDevice device) throws Exception {
115         if (device.getBooleanProperty("init.userspace_reboot.is_supported", false)) {
116             // Soft reboot, reboots only userspace part of device. It waits for device to be
117             // available.
118             device.rebootUserspace();
119         } else {
120             device.reboot(); // reboot() waits for device available
121         }
122     }
123 }
124