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