Merge bitcoin-core/gui#335: test: Use QSignalSpy instead of QEventLoop

7eea659fc908e5edfc90c185a6958ed07ecf5cd4 qt, test: use qsignalspy instead of qeventloop (Jarol Rodriguez)

Pull request description:

  This PR refactors our GUI `apptests` to use [QSignalSpy](https://doc.qt.io/qt-5/qsignalspy.html) instead of [QEventLoop](https://doc.qt.io/qt-5/qeventloop.html).

  `QSignalSpy` is more appropriate for our GUI test's as it is purpose-built for testing emission of signals and sets up its own `QEventLoop` when the `wait` function is called.

ACKs for top commit:
  hebasto:
    ACK 7eea659fc908e5edfc90c185a6958ed07ecf5cd4, tested on Linux Mint 20.1 (Qt 5.12.8).
  promag:
    Code review ACK 7eea659fc908e5edfc90c185a6958ed07ecf5cd4.

Tree-SHA512: 3adddbcc5efd726302b606980c9923025c44bb8ee16cb8a183e633e423179c0822db66de9ccba20dc5124fff34af4151a379c9cd18130625c60789ce809ee6fd
This commit is contained in:
Hennadii Stepanov 2021-05-20 23:57:50 +03:00 committed by Konstantin Akimov
parent c52b75609b
commit ed56e28a7c
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524

View File

@ -20,10 +20,10 @@
#endif #endif
#include <QAction> #include <QAction>
#include <QEventLoop>
#include <QLineEdit> #include <QLineEdit>
#include <QScopedPointer> #include <QScopedPointer>
#include <QSettings> #include <QSettings>
#include <QSignalSpy>
#include <QTest> #include <QTest>
#include <QTextEdit> #include <QTextEdit>
#include <QtGlobal> #include <QtGlobal>
@ -34,13 +34,14 @@ namespace {
//! Call getblockchaininfo RPC and check first field of JSON output. //! Call getblockchaininfo RPC and check first field of JSON output.
void TestRpcCommand(RPCConsole* console) void TestRpcCommand(RPCConsole* console)
{ {
QEventLoop loop;
QTextEdit* messagesWidget = console->findChild<QTextEdit*>("messagesWidget"); QTextEdit* messagesWidget = console->findChild<QTextEdit*>("messagesWidget");
QObject::connect(messagesWidget, &QTextEdit::textChanged, &loop, &QEventLoop::quit);
QLineEdit* lineEdit = console->findChild<QLineEdit*>("lineEdit"); QLineEdit* lineEdit = console->findChild<QLineEdit*>("lineEdit");
QSignalSpy mw_spy(messagesWidget, &QTextEdit::textChanged);
QVERIFY(mw_spy.isValid());
QTest::keyClicks(lineEdit, "getblockchaininfo"); QTest::keyClicks(lineEdit, "getblockchaininfo");
QTest::keyClick(lineEdit, Qt::Key_Return); QTest::keyClick(lineEdit, Qt::Key_Return);
loop.exec(); QVERIFY(mw_spy.wait(1000));
QCOMPARE(mw_spy.count(), 2);
QString output = messagesWidget->toPlainText(); QString output = messagesWidget->toPlainText();
UniValue value; UniValue value;
value.read(output.right(output.size() - output.indexOf("{")).toStdString()); value.read(output.right(output.size() - output.indexOf("{")).toStdString());