@Test public void processFileSchemeWithoutPermissionsAndUserDoesNotGrantThem() { final IntentImportResult result = new IntentImportResult(Uri.parse("file: when(intentImportProcessor.process(intent)).thenReturn(Maybe.just(result)); when(permissionStatusChecker.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE)).thenReturn(Single.just(false)); when(permissionRequester.request(Manifest.permission.READ_EXTERNAL_STORAGE)).thenReturn(Single.just(new PermissionAuthorizationResponse(Manifest.permission.READ_EXTERNAL_STORAGE, false))); final TestObserver<UiIndicator<IntentImportResult>> testObserver1 = interactor.process(intent).test(); testObserver1.awaitTerminalEvent(); testObserver1.assertValues(UiIndicator.idle()) .assertNotComplete() .assertError(PermissionsNotGrantedException.class); final TestObserver<UiIndicator<IntentImportResult>> testObserver2 = interactor.process(intent).test(); testObserver2.awaitTerminalEvent(); testObserver2.assertValues(UiIndicator.idle()) .assertNotComplete() .assertError(PermissionsNotGrantedException.class); }
@NonNull public Observable<UiIndicator<IntentImportResult>> process(@NonNull final Intent intent) { if (intent.hasExtra(INTENT_INFORMATION_SHOW)) { return Observable.just(UiIndicator.idle()); } else { return intentImportProcessor.process(intent) .flatMap(intentImportResult -> { if (intentImportResult.getUri().toString().startsWith(ContentResolver.SCHEME_CONTENT)) { return Maybe.just(intentImportResult); } else { final IntentImportResult intentImportResultReference = intentImportResult; return permissionStatusChecker.isPermissionGranted(READ_PERMISSION) .subscribeOn(AndroidSchedulers.mainThread()) .flatMapMaybe(isGranted -> { if (isGranted) { return Maybe.just(intentImportResult); } else { return permissionRequester.request(READ_PERMISSION) .flatMapMaybe(permissionAuthorizationResponse -> { if (permissionAuthorizationResponse.wasGranted()) { return Maybe.just(intentImportResultReference); } else { permissionRequester.markRequestConsumed(READ_PERMISSION); return Maybe.error(new PermissionsNotGrantedException("User failed to grant READ permission", READ_PERMISSION)); } }); } }); } }) .doOnSuccess(ignored -> intent.putExtra(INTENT_INFORMATION_SHOW, true)) .toObservable() .map(UiIndicator::success) .startWith(UiIndicator.idle()); } }
IntentImportInformationInteractor { @NonNull public Observable<UiIndicator<IntentImportResult>> process(@NonNull final Intent intent) { if (intent.hasExtra(INTENT_INFORMATION_SHOW)) { return Observable.just(UiIndicator.idle()); } else { return intentImportProcessor.process(intent) .flatMap(intentImportResult -> { if (intentImportResult.getUri().toString().startsWith(ContentResolver.SCHEME_CONTENT)) { return Maybe.just(intentImportResult); } else { final IntentImportResult intentImportResultReference = intentImportResult; return permissionStatusChecker.isPermissionGranted(READ_PERMISSION) .subscribeOn(AndroidSchedulers.mainThread()) .flatMapMaybe(isGranted -> { if (isGranted) { return Maybe.just(intentImportResult); } else { return permissionRequester.request(READ_PERMISSION) .flatMapMaybe(permissionAuthorizationResponse -> { if (permissionAuthorizationResponse.wasGranted()) { return Maybe.just(intentImportResultReference); } else { permissionRequester.markRequestConsumed(READ_PERMISSION); return Maybe.error(new PermissionsNotGrantedException("User failed to grant READ permission", READ_PERMISSION)); } }); } }); } }) .doOnSuccess(ignored -> intent.putExtra(INTENT_INFORMATION_SHOW, true)) .toObservable() .map(UiIndicator::success) .startWith(UiIndicator.idle()); } } }
IntentImportInformationInteractor { @NonNull public Observable<UiIndicator<IntentImportResult>> process(@NonNull final Intent intent) { if (intent.hasExtra(INTENT_INFORMATION_SHOW)) { return Observable.just(UiIndicator.idle()); } else { return intentImportProcessor.process(intent) .flatMap(intentImportResult -> { if (intentImportResult.getUri().toString().startsWith(ContentResolver.SCHEME_CONTENT)) { return Maybe.just(intentImportResult); } else { final IntentImportResult intentImportResultReference = intentImportResult; return permissionStatusChecker.isPermissionGranted(READ_PERMISSION) .subscribeOn(AndroidSchedulers.mainThread()) .flatMapMaybe(isGranted -> { if (isGranted) { return Maybe.just(intentImportResult); } else { return permissionRequester.request(READ_PERMISSION) .flatMapMaybe(permissionAuthorizationResponse -> { if (permissionAuthorizationResponse.wasGranted()) { return Maybe.just(intentImportResultReference); } else { permissionRequester.markRequestConsumed(READ_PERMISSION); return Maybe.error(new PermissionsNotGrantedException("User failed to grant READ permission", READ_PERMISSION)); } }); } }); } }) .doOnSuccess(ignored -> intent.putExtra(INTENT_INFORMATION_SHOW, true)) .toObservable() .map(UiIndicator::success) .startWith(UiIndicator.idle()); } } @Inject  IntentImportInformationInteractor(@NonNull IntentImportProcessor intentImportProcessor,
                                             @NonNull PermissionStatusChecker permissionStatusChecker,
                                             @NonNull ActivityPermissionsRequester<SmartReceiptsActivity> permissionRequester); }
IntentImportInformationInteractor { @NonNull public Observable<UiIndicator<IntentImportResult>> process(@NonNull final Intent intent) { if (intent.hasExtra(INTENT_INFORMATION_SHOW)) { return Observable.just(UiIndicator.idle()); } else { return intentImportProcessor.process(intent) .flatMap(intentImportResult -> { if (intentImportResult.getUri().toString().startsWith(ContentResolver.SCHEME_CONTENT)) { return Maybe.just(intentImportResult); } else { final IntentImportResult intentImportResultReference = intentImportResult; return permissionStatusChecker.isPermissionGranted(READ_PERMISSION) .subscribeOn(AndroidSchedulers.mainThread()) .flatMapMaybe(isGranted -> { if (isGranted) { return Maybe.just(intentImportResult); } else { return permissionRequester.request(READ_PERMISSION) .flatMapMaybe(permissionAuthorizationResponse -> { if (permissionAuthorizationResponse.wasGranted()) { return Maybe.just(intentImportResultReference); } else { permissionRequester.markRequestConsumed(READ_PERMISSION); return Maybe.error(new PermissionsNotGrantedException("User failed to grant READ permission", READ_PERMISSION)); } }); } }); } }) .doOnSuccess(ignored -> intent.putExtra(INTENT_INFORMATION_SHOW, true)) .toObservable() .map(UiIndicator::success) .startWith(UiIndicator.idle()); } } @Inject  IntentImportInformationInteractor(@NonNull IntentImportProcessor intentImportProcessor,
                                             @NonNull PermissionStatusChecker permissionStatusChecker,
                                             @NonNull ActivityPermissionsRequester<SmartReceiptsActivity> permissionRequester); @NonNull Observable<UiIndicator<IntentImportResult>> process(@NonNull final Intent intent); }
IntentImportInformationInteractor { @NonNull public Observable<UiIndicator<IntentImportResult>> process(@NonNull final Intent intent) { if (intent.hasExtra(INTENT_INFORMATION_SHOW)) { return Observable.just(UiIndicator.idle()); } else { return intentImportProcessor.process(intent) .flatMap(intentImportResult -> { if (intentImportResult.getUri().toString().startsWith(ContentResolver.SCHEME_CONTENT)) { return Maybe.just(intentImportResult); } else { final IntentImportResult intentImportResultReference = intentImportResult; return permissionStatusChecker.isPermissionGranted(READ_PERMISSION) .subscribeOn(AndroidSchedulers.mainThread()) .flatMapMaybe(isGranted -> { if (isGranted) { return Maybe.just(intentImportResult); } else { return permissionRequester.request(READ_PERMISSION) .flatMapMaybe(permissionAuthorizationResponse -> { if (permissionAuthorizationResponse.wasGranted()) { return Maybe.just(intentImportResultReference); } else { permissionRequester.markRequestConsumed(READ_PERMISSION); return Maybe.error(new PermissionsNotGrantedException("User failed to grant READ permission", READ_PERMISSION)); } }); } }); } }) .doOnSuccess(ignored -> intent.putExtra(INTENT_INFORMATION_SHOW, true)) .toObservable() .map(UiIndicator::success) .startWith(UiIndicator.idle()); } } @Inject  IntentImportInformationInteractor(@NonNull IntentImportProcessor intentImportProcessor,
                                             @NonNull PermissionStatusChecker permissionStatusChecker,
                                             @NonNull ActivityPermissionsRequester<SmartReceiptsActivity> permissionRequester); @NonNull Observable<UiIndicator<IntentImportResult>> process(@NonNull final Intent intent); static final String READ_PERMISSION; }