Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Feature Request: allow custom `Factory` to be passed into `ActivityScenario` (similar to `FragmentScenario`)

未关闭
#1,738 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
领域
mobile, testing

调研方向

从 ActivityScenario.java 开始,将其启动 API 与 FragmentScenario.kt 对 launchInContainer 的 factory 支持进行比较。跟踪 ActivityScenario 当前如何创建 activity,然后确定支持自定义 factory 和 activity 选项所需的 API 和测试更改。当测试能够启动具有自定义构造函数并注入依赖项的 activity 时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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).

To address this issue, I propose that ActivityScenario should support a Factory parameter, similar to FragmentScenario (see here). This would enable developers to take full advantage of providing custom dependencies during tests:

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.
  3. Create an Activity using a custom constructor.
  4. Try to write a test using the custom constructor providing custom dependencies in the test, similar to Fragments: 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

主要语言
Java
星标
1.2k
派生
344
平均合并
3 天 2 小时
30 天内合并 PR
5

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

android/android-test 的其他 Issue

查看 android/android-test 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。