简体   繁体   中英

How to test the Android Dark Mode using Espresso

What is the best way to test the different DayNight themes on Android using Espresso? (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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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