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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtGui/private/qguiapplication_p.h>
#include <QtGui/qpa/qplatformtheme.h>
#include <QtTest/qsignalspy.h>
#include <QtTest/qtest.h>
#include <QtQuick/qquickview.h>
#include <QtQuick/private/qquickmousearea_p.h>
#include <QtQuick/private/qquicktaphandler_p.h>
#include <QtQuickTestUtils/private/viewtestutils_p.h>
#include <QtQuickTestUtils/private/visualtestutils_p.h>
#include <QtQuickControlsTestUtils/private/controlstestutils_p.h>
#include <QtQuickControlsTestUtils/private/qtest_quickcontrols_p.h>
#include <QtQuickTemplates2/private/qquickmenu_p.h>
#include <QtQuickTemplates2/private/qquickmenu_p_p.h>
#include <QtQuickTemplates2/private/qquickmenuitem_p_p.h>
#include <QtQuickTemplates2/private/qquickpopupwindow_p_p.h>
#include <QtQuickTemplates2/private/qquicktextarea_p.h>
#include <QtQuickTest/quicktest.h>
using namespace QQuickControlsTestUtils;
using namespace QQuickVisualTestUtils;
class tst_QQuickContextMenu : public QQmlDataTest
{
Q_OBJECT
public:
tst_QQuickContextMenu();
private slots:
void initTestCase() override;
void customContextMenu_data();
void customContextMenu();
void sharedContextMenu();
void tapHandler();
void notAttachedToItem();
void nullMenu();
void idOnMenu();
void createOnRequested_data();
void createOnRequested();
void drawerShouldntPreventOpening();
void explicitMenuPreventsBuiltInMenu();
void menuItemShouldntTriggerOnRelease();
void textControlsMenuKey();
void mouseAreaUnderTextArea();
private:
bool contextMenuTriggeredOnRelease = false;
};
tst_QQuickContextMenu::tst_QQuickContextMenu()
: QQmlDataTest(QT_QMLTEST_DATADIR, FailOnWarningsPolicy::FailOnWarnings)
{
}
void tst_QQuickContextMenu::initTestCase()
{
QQmlDataTest::initTestCase();
// Can't test native menus with QTest.
QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuWindows);
contextMenuTriggeredOnRelease = QGuiApplicationPrivate::platformTheme()->themeHint(
QPlatformTheme::ContextMenuOnMouseRelease).toBool();
}
void tst_QQuickContextMenu::customContextMenu_data()
{
QTest::addColumn<QString>("qmlFileName");
QTest::addRow("Rectangle") << "customContextMenuOnRectangle.qml";
QTest::addRow("Label") << "customContextMenuOnLabel.qml";
QTest::addRow("Control") << "customContextMenuOnControl.qml";
QTest::addRow("NestedRectangle") << "customContextMenuOnNestedRectangle.qml";
QTest::addRow("Pane") << "customContextMenuOnPane.qml";
}
void tst_QQuickContextMenu::customContextMenu()
{
QFETCH(QString, qmlFileName);
QQuickApplicationHelper helper(this, qmlFileName);
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
auto *tomatoItem = window->findChild<QQuickItem *>("tomato");
QVERIFY(tomatoItem);
const QPoint &tomatoCenter = mapCenterToWindow(tomatoItem);
QTest::mousePress(window, Qt::RightButton, Qt::NoModifier, tomatoCenter);
// Due to the menu property being deferred, the Menu isn't created until
// the context menu event is received, so we can't look for it before the press.
QQuickMenu *menu = window->findChild<QQuickMenu *>();
if (!contextMenuTriggeredOnRelease) {
QVERIFY(menu);
QTRY_VERIFY(menu->isOpened());
} else {
// It's triggered on press, so it shouldn't exist yet.
QVERIFY(!menu);
}
QTest::mouseRelease(window, Qt::RightButton, Qt::NoModifier, tomatoCenter);
if (contextMenuTriggeredOnRelease)
menu = window->findChild<QQuickMenu *>();
#ifdef Q_OS_WIN
if (qgetenv("QTEST_ENVIRONMENT").split(' ').contains("ci"))
QSKIP("Menu fails to open on Windows (QTBUG-132436)");
#endif
QVERIFY(menu);
QTRY_VERIFY(menu->isOpened());
// Popups are positioned relative to their parent, and it should be opened at the center:
// width (100) / 2 = 50
#ifdef Q_OS_ANDROID
if (qgetenv("QTEST_ENVIRONMENT").split(' ').contains("ci"))
QEXPECT_FAIL("", "This test fails on Android 14 in CI, but passes locally with 15", Abort);
#endif
QCOMPARE(menu->position(), QPoint(50, 50));
}
void tst_QQuickContextMenu::sharedContextMenu()
{
QQuickApplicationHelper helper(this, "sharedContextMenuOnRectangle.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
auto *tomato = window->findChild<QQuickItem *>("tomato");
QVERIFY(tomato);
auto *reallyRipeTomato = window->findChild<QQuickItem *>("really ripe tomato");
QVERIFY(reallyRipeTomato);
// Check that parentItem allows users to distinguish which item triggered a menu.
const QPoint &tomatoCenter = mapCenterToWindow(tomato);
QTest::mouseClick(window, Qt::RightButton, Qt::NoModifier, tomatoCenter);
// There should only be one menu.
auto menus = window->findChildren<QQuickMenu *>();
QCOMPARE(menus.count(), 1);
QPointer<QQuickMenu> menu = menus.first();
#ifdef Q_OS_WIN
if (qgetenv("QTEST_ENVIRONMENT").split(' ').contains("ci"))
QSKIP("Menu fails to open on Windows (QTBUG-132436)");
#endif
QTRY_VERIFY(menu->isOpened());
QCOMPARE(menu->parentItem(), tomato);
QCOMPARE(menu->itemAt(0)->property("text").toString(), "Eat tomato");
menu->close();
QTRY_VERIFY(!menu->isVisible());
const QPoint &reallyRipeTomatoCenter = mapCenterToWindow(reallyRipeTomato);
QTest::mouseClick(window, Qt::RightButton, Qt::NoModifier, reallyRipeTomatoCenter);
QVERIFY(menu);
menus = window->findChildren<QQuickMenu *>();
QCOMPARE(menus.count(), 1);
QCOMPARE(menus.last(), menu);
QTRY_VERIFY(menu->isOpened());
QCOMPARE(menu->parentItem(), reallyRipeTomato);
QCOMPARE(menu->itemAt(0)->property("text").toString(), "Eat really ripe tomato");
}
// After 70c61b12efe9d1faf24063b63cf5a69414d45cea in qtbase, accepting a press/release will not
// prevent an item beneath the accepting item from getting a context menu event.
// This test was originally written before that, and would verify that only the handler
// got the event. Now it checks that both received events.
void tst_QQuickContextMenu::tapHandler()
{
QQuickApplicationHelper helper(this, "tapHandler.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
const QSignalSpy contextMenuOpenedSpy(window, SIGNAL(contextMenuOpened()));
QVERIFY(contextMenuOpenedSpy.isValid());
const auto *tapHandler = window->findChild<QObject *>("tapHandler");
QVERIFY(tapHandler);
const QSignalSpy tappedSpy(tapHandler, SIGNAL(tapped(QEventPoint,Qt::MouseButton)));
QVERIFY(tappedSpy.isValid());
const QPoint &windowCenter = mapCenterToWindow(window->contentItem());
QTest::mouseClick(window, Qt::RightButton, Qt::NoModifier, windowCenter);
// First check that the menu was actually created, as this is an easier-to-understand
// failure message than a signal spy count mismatch.
const auto *menu = window->findChild<QQuickMenu *>();
QVERIFY(menu);
QTRY_COMPARE(contextMenuOpenedSpy.count(), 1);
QCOMPARE(tappedSpy.count(), 1);
}
void tst_QQuickContextMenu::notAttachedToItem()
{
// Should warn but shouldn't crash.
QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*ContextMenu must be attached to an Item"));
QQuickApplicationHelper helper(this, "notAttachedToItem.qml");
QVERIFY2(helper.ready, helper.failureMessage());
}
void tst_QQuickContextMenu::nullMenu()
{
QQuickApplicationHelper helper(this, "nullMenu.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
// Shouldn't crash or warn.
const QPoint &windowCenter = mapCenterToWindow(window->contentItem());
QTest::mouseClick(window, Qt::RightButton, Qt::NoModifier, windowCenter);
auto *menu = window->findChild<QQuickMenu *>();
QVERIFY(!menu);
}
void tst_QQuickContextMenu::idOnMenu()
{
QQuickApplicationHelper helper(this, "idOnMenu.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
// Giving the menu an id prevents deferred execution, but the menu should still work.
const QPoint &windowCenter = mapCenterToWindow(window->contentItem());
QTest::mouseClick(window, Qt::RightButton, Qt::NoModifier, windowCenter);
auto *menu = window->findChild<QQuickMenu *>();
QVERIFY(menu);
QTRY_VERIFY(menu->isOpened());
}
void tst_QQuickContextMenu::createOnRequested_data()
{
QTest::addColumn<bool>("programmaticShow");
QTest::addRow("auto") << false;
QTest::addRow("manual") << true;
}
void tst_QQuickContextMenu::createOnRequested()
{
QFETCH(bool, programmaticShow);
QQuickView window;
QVERIFY(QQuickTest::showView(window, testFileUrl("customContextMenuOnRequested.qml")));
auto *tomatoItem = window.findChild<QQuickItem *>("tomato");
QVERIFY(tomatoItem);
const QPoint &tomatoCenter = mapCenterToWindow(tomatoItem);
window.rootObject()->setProperty("showItToo", programmaticShow);
// On press or release (depending on QPlatformTheme::ContextMenuOnMouseRelease),
// ContextMenu.onRequested(pos) should create a standalone custom context menu.
// If programmaticShow, it will call popup() too; if not, QQuickContextMenu
// will show it. Either way, it should still be open after the release.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, tomatoCenter);
QQuickMenu *menu = window.findChild<QQuickMenu *>();
QVERIFY(menu);
QTRY_VERIFY(menu->isOpened());
QCOMPARE(window.rootObject()->property("pressPos").toPoint(), tomatoCenter);
}
// Drawer shouldn't prevent right clicks from opening ContextMenu: QTBUG-132765.
void tst_QQuickContextMenu::drawerShouldntPreventOpening()
{
QQuickApplicationHelper helper(this, "drawer.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
const QPoint &windowCenter = mapCenterToWindow(window->contentItem());
QTest::mouseClick(window, Qt::RightButton, Qt::NoModifier, windowCenter);
auto *menu = window->findChild<QQuickMenu *>();
QVERIFY(menu);
QTRY_VERIFY(menu->isOpened());
}
void tst_QQuickContextMenu::explicitMenuPreventsBuiltInMenu()
{
QQuickApplicationHelper helper(this, "tapHandlerMenuOverride.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
auto *textArea = window->findChild<QQuickItem *>("textArea");
QVERIFY(textArea);
auto *tapHandler = window->findChild<QQuickTapHandler *>();
QVERIFY(tapHandler);
const QSignalSpy tapHandlerTappedSpy(tapHandler, &QQuickTapHandler::tapped);
auto *windowMenu = window->findChild<QQuickMenu *>("windowMenu");
QVERIFY(windowMenu);
const QPoint &windowCenter = mapCenterToWindow(window->contentItem());
QTest::mouseClick(window, Qt::RightButton, Qt::NoModifier, windowCenter);
// The menu that has opened is the window's menu; TextArea's built-in menu has not been instantiated
QCOMPARE(textArea->findChild<QQuickMenu *>(), nullptr);
QTRY_VERIFY(windowMenu->isOpened());
QCOMPARE(tapHandlerTappedSpy.count(), 1);
}
void tst_QQuickContextMenu::menuItemShouldntTriggerOnRelease() // QTBUG-133302
{
#ifdef Q_OS_ANDROID
QSKIP("Crashes on android. See QTBUG-137400");
#endif
QSKIP("Test function has been flaky from the start, QTBUG-141406.");
QQuickApplicationHelper helper(this, "windowedContextMenuOnControl.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
auto *tomatoItem = window->findChild<QQuickItem *>("tomato");
QVERIFY(tomatoItem);
QSignalSpy triggeredSpy(window, SIGNAL(triggered(QObject*)));
QVERIFY(triggeredSpy.isValid());
const QPoint &tomatoCenter = mapCenterToWindow(tomatoItem);
QTest::mousePress(window, Qt::RightButton, Qt::NoModifier, tomatoCenter);
auto *menu = window->findChild<QQuickMenu *>();
QQuickMenuItem *firstMenuItem = nullptr;
QQuickMenuItemPrivate *firstMenuItemPriv = nullptr;
if (!contextMenuTriggeredOnRelease) {
QVERIFY(menu);
QTRY_VERIFY(menu->isOpened());
firstMenuItem = qobject_cast<QQuickMenuItem *>(menu->itemAt(0));
QVERIFY(firstMenuItem);
// The mouse press event alone must not highlight the menu item under the mouse.
// (A mouse move could do that, while holding the button; or, another mouse press/release pair afterwards.)
QCOMPARE(firstMenuItem->isHighlighted(), false);
firstMenuItemPriv = static_cast<QQuickMenuItemPrivate *>(QQuickMenuItemPrivate::get(firstMenuItem));
QVERIFY(firstMenuItemPriv);
} else {
// It's triggered on press, so it shouldn't exist yet.
QVERIFY(!menu);
}
// After release, the menu should still be open, and no triggered() signal received
// (because the user did not intentionally drag over an item and release).
QTest::mouseRelease(window, Qt::RightButton, Qt::NoModifier, tomatoCenter);
if (contextMenuTriggeredOnRelease) {
menu = window->findChild<QQuickMenu *>();
QVERIFY(menu);
QTRY_VERIFY(menu->isOpened());
firstMenuItem = qobject_cast<QQuickMenuItem *>(menu->itemAt(0));
QVERIFY(firstMenuItem);
firstMenuItemPriv = static_cast<QQuickMenuItemPrivate *>(QQuickMenuItemPrivate::get(firstMenuItem));
} else {
QVERIFY(menu->isOpened());
}
// Implementation detail: in the failure case, QQuickMenuPrivate::handleReleaseWithoutGrab
// calls menuItem->animateClick(). We now avoid that if the menu item was not already highlighted.
QCOMPARE(firstMenuItemPriv->animateTimer, 0); // timer not started
// menu item still not highlighted
QCOMPARE(firstMenuItem->isHighlighted(), false);
QCOMPARE(triggeredSpy.size(), 0);
}
void tst_QQuickContextMenu::textControlsMenuKey()
{
#ifdef Q_OS_ANDROID
QSKIP("Crashes on android. See QTBUG-139400");
#endif
QQuickApplicationHelper helper(this, "textControlsAndParentMenus.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
auto *textArea = window->findChild<QQuickItem *>("textArea");
QVERIFY(textArea);
auto *textField = window->findChild<QQuickItem *>("textField");
QVERIFY(textField);
auto *windowMenu = window->findChild<QQuickMenu *>("windowMenu");
QVERIFY(windowMenu);
const QPoint &windowCenter = mapCenterToWindow(window->contentItem());
// give position in the middle of the window: expect the window menu
{
QContextMenuEvent cme(QContextMenuEvent::Keyboard, windowCenter, window->mapToGlobal(windowCenter));
QGuiApplication::sendEvent(window, &cme);
auto *openMenu = window->findChild<QQuickMenu *>();
QVERIFY(openMenu);
QCOMPARE(openMenu->objectName(), "windowMenu");
openMenu->close();
}
// focus the TextArea and give position 0, 0: expect the TextArea's menu
{
textArea->forceActiveFocus();
QContextMenuEvent cme(QContextMenuEvent::Keyboard, {}, window->mapToGlobal(QPoint()));
QGuiApplication::sendEvent(window, &cme);
auto *openMenu = textArea->findChild<QQuickMenu *>();
QVERIFY(openMenu);
openMenu->close();
}
// focus the TextField and give position 0, 0: expect the TextField's menu
{
textField->forceActiveFocus();
QContextMenuEvent cme(QContextMenuEvent::Keyboard, {}, window->mapToGlobal(QPoint()));
QGuiApplication::sendEvent(window, &cme);
auto *openMenu = textField->findChild<QQuickMenu *>();
QVERIFY(openMenu);
openMenu->close();
}
}
void tst_QQuickContextMenu::mouseAreaUnderTextArea()
{
#ifdef Q_OS_ANDROID
QSKIP("Crashes on android. See QTBUG-139400");
#endif
if (!arePopupWindowsSupported())
QSKIP("The platform doesn't support popup windows. Skipping test.");
QQuickApplicationHelper helper(this, "mouseAreaUnderTextArea.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
auto *textArea = window->findChild<QQuickTextArea *>();
QVERIFY(textArea);
auto *mouseArea = window->findChild<QQuickMouseArea *>();
QVERIFY(mouseArea);
// Open the menu by right clicking on the TextArea.
QTest::mouseClick(window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
auto *menu = window->findChild<QQuickMenu *>();
QVERIFY(menu);
QTRY_VERIFY(menu->isOpened());
QQuickMenuPrivate *menuPrivate = QQuickMenuPrivate::get(menu);
QVERIFY(menuPrivate);
QVERIFY(menuPrivate->usePopupWindow());
QVERIFY(menuPrivate->popupWindow);
QVERIFY(menuPrivate->popupWindow->isActive());
QTRY_COMPARE(QQuickTest::qIsPolishScheduled(menuPrivate->popupWindow), false);
// Click on the menu item to close the menu; the MouseArea shouldn't emit pressed().
auto *selectAllMenuItem = qobject_cast<QQuickMenuItem *>(menu->itemAt(menu->count() - 1));
QVERIFY(selectAllMenuItem);
QCOMPARE(selectAllMenuItem->text(), "Select All");
const QSignalSpy mouseAreaPressedSpy(mouseArea, &QQuickMouseArea::pressed);
QVERIFY(mouseAreaPressedSpy.isValid());
const QSignalSpy menuItemTriggeredSpy(selectAllMenuItem, &QQuickMenuItem::triggered);
QVERIFY(menuItemTriggeredSpy.isValid());
const QPointF menuItemCenter(selectAllMenuItem->width() / 2, selectAllMenuItem->height() / 2);
const QPoint posGlobal = selectAllMenuItem->mapToGlobal(menuItemCenter).toPoint();
const QPoint posMainWindowLocal = window->mapFromGlobal(posGlobal);
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, posMainWindowLocal);
QTRY_COMPARE(menuItemTriggeredSpy.size(), 1);
QCOMPARE(mouseAreaPressedSpy.size(), 0);
QTRY_COMPARE(menuPrivate->popupWindow->isVisible(), false);
}
QTEST_QUICKCONTROLS_MAIN(tst_QQuickContextMenu)
#include "tst_qquickcontextmenu.moc"
|