Bitcoin Core  22.99.0
P2P Digital Currency
apptests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2020 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_MAC
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  fs::create_directories([] {
74  BasicTestingSetup test{CBaseChainParams::REGTEST}; // Create a temp data directory to backup the gui settings to
75  return gArgs.GetDataDirNet() / "blocks";
76  }());
77 
78  qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");
80  m_app.createOptionsModel(true /* reset settings */);
81  QScopedPointer<const NetworkStyle> style(NetworkStyle::instantiate(Params().NetworkIDString()));
83  m_app.createWindow(style.data());
85  expectCallback("guiTests");
88  m_app.exec();
90  m_app.exec();
91 
92  // Reset global state to avoid interfering with later tests.
94  AbortShutdown();
95 }
96 
99 {
100  HandleCallback callback{"guiTests", *this};
101  connect(window, &BitcoinGUI::consoleShown, this, &AppTests::consoleTests);
102  expectCallback("consoleTests");
103  QAction* action = window->findChild<QAction*>("openRPCConsoleAction");
104  action->activate(QAction::Trigger);
105 }
106 
109 {
110  HandleCallback callback{"consoleTests", *this};
111  TestRpcCommand(console);
112 }
113 
116 {
117  auto& callbacks = m_app_tests.m_callbacks;
118  auto it = callbacks.find(m_callback);
119  assert(it != callbacks.end());
120  callbacks.erase(it);
121  if (callbacks.empty()) {
122  m_app_tests.m_app.quit();
123  }
124 }
BitcoinApplication::setupPlatformStyle
void setupPlatformStyle()
Setup platform style.
Definition: bitcoin.cpp:224
assert
assert(!tx.IsCoinBase())
ArgsManager::GetDataDirNet
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:282
setup_common.h
AppTests::m_callbacks
std::multiset< std::string > m_callbacks
Set of pending callback names.
Definition: apptests.h:47
bitcoin.h
bitcoin-config.h
chainparams.h
AppTests::consoleTests
void consoleTests(RPCConsole *console)
Entry point for RPCConsole tests.
Definition: apptests.cpp:108
BCLog::Logger::DisconnectTestLogger
void DisconnectTestLogger()
Only for testing.
Definition: logging.cpp:84
shutdown.h
rpcconsole.h
BitcoinApplication::createWindow
void createWindow(const NetworkStyle *networkStyle)
Create main window.
Definition: bitcoin.cpp:259
AppTests::m_app
BitcoinApplication & m_app
Bitcoin application.
Definition: apptests.h:41
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
BitcoinApplication::requestInitialize
void requestInitialize()
Request core initialization.
Definition: bitcoin.cpp:320
RPCConsole
Local Bitcoin RPC console.
Definition: rpcconsole.h:38
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
BitcoinApplication::createOptionsModel
void createOptionsModel(bool resetSettings)
Create options model.
Definition: bitcoin.cpp:254
gArgs
ArgsManager gArgs
Definition: system.cpp:84
apptests.h
AppTests::expectCallback
void expectCallback(std::string callback)
Add expected callback name to list of pending callbacks.
Definition: apptests.h:30
key.h
AppTests::HandleCallback::~HandleCallback
~HandleCallback()
Destructor to shut down after the last expected callback completes.
Definition: apptests.cpp:115
NetworkStyle::instantiate
static const NetworkStyle * instantiate(const std::string &networkId)
Get style associated with provided network id, or 0 if not known.
Definition: networkstyle.cpp:80
AppTests::HandleCallback::m_app_tests
AppTests & m_app_tests
Definition: apptests.h:36
networkstyle.h
BitcoinApplication::requestShutdown
void requestShutdown()
Request core shutdown.
Definition: bitcoin.cpp:327
LogInstance
BCLog::Logger & LogInstance()
Definition: logging.cpp:17
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:559
BitcoinApplication::baseInitialize
bool baseInitialize()
Basic initialization, before starting initialization/shutdown thread. Return true on success.
Definition: bitcoin.cpp:287
bitcoingui.h
AppTests::guiTests
void guiTests(BitcoinGUI *window)
Entry point for BitcoinGUI tests.
Definition: apptests.cpp:98
BitcoinGUI::consoleShown
void consoleShown(RPCConsole *console)
Signal raised when RPC console shown.
BitcoinApplication::windowShown
void windowShown(BitcoinGUI *window)
AppTests::HandleCallback
RAII helper to remove no-longer-pending callback.
Definition: apptests.h:33
BitcoinGUI
Bitcoin GUI main class.
Definition: bitcoingui.h:68
BitcoinApplication::parameterSetup
void parameterSetup()
parameter interaction/setup based on rules
Definition: bitcoin.cpp:305
AppTests::appTests
void appTests()
Entry point for BitcoinApplication tests.
Definition: apptests.cpp:59
AbortShutdown
void AbortShutdown()
Clear shutdown flag.
Definition: shutdown.cpp:77
AppTests::HandleCallback::m_callback
std::string m_callback
Definition: apptests.h:35