Chromium Code Reviews| Index: base/test/android/javatests/src/org/chromium/base/test/BaseActivityInstrumentationTestCase.java |
| diff --git a/base/test/android/javatests/src/org/chromium/base/test/BaseActivityInstrumentationTestCase.java b/base/test/android/javatests/src/org/chromium/base/test/BaseActivityInstrumentationTestCase.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..56d718dcd436383c07b5fc4e9ee56010c90038a8 |
| --- /dev/null |
| +++ b/base/test/android/javatests/src/org/chromium/base/test/BaseActivityInstrumentationTestCase.java |
| @@ -0,0 +1,94 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.base.test; |
| + |
| +import android.app.Activity; |
| +import android.test.ActivityInstrumentationTestCase2; |
| + |
| +import org.chromium.base.BaseChromiumApplication; |
| +import org.chromium.base.CommandLine; |
| +import org.chromium.base.test.util.CommandLineFlags; |
| + |
| +import java.lang.reflect.Method; |
| +import java.util.Arrays; |
| +import java.util.HashSet; |
| +import java.util.Iterator; |
| +import java.util.Set; |
| + |
| +/** |
| + * Base class for all Activity-based Instrumentation tests. |
| + * |
| + * @param <T> The Activity type. |
| + */ |
| +public class BaseActivityInstrumentationTestCase<T extends Activity> |
| + extends ActivityInstrumentationTestCase2<T> { |
| + private static final String TAG = "BaseActivityInstrumentationTestCase"; |
|
nyquist
2015/01/28 23:10:36
Nit: I don't think this field is used.
jbudorick
2015/01/29 04:46:45
Removed.
|
| + |
| + /** |
| + * Creates a instance for running tests against an Activity of the given class. |
| + * |
| + * @param activityClass The type of activity that will be tested. |
| + */ |
| + public BaseActivityInstrumentationTestCase(Class<T> activityClass) { |
| + super(activityClass); |
| + } |
| + |
| + /** |
| + * Sets up the CommandLine with the appropriate flags. |
| + * |
| + * This will add the difference of the sets of flags specified by @CommandLineFlags.Add and |
|
nyquist
2015/01/28 23:10:36
Nit: How about using {@link CommandLineFlags.Add}
jbudorick
2015/01/29 04:46:45
Done.
|
| + * @CommandLineFlags.Remove to the CommandLine. Note that trying to remove a flag set |
|
Ted C
2015/01/29 00:39:51
This caveat scares me. If a test is explicitly tr
jbudorick
2015/01/29 04:46:45
Understandably so. Neither base::CommandLine nor o
jbudorick
2015/01/29 04:46:45
added {@link org.chromium.base.CommandLine} here
|
| + * externally, i.e. by the command-line flags file, will not work. |
| + */ |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + |
| + CommandLine.reset(); |
| + BaseChromiumApplication.initCommandLine(getInstrumentation().getTargetContext()); |
|
Ted C
2015/01/29 00:39:51
You might want to do what is in CommandLineTestUti
jbudorick
2015/01/29 04:46:45
Criteria & CriteriaHelper are in content/. I've re
|
| + |
| + Class<?> testClass = getClass(); |
| + Method testMethod = testClass.getMethod(getName()); |
| + |
| + Set<String> flags = getClassFlags(testClass); |
| + |
| + if (testMethod.isAnnotationPresent(CommandLineFlags.Add.class)) { |
| + flags.addAll( |
| + Arrays.asList(testMethod.getAnnotation(CommandLineFlags.Add.class).value())); |
| + } |
| + |
| + if (testMethod.isAnnotationPresent(CommandLineFlags.Remove.class)) { |
| + flags.removeAll( |
| + Arrays.asList(testMethod.getAnnotation(CommandLineFlags.Remove.class).value())); |
| + } |
| + |
| + Iterator<String> flagIter = flags.iterator(); |
| + while (flagIter.hasNext()) { |
|
nyquist
2015/01/28 23:10:36
Nit: I often find it easier to read:
for (String f
jbudorick
2015/01/29 04:46:45
Done.
|
| + CommandLine.getInstance().appendSwitch(flagIter.next()); |
| + } |
| + } |
| + |
| + private Set<String> getClassFlags(Class<?> testClass) { |
|
nyquist
2015/01/28 23:10:36
Nit: This method should be static to signal that i
jbudorick
2015/01/29 04:46:45
Done.
|
| + Class<?> superclass = testClass.getSuperclass(); |
| + Set<String> flags = null; |
|
nyquist
2015/01/28 23:10:36
Nit: You are always initializing this field below,
jbudorick
2015/01/29 04:46:45
Done.
|
| + if (superclass != null) { |
|
nyquist
2015/01/28 23:10:36
Optional nit: My personal preference is to not fli
jbudorick
2015/01/29 04:46:45
ternary conditional it is
|
| + flags = getClassFlags(superclass); |
| + } else { |
| + flags = new HashSet<String>(); |
| + } |
| + |
| + if (testClass.isAnnotationPresent(CommandLineFlags.Add.class)) { |
|
Ted C
2015/01/29 00:39:51
Can this and the if below be abstracted out to a h
jbudorick
2015/01/29 04:46:45
Done.
|
| + flags.addAll( |
| + Arrays.asList(testClass.getAnnotation(CommandLineFlags.Add.class).value())); |
| + } |
| + |
| + if (testClass.isAnnotationPresent(CommandLineFlags.Remove.class)) { |
| + flags.removeAll( |
| + Arrays.asList(testClass.getAnnotation(CommandLineFlags.Remove.class).value())); |
| + } |
| + |
| + return flags; |
| + } |
| +} |