No way to test "System needs resources" situation using `ActivityScenario`
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 28/100
Research direction
Start by reading ActivityScenario and the LocalActivityInvoker and InstrumentationActivityInvoker paths mentioned in the issue, then compare them with the Robolectric lifecycle sequence using Bundle state. Determine whether the system-needs-resources scenario can be supported by both invokers. Done means the capability is implemented and testable, or the supported Robolectric-only limitation is documented.
Written by the indexing model from the issue text.
Description
Description
I'll refer to the lingo used in the recent Google I/O video Best practices for saving UI state on Android as that coincidently covered the situation I'm describing here. It lays out three ways of "losing app state":
- Configuration changes
- System needs resources
- App dismissal
Using ActivityScenario it's currently possible to write tests for 1 and 3, but not for 2.
Here's an example of testing 1:
val activityScenario = ActivityScenario.launch(Activity::class.java)
// Do something
activityScenario.recreate() // Simulates a config change
// Check something
...and here's an example of testing 3:
val activityScenario1 = ActivityScenario.launch(Activity::class.java)
// Do something
val activityScenario2 = ActivityScenario.launch(Activity::class.java) // Simulates a dismissal and a reopen
// Check something
As far as I understand, when 2 occurs, the following things happen:
onSaveInstanceStateis called on the Activity- The Activity is destroyed
- The process is killed
When the user navigates back after 2 the Activity is created again with the Bundle earlier created by onSavedInstanceState. The Activity and its Fragments (or potentially a whole back stack) is still being "recreated" his is distinct from 1 in that the process (and any state it owns) is being destroyed (meaning loss of Application level, ViewModel or any unfortunately static state). As an aside, a good way to reproduce this situation locally (without changing background limits or using a low memory phone) is to remove a permission from an app in system settings.
We actually can test this scenario using Robolectric in a local test:
val activityController1 = Robolectric.buildActivity(Activity::class.java)
activityController1.setup()
// Do something
val outState = Bundle()
activityController1
.pause()
.saveInstanceState(outState)
.stop()
.destroy()
.close()
val activityController2 = Robolectric.buildActivity(Activity::class.java)
activityController2.setup(outState)
// Check something
Given this, it seems like it'd be possible to add support for this situation to LocalActivityInvoker and build an API around it for ActivityScenario, but I'm not sure how plausible it is for InstrumentationActivityInvoker which would be good to discuss here. It could be that this isn't something that AndroidX Test will ever support and will forever be left up to "local" tests, but if so that would be good to call out so that Robolectric support can be documented and/or smoothed out.
AndroidX Test and Android OS Versions
AndroidX Test 1.5.0, any version of Android
- Dominant language
- Java
- Stars
- 1.2k
- Forks
- 342
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 5
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from android/android-test
-
Difficulty 1/5 Under an hour Newbie friendliness 62/100
android/android-test#681 · 2 comments · 1 reaction ·
-
ξεκσνδξδ Open
Difficulty 5/5 Over a week Newbie friendliness 1/100
android/android-test#2499 ·
-
ησξξσ Open
Difficulty 5/5 Over a week Newbie friendliness 1/100
android/android-test#2498 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
android/android-test#2473 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
android/android-test#2461 · 6 comments · 4 reactions ·
All issues in android/android-test
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
apache/cloudstack#14222 ·
-
[BUG]茶杯方块在取茶时会引发崩溃 Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
1.0.0-alpha2 Type/Improvement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
wso2/dpdp-accelerator#272 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
infinispan/infinispan#18150 ·
-
area/frontend
Difficulty 2/5 1-3 hours Newbie friendliness 65/100