Andre Thiele
Andre Thiele

Reputation: 4191

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.

Upvotes: 5

Views: 2028

Answers (1)

Quintin B
Quintin B

Reputation: 5881

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

Upvotes: 4

Related Questions