| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.test; |
| 18 |
|
| 19 |
import android.test.suitebuilder.annotation.MediumTest; |
| 20 |
import android.test.suitebuilder.annotation.SmallTest; |
| 21 |
import com.android.internal.util.Predicate; |
| 22 |
|
| 23 |
import android.app.Activity; |
| 24 |
import android.app.Instrumentation; |
| 25 |
import android.os.Bundle; |
| 26 |
import android.os.Debug; |
| 27 |
import android.os.Looper; |
| 28 |
import android.test.suitebuilder.TestMethod; |
| 29 |
import android.test.suitebuilder.TestPredicates; |
| 30 |
import android.test.suitebuilder.TestSuiteBuilder; |
| 31 |
import android.test.suitebuilder.annotation.LargeTest; |
| 32 |
import android.util.Log; |
| 33 |
|
| 34 |
import java.io.ByteArrayOutputStream; |
| 35 |
import java.io.File; |
| 36 |
import java.io.PrintStream; |
| 37 |
import java.lang.annotation.Annotation; |
| 38 |
import java.lang.reflect.InvocationTargetException; |
| 39 |
import java.lang.reflect.Method; |
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
import junit.framework.AssertionFailedError; |
| 44 |
import junit.framework.Test; |
| 45 |
import junit.framework.TestCase; |
| 46 |
import junit.framework.TestListener; |
| 47 |
import junit.framework.TestResult; |
| 48 |
import junit.framework.TestSuite; |
| 49 |
import junit.runner.BaseTestRunner; |
| 50 |
import junit.textui.ResultPrinter; |
| 51 |
|
| 52 |
import static android.test.suitebuilder.TestPredicates.hasAnnotation; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
@Deprecated |
| 177 |
public class InstrumentationTestRunner extends Instrumentation implements TestSuiteProvider { |
| 178 |
|
| 179 |
|
| 180 |
static final String ARGUMENT_TEST_CLASS = "class"; |
| 181 |
|
| 182 |
private static final String ARGUMENT_TEST_PACKAGE = "package"; |
| 183 |
|
| 184 |
private static final String ARGUMENT_TEST_SIZE_PREDICATE = "size"; |
| 185 |
|
| 186 |
static final String ARGUMENT_DELAY_MSEC = "delay_msec"; |
| 187 |
|
| 188 |
private static final String SMALL_SUITE = "small"; |
| 189 |
private static final String MEDIUM_SUITE = "medium"; |
| 190 |
private static final String LARGE_SUITE = "large"; |
| 191 |
|
| 192 |
private static final String ARGUMENT_LOG_ONLY = "log"; |
| 193 |
|
| 194 |
static final String ARGUMENT_ANNOTATION = "annotation"; |
| 195 |
|
| 196 |
static final String ARGUMENT_NOT_ANNOTATION = "notAnnotation"; |
| 197 |
|
| 198 |
private static final Predicate<TestMethod> SELECT_SMALL = hasAnnotation(SmallTest.class); |
| 199 |
|
| 200 |
private static final Predicate<TestMethod> SELECT_MEDIUM = hasAnnotation(MediumTest.class); |
| 201 |
|
| 202 |
private static final Predicate<TestMethod> SELECT_LARGE = hasAnnotation(LargeTest.class); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private static final float SMALL_SUITE_MAX_RUNTIME = 100; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
private static final float MEDIUM_SUITE_MAX_RUNTIME = 1000; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public static final String REPORT_VALUE_ID = "InstrumentationTestRunner"; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public static final String REPORT_KEY_NUM_TOTAL = "numtests"; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public static final String REPORT_KEY_NUM_CURRENT = "current"; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public static final String REPORT_KEY_NAME_CLASS = "class"; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public static final String REPORT_KEY_NAME_TEST = "test"; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private static final String REPORT_KEY_RUN_TIME = "runtime"; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
private static final String REPORT_KEY_NUM_ITERATIONS = "numiterations"; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
private static final String REPORT_KEY_SUITE_ASSIGNMENT = "suiteassignment"; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
private static final String REPORT_KEY_COVERAGE_PATH = "coverageFilePath"; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public static final int REPORT_VALUE_RESULT_START = 1; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public static final int REPORT_VALUE_RESULT_OK = 0; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public static final int REPORT_VALUE_RESULT_ERROR = -1; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public static final int REPORT_VALUE_RESULT_FAILURE = -2; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public static final String REPORT_KEY_STACK = "stack"; |
| 294 |
|
| 295 |
|
| 296 |
private static final String DEFAULT_COVERAGE_FILE_NAME = "coverage.ec"; |
| 297 |
|
| 298 |
private static final String LOG_TAG = "InstrumentationTestRunner"; |
| 299 |
|
| 300 |
private final Bundle mResults = new Bundle(); |
| 301 |
private Bundle mArguments; |
| 302 |
private AndroidTestRunner mTestRunner; |
| 303 |
private boolean mDebug; |
| 304 |
private boolean mJustCount; |
| 305 |
private boolean mSuiteAssignmentMode; |
| 306 |
private int mTestCount; |
| 307 |
private String mPackageOfTests; |
| 308 |
private boolean mCoverage; |
| 309 |
private String mCoverageFilePath; |
| 310 |
private int mDelayMsec; |
| 311 |
|
| 312 |
@Override |
| 313 |
public void onCreate(Bundle arguments) { |
| 314 |
super.onCreate(arguments); |
| 315 |
mArguments = arguments; |
| 316 |
|
| 317 |
|
| 318 |
String[] apkPaths = |
| 319 |
{getTargetContext().getPackageCodePath(), getContext().getPackageCodePath()}; |
| 320 |
ClassPathPackageInfoSource.setApkPaths(apkPaths); |
| 321 |
|
| 322 |
Predicate<TestMethod> testSizePredicate = null; |
| 323 |
Predicate<TestMethod> testAnnotationPredicate = null; |
| 324 |
Predicate<TestMethod> testNotAnnotationPredicate = null; |
| 325 |
String testClassesArg = null; |
| 326 |
boolean logOnly = false; |
| 327 |
|
| 328 |
if (arguments != null) { |
| 329 |
|
| 330 |
testClassesArg = arguments.getString(ARGUMENT_TEST_CLASS); |
| 331 |
mDebug = getBooleanArgument(arguments, "debug"); |
| 332 |
mJustCount = getBooleanArgument(arguments, "count"); |
| 333 |
mSuiteAssignmentMode = getBooleanArgument(arguments, "suiteAssignment"); |
| 334 |
mPackageOfTests = arguments.getString(ARGUMENT_TEST_PACKAGE); |
| 335 |
testSizePredicate = getSizePredicateFromArg( |
| 336 |
arguments.getString(ARGUMENT_TEST_SIZE_PREDICATE)); |
| 337 |
testAnnotationPredicate = getAnnotationPredicate( |
| 338 |
arguments.getString(ARGUMENT_ANNOTATION)); |
| 339 |
testNotAnnotationPredicate = getNotAnnotationPredicate( |
| 340 |
arguments.getString(ARGUMENT_NOT_ANNOTATION)); |
| 341 |
|
| 342 |
logOnly = getBooleanArgument(arguments, ARGUMENT_LOG_ONLY); |
| 343 |
mCoverage = getBooleanArgument(arguments, "coverage"); |
| 344 |
mCoverageFilePath = arguments.getString("coverageFile"); |
| 345 |
|
| 346 |
try { |
| 347 |
Object delay = arguments.get(ARGUMENT_DELAY_MSEC); |
| 348 |
if (delay != null) mDelayMsec = Integer.parseInt(delay.toString()); |
| 349 |
} catch (NumberFormatException e) { |
| 350 |
Log.e(LOG_TAG, "Invalid delay_msec parameter", e); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
TestSuiteBuilder testSuiteBuilder = new TestSuiteBuilder(getClass().getName(), |
| 355 |
getTargetContext().getClassLoader()); |
| 356 |
|
| 357 |
if (testSizePredicate != null) { |
| 358 |
testSuiteBuilder.addRequirements(testSizePredicate); |
| 359 |
} |
| 360 |
if (testAnnotationPredicate != null) { |
| 361 |
testSuiteBuilder.addRequirements(testAnnotationPredicate); |
| 362 |
} |
| 363 |
if (testNotAnnotationPredicate != null) { |
| 364 |
testSuiteBuilder.addRequirements(testNotAnnotationPredicate); |
| 365 |
} |
| 366 |
|
| 367 |
if (testClassesArg == null) { |
| 368 |
if (mPackageOfTests != null) { |
| 369 |
testSuiteBuilder.includePackages(mPackageOfTests); |
| 370 |
} else { |
| 371 |
TestSuite testSuite = getTestSuite(); |
| 372 |
if (testSuite != null) { |
| 373 |
testSuiteBuilder.addTestSuite(testSuite); |
| 374 |
} else { |
| 375 |
|
| 376 |
|
| 377 |
testSuiteBuilder.includePackages(""); |
| 378 |
} |
| 379 |
} |
| 380 |
} else { |
| 381 |
parseTestClasses(testClassesArg, testSuiteBuilder); |
| 382 |
} |
| 383 |
|
| 384 |
testSuiteBuilder.addRequirements(getBuilderRequirements()); |
| 385 |
|
| 386 |
mTestRunner = getAndroidTestRunner(); |
| 387 |
mTestRunner.setContext(getTargetContext()); |
| 388 |
mTestRunner.setInstrumentation(this); |
| 389 |
mTestRunner.setSkipExecution(logOnly); |
| 390 |
mTestRunner.setTest(testSuiteBuilder.build()); |
| 391 |
mTestCount = mTestRunner.getTestCases().size(); |
| 392 |
if (mSuiteAssignmentMode) { |
| 393 |
mTestRunner.addTestListener(new SuiteAssignmentPrinter()); |
| 394 |
} else { |
| 395 |
WatcherResultPrinter resultPrinter = new WatcherResultPrinter(mTestCount); |
| 396 |
mTestRunner.addTestListener(new TestPrinter("TestRunner", false)); |
| 397 |
mTestRunner.addTestListener(resultPrinter); |
| 398 |
} |
| 399 |
start(); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public Bundle getArguments() { |
| 408 |
return mArguments; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
protected void addTestListener(TestListener listener){ |
| 415 |
if(mTestRunner!=null && listener!=null){ |
| 416 |
mTestRunner.addTestListener(listener); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
List<Predicate<TestMethod>> getBuilderRequirements() { |
| 421 |
return new ArrayList<Predicate<TestMethod>>(); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
private void parseTestClasses(String testClassArg, TestSuiteBuilder testSuiteBuilder) { |
| 431 |
String[] testClasses = testClassArg.split(","); |
| 432 |
for (String testClass : testClasses) { |
| 433 |
parseTestClass(testClass, testSuiteBuilder); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
private void parseTestClass(String testClassName, TestSuiteBuilder testSuiteBuilder) { |
| 445 |
int methodSeparatorIndex = testClassName.indexOf('#'); |
| 446 |
String testMethodName = null; |
| 447 |
|
| 448 |
if (methodSeparatorIndex > 0) { |
| 449 |
testMethodName = testClassName.substring(methodSeparatorIndex + 1); |
| 450 |
testClassName = testClassName.substring(0, methodSeparatorIndex); |
| 451 |
} |
| 452 |
testSuiteBuilder.addTestClassByName(testClassName, testMethodName, getTargetContext()); |
| 453 |
} |
| 454 |
|
| 455 |
protected AndroidTestRunner getAndroidTestRunner() { |
| 456 |
return new AndroidTestRunner(); |
| 457 |
} |
| 458 |
|
| 459 |
private boolean getBooleanArgument(Bundle arguments, String tag) { |
| 460 |
String tagString = arguments.getString(tag); |
| 461 |
return tagString != null && Boolean.parseBoolean(tagString); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
private Predicate<TestMethod> getSizePredicateFromArg(String sizeArg) { |
| 468 |
|
| 469 |
if (SMALL_SUITE.equals(sizeArg)) { |
| 470 |
return SELECT_SMALL; |
| 471 |
} else if (MEDIUM_SUITE.equals(sizeArg)) { |
| 472 |
return SELECT_MEDIUM; |
| 473 |
} else if (LARGE_SUITE.equals(sizeArg)) { |
| 474 |
return SELECT_LARGE; |
| 475 |
} else { |
| 476 |
return null; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
private Predicate<TestMethod> getAnnotationPredicate(String annotationClassName) { |
| 487 |
Class<? extends Annotation> annotationClass = getAnnotationClass(annotationClassName); |
| 488 |
if (annotationClass != null) { |
| 489 |
return hasAnnotation(annotationClass); |
| 490 |
} |
| 491 |
return null; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
private Predicate<TestMethod> getNotAnnotationPredicate(String annotationClassName) { |
| 501 |
Class<? extends Annotation> annotationClass = getAnnotationClass(annotationClassName); |
| 502 |
if (annotationClass != null) { |
| 503 |
return TestPredicates.not(hasAnnotation(annotationClass)); |
| 504 |
} |
| 505 |
return null; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
private Class<? extends Annotation> getAnnotationClass(String annotationClassName) { |
| 515 |
if (annotationClassName == null) { |
| 516 |
return null; |
| 517 |
} |
| 518 |
try { |
| 519 |
Class<?> annotationClass = Class.forName(annotationClassName); |
| 520 |
if (annotationClass.isAnnotation()) { |
| 521 |
return (Class<? extends Annotation>)annotationClass; |
| 522 |
} else { |
| 523 |
Log.e(LOG_TAG, String.format("Provided annotation value %s is not an Annotation", |
| 524 |
annotationClassName)); |
| 525 |
} |
| 526 |
} catch (ClassNotFoundException e) { |
| 527 |
Log.e(LOG_TAG, String.format("Could not find class for specified annotation %s", |
| 528 |
annotationClassName)); |
| 529 |
} |
| 530 |
return null; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
void prepareLooper() { |
| 539 |
Looper.prepare(); |
| 540 |
} |
| 541 |
|
| 542 |
@Override |
| 543 |
public void onStart() { |
| 544 |
prepareLooper(); |
| 545 |
|
| 546 |
if (mJustCount) { |
| 547 |
mResults.putString(Instrumentation.REPORT_KEY_IDENTIFIER, REPORT_VALUE_ID); |
| 548 |
mResults.putInt(REPORT_KEY_NUM_TOTAL, mTestCount); |
| 549 |
finish(Activity.RESULT_OK, mResults); |
| 550 |
} else { |
| 551 |
if (mDebug) { |
| 552 |
Debug.waitForDebugger(); |
| 553 |
} |
| 554 |
|
| 555 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 556 |
PrintStream writer = new PrintStream(byteArrayOutputStream); |
| 557 |
try { |
| 558 |
StringResultPrinter resultPrinter = new StringResultPrinter(writer); |
| 559 |
|
| 560 |
mTestRunner.addTestListener(resultPrinter); |
| 561 |
|
| 562 |
long startTime = System.currentTimeMillis(); |
| 563 |
mTestRunner.runTest(); |
| 564 |
long runTime = System.currentTimeMillis() - startTime; |
| 565 |
|
| 566 |
resultPrinter.printResult(mTestRunner.getTestResult(), runTime); |
| 567 |
} catch (Throwable t) { |
| 568 |
|
| 569 |
writer.println(String.format("Test run aborted due to unexpected exception: %s", |
| 570 |
t.getMessage())); |
| 571 |
t.printStackTrace(writer); |
| 572 |
} finally { |
| 573 |
mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 574 |
String.format("\nTest results for %s=%s", |
| 575 |
mTestRunner.getTestClassName(), |
| 576 |
byteArrayOutputStream.toString())); |
| 577 |
|
| 578 |
if (mCoverage) { |
| 579 |
generateCoverageReport(); |
| 580 |
} |
| 581 |
writer.close(); |
| 582 |
|
| 583 |
finish(Activity.RESULT_OK, mResults); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
public TestSuite getTestSuite() { |
| 589 |
return getAllTests(); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public TestSuite getAllTests() { |
| 596 |
return null; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public ClassLoader getLoader() { |
| 603 |
return null; |
| 604 |
} |
| 605 |
|
| 606 |
private void generateCoverageReport() { |
| 607 |
|
| 608 |
|
| 609 |
String coverageFilePath = getCoverageFilePath(); |
| 610 |
java.io.File coverageFile = new java.io.File(coverageFilePath); |
| 611 |
try { |
| 612 |
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT"); |
| 613 |
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData", |
| 614 |
coverageFile.getClass(), boolean.class, boolean.class); |
| 615 |
|
| 616 |
dumpCoverageMethod.invoke(null, coverageFile, false, false); |
| 617 |
|
| 618 |
|
| 619 |
mResults.putString(REPORT_KEY_COVERAGE_PATH, coverageFilePath); |
| 620 |
|
| 621 |
final String currentStream = mResults.getString( |
| 622 |
Instrumentation.REPORT_KEY_STREAMRESULT); |
| 623 |
mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 624 |
String.format("%s\nGenerated code coverage data to %s", currentStream, |
| 625 |
coverageFilePath)); |
| 626 |
} catch (ClassNotFoundException e) { |
| 627 |
reportEmmaError("Is emma jar on classpath?", e); |
| 628 |
} catch (SecurityException e) { |
| 629 |
reportEmmaError(e); |
| 630 |
} catch (NoSuchMethodException e) { |
| 631 |
reportEmmaError(e); |
| 632 |
} catch (IllegalArgumentException e) { |
| 633 |
reportEmmaError(e); |
| 634 |
} catch (IllegalAccessException e) { |
| 635 |
reportEmmaError(e); |
| 636 |
} catch (InvocationTargetException e) { |
| 637 |
reportEmmaError(e); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
private String getCoverageFilePath() { |
| 642 |
if (mCoverageFilePath == null) { |
| 643 |
return getTargetContext().getFilesDir().getAbsolutePath() + File.separator + |
| 644 |
DEFAULT_COVERAGE_FILE_NAME; |
| 645 |
} else { |
| 646 |
return mCoverageFilePath; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
private void reportEmmaError(Exception e) { |
| 651 |
reportEmmaError("", e); |
| 652 |
} |
| 653 |
|
| 654 |
private void reportEmmaError(String hint, Exception e) { |
| 655 |
String msg = "Failed to generate emma coverage. " + hint; |
| 656 |
Log.e(LOG_TAG, msg, e); |
| 657 |
mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "\nError: " + msg); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
private class StringResultPrinter extends ResultPrinter { |
| 662 |
|
| 663 |
public StringResultPrinter(PrintStream writer) { |
| 664 |
super(writer); |
| 665 |
} |
| 666 |
|
| 667 |
public synchronized void printResult(TestResult result, long runTime) { |
| 668 |
printHeader(runTime); |
| 669 |
printFooter(result); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
private class SuiteAssignmentPrinter implements TestListener { |
| 678 |
|
| 679 |
private Bundle mTestResult; |
| 680 |
private long mStartTime; |
| 681 |
private long mEndTime; |
| 682 |
private boolean mTimingValid; |
| 683 |
|
| 684 |
public SuiteAssignmentPrinter() { |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
public void startTest(Test test) { |
| 691 |
mTimingValid = true; |
| 692 |
mStartTime = System.currentTimeMillis(); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
public void addError(Test test, Throwable t) { |
| 699 |
mTimingValid = false; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
public void addFailure(Test test, AssertionFailedError t) { |
| 706 |
mTimingValid = false; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
public void endTest(Test test) { |
| 713 |
float runTime; |
| 714 |
String assignmentSuite; |
| 715 |
mEndTime = System.currentTimeMillis(); |
| 716 |
mTestResult = new Bundle(); |
| 717 |
|
| 718 |
if (!mTimingValid || mStartTime < 0) { |
| 719 |
assignmentSuite = "NA"; |
| 720 |
runTime = -1; |
| 721 |
} else { |
| 722 |
runTime = mEndTime - mStartTime; |
| 723 |
if (runTime < SMALL_SUITE_MAX_RUNTIME |
| 724 |
&& !InstrumentationTestCase.class.isAssignableFrom(test.getClass())) { |
| 725 |
assignmentSuite = SMALL_SUITE; |
| 726 |
} else if (runTime < MEDIUM_SUITE_MAX_RUNTIME) { |
| 727 |
assignmentSuite = MEDIUM_SUITE; |
| 728 |
} else { |
| 729 |
assignmentSuite = LARGE_SUITE; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
mStartTime = -1; |
| 734 |
|
| 735 |
mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 736 |
test.getClass().getName() + "#" + ((TestCase) test).getName() |
| 737 |
+ "\nin " + assignmentSuite + " suite\nrunTime: " |
| 738 |
+ String.valueOf(runTime) + "\n"); |
| 739 |
mTestResult.putFloat(REPORT_KEY_RUN_TIME, runTime); |
| 740 |
mTestResult.putString(REPORT_KEY_SUITE_ASSIGNMENT, assignmentSuite); |
| 741 |
|
| 742 |
sendStatus(0, mTestResult); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
private class WatcherResultPrinter implements TestListener { |
| 750 |
private final Bundle mResultTemplate; |
| 751 |
Bundle mTestResult; |
| 752 |
int mTestNum = 0; |
| 753 |
int mTestResultCode = 0; |
| 754 |
String mTestClass = null; |
| 755 |
|
| 756 |
public WatcherResultPrinter(int numTests) { |
| 757 |
mResultTemplate = new Bundle(); |
| 758 |
mResultTemplate.putString(Instrumentation.REPORT_KEY_IDENTIFIER, REPORT_VALUE_ID); |
| 759 |
mResultTemplate.putInt(REPORT_KEY_NUM_TOTAL, numTests); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
public void startTest(Test test) { |
| 767 |
String testClass = test.getClass().getName(); |
| 768 |
String testName = ((TestCase)test).getName(); |
| 769 |
mTestResult = new Bundle(mResultTemplate); |
| 770 |
mTestResult.putString(REPORT_KEY_NAME_CLASS, testClass); |
| 771 |
mTestResult.putString(REPORT_KEY_NAME_TEST, testName); |
| 772 |
mTestResult.putInt(REPORT_KEY_NUM_CURRENT, ++mTestNum); |
| 773 |
|
| 774 |
if (testClass != null && !testClass.equals(mTestClass)) { |
| 775 |
mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 776 |
String.format("\n%s:", testClass)); |
| 777 |
mTestClass = testClass; |
| 778 |
} else { |
| 779 |
mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, ""); |
| 780 |
} |
| 781 |
|
| 782 |
Method testMethod = null; |
| 783 |
try { |
| 784 |
testMethod = test.getClass().getMethod(testName); |
| 785 |
|
| 786 |
if (testMethod.isAnnotationPresent(RepetitiveTest.class)) { |
| 787 |
int numIterations = testMethod.getAnnotation( |
| 788 |
RepetitiveTest.class).numIterations(); |
| 789 |
mTestResult.putInt(REPORT_KEY_NUM_ITERATIONS, numIterations); |
| 790 |
} |
| 791 |
} catch (NoSuchMethodException e) { |
| 792 |
|
| 793 |
|
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
try { |
| 802 |
if (mTestNum == 1) Thread.sleep(mDelayMsec); |
| 803 |
} catch (InterruptedException e) { |
| 804 |
throw new IllegalStateException(e); |
| 805 |
} |
| 806 |
|
| 807 |
sendStatus(REPORT_VALUE_RESULT_START, mTestResult); |
| 808 |
mTestResultCode = 0; |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
public void addError(Test test, Throwable t) { |
| 815 |
mTestResult.putString(REPORT_KEY_STACK, BaseTestRunner.getFilteredTrace(t)); |
| 816 |
mTestResultCode = REPORT_VALUE_RESULT_ERROR; |
| 817 |
|
| 818 |
mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 819 |
String.format("\nError in %s:\n%s", |
| 820 |
((TestCase)test).getName(), BaseTestRunner.getFilteredTrace(t))); |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
public void addFailure(Test test, AssertionFailedError t) { |
| 827 |
mTestResult.putString(REPORT_KEY_STACK, BaseTestRunner.getFilteredTrace(t)); |
| 828 |
mTestResultCode = REPORT_VALUE_RESULT_FAILURE; |
| 829 |
|
| 830 |
mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 831 |
String.format("\nFailure in %s:\n%s", |
| 832 |
((TestCase)test).getName(), BaseTestRunner.getFilteredTrace(t))); |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
public void endTest(Test test) { |
| 839 |
if (mTestResultCode == 0) { |
| 840 |
mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "."); |
| 841 |
} |
| 842 |
sendStatus(mTestResultCode, mTestResult); |
| 843 |
|
| 844 |
try { |
| 845 |
Thread.sleep(mDelayMsec); |
| 846 |
} catch (InterruptedException e) { |
| 847 |
throw new IllegalStateException(e); |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
} |
| 853 |
} |