Debian Codegraph @

tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp

raw
1 // Copyright (C) 2013 BlackBerry Limited. All rights reserved.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QTest>
5 #include <qplatformdefs.h>
6
7 #include <QCoreApplication>
8 #include <QDebug>
9 #include <QDir>
10 #include <QFile>
11 #include <QFileInfo>
12 #include <QString>
13
14 #include <private/qfileselector_p.h>
15 #include <private/qabstractfileengine_p.h>
16 #include <private/qfsfileengine_p.h>
17 #include <private/qfilesystemengine_p.h>
18
19 const ushort selectorIndicator = '+';
20
21 class tst_QFileSelector : public QObject
22 {
23 Q_OBJECT
24 public:
25 tst_QFileSelector() {}
26
27 private slots:
28 void basicTest_data();
29 void basicTest();
30
31 void urlConvenience_data();
32 void urlConvenience();
33
34 void addStatics();
35 };
36
37 void tst_QFileSelector::basicTest_data()
38 {
39 /* Files existing for this test
40 * platform/test
41 * platform/+<platform>/test for all <platform> in QFileSelectorPrivate::platformSelectors()
42 * extras/test
43 * extras/test2 to test for when selector directories exist, but don't have the files
44 * extras/+custom1/test
45 * extras/+custom1/test3 to test for when base file doesn't exist
46 * extras/+custom2/test
47 * extras/+custom3/test
48 * extras/+custom3/+custom2/test
49 * extras/+custom3/+custom4/test
50 * extras/+custom3/+custom5/test
51 * extras/+custom5/+custom3/test
52 */
53 QTest::addColumn<QString>("testPath");
54 QTest::addColumn<QStringList>("customSelectors");
55 QTest::addColumn<QString>("expectedPath");
56
57 QString test("/test");// '/' is here so dir string can also be selector string
58 QString test2("/test2");
59 QString test3("/test3");
60 QString expectedPlatform1File(":/platforms");
61 QString expectedPlatform2File(""); //Only the last selector
62 QString expectedPlatform3File; // Only the first selector (the family)
63 #if defined(Q_OS_UNIX) && !defined(Q_OS_ANDROID) && !defined(Q_OS_WASM) && \
64 !defined(Q_OS_DARWIN) && !defined(Q_OS_LINUX) && !defined(Q_OS_HAIKU) && !defined(Q_OS_QNX) && !defined(Q_OS_VXWORKS)
65 /* We are only aware of specific unixes, and do not have test files for any of the others.
66 However those unixes can get a selector added from the result of a uname call, so this will
67 lead to a case where we don't have that file so we can't expect the concatenation of platform
68 selectors to work. It should just find the +unix/test file.*/
69 expectedPlatform1File = QString(":/platforms/") + QLatin1Char(selectorIndicator)
70 + QString("unix/test");
71 expectedPlatform2File = QString(":/platforms/test2");
72 #else
73 QString distributionName;
74 # if (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) || defined(Q_OS_FREEBSD)
75 distributionName = QSysInfo::productType();
76 # endif
77 const auto platformSelectors = QFileSelectorPrivate::platformSelectors();
78 for (const QString &selector : platformSelectors) {
79 // skip the Linux distribution name (if any) since we don't have files for them
80 if (selector == distributionName)
81 continue;
82
83 expectedPlatform1File = expectedPlatform1File + QLatin1Char('/') + QLatin1Char(selectorIndicator)
84 + selector;
85 expectedPlatform2File = selector;
86 if (expectedPlatform3File.isNull())
87 expectedPlatform3File = selector;
88 }
89 expectedPlatform1File += test;
90 expectedPlatform2File = QLatin1String(":/platforms/") + QLatin1Char(selectorIndicator)
91 + expectedPlatform2File + test2;
92 expectedPlatform3File = QLatin1String(":/platforms/") + QLatin1Char(selectorIndicator)
93 + expectedPlatform3File + test3;
94 #endif
95
96 QTest::newRow("platform1") << QString(":/platforms/test") << QStringList()
97 << expectedPlatform1File;
98
99 QTest::newRow("platform2") << QString(":/platforms/test2") << QStringList()
100 << expectedPlatform2File;
101
102 QTest::newRow("platform3") << QString(":/platforms/test3") << QStringList()
103 << expectedPlatform3File;
104
105 QString resourceTestPath(":/extras/test");
106 QString custom1("custom1");
107 QTest::newRow("custom1-noselector") << resourceTestPath << QStringList()
108 << QString(":/extras") + test;
109
110 QTest::newRow("custom1-withselector") << resourceTestPath << (QStringList() << custom1)
111 << QString(":/extras/") + QLatin1Char(selectorIndicator) + custom1 + test;
112
113 QTest::newRow("customX-withselector-nofile") << QString(":/extras/test2") << (QStringList() << custom1)
114 << QString(":/extras/test2");
115
116 QTest::newRow("custom1-withselector-nobasefile") << QString(":/extras/test3") << (QStringList() << custom1)
117 << QString(":/extras/+custom1/test3");
118
119 QString custom2("custom2");
120 QString custom3("custom3");
121 QString custom4("custom4");
122 QString custom5("custom5");
123 QString slash("/");
124 QTest::newRow("custom12") << resourceTestPath << (QStringList() << custom1 << custom2)
125 << QString(":/extras/") + QLatin1Char(selectorIndicator) + custom1 + test;
126
127 QTest::newRow("custom21") << resourceTestPath << (QStringList() << custom2 << custom1)
128 << QString(":/extras/") + QLatin1Char(selectorIndicator) + custom2 + test;
129
130 QTest::newRow("custom213") << resourceTestPath << (QStringList() << custom2 << custom1 << custom3)
131 << QString(":/extras/") + QLatin1Char(selectorIndicator) + custom2 + test;
132
133 QTest::newRow("custom23") << resourceTestPath << (QStringList() << custom2 << custom3)
134 << QString(":/extras/") + QLatin1Char(selectorIndicator) + custom2 + test;
135
136 QTest::newRow("custom34nested") << resourceTestPath << (QStringList() << custom3 << custom4)
137 << QString(":/extras/") + QLatin1Char(selectorIndicator) + custom3 + slash
138 + QLatin1Char(selectorIndicator) + custom4 + test;
139
140 QTest::newRow("custom43nested") << resourceTestPath << (QStringList() << custom4 << custom3)
141 << QString(":/extras/") + QLatin1Char(selectorIndicator) + custom3 + slash
142 + QLatin1Char(selectorIndicator) + custom4 + test;
143
144 QTest::newRow("custom35conflict") << resourceTestPath << (QStringList() << custom3 << custom5)
145 << QString(":/extras/") + QLatin1Char(selectorIndicator) + custom3 + slash
146 + QLatin1Char(selectorIndicator) + custom5 + test;
147
148 QTest::newRow("relativePaths") << QFINDTESTDATA("extras/test") << (QStringList() << custom1)
149 << QFINDTESTDATA(QString("extras/") + QLatin1Char(selectorIndicator) + custom1
150 + QString("/test"));
151 }
152
153 void tst_QFileSelector::basicTest()
154 {
155 QFETCH(QString, testPath);
156 QFETCH(QStringList, customSelectors);
157 QFETCH(QString, expectedPath);
158
159 QFileSelector fs;
160 fs.setExtraSelectors(customSelectors);
161 QCOMPARE(fs.select(testPath), expectedPath);
162 }
163
164 void tst_QFileSelector::urlConvenience_data()
165 {
166 /* Files existing for this test
167 * extras/test
168 * extras/+custom1/test
169 */
170 QTest::addColumn<QUrl>("testUrl");
171 QTest::addColumn<QStringList>("customSelectors");
172 QTest::addColumn<QUrl>("expectedUrl");
173
174 QString test("/test");// '/' is here so dir string can also be selector string
175 QString custom1("custom1");
176 QString testWithQueryAndFragment("/test?query#Fragment");
177
178 QTest::newRow("qrc") << QUrl("qrc:///extras/test") << (QStringList() << custom1)
179 << QUrl(QString("qrc:///extras/") + QLatin1Char(selectorIndicator) + custom1 + test);
180 QTest::newRow("qrc with query and fragment") << QUrl(QString::fromLatin1("qrc:///extras%1").arg(testWithQueryAndFragment)) << (QStringList() << custom1)
181 << QUrl(QString("qrc:///extras/") + QLatin1Char(selectorIndicator) + custom1 + testWithQueryAndFragment);
182
183 QString fileBasePath = QFINDTESTDATA("extras/test");
184 QString fileSelectedPath = QFINDTESTDATA(QString("extras/") + QLatin1Char(selectorIndicator)
185 + custom1 + QString("/test"));
186 QTest::newRow("file") << QUrl::fromLocalFile(fileBasePath) << (QStringList() << custom1)
187 << QUrl::fromLocalFile(fileSelectedPath);
188 // do not strip off the query and fragment
189 QString strUrlWithFragment = QString("file://") + testWithQueryAndFragment;
190 QTest::newRow("file with query and fragment") << QUrl(strUrlWithFragment) << (QStringList()) << QUrl(strUrlWithFragment);
191 strUrlWithFragment = QString("file:") + testWithQueryAndFragment;
192 QTest::newRow("file with query and fragment too") << QUrl(strUrlWithFragment) << (QStringList()) << QUrl(strUrlWithFragment);
193
194 // preserve path to root
195 QTest::newRow("path to root") << QUrl("file:///") << (QStringList()) << QUrl("file:///");
196
197 // http://qt-project.org/images/qtdn/sprites-combined-latest.png is chosen as a representative real world URL
198 // But note that this test is checking that http urls are NOT selected so it shouldn't be checked
199 QUrl testHttpUrl("http://qt-project.org/images/sprites-combined-latest.png");
200 QTest::newRow("http") << testHttpUrl << (QStringList() << QString("qtdn")) << testHttpUrl;
201 }
202
203 void tst_QFileSelector::urlConvenience()
204 {
205 QFETCH(QUrl, testUrl);
206 QFETCH(QStringList, customSelectors);
207 QFETCH(QUrl, expectedUrl);
208
209 QFileSelector fs;
210 //All rows of this test use only custom selectors, so should not select before the setExtra call
211 QCOMPARE(fs.select(testUrl), testUrl);
212 fs.setExtraSelectors(customSelectors);
213 QCOMPARE(fs.select(testUrl), expectedUrl);
214 }
215
216 void tst_QFileSelector::addStatics()
217 {
218 QFileSelector fs;
219 QCOMPARE(fs.select(QStringLiteral(":/extras/test")), QStringLiteral(":/extras/test"));
220
221 QFileSelectorPrivate::addStatics(QStringList() << QStringLiteral("custom1"));
222 QCOMPARE(fs.select(QStringLiteral(":/extras/test")), QStringLiteral(":/extras/+custom1/test"));
223 }
224
225 QTEST_MAIN(tst_QFileSelector)
226 #include "tst_qfileselector.moc"