简体   繁体   English

如何使用 Espresso 测试 Android 暗模式

[英]How to test the Android Dark Mode using Espresso

What is the best way to test the different DayNight themes on Android using Espresso?使用 Espresso 在 Android 上测试不同 DayNight 主题的最佳方法是什么? (or something better?) I haven't found anything on the internet. (或更好的东西?)我在互联网上没有找到任何东西。 I thought this must be something big because of everything migrating to DayNight now.我认为这一定是一件大事,因为现在一切都迁移到了 DayNight。

I want to know things like: "when I click this button, has my activity theme changed" or "I have this background and this text, is the contrast right".我想知道诸如:“当我单击此按钮时,我的活动主题是否已更改”或“我有此背景和此文本,对比度正确”。

Thanks in advance.提前致谢。

I have found this to work:我发现这个工作:

    @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
                )
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM