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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/qtest.h>
#include <QtTest/qsignalspy.h>
#include <QtTest/qtesttouch.h>
#include <QtCore/private/qabstractanimation_p.h>
#include <QtGui/qclipboard.h>
#include <QtGui/qfontmetrics.h>
#include <QtGui/private/qguiapplication_p.h>
#include <QtGui/qpa/qplatformintegration.h>
#include <QtGui/qtestsupport_gui.h>
#include <QtQuick/qquickview.h>
#include <QtQuickTestUtils/private/qmlutils_p.h>
#include <QtQuickTestUtils/private/viewtestutils_p.h>
#include <QtQuickTestUtils/private/visualtestutils_p.h>
#include <QtQuickTemplates2/private/qquickcontextmenu_p.h>
#include <QtQuickTemplates2/private/qquickmenu_p.h>
#include <QtQuickTemplates2/private/qquickmenuitem_p.h>
#include <QtQuickTemplates2/private/qquickpopup_p_p.h>
#include <QtQuickTemplates2/private/qquickpopupwindow_p_p.h>
#include <QtQuickTemplates2/private/qquicktextarea_p.h>
#include <QtQuickControlsTestUtils/private/controlstestutils_p.h>
#include <QtQuickControlsTestUtils/private/qtest_quickcontrols_p.h>
using namespace QQuickControlsTestUtils;
using namespace QQuickVisualTestUtils;
class tst_QQuickTextArea : public QQmlDataTest
{
Q_OBJECT
public:
tst_QQuickTextArea();
private slots:
void initTestCase() override;
void touchscreenDoesNotSelect_data();
void touchscreenDoesNotSelect();
void touchscreenSetsFocusAndMovesCursor();
void contextMenuCut();
void contextMenuCopy();
void contextMenuPaste();
void contextMenuDelete();
void contextMenuSelectAll();
void customContextMenuOnRelease_data();
void customContextMenuOnRelease();
void testCursorPositionChangedOnDeleteStartWord();
private:
QScopedPointer<QPointingDevice> touchDevice = QScopedPointer<QPointingDevice>(QTest::createTouchDevice());
bool hasClipboardSupport =
#if QT_CONFIG(clipboard)
true;
#else
false;
#endif
};
tst_QQuickTextArea::tst_QQuickTextArea()
: QQmlDataTest(QT_QMLTEST_DATADIR)
{
}
void tst_QQuickTextArea::initTestCase()
{
#ifdef Q_OS_ANDROID
if (QNativeInterface::QAndroidApplication::sdkVersion() > 23)
QSKIP("Crashes on Android 7+, figure out why (QTBUG-107028)");
#endif
QQmlDataTest::initTestCase();
qputenv("QML_NO_TOUCH_COMPRESSION", "1");
// Showing a native menu is a blocking call, so the test will timeout.
QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuWindows);
QUnifiedTimer::instance()->setSpeedModifier(10);
}
void tst_QQuickTextArea::touchscreenDoesNotSelect_data()
{
QTest::addColumn<QUrl>("src");
QTest::addColumn<bool>("setEnv");
QTest::addColumn<bool>("selectByMouse");
QTest::addColumn<bool>("selectByTouch");
QTest::newRow("new default") << testFileUrl("mouseselection_default.qml") << false << true << false;
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
QTest::newRow("putenv") << testFileUrl("mouseselection_default.qml") << true << false << false;
QTest::newRow("old_import") << testFileUrl("mouseselection_old_default.qml") << false << true << false;
QTest::newRow("old+putenv") << testFileUrl("mouseselection_old_default.qml") << true << false << false;
QTest::newRow("old+putenv+selectByMouse") << testFileUrl("mouseselection_old_overridden.qml") << true << true << true;
#endif
}
void tst_QQuickTextArea::touchscreenDoesNotSelect()
{
QFETCH(QUrl, src);
QFETCH(bool, setEnv);
QFETCH(bool, selectByMouse);
QFETCH(bool, selectByTouch);
if (setEnv)
qputenv("QT_QUICK_CONTROLS_TEXT_SELECTION_BEHAVIOR", "old");
else
qunsetenv("QT_QUICK_CONTROLS_TEXT_SELECTION_BEHAVIOR");
QQuickView window;
QVERIFY(QQuickTest::showView(window, src));
QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit *>(window.rootObject());
QVERIFY(textEditObject != nullptr);
QCOMPARE(textEditObject->selectByMouse(), selectByMouse);
textEditObject->setSelectByMouse(true); // enable selection with pre-6.4 import version
QVERIFY(textEditObject->selectedText().isEmpty());
if (selectByMouse) {
// press-drag-and-release from x1 to x2
const int x1 = textEditObject->leftPadding();
const int x2 = textEditObject->width() / 2;
// Account for all styles by being aware of vertical padding.
// contentHeight / 2 should be half the line height considering that we only have one line of text.
const int y = textEditObject->topPadding() + textEditObject->contentHeight() / 2;
QTest::touchEvent(&window, touchDevice.data()).press(0, QPoint(x1,y), &window);
QTest::touchEvent(&window, touchDevice.data()).move(0, QPoint(x2,y), &window);
QTest::touchEvent(&window, touchDevice.data()).release(0, QPoint(x2,y), &window);
QQuickTouchUtils::flush(&window);
// if the import version is old enough, fall back to old behavior: touch swipe _does_ select text if selectByMouse is true
QCOMPARE(textEditObject->selectedText().isEmpty(), !selectByTouch);
}
}
void tst_QQuickTextArea::touchscreenSetsFocusAndMovesCursor()
{
SKIP_IF_NO_WINDOW_ACTIVATION;
qunsetenv("QT_QUICK_CONTROLS_TEXT_SELECTION_BEHAVIOR");
QQuickView window;
QVERIFY(QQuickTest::showView(window, testFileUrl("twoInAColumn.qml")));
window.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window));
QQuickTextEdit *top = window.rootObject()->findChild<QQuickTextEdit*>("top");
QVERIFY(top);
QQuickTextEdit *bottom = window.rootObject()->findChild<QQuickTextEdit*>("bottom");
QVERIFY(bottom);
const auto len = bottom->text().size();
// tap the bottom field
const qreal yOffset = bottom->topPadding() + 6; // where to tap or drag to hit the text
QPoint p1 = bottom->mapToScene({60, yOffset}).toPoint();
QTest::touchEvent(&window, touchDevice.data()).press(0, p1, &window);
QQuickTouchUtils::flush(&window);
// text cursor is at 0 by default, on press
QCOMPARE(bottom->cursorPosition(), 0);
// the focus changes and the cursor moves after release (not after press, as in TextEdit)
QTest::touchEvent(&window, touchDevice.data()).release(0, p1, &window);
QQuickTouchUtils::flush(&window);
QCOMPARE(qApp->focusObject(), bottom);
QTRY_COMPARE_GT(bottom->cursorPosition(), 0);
// typing a character inserts it at the cursor position
QVERIFY(!bottom->text().contains('q'));
QTest::keyClick(&window, Qt::Key_Q);
QCOMPARE(bottom->text().size(), len + 1);
QCOMPARE_GT(bottom->text().indexOf('q'), 0);
// press-drag-and-release from p1 to p2 on the top field
p1 = top->mapToScene({0, yOffset}).toPoint();
QPoint p2 = top->mapToScene({76, yOffset}).toPoint();
QTest::touchEvent(&window, touchDevice.data()).press(0, p1, &window);
QQuickTouchUtils::flush(&window);
QTest::touchEvent(&window, touchDevice.data()).move(0, p2, &window);
QQuickTouchUtils::flush(&window);
QTest::touchEvent(&window, touchDevice.data()).release(0, p2, &window);
QQuickTouchUtils::flush(&window);
QCOMPARE(qApp->focusObject(), top);
QVERIFY(top->selectedText().isEmpty());
QCOMPARE_GT(top->cursorPosition(), 0);
// touch-drag did not select text, but mouse-drag from p2 back to p1
// does select the first part of the text, and leave the cursor at the beginning
QTest::mousePress(&window, Qt::LeftButton, {}, p2);
QTest::mouseMove(&window, p1);
QTest::mouseRelease(&window, Qt::LeftButton, {}, p1);
QCOMPARE(top->cursorPosition(), 0);
QCOMPARE_GT(top->selectedText().size(), 0);
}
static const auto mementoStr = QLatin1String("Memento");
static const auto moriStr = QLatin1String("mori");
static const auto mementoMoriStr = QLatin1String("Memento mori");
void tst_QQuickTextArea::contextMenuCut()
{
QQuickView window;
QVERIFY(QQuickTest::showView(window, testFileUrl("contextMenu.qml")));
window.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window));
auto *textArea = window.rootObject()->property("textArea").value<QQuickTextArea *>();
QVERIFY(textArea);
textArea->forceActiveFocus();
// Right click on the TextArea to open the context menu.
// Right-clicking without a selection should result in the Cut menu item being disabled.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
auto *contextMenu = textArea->findChild<QQuickContextMenu *>();
QVERIFY(contextMenu);
QTRY_VERIFY(contextMenu->menu()->isOpened());
auto *cutMenuItem = qobject_cast<QQuickMenuItem *>(contextMenu->menu()->itemAt(0));
QCOMPARE(cutMenuItem->text(), "Cut");
QVERIFY(!cutMenuItem->isEnabled());
QVERIFY(textArea->selectedText().isEmpty());
// Close the context menu.
QTest::keyClick(&window, Qt::Key_Escape);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
// Right-clicking with a selection should result in the Cut menu item being enabled
// if Qt was built with clipboard support.
textArea->select(mementoStr.length(), textArea->length());
const QString cutText = QLatin1Char(' ') + moriStr;
QCOMPARE(textArea->selectedText(), cutText);
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
QTRY_VERIFY(contextMenu->menu()->isOpened());
QCOMPARE(cutMenuItem->isEnabled(), hasClipboardSupport);
// Click on the Cut menu item (if enabled) and close the menu.
#if QT_CONFIG(clipboard)
auto *contextMenuPrivate = QQuickPopupPrivate::get(contextMenu->menu());
QQuickWindow *contextMenuPopupWindow = &window;
if (contextMenuPrivate->usePopupWindow())
contextMenuPopupWindow = contextMenuPrivate->popupWindow;
QTest::mouseClick(contextMenuPopupWindow, Qt::LeftButton, Qt::NoModifier, mapCenterToWindow(cutMenuItem));
QCOMPARE(textArea->text(), mementoStr);
QCOMPARE(qGuiApp->clipboard()->text(), cutText);
#else
QTest::keyClick(&window, Qt::Key_Escape);
#endif
QTRY_VERIFY(!contextMenu->menu()->isVisible());
// Make the TextArea read-only. Cut should no longer be enabled.
textArea->setReadOnly(true);
textArea->selectAll();
// Right click on the TextArea to open the context menu.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
QTRY_VERIFY(contextMenu->menu()->isOpened());
QCOMPARE(cutMenuItem->text(), "Cut");
QVERIFY(!cutMenuItem->isEnabled());
// Close the context menu.
QTest::keyClick(&window, Qt::Key_Escape);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
}
void tst_QQuickTextArea::contextMenuCopy()
{
QQuickView window;
QVERIFY(QQuickTest::showView(window, testFileUrl("contextMenu.qml")));
window.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window));
auto *textArea = window.rootObject()->property("textArea").value<QQuickTextArea *>();
QVERIFY(textArea);
textArea->forceActiveFocus();
// Right click on the TextArea to open the context menu.
// Right-clicking without a selection should result in the Copy menu item being disabled.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
auto *contextMenu = textArea->findChild<QQuickContextMenu *>();
QVERIFY(contextMenu);
QTRY_VERIFY(contextMenu->menu()->isOpened());
auto *copyMenuItem = qobject_cast<QQuickMenuItem *>(contextMenu->menu()->itemAt(1));
QVERIFY(copyMenuItem);
QCOMPARE(copyMenuItem->text(), "Copy");
QVERIFY(!copyMenuItem->isEnabled());
// Close the context menu.
QTest::keyClick(&window, Qt::Key_Escape);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
// Right-clicking with a selection should result in the Copy menu item being enabled
// if Qt was built with clipboard support.
textArea->select(0, mementoStr.length());
QCOMPARE(textArea->selectedText(), mementoStr);
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
QTRY_VERIFY(contextMenu->menu()->isOpened());
QCOMPARE(copyMenuItem->isEnabled(), hasClipboardSupport);
// Click on the Copy menu item (if enabled) and close the menu.
#if QT_CONFIG(clipboard)
auto *contextMenuPrivate = QQuickPopupPrivate::get(contextMenu->menu());
QQuickWindow *contextMenuPopupWindow = &window;
if (contextMenuPrivate->usePopupWindow())
contextMenuPopupWindow = contextMenuPrivate->popupWindow;
QTest::mouseClick(contextMenuPopupWindow, Qt::LeftButton, Qt::NoModifier, mapCenterToWindow(copyMenuItem));
QCOMPARE(textArea->text(), mementoMoriStr);
const auto *clipboard = QGuiApplication::clipboard();
QCOMPARE(clipboard->text(), mementoStr);
#else
QTest::keyClick(&window, Qt::Key_Escape);
#endif
QTRY_VERIFY(!contextMenu->menu()->isVisible());
// Make the TextArea read-only. Copy should still be enabled if Qt was built with clipboard support.
textArea->setReadOnly(true);
// Right click on the TextArea to open the context menu.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
QTRY_VERIFY(contextMenu->menu()->isOpened());
QCOMPARE(copyMenuItem->text(), "Copy");
// Select some text.
textArea->select(0, mementoStr.length());
QCOMPARE(copyMenuItem->isEnabled(), hasClipboardSupport);
// Close the context menu.
QTest::keyClick(&window, Qt::Key_Escape);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
}
void tst_QQuickTextArea::contextMenuPaste()
{
QQuickView window;
QVERIFY(QQuickTest::showView(window, testFileUrl("contextMenu.qml")));
window.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window));
auto *textArea = window.rootObject()->property("textArea").value<QQuickTextArea *>();
QVERIFY(textArea);
textArea->forceActiveFocus();
#if QT_CONFIG(clipboard)
auto *clipboard = QGuiApplication::clipboard();
clipboard->setText(" 🫠");
#endif
// For some reason the cursor is at the beginning of the text, even when
// right-clicking to the right of it, so first left-click.
QTest::mouseClick(&window, Qt::LeftButton, Qt::NoModifier, mapCenterToWindow(textArea));
// Right click on the TextArea to open the context menu.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
auto *contextMenu = textArea->findChild<QQuickContextMenu *>();
QVERIFY(contextMenu);
QTRY_VERIFY(contextMenu->menu()->isOpened());
auto *pasteMenuItem = qobject_cast<QQuickMenuItem *>(contextMenu->menu()->itemAt(2));
QVERIFY(pasteMenuItem);
QCOMPARE(pasteMenuItem->text(), "Paste");
QCOMPARE(pasteMenuItem->isEnabled(), hasClipboardSupport);
// Click on the Paste menu item (if enabled) and close the menu.
auto *contextMenuPrivate = QQuickPopupPrivate::get(contextMenu->menu());
QQuickWindow *contextMenuPopupWindow = &window;
if (contextMenuPrivate->usePopupWindow())
contextMenuPopupWindow = contextMenuPrivate->popupWindow;
QTest::mouseClick(contextMenuPopupWindow, Qt::LeftButton, Qt::NoModifier, mapCenterToWindow(pasteMenuItem));
#if QT_CONFIG(clipboard)
QCOMPARE(textArea->text(), mementoMoriStr + clipboard->text());
#else
QTest::keyClick(&window, Qt::Key_Escape);
#endif
QTRY_VERIFY(!contextMenu->menu()->isVisible());
// Make the TextArea read-only. Paste should no longer be enabled.
textArea->setReadOnly(true);
// Right click on the TextArea to open the context menu.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
QTRY_VERIFY(contextMenu->menu()->isOpened());
QCOMPARE(pasteMenuItem->text(), "Paste");
QVERIFY(!pasteMenuItem->isEnabled());
// Close the context menu.
QTest::keyClick(&window, Qt::Key_Escape);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
}
void tst_QQuickTextArea::contextMenuDelete()
{
QQuickView window;
QVERIFY(QQuickTest::showView(window, testFileUrl("contextMenu.qml")));
window.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window));
auto *textArea = window.rootObject()->property("textArea").value<QQuickTextArea *>();
QVERIFY(textArea);
textArea->forceActiveFocus();
// Right click on the TextArea to open the context menu.
// Right-clicking without a selection should result in the Delete menu item being disabled.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
auto *contextMenu = textArea->findChild<QQuickContextMenu *>();
QVERIFY(contextMenu);
QTRY_VERIFY(contextMenu->menu()->isOpened());
auto *deleteMenuItem = qobject_cast<QQuickMenuItem *>(contextMenu->menu()->itemAt(3));
QCOMPARE(deleteMenuItem->text(), "Delete");
QVERIFY(!deleteMenuItem->isEnabled());
// Close the context menu.
QTest::keyClick(&window, Qt::Key_Escape);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
// Right-clicking with a selection should result in the Delete menu item being enabled.
textArea->select(mementoStr.length(), textArea->length());
QCOMPARE(textArea->selectedText(), QLatin1Char(' ') + moriStr);
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
QTRY_VERIFY(contextMenu->menu()->isOpened());
QVERIFY(deleteMenuItem->isEnabled());
// Click on the Delete menu item and close the menu.
auto *contextMenuPrivate = QQuickPopupPrivate::get(contextMenu->menu());
QQuickWindow *contextMenuPopupWindow = &window;
if (contextMenuPrivate->usePopupWindow())
contextMenuPopupWindow = contextMenuPrivate->popupWindow;
QTest::mouseClick(contextMenuPopupWindow, Qt::LeftButton, Qt::NoModifier, mapCenterToWindow(deleteMenuItem));
QCOMPARE(textArea->text(), mementoStr);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
// Make the TextArea read-only. Delete should no longer be enabled.
textArea->setReadOnly(true);
textArea->selectAll();
// Right click on the TextArea to open the context menu.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
QTRY_VERIFY(contextMenu->menu()->isOpened());
QCOMPARE(deleteMenuItem->text(), "Delete");
QVERIFY(!deleteMenuItem->isEnabled());
// Close the context menu.
QTest::keyClick(&window, Qt::Key_Escape);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
}
void tst_QQuickTextArea::contextMenuSelectAll()
{
QQuickView window;
QVERIFY(QQuickTest::showView(window, testFileUrl("contextMenu.qml")));
window.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window));
auto *textArea = window.rootObject()->property("textArea").value<QQuickTextArea *>();
QVERIFY(textArea);
textArea->forceActiveFocus();
// Right click on the TextArea to open the context menu.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
auto *contextMenu = textArea->findChild<QQuickContextMenu *>();
QVERIFY(contextMenu);
QTRY_VERIFY(contextMenu->menu()->isOpened());
auto *selectAllMenuItem = qobject_cast<QQuickMenuItem *>(contextMenu->menu()->itemAt(5));
QVERIFY(selectAllMenuItem);
QCOMPARE(selectAllMenuItem->text(), "Select All");
// Click on the Select All menu item and close the menu.
auto *contextMenuPrivate = QQuickPopupPrivate::get(contextMenu->menu());
QQuickWindow *contextMenuPopupWindow = &window;
if (contextMenuPrivate->usePopupWindow())
contextMenuPopupWindow = contextMenuPrivate->popupWindow;
QTest::mouseClick(contextMenuPopupWindow, Qt::LeftButton, Qt::NoModifier, mapCenterToWindow(selectAllMenuItem));
QCOMPARE(textArea->selectedText(), mementoMoriStr);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
// Make the TextArea read-only. Select All should still be enabled.
textArea->setReadOnly(true);
// Right click on the TextArea to open the context menu.
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
QTRY_VERIFY(contextMenu->menu()->isOpened());
QCOMPARE(selectAllMenuItem->text(), "Select All");
QVERIFY(selectAllMenuItem->isEnabled());
textArea->setReadOnly(false);
// Close the context menu.
QTest::keyClick(&window, Qt::Key_Escape);
QTRY_VERIFY(!contextMenu->menu()->isVisible());
}
void tst_QQuickTextArea::customContextMenuOnRelease_data()
{
QTest::addColumn<QUrl>("qmlUrl");
QTest::addColumn<QQuickPopup::PopupType>("popupType");
QTest::newRow("Item menu instead of attached menu, TapHandler")
<< testFileUrl("customContextMenuNotAttachedOnRelease.qml") << QQuickPopup::Item;
QTest::newRow("Item menu instead of attached menu, MouseArea")
<< testFileUrl("customContextMenuNotAttachedOnReleaseMouseArea.qml") << QQuickPopup::Item;
QTest::newRow("Item menu instead of default menu, TapHandler")
<< testFileUrl("customContextMenuOnRelease.qml") << QQuickPopup::Item;
QTest::newRow("Item menu instead of default menu, MouseArea")
<< testFileUrl("customContextMenuOnReleaseMouseArea.qml") << QQuickPopup::Item;
QTest::newRow("Sibling item menu instead of default menu, TapHandler")
<< testFileUrl("customContextMenuOnReleaseInSibling.qml") << QQuickPopup::Item;
QTest::newRow("Sibling item menu instead of default menu, MouseArea")
<< testFileUrl("customContextMenuOnReleaseSiblingMouseArea.qml") << QQuickPopup::Item;
if (arePopupWindowsSupported()) {
QTest::newRow("Windowed menu instead of attached menu, TapHandler")
<< testFileUrl("customContextMenuNotAttachedOnRelease.qml") << QQuickPopup::Window;
QTest::newRow("Windowed menu instead of attached menu, MouseArea")
<< testFileUrl("customContextMenuNotAttachedOnReleaseMouseArea.qml") << QQuickPopup::Window;
QTest::newRow("Windowed menu instead of default menu, TapHandler")
<< testFileUrl("customContextMenuOnRelease.qml") << QQuickPopup::Window;
QTest::newRow("Windowed menu instead of default menu, MouseArea")
<< testFileUrl("customContextMenuOnReleaseMouseArea.qml") << QQuickPopup::Window;
QTest::newRow("Sibling item menu instead of default menu, TapHandler")
<< testFileUrl("customContextMenuOnReleaseInSibling.qml") << QQuickPopup::Window;
QTest::newRow("Sibling item menu instead of default menu, MouseArea")
<< testFileUrl("customContextMenuOnReleaseSiblingMouseArea.qml") << QQuickPopup::Window;
}
}
void tst_QQuickTextArea::customContextMenuOnRelease()
{
QFETCH(QUrl, qmlUrl);
QFETCH(QQuickPopup::PopupType, popupType);
QQuickView window;
QVERIFY(QQuickTest::showView(window, qmlUrl));
window.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window));
auto *ourContextMenu = window.rootObject()->property("ourContextMenu").value<QQuickMenu *>();
std::unique_ptr<QSignalSpy> ourMenuOpenedSpy = nullptr;
if (ourContextMenu) {
ourContextMenu->setPopupType(popupType);
ourMenuOpenedSpy = std::make_unique<QSignalSpy>(ourContextMenu, &QQuickMenu::opened);
}
// Right click on the TextArea to open the context menu. The user's custom context menu
// should be visible, but not ours (ContextMenu's).
auto *textArea = window.rootObject()->property("textArea").value<QQuickTextArea *>();
QVERIFY(textArea);
QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
auto *userContextMenu = window.rootObject()->property("userContextMenu").value<QQuickMenu *>();
QVERIFY(userContextMenu);
QTRY_VERIFY(userContextMenu->isOpened());
// Delivery of the QContextMenuEvent was prevented, so neither the default context menu
// nor the attached context menu ever even tried to open.
if (ourContextMenu)
QCOMPARE(ourMenuOpenedSpy->size(), 0);
else
QVERIFY(window.rootObject()->findChildren<QQuickMenu *>().size() <= 1);
}
void tst_QQuickTextArea::testCursorPositionChangedOnDeleteStartWord()
{
const QString initialText = "The quick brown fox jumps over the lazy dog.";
const QString expectedText = "The quick brown jumps over the lazy dog.";
QQuickView window;
QVERIFY(QQuickTest::showView(window, testFileUrl("mouseselection_default.qml")));
QQuickTextEdit *textField = qobject_cast<QQuickTextEdit *>(window.rootObject());
QVERIFY(textField != nullptr);
textField->setText(initialText);
textField->setCursorPosition(19);
QSignalSpy spy(textField, &QQuickTextEdit::cursorPositionChanged);
textField->forceActiveFocus();
QTest::keySequence(&window, QKeySequence::DeleteStartOfWord);
QCOMPARE(spy.count(), 1);
QCOMPARE(textField->text(), expectedText);
QCOMPARE(textField->cursorPosition(), 16);
}
QTEST_QUICKCONTROLS_MAIN(tst_QQuickTextArea)
#include "tst_qquicktextarea.moc"
|