2

I'm writing a test using ActivityInstrumentationTestCase2.

When the emulator is open and the activity can get the foreground, my tests works perfectly.

When I start the tests wit the phone locked, the activity is not created. This seems to "make sense" since there is no reason to load an activity if it can not get the foreground (the phone is locked!). However, when running a test suite, this leads to false negatives...

Here is a sample test

public void testSplashscreenRedirectsSomewhereAfterTimeout() throws Exception {

    Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(SomeActivity.class.getName(), null, false);

    getActivity(); // to make sure that the activity gets launched
    Activity activity = monitor.waitForActivityWithTimeout(10000); // waiting for the other activity
    assertNotNull("BaselineActivity was not called", activity);
}

when the phone is locked, getActivity() returns the correct activity but its onCreate() method is never called.

Any way to get the tests to work even if the screen is locked?

pbreault
  • 14,176
  • 18
  • 45
  • 38

1 Answers1

0

Assuming you are correct about the activity simply not being created why not just unlock the phone with the KeyGuardManager. Unless there is a specific action that must be taken while the phone is locked and unlocked or, you are testing a service I would just run a "prep test environment" function that checks if the screen is locked and unlocks it and run your tests.

How to tell if your screen is unlocked. If not then unlock it and run your tests.

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();

Update

Use the following to dismiss a Key Guard Lock

//Get the current window 
Window window = getWindow();  
//Add the Flag from the window manager class
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Community
  • 1
  • 1
Terrance
  • 11,764
  • 4
  • 54
  • 80