Android Espresso, Wake up device before test. How to use a custom manifest for test?

前端 未结 8 1282
情书的邮戳
情书的邮戳 2020-12-09 03:52

I\'ve been writing tests with androids new espresso framework and find that it works well. One annoying thing (not particular to espresso) is that I have to make sure my scr

相关标签:
8条回答
  • 2020-12-09 04:15

    Although you already have accepted Matt Wolve answer, I think that polluting your code with test boilerplate is not a good idea (I am aware that using Espresso there are some situations where you have to, like adding idle flags for custom IdlingResources). I would like to add another approach:

        @ClassRule
        public static ActivityTestRule<HomeActivity> mActivityRuleSetUp = new ActivityTestRule<>(
                HomeActivity.class);
    
    
        private static void wakeUpDevice(){
            if (BuildConfig.DEBUG){
                HomeActivity homeActivity = mActivityRuleSetUp.getActivity();
    
                KeyguardManager myKM = (KeyguardManager) homeActivity.getSystemService(HomeActivity.KEYGUARD_SERVICE);
                boolean isPhoneLocked = myKM.inKeyguardRestrictedInputMode();
    
                if (isPhoneLocked){
                    homeActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
                }
            }
         }
    
    
        @BeforeClass
        public static void setUp(){
            wakeUpDevice();
        }
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-09 04:22

    The easiest approach is to use adb command like below if you are running the tests from CI environment for example:

    adb -s $DEVICE_ID shell input keyevent 82
    

    This will unlock your device screen.

    0 讨论(0)
提交回复
热议问题