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

前端 未结 8 1281
情书的邮戳
情书的邮戳 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:03

    I've created the src/debug/AndroidManifest.xml file as Matt suggested and added to following code to the testCase:

       @Override
        public void setUp() throws Exception {
            super.setUp();
            // Espresso will not launch our activity for us, we must launch it via getActivity().
            Activity activity = getActivity();
            KeyguardManager km = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
            KeyguardManager.KeyguardLock keyguardLock = km.newKeyguardLock("TAG");
            keyguardLock.disableKeyguard();
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    
        }
    
    0 讨论(0)
  • 2020-12-09 04:08

    For testing device set Lock pattern to NONE in Settings->Security Then use instance of UiDevice and call its wakeUp() method

    This method simulates pressing the power button if the screen is OFF else it does nothing if the screen is already ON. If the screen was OFF and it just got turned ON, this method will insert a 500ms delay to allow the device time to wake up and accept input.

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

    I actually figured out a really easy way to handle this. Remove the keyguard and wakelock permissions from the main manifest and put them in src/debug/AndroidManifest.xml like so:

    src/debug/AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" >
        <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
        <uses-permission android:name="android.permission.WAKE_LOCK"/>
    </manifest>
    

    When the app is built for debug the above permissions will merge into the main manifest. By default the build system uses the debug build for instrument tests so this works fine.

    Then in my onCreate I put the code mentioned in the question:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (BuildConfig.DEBUG) {
            KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
            KeyguardManager.KeyguardLock keyguardLock = km.newKeyguardLock("TAG");
            keyguardLock.disableKeyguard();
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        }
        ...
    }
    

    Now my phone can run tests without me waking them up by hand first and I didn't have to add the above permissions to the release version of my app.

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

    Another best way to wake up device before test. Simply add ActivityLifecycleCallback in your setUp method.

    public class Moduletest extends ActivityInstrumentationTestCase2<Your Activity>{
    
     protected void setUp(){
        super.setUp();
    
        ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(new ActivityLifecycleCallback() {
          @Override public void onActivityLifecycleChanged(Activity activity, Stage stage) {
            if (stage == Stage.PRE_ON_CREATE) {
              activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
          }
        });
      }
    }
    
    0 讨论(0)
  • 2020-12-09 04:11

    I have done it this way: First make two rules, one for the activity and one for UI Thread:

    @Rule
    public ActivityTestRule<Your Activity> mActivityRule =
            new ActivityTestRule<>(Your Activity.class, true, true);
    
    @Rule
    public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();
    

    And then in my first test method made this:

       @Test
       @LargeTest
       public void CreateAndSaveTaskEntity() throws Throwable {
    
                uiThreadTestRule.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Your Activity activity = mActivityRule.getActivity();
                activity.getWindow()
                        .addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    
                    }
                });
    
                //begin test cases
    
                ...
        }
    

    Of course you have to add in AndroidManifest.xml the permissions:

    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    
    0 讨论(0)
  • 2020-12-09 04:14

    Now that KeyguardLock is deprecated, you can simply use:

        if (BuildConfig.DEBUG) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        }
    
    0 讨论(0)
提交回复
热议问题