| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.test; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.app.Instrumentation; |
| 21 |
import android.content.Intent; |
| 22 |
import android.os.Bundle; |
| 23 |
import android.util.Log; |
| 24 |
import android.view.KeyEvent; |
| 25 |
|
| 26 |
import java.lang.reflect.Field; |
| 27 |
import java.lang.reflect.InvocationTargetException; |
| 28 |
import java.lang.reflect.Method; |
| 29 |
import java.lang.reflect.Modifier; |
| 30 |
|
| 31 |
import junit.framework.TestCase; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@Deprecated |
| 42 |
public class InstrumentationTestCase extends TestCase { |
| 43 |
|
| 44 |
private Instrumentation mInstrumentation; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public void injectInstrumentation(Instrumentation instrumentation) { |
| 53 |
mInstrumentation = instrumentation; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
@Deprecated |
| 66 |
public void injectInsrumentation(Instrumentation instrumentation) { |
| 67 |
injectInstrumentation(instrumentation); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Instrumentation getInstrumentation() { |
| 75 |
return mInstrumentation; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public final <T extends Activity> T launchActivity( |
| 96 |
String pkg, |
| 97 |
Class<T> activityCls, |
| 98 |
Bundle extras) { |
| 99 |
Intent intent = new Intent(Intent.ACTION_MAIN); |
| 100 |
if (extras != null) { |
| 101 |
intent.putExtras(extras); |
| 102 |
} |
| 103 |
return launchActivityWithIntent(pkg, activityCls, intent); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
@SuppressWarnings("unchecked") |
| 119 |
public final <T extends Activity> T launchActivityWithIntent( |
| 120 |
String pkg, |
| 121 |
Class<T> activityCls, |
| 122 |
Intent intent) { |
| 123 |
intent.setClassName(pkg, activityCls.getName()); |
| 124 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 125 |
T activity = (T) getInstrumentation().startActivitySync(intent); |
| 126 |
getInstrumentation().waitForIdleSync(); |
| 127 |
return activity; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public void runTestOnUiThread(final Runnable r) throws Throwable { |
| 140 |
final Throwable[] exceptions = new Throwable[1]; |
| 141 |
getInstrumentation().runOnMainSync(new Runnable() { |
| 142 |
public void run() { |
| 143 |
try { |
| 144 |
r.run(); |
| 145 |
} catch (Throwable throwable) { |
| 146 |
exceptions[0] = throwable; |
| 147 |
} |
| 148 |
} |
| 149 |
}); |
| 150 |
if (exceptions[0] != null) { |
| 151 |
throw exceptions[0]; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
@Override |
| 160 |
protected void runTest() throws Throwable { |
| 161 |
String fName = getName(); |
| 162 |
assertNotNull(fName); |
| 163 |
Method method = null; |
| 164 |
try { |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
method = getClass().getMethod(fName, (Class[]) null); |
| 170 |
} catch (NoSuchMethodException e) { |
| 171 |
fail("Method \""+fName+"\" not found"); |
| 172 |
} |
| 173 |
|
| 174 |
if (!Modifier.isPublic(method.getModifiers())) { |
| 175 |
fail("Method \""+fName+"\" should be public"); |
| 176 |
} |
| 177 |
|
| 178 |
int runCount = 1; |
| 179 |
boolean isRepetitive = false; |
| 180 |
if (method.isAnnotationPresent(FlakyTest.class)) { |
| 181 |
runCount = method.getAnnotation(FlakyTest.class).tolerance(); |
| 182 |
} else if (method.isAnnotationPresent(RepetitiveTest.class)) { |
| 183 |
runCount = method.getAnnotation(RepetitiveTest.class).numIterations(); |
| 184 |
isRepetitive = true; |
| 185 |
} |
| 186 |
|
| 187 |
if (method.isAnnotationPresent(UiThreadTest.class)) { |
| 188 |
final int tolerance = runCount; |
| 189 |
final boolean repetitive = isRepetitive; |
| 190 |
final Method testMethod = method; |
| 191 |
final Throwable[] exceptions = new Throwable[1]; |
| 192 |
getInstrumentation().runOnMainSync(new Runnable() { |
| 193 |
public void run() { |
| 194 |
try { |
| 195 |
runMethod(testMethod, tolerance, repetitive); |
| 196 |
} catch (Throwable throwable) { |
| 197 |
exceptions[0] = throwable; |
| 198 |
} |
| 199 |
} |
| 200 |
}); |
| 201 |
if (exceptions[0] != null) { |
| 202 |
throw exceptions[0]; |
| 203 |
} |
| 204 |
} else { |
| 205 |
runMethod(method, runCount, isRepetitive); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
private void runMethod(Method runMethod, int tolerance) throws Throwable { |
| 211 |
runMethod(runMethod, tolerance, false); |
| 212 |
} |
| 213 |
|
| 214 |
private void runMethod(Method runMethod, int tolerance, boolean isRepetitive) throws Throwable { |
| 215 |
Throwable exception = null; |
| 216 |
|
| 217 |
int runCount = 0; |
| 218 |
do { |
| 219 |
try { |
| 220 |
runMethod.invoke(this, (Object[]) null); |
| 221 |
exception = null; |
| 222 |
} catch (InvocationTargetException e) { |
| 223 |
e.fillInStackTrace(); |
| 224 |
exception = e.getTargetException(); |
| 225 |
} catch (IllegalAccessException e) { |
| 226 |
e.fillInStackTrace(); |
| 227 |
exception = e; |
| 228 |
} finally { |
| 229 |
runCount++; |
| 230 |
|
| 231 |
if (isRepetitive) { |
| 232 |
Bundle iterations = new Bundle(); |
| 233 |
iterations.putInt("currentiterations", runCount); |
| 234 |
getInstrumentation().sendStatus(2, iterations); |
| 235 |
} |
| 236 |
} |
| 237 |
} while ((runCount < tolerance) && (isRepetitive || exception != null)); |
| 238 |
|
| 239 |
if (exception != null) { |
| 240 |
throw exception; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public void sendKeys(String keysSequence) { |
| 254 |
final String[] keys = keysSequence.split(" "); |
| 255 |
final int count = keys.length; |
| 256 |
|
| 257 |
final Instrumentation instrumentation = getInstrumentation(); |
| 258 |
|
| 259 |
for (int i = 0; i < count; i++) { |
| 260 |
String key = keys[i]; |
| 261 |
int repeater = key.indexOf('*'); |
| 262 |
|
| 263 |
int keyCount; |
| 264 |
try { |
| 265 |
keyCount = repeater == -1 ? 1 : Integer.parseInt(key.substring(0, repeater)); |
| 266 |
} catch (NumberFormatException e) { |
| 267 |
Log.w("ActivityTestCase", "Invalid repeat count: " + key); |
| 268 |
continue; |
| 269 |
} |
| 270 |
|
| 271 |
if (repeater != -1) { |
| 272 |
key = key.substring(repeater + 1); |
| 273 |
} |
| 274 |
|
| 275 |
for (int j = 0; j < keyCount; j++) { |
| 276 |
try { |
| 277 |
final Field keyCodeField = KeyEvent.class.getField("KEYCODE_" + key); |
| 278 |
final int keyCode = keyCodeField.getInt(null); |
| 279 |
try { |
| 280 |
instrumentation.sendKeyDownUpSync(keyCode); |
| 281 |
} catch (SecurityException e) { |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
} |
| 286 |
} catch (NoSuchFieldException e) { |
| 287 |
Log.w("ActivityTestCase", "Unknown keycode: KEYCODE_" + key); |
| 288 |
break; |
| 289 |
} catch (IllegalAccessException e) { |
| 290 |
Log.w("ActivityTestCase", "Unknown keycode: KEYCODE_" + key); |
| 291 |
break; |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
instrumentation.waitForIdleSync(); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public void sendKeys(int... keys) { |
| 306 |
final int count = keys.length; |
| 307 |
final Instrumentation instrumentation = getInstrumentation(); |
| 308 |
|
| 309 |
for (int i = 0; i < count; i++) { |
| 310 |
try { |
| 311 |
instrumentation.sendKeyDownUpSync(keys[i]); |
| 312 |
} catch (SecurityException e) { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
instrumentation.waitForIdleSync(); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public void sendRepeatedKeys(int... keys) { |
| 330 |
final int count = keys.length; |
| 331 |
if ((count & 0x1) == 0x1) { |
| 332 |
throw new IllegalArgumentException("The size of the keys array must " |
| 333 |
+ "be a multiple of 2"); |
| 334 |
} |
| 335 |
|
| 336 |
final Instrumentation instrumentation = getInstrumentation(); |
| 337 |
|
| 338 |
for (int i = 0; i < count; i += 2) { |
| 339 |
final int keyCount = keys[i]; |
| 340 |
final int keyCode = keys[i + 1]; |
| 341 |
for (int j = 0; j < keyCount; j++) { |
| 342 |
try { |
| 343 |
instrumentation.sendKeyDownUpSync(keyCode); |
| 344 |
} catch (SecurityException e) { |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
instrumentation.waitForIdleSync(); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
@Override |
| 363 |
protected void tearDown() throws Exception { |
| 364 |
Runtime.getRuntime().gc(); |
| 365 |
Runtime.getRuntime().runFinalization(); |
| 366 |
Runtime.getRuntime().gc(); |
| 367 |
super.tearDown(); |
| 368 |
} |
| 369 |
} |