Changeset View
Changeset View
Standalone View
Standalone View
autotests/openurljobtest.cpp
Show All 33 Lines | |||||
34 | #include <signal.h> // kill | 34 | #include <signal.h> // kill | ||
35 | #endif | 35 | #endif | ||
36 | 36 | | |||
37 | #include <KSharedConfig> | 37 | #include <KSharedConfig> | ||
38 | #include <QStandardPaths> | 38 | #include <QStandardPaths> | ||
39 | #include <QTemporaryDir> | 39 | #include <QTemporaryDir> | ||
40 | #include <QTemporaryFile> | 40 | #include <QTemporaryFile> | ||
41 | #include <QTest> | 41 | #include <QTest> | ||
42 | #include <openurljobhandlerinterface.h> | ||||
42 | #include <untrustedprogramhandlerinterface.h> | 43 | #include <untrustedprogramhandlerinterface.h> | ||
43 | 44 | | |||
44 | QTEST_GUILESS_MAIN(OpenUrlJobTest) | 45 | QTEST_GUILESS_MAIN(OpenUrlJobTest) | ||
45 | 46 | | |||
46 | extern KSERVICE_EXPORT int ksycoca_ms_between_checks; | 47 | extern KSERVICE_EXPORT int ksycoca_ms_between_checks; | ||
47 | 48 | | |||
48 | namespace KIO { | 49 | namespace KIO { | ||
49 | KIOGUI_EXPORT void setDefaultUntrustedProgramHandler(KIO::UntrustedProgramHandlerInterface *iface); | 50 | KIOGUI_EXPORT void setDefaultUntrustedProgramHandler(KIO::UntrustedProgramHandlerInterface *iface); | ||
51 | KIOGUI_EXPORT void setDefaultOpenUrlJobHandler(KIO::OpenUrlJobHandlerInterface *iface); | ||||
50 | } | 52 | } | ||
51 | class TestUntrustedProgramHandler : public KIO::UntrustedProgramHandlerInterface | 53 | class TestUntrustedProgramHandler : public KIO::UntrustedProgramHandlerInterface | ||
52 | { | 54 | { | ||
53 | public: | 55 | public: | ||
54 | void showUntrustedProgramWarning(KJob *job, const QString &programName) override { | 56 | void showUntrustedProgramWarning(KJob *job, const QString &programName) override { | ||
55 | Q_UNUSED(job) | 57 | Q_UNUSED(job) | ||
56 | m_calls << programName; | 58 | m_calls << programName; | ||
57 | Q_EMIT result(m_retVal); | 59 | Q_EMIT result(m_retVal); | ||
58 | } | 60 | } | ||
59 | 61 | | |||
60 | void setRetVal(bool b) { m_retVal = b; } | 62 | void setRetVal(bool b) { m_retVal = b; } | ||
61 | 63 | | |||
62 | QStringList m_calls; | 64 | QStringList m_calls; | ||
63 | bool m_retVal = false; | 65 | bool m_retVal = false; | ||
64 | }; | 66 | }; | ||
65 | | ||||
66 | static TestUntrustedProgramHandler s_handler; | 67 | static TestUntrustedProgramHandler s_handler; | ||
67 | 68 | | |||
69 | class TestOpenUrlJobHandler : public KIO::OpenUrlJobHandlerInterface | ||||
70 | { | ||||
71 | public: | ||||
72 | void promptUserForApplication(KIO::OpenUrlJob *job, const QUrl &url, const QString &mimeType) override | ||||
73 | { | ||||
74 | Q_UNUSED(job); | ||||
75 | m_urls << url; | ||||
76 | m_mimeTypes << mimeType; | ||||
77 | if (m_chosenService) { | ||||
78 | Q_EMIT serviceSelected(m_chosenService); | ||||
79 | } else { | ||||
80 | Q_EMIT canceled(); | ||||
81 | } | ||||
82 | } | ||||
83 | QList<QUrl> m_urls; | ||||
84 | QStringList m_mimeTypes; | ||||
85 | KService::Ptr m_chosenService; | ||||
86 | }; | ||||
87 | static TestOpenUrlJobHandler s_openUrlJobHandler; | ||||
88 | | ||||
68 | static const char s_tempServiceName[] = "openurljobtest_service.desktop"; | 89 | static const char s_tempServiceName[] = "openurljobtest_service.desktop"; | ||
69 | 90 | | |||
70 | void OpenUrlJobTest::initTestCase() | 91 | void OpenUrlJobTest::initTestCase() | ||
71 | { | 92 | { | ||
72 | QStandardPaths::setTestModeEnabled(true); | 93 | QStandardPaths::setTestModeEnabled(true); | ||
73 | 94 | | |||
74 | // Ensure no leftovers from other tests | 95 | // Ensure no leftovers from other tests | ||
75 | QDir(QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)).removeRecursively(); | 96 | QDir(QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)).removeRecursively(); | ||
▲ Show 20 Lines • Show All 361 Lines • ▼ Show 20 Line(s) | 457 | connect(job, &KIO::OpenUrlJob::mimeTypeFound, this, [&](const QString &mimeType) { | |||
437 | foundMime = mimeType; | 458 | foundMime = mimeType; | ||
438 | job->kill(); | 459 | job->kill(); | ||
439 | }); | 460 | }); | ||
440 | QVERIFY(!job->exec()); | 461 | QVERIFY(!job->exec()); | ||
441 | QCOMPARE(job->error(), KJob::KilledJobError); | 462 | QCOMPARE(job->error(), KJob::KilledJobError); | ||
442 | QCOMPARE(foundMime, "image/jpeg"); | 463 | QCOMPARE(foundMime, "image/jpeg"); | ||
443 | } | 464 | } | ||
444 | 465 | | |||
466 | void OpenUrlJobTest::onlyOpenWithDialog_data() | ||||
467 | { | ||||
468 | QTest::addColumn<bool>("knownMimeType"); | ||||
469 | QTest::addColumn<bool>("handlerRetVal"); | ||||
470 | | ||||
471 | QTest::newRow("false_knownMimeType") << true << false; | ||||
472 | QTest::newRow("true_knownMimeType") << true << true; | ||||
473 | | ||||
474 | QTest::newRow("false_unknownMimeType") << false << false; | ||||
475 | QTest::newRow("true_unknownMimeType") << false << true; | ||||
476 | } | ||||
477 | | ||||
478 | void OpenUrlJobTest::onlyOpenWithDialog() | ||||
479 | { | ||||
480 | #ifdef Q_OS_UNIX | ||||
481 | QFETCH(bool, knownMimeType); | ||||
482 | QFETCH(bool, handlerRetVal); | ||||
483 | | ||||
484 | // Given a local text file | ||||
485 | QTemporaryDir tempDir; | ||||
486 | const QString srcDir = tempDir.path(); | ||||
487 | const QString srcFile = srcDir + QLatin1String("/file.txt"); | ||||
488 | createSrcFile(srcFile); | ||||
489 | | ||||
490 | const QString mimeType = knownMimeType ? "text/plain" : ""; | ||||
491 | KIO::OpenUrlJob *job = new KIO::OpenUrlJob(QUrl::fromLocalFile(srcFile), mimeType, this); | ||||
492 | job->setShowOpenWithDialog(true); | ||||
493 | | ||||
494 | KService::Ptr service = KService::serviceByDesktopName(QString(s_tempServiceName).remove(".desktop")); | ||||
495 | QVERIFY(service); | ||||
496 | s_openUrlJobHandler.m_urls.clear(); | ||||
497 | s_openUrlJobHandler.m_mimeTypes.clear(); | ||||
498 | s_openUrlJobHandler.m_chosenService = handlerRetVal ? service : KService::Ptr{}; | ||||
499 | KIO::setDefaultOpenUrlJobHandler(&s_openUrlJobHandler); | ||||
500 | | ||||
501 | const bool success = job->exec(); | ||||
502 | | ||||
503 | // Then --- it depends on what the user says via the handler | ||||
504 | | ||||
505 | QCOMPARE(s_openUrlJobHandler.m_urls.count(), 1); | ||||
506 | QCOMPARE(s_openUrlJobHandler.m_mimeTypes.count(), 1); | ||||
507 | QCOMPARE(s_openUrlJobHandler.m_mimeTypes.at(0), "text/plain"); | ||||
508 | if (handlerRetVal) { | ||||
509 | QVERIFY2(success, qPrintable(job->errorString())); | ||||
510 | // If the user chose a service, it should be executed (it writes to "dest") | ||||
511 | const QString dest = m_tempDir.path() + "/dest"; | ||||
512 | QTRY_VERIFY2(QFile::exists(dest), qPrintable(dest)); | ||||
513 | QCOMPARE(readFile(dest), srcFile); | ||||
514 | } else { | ||||
515 | QVERIFY(!success); | ||||
516 | QCOMPARE(job->error(), KIO::ERR_USER_CANCELED); | ||||
517 | } | ||||
518 | #else | ||||
519 | QSKIP("Test skipped on Windows because the code ends up in QDesktopServices::openUrl") | ||||
520 | #endif | ||||
521 | } | ||||
522 | | ||||
445 | void OpenUrlJobTest::writeApplicationDesktopFile(const QString &filePath) | 523 | void OpenUrlJobTest::writeApplicationDesktopFile(const QString &filePath) | ||
446 | { | 524 | { | ||
447 | KDesktopFile file(filePath); | 525 | KDesktopFile file(filePath); | ||
448 | KConfigGroup group = file.desktopGroup(); | 526 | KConfigGroup group = file.desktopGroup(); | ||
449 | group.writeEntry("Name", "KRunUnittestService"); | 527 | group.writeEntry("Name", "KRunUnittestService"); | ||
450 | group.writeEntry("MimeType", "text/plain;application/x-shellscript;x-scheme-handler/scheme"); | 528 | group.writeEntry("MimeType", "text/plain;application/x-shellscript;x-scheme-handler/scheme"); | ||
451 | group.writeEntry("Type", "Application"); | 529 | group.writeEntry("Type", "Application"); | ||
452 | group.writeEntry("Exec", QByteArray("echo %u > " + QFile::encodeName(m_tempDir.path()) + "/dest")); // not using %d because of remote urls | 530 | group.writeEntry("Exec", QByteArray("echo %u > " + QFile::encodeName(m_tempDir.path()) + "/dest")); // not using %d because of remote urls | ||
453 | QVERIFY(file.sync()); | 531 | QVERIFY(file.sync()); | ||
454 | } | 532 | } |