Bitcoin Core  24.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 <qt/bitcoin.h>
10 #include <qt/bitcoingui.h>
11 #include <qt/networkstyle.h>
12 #include <qt/rpcconsole.h>
13 #include <shutdown.h>
14 #include <test/util/setup_common.h>
15 #include <validation.h>
16 
17 #if defined(HAVE_CONFIG_H)
18 #include <config/bitcoin-config.h>
19 #endif
20 
21 #include <QAction>
22 #include <QLineEdit>
23 #include <QRegularExpression>
24 #include <QScopedPointer>
25 #include <QSignalSpy>
26 #include <QString>
27 #include <QTest>
28 #include <QTextEdit>
29 #include <QtGlobal>
30 #include <QtTest/QtTestWidgets>
31 #include <QtTest/QtTestGui>
32 
33 namespace {
35 QString FindInConsole(const QString& output, const QString& pattern)
36 {
37  const QRegularExpression re(pattern);
38  return re.match(output).captured(1);
39 }
40 
42 void TestRpcCommand(RPCConsole* console)
43 {
44  QTextEdit* messagesWidget = console->findChild<QTextEdit*>("messagesWidget");
45  QLineEdit* lineEdit = console->findChild<QLineEdit*>("lineEdit");
46  QSignalSpy mw_spy(messagesWidget, &QTextEdit::textChanged);
47  QVERIFY(mw_spy.isValid());
48  QTest::keyClicks(lineEdit, "getblockchaininfo");
49  QTest::keyClick(lineEdit, Qt::Key_Return);
50  QVERIFY(mw_spy.wait(1000));
51  QCOMPARE(mw_spy.count(), 4);
52  const QString output = messagesWidget->toPlainText();
53  const QString pattern = QStringLiteral("\"chain\": \"(\\w+)\"");
54  QCOMPARE(FindInConsole(output, pattern), QString("regtest"));
55 }
56 } // namespace
57 
60 {
61 #ifdef Q_OS_MACOS
62  if (QApplication::platformName() == "minimal") {
63  // Disable for mac on "minimal" platform to avoid crashes inside the Qt
64  // framework when it tries to look up unimplemented cocoa functions,
65  // and fails to handle returned nulls
66  // (https://bugreports.qt.io/browse/QTBUG-49686).
67  QWARN("Skipping AppTests on mac build with 'minimal' platform set due to Qt bugs. To run AppTests, invoke "
68  "with 'QT_QPA_PLATFORM=cocoa test_bitcoin-qt' on mac, or else use a linux or windows build.");
69  return;
70  }
71 #endif
72 
73  qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");
75  QVERIFY(m_app.createOptionsModel(/*resetSettings=*/true));
76  QScopedPointer<const NetworkStyle> style(NetworkStyle::instantiate(Params().NetworkIDString()));
78  m_app.createWindow(style.data());
80  expectCallback("guiTests");
83  m_app.exec();
85  m_app.exec();
86 
87  // Reset global state to avoid interfering with later tests.
89  AbortShutdown();
90 }
91 
94 {
95  HandleCallback callback{"guiTests", *this};
96  connect(window, &BitcoinGUI::consoleShown, this, &AppTests::consoleTests);
97  expectCallback("consoleTests");
98  QAction* action = window->findChild<QAction*>("openRPCConsoleAction");
99  action->activate(QAction::Trigger);
100 }
101 
104 {
105  HandleCallback callback{"consoleTests", *this};
106  TestRpcCommand(console);
107 }
108 
111 {
112  auto& callbacks = m_app_tests.m_callbacks;
113  auto it = callbacks.find(m_callback);
114  assert(it != callbacks.end());
115  callbacks.erase(it);
116  if (callbacks.empty()) {
117  m_app_tests.m_app.exit(0);
118  }
119 }
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:93
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:59
void consoleTests(RPCConsole *console)
Entry point for RPCConsole tests.
Definition: apptests.cpp:103
void DisconnectTestLogger()
Only for testing.
Definition: logging.cpp:87
bool createOptionsModel(bool resetSettings)
Create options model.
Definition: bitcoin.cpp:269
void requestShutdown()
Request core shutdown.
Definition: bitcoin.cpp:360
void windowShown(BitcoinGUI *window)
bool baseInitialize()
Basic initialization, before starting initialization/shutdown thread. Return true on success.
Definition: bitcoin.cpp:318
void createWindow(const NetworkStyle *networkStyle)
Create main window.
Definition: bitcoin.cpp:291
void parameterSetup()
parameter interaction/setup based on rules
Definition: bitcoin.cpp:338
void setupPlatformStyle()
Setup platform style.
Definition: bitcoin.cpp:239
void requestInitialize()
Request core initialization.
Definition: bitcoin.cpp:353
Bitcoin GUI main class.
Definition: bitcoingui.h:70
void consoleShown(RPCConsole *console)
Signal raised when RPC console shown.
static const NetworkStyle * instantiate(const std::string &networkId)
Get style associated with provided network id, or 0 if not known.
Local Bitcoin RPC console.
Definition: rpcconsole.h:44
BCLog::Logger & LogInstance()
Definition: logging.cpp:20
void AbortShutdown()
Clear shutdown flag.
Definition: shutdown.cpp:79
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:110
std::string m_callback
Definition: apptests.h:35
AppTests & m_app_tests
Definition: apptests.h:36
assert(!tx.IsCoinBase())