android / android/android-test

ActivityScenario: expectations for Activity.isTaskRoot() behavior

Open
#427 9 comments 14 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.2k
Forks
342
Avg merge
11h 29m
Merged PRs (30d)
2

Description

### Description

When using `ActivityScenario` to test activities, the activity under test is not considered as the task root. I guess this is by design since `FLAG_ACTIVITY_NEW_TASK` is masked when launched via `BootstrapActivity` in `InstrumentationActivityInvoker`. For activities that make use of `Activity.isTaskRoot()` this could lead to unexpected behaviors during testing since the activity under test isn't actually the root of the task. This is a behavior change compared to running the test with `ActivityTestRule` or in some cases compared to running the real application. Is it reasonable to expect the activity under test to be considered as the task root even if it is somehow faked or configurable to return true?

### Steps to Reproduce

minimal sample:
```kotlin
// in main source set as the launcher activity
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!isTaskRoot) {
finish()
}
}
}

// in test sources
class MainActivityTest {
@Test
fun scenario() {
ActivityScenario.launch(MainActivity::class.java).onActivity { }
}

@Test
fun testRule() {
val rule = ActivityTestRule(MainActivity::class.java, true, false)
rule.launchActivity(null)
assertTrue(rule.activity.isTaskRoot)
}
}
```

### Expected Results

Be able to test activities that are making use of `isTaskRoot()` reliably.

### Actual Results

scenario test case cannot run `onActivity` since the activity is immediately finished.
testRule test case works as expected.

### AndroidX Test and Android OS Versions

1.2.1-alpha02, API 28

Contributor guide

Open the contributing guide

Research direction

Start with ActivityScenario and the BootstrapActivity launch path, then inspect InstrumentationActivityInvoker where FLAG_ACTIVITY_NEW_TASK is masked. Reproduce the Kotlin sample on API 28 and compare ActivityScenario with ActivityTestRule; done means the activity under test can reliably report the expected isTaskRoot() behavior and the scenario reaches onActivity.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java, kotlin
Domain
mobile-dev, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.