Skip to content

Commit fdbe7f4

Browse files
author
MarcoFalke
committed
Merge #11625: Add BitcoinApplication & RPCConsole tests
7e4bd19 Add BitcoinApplication & RPCConsole tests (Russell Yanofsky) ca20b65 Move BitcoinApplication to header so it can be tested (Russell Yanofsky) Pull request description: Add test coverage for Qt initialization code & basic RPC console functionality Motivation for this change was a bug in #11603 which existing tests failed to catch. Tree-SHA512: f66546ffc84b8e07679c66a73b265023fbf6a0cb8f24f1606a5fcae2dd3b4dc7b2c6d26c69dedcec53398a26ef17c4d5fb28c055698fa6e45e89aa2995cefe2f
2 parents c6806ee + 7e4bd19 commit fdbe7f4

File tree

11 files changed

+360
-120
lines changed

11 files changed

+360
-120
lines changed

src/Makefile.qt.include

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ QT_MOC_CPP = \
120120
qt/moc_bantablemodel.cpp \
121121
qt/moc_bitcoinaddressvalidator.cpp \
122122
qt/moc_bitcoinamountfield.cpp \
123+
qt/moc_bitcoin.cpp \
123124
qt/moc_bitcoingui.cpp \
124125
qt/moc_bitcoinunits.cpp \
125126
qt/moc_clientmodel.cpp \
@@ -166,7 +167,6 @@ BITCOIN_MM = \
166167
qt/macos_appnap.mm
167168

168169
QT_MOC = \
169-
qt/bitcoin.moc \
170170
qt/bitcoinamountfield.moc \
171171
qt/intro.moc \
172172
qt/overviewpage.moc \
@@ -194,6 +194,7 @@ BITCOIN_QT_H = \
194194
qt/bantablemodel.h \
195195
qt/bitcoinaddressvalidator.h \
196196
qt/bitcoinamountfield.h \
197+
qt/bitcoin.h \
197198
qt/bitcoingui.h \
198199
qt/bitcoinunits.h \
199200
qt/clientmodel.h \
@@ -302,6 +303,7 @@ RES_ICONS = \
302303

303304
BITCOIN_QT_BASE_CPP = \
304305
qt/bantablemodel.cpp \
306+
qt/bitcoin.cpp \
305307
qt/bitcoinaddressvalidator.cpp \
306308
qt/bitcoinamountfield.cpp \
307309
qt/bitcoingui.cpp \
@@ -382,6 +384,9 @@ qt_libbitcoinqt_a_OBJCXXFLAGS = $(AM_OBJCXXFLAGS) $(QT_PIE_FLAGS)
382384

383385
qt_libbitcoinqt_a_SOURCES = $(BITCOIN_QT_CPP) $(BITCOIN_QT_H) $(QT_FORMS_UI) \
384386
$(QT_QRC) $(QT_QRC_LOCALE) $(QT_TS) $(PROTOBUF_PROTO) $(RES_ICONS) $(RES_IMAGES) $(RES_MOVIES)
387+
if TARGET_DARWIN
388+
qt_libbitcoinqt_a_SOURCES += $(BITCOIN_MM)
389+
endif
385390

386391
nodist_qt_libbitcoinqt_a_SOURCES = $(QT_MOC_CPP) $(QT_MOC) $(PROTOBUF_CC) \
387392
$(PROTOBUF_H) $(QT_QRC_CPP) $(QT_QRC_LOCALE_CPP)
@@ -404,10 +409,7 @@ qt_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_INCLUDE
404409
$(QT_INCLUDES) $(PROTOBUF_CFLAGS) $(QR_CFLAGS)
405410
qt_bitcoin_qt_CXXFLAGS = $(AM_CXXFLAGS) $(QT_PIE_FLAGS)
406411

407-
qt_bitcoin_qt_SOURCES = qt/bitcoin.cpp
408-
if TARGET_DARWIN
409-
qt_bitcoin_qt_SOURCES += $(BITCOIN_MM)
410-
endif
412+
qt_bitcoin_qt_SOURCES = qt/main.cpp
411413
if TARGET_WINDOWS
412414
qt_bitcoin_qt_SOURCES += $(BITCOIN_RC)
413415
endif

src/Makefile.qttest.include

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ bin_PROGRAMS += qt/test/test_bitcoin-qt
66
TESTS += qt/test/test_bitcoin-qt
77

88
TEST_QT_MOC_CPP = \
9+
qt/test/moc_apptests.cpp \
910
qt/test/moc_compattests.cpp \
1011
qt/test/moc_rpcnestedtests.cpp \
1112
qt/test/moc_uritests.cpp
@@ -22,6 +23,7 @@ endif # ENABLE_WALLET
2223

2324
TEST_QT_H = \
2425
qt/test/addressbooktests.h \
26+
qt/test/apptests.h \
2527
qt/test/compattests.h \
2628
qt/test/rpcnestedtests.h \
2729
qt/test/uritests.h \
@@ -40,6 +42,7 @@ qt_test_test_bitcoin_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_
4042
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)
4143

4244
qt_test_test_bitcoin_qt_SOURCES = \
45+
qt/test/apptests.cpp \
4346
qt/test/compattests.cpp \
4447
qt/test/rpcnestedtests.cpp \
4548
qt/test/test_main.cpp \

src/qt/bitcoin.cpp

Lines changed: 18 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <config/bitcoin-config.h>
77
#endif
88

9+
#include <qt/bitcoin.h>
910
#include <qt/bitcoingui.h>
1011

1112
#include <chainparams.h>
@@ -71,11 +72,6 @@ Q_DECLARE_METATYPE(bool*)
7172
Q_DECLARE_METATYPE(CAmount)
7273
Q_DECLARE_METATYPE(uint256)
7374

74-
/** Translate string to current locale using Qt. */
75-
const std::function<std::string(const char*)> G_TRANSLATION_FUN = [](const char* psz) {
76-
return QCoreApplication::translate("bitcoin-core", psz).toStdString();
77-
};
78-
7975
static QString GetLangTerritory()
8076
{
8177
QSettings settings;
@@ -140,101 +136,6 @@ void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons
140136
}
141137
}
142138

143-
/** Class encapsulating Bitcoin Core startup and shutdown.
144-
* Allows running startup and shutdown in a different thread from the UI thread.
145-
*/
146-
class BitcoinCore: public QObject
147-
{
148-
Q_OBJECT
149-
public:
150-
explicit BitcoinCore(interfaces::Node& node);
151-
152-
public Q_SLOTS:
153-
void initialize();
154-
void shutdown();
155-
156-
Q_SIGNALS:
157-
void initializeResult(bool success);
158-
void shutdownResult();
159-
void runawayException(const QString &message);
160-
161-
private:
162-
/// Pass fatal exception message to UI thread
163-
void handleRunawayException(const std::exception *e);
164-
165-
interfaces::Node& m_node;
166-
};
167-
168-
/** Main Bitcoin application object */
169-
class BitcoinApplication: public QApplication
170-
{
171-
Q_OBJECT
172-
public:
173-
explicit BitcoinApplication(interfaces::Node& node, int &argc, char **argv);
174-
~BitcoinApplication();
175-
176-
#ifdef ENABLE_WALLET
177-
/// Create payment server
178-
void createPaymentServer();
179-
#endif
180-
/// parameter interaction/setup based on rules
181-
void parameterSetup();
182-
/// Create options model
183-
void createOptionsModel(bool resetSettings);
184-
/// Create main window
185-
void createWindow(const NetworkStyle *networkStyle);
186-
/// Create splash screen
187-
void createSplashScreen(const NetworkStyle *networkStyle);
188-
189-
/// Request core initialization
190-
void requestInitialize();
191-
/// Request core shutdown
192-
void requestShutdown();
193-
194-
/// Get process return value
195-
int getReturnValue() const { return returnValue; }
196-
197-
/// Get window identifier of QMainWindow (BitcoinGUI)
198-
WId getMainWinId() const;
199-
200-
/// Setup platform style
201-
void setupPlatformStyle();
202-
203-
public Q_SLOTS:
204-
void initializeResult(bool success);
205-
void shutdownResult();
206-
/// Handle runaway exceptions. Shows a message box with the problem and quits the program.
207-
void handleRunawayException(const QString &message);
208-
void addWallet(WalletModel* walletModel);
209-
void removeWallet();
210-
211-
Q_SIGNALS:
212-
void requestedInitialize();
213-
void requestedShutdown();
214-
void stopThread();
215-
void splashFinished();
216-
217-
private:
218-
QThread *coreThread;
219-
interfaces::Node& m_node;
220-
OptionsModel *optionsModel;
221-
ClientModel *clientModel;
222-
BitcoinGUI *window;
223-
QTimer *pollShutdownTimer;
224-
#ifdef ENABLE_WALLET
225-
PaymentServer* paymentServer;
226-
std::vector<WalletModel*> m_wallet_models;
227-
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
228-
#endif
229-
int returnValue;
230-
const PlatformStyle *platformStyle;
231-
std::unique_ptr<QWidget> shutdownWindow;
232-
233-
void startThread();
234-
};
235-
236-
#include <qt/bitcoin.moc>
237-
238139
BitcoinCore::BitcoinCore(interfaces::Node& node) :
239140
QObject(), m_node(node)
240141
{
@@ -358,6 +259,11 @@ void BitcoinApplication::createSplashScreen(const NetworkStyle *networkStyle)
358259
connect(this, &BitcoinApplication::requestedShutdown, splash, &QWidget::close);
359260
}
360261

262+
bool BitcoinApplication::baseInitialize()
263+
{
264+
return m_node.baseInitialize();
265+
}
266+
361267
void BitcoinApplication::startThread()
362268
{
363269
if(coreThread)
@@ -467,7 +373,7 @@ void BitcoinApplication::initializeResult(bool success)
467373
#ifdef ENABLE_BIP70
468374
PaymentServer::LoadRootCAs();
469375
#endif
470-
paymentServer->setOptionsModel(optionsModel);
376+
if (paymentServer) paymentServer->setOptionsModel(optionsModel);
471377
#endif
472378

473379
clientModel = new ClientModel(m_node, optionsModel);
@@ -496,16 +402,19 @@ void BitcoinApplication::initializeResult(bool success)
496402
window->show();
497403
}
498404
Q_EMIT splashFinished();
405+
Q_EMIT windowShown(window);
499406

500407
#ifdef ENABLE_WALLET
501408
// Now that initialization/startup is done, process any command-line
502409
// bitcoin: URIs or payment requests:
503-
connect(paymentServer, &PaymentServer::receivedPaymentRequest, window, &BitcoinGUI::handlePaymentRequest);
504-
connect(window, &BitcoinGUI::receivedURI, paymentServer, &PaymentServer::handleURIOrFile);
505-
connect(paymentServer, &PaymentServer::message, [this](const QString& title, const QString& message, unsigned int style) {
506-
window->message(title, message, style);
507-
});
508-
QTimer::singleShot(100, paymentServer, &PaymentServer::uiReady);
410+
if (paymentServer) {
411+
connect(paymentServer, &PaymentServer::receivedPaymentRequest, window, &BitcoinGUI::handlePaymentRequest);
412+
connect(window, &BitcoinGUI::receivedURI, paymentServer, &PaymentServer::handleURIOrFile);
413+
connect(paymentServer, &PaymentServer::message, [this](const QString& title, const QString& message, unsigned int style) {
414+
window->message(title, message, style);
415+
});
416+
QTimer::singleShot(100, paymentServer, &PaymentServer::uiReady);
417+
}
509418
#endif
510419
pollShutdownTimer->start(200);
511420
} else {
@@ -548,7 +457,7 @@ static void SetupUIArgs()
548457
}
549458

550459
#ifndef BITCOIN_QT_TEST
551-
int main(int argc, char *argv[])
460+
int GuiMain(int argc, char* argv[])
552461
{
553462
#ifdef WIN32
554463
util::WinCmdLineArgs winArgs;
@@ -706,7 +615,7 @@ int main(int argc, char *argv[])
706615
// Perform base initialization before spinning up initialization/shutdown thread
707616
// This is acceptable because this function only contains steps that are quick to execute,
708617
// so the GUI thread won't be held up.
709-
if (node->baseInitialize()) {
618+
if (app.baseInitialize()) {
710619
app.requestInitialize();
711620
#if defined(Q_OS_WIN)
712621
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("%1 didn't yet exit safely...").arg(QObject::tr(PACKAGE_NAME)), (HWND)app.getMainWinId());

src/qt/bitcoin.h

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright (c) 2011-2016 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_QT_BITCOIN_H
6+
#define BITCOIN_QT_BITCOIN_H
7+
8+
#if defined(HAVE_CONFIG_H)
9+
#include <config/bitcoin-config.h>
10+
#endif
11+
12+
#include <QApplication>
13+
#include <memory>
14+
#include <vector>
15+
16+
class BitcoinGUI;
17+
class ClientModel;
18+
class NetworkStyle;
19+
class OptionsModel;
20+
class PaymentServer;
21+
class PlatformStyle;
22+
class WalletModel;
23+
24+
namespace interfaces {
25+
class Handler;
26+
class Node;
27+
} // namespace interfaces
28+
29+
/** Class encapsulating Bitcoin Core startup and shutdown.
30+
* Allows running startup and shutdown in a different thread from the UI thread.
31+
*/
32+
class BitcoinCore: public QObject
33+
{
34+
Q_OBJECT
35+
public:
36+
explicit BitcoinCore(interfaces::Node& node);
37+
38+
public Q_SLOTS:
39+
void initialize();
40+
void shutdown();
41+
42+
Q_SIGNALS:
43+
void initializeResult(bool success);
44+
void shutdownResult();
45+
void runawayException(const QString &message);
46+
47+
private:
48+
/// Pass fatal exception message to UI thread
49+
void handleRunawayException(const std::exception *e);
50+
51+
interfaces::Node& m_node;
52+
};
53+
54+
/** Main Bitcoin application object */
55+
class BitcoinApplication: public QApplication
56+
{
57+
Q_OBJECT
58+
public:
59+
explicit BitcoinApplication(interfaces::Node& node, int &argc, char **argv);
60+
~BitcoinApplication();
61+
62+
#ifdef ENABLE_WALLET
63+
/// Create payment server
64+
void createPaymentServer();
65+
#endif
66+
/// parameter interaction/setup based on rules
67+
void parameterSetup();
68+
/// Create options model
69+
void createOptionsModel(bool resetSettings);
70+
/// Create main window
71+
void createWindow(const NetworkStyle *networkStyle);
72+
/// Create splash screen
73+
void createSplashScreen(const NetworkStyle *networkStyle);
74+
/// Basic initialization, before starting initialization/shutdown thread. Return true on success.
75+
bool baseInitialize();
76+
77+
/// Request core initialization
78+
void requestInitialize();
79+
/// Request core shutdown
80+
void requestShutdown();
81+
82+
/// Get process return value
83+
int getReturnValue() const { return returnValue; }
84+
85+
/// Get window identifier of QMainWindow (BitcoinGUI)
86+
WId getMainWinId() const;
87+
88+
/// Setup platform style
89+
void setupPlatformStyle();
90+
91+
public Q_SLOTS:
92+
void initializeResult(bool success);
93+
void shutdownResult();
94+
/// Handle runaway exceptions. Shows a message box with the problem and quits the program.
95+
void handleRunawayException(const QString &message);
96+
void addWallet(WalletModel* walletModel);
97+
void removeWallet();
98+
99+
Q_SIGNALS:
100+
void requestedInitialize();
101+
void requestedShutdown();
102+
void stopThread();
103+
void splashFinished();
104+
void windowShown(BitcoinGUI* window);
105+
106+
private:
107+
QThread *coreThread;
108+
interfaces::Node& m_node;
109+
OptionsModel *optionsModel;
110+
ClientModel *clientModel;
111+
BitcoinGUI *window;
112+
QTimer *pollShutdownTimer;
113+
#ifdef ENABLE_WALLET
114+
PaymentServer* paymentServer;
115+
std::vector<WalletModel*> m_wallet_models;
116+
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
117+
#endif
118+
int returnValue;
119+
const PlatformStyle *platformStyle;
120+
std::unique_ptr<QWidget> shutdownWindow;
121+
122+
void startThread();
123+
};
124+
125+
int GuiMain(int argc, char* argv[]);
126+
127+
#endif // BITCOIN_QT_BITCOIN_H

0 commit comments

Comments
 (0)