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