Skip to content

Commit 74eac3a

Browse files
committed
test: add coverage for 'useAvailableBalance' functionality
The following cases were covered: Case 1: No coin control selected coins. - 'useAvailableBalance' should fill the amount edit box with the total available balance. Case 2: With coin control selected coins. - 'useAvailableBalance' should fill the amount edit box with the sum of the selected coins values.
1 parent dc1cc1c commit 74eac3a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/qt/sendcoinsdialog.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class SendCoinsDialog : public QDialog
4949
void pasteEntry(const SendCoinsRecipient &rv);
5050
bool handlePaymentRequest(const SendCoinsRecipient &recipient);
5151

52+
// Only used for testing-purposes
53+
wallet::CCoinControl* getCoinControl() { return m_coin_control.get(); }
54+
5255
public Q_SLOTS:
5356
void clear();
5457
void reject() override;

src/qt/test/wallettests.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <qt/test/wallettests.h>
66
#include <qt/test/util.h>
77

8+
#include <wallet/coincontrol.h>
89
#include <interfaces/chain.h>
910
#include <interfaces/node.h>
1011
#include <key_io.h>
@@ -136,6 +137,42 @@ void CompareBalance(WalletModel& walletModel, CAmount expected_balance, QLabel*
136137
QCOMPARE(balance_label_to_check->text().trimmed(), balanceComparison);
137138
}
138139

140+
// Verify the 'useAvailableBalance' functionality. With and without manually selected coins.
141+
// Case 1: No coin control selected coins.
142+
// 'useAvailableBalance' should fill the amount edit box with the total available balance
143+
// Case 2: With coin control selected coins.
144+
// 'useAvailableBalance' should fill the amount edit box with the sum of the selected coins values.
145+
void VerifyUseAvailableBalance(SendCoinsDialog& sendCoinsDialog, const WalletModel& walletModel)
146+
{
147+
// Verify first entry amount and "useAvailableBalance" button
148+
QVBoxLayout* entries = sendCoinsDialog.findChild<QVBoxLayout*>("entries");
149+
QVERIFY(entries->count() == 1); // only one entry
150+
SendCoinsEntry* send_entry = qobject_cast<SendCoinsEntry*>(entries->itemAt(0)->widget());
151+
QVERIFY(send_entry->getValue().amount == 0);
152+
// Now click "useAvailableBalance", check updated balance (the entire wallet balance should be set)
153+
Q_EMIT send_entry->useAvailableBalance(send_entry);
154+
QVERIFY(send_entry->getValue().amount == walletModel.getCachedBalance().balance);
155+
156+
// Now manually select two coins and click on "useAvailableBalance". Then check updated balance
157+
// (only the sum of the selected coins should be set).
158+
int COINS_TO_SELECT = 2;
159+
auto coins = walletModel.wallet().listCoins();
160+
CAmount sum_selected_coins = 0;
161+
int selected = 0;
162+
QVERIFY(coins.size() == 1); // context check, coins received only on one destination
163+
for (const auto& [outpoint, tx_out] : coins.begin()->second) {
164+
sendCoinsDialog.getCoinControl()->Select(outpoint);
165+
sum_selected_coins += tx_out.txout.nValue;
166+
if (++selected == COINS_TO_SELECT) break;
167+
}
168+
QVERIFY(selected == COINS_TO_SELECT);
169+
170+
// Now that we have 2 coins selected, "useAvailableBalance" should update the balance label only with
171+
// the sum of them.
172+
Q_EMIT send_entry->useAvailableBalance(send_entry);
173+
QVERIFY(send_entry->getValue().amount == sum_selected_coins);
174+
}
175+
139176
//! Simple qt wallet tests.
140177
//
141178
// Test widgets can be debugged interactively calling show() on them and
@@ -207,6 +244,9 @@ void TestGUI(interfaces::Node& node)
207244
// Check balance in send dialog
208245
CompareBalance(walletModel, walletModel.wallet().getBalance(), sendCoinsDialog.findChild<QLabel*>("labelBalance"));
209246

247+
// Check 'UseAvailableBalance' functionality
248+
VerifyUseAvailableBalance(sendCoinsDialog, walletModel);
249+
210250
// Send two transactions, and verify they are added to transaction list.
211251
TransactionTableModel* transactionTableModel = walletModel.getTransactionTableModel();
212252
QCOMPARE(transactionTableModel->rowCount({}), 105);

0 commit comments

Comments
 (0)