main

square/leakcanary

Last updated at: 29/12/2023 09:38

DetectLeaksAfterTestSuccessTest.kt

TLDR

This file contains a test class called DetectLeaksAfterTestSuccessTest that verifies whether the assertNoLeaksInvoked flag is set to true after executing the given test.

Methods

This file does not contain any methods.

Classes

Class DetectLeaksAfterTestSuccessTest

This class defines a test rule called CheckAssertNoLeaksInvoked that ensures the assertNoLeaksInvoked flag is set to true after the test execution. The class also uses the DetectLeaksAfterTestSuccess rule to handle leaks detection after the test success. It includes a test method called emptyTest which triggers the test rules.

package leakcanary

import org.assertj.core.api.Assertions.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

class DetectLeaksAfterTestSuccessTest {

  object CheckAssertNoLeaksInvoked : TestRule {
    override fun apply(base: Statement, description: Description): Statement {
      return object : Statement() {
        override fun evaluate() {
          try {
            var assertNoLeaksInvoked = false
            DetectLeaksAssert.update { tag ->
              assertNoLeaksInvoked = true
            }
            base.evaluate()
            assertThat(assertNoLeaksInvoked).isTrue()
          } finally {
            DetectLeaksAssert.update(AndroidDetectLeaksAssert())
          }
        }
      }
    }
  }

  @get:Rule
  val rule: RuleChain = RuleChain.outerRule(CheckAssertNoLeaksInvoked).around(DetectLeaksAfterTestSuccess())

  @Test fun emptyTest() {
    // This test triggers the rules.
  }
}