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
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.
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.