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