TuPeuxPasTest.kt
TLDR
This file contains a UI test class named TuPeuxPasTest
that is designed to intentionally fail because it triggers a leak. The purpose of the test is to demonstrate how testing for memory leaks can be done using a library called LeakCanary.
Methods
No methods are present in this file.
Classes
TuPeuxPasTest
This class is a UI test that triggers a memory leak intentionally. It is used to demonstrate the usage of LeakCanary library for testing memory leaks. The class has a single test method called activityLeakingAfterTest
.
In this test method, an ActivityScenarioRule
is used to launch an instance of the MainActivity
. Inside the onActivity
lambda, the MainActivity
instance is added to a list of leaked objects. The purpose of this test is to show that the MainActivity
is not properly destroyed after the test, resulting in a memory leak.
The TuPeuxPasTest
class also contains a companion object called leakedObjects
which is a mutable list of Any
type. This list is used to track leaked objects in the test.
package leakcanary.tests
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.example.leakcanary.MainActivity
import leakcanary.DetectLeaksAfterTestSuccess
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
/**
* This UI test looks like it should succeed, but it will actually fail because
* it triggers a leak.
*
* Run this test with:
*
* ./gradlew leakcanary-android-sample:connectedCheck
*
* Why is this class named "TuPeuxPasTest"?
*
* This test fails, intentionally. In French, "Tu peux pas test" could mean "you cannot test"
* written with poor grammar. Except, that's not what it means.
* If you're curious, interested in French and have time to waste:
* https://www.youtube.com/watch?v=DZZpbmAc-0A
* https://www.youtube.com/watch?v=nHeAA6X-XUQ
*/
class TuPeuxPasTest {
private val activityRule = ActivityScenarioRule(MainActivity::class.java)
@get:Rule
val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(activityRule)!!
@Test
fun activityLeakingAfterTest() {
activityRule.scenario.onActivity { activity ->
leakedObjects += activity
}
}
companion object {
val leakedObjects = mutableListOf<Any>()
}
}