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; }
@Test public void processEmptyMaybe() { when(intentImportProcessor.process(intent)).thenReturn(Maybe.empty()); interactor.process(intent).test() .assertValue(UiIndicator.idle()) .assertComplete() .assertNoErrors(); verifyZeroInteractions(permissionRequester); }
@Test public void processContentScheme() { final IntentImportResult result = new IntentImportResult(Uri.parse("content: when(intentImportProcessor.process(intent)).thenReturn(Maybe.just(result)); final TestObserver<UiIndicator<IntentImportResult>> testObserver1 = interactor.process(intent).test(); testObserver1.awaitTerminalEvent(); testObserver1.assertValues(UiIndicator.idle(), UiIndicator.success(result)) .assertComplete() .assertNoErrors(); final TestObserver<UiIndicator<IntentImportResult>> testObserver2 = interactor.process(intent).test(); testObserver2.awaitTerminalEvent(); testObserver2.assertValue(UiIndicator.idle()) .assertComplete() .assertNoErrors(); verifyZeroInteractions(permissionRequester); }
@Test public void processFileSchemeWithPermissions() { 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(true)); final TestObserver<UiIndicator<IntentImportResult>> testObserver1 = interactor.process(intent).test(); testObserver1.awaitTerminalEvent(); testObserver1.assertValues(UiIndicator.idle(), UiIndicator.success(result)) .assertComplete() .assertNoErrors(); final TestObserver<UiIndicator<IntentImportResult>> testObserver2 = interactor.process(intent).test(); testObserver2.awaitTerminalEvent(); testObserver2.assertValue(UiIndicator.idle()) .assertComplete() .assertNoErrors(); verifyZeroInteractions(permissionRequester); }
@Test public void processFileSchemeWithoutPermissionsAndUserGrantsThem() { 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, true))); final TestObserver<UiIndicator<IntentImportResult>> testObserver1 = interactor.process(intent).test(); testObserver1.awaitTerminalEvent(); testObserver1.assertValues(UiIndicator.idle(), UiIndicator.success(result)) .assertComplete() .assertNoErrors(); final TestObserver<UiIndicator<IntentImportResult>> testObserver2 = interactor.process(intent).test(); testObserver2.awaitTerminalEvent(); testObserver2.assertValue(UiIndicator.idle()) .assertComplete() .assertNoErrors(); }
@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); }