aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/dialogs/qquickfontdialogimpl/tst_qquickfontdialogimpl.cpp
blob: 42f60f1b64465b5f80ae2b80c34ae0a6e190fbca (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
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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "../../shared/dialogtestutil.h"
#include "../../shared/util.h"
#include "../../shared/visualtestutil.h"

#include <QtTest/qtest.h>
#include <QtTest/qsignalspy.h>
#include <QtQml/qqmlfile.h>
#include <QtQuick/private/qquicklistview_p.h>
#include <QtQuickTemplates2/private/qquickitemdelegate_p.h>
#include <QtQuickDialogs2/private/qquickfontdialog_p.h>
#include <QtQuickDialogs2QuickImpl/private/qquickfontdialogimpl_p.h>
#include <QtQuickTest/quicktest.h>
#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h>
#include <QtQuickTemplates2/private/qquickcombobox_p.h>
#include <QtQuickTemplates2/private/qquicktextfield_p.h>
#include <QtQuickTemplates2/private/qquickdialogbuttonbox_p.h>
#include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h>
#include <QtQuickTemplates2/private/qquicklabel_p.h>
#include <QtQuickTemplates2/private/qquickoverlay_p.h>

using namespace QQuickDialogTestUtil;
using namespace QQuickVisualTestUtil;

class tst_QQuickFontDialogImpl : public QQmlDataTest
{
    Q_OBJECT

private slots:
    void writingSystem();
    void listViews();
    void effects();
    void changeFontSize();
    void changeDialogTitle();
    void searchFamily();

private:
    QQuickAbstractButton *findDialogButton(QQuickDialogButtonBox *box, const QString &buttonText)
    {
        for (int i = 0; i < box->count(); ++i) {
            auto button = qobject_cast<QQuickAbstractButton *>(box->itemAt(i));
            if (button && button->text().toUpper() == buttonText.toUpper())
                return button;
        }
        return nullptr;
    }

    bool closePopup(DialogTestHelper<QQuickFontDialog, QQuickFontDialogImpl> *dialogHelper,
                    QString dialogButton, QString &failureMessage)
    {
        auto dialogButtonBox =
                dialogHelper->quickDialog->footer()->findChild<QQuickDialogButtonBox *>();

        if (!dialogButtonBox) {
            failureMessage = QLatin1String("dialogButtonBox is null");
            return false;
        }

        QQuickAbstractButton *openButton = findDialogButton(dialogButtonBox, dialogButton);

        if (!openButton) {
            failureMessage =
                    QLatin1String("Couldn't find a button with text '%1'").arg(dialogButton);
            return false;
        }

        bool clicked = clickButton(openButton);

        if (!clicked) {
            failureMessage = QLatin1String("'%1' button was never clicked").arg(dialogButton);
            return false;
        }

        bool visible = dialogHelper->dialog->isVisible();
        if (visible) {
            failureMessage = QLatin1String("Dialog is still visible after clicking the '%1' button")
                                     .arg(dialogButton);
            return false;
        }
        return true;
    }
};

#define CREATE_DIALOG_TEST_HELPER                                                                  \
    DialogTestHelper<QQuickFontDialog, QQuickFontDialogImpl> dialogHelper(this, "fontDialog.qml"); \
    QVERIFY2(dialogHelper.isWindowInitialized(), dialogHelper.failureMessage());                   \
    QVERIFY(dialogHelper.waitForWindowActive());

#define CLOSE_DIALOG(BUTTON)                                                                       \
    QString errorMessage;                                                                          \
    QVERIFY2(closePopup(&dialogHelper, BUTTON, errorMessage), qPrintable(errorMessage));           \
    QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());

void tst_QQuickFontDialogImpl::writingSystem()
{
    CREATE_DIALOG_TEST_HELPER

    // Open the dialog.
    QVERIFY(dialogHelper.openDialog());
    QTRY_VERIFY(dialogHelper.isQuickDialogOpen());

    const QQuickTextEdit *sampleEdit =
            dialogHelper.quickDialog->findChild<QQuickTextEdit *>("sampleEdit");
    QVERIFY(sampleEdit);

    QCOMPARE(sampleEdit->text(), QFontDatabase::writingSystemSample(QFontDatabase::Any));

    // Check that the font family list view exist and add signal spy.
    const QQuickListView *fontFamilyListView =
            dialogHelper.quickDialog->findChild<QQuickListView *>("familyListView");
    QVERIFY(fontFamilyListView);
    QSignalSpy fontFamilyModelSpy(fontFamilyListView, SIGNAL(modelChanged()));

    // Open the ComboBox's popup.
    const QQuickComboBox *writingSystemComboBox = dialogHelper.quickDialog->findChild<QQuickComboBox*>();
    QVERIFY(writingSystemComboBox);
    const QPoint comboBoxCenterPos = writingSystemComboBox->mapToScene({ writingSystemComboBox->width() / 2, writingSystemComboBox->height() / 2 }).toPoint();
    QTest::mouseClick(dialogHelper.window(), Qt::LeftButton, Qt::NoModifier, comboBoxCenterPos);
    QTRY_VERIFY(writingSystemComboBox->popup()->isOpened());

    // "Any" should be selected by default.
    QQuickListView *comboBoxPopupListView = qobject_cast<QQuickListView*>(writingSystemComboBox->popup()->contentItem());
    QVERIFY(comboBoxPopupListView);
    const int anyIndex = QFontDatabase::Any;
    QQuickAbstractButton *anyDelegate = qobject_cast<QQuickAbstractButton*>(findViewDelegateItem(comboBoxPopupListView, anyIndex));
    QVERIFY(anyDelegate);
    QCOMPARE(anyDelegate->text(), QFontDatabase::writingSystemName(QFontDatabase::Any));

    QCOMPARE(fontFamilyModelSpy.count(), 0);

    // Select "Japanese" from the ComboBox.
    const int japaneseIndex = QFontDatabase::Japanese;
    QQuickAbstractButton *japaneseDelegate = qobject_cast<QQuickAbstractButton*>(findViewDelegateItem(comboBoxPopupListView, japaneseIndex));
    QVERIFY(japaneseDelegate);
    QCOMPARE(japaneseDelegate->text(), QFontDatabase::writingSystemName(QFontDatabase::Japanese));
    QVERIFY(clickButton(japaneseDelegate));
    QTRY_VERIFY(!writingSystemComboBox->popup()->isVisible());

    // Check that the contents of the font family listview changed
    QCOMPARE(fontFamilyModelSpy.count(), 1);

    // And that the sample text is correctly set
    QCOMPARE(sampleEdit->text(), QFontDatabase::writingSystemSample(QFontDatabase::Japanese));

    // Then accept it to close it.
    auto dialogButtonBox = dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox*>();
    QVERIFY(dialogButtonBox);
    QQuickAbstractButton* openButton = findDialogButton(dialogButtonBox, "Ok");
    QVERIFY(openButton);
    QVERIFY(clickButton(openButton));
    QVERIFY(!dialogHelper.dialog->isVisible());
    QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());
}

void tst_QQuickFontDialogImpl::listViews()
{
    CREATE_DIALOG_TEST_HELPER

    // Open the dialog.
    QVERIFY(dialogHelper.openDialog());
    QTRY_VERIFY(dialogHelper.isQuickDialogOpen());

    QQuickListView *fontFamilyListView =
            dialogHelper.quickDialog->findChild<QQuickListView *>("familyListView");
    QVERIFY(fontFamilyListView);

    const QQuickListView *fontStyleListView =
            dialogHelper.quickDialog->findChild<QQuickListView *>("styleListView");
    QVERIFY(fontStyleListView);

    const QQuickListView *fontSizeListView =
            dialogHelper.quickDialog->findChild<QQuickListView *>("sizeListView");
    QVERIFY(fontSizeListView);

    QSignalSpy currentFontSpy(dialogHelper.dialog, SIGNAL(currentFontChanged()));
    QSignalSpy styleModelSpy(fontStyleListView, SIGNAL(modelChanged()));
    QSignalSpy sizeModelSpy(fontSizeListView, SIGNAL(modelChanged()));

    QCOMPARE(fontFamilyListView->currentIndex(), 0);
    QCOMPARE(fontStyleListView->currentIndex(), 0);
    QCOMPARE(fontSizeListView->currentIndex(), 0);

    auto fontListModel = QFontDatabase::families(QFontDatabase::WritingSystem::Any);
    fontListModel.removeIf(QFontDatabase::isPrivateFamily);

    // In case the font database contains a significant number of font families
    const int maxNumberOfFamiliesToTest = 10;

    for (auto i = 1; i < qMin(fontListModel.size(), maxNumberOfFamiliesToTest); ++i) {
        currentFontSpy.clear();
        styleModelSpy.clear();
        sizeModelSpy.clear();

        const QString err = QString("LOOP INDEX %1, EXPECTED %2, ACTUAL %3").arg(i);

        const QFont currentFont = dialogHelper.dialog->currentFont();

        const auto oldStyleModel = fontStyleListView->model();
        const auto oldSizeModel = fontSizeListView->model();

        const QString expected1 = fontListModel[i - 1], actual1 = currentFont.family();
        QVERIFY2(expected1 == actual1, qPrintable(err.arg(expected1, actual1)));

        QQuickAbstractButton *fontDelegate =
                qobject_cast<QQuickAbstractButton *>(findViewDelegateItem(fontFamilyListView, i));
        QVERIFY2(fontDelegate, qPrintable(QString("LOOP INDEX %1").arg(i)));

        QVERIFY2(clickButton(fontDelegate), qPrintable(QString("LOOP INDEX %1").arg(i)));

        const QString expected2 = fontListModel[i],
                      actual2 = dialogHelper.dialog->currentFont().family();
        QVERIFY2(expected2 == actual2, qPrintable(err.arg(expected2, actual2).append(" font: ").append(fontDelegate->text())));
        QVERIFY2(currentFontSpy.count() == 1, qPrintable(err.arg(1, currentFontSpy.count())));
        QVERIFY2((oldStyleModel == fontStyleListView->model()) != (styleModelSpy.count() == 1),
                 qPrintable(QString("LOOP INDEX %1").arg(i)));
        QVERIFY2((oldSizeModel == fontSizeListView->model()) != (sizeModelSpy.count() == 1),
                 qPrintable(QString("LOOP INDEX %1").arg(i)));
    }

    // Then accept it to close it.
    QVERIFY(dialogHelper.dialog->currentFont() != dialogHelper.dialog->selectedFont());

    CLOSE_DIALOG("Ok");

    QVERIFY(dialogHelper.dialog->currentFont() == dialogHelper.dialog->selectedFont());
}

void tst_QQuickFontDialogImpl::effects()
{
    CREATE_DIALOG_TEST_HELPER

    // Open the dialog.
    QVERIFY(dialogHelper.openDialog());
    QTRY_VERIFY(dialogHelper.isQuickDialogOpen());

    QSignalSpy currentFontSpy(dialogHelper.dialog, SIGNAL(currentFontChanged()));

    QQuickCheckBox *underlineCheckBox =
            dialogHelper.quickDialog->findChild<QQuickCheckBox *>("underlineEffect");
    QVERIFY(underlineCheckBox);

    QQuickCheckBox *strikeoutCheckBox =
            dialogHelper.quickDialog->findChild<QQuickCheckBox *>("strikeoutEffect");
    QVERIFY(strikeoutCheckBox);

    QVERIFY(!dialogHelper.dialog->currentFont().underline());
    QVERIFY(!dialogHelper.dialog->currentFont().strikeOut());

    QVERIFY(clickButton(underlineCheckBox));

    QCOMPARE(currentFontSpy.count(), 1);
    QVERIFY(dialogHelper.dialog->currentFont().underline());
    QVERIFY(!dialogHelper.dialog->currentFont().strikeOut());

    QVERIFY(clickButton(underlineCheckBox));

    QCOMPARE(currentFontSpy.count(), 2);
    QVERIFY(!dialogHelper.dialog->currentFont().underline());
    QVERIFY(!dialogHelper.dialog->currentFont().strikeOut());

    QVERIFY(clickButton(strikeoutCheckBox));

    QCOMPARE(currentFontSpy.count(), 3);
    QVERIFY(!dialogHelper.dialog->currentFont().underline());
    QVERIFY(dialogHelper.dialog->currentFont().strikeOut());

    QVERIFY(clickButton(strikeoutCheckBox));

    QCOMPARE(currentFontSpy.count(), 4);
    QVERIFY(!dialogHelper.dialog->currentFont().underline());
    QVERIFY(!dialogHelper.dialog->currentFont().strikeOut());

    // Then accept it to close it.
    CLOSE_DIALOG("Ok");
}

void tst_QQuickFontDialogImpl::changeFontSize()
{
    CREATE_DIALOG_TEST_HELPER

    // Open the dialog.
    QVERIFY(dialogHelper.openDialog());
    QTRY_VERIFY(dialogHelper.isQuickDialogOpen());

    QQuickTextField *sizeEdit = dialogHelper.quickDialog->findChild<QQuickTextField *>("sizeEdit");
    QVERIFY(sizeEdit);

    QQuickListView *fontSizeListView =
            dialogHelper.quickDialog->findChild<QQuickListView *>("sizeListView");
    QVERIFY(fontSizeListView);

    const QQuickItemDelegate *firstSizeDelegate =
            qobject_cast<QQuickItemDelegate *>(findViewDelegateItem(fontSizeListView, 0));

    QCOMPARE(dialogHelper.dialog->currentFont().pointSize(), firstSizeDelegate->text().toInt());

    sizeEdit->setText("15");
    QCOMPARE(dialogHelper.dialog->currentFont().pointSize(), 15);

    sizeEdit->setText("22");
    QCOMPARE(dialogHelper.dialog->currentFont().pointSize(), 22);

    QVERIFY(dialogHelper.dialog->currentFont() != dialogHelper.dialog->selectedFont());

    // Then accept it to close it.
    CLOSE_DIALOG("Ok");

    QVERIFY(dialogHelper.dialog->currentFont() == dialogHelper.dialog->selectedFont());
}

void tst_QQuickFontDialogImpl::changeDialogTitle()
{
    CREATE_DIALOG_TEST_HELPER

    // Open the dialog.
    QVERIFY(dialogHelper.openDialog());
    QTRY_VERIFY(dialogHelper.isQuickDialogOpen());

    const auto dialogButtonBox =
            dialogHelper.quickDialog->footer()->findChild<QQuickDialogButtonBox *>();
    QVERIFY(dialogButtonBox);
    QQuickAbstractButton *cancelButton = findDialogButton(dialogButtonBox, "Cancel");
    QVERIFY(cancelButton);

    const QQuickLabel *titleLabel = dialogHelper.quickDialog->header()->findChild<QQuickLabel *>();
    QVERIFY(titleLabel);

    QCOMPARE(titleLabel->text(), QString());

    const QString newTitle1 = QLatin1String("Some random title #1");

    // Dialog must be closed for the title to update
    QVERIFY(clickButton(cancelButton));
    QVERIFY(!dialogHelper.dialog->isVisible());
    QTRY_VERIFY(!dialogHelper.quickDialog->isVisible());

    dialogHelper.dialog->setTitle(newTitle1);

    // Reopen the dialog
    QVERIFY(dialogHelper.openDialog());
    QTRY_VERIFY(dialogHelper.isQuickDialogOpen());
    QVERIFY(dialogHelper.dialog->isVisible());
    QTRY_VERIFY(dialogHelper.quickDialog->isVisible());

    QCOMPARE(titleLabel->text(), newTitle1);

    const QString newTitle2 = QLatin1String("Some random other title #2");

    dialogHelper.dialog->setTitle(newTitle2);

    // Shouldn't update unless you reopen the dialog
    QCOMPARE(titleLabel->text(), newTitle1);

    QVERIFY(clickButton(cancelButton));
    QVERIFY(dialogHelper.openDialog());
    QTRY_VERIFY(dialogHelper.isQuickDialogOpen());

    // Should now be updated
    QCOMPARE(titleLabel->text(), newTitle2);

    CLOSE_DIALOG("Ok");
}

/*
    Represents the expected search logic we use when the user types text into
    one of the read-only text edits.

    As we test against real fonts installed on the system and hence cannot use
    a hard-coded list, we need this helper to ensure that the (non-trivial)
    searching behavior matches what we expect.
*/
class ListSearchHelper
{
public:
    ListSearchHelper(const QStringList &model)
        : m_model(model)
    {
    }

    int expectedCurrentIndexForSearch(const QString &searchText)
    {
        bool redo = false;

        do {
            m_searchText.append(searchText);

            for (int i = 0; i < m_model.count(); ++i) {
                if (m_model.at(i).startsWith(m_searchText, Qt::CaseInsensitive))
                    return i;
            }

            m_searchText.clear();

            redo = !redo;
        } while (redo);

        return -1;
    }

private:
    QStringList m_model;
    QString m_searchText;
};

void tst_QQuickFontDialogImpl::searchFamily()
{
    CREATE_DIALOG_TEST_HELPER

    // Open the dialog.
    QVERIFY(dialogHelper.openDialog());
    QTRY_VERIFY(dialogHelper.isQuickDialogOpen());

    const QQuickTextField *familyEdit =
            dialogHelper.quickDialog->findChild<QQuickTextField *>("familyEdit");
    QVERIFY(familyEdit);

    QQuickListView *fontFamilyListView =
            dialogHelper.quickDialog->findChild<QQuickListView *>("familyListView");
    QVERIFY(fontFamilyListView);

    const QPoint familyEditCenterPos =
            familyEdit->mapToScene({ familyEdit->width() / 2, familyEdit->height() / 2 }).toPoint();
    QTest::mouseClick(dialogHelper.window(), Qt::LeftButton, Qt::NoModifier, familyEditCenterPos);
    QVERIFY(familyEdit->hasActiveFocus());

    QSignalSpy familyListViewCurrentIndexSpy(fontFamilyListView, SIGNAL(currentIndexChanged()));
    QVERIFY(familyListViewCurrentIndexSpy.isValid());

    const QString alphabet("abcdefghijklmnopqrstuvwxyz");
    QStringList model = QFontDatabase::families(QFontDatabase::WritingSystem::Any);
    model.removeIf(QFontDatabase::isPrivateFamily);

    ListSearchHelper listSearchHelper(model);

    // For each letter in the alphabet, press the corresponding key
    // and check that the relevant delegate item in familyListView is selected.
    for (auto alphabet_it = alphabet.cbegin(); alphabet_it != alphabet.cend(); alphabet_it++) {
        const int previousIndex = fontFamilyListView->currentIndex();

        const char keyEntered = alphabet_it->toLatin1();
        QTest::keyClick(dialogHelper.window(), keyEntered);

        int expectedListViewIndex = listSearchHelper.expectedCurrentIndexForSearch(QString(keyEntered));
        if (expectedListViewIndex == -1) {
            // There was no match, so the currentIndex should remain unchanged.
            expectedListViewIndex = previousIndex;
        }
        if (fontFamilyListView->currentIndex() == expectedListViewIndex) {
            // Working as expected; keep testing.
            continue;
        }

        // Get the actual text of the current delegate item and the expected text.
        auto currentDelegateItem = findViewDelegateItem(fontFamilyListView, fontFamilyListView->currentIndex());
        QVERIFY(currentDelegateItem);
        const auto actualDelegateText = currentDelegateItem->property("text").toString();
        const auto expectedDelegateText = expectedListViewIndex != -1 ? model.at(expectedListViewIndex) : "(none)";

        QFAIL(qPrintable(QString::fromLatin1("Expected fontFamilyListView to"
            " change its currentIndex to %1 (%2) after typing '%3', but it is %4 (%5)")
                .arg(expectedListViewIndex)
                .arg(expectedDelegateText)
                .arg(keyEntered)
                .arg(fontFamilyListView->currentIndex())
                .arg(actualDelegateText)));
    }

    CLOSE_DIALOG("Ok");
}

int main(int argc, char *argv[])
{
    // We need to set this attribute, and this (defining main() ourselves and
    // calling QTEST_MAIN_IMPL) seems to be the nicest way to do it without
    // duplicating too much code.
    // We also don't want to run this for every style, as each one will have
    // different ways of implementing the dialogs.
    QCoreApplication::setAttribute(Qt::AA_DontUseNativeDialogs);
    // For now we only test one style.
    // TODO: use Basic
    QQuickStyle::setStyle("Fusion");
    QTEST_MAIN_IMPL(tst_QQuickFontDialogImpl)
}

#include "tst_qquickfontdialogimpl.moc"