1 /* 2 * Copyright (C) 2014 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.appsecurity.cts; 18 19 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner; 20 import com.android.ddmlib.testrunner.TestResult.TestStatus; 21 import com.android.tradefed.device.DeviceNotAvailableException; 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.result.CollectingTestListener; 24 import com.android.tradefed.result.TestDescription; 25 import com.android.tradefed.result.TestResult; 26 import com.android.tradefed.result.TestRunResult; 27 28 import java.util.Arrays; 29 import java.util.Map; 30 import java.util.concurrent.TimeUnit; 31 32 public class Utils { 33 public static final int USER_SYSTEM = 0; 34 runDeviceTests(ITestDevice device, String packageName, String testClassName, String testMethodName)35 public static void runDeviceTests(ITestDevice device, String packageName, String testClassName, 36 String testMethodName) throws DeviceNotAvailableException { 37 runDeviceTests(device, packageName, testClassName, testMethodName, USER_SYSTEM, null); 38 } 39 runDeviceTests(ITestDevice device, String packageName, String testClassName, String testMethodName, Map<String, String> testArgs)40 public static void runDeviceTests(ITestDevice device, String packageName, String testClassName, 41 String testMethodName, Map<String, String> testArgs) 42 throws DeviceNotAvailableException { 43 runDeviceTests(device, packageName, testClassName, testMethodName, USER_SYSTEM, testArgs); 44 } 45 runDeviceTests(ITestDevice device, String packageName, String testClassName, String testMethodName, int userId)46 public static void runDeviceTests(ITestDevice device, String packageName, String testClassName, 47 String testMethodName, int userId) throws DeviceNotAvailableException { 48 runDeviceTests(device, packageName, testClassName, testMethodName, userId, null); 49 } 50 runDeviceTests(ITestDevice device, String packageName, String testClassName, String testMethodName, int userId, Map<String, String> testArgs)51 public static void runDeviceTests(ITestDevice device, String packageName, String testClassName, 52 String testMethodName, int userId, Map<String, String> testArgs) 53 throws DeviceNotAvailableException { 54 // 60 min timeout per test by default 55 runDeviceTests(device, packageName, testClassName, testMethodName, userId, testArgs, 56 60L, TimeUnit.MINUTES); 57 } 58 runDeviceTests(ITestDevice device, String packageName, String testClassName, String testMethodName, int userId, Map<String, String> testArgs, long timeout, TimeUnit unit)59 public static void runDeviceTests(ITestDevice device, String packageName, String testClassName, 60 String testMethodName, int userId, Map<String, String> testArgs, long timeout, 61 TimeUnit unit) 62 throws DeviceNotAvailableException { 63 if (testClassName != null && testClassName.startsWith(".")) { 64 testClassName = packageName + testClassName; 65 } 66 RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(packageName, 67 "android.support.test.runner.AndroidJUnitRunner", device.getIDevice()); 68 // timeout_msec is the timeout per test for instrumentation 69 testRunner.addInstrumentationArg("timeout_msec", Long.toString(unit.toMillis(timeout))); 70 if (testClassName != null && testMethodName != null) { 71 testRunner.setMethodName(testClassName, testMethodName); 72 } else if (testClassName != null) { 73 testRunner.setClassName(testClassName); 74 } 75 76 if (testArgs != null && testArgs.size() > 0) { 77 for (String name : testArgs.keySet()) { 78 final String value = testArgs.get(name); 79 testRunner.addInstrumentationArg(name, value); 80 } 81 } 82 final CollectingTestListener listener = new CollectingTestListener(); 83 device.runInstrumentationTestsAsUser(testRunner, userId, listener); 84 85 final TestRunResult result = listener.getCurrentRunResults(); 86 if (result.isRunFailure()) { 87 throw new AssertionError("Failed to successfully run device tests for " 88 + result.getName() + ": " + result.getRunFailureMessage()); 89 } 90 if (result.getNumTests() == 0) { 91 throw new AssertionError("No tests were run on the device"); 92 } 93 if (result.hasFailedTests()) { 94 // build a meaningful error message 95 StringBuilder errorBuilder = new StringBuilder("on-device tests failed:\n"); 96 for (Map.Entry<TestDescription, TestResult> resultEntry : 97 result.getTestResults().entrySet()) { 98 if (!resultEntry.getValue().getStatus().equals(TestStatus.PASSED)) { 99 errorBuilder.append(resultEntry.getKey().toString()); 100 errorBuilder.append(":\n"); 101 errorBuilder.append(resultEntry.getValue().getStackTrace()); 102 } 103 } 104 throw new AssertionError(errorBuilder.toString()); 105 } 106 } 107 108 /** 109 * Prepare and return a single user relevant for testing. 110 */ prepareSingleUser(ITestDevice device)111 public static int[] prepareSingleUser(ITestDevice device) 112 throws DeviceNotAvailableException { 113 return prepareMultipleUsers(device, 1); 114 } 115 116 /** 117 * Prepare and return two users relevant for testing. 118 */ prepareMultipleUsers(ITestDevice device)119 public static int[] prepareMultipleUsers(ITestDevice device) 120 throws DeviceNotAvailableException { 121 return prepareMultipleUsers(device, 2); 122 } 123 124 /** 125 * Prepare and return multiple users relevant for testing. 126 */ prepareMultipleUsers(ITestDevice device, int maxUsers)127 public static int[] prepareMultipleUsers(ITestDevice device, int maxUsers) 128 throws DeviceNotAvailableException { 129 final int[] userIds = getAllUsers(device); 130 for (int i = 1; i < userIds.length; i++) { 131 if (i < maxUsers) { 132 device.startUser(userIds[i]); 133 } else { 134 device.stopUser(userIds[i]); 135 } 136 } 137 if (userIds.length > maxUsers) { 138 return Arrays.copyOf(userIds, maxUsers); 139 } else { 140 return userIds; 141 } 142 } 143 getAllUsers(ITestDevice device)144 public static int[] getAllUsers(ITestDevice device) 145 throws DeviceNotAvailableException { 146 Integer primary = device.getPrimaryUserId(); 147 if (primary == null) { 148 primary = USER_SYSTEM; 149 } 150 int[] users = new int[] { primary }; 151 for (Integer user : device.listUsers()) { 152 if ((user != USER_SYSTEM) && (user != primary)) { 153 users = Arrays.copyOf(users, users.length + 1); 154 users[users.length - 1] = user; 155 } 156 } 157 return users; 158 } 159 } 160