diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 278dc38b6..29edeec3d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -48,6 +48,7 @@ espressoWeb = { module = "androidx.test.espresso:espresso-web", version.ref = "e # Workaround for https://github.com/android/android-test/issues/861 accessibilityFramework = "com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:4.0.0" uiAutomator = "androidx.test.uiautomator:uiautomator:2.2.0" +leakCanary = "com.squareup.leakcanary:leakcanary-android-instrumentation:2.12" robolectric = "org.robolectric:robolectric:4.8.2" kakao = { module = "io.github.kakaocup:kakao", version.ref = "kakao" } kakaoCompose = { module = "io.github.kakaocup:compose", version.ref = "kakaoCompose" } diff --git a/kaspresso/build.gradle.kts b/kaspresso/build.gradle.kts index 023d1cd88..54f6d9237 100644 --- a/kaspresso/build.gradle.kts +++ b/kaspresso/build.gradle.kts @@ -17,6 +17,7 @@ dependencies { api(libs.uiAutomator) api(libs.androidXCore) api(libs.androidXTestRules) + api(libs.leakCanary) implementation(libs.kotlinStdlib) implementation(libs.gson) diff --git a/kaspresso/src/main/kotlin/com/kaspersky/kaspresso/testcases/api/testcaserule/DetectMemoryLeaksRule.kt b/kaspresso/src/main/kotlin/com/kaspersky/kaspresso/testcases/api/testcaserule/DetectMemoryLeaksRule.kt new file mode 100644 index 000000000..6c596abfc --- /dev/null +++ b/kaspresso/src/main/kotlin/com/kaspersky/kaspresso/testcases/api/testcaserule/DetectMemoryLeaksRule.kt @@ -0,0 +1,42 @@ +package com.kaspersky.kaspresso.testcases.api.testcaserule + +import com.kaspersky.kaspresso.testcases.core.sections.AfterTestSection +import com.kaspersky.kaspresso.testcases.core.sections.InitSection +import com.kaspersky.kaspresso.testcases.core.testcontext.BaseTestContext +import com.kaspersky.kaspresso.testcases.core.testcontext.TestContext +import leakcanary.LeakAssertions +import org.junit.rules.TestRule +import org.junit.runner.Description +import org.junit.runners.model.Statement + +class DetectMemoryLeaksRule( + testClassName: String +) : TestRule { + + val kaspressoRule = TestCaseRule(testClassName) + + override fun apply(base: Statement, description: Description): Statement { + return kaspressoRule.apply(base, description) + } + + fun before(actions: BaseTestContext.() -> Unit) = After(kaspressoRule.before { + actions(this) + }) + + class After( + private val after: AfterTestSection + ) { + fun after(actions: BaseTestContext.() -> Unit) = Init(after.after { + LeakAssertions.assertNoLeaks() + actions(this) + }) + } + + class Init( + private val init: InitSection + ) { + fun run(steps: TestContext.() -> Unit) = init.run { + steps(this) + } + } +}