android / android/android-test
Unable to test activity that calls finish in onStart.
- Dominant language
- Java
- Stars
- 1.2k
- Forks
- 342
- Avg merge
- 11h 29m
- Merged PRs (30d)
- 2
Description
### Description
Add support for the case when Activity calls `finish` in onStart
In this case activity never reaches RESUMED state and moveToState waits 45s instead of immediate return.
### Steps to Reproduce
Add next activity and run tests:
TestActivity.kt:
```kotlin
class TestActivity: AppCompatActivity() {
var finishOnStart = false
override fun onStart() {
super.onStart()
if (finishOnStart) {
finish()
}
finishOnStart = true
}
}
```
TestActivityTest.lt:
```kotlin
class TestActivityTest {
@Test
fun testOnStart() {
val scenario = ActivityScenario.launch(TestActivity::class.java)
check(scenario.state == Lifecycle.State.RESUMED)
scenario.moveToState(Lifecycle.State.CREATED)
check(scenario.state == Lifecycle.State.CREATED)
// activity calls finish() in onStart
scenario.moveToState(Lifecycle.State.RESUMED)
check(scenario.state == Lifecycle.State.DESTROYED)
}
}
```
### Expected Results
Test succeed.
### Actual Results
AssertionError thrown after 45s waiting for activity to reach RESUMED state.
Contributor guide
Research direction
Start with ActivityScenario.moveToState and reproduce the lifecycle sequence from TestActivity.kt and TestActivityTest.lt, where finish() is called in onStart. Trace the RESUMED wait and lifecycle transitions; done means the test reaches DESTROYED immediately without waiting 45 seconds.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile-dev, testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100