aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed/testtype/UiAutomatorTest.java
blob: 2394d651ccf46006c93e01a409d7e39da49bf16b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tradefed.testtype;

import com.android.ddmlib.FileListingService;
import com.android.ddmlib.testrunner.IRemoteAndroidTestRunner;
import com.android.ddmlib.testrunner.ITestRunListener;
import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
import com.android.ddmlib.testrunner.TestIdentifier;
import com.android.tradefed.config.Option;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.IFileEntry;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.result.FileInputStreamSource;
import com.android.tradefed.result.ITestInvocationListener;
import com.android.tradefed.result.InputStreamSource;
import com.android.tradefed.result.LogDataType;
import com.android.tradefed.util.FileUtil;
import com.android.tradefed.util.IRunUtil;
import com.android.tradefed.util.RunUtil;
import com.android.tradefed.util.StreamUtil;
import com.android.tradefed.util.ZipUtil;

import junit.framework.Assert;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class UiAutomatorTest implements IRemoteTest, IDeviceTest {

    public enum LoggingOption {
        AFTER_TEST,
        AFTER_FAILURE,
        OFF,
    }

    public enum TestFailureAction {
        BUGREPORT,
        SCREENSHOT,
        BUGREPORT_AND_SCREENSHOT,
    }

    private static final String SHELL_EXE_BASE = "/data/local/tmp/";
    private static final String TRACE_ITERATIONS = "traceIterations";
    private static final String TRACE_DEST_DIRECTORY = "destDirectory";

    private ITestDevice mDevice = null;
    private IRemoteAndroidTestRunner mRunner = null;
    protected Collection<ITestRunListener> mListeners = new ArrayList<ITestRunListener>();

    @Option(name = "jar-path", description = "path to jars containing UI Automator test cases and"
            + " dependencies; May be repeated. " +
            "If unspecified will use all jars found in /data/local/tmp/")
    private List<String> mJarPaths = new ArrayList<String>();

    @Option(name = "class",
            description = "test class to run, may be repeated; multiple classess will be run"
                    + " in the same order as provided in command line")
    private List<String> mClasses = new ArrayList<String>();

    @Option(name = "sync-time", description = "time to allow for initial sync, in ms")
    private long mSyncTime = 0;

    @Option(name = "run-arg",
            description = "Additional test specific arguments to provide.")
    private Map<String, String> mArgMap = new LinkedHashMap<String, String>();

    @Option(name = "timeout",
            description = "Aborts the test run if any test takes longer than the specified number "
                    + "of milliseconds. For no timeout, set to 0.")
    private int mTestTimeout = 30 * 60 * 1000;  // default to 30 minutes

    @Option(name = "capture-logs", description =
            "capture bugreport and screenshot as specified.")
    private LoggingOption mLoggingOption = LoggingOption.AFTER_FAILURE;

    @Option(name = "runner-path", description = "path to uiautomator runner; may be null and "
            + "default will be used in this case")
    private String mRunnerPath = null;

    @Option(name = "on-test-failure",
            description = "sets the action to perform if a test fails")
    private TestFailureAction mFailureAction = TestFailureAction.BUGREPORT_AND_SCREENSHOT;

    @Option(name = "ignore-sighup",
            description = "allows uiautomator test to ignore SIGHUP signal")
    private boolean mIgnoreSighup = false;

    @Option(name = "run-name",
            description = "the run name to use when reporting test results.")
    private String mRunName = "uiautomator";

    @Option(name = "instrumentation",
            description = "the specified test should be driven with instrumentation."
            + "jar-path, runner-path, ignore-sighup are ignored when this is set.")
    private boolean mInstrumentation = false;

    @Option(name = "package",
            description = "The manifest package name of the UI test package."
            + "Only applies when 'instrumentation' option is set.")
    private String mPackage = null;

    @Option(name = "runner",
            description="The instrumentation based test runner class name to use."
            + "Only applies when 'instrumentation' option is set.")
    private String mRunnerName =
        "android.support.test.uiautomator.UiAutomatorInstrumentationTestRunner";


    /**
     * {@inheritDoc}
     */
    @Override
    public void setDevice(ITestDevice device) {
        mDevice = device;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ITestDevice getDevice() {
        return mDevice;
    }

    public void setLoggingOption(LoggingOption loggingOption) {
        mLoggingOption = loggingOption;
    }

    /**
     * @deprecated use {@link #setLoggingOption(LoggingOption)} instead.
     * <p/>
     * Retained for compatibility with cts-tradefed
     */
    @Deprecated
    public void setCaptureLogs(boolean captureLogs) {
        if (captureLogs) {
            setLoggingOption(LoggingOption.AFTER_FAILURE);
        } else {
            setLoggingOption(LoggingOption.OFF);
        }
    }

    public void setRunName(String runName) {
        mRunName = runName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
        mListeners.add(listener);
        if (!isInstrumentationTest()) {
            buildJarPaths();
        }
        mRunner = createTestRunner();
        if (!mClasses.isEmpty()) {
            getTestRunner().setClassNames(mClasses.toArray(new String[]{}));
        }
        getTestRunner().setRunName(mRunName);
        preTestSetup();
        getRunUtil().sleep(getSyncTime());
        getTestRunner().setMaxTimeToOutputResponse(mTestTimeout, TimeUnit.MILLISECONDS);
        for (Map.Entry<String, String> entry : getTestRunArgMap().entrySet()) {
            getTestRunner().addInstrumentationArg(entry.getKey(), entry.getValue());
        }
        if (!isInstrumentationTest()) {
            ((UiAutomatorRunner)getTestRunner()).setIgnoreSighup(mIgnoreSighup);
        }
        if (mLoggingOption != LoggingOption.OFF) {
            mListeners.add(new LoggingWrapper(listener));
            getDevice().runInstrumentationTests(getTestRunner(), mListeners);
        } else {
            getDevice().runInstrumentationTests(getTestRunner(), mListeners);
        }

        if (getTestRunArgMap().containsKey(TRACE_ITERATIONS) &&
                getTestRunArgMap().containsKey(TRACE_DEST_DIRECTORY)) {
            try {
                logTraceFiles(listener, getTestRunArgMap().get(TRACE_DEST_DIRECTORY));
            } catch (IOException e) {
                CLog.e(e);
            }
        }

    }

    protected IRemoteAndroidTestRunner createTestRunner() {
        if (isInstrumentationTest()) {
            if (mPackage == null) {
                throw new IllegalArgumentException("package name has not been set");
            }
            IRemoteAndroidTestRunner runner = new RemoteAndroidTestRunner(mPackage, mRunnerName,
                    getDevice().getIDevice());
            return runner;
        } else {
            return new UiAutomatorRunner(getDevice().getIDevice(),
                    getTestJarPaths().toArray(new String[]{}), mRunnerPath);
        }
    }

    private void buildJarPaths() throws DeviceNotAvailableException {
        if (mJarPaths.isEmpty()) {
            String rawFileString =
                    getDevice().executeShellCommand(String.format("ls %s", SHELL_EXE_BASE));
            String[] rawFiles = rawFileString.split("\r?\n");
            for (String rawFile : rawFiles) {
                if (rawFile.endsWith(".jar")) {
                    mJarPaths.add(rawFile);
                }
            }
            Assert.assertFalse(String.format("could not find jars in %s", SHELL_EXE_BASE),
                    mJarPaths.isEmpty());
            CLog.d("built jar paths %s", mJarPaths);
        }
    }

    /**
     * Add an argument to provide when running the UI Automator tests
     *
     * @param key the argument name
     * @param value the argument value
     */
    public void addRunArg(String key, String value) {
        getTestRunArgMap().put(key, value);
    }

    /**
     * Checks if the UI Automator components are present on device
     *
     * @throws DeviceNotAvailableException
     */
    protected void preTestSetup() throws DeviceNotAvailableException {
        if (!isInstrumentationTest()) {
            String runnerPath = ((UiAutomatorRunner)getTestRunner()).getRunnerPath();
            if (!getDevice().doesFileExist(runnerPath)) {
                throw new RuntimeException("Missing UI Automator runner: " + runnerPath);
            }
            for (String jarPath : getTestJarPaths()) {
                if (!jarPath.startsWith(FileListingService.FILE_SEPARATOR)) {
                    jarPath = SHELL_EXE_BASE + jarPath;
                }
                if (!getDevice().doesFileExist(jarPath)) {
                    throw new RuntimeException("Missing UI Automator test jar on device: "
                            + jarPath);
                }
            }
        }
    }

    protected void onScreenshotAndBugreport(ITestDevice device, ITestInvocationListener listener,
            String prefix) {
        onScreenshotAndBugreport(device, listener, prefix, null);
    }

    protected void onScreenshotAndBugreport(ITestDevice device, ITestInvocationListener listener,
            String prefix, TestFailureAction overrideAction) {
        if (overrideAction == null) {
            overrideAction = mFailureAction;
        }
        // get screen shot
        if (overrideAction == TestFailureAction.SCREENSHOT ||
                overrideAction == TestFailureAction.BUGREPORT_AND_SCREENSHOT) {
            InputStreamSource screenshot = null;
            try {
                screenshot = device.getScreenshot();
                listener.testLog(prefix + "_screenshot", LogDataType.PNG, screenshot);
            } catch (DeviceNotAvailableException e) {
                CLog.e(e);
            } finally {
                if (screenshot != null) {
                    screenshot.cancel();
                }
            }
        }
        // get bugreport
        if (overrideAction == TestFailureAction.BUGREPORT ||
                overrideAction == TestFailureAction.BUGREPORT_AND_SCREENSHOT) {
            InputStreamSource data = null;
            data = device.getBugreport();
            listener.testLog(prefix + "_bugreport", LogDataType.BUGREPORT, data);
            if (data != null) {
                data.cancel();
            }
        }
    }


    /**
     * Pull the atrace files if they exist under traceSrcDirectory and log it
     * @param listener test result listener
     * @param traceSrcDirectory source directory in the device where the trace files
     *                          are copied to the local tmp directory
     * @throws DeviceNotAvailableException
     * @throws IOException
     */
    private void logTraceFiles(ITestInvocationListener listener, String traceSrcDirectory)
            throws DeviceNotAvailableException, IOException {
        File tmpDestDir = null;
        try {
            tmpDestDir = FileUtil.createTempDir("atrace");
            IFileEntry traceSrcDir = mDevice.getFileEntry(traceSrcDirectory);
            // Trace files are retrieved from traceSrcDirectory/testDirectory in device
            if (traceSrcDir != null) {
                for (IFileEntry testDirectory : traceSrcDir.getChildren(false)) {
                    File testTmpDirectory = new File(tmpDestDir, testDirectory.getName());
                    if (!testTmpDirectory.mkdir()) {
                        throw new IOException("Not able to create the atrace test directory");
                    }
                    for (IFileEntry traceFile : testDirectory.getChildren(false)) {
                        File pulledFile = new File(testTmpDirectory, traceFile.getName());
                        if (!mDevice.pullFile(traceFile.getFullPath(), pulledFile)) {
                            throw new IOException(
                                    "Not able to pull the trace file from test device");
                        }
                    }
                }
                File atraceZip = ZipUtil.createZip(tmpDestDir);
                FileInputStreamSource streamSource = new FileInputStreamSource(atraceZip);
                listener.testLog(tmpDestDir.getName(), LogDataType.ZIP, streamSource);
                StreamUtil.cancel(streamSource);
                atraceZip.delete();
            }
        } finally {
            if (tmpDestDir != null) {
                FileUtil.recursiveDelete(tmpDestDir);
            }
        }
    }

    /**
     * Wraps an existing listener, capture some data in case of test failure
     */
    // TODO replace this once we have a generic event triggered reporter like
    // BugReportCollector
    private class LoggingWrapper implements ITestInvocationListener {

        ITestInvocationListener mListener;
        private boolean mLoggedTestFailure = false;
        private boolean mLoggedTestRunFailure = false;

        public LoggingWrapper(ITestInvocationListener listener) {
            mListener = listener;
        }

        @Override
        public void testFailed(TestIdentifier test, String trace) {
            captureFailureLog(test);
        }

        @Override
        public void testAssumptionFailure(TestIdentifier test, String trace) {
            captureFailureLog(test);
        }

        private void captureFailureLog(TestIdentifier test) {
            if (mLoggingOption == LoggingOption.AFTER_FAILURE) {
                onScreenshotAndBugreport(getDevice(), mListener, String.format("%s_%s_failure",
                        test.getClassName(), test.getTestName()));
                // set the flag so that we don't log again when test finishes
                mLoggedTestFailure = true;
            }
        }

        @Override
        public void testRunFailed(String errorMessage) {
            if (mLoggingOption == LoggingOption.AFTER_FAILURE) {
                onScreenshotAndBugreport(getDevice(), mListener, "test_run_failure");
                // set the flag so that we don't log again when test run finishes
                mLoggedTestRunFailure = true;
            }
        }

        @Override
        public void testEnded(TestIdentifier test, Map<String, String> testMetrics) {
            if (!mLoggedTestFailure && mLoggingOption == LoggingOption.AFTER_TEST) {
                onScreenshotAndBugreport(getDevice(), mListener, String.format("%s_%s_final",
                        test.getClassName(), test.getTestName()));
            }
        }

        @Override
        public void testRunEnded(long elapsedTime, Map<String, String> runMetrics) {
            if (!mLoggedTestRunFailure && mLoggingOption == LoggingOption.AFTER_TEST) {
                onScreenshotAndBugreport(getDevice(), mListener, "test_run_final");
            }
        }
    }

    protected IRunUtil getRunUtil() {
        return RunUtil.getDefault();
    }

    /**
     * @return the time allocated for the tests to sync.
     */
    public long getSyncTime() {
        return mSyncTime;
    }

    /**
     * @param syncTime the time for the tests files to sync.
     */
    public void setSyncTime(long syncTime) {
        mSyncTime = syncTime;
    }

    /**
     * @return the test runner.
     */
    public IRemoteAndroidTestRunner getTestRunner() {
        return mRunner;
    }

    /**
     * @return the test jar path.
     */
    public List<String> getTestJarPaths() {
        return mJarPaths;
    }

    /**
     * @param jarPaths the locations of the test jars.
     */
    public void setTestJarPaths(List<String> jarPaths) {
        mJarPaths = jarPaths;
    }

    /**
     * @return the arguments map to pass to the UiAutomatorRunner.
     */
    public Map<String, String> getTestRunArgMap() {
        return mArgMap;
    }

    /**
     * @param runArgMap the arguments to pass to the UiAutomatorRunner.
     */
    public void setTestRunArgMap(Map<String, String> runArgMap) {
        mArgMap = runArgMap;
    }

    /**
     * Add a test class name to run.
     */
    public void addClassName(String className) {
        mClasses.add(className);
    }

    /**
     * Add a test class name collection to run.
     */
    public void addClassNames(Collection<String> classNames) {
        mClasses.addAll(classNames);
    }

    public boolean isInstrumentationTest() {
        return mInstrumentation;
    }

    public void setRunnerName(String runnerName) {
        mRunnerName = runnerName;
    }

    /**
     * Gets the list of test class names that the harness is configured to run
     * @return list of test class names
     */
    public List<String> getClassNames() {
        return mClasses;
    }
}