diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2020-11-16 17:31:19 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2021-04-22 15:56:44 +0200 |
commit | a0c95852fed72fb8b315b7cdea6f7b858d9099ea (patch) | |
tree | a66522ee88a520535417de134f777f25ca4bba83 /tests/auto/shared | |
parent | 2a65d8127dd53266342584a862ed7836d118c95e (diff) |
Add findViewDelegateItem function to visualtestutils.h
This helper function finds an item view delegate instance at a given
index, ensuring that the view is polished and positioned at the correct
index if necessary.
Change-Id: Ib133914df723251835be3812c1d0654c07a99f14
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'tests/auto/shared')
-rw-r--r-- | tests/auto/shared/util.pri | 2 | ||||
-rw-r--r-- | tests/auto/shared/visualtestutil.cpp | 29 | ||||
-rw-r--r-- | tests/auto/shared/visualtestutil.h | 11 |
3 files changed, 41 insertions, 1 deletions
diff --git a/tests/auto/shared/util.pri b/tests/auto/shared/util.pri index c2eb9f0a..3e270f06 100644 --- a/tests/auto/shared/util.pri +++ b/tests/auto/shared/util.pri @@ -1,4 +1,4 @@ -QT += testlib-private core-private gui-private qml-private quick-private quicktemplates2-private quickcontrols2 quickcontrols2-private +QT += testlib-private core-private gui-private qml-private quick-private qmltest quicktemplates2-private quickcontrols2 quickcontrols2-private HEADERS += $$PWD/visualtestutil.h \ $$PWD/util.h \ diff --git a/tests/auto/shared/visualtestutil.cpp b/tests/auto/shared/visualtestutil.cpp index 48201832..ff0d9e8e 100644 --- a/tests/auto/shared/visualtestutil.cpp +++ b/tests/auto/shared/visualtestutil.cpp @@ -96,6 +96,35 @@ void QQuickVisualTestUtil::centerOnScreen(QQuickWindow *window) window->setFramePosition(screenGeometry.center() - offset); } +/*! + \internal + + Finds the delegate at \c index belonging to \c itemView, using the given \c flags. + + If the view needs to be polished, the function will wait for it to be done before continuing, + and returns \c nullptr if the polish didn't happen. +*/ +QQuickItem *QQuickVisualTestUtil::findViewDelegateItem(QQuickItemView *itemView, int index, FindViewDelegateItemFlags flags) +{ + if (QQuickTest::qIsPolishScheduled(itemView)) { + if (!QQuickTest::qWaitForItemPolished(itemView)) { + qWarning() << "failed to polish" << itemView; + return nullptr; + } + } + + // Do this after the polish, just in case the count changes after a polish... + if (index <= -1 || index >= itemView->count()) { + qWarning() << "index" << index << "is out of bounds for" << itemView; + return nullptr; + } + + if (flags.testFlag(FindViewDelegateItemFlag::PositionViewAtIndex)) + itemView->positionViewAtIndex(index, QQuickItemView::Center); + + return itemView->itemAtIndex(index); +} + void QQuickVisualTestUtil::forEachControl(QQmlEngine *engine, const QString &sourcePath, const QString &targetPath, const QStringList &skipList, QQuickVisualTestUtil::ForEachCallback callback) { // We cannot use QQmlComponent to load QML files directly from the source tree. diff --git a/tests/auto/shared/visualtestutil.h b/tests/auto/shared/visualtestutil.h index 83c47b2d..1e530c70 100644 --- a/tests/auto/shared/visualtestutil.h +++ b/tests/auto/shared/visualtestutil.h @@ -43,6 +43,8 @@ #include <QtQuick/QQuickItem> #include <QtQml/QQmlExpression> #include <QtQuick/private/qquickitem_p.h> +#include <QtQuick/private/qquickitemview_p.h> +#include <QtQuickTest/quicktest.h> #include <QtQuickControls2/qquickstyle.h> #include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> #include <QtQuickTemplates2/private/qquickabstractbutton_p.h> @@ -116,6 +118,15 @@ namespace QQuickVisualTestUtil return items; } + enum class FindViewDelegateItemFlag { + None = 0x0, + PositionViewAtIndex = 0x01 + }; + Q_DECLARE_FLAGS(FindViewDelegateItemFlags, FindViewDelegateItemFlag) + + QQuickItem* findViewDelegateItem(QQuickItemView *itemView, int index, + FindViewDelegateItemFlags flags = FindViewDelegateItemFlag::PositionViewAtIndex); + class QQuickApplicationHelper { public: |