Bitcoin Core 28.99.0
P2P Digital Currency
apptests.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-2022 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#include <qt/test/apptests.h>
6
7#include <chainparams.h>
8#include <key.h>
9#include <logging.h>
10#include <qt/bitcoin.h>
11#include <qt/bitcoingui.h>
12#include <qt/networkstyle.h>
13#include <qt/rpcconsole.h>
15#include <validation.h>
16
17#include <QAction>
18#include <QLineEdit>
19#include <QRegularExpression>
20#include <QScopedPointer>
21#include <QSignalSpy>
22#include <QString>
23#include <QTest>
24#include <QTextEdit>
25#include <QtGlobal>
26#include <QtTest/QtTestWidgets>
27#include <QtTest/QtTestGui>
28
29namespace {
31QString FindInConsole(const QString& output, const QString& pattern)
32{
33 const QRegularExpression re(pattern);
34 return re.match(output).captured(1);
35}
36
38void TestRpcCommand(RPCConsole* console)
39{
40 QTextEdit* messagesWidget = console->findChild<QTextEdit*>("messagesWidget");
41 QLineEdit* lineEdit = console->findChild<QLineEdit*>("lineEdit");
42 QSignalSpy mw_spy(messagesWidget, &QTextEdit::textChanged);
43 QVERIFY(mw_spy.isValid());
44 QTest::keyClicks(lineEdit, "getblockchaininfo");
45 QTest::keyClick(lineEdit, Qt::Key_Return);
46 QVERIFY(mw_spy.wait(1000));
47 QCOMPARE(mw_spy.count(), 4);
48 const QString output = messagesWidget->toPlainText();
49 const QString pattern = QStringLiteral("\"chain\": \"(\\w+)\"");
50 QCOMPARE(FindInConsole(output, pattern), QString("regtest"));
51}
52} // namespace
53
56{
57#ifdef Q_OS_MACOS
58 if (QApplication::platformName() == "minimal") {
59 // Disable for mac on "minimal" platform to avoid crashes inside the Qt
60 // framework when it tries to look up unimplemented cocoa functions,
61 // and fails to handle returned nulls
62 // (https://bugreports.qt.io/browse/QTBUG-49686).
63 qWarning() << "Skipping AppTests on mac build with 'minimal' platform set due to Qt bugs. To run AppTests, invoke "
64 "with 'QT_QPA_PLATFORM=cocoa test_bitcoin-qt' on mac, or else use a linux or windows build.";
65 return;
66 }
67#endif
68
69 qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");
71 QVERIFY(m_app.createOptionsModel(/*resetSettings=*/true));
72 QScopedPointer<const NetworkStyle> style(NetworkStyle::instantiate(Params().GetChainType()));
74 m_app.createWindow(style.data());
76 expectCallback("guiTests");
79 m_app.exec();
81 m_app.exec();
82
83 // Reset global state to avoid interfering with later tests.
85}
86
89{
90 HandleCallback callback{"guiTests", *this};
91 connect(window, &BitcoinGUI::consoleShown, this, &AppTests::consoleTests);
92 expectCallback("consoleTests");
93 QAction* action = window->findChild<QAction*>("openRPCConsoleAction");
94 action->activate(QAction::Trigger);
95}
96
99{
100 HandleCallback callback{"consoleTests", *this};
101 TestRpcCommand(console);
102}
103
106{
107 auto& callbacks = m_app_tests.m_callbacks;
108 auto it = callbacks.find(m_callback);
109 assert(it != callbacks.end());
110 callbacks.erase(it);
111 if (callbacks.empty()) {
112 m_app_tests.m_app.exit(0);
113 }
114}
const CChainParams & Params()
Return the currently selected parameters.
void expectCallback(std::string callback)
Add expected callback name to list of pending callbacks.
Definition: apptests.h:30
void guiTests(BitcoinGUI *window)
Entry point for BitcoinGUI tests.
Definition: apptests.cpp:88
BitcoinApplication & m_app
Bitcoin application.
Definition: apptests.h:41
std::multiset< std::string > m_callbacks
Set of pending callback names.
Definition: apptests.h:47
void appTests()
Entry point for BitcoinApplication tests.
Definition: apptests.cpp:55
void consoleTests(RPCConsole *console)
Entry point for RPCConsole tests.
Definition: apptests.cpp:98
void DisconnectTestLogger() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Only for testing.
Definition: logging.cpp:96
bool createOptionsModel(bool resetSettings)
Create options model.
Definition: bitcoin.cpp:247
void requestShutdown()
Request core shutdown.
Definition: bitcoin.cpp:338
void windowShown(BitcoinGUI *window)
bool baseInitialize()
Basic initialization, before starting initialization/shutdown thread. Return true on success.
Definition: bitcoin.cpp:296
void createWindow(const NetworkStyle *networkStyle)
Create main window.
Definition: bitcoin.cpp:269
void parameterSetup()
parameter interaction/setup based on rules
Definition: bitcoin.cpp:316
void setupPlatformStyle()
Setup platform style.
Definition: bitcoin.cpp:217
void requestInitialize()
Request core initialization.
Definition: bitcoin.cpp:331
Bitcoin GUI main class.
Definition: bitcoingui.h:68
void consoleShown(RPCConsole *console)
Signal raised when RPC console shown.
static const NetworkStyle * instantiate(const ChainType networkId)
Get style associated with provided network id, or 0 if not known.
Local Bitcoin RPC console.
Definition: rpcconsole.h:42
BCLog::Logger & LogInstance()
Definition: logging.cpp:24
RAII helper to remove no-longer-pending callback.
Definition: apptests.h:34
~HandleCallback()
Destructor to shut down after the last expected callback completes.
Definition: apptests.cpp:105
std::string m_callback
Definition: apptests.h:35
AppTests & m_app_tests
Definition: apptests.h:36
assert(!tx.IsCoinBase())