| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2019 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 | #include <QtTest/QtTest> | 
| 29 | #include <qsignalspy.h> | 
| 30 | #include <QtQml/qqmlengine.h> | 
| 31 | #include <QtQml/qqmlcomponent.h> | 
| 32 | #include <QtQuick/qquickview.h> | 
| 33 | #include <QtQuick/private/qquickdraghandler_p.h> | 
| 34 | #include "../../shared/util.h" | 
| 35 | #include "../shared/viewtestutil.h" | 
| 36 |  | 
| 37 | class tst_qquickboundaryrule : public QQmlDataTest | 
| 38 | { | 
| 39 |     Q_OBJECT | 
| 40 | public: | 
| 41 |     tst_qquickboundaryrule() {} | 
| 42 |  | 
| 43 | private slots: | 
| 44 |     void init() { qApp->processEvents(); }  //work around animation timer bug (QTBUG-22865) | 
| 45 |     void dragHandler(); | 
| 46 | }; | 
| 47 |  | 
| 48 | void tst_qquickboundaryrule::dragHandler() | 
| 49 | { | 
| 50 |     QQuickView window; | 
| 51 |     QByteArray errorMessage; | 
| 52 |     QVERIFY2(QQuickTest::initView(window, testFileUrl("dragHandler.qml" ), true, &errorMessage), errorMessage.constData()); | 
| 53 |     window.show(); | 
| 54 |     QVERIFY(QTest::qWaitForWindowExposed(&window)); | 
| 55 |     QQuickItem *target = window.rootObject(); | 
| 56 |     QVERIFY(target); | 
| 57 |     QQuickDragHandler *dragHandler = target->findChild<QQuickDragHandler*>(); | 
| 58 |     QVERIFY(dragHandler); | 
| 59 |     QObject *boundaryRule = target->findChild<QObject *>(aName: QLatin1String("boundaryRule" )); | 
| 60 |     QVERIFY(boundaryRule); | 
| 61 |     QSignalSpy overshootChangedSpy(boundaryRule, SIGNAL(currentOvershootChanged())); | 
| 62 |  | 
| 63 |     QPoint p1(10, 10); | 
| 64 |     QTest::mousePress(window: &window, button: Qt::LeftButton, stateKey: Qt::NoModifier, pos: p1); | 
| 65 |     // unrestricted drag | 
| 66 |     p1 += QPoint(100, 0); | 
| 67 |     QTest::mouseMove(window: &window, pos: p1); | 
| 68 |     QTRY_VERIFY(dragHandler->active()); | 
| 69 |     QCOMPARE(target->position().x(), 100); | 
| 70 |     bool ok = false; | 
| 71 |     QCOMPARE(boundaryRule->property("currentOvershoot" ).toReal(&ok), 0); | 
| 72 |     QVERIFY(ok); | 
| 73 |     QCOMPARE(boundaryRule->property("peakOvershoot" ).toReal(&ok), 0); | 
| 74 |     QVERIFY(ok); | 
| 75 |     QCOMPARE(overshootChangedSpy.count(), 0); | 
| 76 |     // restricted drag: halfway into overshoot | 
| 77 |     p1 += QPoint(20, 0); | 
| 78 |     QTest::mouseMove(window: &window, pos: p1); | 
| 79 |     QCOMPARE(target->position().x(), 117.5); | 
| 80 |     QCOMPARE(boundaryRule->property("currentOvershoot" ).toReal(), 20); | 
| 81 |     QCOMPARE(boundaryRule->property("peakOvershoot" ).toReal(), 20); | 
| 82 |     QCOMPARE(overshootChangedSpy.count(), 1); | 
| 83 |     // restricted drag: maximum overshoot | 
| 84 |     p1 += QPoint(80, 0); | 
| 85 |     QTest::mouseMove(window: &window, pos: p1); | 
| 86 |     QCOMPARE(target->position().x(), 140); | 
| 87 |     QCOMPARE(boundaryRule->property("currentOvershoot" ).toReal(), 100); | 
| 88 |     QCOMPARE(boundaryRule->property("peakOvershoot" ).toReal(), 100); | 
| 89 |     QCOMPARE(overshootChangedSpy.count(), 2); | 
| 90 |     // release and let it return to bounds | 
| 91 |     QTest::mouseRelease(window: &window, button: Qt::LeftButton, stateKey: Qt::NoModifier, pos: p1); | 
| 92 |     QTRY_COMPARE(dragHandler->active(), false); | 
| 93 |     QTRY_COMPARE(overshootChangedSpy.count(), 3); | 
| 94 |     QCOMPARE(boundaryRule->property("currentOvershoot" ).toReal(&ok), 0); | 
| 95 |     QVERIFY(ok); | 
| 96 |     QCOMPARE(boundaryRule->property("peakOvershoot" ).toReal(&ok), 0); | 
| 97 |     QVERIFY(ok); | 
| 98 |     QCOMPARE(target->position().x(), 100); | 
| 99 | } | 
| 100 |  | 
| 101 | QTEST_MAIN(tst_qquickboundaryrule) | 
| 102 |  | 
| 103 | #include "tst_qquickboundaryrule.moc" | 
| 104 |  |