From 3001a035439d8134a7d70d796376d1dfbff3cdcd Mon Sep 17 00:00:00 2001 From: The Android Open Source Project Date: Thu, 19 Feb 2009 10:57:31 -0800 Subject: auto import from //branches/cupcake/...@132276 --- test-runner/android/test/ServiceTestCase.java | 4 +-- test-runner/android/test/TestCaseUtil.java | 38 +++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) (limited to 'test-runner') diff --git a/test-runner/android/test/ServiceTestCase.java b/test-runner/android/test/ServiceTestCase.java index c53bf7d..fcb9d55 100644 --- a/test-runner/android/test/ServiceTestCase.java +++ b/test-runner/android/test/ServiceTestCase.java @@ -48,7 +48,7 @@ import java.util.Random; * the test case will call onCreate(), and then call the corresponding entry point in your service. * It will record any parameters or other support values necessary to support the lifecycle. *
  • After your test completes, the test case {@link #tearDown} function is - * automatically called, and it will stop & destroy your service with the appropriate + * automatically called, and it will stop and destroy your service with the appropriate * calls (depending on how your test invoked the service.)
  • * * @@ -172,7 +172,7 @@ public abstract class ServiceTestCase extends AndroidTestCase * Return the communication channel to the service. May return null if * clients can not bind to the service. The returned * {@link android.os.IBinder} is usually for a complex interface - * that has been described using + * that has been described using * aidl. * * Note: In order to test with this interface, your service must implement a getService() diff --git a/test-runner/android/test/TestCaseUtil.java b/test-runner/android/test/TestCaseUtil.java index 4109d9c..3ba9711 100644 --- a/test-runner/android/test/TestCaseUtil.java +++ b/test-runner/android/test/TestCaseUtil.java @@ -17,6 +17,7 @@ package android.test; import com.google.android.collect.Lists; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -26,7 +27,9 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Enumeration; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * @hide - This is part of a framework that is under development and should not be used for @@ -48,10 +51,25 @@ public class TestCaseUtil { } public static List getTests(Test test, boolean flatten) { + return getTests(test, flatten, new HashSet>()); + } + + private static List getTests(Test test, boolean flatten, + Set> seen) { List testCases = Lists.newArrayList(); if (test != null) { - Test workingTest = invokeSuiteMethodIfPossible(test.getClass()); + Test workingTest = null; + /* + * If we want to run a single TestCase method only, we must not + * invoke the suite() method, because we will run all test methods + * of the class then. + */ + if (test instanceof TestCase && + ((TestCase)test).getName() == null) { + workingTest = invokeSuiteMethodIfPossible(test.getClass(), + seen); + } if (workingTest == null) { workingTest = test; } @@ -62,7 +80,7 @@ public class TestCaseUtil { while (enumeration.hasMoreElements()) { Test childTest = (Test) enumeration.nextElement(); if (flatten) { - testCases.addAll(getTests(childTest, flatten)); + testCases.addAll(getTests(childTest, flatten, seen)); } else { testCases.add(childTest); } @@ -74,11 +92,20 @@ public class TestCaseUtil { return testCases; } - private static Test invokeSuiteMethodIfPossible(Class testClass) { + private static Test invokeSuiteMethodIfPossible(Class testClass, + Set> seen) { try { Method suiteMethod = testClass.getMethod( BaseTestRunner.SUITE_METHODNAME, new Class[0]); - if (Modifier.isStatic(suiteMethod.getModifiers())) { + /* + * Additional check necessary: If a TestCase contains a suite() + * method that returns a TestSuite including the TestCase itself, + * we need to stop the recursion. We use a set of classes to + * remember which classes' suite() methods were already invoked. + */ + if (Modifier.isStatic(suiteMethod.getModifiers()) + && !seen.contains(testClass)) { + seen.add(testClass); try { return (Test) suiteMethod.invoke(null, (Object[]) null); } catch (InvocationTargetException e) { @@ -128,7 +155,8 @@ public class TestCaseUtil { public static TestSuite createTestSuite(Class testClass) throws InstantiationException, IllegalAccessException { - Test test = invokeSuiteMethodIfPossible(testClass); + Test test = invokeSuiteMethodIfPossible(testClass, + new HashSet>()); if (test == null) { return new TestSuite(testClass); -- cgit v1.1