Search in sources :

Example 56 with TestCase

use of junit.framework.TestCase in project android_frameworks_base by crdroidandroid.

the class UiAutomatorTestRunner method start.

/**
     * Called after all test classes are in place, ready to test
     */
protected void start() {
    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();
    }
}
Also used : Bundle(android.os.Bundle) Tracer(com.android.uiautomator.core.Tracer) TestResult(junit.framework.TestResult) UiAutomationShellWrapper(com.android.uiautomator.core.UiAutomationShellWrapper) HandlerThread(android.os.HandlerThread) TestCase(junit.framework.TestCase) ShellUiAutomatorBridge(com.android.uiautomator.core.ShellUiAutomatorBridge) TestListener(junit.framework.TestListener)

Example 57 with TestCase

use of junit.framework.TestCase in project android_frameworks_base by crdroidandroid.

the class AndroidTestRunner method setTestClassName.

@SuppressWarnings("unchecked")
public void setTestClassName(String testClassName, String testMethodName) {
    Class testClass = loadTestClass(testClassName);
    if (shouldRunSingleTestMethod(testMethodName, testClass)) {
        TestCase testCase = buildSingleTestMethod(testClass, testMethodName);
        mTestCases = Lists.newArrayList(testCase);
        mTestClassName = testClass.getSimpleName();
    } else {
        setTest(getTest(testClass), testClass);
    }
}
Also used : TestCase(junit.framework.TestCase)

Example 58 with TestCase

use of junit.framework.TestCase in project android_frameworks_base by crdroidandroid.

the class AndroidTestRunner method newSingleTestMethod.

private TestCase newSingleTestMethod(Class testClass, String testMethodName, Constructor constructor, Object... args) {
    try {
        TestCase testCase = (TestCase) constructor.newInstance(args);
        testCase.setName(testMethodName);
        return testCase;
    } catch (IllegalAccessException e) {
        runFailed("Could not access test class. Class: " + testClass.getName());
    } catch (InstantiationException e) {
        runFailed("Could not instantiate test class. Class: " + testClass.getName());
    } catch (IllegalArgumentException e) {
        runFailed("Illegal argument passed to constructor. Class: " + testClass.getName());
    } catch (InvocationTargetException e) {
        runFailed("Constructor thew an exception. Class: " + testClass.getName());
    }
    return null;
}
Also used : TestCase(junit.framework.TestCase) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 59 with TestCase

use of junit.framework.TestCase in project android_frameworks_base by crdroidandroid.

the class AndroidTestRunner method runTest.

public void runTest(TestResult testResult) {
    mTestResult = testResult;
    for (TestListener testListener : mTestListeners) {
        mTestResult.addListener(testListener);
    }
    Context testContext = mInstrumentation == null ? mContext : mInstrumentation.getContext();
    for (TestCase testCase : mTestCases) {
        setContextIfAndroidTestCase(testCase, mContext, testContext);
        setInstrumentationIfInstrumentationTestCase(testCase, mInstrumentation);
        setPerformanceWriterIfPerformanceCollectorTestCase(testCase, mPerfWriter);
        testCase.run(mTestResult);
    }
}
Also used : Context(android.content.Context) TestCase(junit.framework.TestCase) TestListener(junit.framework.TestListener)

Example 60 with TestCase

use of junit.framework.TestCase in project android_frameworks_base by crdroidandroid.

the class TestMethod method instantiateTest.

@SuppressWarnings("unchecked")
private TestCase instantiateTest(Class testCaseClass, String testName) throws InvocationTargetException, IllegalAccessException, InstantiationException {
    Constructor[] constructors = testCaseClass.getConstructors();
    if (constructors.length == 0) {
        return instantiateTest(testCaseClass.getSuperclass(), testName);
    } else {
        for (Constructor constructor : constructors) {
            Class[] params = constructor.getParameterTypes();
            if (noargsConstructor(params)) {
                TestCase test = ((Constructor<? extends TestCase>) constructor).newInstance();
                // JUnit will run just the one test if you call
                // {@link TestCase#setName(String)}
                test.setName(testName);
                return test;
            } else if (singleStringConstructor(params)) {
                return ((Constructor<? extends TestCase>) constructor).newInstance(testName);
            }
        }
    }
    throw new RuntimeException("Unable to locate a constructor for " + testCaseClass.getName());
}
Also used : TestCase(junit.framework.TestCase) Constructor(java.lang.reflect.Constructor)

Aggregations

TestCase (junit.framework.TestCase)129 TestSuite (junit.framework.TestSuite)36 Test (junit.framework.Test)22 TestListener (junit.framework.TestListener)17 TestResult (junit.framework.TestResult)13 ArrayList (java.util.ArrayList)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)10 AssertionFailedError (junit.framework.AssertionFailedError)10 Constructor (java.lang.reflect.Constructor)8 Enumeration (java.util.Enumeration)6 Context (android.content.Context)5 PackageManager (android.content.pm.PackageManager)5 ResolveInfo (android.content.pm.ResolveInfo)5 Bundle (android.os.Bundle)5 HandlerThread (android.os.HandlerThread)5 ShellUiAutomatorBridge (com.android.uiautomator.core.ShellUiAutomatorBridge)5 Tracer (com.android.uiautomator.core.Tracer)5 UiAutomationShellWrapper (com.android.uiautomator.core.UiAutomationShellWrapper)5 Field (java.lang.reflect.Field)5 HashSet (java.util.HashSet)5