aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quickcontrols/pointerhandlers/tst_pointerhandlers.cpp
blob: aa831752522121aefd62c61ffb4ace6aa11433ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtTest/QtTest>
#include <QtQuickTest/quicktest.h>

#include <QtQuick/qquickview.h>
#include <QtQuick/private/qquickmousearea_p.h>
#include <QtQuick/private/qquickpointerhandler_p.h>
#include <QtQuick/private/qquicktaphandler_p.h>

#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlcontext.h>

#include <QtQuickTemplates2/private/qquickbutton_p.h>

#include <QtGui/qguiapplication.h>
#include <QtGui/private/qpointingdevice_p.h>

#include <QtQuickTestUtils/private/qmlutils_p.h>
#include <QtQuickTestUtils/private/viewtestutils_p.h>
#include <QtQuickTestUtils/private/visualtestutils_p.h>

Q_LOGGING_CATEGORY(lcPointerTests, "qt.quick.pointer.tests")

using namespace QQuickViewTestUtils;
using namespace QQuickVisualTestUtils;

class tst_pointerhandlers : public QQmlDataTest
{
    Q_OBJECT
public:
    tst_pointerhandlers();

private slots:
    void hover_controlInsideControl();
    void hover_controlAndMouseArea();
    void buttonTapHandler_data();
    void buttonTapHandler();
    void buttonDragHandler_data();
    void buttonDragHandler();

private:
    QScopedPointer<QPointingDevice> touchscreen = QScopedPointer<QPointingDevice>(QTest::createTouchDevice());
};

tst_pointerhandlers::tst_pointerhandlers()
    : QQmlDataTest(QT_QMLTEST_DATADIR)
{
}

void tst_pointerhandlers::hover_controlInsideControl()
{
    // Test that if you move the mouse over a control that is
    // a child of another control, both controls end up hovered.
    // A control should basically not block (accept) hover events.
    QQuickView view(testFileUrl("controlinsidecontrol.qml"));
    QCOMPARE(view.status(), QQuickView::Ready);
    view.show();
    QVERIFY(QTest::qWaitForWindowExposed(&view));

    QQuickItem *rootItem = view.rootObject();
    QVERIFY(rootItem);
    QQuickWindow *window = rootItem->window();
    QVERIFY(window);

    const auto context = qmlContext(rootItem);
    auto outerButton = context->contextProperty("outerButton").value<QQuickButton *>();
    auto innerButton = context->contextProperty("innerButton").value<QQuickButton *>();
    QVERIFY(outerButton);
    QVERIFY(innerButton);

    const QPoint posInWindow(1, 1);
    const QPoint posOnOuterButton = rootItem->mapFromItem(outerButton, QPointF(0, 0)).toPoint();
    const QPoint posOnInnerButton = rootItem->mapFromItem(innerButton, QPointF(0, 0)).toPoint();

    // Start by moving the mouse to the window
    QTest::mouseMove(window, posInWindow);
    QCOMPARE(outerButton->isHovered(), false);
    QCOMPARE(innerButton->isHovered(), false);

    // Move the mouse over the outer control
    QTest::mouseMove(window, posOnOuterButton);
    QCOMPARE(outerButton->isHovered(), true);
    QCOMPARE(innerButton->isHovered(), false);

    // Move the mouse over the inner control
    QTest::mouseMove(window, posOnInnerButton);
    QCOMPARE(outerButton->isHovered(), true);
    QCOMPARE(innerButton->isHovered(), true);

    // Move the mouse over the outer control again
    QTest::mouseMove(window, posOnOuterButton);
    QCOMPARE(outerButton->isHovered(), true);
    QCOMPARE(innerButton->isHovered(), false);

    // Move the mouse outside all controls
    QTest::mouseMove(window, posInWindow);
    QCOMPARE(outerButton->isHovered(), false);
    QCOMPARE(innerButton->isHovered(), false);
}

void tst_pointerhandlers::hover_controlAndMouseArea()
{
    QQuickView view(testFileUrl("controlandmousearea.qml"));
    QCOMPARE(view.status(), QQuickView::Ready);
    view.show();
    QVERIFY(QTest::qWaitForWindowExposed(&view));

    QQuickItem *rootItem = view.rootObject();
    QVERIFY(rootItem);
    QQuickWindow *window = rootItem->window();
    QVERIFY(window);

    const auto context = qmlContext(rootItem);
    auto outerMouseArea = context->contextProperty("outerMouseArea").value<QQuickMouseArea *>();
    auto buttonInTheMiddle = context->contextProperty("buttonInTheMiddle").value<QQuickButton *>();
    auto innerMouseArea = context->contextProperty("innerMouseArea").value<QQuickMouseArea *>();
    QVERIFY(outerMouseArea);
    QVERIFY(buttonInTheMiddle);
    QVERIFY(innerMouseArea);

    const QPoint posInWindow(1, 1);
    const QPoint posOnOuterMouseArea = rootItem->mapFromItem(outerMouseArea, QPointF(0, 0)).toPoint();
    const QPoint posOnButtonInTheMiddle = rootItem->mapFromItem(buttonInTheMiddle, QPointF(0, 0)).toPoint();
    const QPoint posOnInnerMouseArea = rootItem->mapFromItem(innerMouseArea, QPointF(0, 0)).toPoint();

    // Start by moving the mouse to the window
    QTest::mouseMove(window, posInWindow);
    QCOMPARE(outerMouseArea->hovered(), false);
    QCOMPARE(buttonInTheMiddle->isHovered(), false);
    QCOMPARE(innerMouseArea->hovered(), false);

    // Move the mouse over the outer mousearea
    QTest::mouseMove(window, posOnOuterMouseArea);
    QCOMPARE(outerMouseArea->hovered(), true);
    QCOMPARE(buttonInTheMiddle->isHovered(), false);
    QCOMPARE(innerMouseArea->hovered(), false);

    // Move the mouse over the button in the middle
    QTest::mouseMove(window, posOnButtonInTheMiddle);
    QCOMPARE(outerMouseArea->hovered(), true);
    QCOMPARE(buttonInTheMiddle->isHovered(), true);
    QCOMPARE(innerMouseArea->hovered(), false);

    // Move the mouse over the inner mousearea
    QTest::mouseMove(window, posOnInnerMouseArea);
    QCOMPARE(outerMouseArea->hovered(), true);
    QCOMPARE(buttonInTheMiddle->isHovered(), true);
    QCOMPARE(innerMouseArea->hovered(), true);

    // Move the mouse over the button in the middle again
    QTest::mouseMove(window, posOnButtonInTheMiddle);
    QCOMPARE(outerMouseArea->hovered(), true);
    QCOMPARE(buttonInTheMiddle->isHovered(), true);
    QCOMPARE(innerMouseArea->hovered(), false);

    // Move the mouse over the outer mousearea again
    QTest::mouseMove(window, posOnOuterMouseArea);
    QCOMPARE(outerMouseArea->hovered(), true);
    QCOMPARE(buttonInTheMiddle->isHovered(), false);
    QCOMPARE(innerMouseArea->hovered(), false);

    // Move the mouse outside all items
    QTest::mouseMove(window, posInWindow);
    QCOMPARE(outerMouseArea->hovered(), false);
    QCOMPARE(buttonInTheMiddle->isHovered(), false);
    QCOMPARE(innerMouseArea->hovered(), false);
}

void tst_pointerhandlers::buttonTapHandler_data()
{
    QTest::addColumn<QPointingDevice::DeviceType>("deviceType");
    QTest::addColumn<Qt::MouseButton>("mouseButton");

    QTest::newRow("left mouse") << QPointingDevice::DeviceType::Mouse << Qt::LeftButton;
    QTest::newRow("right mouse") << QPointingDevice::DeviceType::Mouse << Qt::RightButton;
    QTest::newRow("touch") << QPointingDevice::DeviceType::TouchScreen << Qt::NoButton;
}

void tst_pointerhandlers::buttonTapHandler() // QTBUG-105609
{
    QFETCH(QPointingDevice::DeviceType, deviceType);
    QFETCH(Qt::MouseButton, mouseButton);

    QQuickView window;
    QVERIFY(QQuickTest::showView(window, testFileUrl("tapHandlerButton.qml")));

    QPointer<QQuickTapHandler> handler = window.rootObject()->findChild<QQuickTapHandler*>();
    QVERIFY(handler);
    handler->setAcceptedButtons(mouseButton);
    QQuickItem *target = handler->target();
    QVERIFY(target);
    QSignalSpy tappedSpy(handler, &QQuickTapHandler::tapped);
    QSignalSpy clickedSpy(target, SIGNAL(clicked())); // avoid #include for this signal

    const QPoint pos(10, 10);
    switch (static_cast<QPointingDevice::DeviceType>(deviceType)) {
    case QPointingDevice::DeviceType::Mouse:
        // click it
        QTest::mouseClick(&window, mouseButton, Qt::NoModifier, pos);
        QTRY_COMPARE(clickedSpy.size(), 1); // perhaps Button should not react to right-click, but it does
        QCOMPARE(tappedSpy.size(), 1);
        break;

    case QPointingDevice::DeviceType::TouchScreen: {
        // tap it
        QTest::QTouchEventSequence touch = QTest::touchEvent(&window, touchscreen.data());
        touch.press(0, pos, &window).commit();
        QTRY_COMPARE(target->property("pressed").toBool(), true);
        touch.release(0, pos, &window).commit();
        QTRY_COMPARE(clickedSpy.size(), 1);
        QCOMPARE(tappedSpy.size(), 1);
        break;
    }
    default:
        break;
    }
    QCOMPARE(handler->isPressed(), false);
}

void tst_pointerhandlers::buttonDragHandler_data()
{
    QTest::addColumn<QPointingDevice::DeviceType>("deviceType");

    QTest::newRow("mouse") << QPointingDevice::DeviceType::Mouse;
    QTest::newRow("touch") << QPointingDevice::DeviceType::TouchScreen;
}

void tst_pointerhandlers::buttonDragHandler() // QTBUG-105610
{
    QFETCH(QPointingDevice::DeviceType, deviceType);

    QQuickView window;
    QVERIFY(QQuickTest::showView(window, testFileUrl("draggableButton.qml")));

    const int dragThreshold = QGuiApplication::styleHints()->startDragDistance();

    QPointer<QQuickPointerHandler> handler = window.rootObject()->findChild<QQuickPointerHandler*>();
    QVERIFY(handler);
    QQuickItem *target = handler->target();
    QVERIFY(target);
    QSignalSpy clickedSpy(target, SIGNAL(clicked()));

    QPoint dragPos(10, 10);
    switch (static_cast<QPointingDevice::DeviceType>(deviceType)) {
    case QPointingDevice::DeviceType::Mouse:
        // click it
        QTest::mouseClick(&window, Qt::LeftButton, Qt::NoModifier, dragPos);
        QTRY_COMPARE(clickedSpy.size(), 1);

        // drag it
        QTest::mousePress(&window, Qt::LeftButton, Qt::NoModifier, dragPos);
        dragPos += QPoint(dragThreshold, dragThreshold);
        QTest::mouseMove(&window, dragPos);
        dragPos += QPoint(1, 1);
        QTest::mouseMove(&window, dragPos);
        qCDebug(lcPointerTests) << handler << "dragged" << target << "to" << target->position();
        QTRY_VERIFY(handler->active());
        QTest::mouseRelease(&window, Qt::LeftButton, Qt::NoModifier, dragPos);
        break;

    case QPointingDevice::DeviceType::TouchScreen: {
        QTest::QTouchEventSequence touch = QTest::touchEvent(&window, touchscreen.data());

        // tap it
        touch.press(0, dragPos, &window).commit();
        touch.release(0, dragPos, &window).commit();
        QTRY_COMPARE(clickedSpy.size(), 1);

        // drag it
        touch.press(0, dragPos, &window).commit();
        dragPos += QPoint(dragThreshold, dragThreshold);
        touch.move(0, dragPos, &window).commit();
        dragPos += QPoint(1, 1);
        touch.move(0, dragPos, &window).commit();
        qCDebug(lcPointerTests) << handler << "dragged" << target << "to" << target->position();
        QTRY_VERIFY(handler->active());
        touch.release(0, dragPos, &window).commit();
        break;
    }
    default:
        break;
    }
    QTRY_COMPARE(handler->active(), false);

    // click it again
    QTest::mouseClick(&window, Qt::LeftButton, Qt::NoModifier, dragPos);
    QTRY_COMPARE(clickedSpy.size(), 2);
}

QTEST_MAIN(tst_pointerhandlers)

#include "tst_pointerhandlers.moc"