package leakcanary

import leakcanary.InstrumentationLeakDetector.Result.AnalysisPerformed
import leakcanary.InstrumentationLeakDetector.Result.NoAnalysis
import shark.HeapAnalysisSuccess

object TestUtils {
  fun assertLeak(expectedLeakClass: Class<*>) {
    val leakDetector = InstrumentationLeakDetector()

    val heapAnalysis = when (val result = leakDetector.detectLeaks()) {
      is NoAnalysis -> throw AssertionError(
        "Expected analysis to be performed but skipped because ${result.reason}"
      )
      is AnalysisPerformed -> result.heapAnalysis
    }

    if (heapAnalysis !is HeapAnalysisSuccess) {
      throw AssertionError(
        "Expected analysis success not $heapAnalysis"
      )
    }

    val applicationLeaks = heapAnalysis.applicationLeaks
    if (applicationLeaks.size != 1) {
      throw AssertionError(
        "Expected exactly one leak in $heapAnalysis"
      )
    }

    val leak = applicationLeaks.first()

    val leakTrace = leak.leakTraces.first()
    val className = leakTrace.leakingObject.className
    if (className != expectedLeakClass.name) {
      throw AssertionError(
        "Expected a leak of $expectedLeakClass, not $className in $heapAnalysis"
      )
    }
  }
}
package leakcanary

import shark.HeapAnalysis
import shark.HeapAnalysisSuccess

object TestUtils {
  fun assertLeak(expectedLeakClass: Class<*>) {
    var heapAnalysisOrNull: HeapAnalysis? = null
    AndroidDetectLeaksAssert { heapAnalysis ->
      heapAnalysisOrNull = heapAnalysis
    }.assertNoLeaks("")

    if (heapAnalysisOrNull == null) {
      throw AssertionError(
        "Expected analysis to be performed but skipped"
      )
    }

    val heapAnalysis = heapAnalysisOrNull

    if (heapAnalysis !is HeapAnalysisSuccess) {
      throw AssertionError(
        "Expected analysis success not $heapAnalysis"
      )
    }

    val applicationLeaks = heapAnalysis.applicationLeaks
    if (applicationLeaks.size != 1) {
      throw AssertionError(
        "Expected exactly one leak in $heapAnalysis"
      )
    }

    val leak = applicationLeaks.first()

    val leakTrace = leak.leakTraces.first()
    val className = leakTrace.leakingObject.className
    if (className != expectedLeakClass.name) {
      throw AssertionError(
        "Expected a leak of $expectedLeakClass, not $className in $heapAnalysis"
      )
    }
  }
}

Migrate TestUtil to use the newer API
Migrate TestUtil to use the newer API

square/leakcanary,square/leakcanary,square/leakcanary