FileDocCategorySizeDatePackage
UiAutomatorTestRunner.javaAPI DocAndroid 5.1 API16878Thu Mar 12 22:22:08 GMT 2015com.android.uiautomator.testrunner

UiAutomatorTestRunner

public class UiAutomatorTestRunner extends Object
hide

Fields Summary
private static final String
LOGTAG
private static final int
EXIT_OK
private static final int
EXIT_EXCEPTION
private static final String
HANDLER_THREAD_NAME
private boolean
mDebug
private boolean
mMonkey
private android.os.Bundle
mParams
private com.android.uiautomator.core.UiDevice
mUiDevice
private List
mTestClasses
private final FakeInstrumentationWatcher
mWatcher
private final IAutomationSupport
mAutomationSupport
private final List
mTestListeners
private android.os.HandlerThread
mHandlerThread
Constructors Summary
Methods Summary
protected voidaddTestListener(junit.framework.TestListener listener)

        if (!mTestListeners.contains(listener)) {
            mTestListeners.add(listener);
        }
    
protected TestCaseCollectorgetTestCaseCollector(java.lang.ClassLoader classLoader)

        return new TestCaseCollector(classLoader, getTestCaseFilter());
    
public UiAutomatorTestCaseFiltergetTestCaseFilter()
Returns an object which determines if the class and its methods should be accepted into the test suite.

return

        return new UiAutomatorTestCaseFilter();
    
protected voidprepareTestCase(junit.framework.TestCase testCase)
subclass may override this method to perform further preparation

param
testCase

        ((UiAutomatorTestCase)testCase).setAutomationSupport(mAutomationSupport);
        ((UiAutomatorTestCase)testCase).setUiDevice(mUiDevice);
        ((UiAutomatorTestCase)testCase).setParams(mParams);
    
protected voidremoveTestListener(junit.framework.TestListener listener)

        mTestListeners.remove(listener);
    
public voidrun(java.util.List testClasses, android.os.Bundle params, boolean debug, boolean monkey)


              
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                Log.e(LOGTAG, "uncaught exception", ex);
                Bundle results = new Bundle();
                results.putString("shortMsg", ex.getClass().getName());
                results.putString("longMsg", ex.getMessage());
                mWatcher.instrumentationFinished(null, 0, results);
                // bailing on uncaught exception
                System.exit(EXIT_EXCEPTION);
            }
        });

        mTestClasses = testClasses;
        mParams = params;
        mDebug = debug;
        mMonkey = monkey;
        start();
        System.exit(EXIT_OK);
    
protected voidstart()
Called after all test classes are in place, ready to test

        TestCaseCollector collector = getTestCaseCollector(this.getClass().getClassLoader());
        try {
            collector.addTestClasses(mTestClasses);
        } catch (ClassNotFoundException e) {
            // will be caught by uncaught handler
            throw new RuntimeException(e.getMessage(), e);
        }
        if (mDebug) {
            Debug.waitForDebugger();
        }
        mHandlerThread = new HandlerThread(HANDLER_THREAD_NAME);
        mHandlerThread.setDaemon(true);
        mHandlerThread.start();
        UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
        automationWrapper.connect();

        long startTime = SystemClock.uptimeMillis();
        TestResult testRunResult = new TestResult();
        ResultReporter resultPrinter;
        String outputFormat = mParams.getString("outputFormat");
        List<TestCase> testCases = collector.getTestCases();
        Bundle testRunOutput = new Bundle();
        if ("simple".equals(outputFormat)) {
            resultPrinter = new SimpleResultPrinter(System.out, true);
        } else {
            resultPrinter = new WatcherResultPrinter(testCases.size());
        }
        try {
            automationWrapper.setRunAsMonkey(mMonkey);
            mUiDevice = UiDevice.getInstance();
            mUiDevice.initialize(new ShellUiAutomatorBridge(automationWrapper.getUiAutomation()));

            String traceType = mParams.getString("traceOutputMode");
            if(traceType != null) {
                Tracer.Mode mode = Tracer.Mode.valueOf(Tracer.Mode.class, traceType);
                if (mode == Tracer.Mode.FILE || mode == Tracer.Mode.ALL) {
                    String filename = mParams.getString("traceLogFilename");
                    if (filename == null) {
                        throw new RuntimeException("Name of log file not specified. " +
                                "Please specify it using traceLogFilename parameter");
                    }
                    Tracer.getInstance().setOutputFilename(filename);
                }
                Tracer.getInstance().setOutputMode(mode);
            }

            // add test listeners
            testRunResult.addListener(resultPrinter);
            // add all custom listeners
            for (TestListener listener : mTestListeners) {
                testRunResult.addListener(listener);
            }

            // run tests for realz!
            for (TestCase testCase : testCases) {
                prepareTestCase(testCase);
                testCase.run(testRunResult);
            }
        } catch (Throwable t) {
            // catch all exceptions so a more verbose error message can be outputted
            resultPrinter.printUnexpectedError(t);
            testRunOutput.putString("shortMsg", t.getMessage());
        } finally {
            long runTime = SystemClock.uptimeMillis() - startTime;
            resultPrinter.print(testRunResult, runTime, testRunOutput);
            automationWrapper.disconnect();
            automationWrapper.setRunAsMonkey(false);
            mHandlerThread.quit();
        }