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