-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bae5880
commit c70e3a1
Showing
4 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
sample/src/androidTest/java/com/sebaslogen/resacaapp/sample/MemoryLeakTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.sebaslogen.resacaapp.sample | ||
|
||
import android.content.Intent | ||
import androidx.compose.ui.test.junit4.createEmptyComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.sebaslogen.resacaapp.sample.ui.main.ComposeActivity | ||
import com.sebaslogen.resacaapp.sample.ui.main.rememberScopedDestination | ||
import com.sebaslogen.resacaapp.sample.utils.ComposeTestUtils | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.lang.ref.WeakReference | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class MemoryLeakTests : ComposeTestUtils { | ||
|
||
private lateinit var scenario: ActivityScenario<ComposeActivity> | ||
|
||
@get:Rule | ||
override val composeTestRule = createEmptyComposeRule() | ||
|
||
@Before | ||
fun setUp() { | ||
scenario = ActivityScenario.launch( | ||
Intent(ApplicationProvider.getApplicationContext(), ComposeActivity::class.java).apply { | ||
putExtra(ComposeActivity.START_DESTINATION, rememberScopedDestination) | ||
}) | ||
} | ||
|
||
@Test | ||
fun givenComposeActivityWithComposablesInANestedNavigationComposable_whenTheActivityIsRecreated_thenTheOriginalComposeActivityObjectIsGarbageCollected() { | ||
var weakActivityReference: WeakReference<ComposeActivity>? = null | ||
// Given I create the Activity | ||
composeTestRule.waitForIdle() | ||
scenario.onActivity { activity: ComposeActivity -> | ||
|
||
// And we grab a WeakReference to the Activity | ||
weakActivityReference = WeakReference(activity) | ||
} | ||
printComposeUiTreeToLog() | ||
|
||
// And I click "Navigate to rememberScoped" to get to a nested screen in the same Activity | ||
composeTestRule.waitForIdle() | ||
composeTestRule.onNodeWithTag("Navigate to rememberScoped").performClick() | ||
printComposeUiTreeToLog() | ||
composeTestRule.waitForIdle() | ||
|
||
// When we recreate the activity | ||
scenario.recreate() | ||
composeTestRule.waitForIdle() | ||
|
||
// And trigger Garbage Collection to make sure old ComposeActivity is collected | ||
Runtime.getRuntime().gc() | ||
printComposeUiTreeToLog() | ||
|
||
// Then the original Activity object is garbage collected | ||
assertNotNull(weakActivityReference) { "WeakReference container for initial ComposeActivity should not be null because it was created" } | ||
assertNull(weakActivityReference?.get(), "Initial ComposeActivity should have been garbage collected but it wasn't, so it's leaking") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
sample/src/test/java/com/sebaslogen/resacaapp/sample/MemoryLeakTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.sebaslogen.resacaapp.sample | ||
|
||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.performClick | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.sebaslogen.resacaapp.sample.ui.main.ComposeActivity | ||
import com.sebaslogen.resacaapp.sample.ui.main.rememberScopedDestination | ||
import com.sebaslogen.resacaapp.sample.utils.ComposeTestUtils | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.lang.ref.WeakReference | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class MemoryLeakTests : ComposeTestUtils { | ||
init { | ||
callFromTestInit() | ||
} | ||
|
||
override fun callFromTestInit() { | ||
ComposeActivity.defaultDestination = rememberScopedDestination // This is needed to reset the destination to the default one on the release app | ||
} | ||
|
||
@get:Rule | ||
override val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun `given ComposeActivity with Composables in a nested Navigation Composable, when the activity is recreated, then the original ComposeActivity object is garbage collected`() { | ||
ActivityScenario.launch(ComposeActivity::class.java).use { scenario -> | ||
var weakActivityReference: WeakReference<ComposeActivity>? = null | ||
// Given I create the Activity and navigate to a nested screen | ||
scenario.onActivity { activity: ComposeActivity -> | ||
|
||
// Given the Activity shows a screen with scoped objects | ||
printComposeUiTreeToLog() | ||
|
||
// And I grab a WeakReference to the Activity | ||
weakActivityReference = WeakReference(activity) | ||
|
||
// And I click "Navigate to rememberScoped" to get to a nested screen in the same Activity | ||
onNodeWithTestTag("Navigate to rememberScoped").performClick() | ||
printComposeUiTreeToLog() | ||
} | ||
|
||
// When we recreate the activity | ||
scenario.recreate().onActivity { | ||
|
||
// And trigger Garbage Collection to make sure old ComposeActivity is collected | ||
System.gc() | ||
printComposeUiTreeToLog() | ||
|
||
// Then the original Activity object is garbage collected | ||
assertNotNull(weakActivityReference) { "WeakReference container for initial ComposeActivity should not be null because it was created" } | ||
assertNull(weakActivityReference?.get(), "Initial ComposeActivity should have been garbage collected but it wasn't, so it's leaking") | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters