Browse Source

Add simple qt wallet test sending a transaction

pull/1/head
Russell Yanofsky 5 years ago
parent
commit
2754ef1c4a
  1. 22
      src/Makefile.qttest.include
  2. 13
      src/qt/test/test_main.cpp
  3. 104
      src/qt/test/wallettests.cpp
  4. 15
      src/qt/test/wallettests.h

22
src/Makefile.qttest.include

@ -11,7 +11,9 @@ TEST_QT_MOC_CPP = \ @@ -11,7 +11,9 @@ TEST_QT_MOC_CPP = \
qt/test/moc_uritests.cpp
if ENABLE_WALLET
TEST_QT_MOC_CPP += qt/test/moc_paymentservertests.cpp
TEST_QT_MOC_CPP += \
qt/test/moc_paymentservertests.cpp \
qt/test/moc_wallettests.cpp
endif
TEST_QT_H = \
@ -19,7 +21,16 @@ TEST_QT_H = \ @@ -19,7 +21,16 @@ TEST_QT_H = \
qt/test/rpcnestedtests.h \
qt/test/uritests.h \
qt/test/paymentrequestdata.h \
qt/test/paymentservertests.h
qt/test/paymentservertests.h \
qt/test/wallettests.h
TEST_BITCOIN_CPP = \
test/test_bitcoin.cpp \
test/testutil.cpp
TEST_BITCOIN_H = \
test/test_bitcoin.h \
test/testutil.h
qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)
@ -29,10 +40,13 @@ qt_test_test_bitcoin_qt_SOURCES = \ @@ -29,10 +40,13 @@ qt_test_test_bitcoin_qt_SOURCES = \
qt/test/rpcnestedtests.cpp \
qt/test/test_main.cpp \
qt/test/uritests.cpp \
$(TEST_QT_H)
$(TEST_QT_H) \
$(TEST_BITCOIN_CPP) \
$(TEST_BITCOIN_H)
if ENABLE_WALLET
qt_test_test_bitcoin_qt_SOURCES += \
qt/test/paymentservertests.cpp
qt/test/paymentservertests.cpp \
qt/test/wallettests.cpp
endif
nodist_qt_test_test_bitcoin_qt_SOURCES = $(TEST_QT_MOC_CPP)

13
src/qt/test/test_main.cpp

@ -14,9 +14,10 @@ @@ -14,9 +14,10 @@
#ifdef ENABLE_WALLET
#include "paymentservertests.h"
#include "wallettests.h"
#endif
#include <QCoreApplication>
#include <QApplication>
#include <QObject>
#include <QTest>
@ -43,8 +44,8 @@ int main(int argc, char *argv[]) @@ -43,8 +44,8 @@ int main(int argc, char *argv[])
bool fInvalid = false;
// Don't remove this, it's needed to access
// QCoreApplication:: in the tests
QCoreApplication app(argc, argv);
// QApplication:: and QCoreApplication:: in the tests
QApplication app(argc, argv);
app.setApplicationName("Bitcoin-Qt-test");
SSL_library_init();
@ -67,6 +68,12 @@ int main(int argc, char *argv[]) @@ -67,6 +68,12 @@ int main(int argc, char *argv[])
if (QTest::qExec(&test4) != 0) {
fInvalid = true;
}
#ifdef ENABLE_WALLET
WalletTests test5;
if (QTest::qExec(&test5) != 0) {
fInvalid = true;
}
#endif
return fInvalid;
}

104
src/qt/test/wallettests.cpp

@ -0,0 +1,104 @@ @@ -0,0 +1,104 @@
#include "wallettests.h"
#include "qt/bitcoinamountfield.h"
#include "qt/optionsmodel.h"
#include "qt/platformstyle.h"
#include "qt/qvalidatedlineedit.h"
#include "qt/sendcoinsdialog.h"
#include "qt/sendcoinsentry.h"
#include "qt/transactiontablemodel.h"
#include "qt/walletmodel.h"
#include "test/test_bitcoin.h"
#include "validation.h"
#include "wallet/wallet.h"
#include <QAbstractButton>
#include <QApplication>
#include <QTimer>
#include <QVBoxLayout>
namespace
{
//! Press "Yes" button in modal send confirmation dialog.
void ConfirmSend()
{
QTimer::singleShot(0, Qt::PreciseTimer, []() {
for (QWidget* widget : QApplication::topLevelWidgets()) {
if (widget->inherits("SendConfirmationDialog")) {
SendConfirmationDialog* dialog = qobject_cast<SendConfirmationDialog*>(widget);
QAbstractButton* button = dialog->button(QMessageBox::Yes);
button->setEnabled(true);
button->click();
}
}
});
}
//! Send coins to address and return txid.
uint256 SendCoins(CWallet& wallet, SendCoinsDialog& sendCoinsDialog, const CBitcoinAddress& address, CAmount amount)
{
QVBoxLayout* entries = sendCoinsDialog.findChild<QVBoxLayout*>("entries");
SendCoinsEntry* entry = qobject_cast<SendCoinsEntry*>(entries->itemAt(0)->widget());
entry->findChild<QValidatedLineEdit*>("payTo")->setText(QString::fromStdString(address.ToString()));
entry->findChild<BitcoinAmountField*>("payAmount")->setValue(amount);
uint256 txid;
boost::signals2::scoped_connection c = wallet.NotifyTransactionChanged.connect([&txid](CWallet*, const uint256& hash, ChangeType status) {
if (status == CT_NEW) txid = hash;
});
ConfirmSend();
QMetaObject::invokeMethod(&sendCoinsDialog, "on_sendButton_clicked");
return txid;
}
//! Find index of txid in transaction list.
QModelIndex FindTx(const QAbstractItemModel& model, const uint256& txid)
{
QString hash = QString::fromStdString(txid.ToString());
int rows = model.rowCount({});
for (int row = 0; row < rows; ++row) {
QModelIndex index = model.index(row, 0, {});
if (model.data(index, TransactionTableModel::TxHashRole) == hash) {
return index;
}
}
return {};
}
}
//! Simple qt wallet tests.
void WalletTests::walletTests()
{
// Set up wallet and chain with 101 blocks (1 mature block for spending).
TestChain100Setup test;
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
bitdb.MakeMock();
CWallet wallet("wallet_test.dat");
bool firstRun;
wallet.LoadWallet(firstRun);
{
LOCK(wallet.cs_wallet);
wallet.SetAddressBook(test.coinbaseKey.GetPubKey().GetID(), "", "receive");
wallet.AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
}
wallet.ScanForWalletTransactions(chainActive.Genesis(), true);
wallet.SetBroadcastTransactions(true);
// Create widgets for sending coins and listing transactions.
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
SendCoinsDialog sendCoinsDialog(platformStyle.get());
OptionsModel optionsModel;
WalletModel walletModel(platformStyle.get(), &wallet, &optionsModel);
sendCoinsDialog.setModel(&walletModel);
// Send two transactions, and verify they are added to transaction list.
TransactionTableModel* transactionTableModel = walletModel.getTransactionTableModel();
QCOMPARE(transactionTableModel->rowCount({}), 101);
uint256 txid1 = SendCoins(wallet, sendCoinsDialog, CBitcoinAddress(CKeyID()), 5 * COIN);
uint256 txid2 = SendCoins(wallet, sendCoinsDialog, CBitcoinAddress(CKeyID()), 10 * COIN);
QCOMPARE(transactionTableModel->rowCount({}), 103);
QVERIFY(FindTx(*transactionTableModel, txid1).isValid());
QVERIFY(FindTx(*transactionTableModel, txid2).isValid());
bitdb.Flush(true);
bitdb.Reset();
}

15
src/qt/test/wallettests.h

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
#ifndef BITCOIN_QT_TEST_WALLETTESTS_H
#define BITCOIN_QT_TEST_WALLETTESTS_H
#include <QObject>
#include <QTest>
class WalletTests : public QObject
{
Q_OBJECT
private Q_SLOTS:
void walletTests();
};
#endif // BITCOIN_QT_TEST_WALLETTESTS_H
Loading…
Cancel
Save