|
| 1 | +#include "wallettests.h" |
| 2 | + |
| 3 | +#include "qt/bitcoinamountfield.h" |
| 4 | +#include "qt/optionsmodel.h" |
| 5 | +#include "qt/platformstyle.h" |
| 6 | +#include "qt/qvalidatedlineedit.h" |
| 7 | +#include "qt/sendcoinsdialog.h" |
| 8 | +#include "qt/sendcoinsentry.h" |
| 9 | +#include "qt/transactiontablemodel.h" |
| 10 | +#include "qt/walletmodel.h" |
| 11 | +#include "test/test_bitcoin.h" |
| 12 | +#include "validation.h" |
| 13 | +#include "wallet/wallet.h" |
| 14 | + |
| 15 | +#include <QAbstractButton> |
| 16 | +#include <QApplication> |
| 17 | +#include <QTimer> |
| 18 | +#include <QVBoxLayout> |
| 19 | + |
| 20 | +namespace |
| 21 | +{ |
| 22 | +//! Press "Yes" button in modal send confirmation dialog. |
| 23 | +void ConfirmSend() |
| 24 | +{ |
| 25 | + QTimer::singleShot(0, Qt::PreciseTimer, []() { |
| 26 | + for (QWidget* widget : QApplication::topLevelWidgets()) { |
| 27 | + if (widget->inherits("SendConfirmationDialog")) { |
| 28 | + SendConfirmationDialog* dialog = qobject_cast<SendConfirmationDialog*>(widget); |
| 29 | + QAbstractButton* button = dialog->button(QMessageBox::Yes); |
| 30 | + button->setEnabled(true); |
| 31 | + button->click(); |
| 32 | + } |
| 33 | + } |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +//! Send coins to address and return txid. |
| 38 | +uint256 SendCoins(CWallet& wallet, SendCoinsDialog& sendCoinsDialog, const CBitcoinAddress& address, CAmount amount) |
| 39 | +{ |
| 40 | + QVBoxLayout* entries = sendCoinsDialog.findChild<QVBoxLayout*>("entries"); |
| 41 | + SendCoinsEntry* entry = qobject_cast<SendCoinsEntry*>(entries->itemAt(0)->widget()); |
| 42 | + entry->findChild<QValidatedLineEdit*>("payTo")->setText(QString::fromStdString(address.ToString())); |
| 43 | + entry->findChild<BitcoinAmountField*>("payAmount")->setValue(amount); |
| 44 | + uint256 txid; |
| 45 | + boost::signals2::scoped_connection c = wallet.NotifyTransactionChanged.connect([&txid](CWallet*, const uint256& hash, ChangeType status) { |
| 46 | + if (status == CT_NEW) txid = hash; |
| 47 | + }); |
| 48 | + ConfirmSend(); |
| 49 | + QMetaObject::invokeMethod(&sendCoinsDialog, "on_sendButton_clicked"); |
| 50 | + return txid; |
| 51 | +} |
| 52 | + |
| 53 | +//! Find index of txid in transaction list. |
| 54 | +QModelIndex FindTx(const QAbstractItemModel& model, const uint256& txid) |
| 55 | +{ |
| 56 | + QString hash = QString::fromStdString(txid.ToString()); |
| 57 | + int rows = model.rowCount({}); |
| 58 | + for (int row = 0; row < rows; ++row) { |
| 59 | + QModelIndex index = model.index(row, 0, {}); |
| 60 | + if (model.data(index, TransactionTableModel::TxHashRole) == hash) { |
| 61 | + return index; |
| 62 | + } |
| 63 | + } |
| 64 | + return {}; |
| 65 | +} |
| 66 | +} |
| 67 | + |
| 68 | +//! Simple qt wallet tests. |
| 69 | +void WalletTests::walletTests() |
| 70 | +{ |
| 71 | + // Set up wallet and chain with 101 blocks (1 mature block for spending). |
| 72 | + TestChain100Setup test; |
| 73 | + test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey())); |
| 74 | + bitdb.MakeMock(); |
| 75 | + CWallet wallet("wallet_test.dat"); |
| 76 | + bool firstRun; |
| 77 | + wallet.LoadWallet(firstRun); |
| 78 | + { |
| 79 | + LOCK(wallet.cs_wallet); |
| 80 | + wallet.SetAddressBook(test.coinbaseKey.GetPubKey().GetID(), "", "receive"); |
| 81 | + wallet.AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey()); |
| 82 | + } |
| 83 | + wallet.ScanForWalletTransactions(chainActive.Genesis(), true); |
| 84 | + wallet.SetBroadcastTransactions(true); |
| 85 | + |
| 86 | + // Create widgets for sending coins and listing transactions. |
| 87 | + std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other")); |
| 88 | + SendCoinsDialog sendCoinsDialog(platformStyle.get()); |
| 89 | + OptionsModel optionsModel; |
| 90 | + WalletModel walletModel(platformStyle.get(), &wallet, &optionsModel); |
| 91 | + sendCoinsDialog.setModel(&walletModel); |
| 92 | + |
| 93 | + // Send two transactions, and verify they are added to transaction list. |
| 94 | + TransactionTableModel* transactionTableModel = walletModel.getTransactionTableModel(); |
| 95 | + QCOMPARE(transactionTableModel->rowCount({}), 101); |
| 96 | + uint256 txid1 = SendCoins(wallet, sendCoinsDialog, CBitcoinAddress(CKeyID()), 5 * COIN); |
| 97 | + uint256 txid2 = SendCoins(wallet, sendCoinsDialog, CBitcoinAddress(CKeyID()), 10 * COIN); |
| 98 | + QCOMPARE(transactionTableModel->rowCount({}), 103); |
| 99 | + QVERIFY(FindTx(*transactionTableModel, txid1).isValid()); |
| 100 | + QVERIFY(FindTx(*transactionTableModel, txid2).isValid()); |
| 101 | + |
| 102 | + bitdb.Flush(true); |
| 103 | + bitdb.Reset(); |
| 104 | +} |
0 commit comments