File indexing completed on 2024-12-08 10:14:50
0001 /* 0002 SPDX-FileCopyrightText: 2018 Michael Heidelbach <ottwolt@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "filemetadatawidgettest.h" 0008 0009 #include <QDebug> 0010 #include <QLabel> 0011 #include <QProcess> 0012 #include <QSignalSpy> 0013 #include <QStandardPaths> 0014 #include <QTest> 0015 0016 #include <KConfig> 0017 #include <KConfigGroup> 0018 #include <KFileItem> 0019 #include <KRatingWidget> 0020 0021 void initLocale() 0022 { 0023 QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates)); 0024 } 0025 Q_CONSTRUCTOR_FUNCTION(initLocale) 0026 0027 QTEST_MAIN(FileMetadataWidgetTest) 0028 0029 void FileMetadataWidgetTest::initTestCase() 0030 { 0031 qRegisterMetaType<KFileItemList>("KFileItemList"); 0032 0033 QStandardPaths::setTestModeEnabled(true); 0034 0035 KConfig balooConfig(QStringLiteral("baloofilerc"), KConfig::NoGlobals); 0036 KConfigGroup balooSettings = balooConfig.group(QStringLiteral("General")); 0037 // If we use .writePathEntry here, the test will fail. 0038 balooSettings.writeEntry(QStringLiteral("folders"), QString()); 0039 0040 // Ensure show configuration 0041 KConfig config(QStringLiteral("baloofileinformationrc"), KConfig::NoGlobals); 0042 KConfigGroup settings = config.group(QStringLiteral("Show")); 0043 const auto keys = settings.keyList(); 0044 for (const auto &key : keys) { 0045 settings.writeEntry(key, true); 0046 } 0047 0048 m_sampleFiles = { 0049 { "untagged", QFINDTESTDATA("samplefiles/test.mp3")}, 0050 { "taggedmp3", QFINDTESTDATA("samplefiles/testtagged.mp3")}, 0051 { "taggedm4a", QFINDTESTDATA("samplefiles/testtagged.m4a")}, 0052 { "multivalue", QFINDTESTDATA("samplefiles/test_multivalue.ogg")}, 0053 }; 0054 for (const auto &file : std::as_const(m_sampleFiles)) { 0055 QVERIFY(!file.isEmpty()); 0056 } 0057 0058 const QString exe = QStandardPaths::findExecutable(QStringLiteral("setfattr")); 0059 if (exe.isEmpty()) { 0060 return; 0061 } 0062 0063 m_mayTestRating = true; 0064 0065 for (const auto &file : { "taggedmp3", "taggedm4a" }) { 0066 const QStringList args = {QStringLiteral("--name=user.baloo.rating"), 0067 QStringLiteral("--value=5"), 0068 m_sampleFiles[file]}; 0069 0070 QProcess process; 0071 process.start(exe, args); 0072 if (!process.waitForFinished(10000)) { 0073 qDebug() << "setfattr timed out"; 0074 m_mayTestRating = false; 0075 return; 0076 } 0077 0078 if ((process.exitStatus() != QProcess::NormalExit) || (process.exitCode() != 0)) { 0079 qDebug() << "setfattr err:" << process.readAllStandardError(); 0080 m_mayTestRating = false; 0081 return; 0082 } 0083 } 0084 } 0085 0086 void FileMetadataWidgetTest::shouldSignalOnceWithoutFile() 0087 { 0088 auto widget = std::make_unique<Baloo::FileMetaDataWidget>(); 0089 0090 QSignalSpy spy(widget.get(), &Baloo::FileMetaDataWidget::metaDataRequestFinished); 0091 widget->setItems(KFileItemList()); 0092 QCOMPARE(spy.count(), 1); 0093 QCOMPARE(widget->items().count(), 0); 0094 } 0095 0096 void FileMetadataWidgetTest::shouldSignalOnceFile() 0097 { 0098 auto widget = std::make_unique<Baloo::FileMetaDataWidget>(); 0099 0100 QSignalSpy spy(widget.get(), &Baloo::FileMetaDataWidget::metaDataRequestFinished); 0101 widget->setItems({KFileItem{QUrl::fromLocalFile(m_sampleFiles["taggedm4a"])}}); 0102 QVERIFY(spy.wait()); 0103 QCOMPARE(spy.count(), 1); 0104 QCOMPARE(widget->items().count(), 1); 0105 } 0106 0107 void FileMetadataWidgetTest::shouldSignalOnceFiles() 0108 { 0109 auto widget = std::make_unique<Baloo::FileMetaDataWidget>(); 0110 0111 QSignalSpy spy(widget.get(), &Baloo::FileMetaDataWidget::metaDataRequestFinished); 0112 widget->setItems({ 0113 KFileItem{QUrl::fromLocalFile(m_sampleFiles["untagged"])}, 0114 KFileItem{QUrl::fromLocalFile(m_sampleFiles["taggedmp3"])}, 0115 KFileItem{QUrl::fromLocalFile(m_sampleFiles["taggedm4a"])}, 0116 }); 0117 QVERIFY(spy.wait()); 0118 QCOMPARE(spy.count(), 1); 0119 QCOMPARE(widget->items().count(), 3); 0120 } 0121 0122 void FileMetadataWidgetTest::shouldShowProperties() 0123 { 0124 auto widget = std::make_unique<Baloo::FileMetaDataWidget>(); 0125 0126 QSignalSpy spy(widget.get(), &Baloo::FileMetaDataWidget::metaDataRequestFinished); 0127 widget->setItems({KFileItem{QUrl::fromLocalFile(m_sampleFiles["taggedmp3"])}}); 0128 0129 QVERIFY(spy.wait()); 0130 QCOMPARE(spy.count(), 1); 0131 0132 // simple property 0133 auto valueWidget = widget->findChild<QLabel *>(QStringLiteral("kfileitem#type")); 0134 QVERIFY2(valueWidget, "Type data missing"); 0135 QCOMPARE(valueWidget->text(), QLatin1String("MP3 audio")); 0136 0137 if (m_mayTestRating) { 0138 // editable property 0139 auto ratingWidget = widget->findChild<KRatingWidget *>(QStringLiteral("rating")); 0140 QVERIFY2(ratingWidget, "Rating data missing"); 0141 QCOMPARE(ratingWidget->rating(), 5u); 0142 } else { 0143 qDebug() << "Skipped 'Rating' test"; 0144 } 0145 // async property 0146 valueWidget = widget->findChild<QLabel *>(QStringLiteral("albumArtist")); 0147 QVERIFY2(valueWidget, "albumArtist data was not found"); 0148 QCOMPARE(valueWidget->text(), QLatin1String("Bill Laswell")); 0149 } 0150 0151 void FileMetadataWidgetTest::shouldShowCommonProperties() 0152 { 0153 auto widget = std::make_unique<Baloo::FileMetaDataWidget>(); 0154 0155 QSignalSpy spy(widget.get(), &Baloo::FileMetaDataWidget::metaDataRequestFinished); 0156 widget->setItems({ 0157 KFileItem{QUrl::fromLocalFile(m_sampleFiles["taggedmp3"])}, 0158 KFileItem{QUrl::fromLocalFile(m_sampleFiles["taggedm4a"])}, 0159 }); 0160 QVERIFY(spy.wait()); 0161 QCOMPARE(spy.count(), 1); 0162 0163 // simple property 0164 auto valueWidget = widget->findChild<QLabel *>(QStringLiteral("kfileitem#type")); 0165 QVERIFY(!valueWidget); 0166 0167 valueWidget = widget->findChild<QLabel *>(QStringLiteral("kfileitem#totalSize")); 0168 // circumvent i18n formatting 0169 QCOMPARE(valueWidget->text().left(3), QLatin1String("153")); 0170 0171 // editable property 0172 if (m_mayTestRating) { 0173 auto ratingWidget = widget->findChild<KRatingWidget *>(QStringLiteral("rating")); 0174 QVERIFY2(ratingWidget, "Rating data missing"); 0175 QCOMPARE(ratingWidget->rating(), 5u); 0176 } else { 0177 qDebug() << "Skipped 'Rating' test"; 0178 } 0179 // async property 0180 // FIXME: Make this pass 0181 // QCOMPARE( map->value("Album Artist:"), QLatin1String("Bill Laswell")); 0182 } 0183 0184 void FileMetadataWidgetTest::shouldShowMultiValueProperties() 0185 { 0186 auto widget = std::make_unique<Baloo::FileMetaDataWidget>(); 0187 0188 QSignalSpy spy(widget.get(), &Baloo::FileMetaDataWidget::metaDataRequestFinished); 0189 widget->setItems({KFileItem{QUrl::fromLocalFile(m_sampleFiles["multivalue"])}}); 0190 QVERIFY(spy.wait()); 0191 QCOMPARE(spy.count(), 1); 0192 auto artistWidget = widget->findChild<QLabel *>(QStringLiteral("artist")); 0193 QVERIFY2(artistWidget, "artist not found"); 0194 QCOMPARE(artistWidget->text(), QStringLiteral("Artist1 and Artist2")); 0195 auto genreWidget = widget->findChild<QLabel *>(QStringLiteral("genre")); 0196 QVERIFY2(genreWidget, "genre not found"); 0197 QCOMPARE(genreWidget->text(), QStringLiteral("Genre1, Genre2, and Genre3")); 0198 } 0199 0200 #include "moc_filemetadatawidgettest.cpp"