Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(512)

Unified Diff: base/test/android/javatests/src/org/chromium/base/test/BaseInstrumentationTestRunner.java

Issue 938883002: [Android] Rework instrumentation test skipping and result generation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | build/android/pylib/base/base_test_result.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/test/android/javatests/src/org/chromium/base/test/BaseInstrumentationTestRunner.java
diff --git a/base/test/android/javatests/src/org/chromium/base/test/BaseInstrumentationTestRunner.java b/base/test/android/javatests/src/org/chromium/base/test/BaseInstrumentationTestRunner.java
index b6c103d6174e299fa28814920def04db6c2e484f..a104831d57dff792cbd2219c843dba098a5e0745 100644
--- a/base/test/android/javatests/src/org/chromium/base/test/BaseInstrumentationTestRunner.java
+++ b/base/test/android/javatests/src/org/chromium/base/test/BaseInstrumentationTestRunner.java
@@ -4,19 +4,20 @@
package org.chromium.base.test;
-import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.test.AndroidTestRunner;
import android.test.InstrumentationTestRunner;
import android.util.Log;
-import junit.framework.Test;
import junit.framework.TestCase;
-import junit.framework.TestSuite;
+import junit.framework.TestResult;
import org.chromium.base.test.util.MinAndroidSdkLevel;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* An Instrumentation test runner that checks SDK level for tests with specific requirements.
*/
@@ -24,40 +25,94 @@ public class BaseInstrumentationTestRunner extends InstrumentationTestRunner {
private static final String TAG = "BaseInstrumentationTestRunner";
- @Override
- protected AndroidTestRunner getAndroidTestRunner() {
- return new BaseAndroidTestRunner(getContext());
+ /**
+ * An interface for classes that check whether a test case should be skipped.
+ */
+ public interface SkipCheck {
+ /**
+ * Checks whether the given test case should be skipped.
+ *
+ * @param testCase The test case to check.
+ * @return Whether the test case should be skipped.
+ */
+ public boolean shouldSkip(TestCase testCase);
}
/**
- * Skips tests that don't meet the requirements of the current device.
+ * A test result that can skip tests.
*/
- public class BaseAndroidTestRunner extends AndroidTestRunner {
- private final Context mContext;
+ public class SkippingTestResult extends TestResult {
+
+ private final List<SkipCheck> mSkipChecks;
+
+ /**
+ * Creates an instance of SkippingTestResult.
+ */
+ public SkippingTestResult() {
+ mSkipChecks = new ArrayList<SkipCheck>();
+ }
+
+ /**
+ * Adds a check for whether a test should run.
+ *
+ * @param skipCheck The check to add.
+ */
+ public void addSkipCheck(SkipCheck skipCheck) {
+ mSkipChecks.add(skipCheck);
+ }
- public BaseAndroidTestRunner(Context context) {
- mContext = context;
+ private boolean shouldSkip(final TestCase test) {
+ for (SkipCheck s : mSkipChecks) {
+ if (s.shouldSkip(test)) return true;
+ }
+ return false;
}
@Override
- public void setTest(Test test) {
- super.setTest(test);
- TestSuite revisedTestSuite = new TestSuite();
- for (TestCase testCase : this.getTestCases()) {
- Class<?> testClass = testCase.getClass();
- if (shouldSkip(testClass, testCase)) {
- revisedTestSuite.addTest(new SkippedTest(testCase));
- Bundle skipResult = new Bundle();
- skipResult.putBoolean("test_skipped", true);
- sendStatus(0, skipResult);
- } else {
- revisedTestSuite.addTest(testCase);
- }
+ protected void run(final TestCase test) {
+ if (shouldSkip(test)) {
+ startTest(test);
+
+ Bundle skipResult = new Bundle();
+ skipResult.putString("class", test.getClass().getName());
+ skipResult.putString("test", test.getName());
+ skipResult.putBoolean("test_skipped", true);
+ sendStatus(0, skipResult);
+
+ endTest(test);
+ } else {
+ super.run(test);
}
- super.setTest(revisedTestSuite);
}
+ }
- protected boolean shouldSkip(Class<?> testClass, TestCase testCase) {
+ @Override
+ protected AndroidTestRunner getAndroidTestRunner() {
+ return new AndroidTestRunner() {
+ @Override
+ protected TestResult createTestResult() {
+ SkippingTestResult r = new SkippingTestResult();
+ r.addSkipCheck(new MinAndroidSdkLevelSkipCheck());
+ return r;
+ }
+ };
+ }
+
+ /**
+ * Checks the device's SDK level against any specified minimum requirement.
+ */
+ public static class MinAndroidSdkLevelSkipCheck implements SkipCheck {
+
+ /**
+ * If {@link org.chromium.base.test.util.MinAndroidSdkLevel} is present, checks its value
+ * against the device's SDK level.
+ *
+ * @param testCase The test to check.
+ * @return true if the device's SDK level is below the specified minimum.
+ */
+ @Override
+ public boolean shouldSkip(TestCase testCase) {
+ Class<?> testClass = testCase.getClass();
if (testClass.isAnnotationPresent(MinAndroidSdkLevel.class)) {
MinAndroidSdkLevel v = testClass.getAnnotation(MinAndroidSdkLevel.class);
if (Build.VERSION.SDK_INT < v.value()) {
@@ -69,28 +124,6 @@ public class BaseInstrumentationTestRunner extends InstrumentationTestRunner {
}
return false;
}
-
- protected Context getContext() {
- return mContext;
- }
}
- /**
- * Replaces a TestCase that should be skipped.
- */
- public static class SkippedTest extends TestCase {
-
- public SkippedTest(TestCase skipped) {
- super(skipped.getClass().getName() + "#" + skipped.getName());
- }
-
- @Override
- protected void runTest() throws Throwable {
- }
-
- @Override
- public String toString() {
- return "SKIPPED " + super.toString();
- }
- }
}
« no previous file with comments | « no previous file | build/android/pylib/base/base_test_result.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698