revoke permission in espresso test in @Test

Viewed 94

I'm Working with espresso test for my project. Inside one of my methods Allow some permissions for test and in next method I have to revoke The same permissions for The rest of the tests, but when test is run it arrive to this method for revoke permissions The test is done . I use revoke permissions in @BeforeClass and @AfterClass But there is no problem in these parts.

Thanks if anyone can help.

here is my code:

@Test
public void test4clickAllowLocation() {
    context = getInstrumentation().getContext();
    onView(withId(R.id.location_switch)).perform(scrollTo()).perform(click());
    onView(isRoot()).perform(waitFor(3000));
    onView(withId(R.id.second_Btn)).inRoot(isDialog()).perform(click());
    allowPermissionsJustOne("ALLOW", "Allow", "Allow only while using the app");
    PurpleLabeledSwitch purpleLabeledSwitch = new PurpleLabeledSwitch(context);
    purpleLabeledSwitch.findViewById(R.id.location_switch);
    purpleLabeledSwitch.setOn(true);
    assertTrue(purpleLabeledSwitch.isOn());
    onView(isRoot()).perform(waitFor(3000));
    onView(withId(R.id.location_image_view))
            .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
    onView(withId(R.id.location_image_view))
            .check(matches(withDrawable(R.drawable.ic_right_arrow_green)));
}


 @Test
    public void test5revokePermission()  {
        UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        mDevice.pressHome();
        revokePermission();
    }

public static void revokePermission() {
        InstrumentationRegistry.getInstrumentation().getUiAutomation().
                executeShellCommand("pm revoke " + getInstrumentation().getTargetContext()
                .getPackageName() + " android.permission.ACCESS_FINE_LOCATION");
        InstrumentationRegistry.getInstrumentation().getUiAutomation().
                executeShellCommand("pm revoke " + getInstrumentation().getTargetContext()
                       .getPackageName() + " android.permission.ACCESS_COARSE_LOCATION");
    }
0 Answers
Related