OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.content.Context; | 7 import android.content.Context; |
8 import android.net.ConnectivityManager; | 8 import android.net.ConnectivityManager; |
9 import android.net.NetworkInfo; | 9 import android.net.NetworkInfo; |
10 import android.os.Build; | 10 import android.os.Build; |
11 import android.os.Bundle; | 11 import android.os.Bundle; |
12 import android.test.AndroidTestRunner; | 12 import android.test.AndroidTestRunner; |
13 import android.test.InstrumentationTestRunner; | 13 import android.test.InstrumentationTestRunner; |
14 import android.text.TextUtils; | 14 import android.text.TextUtils; |
15 | 15 |
16 import junit.framework.TestCase; | 16 import junit.framework.TestCase; |
17 import junit.framework.TestResult; | 17 import junit.framework.TestResult; |
18 | 18 |
19 import org.chromium.base.Log; | 19 import org.chromium.base.Log; |
20 import org.chromium.base.SysUtils; | 20 import org.chromium.base.SysUtils; |
21 import org.chromium.base.multidex.ChromiumMultiDex; | 21 import org.chromium.base.multidex.ChromiumMultiDex; |
22 import org.chromium.base.test.BaseTestResult.SkipCheck; | 22 import org.chromium.base.test.BaseTestResult.SkipCheck; |
23 import org.chromium.base.test.util.CommandLineFlags; | 23 import org.chromium.base.test.util.CommandLineFlags; |
24 import org.chromium.base.test.util.DisableIf; | |
24 import org.chromium.base.test.util.MinAndroidSdkLevel; | 25 import org.chromium.base.test.util.MinAndroidSdkLevel; |
25 import org.chromium.base.test.util.Restriction; | 26 import org.chromium.base.test.util.Restriction; |
26 import org.chromium.test.reporter.TestStatusListener; | 27 import org.chromium.test.reporter.TestStatusListener; |
27 | 28 |
28 import java.lang.reflect.Method; | 29 import java.lang.reflect.Method; |
30 import java.util.Arrays; | |
29 | 31 |
30 // TODO(jbudorick): Add support for on-device handling of timeouts. | 32 // TODO(jbudorick): Add support for on-device handling of timeouts. |
31 /** | 33 /** |
32 * An Instrumentation test runner that checks SDK level for tests with specific requirements. | 34 * An Instrumentation test runner that checks SDK level for tests with specific requirements. |
33 */ | 35 */ |
34 public class BaseInstrumentationTestRunner extends InstrumentationTestRunner { | 36 public class BaseInstrumentationTestRunner extends InstrumentationTestRunner { |
35 private static final String TAG = "base_test"; | 37 private static final String TAG = "base_test"; |
36 | 38 |
37 @Override | 39 @Override |
38 public void onCreate(Bundle arguments) { | 40 public void onCreate(Bundle arguments) { |
(...skipping 18 matching lines...) Expand all Loading... | |
57 /** | 59 /** |
58 * Override this method to register hooks and checks to be run for each test . Make sure to call | 60 * Override this method to register hooks and checks to be run for each test . Make sure to call |
59 * the base implementation if you do so. | 61 * the base implementation if you do so. |
60 * | 62 * |
61 * @see BaseTestResult#addSkipCheck(BaseTestResult.SkipCheck) | 63 * @see BaseTestResult#addSkipCheck(BaseTestResult.SkipCheck) |
62 * @see BaseTestResult#addPreTestHook(BaseTestResult.PreTestHook) | 64 * @see BaseTestResult#addPreTestHook(BaseTestResult.PreTestHook) |
63 */ | 65 */ |
64 protected void addTestHooks(BaseTestResult result) { | 66 protected void addTestHooks(BaseTestResult result) { |
65 result.addSkipCheck(new MinAndroidSdkLevelSkipCheck()); | 67 result.addSkipCheck(new MinAndroidSdkLevelSkipCheck()); |
66 result.addSkipCheck(new RestrictionSkipCheck()); | 68 result.addSkipCheck(new RestrictionSkipCheck()); |
69 result.addSkipCheck(new DisableSkipCheck()); | |
67 | 70 |
68 result.addPreTestHook(CommandLineFlags.getRegistrationHook()); | 71 result.addPreTestHook(CommandLineFlags.getRegistrationHook()); |
69 } | 72 } |
70 | 73 |
74 private static Method getTestMethod(TestCase testCase) { | |
75 try { | |
76 return testCase.getClass().getMethod(testCase.getName(), (Class[]) n ull); | |
77 } catch (NoSuchMethodException e) { | |
78 Log.e(TAG, "Unable to find %s in %s", testCase.getName(), | |
79 testCase.getClass().getName(), e); | |
80 return null; | |
81 } | |
82 } | |
83 | |
71 /** | 84 /** |
72 * Checks if any restrictions exist and skip the test if it meets those rest rictions. | 85 * Checks if any restrictions exist and skip the test if it meets those rest rictions. |
73 */ | 86 */ |
74 public class RestrictionSkipCheck implements SkipCheck { | 87 public class RestrictionSkipCheck implements SkipCheck { |
75 @Override | 88 @Override |
76 public boolean shouldSkip(TestCase testCase) { | 89 public boolean shouldSkip(TestCase testCase) { |
77 Method method; | 90 Method method = getTestMethod(testCase); |
78 try { | 91 if (method == null) return true; |
79 method = testCase.getClass().getMethod(testCase.getName(), (Clas s[]) null); | 92 |
80 } catch (NoSuchMethodException e) { | |
81 Log.e(TAG, "Unable to find %s in %s", testCase.getName(), | |
82 testCase.getClass().getName(), e); | |
83 return true; | |
84 } | |
85 Restriction restrictions = method.getAnnotation(Restriction.class); | 93 Restriction restrictions = method.getAnnotation(Restriction.class); |
86 if (restrictions != null) { | 94 if (restrictions != null) { |
87 for (String restriction : restrictions.value()) { | 95 for (String restriction : restrictions.value()) { |
88 if (restrictionApplies(restriction)) { | 96 if (restrictionApplies(restriction)) { |
89 return true; | 97 return true; |
90 } | 98 } |
91 } | 99 } |
92 } | 100 } |
93 return false; | 101 return false; |
94 } | 102 } |
(...skipping 16 matching lines...) Expand all Loading... | |
111 | 119 |
112 private boolean isNetworkAvailable() { | 120 private boolean isNetworkAvailable() { |
113 final ConnectivityManager connectivityManager = (ConnectivityManager ) | 121 final ConnectivityManager connectivityManager = (ConnectivityManager ) |
114 getTargetContext().getSystemService(Context.CONNECTIVITY_SER VICE); | 122 getTargetContext().getSystemService(Context.CONNECTIVITY_SER VICE); |
115 final NetworkInfo activeNetworkInfo = connectivityManager.getActiveN etworkInfo(); | 123 final NetworkInfo activeNetworkInfo = connectivityManager.getActiveN etworkInfo(); |
116 return activeNetworkInfo != null && activeNetworkInfo.isConnected(); | 124 return activeNetworkInfo != null && activeNetworkInfo.isConnected(); |
117 } | 125 } |
118 } | 126 } |
119 | 127 |
120 /** | 128 /** |
129 * Checks for conditional disables. | |
130 * | |
131 * Currently, this only includes checks against a few {@link android.os.Buil d} values. | |
132 */ | |
133 public static class DisableIfSkipCheck implements SkipCheck { | |
134 | |
135 @Override | |
136 public boolean shouldSkip(TestCase testCase) { | |
137 Method method = getTestMethod(testCase); | |
138 if (method == null) return true; | |
139 | |
140 if (method.isAnnotationPresent(DisableIf.Build.class)) { | |
141 DisableIf.Build v = method.getAnnotation( | |
142 DisableIf.Build.class); | |
143 return sdk(v) && abi(v) && hardware(v); | |
144 } | |
145 | |
146 return false; | |
147 } | |
148 | |
149 private boolean sdk(DisableIf.Build v) { | |
150 return Build.VERSION.SDK_INT < v.sdk_is_less_than() | |
Yaron
2015/12/11 02:22:16
*sigh*
I guess I'll eventually lose this battle o
jbudorick
2015/12/17 02:08:13
I'm fine with leaving this out until someone needs
| |
151 && Build.VERSION.SDK_INT > v.sdk_is_greater_than(); | |
152 } | |
153 | |
154 private boolean abi(DisableIf.Build v) { | |
155 if (v.supported_abis_includes().isEmpty()) return true; | |
156 | |
157 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
158 for (String s : Build.SUPPORTED_ABIS) { | |
159 Log.i(TAG, " %s", s); | |
Yaron
2015/12/11 02:22:16
?
jbudorick
2015/12/17 02:08:13
leftover, removed.
| |
160 } | |
161 return Arrays.asList(Build.SUPPORTED_ABIS).contains( | |
162 v.supported_abis_includes()); | |
Yaron
2015/12/11 02:22:16
would you need to strip this on spaces or can you
jbudorick
2015/12/17 02:08:13
at the moment, only one.
| |
163 } else { | |
164 return Build.CPU_ABI.equals(v.supported_abis_includes()) | |
165 || Build.CPU_ABI2.equals(v.supported_abis_includes()); | |
166 } | |
167 } | |
168 | |
169 private boolean hardware(DisableIf.Build v) { | |
170 return v.hardware_is().isEmpty() || Build.HARDWARE.equals(v.hardware _is()); | |
171 } | |
172 } | |
173 | |
174 /** | |
121 * Checks the device's SDK level against any specified minimum requirement. | 175 * Checks the device's SDK level against any specified minimum requirement. |
122 */ | 176 */ |
123 public static class MinAndroidSdkLevelSkipCheck implements SkipCheck { | 177 public static class MinAndroidSdkLevelSkipCheck implements SkipCheck { |
124 | 178 |
125 /** | 179 /** |
126 * If {@link MinAndroidSdkLevel} is present, checks its value | 180 * If {@link MinAndroidSdkLevel} is present, checks its value |
127 * against the device's SDK level. | 181 * against the device's SDK level. |
128 * | 182 * |
129 * @param testCase The test to check. | 183 * @param testCase The test to check. |
130 * @return true if the device's SDK level is below the specified minimum . | 184 * @return true if the device's SDK level is below the specified minimum . |
131 */ | 185 */ |
132 @Override | 186 @Override |
133 public boolean shouldSkip(TestCase testCase) { | 187 public boolean shouldSkip(TestCase testCase) { |
134 Class<?> testClass = testCase.getClass(); | 188 Class<?> testClass = testCase.getClass(); |
135 if (testClass.isAnnotationPresent(MinAndroidSdkLevel.class)) { | 189 if (testClass.isAnnotationPresent(MinAndroidSdkLevel.class)) { |
136 MinAndroidSdkLevel v = testClass.getAnnotation(MinAndroidSdkLeve l.class); | 190 MinAndroidSdkLevel v = testClass.getAnnotation(MinAndroidSdkLeve l.class); |
137 if (Build.VERSION.SDK_INT < v.value()) { | 191 if (Build.VERSION.SDK_INT < v.value()) { |
138 Log.i(TAG, "Test " + testClass.getName() + "#" + testCase.ge tName() | 192 Log.i(TAG, "Test " + testClass.getName() + "#" + testCase.ge tName() |
139 + " is not enabled at SDK level " + Build.VERSION.SD K_INT | 193 + " is not enabled at SDK level " + Build.VERSION.SD K_INT |
140 + "."); | 194 + "."); |
141 return true; | 195 return true; |
142 } | 196 } |
143 } | 197 } |
144 return false; | 198 return false; |
145 } | 199 } |
146 } | 200 } |
147 } | 201 } |
OLD | NEW |