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; | |
23 import org.chromium.base.test.util.CommandLineFlags; | 22 import org.chromium.base.test.util.CommandLineFlags; |
| 23 import org.chromium.base.test.util.DisableIfSkipCheck; |
24 import org.chromium.base.test.util.MinAndroidSdkLevel; | 24 import org.chromium.base.test.util.MinAndroidSdkLevel; |
25 import org.chromium.base.test.util.Restriction; | 25 import org.chromium.base.test.util.Restriction; |
| 26 import org.chromium.base.test.util.SkipCheck; |
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; |
29 | 30 |
30 // TODO(jbudorick): Add support for on-device handling of timeouts. | 31 // TODO(jbudorick): Add support for on-device handling of timeouts. |
31 /** | 32 /** |
32 * An Instrumentation test runner that checks SDK level for tests with specific
requirements. | 33 * An Instrumentation test runner that checks SDK level for tests with specific
requirements. |
33 */ | 34 */ |
34 public class BaseInstrumentationTestRunner extends InstrumentationTestRunner { | 35 public class BaseInstrumentationTestRunner extends InstrumentationTestRunner { |
35 private static final String TAG = "base_test"; | 36 private static final String TAG = "base_test"; |
(...skipping 21 matching lines...) Expand all Loading... |
57 /** | 58 /** |
58 * Override this method to register hooks and checks to be run for each test
. Make sure to call | 59 * 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. | 60 * the base implementation if you do so. |
60 * | 61 * |
61 * @see BaseTestResult#addSkipCheck(BaseTestResult.SkipCheck) | 62 * @see BaseTestResult#addSkipCheck(BaseTestResult.SkipCheck) |
62 * @see BaseTestResult#addPreTestHook(BaseTestResult.PreTestHook) | 63 * @see BaseTestResult#addPreTestHook(BaseTestResult.PreTestHook) |
63 */ | 64 */ |
64 protected void addTestHooks(BaseTestResult result) { | 65 protected void addTestHooks(BaseTestResult result) { |
65 result.addSkipCheck(new MinAndroidSdkLevelSkipCheck()); | 66 result.addSkipCheck(new MinAndroidSdkLevelSkipCheck()); |
66 result.addSkipCheck(new RestrictionSkipCheck()); | 67 result.addSkipCheck(new RestrictionSkipCheck()); |
| 68 result.addSkipCheck(new DisableIfSkipCheck()); |
67 | 69 |
68 result.addPreTestHook(CommandLineFlags.getRegistrationHook()); | 70 result.addPreTestHook(CommandLineFlags.getRegistrationHook()); |
69 } | 71 } |
70 | 72 |
| 73 |
71 /** | 74 /** |
72 * Checks if any restrictions exist and skip the test if it meets those rest
rictions. | 75 * Checks if any restrictions exist and skip the test if it meets those rest
rictions. |
73 */ | 76 */ |
74 public class RestrictionSkipCheck implements SkipCheck { | 77 public class RestrictionSkipCheck extends SkipCheck { |
75 @Override | 78 @Override |
76 public boolean shouldSkip(TestCase testCase) { | 79 public boolean shouldSkip(TestCase testCase) { |
77 Method method; | 80 Method method = getTestMethod(testCase); |
78 try { | 81 if (method == null) return true; |
79 method = testCase.getClass().getMethod(testCase.getName(), (Clas
s[]) null); | 82 |
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); | 83 Restriction restrictions = method.getAnnotation(Restriction.class); |
86 if (restrictions != null) { | 84 if (restrictions != null) { |
87 for (String restriction : restrictions.value()) { | 85 for (String restriction : restrictions.value()) { |
88 if (restrictionApplies(restriction)) { | 86 if (restrictionApplies(restriction)) { |
89 return true; | 87 return true; |
90 } | 88 } |
91 } | 89 } |
92 } | 90 } |
93 return false; | 91 return false; |
94 } | 92 } |
(...skipping 18 matching lines...) Expand all Loading... |
113 final ConnectivityManager connectivityManager = (ConnectivityManager
) | 111 final ConnectivityManager connectivityManager = (ConnectivityManager
) |
114 getTargetContext().getSystemService(Context.CONNECTIVITY_SER
VICE); | 112 getTargetContext().getSystemService(Context.CONNECTIVITY_SER
VICE); |
115 final NetworkInfo activeNetworkInfo = connectivityManager.getActiveN
etworkInfo(); | 113 final NetworkInfo activeNetworkInfo = connectivityManager.getActiveN
etworkInfo(); |
116 return activeNetworkInfo != null && activeNetworkInfo.isConnected(); | 114 return activeNetworkInfo != null && activeNetworkInfo.isConnected(); |
117 } | 115 } |
118 } | 116 } |
119 | 117 |
120 /** | 118 /** |
121 * Checks the device's SDK level against any specified minimum requirement. | 119 * Checks the device's SDK level against any specified minimum requirement. |
122 */ | 120 */ |
123 public static class MinAndroidSdkLevelSkipCheck implements SkipCheck { | 121 public static class MinAndroidSdkLevelSkipCheck extends SkipCheck { |
124 | 122 |
125 /** | 123 /** |
126 * If {@link MinAndroidSdkLevel} is present, checks its value | 124 * If {@link MinAndroidSdkLevel} is present, checks its value |
127 * against the device's SDK level. | 125 * against the device's SDK level. |
128 * | 126 * |
129 * @param testCase The test to check. | 127 * @param testCase The test to check. |
130 * @return true if the device's SDK level is below the specified minimum
. | 128 * @return true if the device's SDK level is below the specified minimum
. |
131 */ | 129 */ |
132 @Override | 130 @Override |
133 public boolean shouldSkip(TestCase testCase) { | 131 public boolean shouldSkip(TestCase testCase) { |
134 Class<?> testClass = testCase.getClass(); | 132 Class<?> testClass = testCase.getClass(); |
135 if (testClass.isAnnotationPresent(MinAndroidSdkLevel.class)) { | 133 if (testClass.isAnnotationPresent(MinAndroidSdkLevel.class)) { |
136 MinAndroidSdkLevel v = testClass.getAnnotation(MinAndroidSdkLeve
l.class); | 134 MinAndroidSdkLevel v = testClass.getAnnotation(MinAndroidSdkLeve
l.class); |
137 if (Build.VERSION.SDK_INT < v.value()) { | 135 if (Build.VERSION.SDK_INT < v.value()) { |
138 Log.i(TAG, "Test " + testClass.getName() + "#" + testCase.ge
tName() | 136 Log.i(TAG, "Test " + testClass.getName() + "#" + testCase.ge
tName() |
139 + " is not enabled at SDK level " + Build.VERSION.SD
K_INT | 137 + " is not enabled at SDK level " + Build.VERSION.SD
K_INT |
140 + "."); | 138 + "."); |
141 return true; | 139 return true; |
142 } | 140 } |
143 } | 141 } |
144 return false; | 142 return false; |
145 } | 143 } |
146 } | 144 } |
147 } | 145 } |
OLD | NEW |