File indexing completed on 2024-05-12 05:50:09

0001 /*
0002     SPDX-FileCopyrightText: 2016 Ragnar Thomsen <rthomsen6@gmail.com>
0003 
0004     SPDX-License-Identifier: BSD-2-Clause
0005 */
0006 
0007 #include "adddialog.h"
0008 #include "archiveformat.h"
0009 #include "pluginmanager.h"
0010 
0011 #include <KCollapsibleGroupBox>
0012 
0013 #include <QCheckBox>
0014 #include <QMimeDatabase>
0015 #include <QTest>
0016 
0017 using namespace Kerfuffle;
0018 
0019 class AddDialogTest : public QObject
0020 {
0021     Q_OBJECT
0022 
0023 private Q_SLOTS:
0024     void testBasicWidgets_data();
0025     void testBasicWidgets();
0026 
0027 private:
0028     PluginManager m_pluginManager;
0029 };
0030 
0031 void AddDialogTest::testBasicWidgets_data()
0032 {
0033     QTest::addColumn<QString>("mimeType");
0034     QTest::addColumn<bool>("supportsCompLevel");
0035     QTest::addColumn<int>("initialCompLevel");
0036     QTest::addColumn<int>("changeToCompLevel");
0037 
0038     QTest::newRow("tar") << QStringLiteral("application/x-tar") << false << -1 << -1;
0039     QTest::newRow("targzip") << QStringLiteral("application/x-compressed-tar") << true << 3 << 7;
0040     QTest::newRow("tarbzip") << QMimeDatabase().mimeTypeForFile(QStringLiteral("dummy.tar.bz2"), QMimeDatabase::MatchExtension).name() << true << 3 << 7;
0041     QTest::newRow("tarZ") << QStringLiteral("application/x-tarz") << false << -1 << -1;
0042     QTest::newRow("tarxz") << QStringLiteral("application/x-xz-compressed-tar") << true << 3 << 7;
0043     QTest::newRow("tarlzma") << QStringLiteral("application/x-lzma-compressed-tar") << true << 3 << 7;
0044     QTest::newRow("tarlzip") << QStringLiteral("application/x-lzip-compressed-tar") << true << 3 << 7;
0045 
0046     const auto writeMimeTypes = m_pluginManager.supportedWriteMimeTypes();
0047 
0048     if (writeMimeTypes.contains(QLatin1String("application/zip"))) {
0049         QTest::newRow("zip") << QStringLiteral("application/zip") << true << 3 << 7;
0050     } else {
0051         qDebug() << "zip format not available, skipping test.";
0052     }
0053 
0054     if (writeMimeTypes.contains(QLatin1String("application/x-7z-compressed"))) {
0055         QTest::newRow("7z") << QStringLiteral("application/x-7z-compressed") << true << 3 << 7;
0056     } else {
0057         qDebug() << "7z format not available, skipping test.";
0058     }
0059 
0060     if (writeMimeTypes.contains(QLatin1String("application/vnd.rar"))) {
0061         QTest::newRow("rar") << QStringLiteral("application/vnd.rar") << true << 2 << 5;
0062     } else {
0063         qDebug() << "rar format not available, skipping test.";
0064     }
0065 
0066     if (writeMimeTypes.contains(QLatin1String("application/x-lrzip-compressed-tar"))) {
0067         QTest::newRow("tarlrzip") << QStringLiteral("application/x-lrzip-compressed-tar") << true << 3 << 7;
0068     } else {
0069         qDebug() << "tar.lrzip format not available, skipping test.";
0070     }
0071 
0072     if (writeMimeTypes.contains(QLatin1String("application/x-tzo"))) {
0073         QTest::newRow("tarlzop") << QStringLiteral("application/x-tzo") << true << 3 << 7;
0074     } else {
0075         qDebug() << "tar.lzo format not available, skipping test.";
0076     }
0077 }
0078 
0079 void AddDialogTest::testBasicWidgets()
0080 {
0081     QFETCH(QString, mimeType);
0082     const QMimeType mime = QMimeDatabase().mimeTypeForName(mimeType);
0083     QFETCH(bool, supportsCompLevel);
0084     QFETCH(int, initialCompLevel);
0085     QFETCH(int, changeToCompLevel);
0086 
0087     AddDialog *dialog = new AddDialog(nullptr, QString(), QUrl(), mime);
0088 
0089     dialog->slotOpenOptions();
0090 
0091     auto collapsibleCompression = dialog->optionsDialog->findChild<KCollapsibleGroupBox *>(QStringLiteral("collapsibleCompression"));
0092     QVERIFY(collapsibleCompression);
0093 
0094     const KPluginMetaData metadata = PluginManager().preferredPluginFor(mime)->metaData();
0095     const ArchiveFormat archiveFormat = ArchiveFormat::fromMetadata(mime, metadata);
0096     QVERIFY(archiveFormat.isValid());
0097 
0098     if (archiveFormat.defaultCompressionLevel() > 0 && supportsCompLevel) {
0099         // Test that collapsiblegroupbox is enabled for mimetypes that support compression levels.
0100         QVERIFY(collapsibleCompression->isEnabled());
0101 
0102         auto compLevelSlider = dialog->optionsDialog->findChild<QSlider *>(QStringLiteral("compLevelSlider"));
0103         QVERIFY(compLevelSlider);
0104 
0105         // Test that min/max of slider are correct.
0106         QCOMPARE(compLevelSlider->minimum(), archiveFormat.minCompressionLevel());
0107         QCOMPARE(compLevelSlider->maximum(), archiveFormat.maxCompressionLevel());
0108 
0109         // Test that the slider is set to default compression level.
0110         QCOMPARE(compLevelSlider->value(), archiveFormat.defaultCompressionLevel());
0111 
0112         // Set the compression level slider.
0113         compLevelSlider->setValue(changeToCompLevel);
0114     } else {
0115         // Test that collapsiblegroupbox is disabled for mimetypes that don't support compression levels.
0116         QVERIFY(!collapsibleCompression->isEnabled());
0117     }
0118 
0119     dialog->optionsDialog->accept();
0120     dialog->accept();
0121 
0122     if (archiveFormat.defaultCompressionLevel() > 0 && supportsCompLevel) {
0123         // Test that the value set by slider is exported from AddDialog.
0124         QCOMPARE(dialog->compressionOptions().compressionLevel(), changeToCompLevel);
0125     }
0126 
0127     // Test that passing a compression level in ctor works.
0128     CompressionOptions opts;
0129     opts.setCompressionLevel(initialCompLevel);
0130 
0131     dialog = new AddDialog(nullptr, QString(), QUrl(), mime, opts);
0132     dialog->slotOpenOptions();
0133 
0134     if (archiveFormat.defaultCompressionLevel() > 0 && supportsCompLevel) {
0135         auto compLevelSlider = dialog->optionsDialog->findChild<QSlider *>(QStringLiteral("compLevelSlider"));
0136         QVERIFY(compLevelSlider);
0137 
0138         // Test that slider is set to the compression level supplied in ctor.
0139         QCOMPARE(compLevelSlider->value(), initialCompLevel);
0140     }
0141     dialog->optionsDialog->accept();
0142     dialog->accept();
0143 }
0144 
0145 QTEST_MAIN(AddDialogTest)
0146 
0147 #include "adddialogtest.moc"