Skip to content

Commit ca3d42f

Browse files
jonasschnelliUdjinM6
authored andcommitted
Merge bitcoin#9974: Add basic Qt wallet test
9576b01 Enable xvfb in travis to allow running test_bitcoin-qt (Russell Yanofsky) 9e6817e Add new test_bitcoin-qt static library dependencies (Russell Yanofsky) 2754ef1 Add simple qt wallet test sending a transaction (Russell Yanofsky) b61b34c Add braces to if statements in Qt test_main (Russell Yanofsky) cc9503c Make qt test compatible with TestChain100Setup framework (Russell Yanofsky) 91e3035 Make test_bitcoin.cpp compatible with Qt Test framework (Russell Yanofsky) Tree-SHA512: da491181848b8c39138e997ae5ff2df0b16eef2d9cdd0a965229b1a28d4fa862d5f1ef314a1736e5050e88858f329124d15c689659fc6e50fefde769ba24e523 remove line, testing bitcoin -> dash, testing bitcoin -> dash, testing resolve name conflict, testing bitcoin -> dash re-add test fixture line set dash pointers to nullptr after deletion move ExceptionInitializer into test_dash_main.cpp code review, fix tests Signed-off-by: Pasta <Pasta@dash.org>
1 parent 6d1344b commit ca3d42f

File tree

10 files changed

+247
-74
lines changed

10 files changed

+247
-74
lines changed

ci/matrix.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ elif [ "$BUILD_TARGET" = "linux64" ]; then
7070
export RUN_TESTS=true
7171
elif [ "$BUILD_TARGET" = "linux64_nowallet" ]; then
7272
export HOST=x86_64-unknown-linux-gnu
73-
export PACKAGES="python3"
73+
export PACKAGES="python3 xvfb"
7474
export DEP_OPTS="NO_WALLET=1"
7575
export BITCOIN_CONFIG="--enable-glibc-back-compat --enable-reduce-exports"
7676
elif [ "$BUILD_TARGET" = "linux64_release" ]; then

ci/test_unittests.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export WINEDEBUG=fixme-all
1919
export BOOST_TEST_LOG_LEVEL=test_suite
2020

2121
cd build-ci/dashcore-$BUILD_TARGET
22+
23+
if [ "$RUN_TESTS" = "true" -a "${DEP_OPTS#*NO_QT=1}" = "$DEP_OPTS" ]; then
24+
export DISPLAY=:99.0;
25+
/sbin/start-stop-daemon --start --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac;
26+
fi
27+
2228
if [ "$DIRECT_WINE_EXEC_TESTS" = "true" ]; then
2329
# Inside Docker, binfmt isn't working so we can't trust in make invoking windows binaries correctly
2430
wine ./src/test/test_dash.exe

src/Makefile.qttest.include

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ TEST_QT_MOC_CPP = \
1010
qt/test/moc_compattests.cpp \
1111
qt/test/moc_rpcnestedtests.cpp \
1212
qt/test/moc_trafficgraphdatatests.cpp \
13-
qt/test/moc_uritests.cpp
13+
qt/test/moc_uritests.cpp
1414

1515
if ENABLE_WALLET
16-
TEST_QT_MOC_CPP += qt/test/moc_paymentservertests.cpp
16+
TEST_QT_MOC_CPP += \
17+
qt/test/moc_paymentservertests.cpp \
18+
qt/test/moc_wallettests.cpp
1719
endif
1820

1921
TEST_QT_H = \
@@ -22,7 +24,16 @@ TEST_QT_H = \
2224
qt/test/uritests.h \
2325
qt/test/paymentrequestdata.h \
2426
qt/test/paymentservertests.h \
25-
qt/test/trafficgraphdatatests.h
27+
qt/test/trafficgraphdatatests.h \
28+
qt/test/wallettests.h
29+
30+
TEST_BITCOIN_CPP = \
31+
test/test_dash.cpp \
32+
test/testutil.cpp
33+
34+
TEST_BITCOIN_H = \
35+
test/test_dash.h \
36+
test/testutil.h
2637

2738
qt_test_test_dash_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDES) \
2839
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)
@@ -33,10 +44,13 @@ qt_test_test_dash_qt_SOURCES = \
3344
qt/test/test_main.cpp \
3445
qt/test/uritests.cpp \
3546
qt/test/trafficgraphdatatests.cpp \
36-
$(TEST_QT_H)
47+
$(TEST_QT_H) \
48+
$(TEST_BITCOIN_CPP) \
49+
$(TEST_BITCOIN_H)
3750
if ENABLE_WALLET
3851
qt_test_test_dash_qt_SOURCES += \
39-
qt/test/paymentservertests.cpp
52+
qt/test/paymentservertests.cpp \
53+
qt/test/wallettests.cpp
4054
endif
4155

4256
nodist_qt_test_test_dash_qt_SOURCES = $(TEST_QT_MOC_CPP)

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ BITCOIN_TESTS =\
129129
test/subsidy_tests.cpp \
130130
test/test_dash.cpp \
131131
test/test_dash.h \
132+
test/test_dash_main.cpp \
132133
test/test_random.h \
133134
test/testutil.cpp \
134135
test/testutil.h \

src/qt/test/rpcnestedtests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,17 @@ void RPCNestedTests::rpcNestedTests()
154154
QVERIFY_EXCEPTION_THROWN(RPCConsole::RPCExecuteCommandLine(result, "rpcNestedTest(abc,,)"), std::runtime_error); //don't tollerate empty arguments when using ,
155155
#endif
156156

157+
UnloadBlockIndex();
157158
delete pcoinsTip;
159+
pcoinsTip = nullptr;
158160
delete deterministicMNManager;
161+
deterministicMNManager = nullptr;
159162
delete pcoinsdbview;
163+
pcoinsdbview = nullptr;
160164
delete pblocktree;
165+
pblocktree = nullptr;
161166
delete evoDb;
167+
evoDb = nullptr;
162168

163169
boost::filesystem::remove_all(boost::filesystem::path(path));
164170
}

src/qt/test/test_main.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#endif
99

1010
#include "chainparams.h"
11-
#include "key.h"
1211
#include "rpcnestedtests.h"
1312
#include "util.h"
1413
#include "uritests.h"
@@ -17,28 +16,38 @@
1716

1817
#ifdef ENABLE_WALLET
1918
#include "paymentservertests.h"
19+
#include "wallettests.h"
2020
#endif
2121

22-
#include <QCoreApplication>
22+
#include <QApplication>
2323
#include <QObject>
2424
#include <QTest>
2525

2626
#include <openssl/ssl.h>
2727

28-
#if defined(QT_STATICPLUGIN) && QT_VERSION < 0x050000
28+
#if defined(QT_STATICPLUGIN)
2929
#include <QtPlugin>
30+
#if QT_VERSION < 0x050000
3031
Q_IMPORT_PLUGIN(qcncodecs)
3132
Q_IMPORT_PLUGIN(qjpcodecs)
3233
Q_IMPORT_PLUGIN(qtwcodecs)
3334
Q_IMPORT_PLUGIN(qkrcodecs)
35+
#else
36+
#if defined(QT_QPA_PLATFORM_XCB)
37+
Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
38+
#elif defined(QT_QPA_PLATFORM_WINDOWS)
39+
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
40+
#elif defined(QT_QPA_PLATFORM_COCOA)
41+
Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin);
42+
#endif
43+
#endif
3444
#endif
3545

3646
extern void noui_connect();
3747

3848
// This is all you need to run all the tests
3949
int main(int argc, char *argv[])
4050
{
41-
ECC_Start();
4251
SetupEnvironment();
4352
SetupNetworking();
4453
SelectParams(CBaseChainParams::MAIN);
@@ -47,31 +56,39 @@ int main(int argc, char *argv[])
4756
bool fInvalid = false;
4857

4958
// Don't remove this, it's needed to access
50-
// QCoreApplication:: in the tests
51-
QCoreApplication app(argc, argv);
59+
// QApplication:: and QCoreApplication:: in the tests
60+
QApplication app(argc, argv);
5261
app.setApplicationName("Dash-Qt-test");
5362

5463
SSL_library_init();
5564

5665
URITests test1;
57-
if (QTest::qExec(&test1) != 0)
66+
if (QTest::qExec(&test1) != 0) {
5867
fInvalid = true;
68+
}
5969
#ifdef ENABLE_WALLET
6070
PaymentServerTests test2;
61-
if (QTest::qExec(&test2) != 0)
71+
if (QTest::qExec(&test2) != 0) {
6272
fInvalid = true;
73+
}
6374
#endif
6475
RPCNestedTests test3;
65-
if (QTest::qExec(&test3) != 0)
76+
if (QTest::qExec(&test3) != 0) {
6677
fInvalid = true;
67-
78+
}
6879
CompatTests test4;
69-
if (QTest::qExec(&test4) != 0)
80+
if (QTest::qExec(&test4) != 0) {
7081
fInvalid = true;
82+
}
83+
#ifdef ENABLE_WALLET
84+
WalletTests test5;
85+
if (QTest::qExec(&test5) != 0) {
86+
fInvalid = true;
87+
}
88+
#endif
7189

72-
TrafficGraphDataTests test5;
73-
if (QTest::qExec(&test5) != 0)
90+
TrafficGraphDataTests test6;
91+
if (QTest::qExec(&test6) != 0)
7492
fInvalid = true;
75-
ECC_Stop();
7693
return fInvalid;
7794
}

src/qt/test/wallettests.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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_dash.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+
}

src/qt/test/wallettests.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef BITCOIN_QT_TEST_WALLETTESTS_H
2+
#define BITCOIN_QT_TEST_WALLETTESTS_H
3+
4+
#include <QObject>
5+
#include <QTest>
6+
7+
class WalletTests : public QObject
8+
{
9+
Q_OBJECT
10+
11+
private Q_SLOTS:
12+
void walletTests();
13+
};
14+
15+
#endif // BITCOIN_QT_TEST_WALLETTESTS_H

0 commit comments

Comments
 (0)