1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "dialogstestutils_p.h" |
5 | |
6 | #include <QtTest/qsignalspy.h> |
7 | #include <QtQuick/private/qtquickglobal_p.h> |
8 | #if QT_CONFIG(quick_listview) |
9 | #include <QtQuick/private/qquicklistview_p.h> |
10 | #endif |
11 | #include <QtQuickTest/quicktest.h> |
12 | #include <QtQuickControls2/qquickstyle.h> |
13 | #include <QtQuickTemplates2/private/qquickabstractbutton_p.h> |
14 | #include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> |
15 | #include <QtQuickTemplates2/private/qquickdialogbuttonbox_p.h> |
16 | #if QT_CONFIG(quick_listview) |
17 | #include <QtQuickDialogs2QuickImpl/private/qquickfiledialogdelegate_p.h> |
18 | #include <QtQuickDialogs2QuickImpl/private/qquickfolderbreadcrumbbar_p.h> |
19 | #include <QtQuickDialogs2QuickImpl/private/qquickfolderbreadcrumbbar_p_p.h> |
20 | #endif |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | #if QT_CONFIG(quick_listview) |
25 | bool QQuickDialogTestUtils::verifyFileDialogDelegates(QQuickListView *fileDialogListView, |
26 | const QStringList &expectedFiles, QString &failureMessage) |
27 | { |
28 | if (QQuickTest::qIsPolishScheduled(item: fileDialogListView)) { |
29 | if (!QQuickTest::qWaitForPolish(item: fileDialogListView)) { |
30 | failureMessage = QLatin1String("Failed to polish fileDialogListView" ); |
31 | return false; |
32 | } |
33 | } |
34 | |
35 | QStringList actualFiles; |
36 | for (int i = 0; i < fileDialogListView->count(); ++i) { |
37 | auto delegate = qobject_cast<QQuickFileDialogDelegate*>( |
38 | object: QQuickVisualTestUtils::findViewDelegateItem(itemView: fileDialogListView, index: i)); |
39 | if (!delegate) { |
40 | failureMessage = QString::fromLatin1(ba: "Delegate at index %1 is null" ).arg(a: i); |
41 | return false; |
42 | } |
43 | |
44 | // Need to call absoluteFilePath on Windows; see comment in dialogtestutil.h. |
45 | actualFiles.append(t: QFileInfo(delegate->file().toLocalFile()).absoluteFilePath()); |
46 | } |
47 | |
48 | if (actualFiles != expectedFiles) { |
49 | QString expectedFilesStr = QDebug::toString(object: expectedFiles); |
50 | QString actualFilesStr = QDebug::toString(object&: actualFiles); |
51 | failureMessage = QString::fromLatin1(ba: "Mismatch in actual vs expected " |
52 | "delegates in fileDialogListView:\n expected: %1\n actual: %2" ); |
53 | if (failureMessage.size() + expectedFilesStr.size() + actualFilesStr.size() > 1024) { |
54 | // If we've exceeded QTest's character limit for failure messages, |
55 | // just show the number of files. |
56 | expectedFilesStr = QString::number(expectedFiles.size()); |
57 | actualFilesStr = QString::number(actualFiles.size()); |
58 | } |
59 | failureMessage = failureMessage.arg(args&: expectedFilesStr, args&: actualFilesStr); |
60 | return false; |
61 | } |
62 | |
63 | return true; |
64 | } |
65 | |
66 | bool QQuickDialogTestUtils::verifyBreadcrumbDelegates(QQuickFolderBreadcrumbBar *breadcrumbBar, |
67 | const QUrl &expectedFolder, QString &failureMessage) |
68 | { |
69 | if (!breadcrumbBar) { |
70 | failureMessage = QLatin1String("breadcrumbBar is null" ); |
71 | return false; |
72 | } |
73 | |
74 | auto breadcrumbBarListView = qobject_cast<QQuickListView*>(object: breadcrumbBar->contentItem()); |
75 | if (!breadcrumbBarListView) { |
76 | failureMessage = QLatin1String("breadcrumbBar's ListView is null" ); |
77 | return false; |
78 | } |
79 | |
80 | if (QQuickTest::qIsPolishScheduled(item: breadcrumbBarListView)) { |
81 | if (!QQuickTest::qWaitForPolish(item: breadcrumbBarListView)) { |
82 | failureMessage = QLatin1String("Failed to polish breadcrumbBarListView" ); |
83 | return false; |
84 | } |
85 | } |
86 | |
87 | QStringList actualCrumbs; |
88 | for (int i = 0; i < breadcrumbBarListView->count(); ++i) { |
89 | auto delegate = qobject_cast<QQuickAbstractButton*>( |
90 | object: QQuickVisualTestUtils::findViewDelegateItem(itemView: breadcrumbBarListView, index: i)); |
91 | if (!delegate) { |
92 | // It's a separator or some other non-crumb item. |
93 | continue; |
94 | } |
95 | |
96 | actualCrumbs.append(t: delegate->text()); |
97 | } |
98 | |
99 | QStringList expectedCrumbs = QQuickFolderBreadcrumbBarPrivate::crumbPathsForFolder(folder: expectedFolder); |
100 | for (int i = 0; i < expectedCrumbs.size(); ++i) { |
101 | QString &crumbPath = expectedCrumbs[i]; |
102 | crumbPath = QQuickFolderBreadcrumbBarPrivate::folderBaseName(folderPath: crumbPath); |
103 | } |
104 | |
105 | if (actualCrumbs != expectedCrumbs) { |
106 | failureMessage = QString::fromLatin1(ba: "Mismatch in actual vs expected " |
107 | "delegates in breadcrumbBarListView:\n expected: %1\n actual: %2" ) |
108 | .arg(args: QDebug::toString(object&: expectedCrumbs), args: QDebug::toString(object&: actualCrumbs)); |
109 | return false; |
110 | } |
111 | |
112 | return true; |
113 | } |
114 | #endif |
115 | |
116 | QQuickAbstractButton *QQuickDialogTestUtils::findDialogButton(QQuickDialogButtonBox *box, const QString &buttonText) |
117 | { |
118 | for (int i = 0; i < box->count(); ++i) { |
119 | auto button = qobject_cast<QQuickAbstractButton*>(object: box->itemAt(index: i)); |
120 | if (button && button->text().toUpper() == buttonText.toUpper()) |
121 | return button; |
122 | } |
123 | return nullptr; |
124 | } |
125 | |
126 | void QQuickDialogTestUtils::enterText(QWindow *window, const QString &textToEnter) |
127 | { |
128 | for (int i = 0; i < textToEnter.size(); ++i) { |
129 | const QChar key = textToEnter.at(i); |
130 | QTest::keyClick(window, key: key.toLatin1()); |
131 | } |
132 | } |
133 | |
134 | QT_END_NAMESPACE |
135 | |