Changeset View
Changeset View
Standalone View
Standalone View
autotests/kfilewidgettest.cpp
Show All 26 Lines | |||||
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> | 32 | #include <KFileFilterCombo> | ||
33 | #include "kiotesthelper.h" // createTestFile | 33 | #include "kiotesthelper.h" // createTestFile | ||
34 | 34 | | |||
35 | #include <QAbstractItemView> | ||||
36 | #include <QDropEvent> | ||||
37 | #include <QMimeData> | ||||
38 | #include <QDebug> | ||||
elvisangelaccio: Unused? | |||||
39 | | ||||
35 | /** | 40 | /** | ||
36 | * Unit test for KFileWidget | 41 | * Unit test for KFileWidget | ||
37 | */ | 42 | */ | ||
38 | class KFileWidgetTest : public QObject | 43 | class KFileWidgetTest : public QObject | ||
39 | { | 44 | { | ||
40 | Q_OBJECT | 45 | Q_OBJECT | ||
41 | private Q_SLOTS: | 46 | private Q_SLOTS: | ||
42 | void initTestCase() | 47 | void initTestCase() | ||
▲ Show 20 Lines • Show All 316 Lines • ▼ Show 20 Line(s) | 343 | { | |||
359 | QCOMPARE(fw.filterWidget()->currentFilter(), QStringLiteral("*.c")); | 364 | QCOMPARE(fw.filterWidget()->currentFilter(), QStringLiteral("*.c")); | ||
360 | 365 | | |||
361 | // Do not update extension if the current selection is a directory. | 366 | // Do not update extension if the current selection is a directory. | ||
362 | fw.setSelectedUrl(QUrl::fromLocalFile(tempDir.filePath("directory"))); | 367 | fw.setSelectedUrl(QUrl::fromLocalFile(tempDir.filePath("directory"))); | ||
363 | fw.filterWidget()->setCurrentFilter("*.txt|Txt"); | 368 | fw.filterWidget()->setCurrentFilter("*.txt|Txt"); | ||
364 | QCOMPARE(fw.locationEdit()->currentText(), QStringLiteral("directory")); | 369 | QCOMPARE(fw.locationEdit()->currentText(), QStringLiteral("directory")); | ||
365 | QCOMPARE(fw.filterWidget()->currentFilter(), QStringLiteral("*.txt")); | 370 | QCOMPARE(fw.filterWidget()->currentFilter(), QStringLiteral("*.txt")); | ||
366 | } | 371 | } | ||
372 | | ||||
373 | void testDropFile_data() | ||||
374 | { | ||||
375 | QTest::addColumn<QString>("dir"); | ||||
376 | QTest::addColumn<QString>("fileName"); | ||||
377 | QTest::addColumn<QString>("expectedCurrentText"); | ||||
378 | | ||||
379 | QTest::newRow("some.txt") | ||||
380 | << "" | ||||
381 | << "some.txt" | ||||
382 | << "some.txt" | ||||
383 | | ||||
384 | // XXX perhaps the "extension" should not be modified when it does not | ||||
385 | // match any of the existing types? Should "some.2019.txt" be expected? | ||||
386 | QTest::newRow("subdir/some.txt") | ||||
387 | << "subdir" | ||||
388 | << "subdir/some.txt" | ||||
389 | << "some.txt" | ||||
390 | } | ||||
391 | | ||||
392 | void testDropFile() | ||||
393 | { | ||||
394 | QFETCH(QString, dir); | ||||
395 | QFETCH(QString, fileName); | ||||
396 | QFETCH(QString, expectedCurrentText); | ||||
397 | | ||||
398 | // Use a temporary directory since the presence of existing files | ||||
399 | // influences whether an extension is automatically appended. | ||||
400 | QTemporaryDir tempDir; | ||||
401 | QUrl dirUrl = QUrl::fromLocalFile(tempDir.path()); | ||||
402 | const QUrl fileUrl = QUrl::fromLocalFile(tempDir.filePath(fileName)); | ||||
403 | if (!dir.isEmpty()) { | ||||
404 | createTestDirectory(tempDir.filePath(dir)); | ||||
405 | dirUrl = QUrl::fromLocalFile(tempDir.filePath(dir)); | ||||
406 | } | ||||
407 | createTestFile(tempDir.filePath(fileName)); | ||||
408 | | ||||
409 | QDir f(tempDir.filePath(dir)); | ||||
410 | | ||||
411 | KFileWidget fw(QUrl::fromLocalFile(tempDir.path())); | ||||
412 | fw.setOperationMode(KFileWidget::Opening); | ||||
413 | fw.setMode(KFile::File); | ||||
elvisangelaccio: Please use descriptive variable names instead of `f` and `fw` | |||||
414 | fw.show(); | ||||
415 | fw.activateWindow(); | ||||
elvisangelaccio: Missing camelCase | |||||
416 | | ||||
417 | QList<QUrl> list_urls = {fileUrl}; | ||||
418 | QMimeData *mimeData = new QMimeData(); | ||||
419 | mimeData->setUrls(list_urls); | ||||
420 | | ||||
421 | QDragEnterEvent event1(QPoint(), Qt::DropAction::MoveAction, mimeData, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier); | ||||
422 | | ||||
423 | QVERIFY(qApp->sendEvent(fw.dirOperator()->view()->viewport(), &event1)); | ||||
424 | | ||||
425 | // Fake drop | ||||
426 | QDropEvent event(QPoint(), Qt::DropAction::MoveAction, mimeData, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier); | ||||
427 | | ||||
428 | QVERIFY(qApp->sendEvent(fw.dirOperator()->view()->viewport(), &event)); | ||||
429 | | ||||
430 | QVERIFY(QTest::qWaitForWindowActive(&fw)); | ||||
431 | | ||||
432 | // once we drop a file the dirlister scans the dir | ||||
433 | // wait a bit to the dirlister time to finish | ||||
434 | QTest::qWait(100); | ||||
435 | | ||||
436 | // Verify the expected populated name. | ||||
437 | QCOMPARE(fw.baseUrl().adjusted(QUrl::StripTrailingSlash), dirUrl); | ||||
438 | QCOMPARE(fw.locationEdit()->currentText(), expectedCurrentText); | ||||
439 | | ||||
440 | // QFileDialog ends up calling KDEPlatformFileDialog::selectedFiles() | ||||
441 | // which calls KFileWidget::selectedUrls(). | ||||
442 | // Accept the filename to ensure that a filename is selected. | ||||
443 | connect(&fw, &KFileWidget::accepted, &fw, &KFileWidget::accept); | ||||
444 | QTest::keyClick(fw.locationEdit(), Qt::Key_Return); | ||||
445 | QList<QUrl> urls = fw.selectedUrls(); | ||||
446 | QCOMPARE(urls.size(), 1); | ||||
447 | QCOMPARE(urls[0], fileUrl); | ||||
448 | } | ||||
367 | }; | 449 | }; | ||
368 | 450 | | |||
369 | QTEST_MAIN(KFileWidgetTest) | 451 | QTEST_MAIN(KFileWidgetTest) | ||
370 | 452 | | |||
371 | #include "kfilewidgettest.moc" | 453 | #include "kfilewidgettest.moc" |
Unused?