OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base.test; | 5 package org.chromium.base.test; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.os.SystemClock; | 9 import android.os.SystemClock; |
10 import android.test.ActivityInstrumentationTestCase2; | 10 import android.test.ActivityInstrumentationTestCase2; |
11 import android.util.Log; | 11 import android.util.Log; |
12 | 12 |
13 import org.chromium.base.BaseChromiumApplication; | |
14 import org.chromium.base.CommandLine; | |
15 import org.chromium.base.test.util.CommandLineFlags; | 13 import org.chromium.base.test.util.CommandLineFlags; |
16 | 14 |
17 import java.lang.reflect.AnnotatedElement; | |
18 import java.lang.reflect.Method; | |
19 import java.util.Arrays; | |
20 import java.util.HashSet; | |
21 import java.util.List; | |
22 import java.util.Set; | |
23 | |
24 /** | 15 /** |
25 * Base class for all Activity-based Instrumentation tests. | 16 * Base class for all Activity-based Instrumentation tests. |
26 * | 17 * |
27 * @param <T> The Activity type. | 18 * @param <T> The Activity type. |
28 */ | 19 */ |
29 public class BaseActivityInstrumentationTestCase<T extends Activity> | 20 public class BaseActivityInstrumentationTestCase<T extends Activity> |
30 extends ActivityInstrumentationTestCase2<T> { | 21 extends ActivityInstrumentationTestCase2<T> { |
31 | 22 |
32 private static final String TAG = "BaseActivityInstrumentationTestCase"; | 23 private static final String TAG = "BaseActivityInstrumentationTestCase"; |
33 | 24 |
34 private static final int SLEEP_INTERVAL = 50; // milliseconds | 25 private static final int SLEEP_INTERVAL = 50; // milliseconds |
35 private static final int WAIT_DURATION = 5000; // milliseconds | 26 private static final int WAIT_DURATION = 5000; // milliseconds |
36 | 27 |
37 /** | 28 /** |
38 * Creates a instance for running tests against an Activity of the given cla
ss. | 29 * Creates a instance for running tests against an Activity of the given cla
ss. |
39 * | 30 * |
40 * @param activityClass The type of activity that will be tested. | 31 * @param activityClass The type of activity that will be tested. |
41 */ | 32 */ |
42 public BaseActivityInstrumentationTestCase(Class<T> activityClass) { | 33 public BaseActivityInstrumentationTestCase(Class<T> activityClass) { |
43 super(activityClass); | 34 super(activityClass); |
44 } | 35 } |
45 | 36 |
46 /** | |
47 * Sets up the CommandLine with the appropriate flags. | |
48 * | |
49 * This will add the difference of the sets of flags specified by {@link Com
mandLineFlags.Add} | |
50 * and {@link CommandLineFlags.Remove} to the {@link org.chromium.base.Comma
ndLine}. Note that | |
51 * trying to remove a flag set externally, i.e. by the command-line flags fi
le, will not work. | |
52 */ | |
53 @Override | 37 @Override |
54 protected void setUp() throws Exception { | 38 protected void setUp() throws Exception { |
55 super.setUp(); | 39 super.setUp(); |
56 | 40 CommandLineFlags.setUp(getTargetContext(), getClass().getMethod(getName(
))); |
57 CommandLine.reset(); | |
58 Context targetContext = getTargetContext(); | |
59 assertNotNull("Unable to get a non-null target context.", targetContext)
; | |
60 | |
61 BaseChromiumApplication.initCommandLine(targetContext); | |
62 Set<String> flags = getFlags(getClass().getMethod(getName())); | |
63 for (String flag : flags) { | |
64 CommandLine.getInstance().appendSwitch(flag); | |
65 } | |
66 } | 41 } |
67 | 42 |
68 /** | 43 /** |
69 * Gets the target context. | 44 * Gets the target context. |
70 * | 45 * |
71 * On older versions of Android, getTargetContext() may initially return nul
l, so we have to | 46 * On older versions of Android, getTargetContext() may initially return nul
l, so we have to |
72 * wait for it to become available. | 47 * wait for it to become available. |
73 * | 48 * |
74 * @return The target {@link android.content.Context} if available; null oth
erwise. | 49 * @return The target {@link android.content.Context} if available; null oth
erwise. |
75 */ | 50 */ |
76 private Context getTargetContext() { | 51 private Context getTargetContext() { |
77 Context targetContext = getInstrumentation().getTargetContext(); | 52 Context targetContext = getInstrumentation().getTargetContext(); |
78 try { | 53 try { |
79 long startTime = SystemClock.uptimeMillis(); | 54 long startTime = SystemClock.uptimeMillis(); |
80 // TODO(jbudorick): Convert this to CriteriaHelper once that moves t
o base/. | 55 // TODO(jbudorick): Convert this to CriteriaHelper once that moves t
o base/. |
81 while (targetContext == null | 56 while (targetContext == null |
82 && SystemClock.uptimeMillis() - startTime < WAIT_DURATION) { | 57 && SystemClock.uptimeMillis() - startTime < WAIT_DURATION) { |
83 Thread.sleep(SLEEP_INTERVAL); | 58 Thread.sleep(SLEEP_INTERVAL); |
84 targetContext = getInstrumentation().getTargetContext(); | 59 targetContext = getInstrumentation().getTargetContext(); |
85 } | 60 } |
86 } catch (InterruptedException e) { | 61 } catch (InterruptedException e) { |
87 Log.e(TAG, "Interrupted while attempting to initialize the command l
ine."); | 62 Log.e(TAG, "Interrupted while attempting to initialize the command l
ine."); |
88 } | 63 } |
89 return targetContext; | 64 return targetContext; |
90 } | 65 } |
91 | |
92 private static Set<String> getFlags(AnnotatedElement element) { | |
93 AnnotatedElement parent = (element instanceof Method) | |
94 ? ((Method) element).getDeclaringClass() | |
95 : ((Class) element).getSuperclass(); | |
96 Set<String> flags = (parent == null) ? new HashSet<String>() : getFlags(
parent); | |
97 | |
98 if (element.isAnnotationPresent(CommandLineFlags.Add.class)) { | |
99 flags.addAll( | |
100 Arrays.asList(element.getAnnotation(CommandLineFlags.Add.cla
ss).value())); | |
101 } | |
102 | |
103 if (element.isAnnotationPresent(CommandLineFlags.Remove.class)) { | |
104 List<String> flagsToRemove = | |
105 Arrays.asList(element.getAnnotation(CommandLineFlags.Remove.
class).value()); | |
106 for (String flagToRemove : flagsToRemove) { | |
107 // If your test fails here, you have tried to remove a command-l
ine flag via | |
108 // CommandLineFlags.Remove that was loaded into CommandLine via
something other | |
109 // than CommandLineFlags.Add (probably the command-line flag fil
e). | |
110 assertFalse("Unable to remove command-line flag \"" + flagToRemo
ve + "\".", | |
111 CommandLine.getInstance().hasSwitch(flagToRemove)); | |
112 } | |
113 flags.removeAll(flagsToRemove); | |
114 } | |
115 | |
116 return flags; | |
117 } | |
118 } | 66 } |
OLD | NEW |