如何在 Android 测试中激活夜间模式

问题描述

我在本地测试(使用 robolectric)和仪器测试中都尝试了这个:

   val context: Context = ApplicationProvider.getApplicationContext()
   
   // Set to night mode
   AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
   InstrumentationRegistry.getInstrumentation().waitForIdleSync()

   // Now check if night mode is active
   val mode = context?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK)
   when (mode) {
        Configuration.UI_MODE_NIGHT_YES -> {
            //breakpoint
        }
        Configuration.UI_MODE_NIGHT_NO -> {
            //breakpoint 
        }
        Configuration.UI_MODE_NIGHT_UNDEFINED -> {
            //breakpoint
        }
    }

结果总是夜间模式处于非活动状态。 如何为我的测试激活夜间模式?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)