From d44823afd660aafbb0f4ddffebc6cbb5b407ff77 Mon Sep 17 00:00:00 2001 From: Christoph Atteneder Date: Wed, 23 Jan 2019 13:16:19 +0100 Subject: [PATCH 1/3] Bind currency combobox to trade currency from model Remove data logic from view model that is set in data model --- .../java/bisq/desktop/main/offer/MutableOfferView.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferView.java b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferView.java index 72bf98656ce..3ace8007f12 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferView.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferView.java @@ -239,6 +239,7 @@ protected void doActivate() { paymentAccountsComboBox.setItems(model.getDataModel().getPaymentAccounts()); paymentAccountsComboBox.getSelectionModel().select(model.getPaymentAccount()); + currencyComboBox.getSelectionModel().select(model.getTradeCurrency()); onPaymentAccountsComboBoxSelected(); @@ -515,13 +516,6 @@ protected void onPaymentAccountsComboBoxSelected() { if (paymentAccount.hasMultipleCurrencies()) { final List tradeCurrencies = paymentAccount.getTradeCurrencies(); currencyComboBox.setItems(FXCollections.observableArrayList(tradeCurrencies)); - if (paymentAccount.getSelectedTradeCurrency() != null) - currencyComboBox.getSelectionModel().select(paymentAccount.getSelectedTradeCurrency()); - else if (tradeCurrencies.contains(model.getTradeCurrency())) - currencyComboBox.getSelectionModel().select(model.getTradeCurrency()); - else - currencyComboBox.getSelectionModel().select(tradeCurrencies.get(0)); - model.onPaymentAccountSelected(paymentAccount); } else { TradeCurrency singleTradeCurrency = paymentAccount.getSingleTradeCurrency(); From 75655c689d78f3b14dc49087eab1390313b6727f Mon Sep 17 00:00:00 2001 From: Christoph Atteneder Date: Wed, 23 Jan 2019 13:16:34 +0100 Subject: [PATCH 2/3] Exclude localized name to be used in equals as it fails for users using different locales than en_US --- core/src/main/java/bisq/core/locale/TradeCurrency.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/bisq/core/locale/TradeCurrency.java b/core/src/main/java/bisq/core/locale/TradeCurrency.java index b349785bbb0..8ff4c79e294 100644 --- a/core/src/main/java/bisq/core/locale/TradeCurrency.java +++ b/core/src/main/java/bisq/core/locale/TradeCurrency.java @@ -35,6 +35,7 @@ @Slf4j public abstract class TradeCurrency implements PersistablePayload, Comparable { protected final String code; + @EqualsAndHashCode.Exclude protected final String name; public TradeCurrency(String code, String name) { From 0a7c85f7d81c894b236d24ba3dd6cdd6cbe7eeb8 Mon Sep 17 00:00:00 2001 From: Christoph Atteneder Date: Wed, 23 Jan 2019 13:16:55 +0100 Subject: [PATCH 3/3] Use selected trade currency if available in payment account --- .../main/offer/MutableOfferDataModel.java | 16 +-- .../createoffer/CreateOfferDataModelTest.java | 118 ++++++++++++++++++ 2 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModelTest.java diff --git a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java index e651f53f845..6f0a30050c6 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java @@ -475,12 +475,14 @@ void onPaymentAccountSelected(PaymentAccount paymentAccount) { } private void setTradeCurrencyFromPaymentAccount(PaymentAccount paymentAccount) { - if (paymentAccount.getSelectedTradeCurrency() != null) - tradeCurrency = paymentAccount.getSelectedTradeCurrency(); - else if (paymentAccount.getSingleTradeCurrency() != null) - tradeCurrency = paymentAccount.getSingleTradeCurrency(); - else if (!paymentAccount.getTradeCurrencies().isEmpty()) - tradeCurrency = paymentAccount.getTradeCurrencies().get(0); + if (!paymentAccount.getTradeCurrencies().contains(tradeCurrency)) { + if (paymentAccount.getSelectedTradeCurrency() != null) + tradeCurrency = paymentAccount.getSelectedTradeCurrency(); + else if (paymentAccount.getSingleTradeCurrency() != null) + tradeCurrency = paymentAccount.getSingleTradeCurrency(); + else if (!paymentAccount.getTradeCurrencies().isEmpty()) + tradeCurrency = paymentAccount.getTradeCurrencies().get(0); + } checkNotNull(tradeCurrency, "tradeCurrency must not be null"); tradeCurrencyCode.set(tradeCurrency.getCode()); @@ -744,7 +746,7 @@ protected void setMinAmount(Coin minAmount) { this.minAmount.set(minAmount); } - ReadOnlyStringProperty getTradeCurrencyCode() { + public ReadOnlyStringProperty getTradeCurrencyCode() { return tradeCurrencyCode; } diff --git a/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModelTest.java b/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModelTest.java new file mode 100644 index 00000000000..6e1815d22ac --- /dev/null +++ b/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModelTest.java @@ -0,0 +1,118 @@ +package bisq.desktop.main.offer.createoffer; + +import bisq.core.btc.model.AddressEntry; +import bisq.core.btc.wallet.BtcWalletService; +import bisq.core.locale.CryptoCurrency; +import bisq.core.locale.FiatCurrency; +import bisq.core.locale.GlobalSettings; +import bisq.core.locale.Res; +import bisq.core.offer.OfferPayload; +import bisq.core.offer.OfferUtil; +import bisq.core.payment.ClearXchangeAccount; +import bisq.core.payment.PaymentAccount; +import bisq.core.payment.RevolutAccount; +import bisq.core.provider.fee.FeeService; +import bisq.core.provider.price.PriceFeedService; +import bisq.core.user.Preferences; +import bisq.core.user.User; + +import org.bitcoinj.core.Coin; + +import java.util.HashSet; + +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + + +import org.mockito.BDDMockito; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({BtcWalletService.class, AddressEntry.class, Preferences.class, User.class, + PriceFeedService.class, OfferUtil.class}) +@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"}) +public class CreateOfferDataModelTest { + + private CreateOfferDataModel model; + private User user; + private Preferences preferences; + + @Before + public void setUp() { + final CryptoCurrency btc = new CryptoCurrency("BTC", "bitcoin"); + GlobalSettings.setDefaultTradeCurrency(btc); + Res.setup(); + + AddressEntry addressEntry = mock(AddressEntry.class); + BtcWalletService btcWalletService = mock(BtcWalletService.class); + PriceFeedService priceFeedService = mock(PriceFeedService.class); + FeeService feeService = mock(FeeService.class); + preferences = mock(Preferences.class); + user = mock(User.class); + + when(btcWalletService.getOrCreateAddressEntry(anyString(), any())).thenReturn(addressEntry); + when(preferences.isUsePercentageBasedPrice()).thenReturn(true); + when(preferences.getBuyerSecurityDepositAsCoin()).thenReturn(Coin.FIFTY_COINS); + + model = new CreateOfferDataModel(null, btcWalletService, + null, preferences, user, null, + null, priceFeedService, null, + null, null, feeService, + null, null); + } + + @Test + public void testUseTradeCurrencySetInOfferViewWhenInPaymentAccountAvailable() { + final HashSet paymentAccounts = new HashSet<>(); + final ClearXchangeAccount zelleAccount = new ClearXchangeAccount(); + zelleAccount.setId("234"); + paymentAccounts.add(zelleAccount); + final RevolutAccount revolutAccount = new RevolutAccount(); + revolutAccount.setId("123"); + revolutAccount.setSingleTradeCurrency(new FiatCurrency("EUR")); + revolutAccount.addCurrency(new FiatCurrency("USD")); + paymentAccounts.add(revolutAccount); + + when(user.getPaymentAccounts()).thenReturn(paymentAccounts); + when(preferences.getSelectedPaymentAccountForCreateOffer()).thenReturn(revolutAccount); + PowerMockito.mockStatic(OfferUtil.class); + BDDMockito.given(OfferUtil.getMakerFee(any(), any(), any())).willReturn(Coin.ZERO); + + model.initWithData(OfferPayload.Direction.BUY, new FiatCurrency("USD")); + assertEquals("USD", model.getTradeCurrencyCode().get()); + } + + @Test + public void testUseTradeAccountThatMatchesTradeCurrencySetInOffer() { + final HashSet paymentAccounts = new HashSet<>(); + final ClearXchangeAccount zelleAccount = new ClearXchangeAccount(); + zelleAccount.setId("234"); + paymentAccounts.add(zelleAccount); + final RevolutAccount revolutAccount = new RevolutAccount(); + revolutAccount.setId("123"); + revolutAccount.setSingleTradeCurrency(new FiatCurrency("EUR")); + paymentAccounts.add(revolutAccount); + + when(user.getPaymentAccounts()).thenReturn(paymentAccounts); + when(user.findFirstPaymentAccountWithCurrency(new FiatCurrency("USD"))).thenReturn(zelleAccount); + when(preferences.getSelectedPaymentAccountForCreateOffer()).thenReturn(revolutAccount); + PowerMockito.mockStatic(OfferUtil.class); + BDDMockito.given(OfferUtil.getMakerFee(any(), any(), any())).willReturn(Coin.ZERO); + + model.initWithData(OfferPayload.Direction.BUY, new FiatCurrency("USD")); + assertEquals("USD", model.getTradeCurrencyCode().get()); + } + +}