File indexing completed on 2024-05-19 03:57:29

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 testShowsAllFiles();
0021     void testShowsAllFiles_data();
0022     void testCurrentFilter();
0023 };
0024 
0025 void KFileFilterComboTest::initTestCase()
0026 {
0027     QLocale::setDefault(QLocale::c());
0028     qputenv("LC_ALL", "en_US.UTF-8");
0029     qputenv("LANG", "en_US.UTF-8");
0030     qputenv("LANGUAGE", "en");
0031 }
0032 
0033 void KFileFilterComboTest::testSetFilter()
0034 {
0035     QFETCH(QString, filterString);
0036     QFETCH(QStringList, expectedComboboxText);
0037 
0038     KFileFilterCombo combo;
0039     combo.setFilters(KFileFilter::fromFilterString(filterString));
0040 
0041     for (int i = 0; i < combo.count(); ++i) {
0042         const QString text = combo.itemData(i, Qt::DisplayRole).toString();
0043         QCOMPARE(text, expectedComboboxText[i]);
0044     }
0045 }
0046 
0047 void KFileFilterComboTest::testSetFilter_data()
0048 {
0049     QTest::addColumn<QString>("filterString");
0050     QTest::addColumn<QStringList>("expectedComboboxText");
0051 
0052     QTest::addRow("extension_name") << "*.cpp|Sources (*.cpp)" << QStringList{"Sources (*.cpp)"};
0053     QTest::addRow("multiple_filter") << "*.cpp|Sources (*.cpp)\n*.h|Header files" << QStringList{"Sources (*.cpp)", "Header files"};
0054     QTest::addRow("mutiple_extension_multiple_filter")
0055         << "*.cpp *.cc *.C|C++ Source Files\n*.h *.H|Header files" << QStringList{"C++ Source Files", "Header files"};
0056     QTest::addRow("pattern_only") << "*.cpp" << QStringList{"*.cpp"};
0057 }
0058 
0059 void KFileFilterComboTest::testDefaultFilter()
0060 {
0061     KFileFilterCombo combo;
0062     combo.setDefaultFilter(KFileFilter::fromFilterString("*.cpp|Sources (*.cpp)").first());
0063 
0064     combo.setFilters({});
0065     QCOMPARE(combo.itemData(0, Qt::DisplayRole).toString(), "Sources (*.cpp)");
0066 
0067     combo.setFilters(KFileFilter::fromFilterString("*.png|PNG Image (*.png)"));
0068     QCOMPARE(combo.itemData(0, Qt::DisplayRole).toString(), "PNG Image (*.png)");
0069 
0070     combo.setFilters({});
0071     QCOMPARE(combo.itemData(0, Qt::DisplayRole).toString(), "Sources (*.cpp)");
0072 }
0073 
0074 void KFileFilterComboTest::testShowsAllFiles_data()
0075 {
0076     QTest::addColumn<QStringList>("mimeTypes");
0077     QTest::addColumn<QString>("defaultType");
0078     QTest::addColumn<bool>("expectedShowsAllFiles");
0079 
0080     QTest::addRow("one") << QStringList{"image/png"} << "" << false;
0081     QTest::addRow("two") << QStringList{"image/png", "text/plain"} << "" << true;
0082     QTest::addRow("two_with_default") << QStringList{"image/png", "text/plain"} << "text/plain" << false;
0083     QTest::addRow("three") << QStringList{"image/png", "text/plain", "text/calendar"} << "" << true;
0084     QTest::addRow("four") << QStringList{"image/png", "text/plain", "text/calendar", "image/jpeg"} << "" << true;
0085     QTest::addRow("four") << QStringList{"image/png", "text/plain", "text/calendar", "image/jpeg"} << "text/calendar" << false;
0086 }
0087 
0088 void KFileFilterComboTest::testShowsAllFiles()
0089 {
0090     QFETCH(QStringList, mimeTypes);
0091     QFETCH(QString, defaultType);
0092     QFETCH(bool, expectedShowsAllFiles);
0093 
0094     KFileFilterCombo combo;
0095 
0096     QList<KFileFilter> filters;
0097 
0098     for (const QString &mimeType : mimeTypes) {
0099         filters << KFileFilter::fromMimeType(mimeType);
0100     }
0101 
0102     combo.setFilters(filters, KFileFilter::fromMimeType(defaultType));
0103 
0104     QCOMPARE(combo.showsAllTypes(), expectedShowsAllFiles);
0105 }
0106 
0107 void KFileFilterComboTest::testCurrentFilter()
0108 {
0109     KFileFilterCombo combo;
0110 
0111     const KFileFilter cppFilter("C++ Sources", {"*.cpp"}, {});
0112     const KFileFilter pngFilter("PNG Images", {"*.png"}, {});
0113     const KFileFilter pdfFilter("PDF Documents", {"*.pdf"}, {});
0114 
0115     combo.setFilters({cppFilter, pngFilter, pdfFilter});
0116 
0117     QCOMPARE(combo.currentFilter(), cppFilter);
0118     QCOMPARE(combo.currentIndex(), 0);
0119 
0120     combo.setCurrentFilter(pngFilter);
0121     QCOMPARE(combo.currentFilter(), pngFilter);
0122     QCOMPARE(combo.currentIndex(), 1);
0123 
0124     // User enters something custom
0125     combo.setCurrentText("*.md");
0126     QCOMPARE(combo.currentFilter(), KFileFilter("*.md", {"*.md"}, {}));
0127 
0128     combo.setCurrentText("*.c|C Sources");
0129     QCOMPARE(combo.currentFilter(), KFileFilter("C Sources", {"*.c"}, {}));
0130 
0131     // User enters something custom
0132     combo.setCurrentText("text/plain");
0133     QCOMPARE(combo.currentFilter(), KFileFilter::fromMimeType("text/plain"));
0134 }
0135 
0136 QTEST_MAIN(KFileFilterComboTest)
0137 #include "kfilefiltercombotest.moc"