|
| 1 | +// Copyright (c) 2018 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 <init.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 <validation.h> |
| 15 | + |
| 16 | +#if defined(HAVE_CONFIG_H) |
| 17 | +#include <config/bitcoin-config.h> |
| 18 | +#endif |
| 19 | +#ifdef ENABLE_WALLET |
| 20 | +#include <wallet/db.h> |
| 21 | +#endif |
| 22 | + |
| 23 | +#include <QAction> |
| 24 | +#include <QEventLoop> |
| 25 | +#include <QLineEdit> |
| 26 | +#include <QScopedPointer> |
| 27 | +#include <QTest> |
| 28 | +#include <QTextEdit> |
| 29 | +#include <QtGlobal> |
| 30 | +#if QT_VERSION >= 0x050000 |
| 31 | +#include <QtTest/QtTestWidgets> |
| 32 | +#endif |
| 33 | +#include <QtTest/QtTestGui> |
| 34 | +#include <new> |
| 35 | +#include <string> |
| 36 | +#include <univalue.h> |
| 37 | + |
| 38 | +namespace { |
| 39 | +//! Call getblockchaininfo RPC and check first field of JSON output. |
| 40 | +void TestRpcCommand(RPCConsole* console) |
| 41 | +{ |
| 42 | + QEventLoop loop; |
| 43 | + QTextEdit* messagesWidget = console->findChild<QTextEdit*>("messagesWidget"); |
| 44 | + QObject::connect(messagesWidget, &QTextEdit::textChanged, &loop, &QEventLoop::quit); |
| 45 | + QLineEdit* lineEdit = console->findChild<QLineEdit*>("lineEdit"); |
| 46 | + QTest::keyClicks(lineEdit, "getblockchaininfo"); |
| 47 | + QTest::keyClick(lineEdit, Qt::Key_Return); |
| 48 | + loop.exec(); |
| 49 | + QString output = messagesWidget->toPlainText(); |
| 50 | + UniValue value; |
| 51 | + value.read(output.right(output.size() - output.lastIndexOf(QChar::ObjectReplacementCharacter) - 1).toStdString()); |
| 52 | + QCOMPARE(value["chain"].get_str(), std::string("regtest")); |
| 53 | +} |
| 54 | +} // namespace |
| 55 | + |
| 56 | +//! Entry point for BitcoinApplication tests. |
| 57 | +void AppTests::appTests() |
| 58 | +{ |
| 59 | +#ifdef Q_OS_MAC |
| 60 | + if (QApplication::platformName() == "minimal") { |
| 61 | + // Disable for mac on "minimal" platform to avoid crashes inside the Qt |
| 62 | + // framework when it tries to look up unimplemented cocoa functions, |
| 63 | + // and fails to handle returned nulls |
| 64 | + // (https://bugreports.qt.io/browse/QTBUG-49686). |
| 65 | + QWARN("Skipping AppTests on mac build with 'minimal' platform set due to Qt bugs. To run AppTests, invoke " |
| 66 | + "with 'test_bitcoin-qt -platform cocoa' on mac, or else use a linux or windows build."); |
| 67 | + return; |
| 68 | + } |
| 69 | +#endif |
| 70 | + |
| 71 | + m_app.parameterSetup(); |
| 72 | + m_app.createOptionsModel(true /* reset settings */); |
| 73 | + QScopedPointer<const NetworkStyle> style( |
| 74 | + NetworkStyle::instantiate(QString::fromStdString(Params().NetworkIDString()))); |
| 75 | + m_app.setupPlatformStyle(); |
| 76 | + m_app.createWindow(style.data()); |
| 77 | + connect(&m_app, &BitcoinApplication::windowShown, this, &AppTests::guiTests); |
| 78 | + expectCallback("guiTests"); |
| 79 | + m_app.baseInitialize(); |
| 80 | + m_app.requestInitialize(); |
| 81 | + m_app.exec(); |
| 82 | + m_app.requestShutdown(); |
| 83 | + m_app.exec(); |
| 84 | + |
| 85 | + // Reset global state to avoid interfering with later tests. |
| 86 | + AbortShutdown(); |
| 87 | + UnloadBlockIndex(); |
| 88 | +} |
| 89 | + |
| 90 | +//! Entry point for BitcoinGUI tests. |
| 91 | +void AppTests::guiTests(BitcoinGUI* window) |
| 92 | +{ |
| 93 | + HandleCallback callback{"guiTests", *this}; |
| 94 | + connect(window, &BitcoinGUI::consoleShown, this, &AppTests::consoleTests); |
| 95 | + expectCallback("consoleTests"); |
| 96 | + QAction* action = window->findChild<QAction*>("openRPCConsoleAction"); |
| 97 | + action->activate(QAction::Trigger); |
| 98 | +} |
| 99 | + |
| 100 | +//! Entry point for RPCConsole tests. |
| 101 | +void AppTests::consoleTests(RPCConsole* console) |
| 102 | +{ |
| 103 | + HandleCallback callback{"consoleTests", *this}; |
| 104 | + TestRpcCommand(console); |
| 105 | +} |
| 106 | + |
| 107 | +//! Destructor to shut down after the last expected callback completes. |
| 108 | +AppTests::HandleCallback::~HandleCallback() |
| 109 | +{ |
| 110 | + auto& callbacks = m_app_tests.m_callbacks; |
| 111 | + auto it = callbacks.find(m_callback); |
| 112 | + assert(it != callbacks.end()); |
| 113 | + callbacks.erase(it); |
| 114 | + if (callbacks.empty()) { |
| 115 | + m_app_tests.m_app.quit(); |
| 116 | + } |
| 117 | +} |
0 commit comments