/** Can be overridden to report leaks in a different way or do additional reporting. */ protected void reportLeaks(@NonNull InstrumentationLeakResults results) { if (!results.detectedLeaks.isEmpty()) { String message = checkNotNull(buildLeakDetectedMessage(results.detectedLeaks), "buildLeakDetectedMessage"); bundle.putString(InstrumentationResultPrinter.REPORT_KEY_STACK, message); getInstrumentation().sendStatus(REPORT_VALUE_RESULT_FAILURE, bundle); } }
@Override public final void testStarted(Description description) { skipLeakDetectionReason = skipLeakDetectionReason(description); if (skipLeakDetectionReason != null) { return; } String testClass = description.getClassName(); String testName = description.getMethodName(); bundle = new Bundle(); bundle.putString(Instrumentation.REPORT_KEY_IDENTIFIER, FailTestOnLeakRunListener.class.getName()); bundle.putString(InstrumentationResultPrinter.REPORT_KEY_NAME_CLASS, testClass); bundle.putString(InstrumentationResultPrinter.REPORT_KEY_NAME_TEST, testName); }
@Override public final void testFinished(Description description) { detectLeaks(); LeakCanary.installedRefWatcher().clearWatchedReferences(); }
private void detectLeaks() { if (skipLeakDetectionReason != null) { CanaryLog.d("Skipping leak detection because the test %s", skipLeakDetectionReason); skipLeakDetectionReason = null; return; } InstrumentationLeakDetector leakDetector = new InstrumentationLeakDetector(); InstrumentationLeakResults results = leakDetector.detectLeaks(); reportLeaks(results); }