Skip to content

Commit

Permalink
ISSUE-604: LeakCanary added to dependencies, also created DetectMemor…
Browse files Browse the repository at this point in the history
…yLeaksRule
  • Loading branch information
avfe committed Dec 7, 2023
1 parent c6df154 commit c247e67
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions kaspresso/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
api(libs.uiAutomator)
api(libs.androidXCore)
api(libs.androidXTestRules)
api(libs.leakCanary)

implementation(libs.kotlinStdlib)
implementation(libs.gson)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Unit, Unit>
) {
fun after(actions: BaseTestContext.() -> Unit) = Init(after.after {
LeakAssertions.assertNoLeaks()
actions(this)
})
}

class Init(
private val init: InitSection<Unit, Unit>
) {
fun run(steps: TestContext<Unit>.() -> Unit) = init.run {
steps(this)
}
}
}

0 comments on commit c247e67

Please sign in to comment.