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
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/qtest.h>
#ifndef QTEST_THROW_ON_FAIL
# error This test requires QTEST_THROW_ON_FAIL being active.
#endif
#include <bitset>
#include <QtTest/qsignalspy.h>
#include <QtQml/qqmlfile.h>
#include <QtQuick/private/qquickmousearea_p.h>
#include <QtQuickDialogs2/private/qquickmessagedialog_p.h>
#include <QtQuickDialogs2QuickImpl/private/qquickmessagedialogimpl_p.h>
#include <QtQuickTest/quicktest.h>
#include <QtQuickControls2/qquickstyle.h>
#include <QtQuickTemplates2/private/qquickdialog_p_p.h>
#include <QtQuickControlsTestUtils/private/controlstestutils_p.h>
#include <QtQuickControlsTestUtils/private/dialogstestutils_p.h>
using namespace QQuickVisualTestUtils;
using namespace QQuickDialogTestUtils;
using namespace QQuickControlsTestUtils;
class tst_QQuickMessageDialogImpl : public QQmlDataTest
{
Q_OBJECT
public:
tst_QQuickMessageDialogImpl();
static void initMain()
{
// We need to set this attribute.
QCoreApplication::setAttribute(Qt::AA_DontUseNativeDialogs);
// We don't want to run this test for every style, as each one will have
// different ways of implementing the dialogs.
// For now we only test one style.
// TODO: use Basic
QQuickStyle::setStyle("Fusion");
}
private slots:
void changeText_data();
void changeText();
void changeInformativeText_data();
void changeInformativeText();
void changeStandardButtons();
void detailedText();
void resultReflectsLastStandardButtonPressed();
void checkModality_data();
void checkModality();
};
// We don't want to fail on warnings until QTBUG-98964 is fixed,
// as we deliberately prevent deferred execution in some of the tests here,
// which causes warnings.
tst_QQuickMessageDialogImpl::tst_QQuickMessageDialogImpl()
: QQmlDataTest(QT_QMLTEST_DATADIR, FailOnWarningsPolicy::DoNotFailOnWarnings)
{
}
void tst_QQuickMessageDialogImpl::changeText_data()
{
QTest::addColumn<QString>("testString1");
QTest::addColumn<QString>("testString2");
QTest::newRow("data") << "the quick brown fox jumped over the lazy dog"
<< "the lazy dog jumped over the quick brown fox";
}
void tst_QQuickMessageDialogImpl::changeText()
{
QTest::failOnWarning(QRegularExpression(".*"));
QFETCH(QString, testString1);
QFETCH(QString, testString2);
DialogTestHelper<QQuickMessageDialog, QQuickMessageDialogImpl> dialogHelper(
this, "messageDialog.qml");
QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());
QVERIFY(dialogHelper.waitForWindowActive());
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QSignalSpy textSpy(dialogHelper.dialog, SIGNAL(textChanged()));
QVERIFY(textSpy.isValid());
auto textLabel = dialogHelper.quickDialog->findChild<QQuickLabel *>("textLabel");
QVERIFY(textLabel);
// default value is empty string
QCOMPARE(dialogHelper.dialog->text(), "");
// update the text property
dialogHelper.dialog->setText(testString1);
QCOMPARE(textSpy.size(), 1);
// The textLabel is empty until dialog is re-opened
QCOMPARE(dialogHelper.dialog->text(), testString1);
QCOMPARE(textLabel->text(), "");
dialogHelper.dialog->close();
dialogHelper.dialog->open();
QCOMPARE(dialogHelper.dialog->text(), testString1);
QCOMPARE(textLabel->text(), testString1);
// The textLabel isn't updated immediately
dialogHelper.dialog->setText(testString2);
QCOMPARE(textSpy.size(), 2);
QCOMPARE(textLabel->text(), testString1);
dialogHelper.dialog->close();
}
void tst_QQuickMessageDialogImpl::changeInformativeText_data()
{
QTest::addColumn<QString>("testString1");
QTest::addColumn<QString>("testString2");
QTest::newRow("data") << "the quick brown fox jumped over the lazy dog"
<< "the lazy dog jumped over the quick brown fox";
}
void tst_QQuickMessageDialogImpl::changeInformativeText()
{
QFETCH(QString, testString1);
QFETCH(QString, testString2);
DialogTestHelper<QQuickMessageDialog, QQuickMessageDialogImpl> dialogHelper(
this, "messageDialog.qml");
QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());
QVERIFY(dialogHelper.waitForWindowActive());
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QSignalSpy informativeTextSpy(dialogHelper.dialog, SIGNAL(informativeTextChanged()));
QVERIFY(informativeTextSpy.isValid());
auto informativeTextLabel =
dialogHelper.quickDialog->findChild<QQuickLabel *>("informativeTextLabel");
QVERIFY(informativeTextLabel);
// default value is empty string
QCOMPARE(dialogHelper.dialog->informativeText(), "");
// update the informativeText property
dialogHelper.dialog->setInformativeText(testString1);
QCOMPARE(informativeTextSpy.size(), 1);
// The textLabel is empty until dialog is re-opened
QCOMPARE(dialogHelper.dialog->informativeText(), testString1);
QCOMPARE(informativeTextLabel->text(), "");
dialogHelper.dialog->close();
dialogHelper.dialog->open();
QCOMPARE(dialogHelper.dialog->informativeText(), testString1);
QCOMPARE(informativeTextLabel->text(), testString1);
// The textLabel shouldn't update immediately
dialogHelper.dialog->setInformativeText(testString2);
QCOMPARE(informativeTextSpy.size(), 2);
QCOMPARE(informativeTextLabel->text(), testString1);
dialogHelper.dialog->close();
}
void tst_QQuickMessageDialogImpl::changeStandardButtons()
{
DialogTestHelper<QQuickMessageDialog, QQuickMessageDialogImpl> dialogHelper(
this, "messageDialog.qml");
QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());
QVERIFY(dialogHelper.waitForWindowActive());
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QSignalSpy buttonBoxSpy(dialogHelper.dialog, SIGNAL(buttonsChanged()));
QVERIFY(buttonBoxSpy.isValid());
auto buttonBox = dialogHelper.quickDialog->findChild<QQuickDialogButtonBox *>("buttonBox");
QVERIFY(buttonBox);
std::bitset<QPlatformDialogHelper::NRoles> availableButtonRoles;
auto verifyButtonBox = [&]() {
availableButtonRoles.reset();
for (int i = 0; i < buttonBox->count(); ++i) {
const auto button = qobject_cast<QQuickAbstractButton *>(buttonBox->itemAt(i));
const auto role = QQuickDialogPrivate::buttonRole(button);
QVERIFY(role > QPlatformDialogHelper::InvalidRole && role < QPlatformDialogHelper::NRoles);
availableButtonRoles.set(role);
}
};
QCOMPARE(buttonBox->count(), 1);
QVERIFY2(dialogHelper.dialog->buttons() & QPlatformDialogHelper::StandardButton::Ok,
"The QMessageDialogOptionsPrivate constructor assures that the Ok button will be "
"added by default, if no other buttons are set");
dialogHelper.dialog->setButtons(
QPlatformDialogHelper::StandardButtons(QPlatformDialogHelper::StandardButton::Save
| QPlatformDialogHelper::StandardButton::Cancel
| QPlatformDialogHelper::StandardButton::Apply));
QCOMPARE(buttonBoxSpy.size(), 1);
QCOMPARE(buttonBox->count(), 1);
dialogHelper.dialog->close();
dialogHelper.dialog->open();
QCOMPARE(buttonBox->count(), 3);
verifyButtonBox();
QVERIFY(availableButtonRoles.test(QPlatformDialogHelper::AcceptRole));
QVERIFY(availableButtonRoles.test(QPlatformDialogHelper::RejectRole));
QVERIFY(availableButtonRoles.test(QPlatformDialogHelper::ApplyRole));
// change buttons when the dialog is closed
dialogHelper.dialog->close();
dialogHelper.dialog->setButtons(
QPlatformDialogHelper::StandardButton(QPlatformDialogHelper::StandardButton::Ok
| QPlatformDialogHelper::StandardButton::Close));
QCOMPARE(buttonBoxSpy.size(), 2);
QCOMPARE(buttonBox->count(), 3);
dialogHelper.dialog->open();
QCOMPARE(buttonBox->count(), 2);
verifyButtonBox();
QVERIFY(availableButtonRoles.test(QPlatformDialogHelper::AcceptRole));
QVERIFY(availableButtonRoles.test(QPlatformDialogHelper::RejectRole));
dialogHelper.dialog->close();
}
void tst_QQuickMessageDialogImpl::detailedText()
{
const QString emptyString;
const QString nonEmptyString("the quick brown fox jumped over the lazy dog");
DialogTestHelper<QQuickMessageDialog, QQuickMessageDialogImpl> dialogHelper(
this, "messageDialog.qml");
QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());
QVERIFY(dialogHelper.waitForWindowActive());
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QSignalSpy detailedTextSpy(dialogHelper.dialog, SIGNAL(detailedTextChanged()));
QVERIFY(detailedTextSpy.isValid());
auto detailedTextArea = dialogHelper.quickDialog->findChild<QQuickTextArea *>("detailedText");
QVERIFY(detailedTextArea);
auto detailedTextButton =
dialogHelper.quickDialog->findChild<QQuickAbstractButton *>("detailedTextButton");
QVERIFY(detailedTextButton);
// Should be empty by default
QCOMPARE(dialogHelper.dialog->detailedText(), emptyString);
QCOMPARE(detailedTextArea->text(), emptyString);
QVERIFY(!detailedTextButton->isVisible());
// Set the detailed text to a non-empty string
dialogHelper.dialog->setDetailedText(nonEmptyString);
QCOMPARE(dialogHelper.dialog->detailedText(), nonEmptyString);
QCOMPARE(detailedTextSpy.size(), 1);
QCOMPARE(detailedTextArea->text(), emptyString);
QVERIFY(!detailedTextButton->isVisible());
dialogHelper.dialog->close();
dialogHelper.dialog->open();
QCOMPARE(dialogHelper.dialog->detailedText(), nonEmptyString);
QCOMPARE(detailedTextArea->text(), nonEmptyString);
QVERIFY2(detailedTextButton->isVisible(),
"The 'show details' button should be visible when the detailedText property is not "
"empty.");
// Set the detailed text to an empty string
dialogHelper.dialog->setDetailedText(emptyString);
QCOMPARE(detailedTextSpy.size(), 2);
QCOMPARE(dialogHelper.dialog->detailedText(), emptyString);
QCOMPARE(detailedTextArea->text(), nonEmptyString);
QVERIFY(detailedTextButton->isVisible());
dialogHelper.dialog->close();
dialogHelper.dialog->open();
QCOMPARE(dialogHelper.dialog->detailedText(), emptyString);
QCOMPARE(detailedTextArea->text(), emptyString);
QVERIFY2(!detailedTextButton->isVisible(),
"The 'show details' button should be invisible when the detailedText property is an empty string");
// Change the detailed text property while the dialog is already open, should not immediately
// update the dialog ui
dialogHelper.dialog->setDetailedText(nonEmptyString);
QCOMPARE(detailedTextSpy.size(), 3);
QCOMPARE(dialogHelper.dialog->detailedText(), nonEmptyString);
QCOMPARE(detailedTextArea->text(), emptyString);
QVERIFY2(!detailedTextButton->isVisible(),
"The 'show details' button should only become visible when the dialog is re-opened.");
dialogHelper.dialog->close();
}
void tst_QQuickMessageDialogImpl::resultReflectsLastStandardButtonPressed()
{
DialogTestHelper<QQuickMessageDialog, QQuickMessageDialogImpl> dialogHelper(
this, "messageDialogWithButtons.qml");
QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());
QVERIFY(dialogHelper.waitForWindowActive());
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
QSignalSpy acceptedSpy(dialogHelper.dialog, SIGNAL(accepted()));
QSignalSpy rejectedSpy(dialogHelper.dialog, SIGNAL(rejected()));
QSignalSpy resultChangedSpy(dialogHelper.dialog, SIGNAL(resultChanged()));
auto buttonBox = dialogHelper.quickDialog->findChild<QQuickDialogButtonBox *>("buttonBox");
QVERIFY(buttonBox);
bool yesFound = false;
bool noFound = false;
bool discardFound = false;
bool applyFound = false;
int expectedNumberOfAcceptedSignals = 0;
int expectedNumberOfRejectedSignals = 0;
// The dialogButtonBox has different layouts depending on platform. This tries to account for all possible layouts.
// If the role of a button is YesRole, AcceptRole, NoRole or RejectRole, then pressing that button should emit accepted, or rejected.
// And since this is a MessageDialog, the result property should reflect the last button pressed, rather than the StandardCode.
for (int i = 0; i < buttonBox->count(); ++i) {
auto button = qobject_cast<QQuickAbstractButton *>(buttonBox->itemAt(i));
switch (QQuickDialogPrivate::buttonRole(button)) {
case QPlatformDialogHelper::YesRole:
yesFound = true;
expectedNumberOfAcceptedSignals++;
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, button->mapToScene({ button->width() / 2, button->height() / 2 }).toPoint());
QTRY_VERIFY(!dialogHelper.isQuickDialogOpen());
QCOMPARE(dialogHelper.dialog->result(), QPlatformDialogHelper::StandardButton::Yes);
QCOMPARE(resultChangedSpy.count(), i + 1);
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
break;
case QPlatformDialogHelper::NoRole:
noFound = true;
expectedNumberOfRejectedSignals++;
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, button->mapToScene({ button->width() / 2, button->height() / 2 }).toPoint());
QTRY_VERIFY(!dialogHelper.isQuickDialogOpen());
QCOMPARE(dialogHelper.dialog->result(), QPlatformDialogHelper::StandardButton::No);
QCOMPARE(resultChangedSpy.count(), i + 1);
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
break;
case QPlatformDialogHelper::DestructiveRole:
discardFound = true;
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, button->mapToScene({ button->width() / 2, button->height() / 2 }).toPoint());
QTRY_VERIFY(!dialogHelper.isQuickDialogOpen());
QCOMPARE(dialogHelper.dialog->result(), QPlatformDialogHelper::StandardButton::Discard);
QCOMPARE(resultChangedSpy.count(), i + 1);
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
break;
case QPlatformDialogHelper::ApplyRole:
applyFound = true;
QTest::mouseClick(dialogHelper.popupWindow(), Qt::LeftButton, Qt::NoModifier, button->mapToScene({ button->width() / 2, button->height() / 2 }).toPoint());
QTRY_VERIFY(!dialogHelper.isQuickDialogOpen());
QCOMPARE(dialogHelper.dialog->result(), QPlatformDialogHelper::StandardButton::Apply);
QCOMPARE(resultChangedSpy.count(), i + 1);
QVERIFY(dialogHelper.openDialog());
QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
break;
default:
QFAIL(qPrintable(QStringLiteral("Unexpected role %1").arg(QQuickDialogPrivate::buttonRole(button))));
}
}
QVERIFY2(yesFound && noFound && discardFound && applyFound, "A button that was expected to be present, wasn't found when iterating over all of them.");
QCOMPARE(acceptedSpy.count(), expectedNumberOfAcceptedSignals);
QCOMPARE(rejectedSpy.count(), expectedNumberOfRejectedSignals);
dialogHelper.dialog->close();
}
void tst_QQuickMessageDialogImpl::checkModality_data()
{
QTest::addColumn<Qt::WindowModality>("modality");
QTest::addColumn<int>("expectedRootWindowChildCount");
QTest::addColumn<int>("expectedChildWindowClickCount");
QTest::newRow("nonModal") << Qt::NonModal << 1 << 1;
QTest::newRow("windowModal") << Qt::WindowModal << 0 << 1;
// Verify application modal case only for the platform which supports multiple windows
if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::MultipleWindows))
QTest::newRow("applicationModal") << Qt::ApplicationModal << 0 << 0;
}
void tst_QQuickMessageDialogImpl::checkModality()
{
QFETCH(Qt::WindowModality, modality);
QFETCH(int, expectedRootWindowChildCount);
QFETCH(int, expectedChildWindowClickCount);
DialogTestHelper<QQuickMessageDialog, QQuickMessageDialogImpl> dialogHelper(this, "checkModality.qml");
QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());
QVERIFY(dialogHelper.waitForWindowActive());
dialogHelper.dialog->setModality(modality);
QCOMPARE(dialogHelper.dialog->modality(), modality);
OPEN_QUICK_DIALOG();
QVERIFY(dialogHelper.waitForPopupWindowActiveAndPolished());
auto *childWindow = dialogHelper.window()->property("childWindow").value<QQuickWindow *>();
QVERIFY(childWindow);
// Setting the child window transient parent as root window won't allow it to receive mouse
// events, causing WindowModal case not to be tested. Thus, resetting the transient parent.
if (modality == Qt::WindowModal)
childWindow->setTransientParent(nullptr);
// Platforms such as Android have system bars obscuring the content item on
// top and bottom, which can lead to a mouse event being delivered to the
// system bar instead of the content item. To avoid this, safe area margin
// has been considered.
const QMargins safeAreaMargin = dialogHelper.window()->safeAreaMargins();
const auto *rootMouseArea = dialogHelper.window()->property("rootMArea").value<QQuickMouseArea *>();
QVERIFY(rootMouseArea);
QSignalSpy rmaMouseSpy(rootMouseArea, &QQuickMouseArea::clicked);
QTest::mouseClick(dialogHelper.window(), Qt::LeftButton, Qt::NoModifier,
QPoint(5 + safeAreaMargin.left(), 5 + safeAreaMargin.top()));
QCOMPARE(rmaMouseSpy.size(), expectedRootWindowChildCount);
const auto *childMouseArea = dialogHelper.window()->property("childMArea").value<QQuickMouseArea *>();
QVERIFY(childMouseArea);
QSignalSpy cmaMouseSpy(childMouseArea, &QQuickMouseArea::clicked);
QTest::mouseClick(childWindow, Qt::LeftButton, Qt::NoModifier,
QPoint(5 + safeAreaMargin.left(), 5 + safeAreaMargin.top()));
QCOMPARE(cmaMouseSpy.size(), expectedChildWindowClickCount);
}
QTEST_MAIN(tst_QQuickMessageDialogImpl)
#include "tst_qquickmessagedialogimpl.moc"
|