File indexing completed on 2024-04-28 15:29:18
0001 /* 0002 SPDX-FileCopyrightText: 2009 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include <KApplicationTrader> 0008 #include <kparts/browseropenorsavequestion.h> 0009 #include <qtest_widgets.h> 0010 0011 #include <KConfigGroup> 0012 #include <KSharedConfig> 0013 #include <QDebug> 0014 0015 #include <QDialog> 0016 #include <QMenu> 0017 #include <QPushButton> 0018 #include <QWidget> 0019 0020 using namespace KParts; 0021 0022 // SYNC - keep this in sync with browseropenorsavequestion.cpp 0023 static const QString Save = QStringLiteral("saveButton"); 0024 static const QString OpenDefault = QStringLiteral("openDefaultButton"); 0025 static const QString OpenWith = QStringLiteral("openWithButton"); 0026 static const QString Cancel = QStringLiteral("cancelButton"); 0027 0028 class OpenOrSaveTest : public QObject 0029 { 0030 Q_OBJECT 0031 private Q_SLOTS: 0032 void initTestCase() 0033 { 0034 extern KSERVICE_EXPORT bool kservice_require_kded; 0035 kservice_require_kded = false; 0036 } 0037 void testAutoEmbed() 0038 { 0039 // This one should get the fast path, no dialog should show up. 0040 BrowserOpenOrSaveQuestion questionEmbedHtml(nullptr, QUrl(QStringLiteral("http://www.example.com/")), QString::fromLatin1("text/html")); 0041 QCOMPARE(questionEmbedHtml.askEmbedOrSave(), BrowserOpenOrSaveQuestion::Embed); 0042 } 0043 void testDontAskAgain() 0044 { 0045 KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("filetypesrc"), KConfig::NoGlobals); 0046 cfg->group("Notification Messages") 0047 .writeEntry( 0048 "askSave" 0049 "text/plain", 0050 "false"); 0051 BrowserOpenOrSaveQuestion question(nullptr, QUrl(QStringLiteral("http://www.example.com/")), QString::fromLatin1("text/plain")); 0052 QCOMPARE((int)question.askOpenOrSave(), (int)BrowserOpenOrSaveQuestion::Open); 0053 cfg->group("Notification Messages") 0054 .writeEntry( 0055 "askSave" 0056 "text/plain", 0057 "true"); 0058 QCOMPARE((int)question.askOpenOrSave(), (int)BrowserOpenOrSaveQuestion::Save); 0059 cfg->group("Notification Messages") 0060 .deleteEntry( 0061 "askSave" 0062 "text/plain"); 0063 } 0064 0065 void testAllChoices_data() 0066 { 0067 qRegisterMetaType<QDialog *>("QDialog*"); 0068 0069 QTest::addColumn<QString>("mimetype"); 0070 QTest::addColumn<QString>("buttonName"); 0071 QTest::addColumn<int>("expectedResult"); 0072 QTest::addColumn<bool>("expectedService"); 0073 0074 // For this test, we rely on the fact that: 0075 // 1. there is at least one app associated with application/zip, 0076 // 2. there is at least one app associated with text/plain, and 0077 // 3. there are no apps associated with application/x-zerosize. 0078 0079 if (KApplicationTrader::queryByMimeType(QStringLiteral("application/zip")).count() > 0) { 0080 QTest::newRow("(zip) cancel") << "application/zip" << Cancel << (int)BrowserOpenOrSaveQuestion::Cancel << false; 0081 QTest::newRow("(zip) open default app") << "application/zip" << OpenDefault << (int)BrowserOpenOrSaveQuestion::Open << true; 0082 QTest::newRow("(zip) open with...") << "application/zip" << OpenWith << (int)BrowserOpenOrSaveQuestion::Open << false; 0083 QTest::newRow("(zip) save") << "application/zip" << Save << (int)BrowserOpenOrSaveQuestion::Save << false; 0084 } else { 0085 qWarning() << "This test relies on the fact that there is at least one app associated with appliation/zip."; 0086 } 0087 0088 if (KApplicationTrader::queryByMimeType(QStringLiteral("text/plain")).count() > 0) { 0089 QTest::newRow("(text) cancel") << "text/plain" << Cancel << (int)BrowserOpenOrSaveQuestion::Cancel << false; 0090 QTest::newRow("(text) open default app") << "text/plain" << OpenDefault << (int)BrowserOpenOrSaveQuestion::Open << true; 0091 QTest::newRow("(text) open with...") << "text/plain" << OpenWith << (int)BrowserOpenOrSaveQuestion::Open << false; 0092 QTest::newRow("(text) save") << "text/plain" << Save << (int)BrowserOpenOrSaveQuestion::Save << false; 0093 } else { 0094 qWarning() << "This test relies on the fact that there is at least one app associated with text/plain."; 0095 } 0096 0097 if (KApplicationTrader::queryByMimeType(QStringLiteral("application/x-zerosize")).count() == 0) { 0098 QTest::newRow("(zero) cancel") << "application/x-zerosize" << Cancel << (int)BrowserOpenOrSaveQuestion::Cancel << false; 0099 QTest::newRow("(zero) open with...") << "application/x-zerosize" << OpenDefault /*Yes, not OpenWith*/ << (int)BrowserOpenOrSaveQuestion::Open 0100 << false; 0101 QTest::newRow("(zero) save") << "application/x-zerosize" << Save << (int)BrowserOpenOrSaveQuestion::Save << false; 0102 } else { 0103 qWarning() << "This test relies on the fact that there are no apps associated with application/x-zerosize."; 0104 } 0105 } 0106 0107 void testAllChoices() 0108 { 0109 QFETCH(QString, mimetype); 0110 QFETCH(QString, buttonName); 0111 QFETCH(int, expectedResult); 0112 QFETCH(bool, expectedService); 0113 0114 QWidget parent; 0115 BrowserOpenOrSaveQuestion questionEmbedZip(&parent, QUrl(QStringLiteral("http://www.example.com/")), mimetype); 0116 questionEmbedZip.setFeatures(BrowserOpenOrSaveQuestion::ServiceSelection); 0117 QDialog *theDialog = parent.findChild<QDialog *>(); 0118 QVERIFY(theDialog); 0119 // QMetaObject::invokeMethod(theDialog, "slotButtonClicked", Qt::QueuedConnection, Q_ARG(int, button)); 0120 QMetaObject::invokeMethod(this, "clickButton", Qt::QueuedConnection, Q_ARG(QDialog *, theDialog), Q_ARG(QString, buttonName)); 0121 QCOMPARE((int)questionEmbedZip.askOpenOrSave(), expectedResult); 0122 QCOMPARE((bool)questionEmbedZip.selectedService(), expectedService); 0123 } 0124 0125 protected Q_SLOTS: // our own slots, not tests 0126 void clickButton(QDialog *dialog, const QString &buttonName) 0127 { 0128 QPushButton *button = dialog->findChild<QPushButton *>(buttonName); 0129 Q_ASSERT(button); 0130 Q_ASSERT(!button->isHidden()); 0131 if (button->menu()) { 0132 Q_ASSERT(buttonName == OpenWith); // only this one has a menu 0133 button->menu()->actions().last()->trigger(); 0134 } else { 0135 button->click(); 0136 } 0137 } 0138 }; 0139 0140 QTEST_MAIN(OpenOrSaveTest) 0141 0142 #include "openorsavequestion_unittest.moc"