Skip to content

Commit

Permalink
test: close Scenarios after use to reduce memory pressure (#17523)
Browse files Browse the repository at this point in the history
* test: close ActivityScenario after use

fixes OutOfMemoryError: Java heap space

Follow up from 1113858

```
CreateDeckDialogTest > searchDecksIconVisibilityDeckCreationTest FAILED
    java.lang.OutOfMemoryError: Java heap space
        at org.robolectric.res.android.CppAssetManager2$Theme.SetTo(CppAssetManager2.java:1700)
        at org.robolectric.shadows.ShadowArscAssetManager10.nativeThemeCopy(ShadowArscAssetManager10.java:1657)
        ...
```

Cause:

https://redirect.github.com/ankidroid/Anki-Android/pull/17495

* test: close FragmentScenario after use

fixes OutOfMemoryError: Java heap space

Follow up from 1113858

```
CreateDeckDialogTest > searchDecksIconVisibilityDeckCreationTest FAILED
    java.lang.OutOfMemoryError: Java heap space
        at org.robolectric.res.android.CppAssetManager2$Theme.SetTo(CppAssetManager2.java:1700)
        at org.robolectric.shadows.ShadowArscAssetManager10.nativeThemeCopy(ShadowArscAssetManager10.java:1657)
        ...
```

Cause:

https://redirect.github.com/ankidroid/Anki-Android/pull/17495
  • Loading branch information
david-allison authored Nov 30, 2024
1 parent 4f4cd00 commit 97e7386
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ class FilteredDeckOptionsTest : InstrumentedTest() {

@Test
fun canOpenDeckOptions() {
ActivityScenario.launch(DeckPicker::class.java).onActivity { deckPicker ->
val deckId = col.decks.newFiltered("Filtered Testing")
deckPicker.showContextMenuDeckOptions(deckId)
}.close()
ActivityScenario.launch(DeckPicker::class.java).use { scenario ->
scenario.onActivity { deckPicker ->
val deckId = col.decks.newFiltered("Filtered Testing")
deckPicker.showContextMenuDeckOptions(deckId)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,10 @@ class TagsDialogTest : RobolectricTest() {
.arguments
val mockListener = Mockito.mock(TagsDialogListener::class.java)
val factory = TagsDialogFactory(mockListener)
val scenario = FragmentScenario.launch(TagsDialog::class.java, args, R.style.Theme_Light, factory)
scenario.moveToState(Lifecycle.State.STARTED)
scenario.onFragment { Timber.d("Dialog successfully opened") }
FragmentScenario.launch(TagsDialog::class.java, args, R.style.Theme_Light, factory).use { scenario ->
scenario.moveToState(Lifecycle.State.STARTED)
scenario.onFragment { Timber.d("Dialog successfully opened") }
}
}

@Test
Expand All @@ -637,20 +638,21 @@ class TagsDialogTest : RobolectricTest() {
.arguments
val mockListener = Mockito.mock(TagsDialogListener::class.java)
val factory = TagsDialogFactory(mockListener)
val scenario = FragmentScenario.launch(TagsDialog::class.java, args, R.style.Theme_Light, factory)
scenario.moveToState(Lifecycle.State.STARTED)
scenario.onFragment { f ->
val tagsFile = requireNotNull(
BundleCompat.getParcelable(
f.requireArguments(),
"tagsFile",
TagsFile::class.java
FragmentScenario.launch(TagsDialog::class.java, args, R.style.Theme_Light, factory).use { scenario ->
scenario.moveToState(Lifecycle.State.STARTED)
scenario.onFragment { f ->
val tagsFile = requireNotNull(
BundleCompat.getParcelable(
f.requireArguments(),
"tagsFile",
TagsFile::class.java
)
)
)

val dataFromArguments = tagsFile.getData()
val dataFromArguments = tagsFile.getData()

assertThat(dataFromArguments.allTags, containsInAnyOrder(allTags))
assertThat(dataFromArguments.allTags, containsInAnyOrder(allTags))
}
}
}

Expand Down
52 changes: 27 additions & 25 deletions AnkiDroid/src/test/java/com/ichi2/anki/pages/DeckOptionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ class DeckOptionsTest : RobolectricTest() {
// Launch deck options
val intent = DeckOptions.getIntent(targetContext, col.decks.selected())

val scenario = ActivityScenario.launch<SingleFragmentActivity>(intent)
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.onActivity { activity ->
assertThat("Activity should not be finishing", !activity.isFinishing)
ActivityScenario.launch<SingleFragmentActivity>(intent).use { scenario ->
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.onActivity { activity ->
assertThat("Activity should not be finishing", !activity.isFinishing)

// Perform system-level back press
pressBack()
// Perform system-level back press
pressBack()

// Discard on modal
clickAlertDialogButton(DialogInterface.BUTTON_POSITIVE, true)
// Discard on modal
clickAlertDialogButton(DialogInterface.BUTTON_POSITIVE, true)

assertThat("Activity should be finishing", activity.isFinishing)
assertThat("Activity should be finishing", activity.isFinishing)
}
}
}

Expand All @@ -59,24 +60,25 @@ class DeckOptionsTest : RobolectricTest() {
// Launch deck options
val intent = DeckOptions.getIntent(targetContext, col.decks.selected())

val scenario = ActivityScenario.launch<SingleFragmentActivity>(intent)
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.onActivity { activity ->
assertThat("Activity should not be finishing", !activity.isFinishing)
}
ActivityScenario.launch<SingleFragmentActivity>(intent).use { scenario ->
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.onActivity { activity ->
assertThat("Activity should not be finishing", !activity.isFinishing)
}

// Perform toolbar up button press
try {
onView(withContentDescription("Navigate up")).perform(click())
} catch (ex: NoMatchingViewException) {
Timber.d("Toolbar UP button not found. Is english locale being used?")
return // Abort
}
// Perform toolbar up button press
try {
onView(withContentDescription("Navigate up")).perform(click())
} catch (ex: NoMatchingViewException) {
Timber.d("Toolbar UP button not found. Is english locale being used?")
return // Abort
}

scenario.onActivity { activity ->
// Discard on modal
clickAlertDialogButton(DialogInterface.BUTTON_POSITIVE, true)
assertThat("Activity should be finishing", activity.isFinishing)
scenario.onActivity { activity ->
// Discard on modal
clickAlertDialogButton(DialogInterface.BUTTON_POSITIVE, true)
assertThat("Activity should be finishing", activity.isFinishing)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ class PreferencesTest : RobolectricTest() {
/** checks if any of the Preferences fragments throws while being created */
@Test
fun fragmentsDoNotThrowOnCreation() {
val activityScenario = ActivityScenario.launch(Preferences::class.java)

activityScenario.onActivity { activity ->
PreferenceTestUtils.getAllPreferencesFragments(activity).forEach {
activity.supportFragmentManager.commitNow {
add(R.id.settings_container, it)
ActivityScenario.launch(Preferences::class.java).use { activityScenario ->
activityScenario.onActivity { activity ->
PreferenceTestUtils.getAllPreferencesFragments(activity).forEach {
activity.supportFragmentManager.commitNow {
add(R.id.settings_container, it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ class PermissionsActivityTest : RobolectricTest() {
ApplicationProvider.getApplicationContext(),
permissionSet
)
ActivityScenario.launch<PermissionsActivity>(intent).onActivity { activity ->
action.perform(activity)
ActivityScenario.launch<PermissionsActivity>(intent).use { scenario ->
scenario.onActivity { activity ->
action.perform(activity)
}
}
}

Expand Down

0 comments on commit 97e7386

Please sign in to comment.