ObjectWatcherTest.kt
TLDR
This file contains a test class ObjectWatcherTest
which tests the functionality of the ObjectWatcher
class in the leakcanary
package.
Methods
There are no methods in this file.
Classes
ObjectWatcherTest
This test class is used to validate the functionality of the ObjectWatcher
class. It contains two test methods:
-
unreachable object not retained
: This test verifies that when an object is no longer reachable, it is not retained by theObjectWatcher
. It sets up anObjectWatcher
instance, creates an object reference (ref
) and expects it to be weakly reachable. Then, it sets the object reference to null, triggers garbage collection, and checks if theObjectWatcher
does not have any retained objects. -
reachable object retained
: This test validates that when an object is still reachable, it is retained by theObjectWatcher
. Similar to the previous test, it sets up anObjectWatcher
instance, creates an object reference (ref
) and expects it to be weakly reachable. Then, it triggers garbage collection and checks if theObjectWatcher
has any retained objects.
package leakcanary
import leakcanary.GcTrigger.Default.runGc
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.util.concurrent.Executor
class ObjectWatcherTest {
private val checkRetainedExecutor = Executor {
it.run()
}
private val objectWatcher = ObjectWatcher({ time }, checkRetainedExecutor)
var time: Long = 0
var ref: Any? = Any()
@Test fun `unreachable object not retained`() {
objectWatcher.expectWeaklyReachable(ref!!, "unreachable object not retained")
ref = null
runGc()
assertThat(objectWatcher.hasRetainedObjects).isFalse()
}
@Test fun `reachable object retained`() {
objectWatcher.expectWeaklyReachable(ref!!, "reachable object retained")
runGc()
assertThat(objectWatcher.hasRetainedObjects).isTrue()
}
}