Changeset View
Changeset View
Standalone View
Standalone View
autotests/kfilewidgettest.cpp
Show All 23 Lines | |||||
24 | #include <QLabel> | 24 | #include <QLabel> | ||
25 | #include <QTest> | 25 | #include <QTest> | ||
26 | #include <QStandardPaths> | 26 | #include <QStandardPaths> | ||
27 | 27 | | |||
28 | #include <kdiroperator.h> | 28 | #include <kdiroperator.h> | ||
29 | #include <klocalizedstring.h> | 29 | #include <klocalizedstring.h> | ||
30 | #include <kurlnavigator.h> | 30 | #include <kurlnavigator.h> | ||
31 | #include <KUrlComboBox> | 31 | #include <KUrlComboBox> | ||
32 | #include <KFileFilterCombo> | ||||
33 | #include "kiotesthelper.h" // createTestFile | ||||
32 | 34 | | |||
33 | /** | 35 | /** | ||
34 | * Unit test for KFileWidget | 36 | * Unit test for KFileWidget | ||
35 | */ | 37 | */ | ||
36 | class KFileWidgetTest : public QObject | 38 | class KFileWidgetTest : public QObject | ||
37 | { | 39 | { | ||
38 | Q_OBJECT | 40 | Q_OBJECT | ||
39 | private Q_SLOTS: | 41 | private Q_SLOTS: | ||
▲ Show 20 Lines • Show All 200 Lines • ▼ Show 20 Line(s) | 235 | { | |||
240 | QUrl u(expectedUrl); | 242 | QUrl u(expectedUrl); | ||
241 | if (!u.path().isEmpty() && !u.path().endsWith(QLatin1Char('/'))) { | 243 | if (!u.path().isEmpty() && !u.path().endsWith(QLatin1Char('/'))) { | ||
242 | u.setPath(u.path() + QLatin1Char('/')); | 244 | u.setPath(u.path() + QLatin1Char('/')); | ||
243 | } | 245 | } | ||
244 | // THEN | 246 | // THEN | ||
245 | QVERIFY(u.isValid()); | 247 | QVERIFY(u.isValid()); | ||
246 | QCOMPARE(u, expectedUrl); | 248 | QCOMPARE(u, expectedUrl); | ||
247 | } | 249 | } | ||
250 | | ||||
251 | void testSetFilterForSave_data() | ||||
252 | { | ||||
253 | QTest::addColumn<QString>("fileName"); | ||||
254 | QTest::addColumn<QString>("filter"); | ||||
255 | QTest::addColumn<QString>("expectedCurrentText"); | ||||
256 | QTest::addColumn<QString>("expectedSelectedFileName"); | ||||
257 | | ||||
258 | const QString filter = QStringLiteral("*.txt|Text files\n*.HTML|HTML files"); | ||||
259 | | ||||
260 | QTest::newRow("some.txt") | ||||
261 | << "some.txt" | ||||
262 | << filter | ||||
263 | << QStringLiteral("some.txt") | ||||
264 | << QStringLiteral("some.txt"); | ||||
265 | | ||||
266 | // If an application provides a name without extension, then the | ||||
267 | // displayed name will not receive an extension. It will however be | ||||
268 | // appended when the dialog is closed. | ||||
269 | QTest::newRow("extensionless name") | ||||
270 | << "some" | ||||
271 | << filter | ||||
272 | << QStringLiteral("some") | ||||
273 | << QStringLiteral("some.txt"); | ||||
274 | | ||||
275 | // If the file literally exists, then no new extension will be appended. | ||||
276 | QTest::newRow("existing file") | ||||
277 | << "README" | ||||
278 | << filter | ||||
279 | << QStringLiteral("README") | ||||
280 | << QStringLiteral("README"); | ||||
281 | | ||||
282 | // XXX perhaps the "extension" should not be modified when it does not | ||||
283 | // match any of the existing types? Should "some.2019.txt" be expected? | ||||
284 | QTest::newRow("some.2019") | ||||
285 | << "some.2019" | ||||
286 | << filter | ||||
287 | << QStringLiteral("some.txt") | ||||
288 | << QStringLiteral("some.txt"); | ||||
289 | | ||||
290 | // XXX be smarter and do not change the extension if one of the other | ||||
291 | // filters match. Should "some.html" be expected? | ||||
292 | QTest::newRow("some.html") | ||||
293 | << "some.html" | ||||
294 | << filter | ||||
295 | << QStringLiteral("some.txt") | ||||
296 | << QStringLiteral("some.txt"); | ||||
297 | } | ||||
298 | | ||||
299 | void testSetFilterForSave() | ||||
300 | { | ||||
301 | QFETCH(QString, fileName); | ||||
302 | QFETCH(QString, filter); | ||||
303 | QFETCH(QString, expectedCurrentText); | ||||
304 | QFETCH(QString, expectedSelectedFileName); | ||||
305 | | ||||
306 | // Use a temporary directory since the presence of existing files | ||||
307 | // influences whether an extension is automatically appended. | ||||
308 | QTemporaryDir tempDir; | ||||
309 | const QUrl dirUrl = QUrl::fromLocalFile(tempDir.path()); | ||||
310 | const QUrl fileUrl = QUrl::fromLocalFile(tempDir.filePath(fileName)); | ||||
311 | const QUrl expectedFileUrl = QUrl::fromLocalFile(tempDir.filePath(expectedSelectedFileName)); | ||||
312 | createTestFile(tempDir.filePath("README")); | ||||
313 | | ||||
314 | KFileWidget fw(dirUrl); | ||||
315 | fw.setOperationMode(KFileWidget::Saving); | ||||
316 | fw.setSelectedUrl(fileUrl); | ||||
317 | // Calling setFilter has side-effects and changes the file name. | ||||
318 | fw.setFilter(filter); | ||||
319 | fw.show(); | ||||
320 | fw.activateWindow(); | ||||
321 | QVERIFY(QTest::qWaitForWindowActive(&fw)); | ||||
322 | | ||||
323 | // Verify the expected populated name. | ||||
324 | QCOMPARE(fw.baseUrl().adjusted(QUrl::StripTrailingSlash), dirUrl); | ||||
325 | QCOMPARE(fw.locationEdit()->currentText(), expectedCurrentText); | ||||
326 | | ||||
327 | // QFileDialog ends up calling KDEPlatformFileDialog::selectedFiles() | ||||
328 | // which calls KFileWidget::selectedUrls(). | ||||
329 | // Accept the filename to ensure that a filename is selected. | ||||
330 | connect(&fw, &KFileWidget::accepted, &fw, &KFileWidget::accept); | ||||
331 | QTest::keyClick(fw.locationEdit(), Qt::Key_Return); | ||||
332 | QList<QUrl> urls = fw.selectedUrls(); | ||||
333 | QCOMPARE(urls.size(), 1); | ||||
334 | QCOMPARE(urls[0], expectedFileUrl); | ||||
335 | } | ||||
336 | | ||||
337 | void testFilterChange() | ||||
338 | { | ||||
339 | QTemporaryDir tempDir; | ||||
340 | createTestFile(tempDir.filePath("some.c")); | ||||
341 | bool created = QDir(tempDir.path()).mkdir("directory"); | ||||
342 | Q_ASSERT(created); | ||||
343 | | ||||
344 | KFileWidget fw(QUrl::fromLocalFile(tempDir.path())); | ||||
345 | fw.setOperationMode(KFileWidget::Saving); | ||||
346 | fw.setSelectedUrl(QUrl::fromLocalFile(tempDir.filePath("some.txt"))); | ||||
347 | fw.setFilter("*.txt|Txt\n*.c|C"); | ||||
348 | fw.show(); | ||||
349 | fw.activateWindow(); | ||||
350 | QVERIFY(QTest::qWaitForWindowActive(&fw)); | ||||
351 | | ||||
352 | // Initial filename. | ||||
353 | QCOMPARE(fw.locationEdit()->currentText(), QStringLiteral("some.txt")); | ||||
354 | QCOMPARE(fw.filterWidget()->currentFilter(), QStringLiteral("*.txt")); | ||||
355 | | ||||
356 | // Select type with an existing file. | ||||
357 | fw.filterWidget()->setCurrentFilter("*.c|C"); | ||||
358 | QCOMPARE(fw.locationEdit()->currentText(), QStringLiteral("some.c")); | ||||
359 | QCOMPARE(fw.filterWidget()->currentFilter(), QStringLiteral("*.c")); | ||||
360 | | ||||
361 | // Do not update extension if the current selection is a directory. | ||||
362 | fw.setSelectedUrl(QUrl::fromLocalFile(tempDir.filePath("directory"))); | ||||
363 | fw.filterWidget()->setCurrentFilter("*.txt|Txt"); | ||||
364 | QCOMPARE(fw.locationEdit()->currentText(), QStringLiteral("directory")); | ||||
365 | QCOMPARE(fw.filterWidget()->currentFilter(), QStringLiteral("*.txt")); | ||||
366 | } | ||||
248 | }; | 367 | }; | ||
249 | 368 | | |||
250 | QTEST_MAIN(KFileWidgetTest) | 369 | QTEST_MAIN(KFileWidgetTest) | ||
251 | 370 | | |||
252 | #include "kfilewidgettest.moc" | 371 | #include "kfilewidgettest.moc" |