QQuickTest Namespace

The QQuickTest namespace contains all the functions and macros related to Qt Quick Test. 更多...

头: #include <QtQuickTest>

函数

bool qIsPolishScheduled (const QQuickItem * item )
(从 6.4 起) bool qIsPolishScheduled (const QQuickWindow * window )
(从 6.4 起) bool qWaitForPolish (const QQuickItem * item , int timeout = defaultTimeout)
(从 6.4 起) bool qWaitForPolish (const QQuickWindow * window , int timeout = defaultTimeout)

(从 6.10 起) QTRY_VERIFY_ACTIVE_FOCUS ( item )
QUICK_TEST_MAIN ( name )
QUICK_TEST_MAIN_WITH_SETUP ( name , QuickTestSetupClass )
(从 6.10 起) QVERIFY_ACTIVE_FOCUS ( item )

详细描述

Introduction to Qt Quick Test for information about how to write Qt Quick unit tests.

To link to the Qt Quick Test C++ library, see Qt Quick Test C++ API .

另请参阅 在 QML 测试之前执行 C++ .

函数文档编制

bool QQuickTest:: qIsPolishScheduled (const QQuickItem * item )

返回 true if updatePolish () has not been called on item since the last call to polish (),否则返回 false .

When assigning values to properties in QML, any layouting the item must do as a result of the assignment might not take effect immediately, but can instead be postponed until the item is polished. For these cases, you can use this function to ensure that the item has been polished before the execution of the test continues. For example:

QVERIFY(QQuickTest::qIsPolishScheduled(item));
QVERIFY(QQuickTest::qWaitForItemPolished(item));
					

Without the call to qIsPolishScheduled() above, the call to qWaitForItemPolished() might see that no polish was scheduled and therefore pass instantly, assuming that the item had already been polished. This function makes it obvious why an item wasn't polished and allows tests to fail early under such circumstances.

The QML equivalent of this function is isPolishScheduled ().

另请参阅 QQuickItem::polish () 和 QQuickItem::updatePolish ().

[since 6.4] bool QQuickTest:: qIsPolishScheduled (const QQuickWindow * window )

返回 true if there are any items managed by this window for which qIsPolishScheduled(item) 返回 true ,否则返回 false .

For example, if an item somewhere within the scene may or may not be polished, but you need to wait for it if it is, you can use the following code:

if (QQuickTest::qIsPolishScheduled(window))
    QVERIFY(QQuickTest::qWaitForPolish(window));
					

The QML equivalent of this function is isPolishScheduled ().

此函数重载 QQuickTest::qIsPolishScheduled ().

该函数在 Qt 6.4 引入。

另请参阅 QQuickItem::polish (), QQuickItem::updatePolish (),和 QQuickTest::qWaitForPolish ().

[since 6.4] bool QQuickTest:: qWaitForPolish (const QQuickItem * item , int timeout = defaultTimeout)

等待 timeout milliseconds or until updatePolish () has been called on item .

返回 true if updatePolish() was called on itemtimeout milliseconds, otherwise returns false .

该函数在 Qt 6.4 引入。

另请参阅 QQuickItem::polish (), QQuickItem::updatePolish (),和 QQuickTest::qIsPolishScheduled ().

[since 6.4] bool QQuickTest:: qWaitForPolish (const QQuickWindow * window , int timeout = defaultTimeout)

等待 timeout milliseconds or until qIsPolishScheduled(item) 返回 false for all items managed by window .

返回 true if qIsPolishScheduled(item) returns false for all items within timeout milliseconds, otherwise returns false .

The QML equivalent of this function is waitForPolish ().

该函数在 Qt 6.4 引入。

另请参阅 QQuickItem::polish (), QQuickItem::updatePolish (),和 QQuickTest::qIsPolishScheduled ().

宏文档编制

[since 6.10] QTRY_VERIFY_ACTIVE_FOCUS ( item )

This macro does the same check as QVERIFY_ACTIVE_FOCUS with item , but repeatedly, until either the condition becomes true or the timeout (in milliseconds) is reached. Between each evaluation, events will be processed. If the timeout is reached, a failure is recorded in the test log and the test won't be executed further.

注意: This macro can only be used in a test function that is invoked by the test framework.

This macro was introduced in Qt 6.10.

QUICK_TEST_MAIN ( name )

Sets up the entry point for a Qt Quick Test application. The name argument uniquely identifies this set of tests.

#include <QtQuickTest>
QUICK_TEST_MAIN(example)
					

注意: The macro assumes that your test sources are in the current directory, unless the QUICK_TEST_SOURCE_DIR environment variable is set.

另请参阅 QUICK_TEST_MAIN_WITH_SETUP () 和 Running Qt Quick Tests .

QUICK_TEST_MAIN_WITH_SETUP ( name , QuickTestSetupClass )

Sets up the entry point for a Qt Quick Test application. The name argument uniquely identifies this set of tests.

This macro is identical to QUICK_TEST_MAIN (), except that it takes an additional argument QuickTestSetupClass , the type of a QObject -derived class which will be instantiated. With this class it is possible to define additional setup code to execute before running the QML test.

注意: The macro assumes that your test sources are in the current directory, unless the QUICK_TEST_SOURCE_DIR environment variable is set.

The following snippet demonstrates the use of this macro:

// src_qmltest_qquicktest.cpp
#include <QtQuickTest>
#include <QQmlEngine>
#include <QQmlContext>
#include <QGuiApplication>
class Setup : public QObject
{
    Q_OBJECT
public:
    Setup() {}
public slots:
    void applicationAvailable()
    {
        // Initialization that only requires the QGuiApplication object to be available
    }
    void qmlEngineAvailable(QQmlEngine *engine)
    {
        // Initialization requiring the QQmlEngine to be constructed
        engine->rootContext()->setContextProperty("myContextProperty", QVariant(true));
    }
    void cleanupTestCase()
    {
        // Implement custom resource cleanup
    }
};
QUICK_TEST_MAIN_WITH_SETUP(mytest, Setup)
#include "src_qmltest_qquicktest.moc"
					

另请参阅 QUICK_TEST_MAIN () 和 Running Qt Quick Tests .

[since 6.10] QVERIFY_ACTIVE_FOCUS ( item )

校验是否 item (which must be derived from QQuickItem ) has active focus . If it does, execution continues. If not, a failure is recorded in the test log and the test won't be executed further. The failure message contains focus-specific information that is relevant for diagnosing the cause of the failure.

注意: This macro can only be used in a test function that is invoked by the test framework.

This macro was introduced in Qt 6.10.