Skip to content

Commit

Permalink
Create wrapper for ActivityScenario for extra use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed May 23, 2023
1 parent 5adc4fe commit 8fa6908
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object OnSavedInstanceStateRegistry {

private var bundle: Bundle? = null

fun setState(savedInstanceState: Bundle) {
fun setState(savedInstanceState: Bundle?) {
bundle = savedInstanceState
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ class ProcessRestoreTest {
* when navigated back to
*/
private fun <T : Page<T>> simulateProcessRestore(destination: Page<T>): Page<T> {
rule.destroyAndRestoreActivity {
CollectHelpers.simulateProcessRestart()
}
rule.navigateAwayFromActivity()
rule.destroyActivity()

CollectHelpers.simulateProcessRestart()
rule.restoreActivity()

return destination.assertOnPage()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class SavePointTest {
* Simulate a "process death" case where an app in the background is killed
*/
private fun simulateProcessDeath(): FormEntryActivityTestRule {
rule.saveInstanceStateForActivity()
rule.navigateAwayFromActivity()
.destroyActivity()

CollectHelpers.simulateProcessRestart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import org.odk.collect.android.external.FormsContract
import org.odk.collect.android.formmanagement.FormFillingIntentFactory
import org.odk.collect.android.injection.DaggerUtils
import org.odk.collect.android.storage.StorageSubdirectory
import org.odk.collect.android.support.ActivityHelpers
import org.odk.collect.android.support.StorageUtils
import org.odk.collect.android.support.pages.FormEntryPage
import org.odk.collect.android.support.pages.FormHierarchyPage
Expand All @@ -27,7 +26,7 @@ import java.io.IOException
class FormEntryActivityTestRule : ExternalResource() {

private lateinit var intent: Intent
private lateinit var scenario: ActivityScenario<Activity>
private lateinit var scenario: ActivityScenarioWrapper

override fun after() {
try {
Expand All @@ -54,7 +53,7 @@ class FormEntryActivityTestRule : ExternalResource() {

fun <D : Page<D>> fillNewForm(formFilename: String, destination: D): D {
intent = createNewFormIntent(formFilename)
scenario = ActivityScenario.launch(intent)
scenario = ActivityScenarioWrapper.launch(intent)
return destination.assertOnPage()
}

Expand All @@ -64,51 +63,23 @@ class FormEntryActivityTestRule : ExternalResource() {

fun editForm(formFilename: String, instanceName: String): FormHierarchyPage {
intent = createEditFormIntent(formFilename)
scenario = ActivityScenario.launch(intent)
scenario = ActivityScenarioWrapper.launch(intent)
return FormHierarchyPage(instanceName).assertOnPage()
}

fun saveInstanceStateForActivity(): FormEntryActivityTestRule {
scenario.onActivity {
it.onSaveInstanceState(Bundle(), PersistableBundle())
}

return this
}

fun destroyAndRestoreActivity(betweenDestroyAndCreate: () -> Unit): FormEntryActivityTestRule {
assertNoBackstack()

scenario.onActivity {
val outState = Bundle()
it.onSaveInstanceState(outState, PersistableBundle())
OnSavedInstanceStateRegistry.setState(outState)
}

destroyActivity()

betweenDestroyAndCreate()
scenario = ActivityScenario.launch(intent)

fun navigateAwayFromActivity(): FormEntryActivityTestRule {
scenario.moveToState(Lifecycle.State.STARTED)
scenario.saveInstanceState()
return this
}

fun destroyActivity(): FormEntryActivityTestRule {
assertNoBackstack()

scenario.moveToState(Lifecycle.State.DESTROYED)
return this
}

private fun assertNoBackstack() {
lateinit var scenarioActivity: Activity
scenario.onActivity {
scenarioActivity = it
}

if (ActivityHelpers.getActivity() != scenarioActivity) {
throw IllegalStateException("Can't manipulate state for backstack!")
}
fun restoreActivity() {
scenario.restore()
}

private fun createNewFormIntent(formFilename: String): Intent {
Expand All @@ -120,7 +91,11 @@ class FormEntryActivityTestRule : ExternalResource() {
val projectId = DaggerUtils.getComponent(application).currentProjectProvider()
.getCurrentProject().uuid

return FormFillingIntentFactory.newInstanceIntent(application, FormsContract.getUri(projectId, form!!.dbId), FormFillingActivity::class)
return FormFillingIntentFactory.newInstanceIntent(
application,
FormsContract.getUri(projectId, form!!.dbId),
FormFillingActivity::class
)
}

private fun createEditFormIntent(formFilename: String): Intent {
Expand All @@ -142,3 +117,34 @@ class FormEntryActivityTestRule : ExternalResource() {
)
}
}

private class ActivityScenarioWrapper private constructor(private var intent: Intent) {

private var outState: Bundle? = null
private var scenario: ActivityScenario<Activity> = ActivityScenario.launch(intent)

fun moveToState(newState: Lifecycle.State) {
scenario.moveToState(newState)
}

fun restore() {
OnSavedInstanceStateRegistry.setState(outState)
scenario = ActivityScenario.launch(intent)
}

fun saveInstanceState() {
val bundle = Bundle()
scenario.onActivity { it.onSaveInstanceState(bundle, PersistableBundle()) }
outState = bundle
}

fun close() {
scenario.close()
}

companion object {
fun launch(intent: Intent): ActivityScenarioWrapper {
return ActivityScenarioWrapper(intent)
}
}
}

0 comments on commit 8fa6908

Please sign in to comment.