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.app.ActivityOptions; | 7 import android.app.ActivityOptions; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.ContextWrapper; | 9 import android.content.ContextWrapper; |
10 import android.content.Intent; | 10 import android.content.Intent; |
11 import android.net.ConnectivityManager; | |
12 import android.net.NetworkInfo; | |
13 import android.os.Build; | |
14 import android.os.Bundle; | 11 import android.os.Bundle; |
15 import android.test.AndroidTestRunner; | 12 import android.test.AndroidTestRunner; |
16 import android.test.InstrumentationTestRunner; | 13 import android.test.InstrumentationTestRunner; |
17 import android.text.TextUtils; | |
18 | 14 |
19 import junit.framework.TestCase; | |
20 import junit.framework.TestResult; | 15 import junit.framework.TestResult; |
21 | 16 |
22 import org.chromium.base.Log; | |
23 import org.chromium.base.SysUtils; | |
24 import org.chromium.base.multidex.ChromiumMultiDexInstaller; | 17 import org.chromium.base.multidex.ChromiumMultiDexInstaller; |
25 import org.chromium.base.test.util.CommandLineFlags; | 18 import org.chromium.base.test.util.CommandLineFlags; |
26 import org.chromium.base.test.util.DisableIfSkipCheck; | 19 import org.chromium.base.test.util.DisableIfSkipCheck; |
27 import org.chromium.base.test.util.MinAndroidSdkLevel; | 20 import org.chromium.base.test.util.MinAndroidSdkLevelSkipCheck; |
28 import org.chromium.base.test.util.Restriction; | 21 import org.chromium.base.test.util.RestrictionSkipCheck; |
29 import org.chromium.base.test.util.SkipCheck; | |
30 import org.chromium.test.reporter.TestStatusListener; | 22 import org.chromium.test.reporter.TestStatusListener; |
31 | 23 |
32 import java.lang.reflect.Method; | |
33 | |
34 // TODO(jbudorick): Add support for on-device handling of timeouts. | 24 // TODO(jbudorick): Add support for on-device handling of timeouts. |
35 /** | 25 /** |
36 * An Instrumentation test runner that checks SDK level for tests with specific
requirements. | 26 * An Instrumentation test runner that checks SDK level for tests with specific
requirements. |
37 */ | 27 */ |
38 public class BaseInstrumentationTestRunner extends InstrumentationTestRunner { | 28 public class BaseInstrumentationTestRunner extends InstrumentationTestRunner { |
39 private static final String TAG = "base_test"; | 29 private static final String TAG = "base_test"; |
40 | 30 |
41 @Override | 31 @Override |
42 public void onCreate(Bundle arguments) { | 32 public void onCreate(Bundle arguments) { |
43 ChromiumMultiDexInstaller.install(getTargetContext()); | 33 ChromiumMultiDexInstaller.install(getTargetContext()); |
(...skipping 16 matching lines...) Expand all Loading... |
60 | 50 |
61 /** | 51 /** |
62 * Override this method to register hooks and checks to be run for each test
. Make sure to call | 52 * Override this method to register hooks and checks to be run for each test
. Make sure to call |
63 * the base implementation if you do so. | 53 * the base implementation if you do so. |
64 * | 54 * |
65 * @see BaseTestResult#addSkipCheck(BaseTestResult.SkipCheck) | 55 * @see BaseTestResult#addSkipCheck(BaseTestResult.SkipCheck) |
66 * @see BaseTestResult#addPreTestHook(BaseTestResult.PreTestHook) | 56 * @see BaseTestResult#addPreTestHook(BaseTestResult.PreTestHook) |
67 */ | 57 */ |
68 protected void addTestHooks(BaseTestResult result) { | 58 protected void addTestHooks(BaseTestResult result) { |
69 result.addSkipCheck(new MinAndroidSdkLevelSkipCheck()); | 59 result.addSkipCheck(new MinAndroidSdkLevelSkipCheck()); |
70 result.addSkipCheck(new RestrictionSkipCheck()); | 60 result.addSkipCheck(new RestrictionSkipCheck(getTargetContext())); |
71 result.addSkipCheck(new DisableIfSkipCheck()); | 61 result.addSkipCheck(new DisableIfSkipCheck()); |
72 | 62 |
73 result.addPreTestHook(CommandLineFlags.getRegistrationHook()); | 63 result.addPreTestHook(CommandLineFlags.getRegistrationHook()); |
74 } | 64 } |
75 | 65 |
76 | |
77 /** | |
78 * Checks if any restrictions exist and skip the test if it meets those rest
rictions. | |
79 */ | |
80 public class RestrictionSkipCheck extends SkipCheck { | |
81 @Override | |
82 public boolean shouldSkip(TestCase testCase) { | |
83 Method method = getTestMethod(testCase); | |
84 if (method == null) return true; | |
85 | |
86 Restriction restrictions = method.getAnnotation(Restriction.class); | |
87 if (restrictions != null) { | |
88 for (String restriction : restrictions.value()) { | |
89 if (restrictionApplies(restriction)) { | |
90 Log.i(TAG, "Test " + testCase.getClass().getName() + "#" | |
91 + testCase.getName() + " skipped because of rest
riction " | |
92 + restriction); | |
93 return true; | |
94 } | |
95 } | |
96 } | |
97 return false; | |
98 } | |
99 | |
100 protected boolean restrictionApplies(String restriction) { | |
101 if (TextUtils.equals(restriction, Restriction.RESTRICTION_TYPE_LOW_E
ND_DEVICE) | |
102 && !SysUtils.isLowEndDevice()) { | |
103 return true; | |
104 } | |
105 if (TextUtils.equals(restriction, Restriction.RESTRICTION_TYPE_NON_L
OW_END_DEVICE) | |
106 && SysUtils.isLowEndDevice()) { | |
107 return true; | |
108 } | |
109 if (TextUtils.equals(restriction, Restriction.RESTRICTION_TYPE_INTER
NET) | |
110 && !isNetworkAvailable()) { | |
111 return true; | |
112 } | |
113 return false; | |
114 } | |
115 | |
116 private boolean isNetworkAvailable() { | |
117 final ConnectivityManager connectivityManager = (ConnectivityManager
) | |
118 getTargetContext().getSystemService(Context.CONNECTIVITY_SER
VICE); | |
119 final NetworkInfo activeNetworkInfo = connectivityManager.getActiveN
etworkInfo(); | |
120 return activeNetworkInfo != null && activeNetworkInfo.isConnected(); | |
121 } | |
122 } | |
123 | |
124 /** | |
125 * Checks the device's SDK level against any specified minimum requirement. | |
126 */ | |
127 public static class MinAndroidSdkLevelSkipCheck extends SkipCheck { | |
128 | |
129 /** | |
130 * If {@link MinAndroidSdkLevel} is present, checks its value | |
131 * against the device's SDK level. | |
132 * | |
133 * @param testCase The test to check. | |
134 * @return true if the device's SDK level is below the specified minimum
. | |
135 */ | |
136 @Override | |
137 public boolean shouldSkip(TestCase testCase) { | |
138 Class<?> testClass = testCase.getClass(); | |
139 Method testMethod = getTestMethod(testCase); | |
140 | |
141 int minSdkLevel = 0; | |
142 if (testClass.isAnnotationPresent(MinAndroidSdkLevel.class)) { | |
143 minSdkLevel = Math.max(testClass.getAnnotation(MinAndroidSdkLeve
l.class).value(), | |
144 minSdkLevel); | |
145 } | |
146 if (testMethod != null && testMethod.isAnnotationPresent(MinAndroidS
dkLevel.class)) { | |
147 minSdkLevel = Math.max(testMethod.getAnnotation(MinAndroidSdkLev
el.class).value(), | |
148 minSdkLevel); | |
149 } | |
150 | |
151 if (Build.VERSION.SDK_INT < minSdkLevel) { | |
152 Log.i(TAG, "Test " + testClass.getName() + "#" + testCase.getNam
e() | |
153 + " is not enabled at SDK level " + Build.VERSION.SDK_IN
T | |
154 + "."); | |
155 return true; | |
156 } | |
157 return false; | |
158 } | |
159 } | |
160 | |
161 @Override | 66 @Override |
162 public Context getTargetContext() { | 67 public Context getTargetContext() { |
163 return new ContextWrapper(super.getTargetContext()) { | 68 return new ContextWrapper(super.getTargetContext()) { |
164 @Override | 69 @Override |
165 public void startActivity(Intent intent) { | 70 public void startActivity(Intent intent) { |
166 Context context = getApplicationContext(); | 71 Context context = getApplicationContext(); |
167 ActivityOptions activityOptions = | 72 ActivityOptions activityOptions = |
168 ActivityOptions.makeCustomAnimation(context, 0, 0); | 73 ActivityOptions.makeCustomAnimation(context, 0, 0); |
169 startActivity(intent, activityOptions.toBundle()); | 74 startActivity(intent, activityOptions.toBundle()); |
170 } | 75 } |
171 }; | 76 }; |
172 } | 77 } |
173 } | 78 } |
OLD | NEW |