Search in sources :

Example 86 with TestCase

use of junit.framework.TestCase in project platform_frameworks_base by android.

the class TestCaseUtilTest method testGetTestCaseNamesForTestCaseWithSuiteMethod.

public void testGetTestCaseNamesForTestCaseWithSuiteMethod() throws Exception {
    TestCase testCase = new OneTestTestCaseWithSuite();
    List<String> testCaseNames = TestCaseUtil.getTestCaseNames(testCase, false);
    assertEquals(1, testCaseNames.size());
    assertTrue(testCaseNames.get(0).endsWith("testOne"));
}
Also used : TestCase(junit.framework.TestCase)

Example 87 with TestCase

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

the class InstrumentationCoreTestRunner method getAndroidTestRunner.

@Override
protected AndroidTestRunner getAndroidTestRunner() {
    AndroidTestRunner runner = super.getAndroidTestRunner();
    runner.addTestListener(new TestListener() {

        /**
             * The last test class we executed code from.
             */
        private Class<?> lastClass;

        /**
             * The minimum time we expect a test to take.
             */
        private static final int MINIMUM_TIME = 100;

        /**
             * The start time of our current test in System.currentTimeMillis().
             */
        private long startTime;

        public void startTest(Test test) {
            if (test.getClass() != lastClass) {
                lastClass = test.getClass();
                printMemory(test.getClass());
            }
            Thread.currentThread().setContextClassLoader(test.getClass().getClassLoader());
            startTime = System.currentTimeMillis();
        }

        public void endTest(Test test) {
            if (test instanceof TestCase) {
                cleanup((TestCase) test);
                /*
                     * Make sure all tests take at least MINIMUM_TIME to
                     * complete. If they don't, we wait a bit. The Cupcake
                     * Binder can't handle too many operations in a very
                     * short time, which causes headache for the CTS.
                     */
                long timeTaken = System.currentTimeMillis() - startTime;
                if (timeTaken < MINIMUM_TIME) {
                    try {
                        Thread.sleep(MINIMUM_TIME - timeTaken);
                    } catch (InterruptedException ignored) {
                    // We don't care.
                    }
                }
            }
        }

        public void addError(Test test, Throwable t) {
        // This space intentionally left blank.
        }

        public void addFailure(Test test, AssertionFailedError t) {
        // This space intentionally left blank.
        }

        /**
             * Dumps some memory info.
             */
        private void printMemory(Class<? extends Test> testClass) {
            Runtime runtime = Runtime.getRuntime();
            long total = runtime.totalMemory();
            long free = runtime.freeMemory();
            long used = total - free;
            Log.d(TAG, "Total memory  : " + total);
            Log.d(TAG, "Used memory   : " + used);
            Log.d(TAG, "Free memory   : " + free);
            Log.d(TAG, "Now executing : " + testClass.getName());
        }

        /**
             * Nulls all non-static reference fields in the given test class.
             * This method helps us with those test classes that don't have an
             * explicit tearDown() method. Normally the garbage collector should
             * take care of everything, but since JUnit keeps references to all
             * test cases, a little help might be a good idea.
             */
        private void cleanup(TestCase test) {
            Class<?> clazz = test.getClass();
            while (clazz != TestCase.class) {
                Field[] fields = clazz.getDeclaredFields();
                for (int i = 0; i < fields.length; i++) {
                    Field f = fields[i];
                    if (!f.getType().isPrimitive() && !Modifier.isStatic(f.getModifiers())) {
                        try {
                            f.setAccessible(true);
                            f.set(test, null);
                        } catch (Exception ignored) {
                        // Nothing we can do about it.
                        }
                    }
                }
                clazz = clazz.getSuperclass();
            }
        }
    });
    return runner;
}
Also used : Field(java.lang.reflect.Field) BrokenTest(dalvik.annotation.BrokenTest) Test(junit.framework.Test) TestCase(junit.framework.TestCase) TestListener(junit.framework.TestListener) AssertionFailedError(junit.framework.AssertionFailedError)

Example 88 with TestCase

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

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 89 with TestCase

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

the class ListTestCaseNames method getTestCaseNames.

public static List<String> getTestCaseNames(TestSuite suite) {
    // TODO: deprecate this method and move all callers to use getTestNames
    List<Test> tests = Collections.<Test>list(suite.tests());
    ArrayList<String> testCaseNames = new ArrayList<String>();
    for (Test test : tests) {
        if (test instanceof TestCase) {
            testCaseNames.add(((TestCase) test).getName());
        } else if (test instanceof TestSuite) {
            testCaseNames.addAll(getTestCaseNames((TestSuite) test));
        }
    }
    return testCaseNames;
}
Also used : TestSuite(junit.framework.TestSuite) Test(junit.framework.Test) TestCase(junit.framework.TestCase) ArrayList(java.util.ArrayList)

Example 90 with TestCase

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

the class ListTestCaseNames method getTestNames.

/** 
     * Returns a list of test class and method names for each TestCase in suite.  
     */
public static List<TestDescriptor> getTestNames(TestSuite suite) {
    List<Test> tests = Collections.<Test>list(suite.tests());
    ArrayList<TestDescriptor> testNames = new ArrayList<TestDescriptor>();
    for (Test test : tests) {
        if (test instanceof TestCase) {
            String className = test.getClass().getName();
            String testName = ((TestCase) test).getName();
            testNames.add(new TestDescriptor(className, testName));
        } else if (test instanceof TestSuite) {
            testNames.addAll(getTestNames((TestSuite) test));
        }
    }
    return testNames;
}
Also used : TestSuite(junit.framework.TestSuite) Test(junit.framework.Test) TestCase(junit.framework.TestCase) ArrayList(java.util.ArrayList)

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