Fix incorrect test counts when filtering JUnit3 tests.

Bug 8399471

Change-Id: I6613b1a7d3bdc670760c1618d900e6deb673c51a
diff --git a/androidtestlib/src/com/android/test/runner/TestRequestBuilder.java b/androidtestlib/src/com/android/test/runner/TestRequestBuilder.java
index 8bf5929..fa86b98 100644
--- a/androidtestlib/src/com/android/test/runner/TestRequestBuilder.java
+++ b/androidtestlib/src/com/android/test/runner/TestRequestBuilder.java
@@ -80,9 +80,18 @@
                 return description.getAnnotation(mAnnotationClass) != null ||
                         description.getTestClass().isAnnotationPresent(mAnnotationClass);
             } else {
-                // don't filter out any test classes/suites, because their methods may have correct
-                // annotation
-                return true;
+                // the entire test class/suite should be filtered out if all its methods are
+                // filtered
+                // TODO: This is not efficient since some children may end up being evaluated more
+                // than once. This logic seems to be only necessary for JUnit3 tests. Look into
+                // fixing in upstream
+                for (Description child : description.getChildren()) {
+                    if (shouldRun(child)) {
+                        return true;
+                    }
+                }
+                // no children to run, filter this out
+                return false;
             }
         }
 
diff --git a/androidtestlib/tests/src/com/android/test/runner/TestRequestBuilderTest.java b/androidtestlib/tests/src/com/android/test/runner/TestRequestBuilderTest.java
index 9c4ae12..7028f06 100644
--- a/androidtestlib/tests/src/com/android/test/runner/TestRequestBuilderTest.java
+++ b/androidtestlib/tests/src/com/android/test/runner/TestRequestBuilderTest.java
@@ -22,10 +22,14 @@
 import com.android.test.InjectBundle;
 import com.android.test.InjectInstrumentation;
 
+import junit.framework.TestCase;
+
 import org.junit.Assert;
 import org.junit.Test;
+import org.junit.runner.Description;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
+import org.junit.runner.notification.RunListener;
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
@@ -59,6 +63,16 @@
         }
     }
 
+    public static class SampleNoSize extends TestCase {
+
+        public void testOther() {
+        }
+
+        public void testOther2() {
+        }
+
+    }
+
     @InjectInstrumentation
     public Instrumentation mInstr;
 
@@ -109,6 +123,31 @@
     }
 
     /**
+     * Test case where entire JUnit3 test class has been filtered out
+     */
+    @Test
+    public void testSize_classFiltered() {
+        TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
+        b.addTestClass(SampleTest.class.getName());
+        b.addTestClass(SampleNoSize.class.getName());
+        b.addTestSizeFilter("small");
+        TestRequest request = b.build(mInstr, mBundle);
+        MyRunListener l = new MyRunListener();
+        JUnitCore testRunner = new JUnitCore();
+        testRunner.addListener(l);
+        testRunner.run(request.getRequest());
+        Assert.assertEquals(1, l.mTestCount);
+    }
+
+    private static class MyRunListener extends RunListener {
+        private int mTestCount = -1;
+
+        public void testRunStarted(Description description) throws Exception {
+            mTestCount = description.testCount();
+        }
+    }
+
+    /**
      * Test that annotation filtering by class works
      */
     @Test