forked from bisq-network/bisq
/
GUIUtilTest.java
188 lines (152 loc) · 7.55 KB
/
GUIUtilTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop.util;
import bisq.desktop.util.validation.RegexValidator;
import bisq.core.locale.GlobalSettings;
import bisq.core.locale.Res;
import bisq.core.locale.TradeCurrency;
import bisq.core.monetary.Price;
import bisq.core.provider.price.MarketPrice;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.user.DontShowAgainLookup;
import bisq.core.user.Preferences;
import bisq.core.util.coin.BsqFormatter;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.CoinMaker;
import javafx.util.StringConverter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static bisq.desktop.maker.TradeCurrencyMakers.bitcoin;
import static bisq.desktop.maker.TradeCurrencyMakers.euro;
import static com.natpryce.makeiteasy.MakeItEasy.a;
import static com.natpryce.makeiteasy.MakeItEasy.make;
import static com.natpryce.makeiteasy.MakeItEasy.with;
import static org.bitcoinj.core.CoinMaker.oneBitcoin;
import static org.bitcoinj.core.CoinMaker.satoshis;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Ignore
public class GUIUtilTest {
@Before
public void setup() {
Locale.setDefault(new Locale("en", "US"));
GlobalSettings.setLocale(new Locale("en", "US"));
Res.setBaseCurrencyCode("BTC");
Res.setBaseCurrencyName("Bitcoin");
}
@Test
public void testTradeCurrencyConverter() {
Map<String, Integer> offerCounts = new HashMap<>() {{
put("BTC", 11);
put("EUR", 10);
}};
StringConverter<TradeCurrency> tradeCurrencyConverter = GUIUtil.getTradeCurrencyConverter(
Res.get("shared.oneOffer"),
Res.get("shared.multipleOffers"),
offerCounts
);
assertEquals("✦ Bitcoin (BTC) - 11 offers", tradeCurrencyConverter.toString(bitcoin));
assertEquals("★ Euro (EUR) - 10 offers", tradeCurrencyConverter.toString(euro));
}
@Test
public void testOpenURLWithCampaignParameters() {
Preferences preferences = mock(Preferences.class);
DontShowAgainLookup.setPreferences(preferences);
GUIUtil.setPreferences(preferences);
when(preferences.showAgain("warnOpenURLWhenTorEnabled")).thenReturn(false);
when(preferences.getUserLanguage()).thenReturn("en");
/* PowerMockito.mockStatic(Utilities.class);
ArgumentCaptor<URI> captor = ArgumentCaptor.forClass(URI.class);
PowerMockito.doNothing().when(Utilities.class, "openURI", captor.capture());
GUIUtil.openWebPage("https://bisq.network");
assertEquals("https://bisq.network?utm_source=desktop-client&utm_medium=in-app-link&utm_campaign=language_en", captor.getValue().toString());
GUIUtil.openWebPage("https://docs.bisq.network/trading-rules.html#f2f-trading");
assertEquals("https://docs.bisq.network/trading-rules.html?utm_source=desktop-client&utm_medium=in-app-link&utm_campaign=language_en#f2f-trading", captor.getValue().toString());
*/
}
@Test
public void testOpenURLWithoutCampaignParameters() {
Preferences preferences = mock(Preferences.class);
DontShowAgainLookup.setPreferences(preferences);
GUIUtil.setPreferences(preferences);
when(preferences.showAgain("warnOpenURLWhenTorEnabled")).thenReturn(false);
/*
PowerMockito.mockStatic(Utilities.class);
ArgumentCaptor<URI> captor = ArgumentCaptor.forClass(URI.class);
PowerMockito.doNothing().when(Utilities.class, "openURI", captor.capture());
GUIUtil.openWebPage("https://www.github.com");
assertEquals("https://www.github.com", captor.getValue().toString());
*/
}
@Test
public void testAddressRegexValidator() {
RegexValidator regexValidator = GUIUtil.addressRegexValidator();
assertTrue(regexValidator.validate("").isValid);
assertTrue(regexValidator.validate("abcdefghij234567.onion").isValid);
assertTrue(regexValidator.validate("abcdefghijklmnop.onion,abcdefghijklmnop.onion").isValid);
assertTrue(regexValidator.validate("qrstuvwxyzABCDEF.onion,qrstuvwxyzABCDEF.onion,aaaaaaaaaaaaaaaa.onion").isValid);
assertTrue(regexValidator.validate("GHIJKLMNOPQRSTUV.onion:9999").isValid);
assertTrue(regexValidator.validate("WXYZ234567abcdef.onion,GHIJKLMNOPQRSTUV.onion:9999").isValid);
assertTrue(regexValidator.validate("aaaaaaaaaaaaaaaa.onion:9999,WXYZ234567abcdef.onion:9999,2222222222222222.onion:9999").isValid);
assertTrue(regexValidator.validate("12.34.56.78").isValid);
assertTrue(regexValidator.validate("12.34.56.78:8888").isValid);
assertFalse(regexValidator.validate(" ").isValid);
assertFalse(regexValidator.validate("abcd.onion").isValid);
assertFalse(regexValidator.validate("abcdefghijklmnop,abcdefghijklmnop.onion").isValid);
assertFalse(regexValidator.validate("abcdefghi2345689.onion:9999").isValid);
assertFalse(regexValidator.validate("onion:9999,abcdefghijklmnop.onion:9999").isValid);
assertFalse(regexValidator.validate("abcdefghijklmnop.onion:").isValid);
assertFalse(regexValidator.validate("32zzibxmqi2ybxpqyggwwuwz7a3lbvtzoloti7cxoevyvijexvgsfeid.onion:8333").isValid);
assertFalse(regexValidator.validate("12.34.56.788").isValid);
assertFalse(regexValidator.validate("12.34.56.78:").isValid);
}
@Test
public void testGetBsqInUsd() {
PriceFeedService priceFeedService = mock(PriceFeedService.class);
when(priceFeedService.getMarketPrice("USD"))
.thenReturn(new MarketPrice("USD", 12345.6789, 0, true));
Coin oneBsq = Coin.valueOf(100);
Price avgPrice = Price.valueOf("BSQ", 10000);
assertEquals("1.23 USD", GUIUtil.getBsqInUsd(avgPrice, oneBsq, priceFeedService, new BsqFormatter()));
}
@Test
public void percentageOfTradeAmount_higherFeeAsMin() {
Coin fee = make(a(CoinMaker.Coin).but(with(satoshis, 20000L)));
Coin min = make(a(CoinMaker.Coin).but(with(satoshis, 10000L)));
assertEquals(" (0.02% of trade amount)", GUIUtil.getPercentageOfTradeAmount(fee, oneBitcoin, min));
}
@Test
public void percentageOfTradeAmount_minFee() {
Coin fee = make(a(CoinMaker.Coin).but(with(satoshis, 10000L)));
Coin min = make(a(CoinMaker.Coin).but(with(satoshis, 10000L)));
assertEquals(" (required minimum)",
GUIUtil.getPercentageOfTradeAmount(fee, oneBitcoin, min));
}
@Test
public void percentageOfTradeAmount_minFeeZERO() {
Coin fee = make(a(CoinMaker.Coin).but(with(satoshis, 10000L)));
assertEquals(" (0.01% of trade amount)",
GUIUtil.getPercentageOfTradeAmount(fee, oneBitcoin, Coin.ZERO));
}
}