android / android/android-test
Feature Request: allow custom `Factory` to be passed into `ActivityScenario` (similar to `FragmentScenario`)
- Dominant language
- Java
- Stars
- 1.2k
- Forks
- 342
- Avg merge
- 11h 29m
- Merged PRs (30d)
- 2
Description
### Description
API 28 introduced `AppComponentFactory`. It allows one to provide a custom constructor to any Android component, including an `Activity`. Most apps cannot utilize this feature due to the minimum API requirement. For those that can, defining a custom constructor for an `Activity` means that there may be no empty default constructor available.
That complicates the usage of `ActivityScenario`: it doesn't support a `Factory` parameter to provide custom dependencies (see [here](https://cs.android.com/androidx/android-test/+/master:core/java/androidx/test/core/app/ActivityScenario.java;l=200?q=ActivityScenario)).
To address this issue, I propose that `ActivityScenario` should support a `Factory` parameter, similar to `FragmentScenario` (see [here](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:fragment/fragment-testing/src/main/java/androidx/fragment/app/testing/FragmentScenario.kt;l=435?q=launchInContainer)). This would enable developers to take full advantage of providing custom dependencies during tests:
```kotlin
val scenario = launchActivity(
activityOptions, // we likely want to support activity options.
factory = { MyCustomActivity(Deps1(), Deps2()) } // where factory is a `() -> Activity` or similar.
)
```
### Steps to Reproduce
1. Create an app using API 28 or above.
2. Create a custom `AppComponentFactory`.
2. Create an `Activity` using a custom constructor.
3. Try to write a test using the custom constructor providing custom dependencies in the test, similar to [Fragments: Provide Dependencies](https://developer.android.com/guide/fragments/test#provide-dependencies).
### Expected Results
Similar to `FragmentScenario`, `ActivityScenario` should support custom dependencies.
### Actual Results
There is no way to provide custom dependencies without modifying the whole `AppComponentFactory` of the tests or using workaround (i.e., leaving the `Activity` as an open class and provide a custom implementation in the test).
### AndroidX Test and Android OS Versions
Current version: [1.5.0](https://developer.android.com/jetpack/androidx/releases/test#core-1.5.0)
Contributor guide
Research direction
Start with ActivityScenario.java and compare its launch APIs with FragmentScenario.kt's launchInContainer factory support. Trace how ActivityScenario currently creates activities, then identify the API and test changes needed to support custom factories and activity options. Done means custom-constructor activities can be launched with injected dependencies in tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java, kotlin
- Domain
- mobile, testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100