【问题标题】:How to test the Android Dark Mode using Espresso如何使用 Espresso 测试 Android 暗模式
【发布时间】:2020-05-26 08:40:48
【问题描述】:

使用 Espresso 在 Android 上测试不同 DayNight 主题的最佳方法是什么? (或更好的东西?)我在互联网上没有找到任何东西。我认为这一定是一件大事,因为现在一切都迁移到了 DayNight。

我想知道诸如“当我点击这个按钮时,我的活动主题是否改变了”或“我有这个背景和这个文本,对比度正确”。

提前致谢。

【问题讨论】:

  • 问题的一个方面是通过 Espresso 代码以编程方式确定操作系统设置中是否打开了暗模式。为此,您可以使用context?.resources?.configuration?.uiMode(来源:stackoverflow.com/questions/44170028/…)确定暗模式状态。

标签: android android-layout android-dark-theme android-darkmode


【解决方案1】:

我发现这是可行的:

    @get:Rule
    val activityTestRule = ActivityScenarioRule(...Activity::class.java)

    private fun createActivityScenarioRule(withNightMode: Boolean = false) =
        activityTestRule.scenario.apply {
            onActivity {
                AppCompatDelegate.setDefaultNightMode(
                    if (withNightMode) AppCompatDelegate.MODE_NIGHT_YES
                    else AppCompatDelegate.MODE_NIGHT_NO
                )
            }
        }

【讨论】:

  • 它似乎不适合我
  • 你的模拟器有暗模式吗?你在使用 AppCompat 吗?你调用函数了吗?
  • 是的,是的,是的
  • 你有一些示例代码吗?
猜你喜欢
  • 2021-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 2016-02-29
  • 1970-01-01
相关资源
最近更新 更多