/** * Create new element instance for given permissions. * * @param permissions List of requested permissions. * * @return New element instance for actions and assertions. * * @since Espresso Macchiato 0.2 */ public static EspPermissionDialog build(String... permissions) { return new EspPermissionDialog(permissions); }
/** * Perform click on deny permission button. * * This will fail if no permission dialog is shown except on pre Android M version.. * * > Warning: Revoking an already granted permission would force your app to restart (test fail). * You can workaround with @FixMethodOrder(MethodSorters.NAME_ASCENDING) * or use {@link EspPermissionsTool#resetAllPermission()} which does not restart your app. * * @since Espresso Macchiato 0.2 */ public void deny() { // permissions handling only available since android marshmallow if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return; } avoidAppCrashWhenDenyGrantedPermission(); //In Android N preview the Package changed to com.google.android.packageinstaller click("com.android.packageinstaller:id/permission_deny_button"); waitUntilPermissionIsChanged(); }
/** * Perform click on allow permission button. * * This will fail if no permission dialog is shown except on pre Android M version. * * @since Espresso Macchiato 0.2 */ public void allow() { // permissions handling only available since android marshmallow if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return; } //In Android N the Package is com.google.android.packageinstaller click("com.android.packageinstaller:id/permission_allow_button"); waitUntilPermissionIsChanged(); }
@Test public void testDenyWhenAllowedFailure() throws Throwable { // deny permission only available since android marshmallow skipTestIfBelowAndroidMarshmallow(); exception.expect(IllegalStateException.class); exception.expectMessage("Deny would revoke permission and restart app. This would let all following tests fail. See documentation for details."); whenRequestTestPermission(); espPermissionDialog.allow(); assertTestPermissionIsGranted(); espPermissionDialog.deny(); }
@Test public void testAllow() throws Throwable { whenRequestTestPermission(); espPermissionDialog.allow(); assertTestPermissionIsGranted(); }
@Test public void testDialogNeedsRequestedPermission() { exception.expect(IllegalStateException.class); exception.expectMessage("No expected permissions specified. This could lead to curious test failures."); EspPermissionDialog.build(); }
@Test public void testClickFailure() throws Throwable { // deny permission only available since android marshmallow skipTestIfBelowAndroidMarshmallow(); exception.expect(IllegalStateException.class); espPermissionDialog.deny(); }
@Test public void testDeny() throws Throwable { whenRequestTestPermission(); espPermissionDialog.deny(); // deny permissions only available since android marshmallow if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { assertTestPermissionIsDenied(); } }
@Test public void testScreenshotWithPermissionDialog() { skipTestIfBelowAndroidMarshmallow(); EspPermissionsTool.resetAllPermission(); EspPermissionsTool.requestPermissions(getActivity(), 42, Manifest.permission.READ_CONTACTS); // wait until all expected content is displayed // EspWait.forIdle() will not finish with permission dialog EspWait.forDelay(2000); EspScreenshotTool.takeWithName("test screenshot with permission dialog"); EspPermissionDialog.build(Manifest.permission.READ_CONTACTS).allow(); File screenshot = new File(InstrumentationRegistry.getTargetContext().getFilesDir(), EspScreenshotTool.screenshotFolderName + "/test screenshot with permission dialog.png"); assertThat(screenshot.exists(), is(true)); }