From e8c7124768b0e3e556a8ae53629478284bbdca8e Mon Sep 17 00:00:00 2001 From: Vitaly Fanaskov Date: Mon, 12 Aug 2019 14:36:06 +0200 Subject: Introduce QSignalSpy constructor allows to spy on a meta method This functionality is especially convenient if meta-object system is heavily used in a test. For example, if you need to test a bunch of signals based on their names and/or argument types. Change-Id: I09a4ecbbd3d0859b5fd466d9dde7679804eb7614 Reviewed-by: Volker Hilsheimer --- .../doc/snippets/code/doc_src_qsignalspy.cpp | 48 ++++++++++++++++++++++ src/testlib/qsignalspy.h | 10 +++++ src/testlib/qsignalspy.qdoc | 22 ++++++++++ 3 files changed, 80 insertions(+) (limited to 'src/testlib') diff --git a/src/testlib/doc/snippets/code/doc_src_qsignalspy.cpp b/src/testlib/doc/snippets/code/doc_src_qsignalspy.cpp index a4513a55a9..37aba2715b 100644 --- a/src/testlib/doc/snippets/code/doc_src_qsignalspy.cpp +++ b/src/testlib/doc/snippets/code/doc_src_qsignalspy.cpp @@ -98,3 +98,51 @@ QVERIFY(spy.wait(1000)); QSignalSpy spy(myPushButton, &QPushButton::clicked); //! [6] +//! [7] +QObject object; +auto mo = object.metaObject(); +auto signalIndex = mo->indexOfSignal("objectNameChanged(QString)"); +auto signal = mo->method(signalIndex); + +QSignalSpy spy(&object, signal); +object.setObjectName("A new object name"); +QCOMPARE(spy.count(), 1); +//! [7] + +//! [8] +void tst_QWindow::writeMinMaxDimensionalProps_data() + QTest::addColumn("propertyIndex"); + + // Collect all relevant properties + static const auto mo = QWindow::staticMetaObject; + for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) { + auto property = mo.property(i); + + // ...that have type int + if (property.type() == QVariant::Int) { + static const QRegularExpression re("^minimum|maximum"); + const auto name = property.name(); + + // ...and start with "minimum" or "maximum" + if (re.match(name).hasMatch()) { + QTest::addRow("%s", name) << i; + } + } + } +} + +void tst_QWindow::writeMinMaxDimensionalProps() +{ + QFETCH(int, propertyIndex); + + auto property = QWindow::staticMetaObject.property(propertyIndex); + QVERIFY(property.isWritable()); + QVERIFY(property.hasNotifySignal()); + + QWindow window; + QSignalSpy spy(&window, property.notifySignal()); + + QVERIFY(property.write(&window, 42)); + QCOMPARE(spy.count(), 1); +} +//! [8] diff --git a/src/testlib/qsignalspy.h b/src/testlib/qsignalspy.h index 91c2d139a8..dc0c58044f 100644 --- a/src/testlib/qsignalspy.h +++ b/src/testlib/qsignalspy.h @@ -118,6 +118,16 @@ public: } #endif // Q_CLANG_QDOC + QSignalSpy(const QObject *obj, const QMetaMethod &signal) + : m_waiting(false) + { + if (isObjectValid(obj) && isSignalMetaMethodValid(signal) && + connectToSignal(obj, signal.methodIndex())) { + sig = signal.methodSignature(); + initArgs(signal, obj); + } + } + inline bool isValid() const { return !sig.isEmpty(); } inline QByteArray signal() const { return sig; } diff --git a/src/testlib/qsignalspy.qdoc b/src/testlib/qsignalspy.qdoc index 3352307d69..5ea6bc5dc7 100644 --- a/src/testlib/qsignalspy.qdoc +++ b/src/testlib/qsignalspy.qdoc @@ -86,6 +86,28 @@ \snippet code/doc_src_qsignalspy.cpp 6 */ +/*! \fn QSignalSpy(const QObject *obj, const QMetaMethod &signal) + \since 5.14 + + Constructs a new QSignalSpy that listens for emissions of the \a signal + from the QObject \a object. If QSignalSpy is not able to listen for a + valid signal (for example, because \a object is \nullptr or \a signal does + not denote a valid signal of \a object), an explanatory warning message + will be output using qWarning() and subsequent calls to \c isValid() will + return false. + + This constructor is convenient to use when Qt's meta-object system is + heavily used in a test. + + Basic usage example: + \snippet code/doc_src_qsignalspy.cpp 7 + + Imagine we need to check whether all properties of the QWindow class + that represent minimum and maximum dimensions are properly writable. + The following example demonstrates one of the approaches: + \snippet code/doc_src_qsignalspy.cpp 8 +*/ + /*! \fn QSignalSpy::isValid() const Returns \c true if the signal spy listens to a valid signal, otherwise false. -- cgit v1.2.3