/**************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 as published by the Free Software ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include #include #include #include #include "../shared/viewtestutil.h" #include "../shared/visualtestutil.h" #include "../../shared/util.h" using namespace QQuickViewTestUtil; using namespace QQuickVisualTestUtil; class tst_QQuickListView2 : public QQmlDataTest { Q_OBJECT public: tst_QQuickListView2(); private slots: void urlListModel(); void dragDelegateWithMouseArea_data(); void dragDelegateWithMouseArea(); }; tst_QQuickListView2::tst_QQuickListView2() { } void tst_QQuickListView2::urlListModel() { QScopedPointer window(createView()); QVERIFY(window); QList model = { QUrl::fromLocalFile("abc"), QUrl::fromLocalFile("123") }; window->setInitialProperties({{ "model", QVariant::fromValue(model) }}); window->setSource(testFileUrl("urlListModel.qml")); window->show(); QVERIFY(QTest::qWaitForWindowExposed(window.data())); QQuickListView *view = window->rootObject()->property("view").value(); QVERIFY(view); if (QQuickTest::qIsPolishScheduled(view)) QVERIFY(QQuickTest::qWaitForItemPolished(view)); QCOMPARE(view->count(), model.size()); } static void dragListView(QWindow *window, QPoint *startPos, const QPoint &delta) { auto drag_helper = [&](QWindow *window, QPoint *startPos, const QPoint &d) { QPoint pos = *startPos; const int dragDistance = d.manhattanLength(); const QPoint unitVector(qBound(-1, d.x(), 1), qBound(-1, d.y(), 1)); for (int i = 0; i < dragDistance; ++i) { QTest::mouseMove(window, pos); pos += unitVector; } // Move to the final position pos = *startPos + d; QTest::mouseMove(window, pos); *startPos = pos; }; if (delta.manhattanLength() == 0) return; const int dragThreshold = QGuiApplication::styleHints()->startDragDistance(); const QPoint unitVector(qBound(-1, delta.x(), 1), qBound(-1, delta.y(), 1)); // go just beyond the drag theshold drag_helper(window, startPos, unitVector * (dragThreshold + 1)); drag_helper(window, startPos, unitVector); // next drag will actually scroll the listview drag_helper(window, startPos, delta); } void tst_QQuickListView2::dragDelegateWithMouseArea_data() { QTest::addColumn("layoutDirection"); for (int layDir = QQuickItemView::LeftToRight; layDir <= (int)QQuickItemView::VerticalBottomToTop; layDir++) { const char *enumValueName = QMetaEnum::fromType().valueToKey(layDir); QTest::newRow(enumValueName) << static_cast(layDir); } } void tst_QQuickListView2::dragDelegateWithMouseArea() { QFETCH(QQuickItemView::LayoutDirection, layoutDirection); QScopedPointer window(createView()); QVERIFY(window); window->setSource(testFileUrl("delegateWithMouseArea.qml")); window->show(); QVERIFY(QTest::qWaitForWindowExposed(window.data())); QQuickListView *listview = findItem(window->rootObject(), "list"); QVERIFY(listview != nullptr); const bool horizontal = layoutDirection < QQuickItemView::VerticalTopToBottom; listview->setOrientation(horizontal ? QQuickListView::Horizontal : QQuickListView::Vertical); if (horizontal) listview->setLayoutDirection(static_cast(layoutDirection)); else listview->setVerticalLayoutDirection(static_cast(layoutDirection)); QVERIFY(QQuickTest::qWaitForItemPolished(listview)); auto contentPosition = [&](QQuickListView *listview) { return (listview->orientation() == QQuickListView::Horizontal ? listview->contentX(): listview->contentY()); }; qreal expectedContentPosition = contentPosition(listview); QPoint startPos = (QPointF(listview->width(), listview->height())/2).toPoint(); QTest::mousePress(window.data(), Qt::LeftButton, Qt::NoModifier, startPos, 200); QPoint dragDelta(0, -10); if (layoutDirection == QQuickItemView::RightToLeft || layoutDirection == QQuickItemView::VerticalBottomToTop) dragDelta = -dragDelta; expectedContentPosition -= dragDelta.y(); if (horizontal) dragDelta = dragDelta.transposed(); dragListView(window.data(), &startPos, dragDelta); QTest::mouseRelease(window.data(), Qt::LeftButton, Qt::NoModifier, startPos, 200); // Wait 200 ms before we release to avoid trigger a flick // wait for the "fixup" animation to finish QVERIFY(QTest::qWaitFor([&]() { return !listview->isMoving();} )); QCOMPARE(contentPosition(listview), expectedContentPosition); } QTEST_MAIN(tst_QQuickListView2) #include "tst_qquicklistview2.moc"