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