CurrencyExchangeRateEditorPresenter extends BasePresenter<CurrencyExchangeRateEditorView> { @Override public void subscribe() { skipCount = 0; final ConnectableObservable<String> selectedCurrencyConnectableObservable = Observable.fromCallable(this.databaseHelper::getCurrenciesList) .subscribeOn(ioScheduler) .observeOn(mainThreadScheduler) .flatMap(currenciesList -> { return currencyListEditorView.currencyClicks() .filter(currencyIndex -> currencyIndex >= 0) .map(currenciesList::get); }) .map(CharSequence::toString) .publish(); final ConnectableObservable<Pair<String, Date>> currencyDatePairConnectableObservable = Observable.combineLatest(selectedCurrencyConnectableObservable, receiptDateView.getReceiptDateChanges(), Pair::new) .publish(); this.compositeDisposable.add(currencyDatePairConnectableObservable .skipWhile(currencyDatePair -> { if (editableReceipt != null) { return editableReceipt.getPrice().getCurrencyCode().equals(currencyDatePair.first); } else { return savedInstanceState != null && skipCount++ > 0; } }) .switchMap(currencyDatePair -> { if (!trip.getDefaultCurrencyCode().equals(currencyDatePair.first)) { return exchangeRateServiceManager.getExchangeRate(currencyDatePair.second, currencyDatePair.first, trip.getDefaultCurrencyCode()); } else { return Observable.just(UiIndicator.<ExchangeRate>success()); } }) .observeOn(mainThreadScheduler) .subscribe(view.displayExchangeRate())); this.compositeDisposable.add(currencyDatePairConnectableObservable .switchMap(currencyDatePair -> view.getUserInitiatedExchangeRateRetries().map(userRetry -> currencyDatePair)) .doOnNext(currencyDatePair -> Logger.info(CurrencyExchangeRateEditorPresenter.this, "User clicked to initiate exchange rate retry")) .flatMap(currencyDatePair -> { return exchangeRateServiceManager.getExchangeRateOrInitiatePurchase(currencyDatePair.second, currencyDatePair.first, trip.getDefaultCurrencyCode()); }) .doOnError(exception -> Logger.error(CurrencyExchangeRateEditorPresenter.this, "Failed to initiate user retry", exception)) .observeOn(mainThreadScheduler) .subscribe(view.displayExchangeRate())); final ConnectableObservable<CharSequence> exchangeRateChangesConnectableObservable = view.getExchangeRateChanges() .publish(); final ConnectableObservable<CharSequence> exchangedPriceInBaseCurrencyChanges = view.getExchangedPriceInBaseCurrencyChanges() .publish(); final ConnectableObservable<Optional<BigDecimal>> exchangeRateChangesAsDecimalConnectableObservable = exchangeRateChangesConnectableObservable .compose(new PriceCharSequenceToBigDecimalObservableTransformer()) .publish(); final ConnectableObservable<Optional<BigDecimal>> receiptPriceChangesAsDecimalConnectableObservable = receiptPricingView.getReceiptPriceChanges() .compose(new PriceCharSequenceToBigDecimalObservableTransformer()) .publish(); final ConnectableObservable<Optional<BigDecimal>> exchangedPriceInBaseCurrencyAsDecimalChanges = exchangedPriceInBaseCurrencyChanges .compose(new PriceCharSequenceToBigDecimalObservableTransformer()) .publish(); this.compositeDisposable.add(Observable.combineLatest( receiptPriceChangesAsDecimalConnectableObservable, exchangeRateChangesAsDecimalConnectableObservable, selectedCurrencyConnectableObservable, (priceDecimal, exchangeRateDecimal, selectedReceiptCurrencyCode) -> { if (priceDecimal.isPresent() && exchangeRateDecimal.isPresent()) { final Price price = new PriceBuilderFactory() .setCurrency(trip.getDefaultCurrencyCode()) .setPrice(priceDecimal.get().multiply(exchangeRateDecimal.get())) .build(); return Optional.of(price); } else { return Optional.<Price>absent(); } } ) .filter(Optional::isPresent) .map(Optional::get) .doOnNext(exchangedPriceInBaseCurrency -> Logger.debug(CurrencyExchangeRateEditorPresenter.this, "Updating the exchanged price in base currency: {}", exchangedPriceInBaseCurrency)) .flatMapSingle(exchangedPriceInBaseCurrency -> view.getExchangedPriceInBaseCurrencyFocusChanges() .firstOrError() .map(isFocused -> { if (!isFocused) { return Optional.of(exchangedPriceInBaseCurrency); } else { return Optional.<Price>absent(); } }) .doOnSuccess(ignored -> Logger.debug(CurrencyExchangeRateEditorPresenter.this, "Allowing base rate changes as the field is not currently focuses")) ) .filter(Optional::isPresent) .doOnError(exception -> Logger.error(CurrencyExchangeRateEditorPresenter.this, "Failed to exchanged price in base currency", exception)) .subscribeOn(computationScheduler) .observeOn(mainThreadScheduler) .subscribe(view.displayExchangedPriceInBaseCurrency())); this.compositeDisposable.add(exchangedPriceInBaseCurrencyAsDecimalChanges .withLatestFrom(receiptPriceChangesAsDecimalConnectableObservable, selectedCurrencyConnectableObservable, (exchangedPriceInBaseCurrency, receiptPrice, selectedReceiptCurrencyCode) -> { if (exchangedPriceInBaseCurrency.isPresent() && receiptPrice.isPresent()) { if (receiptPrice.get().compareTo(BigDecimal.ZERO) == 0) { return Optional.<ExchangeRate>absent(); } else { final ExchangeRateBuilderFactory factory = new ExchangeRateBuilderFactory(); final BigDecimal rate = exchangedPriceInBaseCurrency.get().divide(receiptPrice.get(), ExchangeRate.PRECISION, BigDecimal.ROUND_HALF_UP); factory.setBaseCurrency(selectedReceiptCurrencyCode); factory.setRate(trip.getDefaultCurrencyCode(), rate); return Optional.of(factory.build()); } } else { return Optional.<ExchangeRate>absent(); } }) .doOnNext(exchangeRateOptional -> { if (!exchangeRateOptional.isPresent()) { Logger.debug(CurrencyExchangeRateEditorPresenter.this, "Ignoring divide by zero condition..."); } }) .filter(Optional::isPresent) .map(Optional::get) .doOnNext(exchangeRate -> Logger.debug(CurrencyExchangeRateEditorPresenter.this, "Updating exchange rate from user input: {}", exchangeRate)) .map(UiIndicator::success) .doOnError(exception -> Logger.error(CurrencyExchangeRateEditorPresenter.this, "Failed to exchanged rate update from base", exception)) .subscribeOn(computationScheduler) .observeOn(mainThreadScheduler) .subscribe(view.displayExchangeRate())); this.compositeDisposable.add(exchangeRateChangesConnectableObservable .filter(exchangeRateText -> exchangeRateText.length() == 0) .map(ignored -> Optional.<Price>absent()) .doOnNext(ignored -> Logger.debug(CurrencyExchangeRateEditorPresenter.this, "Clearing exchanged price now that exchange rate is empty")) .subscribe(view.displayExchangedPriceInBaseCurrency())); this.compositeDisposable.add(exchangedPriceInBaseCurrencyChanges .skip(1) .filter(exchangedPriceInBaseCurrencyText -> exchangedPriceInBaseCurrencyText.length() == 0) .map(ignored -> Optional.<Price>absent()) .doOnNext(ignored -> Logger.debug(CurrencyExchangeRateEditorPresenter.this, "Clearing exchange rate now that exchanged price is empty")) .map(exchangedPriceInBaseCurrencyText -> UiIndicator.<ExchangeRate>success()) .subscribe(view.displayExchangeRate())); compositeDisposable.add(selectedCurrencyConnectableObservable .map(selectedCurrencyCode -> !trip.getDefaultCurrencyCode().equals(selectedCurrencyCode)) .doOnNext(exchangeFieldsAreVisible -> Logger.debug(CurrencyExchangeRateEditorPresenter.this, "Exchange rate field visibility -> {}", exchangeFieldsAreVisible)) .subscribe(view.toggleExchangeRateFieldVisibility())); compositeDisposable.add(Observable.just(trip) .map(Trip::getDefaultCurrencyCode) .map(CurrencyUnit::of) .subscribe(view.displayBaseCurrency())); compositeDisposable.add(exchangeRateChangesConnectableObservable.connect()); compositeDisposable.add(exchangedPriceInBaseCurrencyChanges.connect()); compositeDisposable.add(currencyDatePairConnectableObservable.connect()); compositeDisposable.add(selectedCurrencyConnectableObservable.connect()); compositeDisposable.add(exchangeRateChangesAsDecimalConnectableObservable.connect()); compositeDisposable.add(receiptPriceChangesAsDecimalConnectableObservable.connect()); compositeDisposable.add(exchangedPriceInBaseCurrencyAsDecimalChanges.connect()); compositeDisposable.add(Observable.just(Optional.ofNullable(editableReceipt)) .filter(Optional::isPresent) .filter(ignored -> savedInstanceState == null) .map(Optional::get) .map(receipt -> receipt.getPrice().getExchangeRate()) .filter(exchangeRate -> exchangeRate.supportsExchangeRateFor(trip.getDefaultCurrencyCode())) .map(UiIndicator::success) .subscribe(view.displayExchangeRate())); compositeDisposable.add(Observable.just(view.getCurrencySelectionText()) .map(editorCurrency -> !trip.getDefaultCurrencyCode().equals(editorCurrency)) .subscribe(view.toggleExchangeRateFieldVisibility())); } CurrencyExchangeRateEditorPresenter(@NonNull CurrencyExchangeRateEditorView view,
                                               @NonNull EditableReceiptPricingView receiptPricingView,
                                               @NonNull CurrencyListEditorView currencyListEditorView,
                                               @NonNull ReceiptDateView receiptDateView,
                                               @NonNull ExchangeRateServiceManager exchangeRateServiceManager,
                                               @NonNull DatabaseHelper databaseHelper,
                                               @NonNull Trip trip,
                                               @Nullable Receipt editableReceipt,
                                               @Nullable Bundle savedInstanceState);  CurrencyExchangeRateEditorPresenter(@NonNull CurrencyExchangeRateEditorView view,
                                               @NonNull EditableReceiptPricingView receiptPricingView,
                                               @NonNull CurrencyListEditorView currencyListEditorView,
                                               @NonNull ReceiptDateView receiptDateView,
                                               @NonNull ExchangeRateServiceManager exchangeRateServiceManager,
                                               @NonNull DatabaseHelper databaseHelper,
                                               @NonNull Trip trip,
                                               @Nullable Receipt editableReceipt,
                                               @Nullable Bundle savedInstanceState,
                                               @NonNull Scheduler ioScheduler,
                                               @NonNull Scheduler computationScheduler,
                                               @NonNull Scheduler mainThreadScheduler); @Override void subscribe();  }
@Test public void subscribeDisplaysBaseCurrency() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); verify(displayBaseCurrencyConsumer).accept(CurrencyUnit.of(TRIP_CURRENCY)); }
@Test public void subscribeDoesNotDisplayExchangeRateForNullReceipt() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); verify(displayExchangeRateConsumer, never()).accept(UiIndicator.success(EXCHANGE_RATE)); }
@Test public void subscribeDoesNotDisplayExchangeRateNonNullReceiptWithSavedState() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, editableReceipt, savedInstanceState, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); verify(displayExchangeRateConsumer, never()).accept(UiIndicator.success(EXCHANGE_RATE)); }
@Test public void subscribeDisplayExchangeRateNonNullReceiptWithNullSavedState() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, editableReceipt, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); verify(displayExchangeRateConsumer).accept(UiIndicator.success(EXCHANGE_RATE)); }
@Test public void subscribeAlwaysTogglesExchangeRateVisibility() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, editableReceipt, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); verify(toggleExchangeRateFieldVisibilityConsumer).accept(false); }
@Test public void subscribeTogglesExchangeRateVisibilityAndListensToChangeEvents() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, editableReceipt, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); final InOrder inOrderVerifier = inOrder(toggleExchangeRateFieldVisibilityConsumer); inOrderVerifier.verify(toggleExchangeRateFieldVisibilityConsumer).accept(false); currencyClicks.onNext(0); inOrderVerifier.verify(toggleExchangeRateFieldVisibilityConsumer).accept(false); currencyClicks.onNext(1); inOrderVerifier.verify(toggleExchangeRateFieldVisibilityConsumer).accept(true); }
@Test public void changingCurrencyToTripCurrencyClearsTheExchangeRate() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); currencyClicks.onNext(0); receiptDateChanges.onNext(new Date(100L)); verify(displayExchangeRateConsumer).accept(UiIndicator.success()); verify(exchangeRateServiceManager, never()).getExchangeRate(any(Date.class), anyString(), anyString()); }
@Test public void changingCurrencyGetsExchangeRate() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); currencyClicks.onNext(1); receiptDateChanges.onNext(new Date(100L)); verify(displayExchangeRateConsumer).accept(UiIndicator.success(EXCHANGE_RATE)); verify(exchangeRateServiceManager).getExchangeRate(any(Date.class), anyString(), anyString()); }
@Test public void changingDateGetsExchangeRate() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); currencyClicks.onNext(1); receiptDateChanges.onNext(new Date(100L)); receiptDateChanges.onNext(new Date(200L)); verify(displayExchangeRateConsumer, times(2)).accept(UiIndicator.success(EXCHANGE_RATE)); verify(exchangeRateServiceManager, times(2)).getExchangeRate(any(Date.class), anyString(), anyString()); }
@Test public void changingDateFirstOnlyProducesOneExchangeRateChange() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); currencyClicks.onNext(0); receiptDateChanges.onNext(new Date(100L)); receiptDateChanges.onNext(new Date(200L)); currencyClicks.onNext(1); verify(displayExchangeRateConsumer).accept(UiIndicator.success(EXCHANGE_RATE)); verify(exchangeRateServiceManager).getExchangeRate(any(Date.class), anyString(), anyString()); }
@Test public void onlyOneExchangeRateIsFetchedPerDay() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); currencyClicks.onNext(0); receiptDateChanges.onNext(new Date(100L)); receiptDateChanges.onNext(new Date(200L)); currencyClicks.onNext(1); currencyClicks.onNext(2); verify(displayExchangeRateConsumer, times(2)).accept(UiIndicator.success(EXCHANGE_RATE)); verify(exchangeRateServiceManager, times(2)).getExchangeRate(any(Date.class), anyString(), anyString()); }
@Test public void userMultipleRetriesGetsExchangeRateOrPurchase() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); currencyClicks.onNext(1); receiptDateChanges.onNext(new Date(200L)); currencyClicks.onNext(2); userInitiatedExchangeRateRetries.onNext(new Object()); userInitiatedExchangeRateRetries.onNext(new Object()); userInitiatedExchangeRateRetries.onNext(new Object()); verify(displayExchangeRateConsumer, atLeastOnce()).accept(UiIndicator.success(EXCHANGE_RATE)); verify(exchangeRateServiceManager, times(3)).getExchangeRateOrInitiatePurchase(any(Date.class), anyString(), anyString()); }
@Test public void userRetryGetsExchangeRateOrPurchase() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); currencyClicks.onNext(1); receiptDateChanges.onNext(new Date(200L)); currencyClicks.onNext(2); userInitiatedExchangeRateRetries.onNext(new Object()); currencyClicks.onNext(1); receiptDateChanges.onNext(new Date(100L)); verify(displayExchangeRateConsumer, atLeastOnce()).accept(UiIndicator.success(EXCHANGE_RATE)); verify(exchangeRateServiceManager).getExchangeRateOrInitiatePurchase(any(Date.class), anyString(), anyString()); }
@Test public void userEditsPriceThenExchangeRateFields() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); exchangedPriceInBaseCurrencyFocusChanges.onNext(false); currencyClicks.onNext(1); receiptPriceChanges.onNext("1"); receiptPriceChanges.onNext("10"); receiptPriceChanges.onNext("10."); receiptPriceChanges.onNext("10.0"); receiptPriceChanges.onNext("10.00"); final InOrder inOrderVerifier = inOrder(displayExchangedPriceInBaseCurrencyConsumer); exchangeRateChanges.onNext(""); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.absent()); exchangeRateChanges.onNext("0"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext("0."); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext("0.1"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("1").build())); exchangeRateChanges.onNext("0."); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext("0.2"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); exchangeRateChanges.onNext("0."); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext("0"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext(""); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.absent()); }
@Test public void userEditsPriceThenExchangeRateFieldsWithCommaForDecimal() throws Exception { TestLocaleToggler.setDefaultLocale(Locale.FRANCE); presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); exchangedPriceInBaseCurrencyFocusChanges.onNext(false); currencyClicks.onNext(1); receiptPriceChanges.onNext("1"); receiptPriceChanges.onNext("10"); receiptPriceChanges.onNext("10,"); receiptPriceChanges.onNext("10,0"); receiptPriceChanges.onNext("10,00"); final InOrder inOrderVerifier = inOrder(displayExchangedPriceInBaseCurrencyConsumer); exchangeRateChanges.onNext(""); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.absent()); exchangeRateChanges.onNext("0"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext("0,"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext("0,1"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("1").build())); exchangeRateChanges.onNext("0,"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext("0,2"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); exchangeRateChanges.onNext("0,"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext("0"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0").build())); exchangeRateChanges.onNext(""); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.absent()); }
@Test public void userEditsExchangeRateThenPriceFields() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); exchangedPriceInBaseCurrencyFocusChanges.onNext(false); currencyClicks.onNext(1); exchangeRateChanges.onNext(""); exchangeRateChanges.onNext("0"); exchangeRateChanges.onNext("0.1"); exchangeRateChanges.onNext("0."); exchangeRateChanges.onNext("0.2"); final InOrder inOrderVerifier = inOrder(displayExchangedPriceInBaseCurrencyConsumer); receiptPriceChanges.onNext(""); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.absent()); receiptPriceChanges.onNext("1"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0.2").build())); receiptPriceChanges.onNext("10"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); receiptPriceChanges.onNext("10."); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); receiptPriceChanges.onNext("10.0"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); receiptPriceChanges.onNext("10.00"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); receiptPriceChanges.onNext("10.0"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); receiptPriceChanges.onNext("10."); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); receiptPriceChanges.onNext("10"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("2").build())); receiptPriceChanges.onNext("1"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.of(new PriceBuilderFactory().setCurrency(TRIP_CURRENCY).setPrice("0.2").build())); }
@Test public void userEditsExchangeRateThenPriceFieldThatCausesDivisionByZero() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); exchangedPriceInBaseCurrencyFocusChanges.onNext(false); currencyClicks.onNext(1); exchangeRateChanges.onNext(""); exchangeRateChanges.onNext("0"); exchangeRateChanges.onNext("0.1"); exchangeRateChanges.onNext("0."); exchangeRateChanges.onNext("0.2"); final InOrder inOrderVerifier = inOrder(displayExchangedPriceInBaseCurrencyConsumer); receiptPriceChanges.onNext(""); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer).accept(Optional.absent()); receiptPriceChanges.onNext("0"); receiptPriceChanges.onNext("0.0"); receiptPriceChanges.onNext("0.00"); receiptPriceChanges.onNext("0.000"); inOrderVerifier.verify(displayExchangedPriceInBaseCurrencyConsumer, never()).accept(Optional.absent()); }
@Test public void userEditsPriceThenExchangedTotalInBaseCurrency() throws Exception { presenter = new CurrencyExchangeRateEditorPresenter(currencyExchangeRateEditorView, receiptPricingView, currencyListEditorView, receiptDateView, exchangeRateServiceManager, databaseHelper, trip, null, null, Schedulers.trampoline(), Schedulers.trampoline(), Schedulers.trampoline()); presenter.subscribe(); exchangedPriceInBaseCurrencyFocusChanges.onNext(true); currencyClicks.onNext(1); receiptPriceChanges.onNext("1"); receiptPriceChanges.onNext("10"); receiptPriceChanges.onNext("10."); receiptPriceChanges.onNext("10.0"); receiptPriceChanges.onNext("10.00"); final InOrder inOrderVerifier = inOrder(displayExchangeRateConsumer); exchangedPriceInBaseCurrencyChanges.onNext(""); exchangedPriceInBaseCurrencyChanges.onNext("2"); inOrderVerifier.verify(displayExchangeRateConsumer).accept(UiIndicator.success(new ExchangeRateBuilderFactory().setRate(TRIP_CURRENCY, new BigDecimal("0.200000")).setBaseCurrency(RECEIPT_CURRENCY).build())); exchangedPriceInBaseCurrencyChanges.onNext("2."); inOrderVerifier.verify(displayExchangeRateConsumer).accept(UiIndicator.success(new ExchangeRateBuilderFactory().setRate(TRIP_CURRENCY, new BigDecimal("0.200000")).setBaseCurrency(RECEIPT_CURRENCY).build())); exchangedPriceInBaseCurrencyChanges.onNext("2.0"); inOrderVerifier.verify(displayExchangeRateConsumer).accept(UiIndicator.success(new ExchangeRateBuilderFactory().setRate(TRIP_CURRENCY, new BigDecimal("0.200000")).setBaseCurrency(RECEIPT_CURRENCY).build())); exchangedPriceInBaseCurrencyChanges.onNext("2.00"); inOrderVerifier.verify(displayExchangeRateConsumer).accept(UiIndicator.success(new ExchangeRateBuilderFactory().setRate(TRIP_CURRENCY, new BigDecimal("0.200000")).setBaseCurrency(RECEIPT_CURRENCY).build())); exchangedPriceInBaseCurrencyChanges.onNext("2.0"); inOrderVerifier.verify(displayExchangeRateConsumer).accept(UiIndicator.success(new ExchangeRateBuilderFactory().setRate(TRIP_CURRENCY, new BigDecimal("0.200000")).setBaseCurrency(RECEIPT_CURRENCY).build())); exchangedPriceInBaseCurrencyChanges.onNext("2."); inOrderVerifier.verify(displayExchangeRateConsumer).accept(UiIndicator.success(new ExchangeRateBuilderFactory().setRate(TRIP_CURRENCY, new BigDecimal("0.200000")).setBaseCurrency(RECEIPT_CURRENCY).build())); exchangedPriceInBaseCurrencyChanges.onNext("2"); inOrderVerifier.verify(displayExchangeRateConsumer).accept(UiIndicator.success(new ExchangeRateBuilderFactory().setRate(TRIP_CURRENCY, new BigDecimal("0.200000")).setBaseCurrency(RECEIPT_CURRENCY).build())); exchangedPriceInBaseCurrencyChanges.onNext(""); inOrderVerifier.verify(displayExchangeRateConsumer).accept(UiIndicator.success()); }