File indexing completed on 2024-04-14 14:24:00

0001 /*
0002     This file is part of the KDE Frameworks
0003     SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include <QTest>
0009 
0010 #include "kfilefiltercombo.h"
0011 
0012 class KFileFilterComboTest : public QObject
0013 {
0014     Q_OBJECT
0015 private Q_SLOTS:
0016     void initTestCase();
0017     void testSetFilter();
0018     void testSetFilter_data();
0019     void testDefaultFilter();
0020     void testSetMimeFilter();
0021     void testSetMimeFilter_data();
0022     void testIsMimeFilter();
0023     void testSetMimeFilterDefault();
0024     void testSetMimeFilterDefault_data();
0025     void testFilters();
0026     void testFilters_data();
0027     void testFiltersMime();
0028     void testFiltersMime_data();
0029     void testShowsAllFiles();
0030     void testShowsAllFiles_data();
0031 };
0032 
0033 void KFileFilterComboTest::initTestCase()
0034 {
0035     QLocale::setDefault(QLocale::c());
0036     qputenv("LC_ALL", "en_US.UTF-8");
0037     qputenv("LANG", "en_US.UTF-8");
0038     qputenv("LANGUAGE", "en");
0039 }
0040 
0041 void KFileFilterComboTest::testSetFilter()
0042 {
0043     QFETCH(QString, filterString);
0044     QFETCH(QStringList, expectedComboboxText);
0045 
0046     KFileFilterCombo combo;
0047     combo.setFilter(filterString);
0048 
0049     QCOMPARE(combo.count(), expectedComboboxText.count());
0050 
0051     for (int i = 0; i < combo.count(); ++i) {
0052         const QString text = combo.itemData(i, Qt::DisplayRole).toString();
0053         QCOMPARE(text, expectedComboboxText[i]);
0054     }
0055 }
0056 
0057 void KFileFilterComboTest::testSetFilter_data()
0058 {
0059     QTest::addColumn<QString>("filterString");
0060     QTest::addColumn<QStringList>("expectedComboboxText");
0061 
0062     QTest::addRow("extension_name") << "*.cpp|Sources (*.cpp)" << QStringList{"Sources (*.cpp)"};
0063     QTest::addRow("multiple_filter") << "*.cpp|Sources (*.cpp)\n*.h|Header files" << QStringList{"Sources (*.cpp)", "Header files"};
0064     QTest::addRow("mutiple_extension_multiple_filter")
0065         << "*.cpp *.cc *.C|C++ Source Files\n*.h *.H|Header files" << QStringList{"C++ Source Files", "Header files"};
0066     QTest::addRow("pattern_only") << "*.cpp" << QStringList{"*.cpp"};
0067 
0068     // must handle an unescaped slash https://bugs.kde.org/show_bug.cgi?id=463309
0069     QTest::addRow("slash") << "*.c *.cpp|C/C++ Files" << QStringList{"C/C++ Files"};
0070 
0071     QString k3bFilter =
0072         "*|All Files\naudio/x-mp3 audio/x-wav application/x-ogg |Sound Files\naudio/x-wav |Wave Sound Files\naudio/x-mp3 |MP3 Sound Files\napplication/x-ogg "
0073         "|Ogg Vorbis Sound Files\nvideo/mpeg |MPEG Video Files";
0074     QTest::addRow("k3b") << k3bFilter
0075                          << QStringList{"All Files", "Sound Files", "Wave Sound Files", "MP3 Sound Files", "Ogg Vorbis Sound Files", "MPEG Video Files"};
0076 }
0077 
0078 void KFileFilterComboTest::testDefaultFilter()
0079 {
0080     KFileFilterCombo combo;
0081     combo.setDefaultFilter("*.cpp|Sources (*.cpp)");
0082 
0083     combo.setFilter(QString());
0084     QCOMPARE(combo.itemData(0, Qt::DisplayRole).toString(), "Sources (*.cpp)");
0085 
0086     combo.setFilter("*.png|PNG Image (*.png)");
0087     QCOMPARE(combo.itemData(0, Qt::DisplayRole).toString(), "PNG Image (*.png)");
0088 
0089     combo.setFilter(QString());
0090     QCOMPARE(combo.itemData(0, Qt::DisplayRole).toString(), "Sources (*.cpp)");
0091 }
0092 
0093 void KFileFilterComboTest::testSetMimeFilter_data()
0094 {
0095     QTest::addColumn<QStringList>("mimeTypes");
0096     QTest::addColumn<QString>("defaultType");
0097     QTest::addColumn<QStringList>("expectedComboboxText");
0098 
0099     QTest::addRow("one_mime") << QStringList{"image/png"} << "" << QStringList{"PNG image"};
0100     QTest::addRow("two_mime") << QStringList{"image/png", "image/jpeg"} << "" << QStringList{"PNG image, JPEG image", "PNG image", "JPEG image"};
0101     QTest::addRow("two_mime_with_default") << QStringList{"image/png", "image/jpeg"} << "image/jpeg" << QStringList{"PNG image", "JPEG image"};
0102     QTest::addRow("three_mime") << QStringList{"image/png", "image/jpeg", "text/plain"} << ""
0103                                 << QStringList{"PNG image, JPEG image, plain text document", "PNG image", "JPEG image", "plain text document"};
0104     QTest::addRow("four_mime") << QStringList{"image/png", "image/jpeg", "text/plain", "application/mbox"} << ""
0105                                << QStringList{"All Supported Files", "PNG image", "JPEG image", "plain text document", "mailbox file"};
0106     QTest::addRow("duplicate_mime_comment") << QStringList{"application/metalink+xml", "application/metalink4+xml"} << ""
0107                                             << QStringList{"Metalink file (metalink), Metalink file (meta4)",
0108                                                            "Metalink file (metalink)",
0109                                                            "Metalink file (meta4)"};
0110     QTest::addRow("all") << QStringList{"application/octet-stream"} << "" << QStringList{"All Files"};
0111     QTest::addRow("all2") << QStringList{"application/octet-stream", "image/png"} << "" << QStringList{"PNG image", "All Files"};
0112     QTest::addRow("all_with_all_supported")
0113         << QStringList{"application/octet-stream", "image/png", "image/jpeg", "text/plain", "application/mbox"} << ""
0114         << QStringList{"All Supported Files", "PNG image", "JPEG image", "plain text document", "mailbox file", "All Files"};
0115     QTest::addRow("four_mime_with_default") << QStringList{"image/png", "image/jpeg", "text/plain", "application/mbox"} << "text/plain"
0116                                             << QStringList{"PNG image", "JPEG image", "plain text document", "mailbox file"};
0117 }
0118 
0119 void KFileFilterComboTest::testSetMimeFilter()
0120 {
0121     QFETCH(QStringList, mimeTypes);
0122     QFETCH(QString, defaultType);
0123     QFETCH(QStringList, expectedComboboxText);
0124 
0125     KFileFilterCombo combo;
0126 
0127     combo.setMimeFilter(mimeTypes, defaultType);
0128 
0129     QStringList actual;
0130 
0131     for (int i = 0; i < combo.count(); ++i) {
0132         QString text = combo.itemData(i, Qt::DisplayRole).toString();
0133         text.replace("Plain text document", "plain text document"); // shared-mime-info >= 2.3 uses title case
0134         text.replace("Mailbox file", "mailbox file");
0135         actual << text;
0136     }
0137     QCOMPARE(actual, expectedComboboxText);
0138 }
0139 
0140 void KFileFilterComboTest::testSetMimeFilterDefault_data()
0141 {
0142     QTest::addColumn<QStringList>("mimeTypes");
0143     QTest::addColumn<QString>("defaultType");
0144     QTest::addColumn<QString>("expectedComboboxCurrentText");
0145 
0146     QTest::addRow("one") << QStringList{"image/png"} << ""
0147                          << "PNG image";
0148     QTest::addRow("two") << QStringList{"image/png", "image/jpeg"} << ""
0149                          << "PNG image, JPEG image";
0150     QTest::addRow("two_with_default") << QStringList{"image/png", "image/jpeg"} << "image/png"
0151                                       << "PNG image";
0152     QTest::addRow("four_with_default") << QStringList{"image/png", "image/jpeg", "text/plain", "application/mbox"} << "text/plain"
0153                                        << "plain text document";
0154 }
0155 
0156 void KFileFilterComboTest::testSetMimeFilterDefault()
0157 {
0158     QFETCH(QStringList, mimeTypes);
0159     QFETCH(QString, defaultType);
0160     QFETCH(QString, expectedComboboxCurrentText);
0161 
0162     KFileFilterCombo combo;
0163 
0164     combo.setMimeFilter(mimeTypes, defaultType);
0165 
0166     QString currentText = combo.currentText();
0167     currentText.replace("Plain text document", "plain text document"); // shared-mime-info >= 2.3 uses title case
0168     QCOMPARE(currentText, expectedComboboxCurrentText);
0169 }
0170 
0171 void KFileFilterComboTest::testIsMimeFilter()
0172 {
0173     KFileFilterCombo combo;
0174 
0175     combo.setFilter("*.png");
0176     QVERIFY(!combo.isMimeFilter());
0177 
0178     combo.setMimeFilter(QStringList{"image/jpeg"}, QString());
0179     QVERIFY(combo.isMimeFilter());
0180 }
0181 
0182 void KFileFilterComboTest::testFilters_data()
0183 {
0184     QTest::addColumn<QString>("filterString");
0185     QTest::addColumn<QStringList>("expectedFilters");
0186 
0187     QTest::addRow("extension_name") << "*.cpp|Sources (*.cpp)" << QStringList{"*.cpp|Sources (*.cpp)"};
0188     QTest::addRow("multiple_filter") << "*.cpp|Sources (*.cpp)\n*.h|Header files" << QStringList{"*.cpp|Sources (*.cpp)", "*.h|Header files"};
0189     QTest::addRow("mutiple_extension_multiple_filter")
0190         << "*.cpp *.cc *.C|C++ Source Files\n*.h *.H|Header files" << QStringList{"*.cpp *.cc *.C|C++ Source Files", "*.h *.H|Header files"};
0191     QTest::addRow("pattern_only") << "*.cpp" << QStringList{"*.cpp"};
0192 
0193     QString k3bFilter =
0194         "*|All Files\naudio/x-mp3 audio/x-wav application/x-ogg |Sound Files\naudio/x-wav |Wave Sound Files\naudio/x-mp3 |MP3 Sound Files\napplication/x-ogg "
0195         "|Ogg Vorbis Sound Files\nvideo/mpeg |MPEG Video Files";
0196     QTest::addRow("k3b") << k3bFilter
0197                          << QStringList{"*|All Files",
0198                                         "audio/x-mp3 audio/x-wav application/x-ogg |Sound Files",
0199                                         "audio/x-wav |Wave Sound Files",
0200                                         "audio/x-mp3 |MP3 Sound Files",
0201                                         "application/x-ogg |Ogg Vorbis Sound Files",
0202                                         "video/mpeg |MPEG Video Files"};
0203 }
0204 
0205 void KFileFilterComboTest::testFilters()
0206 {
0207     QFETCH(QString, filterString);
0208     QFETCH(QStringList, expectedFilters);
0209 
0210     KFileFilterCombo combo;
0211 
0212     combo.setFilter(filterString);
0213 
0214     QCOMPARE(combo.filters(), expectedFilters);
0215 }
0216 
0217 void KFileFilterComboTest::testFiltersMime_data()
0218 {
0219     QTest::addColumn<QStringList>("mimeTypes");
0220     QTest::addColumn<QStringList>("expectedFilters");
0221 
0222     QTest::addRow("one") << QStringList{"image/png"} << QStringList{"image/png"};
0223     QTest::addRow("two") << QStringList{"image/png", "image/jpeg"} << QStringList{"image/png image/jpeg", "image/png", "image/jpeg"};
0224     QTest::addRow("four") << QStringList{"image/png", "image/jpeg", "text/calendar", "application/gzip"}
0225                           << QStringList{"image/png image/jpeg text/calendar application/gzip", "image/png", "image/jpeg", "text/calendar", "application/gzip"};
0226 }
0227 
0228 void KFileFilterComboTest::testFiltersMime()
0229 {
0230     QFETCH(QStringList, mimeTypes);
0231     QFETCH(QStringList, expectedFilters);
0232 
0233     KFileFilterCombo combo;
0234 
0235     combo.setMimeFilter(mimeTypes, QString());
0236 
0237     QCOMPARE(combo.filters(), expectedFilters);
0238 }
0239 
0240 void KFileFilterComboTest::testShowsAllFiles_data()
0241 {
0242     QTest::addColumn<QStringList>("mimeTypes");
0243     QTest::addColumn<QString>("defaultType");
0244     QTest::addColumn<bool>("expectedShowsAllFiles");
0245 
0246     QTest::addRow("one") << QStringList{"image/png"} << "" << false;
0247     QTest::addRow("two") << QStringList{"image/png", "text/plain"} << "" << true;
0248     QTest::addRow("two_with_default") << QStringList{"image/png", "text/plain"} << "text/plain" << false;
0249     QTest::addRow("three") << QStringList{"image/png", "text/plain", "text/calendar"} << "" << true;
0250     QTest::addRow("four") << QStringList{"image/png", "text/plain", "text/calendar", "image/jpeg"} << "" << true;
0251     QTest::addRow("four") << QStringList{"image/png", "text/plain", "text/calendar", "image/jpeg"} << "text/calendar" << false;
0252 }
0253 
0254 void KFileFilterComboTest::testShowsAllFiles()
0255 {
0256     QFETCH(QStringList, mimeTypes);
0257     QFETCH(QString, defaultType);
0258     QFETCH(bool, expectedShowsAllFiles);
0259 
0260     KFileFilterCombo combo;
0261 
0262     combo.setMimeFilter(mimeTypes, defaultType);
0263 
0264     QCOMPARE(combo.showsAllTypes(), expectedShowsAllFiles);
0265 }
0266 
0267 QTEST_MAIN(KFileFilterComboTest)
0268 #include "kfilefiltercombotest.moc"