| 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();
|
| - }
|
| - }
|
| }
|
|
|