Commit a6a54b3a authored by Bernhard Bauer's avatar Bernhard Bauer Committed by Commit Bot

Replace @UseMethodParam annotation with generator classes.

Change-Id: I839b4f520300b17e3ba22cf0ea62d2a03b376c5e
Reviewed-on: https://chromium-review.googlesource.com/760419Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Commit-Queue: Bernhard Bauer <bauerb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551007}
parent 051ec184
...@@ -18,16 +18,16 @@ import org.junit.Test; ...@@ -18,16 +18,16 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.test.params.BaseJUnit4RunnerDelegate; import org.chromium.base.test.params.BaseJUnit4RunnerDelegate;
import org.chromium.base.test.params.ParameterAnnotations.MethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter; import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate; import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;
import org.chromium.base.test.params.ParameterProvider;
import org.chromium.base.test.params.ParameterSet; import org.chromium.base.test.params.ParameterSet;
import org.chromium.base.test.params.ParameterizedRunner; import org.chromium.base.test.params.ParameterizedRunner;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.Restriction; import org.chromium.base.test.util.Restriction;
import org.chromium.webview_shell.PageCyclerTestActivity; import org.chromium.webview_shell.PageCyclerTestActivity;
import java.util.ArrayList; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
...@@ -41,18 +41,20 @@ import java.util.concurrent.TimeoutException; ...@@ -41,18 +41,20 @@ import java.util.concurrent.TimeoutException;
public class PageCyclerTest { public class PageCyclerTest {
private static final long TIMEOUT_IN_SECS = 20; private static final long TIMEOUT_IN_SECS = 20;
@MethodParameter("A") private static class WebsiteParams implements ParameterProvider {
private static List<ParameterSet> sMethodParameter = new ArrayList<>(); @Override
public List<ParameterSet> getParameters() {
static { return Arrays.asList(
sMethodParameter.add(new ParameterSet().value("http://google.com").name("Google")); new ParameterSet().value("http://google.com").name("Google"),
sMethodParameter.add(new ParameterSet().value("http://facebook.com").name("Facebook")); new ParameterSet().value("http://facebook.com").name("Facebook"),
sMethodParameter.add(new ParameterSet().value("http://wikipedia.org").name("Wikipedia")); new ParameterSet().value("http://wikipedia.org").name("Wikipedia"),
sMethodParameter.add(new ParameterSet().value("http://amazon.com").name("Amazon")); new ParameterSet().value("http://amazon.com").name("Amazon"),
sMethodParameter.add(new ParameterSet().value("http://youtube.com").name("Youtube")); new ParameterSet().value("http://youtube.com").name("Youtube"),
sMethodParameter.add(new ParameterSet().value("http://yahoo.com").name("Yahoo")); new ParameterSet().value("http://yahoo.com").name("Yahoo"),
sMethodParameter.add(new ParameterSet().value("http://ebay.com").name("Ebay")); new ParameterSet().value("http://ebay.com").name("Ebay"),
sMethodParameter.add(new ParameterSet().value("http://reddit.com").name("reddit")); new ParameterSet().value("http://reddit.com").name("reddit")
);
}
} }
@Rule @Rule
...@@ -66,7 +68,7 @@ public class PageCyclerTest { ...@@ -66,7 +68,7 @@ public class PageCyclerTest {
@Test @Test
@LargeTest @LargeTest
@UseMethodParameter("A") @UseMethodParameter(WebsiteParams.class)
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET) @Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
public void testVisitPage(String url) throws Throwable { public void testVisitPage(String url) throws Throwable {
final PageCyclerWebViewClient pageCyclerWebViewClient = new PageCyclerWebViewClient(); final PageCyclerWebViewClient pageCyclerWebViewClient = new PageCyclerWebViewClient();
......
...@@ -2831,13 +2831,13 @@ if (is_android) { ...@@ -2831,13 +2831,13 @@ if (is_android) {
"test/android/javatests/src/org/chromium/base/test/params/BaseJUnit4RunnerDelegate.java", "test/android/javatests/src/org/chromium/base/test/params/BaseJUnit4RunnerDelegate.java",
"test/android/javatests/src/org/chromium/base/test/params/MethodParamAnnotationRule.java", "test/android/javatests/src/org/chromium/base/test/params/MethodParamAnnotationRule.java",
"test/android/javatests/src/org/chromium/base/test/params/MethodParamRule.java", "test/android/javatests/src/org/chromium/base/test/params/MethodParamRule.java",
"test/android/javatests/src/org/chromium/base/test/params/ParameterizedRunnerDelegateFactory.java", "test/android/javatests/src/org/chromium/base/test/params/ParameterAnnotations.java",
"test/android/javatests/src/org/chromium/base/test/params/ParameterizedFrameworkMethod.java", "test/android/javatests/src/org/chromium/base/test/params/ParameterizedFrameworkMethod.java",
"test/android/javatests/src/org/chromium/base/test/params/ParameterSet.java",
"test/android/javatests/src/org/chromium/base/test/params/ParameterizedRunnerDelegate.java", "test/android/javatests/src/org/chromium/base/test/params/ParameterizedRunnerDelegate.java",
"test/android/javatests/src/org/chromium/base/test/params/ParameterizedRunnerDelegateCommon.java", "test/android/javatests/src/org/chromium/base/test/params/ParameterizedRunnerDelegateCommon.java",
"test/android/javatests/src/org/chromium/base/test/params/ParameterAnnotations.java", "test/android/javatests/src/org/chromium/base/test/params/ParameterizedRunnerDelegateFactory.java",
"test/android/javatests/src/org/chromium/base/test/params/ParameterProvider.java", "test/android/javatests/src/org/chromium/base/test/params/ParameterProvider.java",
"test/android/javatests/src/org/chromium/base/test/params/ParameterSet.java",
"test/android/javatests/src/org/chromium/base/test/util/AdvancedMockContext.java", "test/android/javatests/src/org/chromium/base/test/util/AdvancedMockContext.java",
"test/android/javatests/src/org/chromium/base/test/util/AnnotationRule.java", "test/android/javatests/src/org/chromium/base/test/util/AnnotationRule.java",
"test/android/javatests/src/org/chromium/base/test/util/CallbackHelper.java", "test/android/javatests/src/org/chromium/base/test/util/CallbackHelper.java",
......
...@@ -28,6 +28,10 @@ ...@@ -28,6 +28,10 @@
public static **[] values(); public static **[] values();
} }
# Keep classes implementing ParameterProvider -- these will be instantiated
# via reflection.
-keep class * implements org.chromium.base.test.params.ParameterProvider
# Allows Proguard freedom in removing these log related calls. We ask for debug # Allows Proguard freedom in removing these log related calls. We ask for debug
# and verbose logs to be stripped out in base.Log, so we are just ensuring we # and verbose logs to be stripped out in base.Log, so we are just ensuring we
# get rid of all other debug/verbose logs. # get rid of all other debug/verbose logs.
......
...@@ -21,8 +21,8 @@ import java.util.List; ...@@ -21,8 +21,8 @@ import java.util.List;
*/ */
public class MethodParamAnnotationRule extends MethodParamRule { public class MethodParamAnnotationRule extends MethodParamRule {
@Override @Override
protected Statement applyParameterAndValues( protected Statement applyParameterAndValues(final Statement base, Object target,
final Statement base, Object target, String parameterName, List<Object> values) { Class<? extends ParameterProvider> parameterProvider, List<Object> values) {
final List<Method> beforeMethods = new ArrayList<>(); final List<Method> beforeMethods = new ArrayList<>();
final List<Method> afterMethods = new ArrayList<>(); final List<Method> afterMethods = new ArrayList<>();
for (Method m : target.getClass().getDeclaredMethods()) { for (Method m : target.getClass().getDeclaredMethods()) {
...@@ -31,13 +31,13 @@ public class MethodParamAnnotationRule extends MethodParamRule { ...@@ -31,13 +31,13 @@ public class MethodParamAnnotationRule extends MethodParamRule {
UseMethodParameterBefore beforeAnnotation = UseMethodParameterBefore beforeAnnotation =
m.getAnnotation(UseMethodParameterBefore.class); m.getAnnotation(UseMethodParameterBefore.class);
if (beforeAnnotation != null && beforeAnnotation.value().equals(parameterName)) { if (beforeAnnotation != null && beforeAnnotation.value().equals(parameterProvider)) {
beforeMethods.add(m); beforeMethods.add(m);
} }
UseMethodParameterAfter afterAnnotation = UseMethodParameterAfter afterAnnotation =
m.getAnnotation(UseMethodParameterAfter.class); m.getAnnotation(UseMethodParameterAfter.class);
if (afterAnnotation != null && afterAnnotation.value().equals(parameterName)) { if (afterAnnotation != null && afterAnnotation.value().equals(parameterProvider)) {
afterMethods.add(m); afterMethods.add(m);
} }
} }
......
...@@ -14,22 +14,22 @@ import java.util.List; ...@@ -14,22 +14,22 @@ import java.util.List;
/** /**
* Abstract base class for rules that are applied to test methods using * Abstract base class for rules that are applied to test methods using
* {@link org.chromium.base.test.params.ParameterAnnotations.MethodParameter method parameters}. * {@link org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter method parameters}.
*/ */
public abstract class MethodParamRule implements MethodRule { public abstract class MethodParamRule implements MethodRule {
@Override @Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) { public Statement apply(final Statement base, FrameworkMethod method, Object target) {
UseMethodParameter useMethodParameter = method.getAnnotation(UseMethodParameter.class); UseMethodParameter useParameterProvider = method.getAnnotation(UseMethodParameter.class);
if (useMethodParameter == null) return base; if (useParameterProvider == null) return base;
String parameterName = useMethodParameter.value(); Class<? extends ParameterProvider> parameterProvider = useParameterProvider.value();
if (!(method instanceof ParameterizedFrameworkMethod)) return base; if (!(method instanceof ParameterizedFrameworkMethod)) return base;
ParameterSet parameters = ((ParameterizedFrameworkMethod) method).getParameterSet(); ParameterSet parameters = ((ParameterizedFrameworkMethod) method).getParameterSet();
List<Object> values = parameters.getValues(); List<Object> values = parameters.getValues();
return applyParameterAndValues(base, target, parameterName, values); return applyParameterAndValues(base, target, parameterProvider, values);
} }
protected abstract Statement applyParameterAndValues( protected abstract Statement applyParameterAndValues(final Statement base, Object target,
final Statement base, Object target, String parameterName, List<Object> values); Class<? extends ParameterProvider> parameterProvider, List<Object> values);
} }
...@@ -13,45 +13,42 @@ import java.lang.annotation.Target; ...@@ -13,45 +13,42 @@ import java.lang.annotation.Target;
* Annotations for Parameterized Tests * Annotations for Parameterized Tests
*/ */
public class ParameterAnnotations { public class ParameterAnnotations {
/**
* Annotation for test methods to indicate associated List<ParameterSet>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UseMethodParameter {
String value();
}
/** /**
* Annotation for test methods to indicate associated {@link ParameterProvider}. * Annotation for test methods to indicate associated {@link ParameterProvider}.
* Note: the class referred to must be public and have a public default constructor. * Note: the class referred to must be public and have a public default constructor.
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
public @interface UseParameterProvider { public @interface UseMethodParameter {
Class<? extends ParameterProvider> value(); Class<? extends ParameterProvider> value();
} }
/** /**
* Annotation for methods that should be called before running a test with method parameters. * Annotation for methods that should be called before running a test with method parameters.
* @see MethodParameter *
* In order to use this, add a {@link MethodParamAnnotationRule} annotated with
* {@code @}{@link org.junit.Rule Rule} to your test class.
* @see ParameterProvider
* @see UseMethodParameterAfter * @see UseMethodParameterAfter
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
public @interface UseMethodParameterBefore { public @interface UseMethodParameterBefore {
String value(); Class<? extends ParameterProvider> value();
} }
/** /**
* Annotation for methods that should be called after running a test with method parameters. * Annotation for methods that should be called after running a test with method parameters.
* @see MethodParameter *
* In order to use this, add a {@link MethodParamAnnotationRule} annotated with
* {@code @}{@link org.junit.Rule Rule} to your test class.
* @see ParameterProvider
* @see UseMethodParameterBefore * @see UseMethodParameterBefore
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
public @interface UseMethodParameterAfter { public @interface UseMethodParameterAfter {
String value(); Class<? extends ParameterProvider> value();
} }
/** /**
...@@ -61,15 +58,6 @@ public class ParameterAnnotations { ...@@ -61,15 +58,6 @@ public class ParameterAnnotations {
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
public @interface ClassParameter {} public @interface ClassParameter {}
/**
* Annotation for static field of a `List<ParameterSet>` for certain test methods
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MethodParameter {
String value();
}
/** /**
* Annotation for static field of a `List<ParameterSet>` of TestRule * Annotation for static field of a `List<ParameterSet>` of TestRule
*/ */
......
...@@ -4,10 +4,8 @@ ...@@ -4,10 +4,8 @@
package org.chromium.base.test.params; package org.chromium.base.test.params;
import java.util.List;
/** /**
* Generator to use generate arguments for parameterized test methods. * Generator to use generate arguments for parameterized test methods.
* @see ParameterAnnotations.UseParameterProvider * @see ParameterAnnotations.UseMethodParameter
*/ */
public interface ParameterProvider { List<ParameterSet> getParameters(); } public interface ParameterProvider { Iterable<ParameterSet> getParameters(); }
...@@ -99,6 +99,7 @@ public class ParameterSet { ...@@ -99,6 +99,7 @@ public class ParameterSet {
} }
int size() { int size() {
if (mValues == null) return 0;
return mValues.size(); return mValues.size();
} }
......
...@@ -12,7 +12,7 @@ import org.junit.runners.model.FrameworkField; ...@@ -12,7 +12,7 @@ import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.TestClass; import org.junit.runners.model.TestClass;
import org.chromium.base.test.params.ParameterAnnotations.ClassParameter; import org.chromium.base.test.params.ParameterAnnotations.ClassParameter;
import org.chromium.base.test.params.ParameterAnnotations.MethodParameter; import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate; import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;
import org.chromium.base.test.params.ParameterizedRunnerDelegateFactory.ParameterizedRunnerDelegateInstantiationException; import org.chromium.base.test.params.ParameterizedRunnerDelegateFactory.ParameterizedRunnerDelegateInstantiationException;
...@@ -20,21 +20,16 @@ import java.lang.reflect.Modifier; ...@@ -20,21 +20,16 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
/** /**
* ParameterizedRunner generates a list of runners for each of class parameter set in a test class. * ParameterizedRunner generates a list of runners for each of class parameter set in a test class.
* *
* ParameterizedRunner looks for {@code @ClassParameter} annotation in test class and * ParameterizedRunner looks for {@code @ClassParameter} annotation in test class and
* generates a list of ParameterizedRunnerDelegate runners for each ParameterSet. The class * generates a list of ParameterizedRunnerDelegate runners for each ParameterSet.
* runner also looks for {@code @MethodParameter} annotation, and creates a map that maps Strings
* value(tag) to ParameterSet List.
*/ */
public final class ParameterizedRunner extends Suite { public final class ParameterizedRunner extends Suite {
private static final String TAG = "ParameterizedRunner";
private final List<Runner> mRunners; private final List<Runner> mRunners;
/** /**
...@@ -97,10 +92,10 @@ public final class ParameterizedRunner extends Suite { ...@@ -97,10 +92,10 @@ public final class ParameterizedRunner extends Suite {
private void validateAtLeastOneParameterSetField() { private void validateAtLeastOneParameterSetField() {
if (getTestClass().getAnnotatedFields(ClassParameter.class).isEmpty() if (getTestClass().getAnnotatedFields(ClassParameter.class).isEmpty()
&& getTestClass().getAnnotatedFields(MethodParameter.class).isEmpty()) { && getTestClass().getAnnotatedMethods(UseMethodParameter.class).isEmpty()) {
throw new IllegalArgumentException(String.format(Locale.getDefault(), throw new IllegalArgumentException(String.format(Locale.getDefault(),
"%s has no field annotated with @ClassParameter or @MethodParameter field, " "%s has no field annotated with @ClassParameter or method annotated with"
+ "it should not use ParameterizedRunner", + "@UseMethodParameter; it should not use ParameterizedRunner",
getTestClass().getName())); getTestClass().getName()));
} }
} }
...@@ -119,8 +114,8 @@ public final class ParameterizedRunner extends Suite { ...@@ -119,8 +114,8 @@ public final class ParameterizedRunner extends Suite {
* *
* For method parameter set: a single list method parameter sets is associated with * For method parameter set: a single list method parameter sets is associated with
* a string tag, an immutable map of string to parameter set list will be created and * a string tag, an immutable map of string to parameter set list will be created and
* passed into factory for each runner delegate to create multiple tests. only one * passed into factory for each runner delegate to create multiple tests. Only one
* Runner will be created for a method that uses @MethodParameter, regardless of the * Runner will be created for a method that uses @UseMethodParameter, regardless of the
* number of ParameterSets in the associated list. * number of ParameterSets in the associated list.
* *
* @return a list of runners * @return a list of runners
...@@ -145,35 +140,19 @@ public final class ParameterizedRunner extends Suite { ...@@ -145,35 +140,19 @@ public final class ParameterizedRunner extends Suite {
Class<? extends ParameterizedRunnerDelegate> runnerDelegateClass = Class<? extends ParameterizedRunnerDelegate> runnerDelegateClass =
getRunnerDelegateClass(testClass); getRunnerDelegateClass(testClass);
ParameterizedRunnerDelegateFactory factory = new ParameterizedRunnerDelegateFactory(); ParameterizedRunnerDelegateFactory factory = new ParameterizedRunnerDelegateFactory();
Map<String, List<ParameterSet>> tagToMethodParameterSetList =
Collections.unmodifiableMap(generateMethodParameterMap(testClass));
List<Runner> runnersForTestClass = new ArrayList<>(); List<Runner> runnersForTestClass = new ArrayList<>();
for (ParameterSet classParameterSet : classParameterSetList) { for (ParameterSet classParameterSet : classParameterSetList) {
BlockJUnit4ClassRunner runner = (BlockJUnit4ClassRunner) factory.createRunner( BlockJUnit4ClassRunner runner = (BlockJUnit4ClassRunner) factory.createRunner(
testClass, classParameterSet, tagToMethodParameterSetList, runnerDelegateClass); testClass, classParameterSet, runnerDelegateClass);
runnersForTestClass.add(runner); runnersForTestClass.add(runner);
} }
return runnersForTestClass; return runnersForTestClass;
} }
/**
* Returns a map between MethodParameter tags and corresponding ParameterSetLists.
*/
static Map<String, List<ParameterSet>> generateMethodParameterMap(TestClass testClass)
throws IllegalAccessException {
Map<String, List<ParameterSet>> result = new HashMap<>();
for (FrameworkField field : testClass.getAnnotatedFields(MethodParameter.class)) {
List<ParameterSet> parameterSetList = getParameterSetList(field, testClass);
validateWidth(parameterSetList);
result.put(field.getAnnotation(MethodParameter.class).value(), parameterSetList);
}
return result;
}
/** /**
* Return an unmodifiable list of ParameterSet through a FrameworkField * Return an unmodifiable list of ParameterSet through a FrameworkField
*/ */
static List<ParameterSet> getParameterSetList(FrameworkField field, TestClass testClass) private static List<ParameterSet> getParameterSetList(FrameworkField field, TestClass testClass)
throws IllegalAccessException { throws IllegalAccessException {
field.getField().setAccessible(true); field.getField().setAccessible(true);
if (!Modifier.isStatic(field.getField().getModifiers())) { if (!Modifier.isStatic(field.getField().getModifiers())) {
...@@ -183,8 +162,8 @@ public final class ParameterizedRunner extends Suite { ...@@ -183,8 +162,8 @@ public final class ParameterizedRunner extends Suite {
} }
if (!(field.get(testClass.getJavaClass()) instanceof List)) { if (!(field.get(testClass.getJavaClass()) instanceof List)) {
throw new IllegalArgumentException(String.format(Locale.getDefault(), throw new IllegalArgumentException(String.format(Locale.getDefault(),
"Fields with @ClassParameter or @MethodParameter annotations must be an" "Fields with @ClassParameter annotations must be an instance of List, "
+ " instance of List, this field %s in %s is not list", + "this field %s in %s is not list",
field.getName(), testClass.getName())); field.getName(), testClass.getName()));
} }
@SuppressWarnings("unchecked") // checked above @SuppressWarnings("unchecked") // checked above
...@@ -192,9 +171,13 @@ public final class ParameterizedRunner extends Suite { ...@@ -192,9 +171,13 @@ public final class ParameterizedRunner extends Suite {
return Collections.unmodifiableList(result); return Collections.unmodifiableList(result);
} }
static void validateWidth(List<ParameterSet> parameterSetList) { static void validateWidth(Iterable<ParameterSet> parameterSetList) {
int lastSize = -1; int lastSize = -1;
for (ParameterSet set : parameterSetList) { for (ParameterSet set : parameterSetList) {
if (set.size() == 0) {
throw new IllegalParameterArgumentException(
"No parameter is added to method ParameterSet");
}
if (lastSize == -1 || set.size() == lastSize) { if (lastSize == -1 || set.size() == lastSize) {
lastSize = set.size(); lastSize = set.size();
} else { } else {
...@@ -220,7 +203,7 @@ public final class ParameterizedRunner extends Suite { ...@@ -220,7 +203,7 @@ public final class ParameterizedRunner extends Suite {
} }
static class IllegalParameterArgumentException extends IllegalArgumentException { static class IllegalParameterArgumentException extends IllegalArgumentException {
public IllegalParameterArgumentException(String msg) { IllegalParameterArgumentException(String msg) {
super(msg); super(msg);
} }
} }
......
...@@ -4,22 +4,17 @@ ...@@ -4,22 +4,17 @@
package org.chromium.base.test.params; package org.chromium.base.test.params;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.TestClass; import org.junit.runners.model.TestClass;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter; import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseParameterProvider;
import org.chromium.base.test.params.ParameterizedRunner.ParameterizedTestInstantiationException; import org.chromium.base.test.params.ParameterizedRunner.ParameterizedTestInstantiationException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/** /**
* Factory to generate delegate class runners for ParameterizedRunner * Factory to generate delegate class runners for ParameterizedRunner
...@@ -30,29 +25,24 @@ public class ParameterizedRunnerDelegateFactory { ...@@ -30,29 +25,24 @@ public class ParameterizedRunnerDelegateFactory {
* *
* @param testClass the TestClass object for current test class * @param testClass the TestClass object for current test class
* @param classParameterSet A parameter set for test constructor arguments * @param classParameterSet A parameter set for test constructor arguments
* @param testMethodToParameterSetListMap maps annotation tag to list of parameter set
* @param parameterizedRunnerDelegateClass the parameterized runner delegate class specified * @param parameterizedRunnerDelegateClass the parameterized runner delegate class specified
* through {@code @UseRunnerDelegate} * through {@code @UseRunnerDelegate}
*/ */
<T extends ParameterizedRunnerDelegate> T createRunner(TestClass testClass, <T extends ParameterizedRunnerDelegate> T createRunner(TestClass testClass,
ParameterSet classParameterSet, ParameterSet classParameterSet,
Map<String, List<ParameterSet>> testMethodToParameterSetListMap,
Class<T> parameterizedRunnerDelegateClass) Class<T> parameterizedRunnerDelegateClass)
throws ParameterizedTestInstantiationException, throws ParameterizedTestInstantiationException,
ParameterizedRunnerDelegateInstantiationException { ParameterizedRunnerDelegateInstantiationException {
String testMethodPostfix = classParameterSet == null ? null : classParameterSet.getName(); String testMethodPostfix = classParameterSet == null ? null : classParameterSet.getName();
List<FrameworkMethod> unmodifiableFrameworkMethodList = List<FrameworkMethod> unmodifiableFrameworkMethodList =
generateUnmodifiableFrameworkMethodList( generateUnmodifiableFrameworkMethodList(testClass, testMethodPostfix);
testClass, testMethodToParameterSetListMap, testMethodPostfix);
Object test = createTest(testClass, classParameterSet); Object test = createTest(testClass, classParameterSet);
ParameterizedRunnerDelegateCommon delegateCommon = ParameterizedRunnerDelegateCommon delegateCommon =
new ParameterizedRunnerDelegateCommon(test, unmodifiableFrameworkMethodList); new ParameterizedRunnerDelegateCommon(test, unmodifiableFrameworkMethodList);
try { try {
T runnerDelegate = parameterizedRunnerDelegateClass return parameterizedRunnerDelegateClass
.getDeclaredConstructor( .getDeclaredConstructor(Class.class, ParameterizedRunnerDelegateCommon.class)
Class.class, ParameterizedRunnerDelegateCommon.class)
.newInstance(testClass.getJavaClass(), delegateCommon); .newInstance(testClass.getJavaClass(), delegateCommon);
return runnerDelegate;
} catch (Exception e) { } catch (Exception e) {
throw new ParameterizedRunnerDelegateInstantiationException( throw new ParameterizedRunnerDelegateInstantiationException(
parameterizedRunnerDelegateClass.toString(), e); parameterizedRunnerDelegateClass.toString(), e);
...@@ -65,33 +55,18 @@ public class ParameterizedRunnerDelegateFactory { ...@@ -65,33 +55,18 @@ public class ParameterizedRunnerDelegateFactory {
* *
* @param testClass a {@code TestClass} that wraps around the actual java * @param testClass a {@code TestClass} that wraps around the actual java
* test class * test class
* @param tagToParameterSetList A map of String tags to ParameterSetList
* @param postFix a name postfix for each test * @param postFix a name postfix for each test
* @return a list of ParameterizedFrameworkMethod * @return a list of ParameterizedFrameworkMethod
*/ */
static List<FrameworkMethod> generateUnmodifiableFrameworkMethodList(TestClass testClass, static List<FrameworkMethod> generateUnmodifiableFrameworkMethodList(
Map<String, List<ParameterSet>> tagToParameterSetList, String postFix) { TestClass testClass, String postFix) {
// A Map that maps string tag X to a list of test framework methods that are
// annotated with @UseMethodParameter(X)
Map<String, List<FrameworkMethod>> tagToListOfFrameworkMethod = new HashMap<>();
// Represent the list of all ParameterizedFrameworkMethod in this test class // Represent the list of all ParameterizedFrameworkMethod in this test class
List<FrameworkMethod> returnList = new ArrayList<>(); List<FrameworkMethod> returnList = new ArrayList<>();
// Create tagToListOfFrameworkMethod
for (FrameworkMethod method : testClass.getAnnotatedMethods(Test.class)) { for (FrameworkMethod method : testClass.getAnnotatedMethods(Test.class)) {
if (method.getMethod().isAnnotationPresent(UseMethodParameter.class)) { if (method.getMethod().isAnnotationPresent(UseMethodParameter.class)) {
String currentGroup = method.getAnnotation(UseMethodParameter.class).value(); Iterable<ParameterSet> parameterSets =
if (tagToListOfFrameworkMethod.get(currentGroup) == null) { getParameters(method.getAnnotation(UseMethodParameter.class).value());
List<FrameworkMethod> list = new ArrayList<>();
list.add(method);
tagToListOfFrameworkMethod.put(currentGroup, list);
} else {
tagToListOfFrameworkMethod.get(currentGroup).add(method);
}
} else if (method.getMethod().isAnnotationPresent(UseParameterProvider.class)) {
List<ParameterSet> parameterSets =
getParameters(method.getAnnotation(UseParameterProvider.class).value());
returnList.addAll(createParameterizedMethods(method, parameterSets, postFix)); returnList.addAll(createParameterizedMethods(method, parameterSets, postFix));
} else { } else {
// If test method is not parameterized (does not have UseMethodParameter annotation) // If test method is not parameterized (does not have UseMethodParameter annotation)
...@@ -99,22 +74,6 @@ public class ParameterizedRunnerDelegateFactory { ...@@ -99,22 +74,6 @@ public class ParameterizedRunnerDelegateFactory {
} }
} }
Assert.assertArrayEquals(
"All parameters used by must be defined, and all defined parameters must be used.",
tagToParameterSetList.keySet().toArray(),
tagToListOfFrameworkMethod.keySet().toArray());
// Loop through each of the tags and create all the parameterized framework
// methods for every method parameter set in the method parameter set list
// annotated with that tag
for (Entry<String, List<ParameterSet>> entry : tagToParameterSetList.entrySet()) {
String tagString = entry.getKey();
List<ParameterSet> parameterSetList = entry.getValue();
for (FrameworkMethod method : tagToListOfFrameworkMethod.get(tagString)) {
returnList.addAll(createParameterizedMethods(method, parameterSetList, postFix));
}
}
return Collections.unmodifiableList(returnList); return Collections.unmodifiableList(returnList);
} }
...@@ -153,7 +112,7 @@ public class ParameterizedRunnerDelegateFactory { ...@@ -153,7 +112,7 @@ public class ParameterizedRunnerDelegateFactory {
} }
} }
private static List<ParameterSet> getParameters(Class<? extends ParameterProvider> clazz) { private static Iterable<ParameterSet> getParameters(Class<? extends ParameterProvider> clazz) {
ParameterProvider parameterProvider; ParameterProvider parameterProvider;
try { try {
parameterProvider = clazz.getDeclaredConstructor().newInstance(); parameterProvider = clazz.getDeclaredConstructor().newInstance();
...@@ -170,12 +129,10 @@ public class ParameterizedRunnerDelegateFactory { ...@@ -170,12 +129,10 @@ public class ParameterizedRunnerDelegateFactory {
} }
private static List<FrameworkMethod> createParameterizedMethods( private static List<FrameworkMethod> createParameterizedMethods(
FrameworkMethod baseMethod, List<ParameterSet> parameterSetList, String suffix) { FrameworkMethod baseMethod, Iterable<ParameterSet> parameterSetList, String suffix) {
ParameterizedRunner.validateWidth(parameterSetList);
List<FrameworkMethod> returnList = new ArrayList<>(); List<FrameworkMethod> returnList = new ArrayList<>();
for (ParameterSet set : parameterSetList) { for (ParameterSet set : parameterSetList) {
if (set.getValues() == null) {
throw new IllegalArgumentException("No parameter is added to method ParameterSet");
}
returnList.add(new ParameterizedFrameworkMethod(baseMethod.getMethod(), set, suffix)); returnList.add(new ParameterizedFrameworkMethod(baseMethod.getMethod(), set, suffix));
} }
return returnList; return returnList;
......
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
package org.chromium.base.test.params; package org.chromium.base.test.params;
import static org.chromium.base.test.params.ParameterAnnotations.MethodParameter;
import static org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
...@@ -14,16 +11,16 @@ import org.junit.rules.MethodRule; ...@@ -14,16 +11,16 @@ import org.junit.rules.MethodRule;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.test.params.ParameterAnnotations.ClassParameter; import org.chromium.base.test.params.ParameterAnnotations.ClassParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameterAfter; import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameterAfter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameterBefore; import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameterBefore;
import org.chromium.base.test.params.ParameterAnnotations.UseParameterProvider;
import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate; import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
* Example test that uses ParamRunner * Example test that uses ParameterizedRunner
*/ */
@RunWith(ParameterizedRunner.class) @RunWith(ParameterizedRunner.class)
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class) @UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
...@@ -34,23 +31,28 @@ public class ExampleParameterizedTest { ...@@ -34,23 +31,28 @@ public class ExampleParameterizedTest {
new ParameterSet().value("Xxxx", "Yyyy").name("XxxxYyyy"), new ParameterSet().value("Xxxx", "Yyyy").name("XxxxYyyy"),
new ParameterSet().value("aa", "yy").name("AaYy")); new ParameterSet().value("aa", "yy").name("AaYy"));
@MethodParameter("A") public static class MethodParamsA implements ParameterProvider {
private static List<ParameterSet> sMethodParamA = private static List<ParameterSet> sMethodParamA =
Arrays.asList(new ParameterSet().value(1, 2).name("OneTwo"), Arrays.asList(new ParameterSet().value(1, 2).name("OneTwo"),
new ParameterSet().value(2, 3).name("TwoThree"), new ParameterSet().value(2, 3).name("TwoThree"),
new ParameterSet().value(3, 4).name("ThreeFour")); new ParameterSet().value(3, 4).name("ThreeFour"));
@MethodParameter("B") @Override
public List<ParameterSet> getParameters() {
return sMethodParamA;
}
}
public static class MethodParamsB implements ParameterProvider {
private static List<ParameterSet> sMethodParamB = private static List<ParameterSet> sMethodParamB =
Arrays.asList(new ParameterSet().value("a", "b").name("Ab"), Arrays.asList(new ParameterSet().value("a", "b").name("Ab"),
new ParameterSet().value("b", "c").name("Bc"), new ParameterSet().value("b", "c").name("Bc"),
new ParameterSet().value("c", "d").name("Cd"), new ParameterSet().value("c", "d").name("Cd"),
new ParameterSet().value("d", "e").name("De")); new ParameterSet().value("d", "e").name("De"));
public static class MethodParamsA implements ParameterProvider {
@Override @Override
public List<ParameterSet> getParameters() { public List<ParameterSet> getParameters() {
return sMethodParamA; return sMethodParamB;
} }
} }
...@@ -73,35 +75,29 @@ public class ExampleParameterizedTest { ...@@ -73,35 +75,29 @@ public class ExampleParameterizedTest {
private Integer mSum; private Integer mSum;
@UseMethodParameterBefore("A") @UseMethodParameterBefore(MethodParamsA.class)
public void setupWithOnlyA(int intA, int intB) { public void setupWithOnlyA(int intA, int intB) {
mSum = intA + intB; mSum = intA + intB;
} }
@Test @Test
@UseMethodParameter("A") @UseMethodParameter(MethodParamsA.class)
public void testWithOnlyA(int intA, int intB) { public void testWithOnlyA(int intA, int intB) {
Assert.assertEquals(intA + 1, intB); Assert.assertEquals(intA + 1, intB);
Assert.assertEquals(mSum, Integer.valueOf(intA + intB)); Assert.assertEquals(mSum, Integer.valueOf(intA + intB));
mSum = null; mSum = null;
} }
@Test
@UseParameterProvider(MethodParamsA.class)
public void testWithOnlyAFromGenerator(int intA, int intB) {
Assert.assertEquals(intA + 1, intB);
}
private String mConcatenation; private String mConcatenation;
@Test @Test
@UseMethodParameter("B") @UseMethodParameter(MethodParamsB.class)
public void testWithOnlyB(String a, String b) { public void testWithOnlyB(String a, String b) {
Assert.assertTrue(!a.equals(b)); Assert.assertTrue(!a.equals(b));
mConcatenation = a + b; mConcatenation = a + b;
} }
@UseMethodParameterAfter("B") @UseMethodParameterAfter(MethodParamsB.class)
public void teardownWithOnlyB(String a, String b) { public void teardownWithOnlyB(String a, String b) {
Assert.assertEquals(mConcatenation, a + b); Assert.assertEquals(mConcatenation, a + b);
mConcatenation = null; mConcatenation = null;
......
...@@ -17,8 +17,7 @@ import org.chromium.base.test.params.ParameterizedRunner.ParameterizedTestInstan ...@@ -17,8 +17,7 @@ import org.chromium.base.test.params.ParameterizedRunner.ParameterizedTestInstan
import org.chromium.base.test.params.ParameterizedRunnerDelegateFactory.ParameterizedRunnerDelegateInstantiationException; import org.chromium.base.test.params.ParameterizedRunnerDelegateFactory.ParameterizedRunnerDelegateInstantiationException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -72,13 +71,34 @@ public class ParameterizedRunnerDelegateFactoryTest { ...@@ -72,13 +71,34 @@ public class ParameterizedRunnerDelegateFactoryTest {
} }
static class ExampleTestClass { static class ExampleTestClass {
static class MethodParamsA implements ParameterProvider {
@Override
public Iterable<ParameterSet> getParameters() {
return Arrays.asList(
new ParameterSet().value("a").name("testWithValue_a"),
new ParameterSet().value("b").name("testWithValue_b")
);
}
}
@SuppressWarnings("unused") @SuppressWarnings("unused")
@UseMethodParameter("A") @UseMethodParameter(MethodParamsA.class)
@Test @Test
public void testA(String a) {} public void testA(String a) {}
static class MethodParamsB implements ParameterProvider {
@Override
public Iterable<ParameterSet> getParameters() {
return Arrays.asList(
new ParameterSet().value(1).name("testWithValue_1"),
new ParameterSet().value(2).name("testWithValue_2"),
new ParameterSet().value(3).name("testWithValue_3")
);
}
}
@SuppressWarnings("unused") @SuppressWarnings("unused")
@UseMethodParameter("B") @UseMethodParameter(MethodParamsB.class)
@Test @Test
public void testB(int b) {} public void testB(int b) {}
...@@ -94,8 +114,7 @@ public class ParameterizedRunnerDelegateFactoryTest { ...@@ -94,8 +114,7 @@ public class ParameterizedRunnerDelegateFactoryTest {
public void testBadRunnerDelegateWithIncorrectValidationCall() throws Throwable { public void testBadRunnerDelegateWithIncorrectValidationCall() throws Throwable {
ParameterizedRunnerDelegateFactory factory = new ParameterizedRunnerDelegateFactory(); ParameterizedRunnerDelegateFactory factory = new ParameterizedRunnerDelegateFactory();
TestClass testClass = new TestClass(BadExampleRunnerDelegate.LalaTestClass.class); TestClass testClass = new TestClass(BadExampleRunnerDelegate.LalaTestClass.class);
factory.createRunner( factory.createRunner(testClass, null, BadExampleRunnerDelegate.class);
testClass, null, Collections.emptyMap(), BadExampleRunnerDelegate.class);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
...@@ -113,21 +132,9 @@ public class ParameterizedRunnerDelegateFactoryTest { ...@@ -113,21 +132,9 @@ public class ParameterizedRunnerDelegateFactoryTest {
@Test @Test
public void testGenerateParameterizedFrameworkMethod() throws Throwable { public void testGenerateParameterizedFrameworkMethod() throws Throwable {
Map<String, List<ParameterSet>> map = new HashMap<>();
List<ParameterSet> listA = new ArrayList<>();
listA.add(new ParameterSet().value("a").name("testWithValue_a"));
listA.add(new ParameterSet().value("b").name("testWithValue_b"));
List<ParameterSet> listB = new ArrayList<>();
listB.add(new ParameterSet().value(1).name("testWithValue_1"));
listB.add(new ParameterSet().value(2).name("testWithValue_2"));
listB.add(new ParameterSet().value(3).name("testWithValue_3"));
map.put("A", listA);
map.put("B", listB);
List<FrameworkMethod> methods = List<FrameworkMethod> methods =
ParameterizedRunnerDelegateFactory.generateUnmodifiableFrameworkMethodList( ParameterizedRunnerDelegateFactory.generateUnmodifiableFrameworkMethodList(
new TestClass(ExampleTestClass.class), map, ""); new TestClass(ExampleTestClass.class), "");
Assert.assertEquals(methods.size(), 6); Assert.assertEquals(methods.size(), 6);
...@@ -148,44 +155,4 @@ public class ParameterizedRunnerDelegateFactoryTest { ...@@ -148,44 +155,4 @@ public class ParameterizedRunnerDelegateFactoryTest {
} }
Assert.assertTrue(expectedTests.isEmpty()); Assert.assertTrue(expectedTests.isEmpty());
} }
@Test(expected = IllegalArgumentException.class)
public void testEmptyParameterSet() {
Map<String, List<ParameterSet>> map = new HashMap<>();
List<ParameterSet> listA = new ArrayList<>();
listA.add(new ParameterSet().value("a").name("testWithValue_a"));
List<ParameterSet> listB = new ArrayList<>();
listB.add(new ParameterSet()); //Empty parameter set
map.put("A", listA);
map.put("B", listB);
ParameterizedRunnerDelegateFactory.generateUnmodifiableFrameworkMethodList(
new TestClass(ExampleTestClass.class), map, "");
}
@Test(expected = AssertionError.class)
public void testMissingParameterSet() {
Map<String, List<ParameterSet>> map = new HashMap<>();
List<ParameterSet> listA = new ArrayList<>();
listA.add(new ParameterSet().value("a").name("testWithValue_a"));
map.put("A", listA);
//Missing ParameterSet list under group "B"
ParameterizedRunnerDelegateFactory.generateUnmodifiableFrameworkMethodList(
new TestClass(ExampleTestClass.class), map, "");
}
@Test(expected = AssertionError.class)
public void testMissingTestMethod() {
Map<String, List<ParameterSet>> map = new HashMap<>();
List<ParameterSet> listA = new ArrayList<>();
listA.add(new ParameterSet().value("a").name("testWithValue_a"));
List<ParameterSet> listB = new ArrayList<>();
listB.add(new ParameterSet().value(1).name("testWithValue_1"));
List<ParameterSet> listC = new ArrayList<>();
listC.add(new ParameterSet().value(10).name("extra"));
map.put("A", listA);
map.put("B", listB);
map.put("C", listC);
ParameterizedRunnerDelegateFactory.generateUnmodifiableFrameworkMethodList(
new TestClass(ExampleTestClass.class), map, "");
}
} }
...@@ -4,75 +4,22 @@ ...@@ -4,75 +4,22 @@
package org.chromium.base.test.params; package org.chromium.base.test.params;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.TestClass;
import org.chromium.base.test.params.ParameterAnnotations.ClassParameter; import org.chromium.base.test.params.ParameterAnnotations.ClassParameter;
import org.chromium.base.test.params.ParameterAnnotations.MethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate; import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;
import org.chromium.base.test.params.ParameterizedRunner.IllegalParameterArgumentException; import org.chromium.base.test.params.ParameterizedRunner.IllegalParameterArgumentException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Test for org.chromium.base.test.params.ParameterizedRunner * Test for org.chromium.base.test.params.ParameterizedRunner
*/ */
@RunWith(BlockJUnit4ClassRunner.class) @RunWith(BlockJUnit4ClassRunner.class)
public class ParameterizedRunnerTest { public class ParameterizedRunnerTest {
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class TestClassWithPrivateParameterSetList {
@ClassParameter
private static List<ParameterSet> sClassParams = new ArrayList<>();
static {
sClassParams.add(new ParameterSet().value(1));
sClassParams.add(new ParameterSet().value(2));
}
@MethodParameter("A")
private static List<ParameterSet> sMethodParamA = new ArrayList<>();
static {
sMethodParamA.add(new ParameterSet().value("a", "b"));
}
public TestClassWithPrivateParameterSetList(int x) {}
@Test
@UseMethodParameter("A")
public void test(String a, String b) {}
}
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class TestClassWithDefaultParameterSetList {
@ClassParameter
static List<ParameterSet> sClassParams = new ArrayList<>();
static {
sClassParams.add(new ParameterSet().value(1, 2));
}
@MethodParameter("A")
static List<ParameterSet> sMethodParamA = new ArrayList<>();
static {
sMethodParamA.add(new ParameterSet().value(null));
}
public TestClassWithDefaultParameterSetList(int a, int b) {}
@Test
@UseMethodParameter("A")
public void test(String x) {}
}
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class) @UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class BadTestClassWithMoreThanOneConstructor { public static class BadTestClassWithMoreThanOneConstructor {
@ClassParameter @ClassParameter
...@@ -101,25 +48,12 @@ public class ParameterizedRunnerTest { ...@@ -101,25 +48,12 @@ public class ParameterizedRunnerTest {
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class) @UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class BadTestClassWithNonStaticParameterSetList { public static class BadTestClassWithNonStaticParameterSetList {
@ClassParameter @ClassParameter
public List<ParameterSet> sClassParams = new ArrayList<>(); public List<ParameterSet> mClassParams = new ArrayList<>();
@Test @Test
public void test() {} public void test() {}
} }
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class BadTestClassWithMissingMethodParameter {
@MethodParameter("A")
private static List<ParameterSet> sParameterSetListA = new ArrayList<>();
@MethodParameter("B")
private static List<ParameterSet> sParameterSetListB = new ArrayList<>();
@Test
@UseMethodParameter("A")
public void testA() {}
}
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class) @UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class BadTestClassWithMultipleClassParameter { public static class BadTestClassWithMultipleClassParameter {
@ClassParameter @ClassParameter
...@@ -129,28 +63,11 @@ public class ParameterizedRunnerTest { ...@@ -129,28 +63,11 @@ public class ParameterizedRunnerTest {
private static List<ParameterSet> sParamB = new ArrayList<>(); private static List<ParameterSet> sParamB = new ArrayList<>();
} }
@Test @Test(expected = ParameterizedRunner.IllegalParameterArgumentException.class)
public void testPrivateAccessible() throws Throwable { public void testEmptyParameterSet() {
TestClass testClass = new TestClass(TestClassWithPrivateParameterSetList.class); List<ParameterSet> paramList = new ArrayList<>();
List<Runner> runners = ParameterizedRunner.createRunners(testClass); paramList.add(new ParameterSet());
Assert.assertEquals(runners.size(), 2); ParameterizedRunner.validateWidth(paramList);
Map<String, List<ParameterSet>> generatedMap =
ParameterizedRunner.generateMethodParameterMap(testClass);
Assert.assertEquals(generatedMap.keySet().size(), 1);
Assert.assertTrue(generatedMap.keySet().contains("A"));
Assert.assertEquals(generatedMap.get("A").size(), 1);
}
@Test
public void testDefaultAccessible() throws Throwable {
TestClass testClass = new TestClass(TestClassWithDefaultParameterSetList.class);
List<Runner> runners = ParameterizedRunner.createRunners(testClass);
Assert.assertEquals(runners.size(), 1);
Map<String, List<ParameterSet>> generatedMap =
ParameterizedRunner.generateMethodParameterMap(testClass);
Assert.assertEquals(generatedMap.keySet().size(), 1);
Assert.assertTrue(generatedMap.keySet().contains("A"));
Assert.assertEquals(generatedMap.get("A").size(), 1);
} }
@Test(expected = ParameterizedRunner.IllegalParameterArgumentException.class) @Test(expected = ParameterizedRunner.IllegalParameterArgumentException.class)
...@@ -171,31 +88,21 @@ public class ParameterizedRunnerTest { ...@@ -171,31 +88,21 @@ public class ParameterizedRunnerTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testBadClassWithNonListParameters() throws Throwable { public void testBadClassWithNonListParameters() throws Throwable {
ParameterizedRunner runner =
new ParameterizedRunner(BadTestClassWithNonListParameters.class); new ParameterizedRunner(BadTestClassWithNonListParameters.class);
} }
@Test(expected = IllegalParameterArgumentException.class) @Test(expected = IllegalParameterArgumentException.class)
public void testBadClassWithNonStaticParameterSetList() throws Throwable { public void testBadClassWithNonStaticParameterSetList() throws Throwable {
ParameterizedRunner runner =
new ParameterizedRunner(BadTestClassWithNonStaticParameterSetList.class); new ParameterizedRunner(BadTestClassWithNonStaticParameterSetList.class);
} }
@Test(expected = AssertionError.class)
public void testBadClassWithMissingMethodParameter() throws Throwable {
ParameterizedRunner runner =
new ParameterizedRunner(BadTestClassWithMissingMethodParameter.class);
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testBadClassWithoutNeedForParameterization() throws Throwable { public void testBadClassWithoutNeedForParameterization() throws Throwable {
ParameterizedRunner runner =
new ParameterizedRunner(BadTestClassWithoutNeedForParameterization.class); new ParameterizedRunner(BadTestClassWithoutNeedForParameterization.class);
} }
@Test(expected = Exception.class) @Test(expected = Exception.class)
public void testBadClassWithMoreThanOneConstructor() throws Throwable { public void testBadClassWithMoreThanOneConstructor() throws Throwable {
ParameterizedRunner runner =
new ParameterizedRunner(BadTestClassWithMoreThanOneConstructor.class); new ParameterizedRunner(BadTestClassWithMoreThanOneConstructor.class);
} }
} }
...@@ -13,7 +13,6 @@ import org.junit.runners.model.FrameworkMethod; ...@@ -13,7 +13,6 @@ import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.TestClass; import org.junit.runners.model.TestClass;
import org.chromium.base.test.params.ParameterAnnotations.ClassParameter; import org.chromium.base.test.params.ParameterAnnotations.ClassParameter;
import org.chromium.base.test.params.ParameterAnnotations.MethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter; import org.chromium.base.test.params.ParameterAnnotations.UseMethodParameter;
import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate; import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;
...@@ -30,11 +29,10 @@ public class ParameterizedTestNameTest { ...@@ -30,11 +29,10 @@ public class ParameterizedTestNameTest {
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class) @UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class TestClassWithClassParameterAppendName { public static class TestClassWithClassParameterAppendName {
@ClassParameter @ClassParameter
static List<ParameterSet> sAllName = new ArrayList<>(); static List<ParameterSet> sAllName = Arrays.asList(
static { new ParameterSet().value("hello").name("Hello"),
sAllName.add(new ParameterSet().value("hello").name("Hello")); new ParameterSet().value("world").name("World")
sAllName.add(new ParameterSet().value("world").name("World")); );
}
public TestClassWithClassParameterAppendName(String a) {} public TestClassWithClassParameterAppendName(String a) {}
...@@ -45,11 +43,10 @@ public class ParameterizedTestNameTest { ...@@ -45,11 +43,10 @@ public class ParameterizedTestNameTest {
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class) @UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class TestClassWithClassParameterDefaultName { public static class TestClassWithClassParameterDefaultName {
@ClassParameter @ClassParameter
static List<ParameterSet> sAllName = new ArrayList<>(); static List<ParameterSet> sAllName = Arrays.asList(
static { new ParameterSet().value("hello"),
sAllName.add(new ParameterSet().value("hello")); new ParameterSet().value("world")
sAllName.add(new ParameterSet().value("world")); );
}
public TestClassWithClassParameterDefaultName(String a) {} public TestClassWithClassParameterDefaultName(String a) {}
...@@ -59,25 +56,31 @@ public class ParameterizedTestNameTest { ...@@ -59,25 +56,31 @@ public class ParameterizedTestNameTest {
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class) @UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class TestClassWithMethodParameter { public static class TestClassWithMethodParameter {
@MethodParameter("A") static class AppendNameParams implements ParameterProvider {
static List<ParameterSet> sAppendName = new ArrayList<>(); @Override
static { public Iterable<ParameterSet> getParameters() {
sAppendName.add(new ParameterSet().value("hello").name("Hello")); return Arrays.asList(
sAppendName.add(new ParameterSet().value("world").name("World")); new ParameterSet().value("hello").name("Hello"),
new ParameterSet().value("world").name("World")
);
}
} }
@MethodParameter("B") static class DefaultNameParams implements ParameterProvider {
static List<ParameterSet> sDefaultName = new ArrayList<>(); @Override
static { public Iterable<ParameterSet> getParameters() {
sDefaultName.add(new ParameterSet().value("hello")); return Arrays.asList(
sDefaultName.add(new ParameterSet().value("world")); new ParameterSet().value("hello"),
new ParameterSet().value("world")
);
}
} }
@UseMethodParameter("A") @UseMethodParameter(AppendNameParams.class)
@Test @Test
public void test(String a) {} public void test(String a) {}
@UseMethodParameter("B") @UseMethodParameter(DefaultNameParams.class)
@Test @Test
public void testDefaultName(String b) {} public void testDefaultName(String b) {}
} }
...@@ -85,22 +88,24 @@ public class ParameterizedTestNameTest { ...@@ -85,22 +88,24 @@ public class ParameterizedTestNameTest {
@UseRunnerDelegate(BlockJUnit4RunnerDelegate.class) @UseRunnerDelegate(BlockJUnit4RunnerDelegate.class)
public static class TestClassWithMixedParameter { public static class TestClassWithMixedParameter {
@ClassParameter @ClassParameter
static List<ParameterSet> sAllName = new ArrayList<>(); static List<ParameterSet> sAllName = Arrays.asList(
static { new ParameterSet().value("hello").name("Hello"),
sAllName.add(new ParameterSet().value("hello").name("Hello")); new ParameterSet().value("world").name("World")
sAllName.add(new ParameterSet().value("world").name("World")); );
}
@MethodParameter("A") static class AppendNameParams implements ParameterProvider {
static List<ParameterSet> sAppendName = new ArrayList<>(); @Override
static { public Iterable<ParameterSet> getParameters() {
sAppendName.add(new ParameterSet().value("1").name("A")); return Arrays.asList(
sAppendName.add(new ParameterSet().value("2").name("B")); new ParameterSet().value("1").name("A"),
new ParameterSet().value("2").name("B")
);
}
} }
public TestClassWithMixedParameter(String a) {} public TestClassWithMixedParameter(String a) {}
@UseMethodParameter("A") @UseMethodParameter(AppendNameParams.class)
@Test @Test
public void testA(String a) {} public void testA(String a) {}
...@@ -113,7 +118,7 @@ public class ParameterizedTestNameTest { ...@@ -113,7 +118,7 @@ public class ParameterizedTestNameTest {
List<Runner> runners = ParameterizedRunner.createRunners( List<Runner> runners = ParameterizedRunner.createRunners(
new TestClass(TestClassWithClassParameterAppendName.class)); new TestClass(TestClassWithClassParameterAppendName.class));
List<String> expectedTestNames = List<String> expectedTestNames =
new LinkedList<String>(Arrays.asList(new String[] {"test__Hello", "test__World"})); new LinkedList<String>(Arrays.asList("test__Hello", "test__World"));
List<String> computedMethodNames = new ArrayList<>(); List<String> computedMethodNames = new ArrayList<>();
for (Runner r : runners) { for (Runner r : runners) {
BlockJUnit4RunnerDelegate castedRunner = (BlockJUnit4RunnerDelegate) r; BlockJUnit4RunnerDelegate castedRunner = (BlockJUnit4RunnerDelegate) r;
...@@ -137,8 +142,7 @@ public class ParameterizedTestNameTest { ...@@ -137,8 +142,7 @@ public class ParameterizedTestNameTest {
public void testClassParameterDefaultName() throws Throwable { public void testClassParameterDefaultName() throws Throwable {
List<Runner> runners = ParameterizedRunner.createRunners( List<Runner> runners = ParameterizedRunner.createRunners(
new TestClass(TestClassWithClassParameterDefaultName.class)); new TestClass(TestClassWithClassParameterDefaultName.class));
List<String> expectedTestNames = List<String> expectedTestNames = new LinkedList<String>(Arrays.asList("test", "test"));
new LinkedList<String>(Arrays.asList(new String[] {"test", "test"}));
for (Runner r : runners) { for (Runner r : runners) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
BlockJUnit4RunnerDelegate castedRunner = (BlockJUnit4RunnerDelegate) r; BlockJUnit4RunnerDelegate castedRunner = (BlockJUnit4RunnerDelegate) r;
...@@ -158,8 +162,8 @@ public class ParameterizedTestNameTest { ...@@ -158,8 +162,8 @@ public class ParameterizedTestNameTest {
public void testMethodParameter() throws Throwable { public void testMethodParameter() throws Throwable {
List<Runner> runners = ParameterizedRunner.createRunners( List<Runner> runners = ParameterizedRunner.createRunners(
new TestClass(TestClassWithMethodParameter.class)); new TestClass(TestClassWithMethodParameter.class));
List<String> expectedTestNames = new LinkedList<String>(Arrays.asList( List<String> expectedTestNames = new LinkedList<String>(
new String[] {"test__Hello", "test__World", "testDefaultName", "testDefaultName"})); Arrays.asList("test__Hello", "test__World", "testDefaultName", "testDefaultName"));
for (Runner r : runners) { for (Runner r : runners) {
BlockJUnit4RunnerDelegate castedRunner = (BlockJUnit4RunnerDelegate) r; BlockJUnit4RunnerDelegate castedRunner = (BlockJUnit4RunnerDelegate) r;
for (FrameworkMethod method : castedRunner.computeTestMethods()) { for (FrameworkMethod method : castedRunner.computeTestMethods()) {
...@@ -178,9 +182,9 @@ public class ParameterizedTestNameTest { ...@@ -178,9 +182,9 @@ public class ParameterizedTestNameTest {
public void testMixedParameterTestA() throws Throwable { public void testMixedParameterTestA() throws Throwable {
List<Runner> runners = List<Runner> runners =
ParameterizedRunner.createRunners(new TestClass(TestClassWithMixedParameter.class)); ParameterizedRunner.createRunners(new TestClass(TestClassWithMixedParameter.class));
List<String> expectedTestNames = new LinkedList<String>( List<String> expectedTestNames =
Arrays.asList(new String[] {"testA__Hello_A", "testA__World_A", "testA__Hello_B", new LinkedList<String>(Arrays.asList("testA__Hello_A", "testA__World_A",
"testA__World_B", "test__Hello", "test__World"})); "testA__Hello_B", "testA__World_B", "test__Hello", "test__World"));
for (Runner r : runners) { for (Runner r : runners) {
BlockJUnit4RunnerDelegate castedRunner = (BlockJUnit4RunnerDelegate) r; BlockJUnit4RunnerDelegate castedRunner = (BlockJUnit4RunnerDelegate) r;
for (FrameworkMethod method : castedRunner.computeTestMethods()) { for (FrameworkMethod method : castedRunner.computeTestMethods()) {
......
...@@ -30,6 +30,7 @@ import org.junit.runner.RunWith; ...@@ -30,6 +30,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.test.params.MethodParamAnnotationRule; import org.chromium.base.test.params.MethodParamAnnotationRule;
import org.chromium.base.test.params.ParameterAnnotations; import org.chromium.base.test.params.ParameterAnnotations;
import org.chromium.base.test.params.ParameterProvider;
import org.chromium.base.test.params.ParameterSet; import org.chromium.base.test.params.ParameterSet;
import org.chromium.base.test.params.ParameterizedRunner; import org.chromium.base.test.params.ParameterizedRunner;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
...@@ -119,10 +120,13 @@ public class NewTabPageTest { ...@@ -119,10 +120,13 @@ public class NewTabPageTest {
@Rule @Rule
public TestRule mFeatureRule = new Features.InstrumentationProcessor(); public TestRule mFeatureRule = new Features.InstrumentationProcessor();
@ParameterAnnotations.MethodParameter("Modern") public static class ModernParams implements ParameterProvider {
private static List<ParameterSet> sMethodParamModern = @Override
Arrays.asList(new ParameterSet().value(false).name("DisableChromeModern"), public Iterable<ParameterSet> getParameters() {
return Arrays.asList(new ParameterSet().value(false).name("DisableChromeModern"),
new ParameterSet().value(true).name("EnableChromeModern")); new ParameterSet().value(true).name("EnableChromeModern"));
}
}
private static final String TEST_PAGE = "/chrome/test/data/android/navigate/simple.html"; private static final String TEST_PAGE = "/chrome/test/data/android/navigate/simple.html";
...@@ -134,14 +138,14 @@ public class NewTabPageTest { ...@@ -134,14 +138,14 @@ public class NewTabPageTest {
private EmbeddedTestServer mTestServer; private EmbeddedTestServer mTestServer;
private List<SiteSuggestion> mSiteSuggestions; private List<SiteSuggestion> mSiteSuggestions;
@ParameterAnnotations.UseMethodParameterBefore("Modern") @ParameterAnnotations.UseMethodParameterBefore(ModernParams.class)
public void setupModernDesign(boolean enabled) { public void setupModernDesign(boolean enabled) {
mChromeModernProcessor.setPrefs(enabled); mChromeModernProcessor.setPrefs(enabled);
if (enabled) mRenderTestRule.setVariantPrefix("modern"); if (enabled) mRenderTestRule.setVariantPrefix("modern");
} }
@ParameterAnnotations.UseMethodParameterAfter("Modern") @ParameterAnnotations.UseMethodParameterAfter(ModernParams.class)
public void teardownModernDesign(boolean enabled) { public void teardownModernDesign(boolean enabled) {
mChromeModernProcessor.clearTestState(); mChromeModernProcessor.clearTestState();
} }
...@@ -202,7 +206,7 @@ public class NewTabPageTest { ...@@ -202,7 +206,7 @@ public class NewTabPageTest {
@Test @Test
@MediumTest @MediumTest
@Feature({"NewTabPage", "RenderTest"}) @Feature({"NewTabPage", "RenderTest"})
@ParameterAnnotations.UseMethodParameter("Modern") @ParameterAnnotations.UseMethodParameter(ModernParams.class)
public void testRender(boolean modern) throws IOException { public void testRender(boolean modern) throws IOException {
InstrumentationRegistry.getInstrumentation().waitForIdleSync(); InstrumentationRegistry.getInstrumentation().waitForIdleSync();
RenderTestRule.sanitize(mNtp.getView()); RenderTestRule.sanitize(mNtp.getView());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment