File indexing completed on 2024-04-28 15:22:16

0001 /*
0002     This file is part of the KDE KFileMetaData project
0003     SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "propertyinfotest.h"
0009 #include "propertyinfo.h"
0010 
0011 #include <QTest>
0012 
0013 using namespace KFileMetaData;
0014 
0015 //QTEST_GUILESS_MAIN(PropertyInfoTest)
0016 int main(int argc, char *argv[])
0017 {
0018     QCoreApplication app(argc, argv);
0019     PropertyInfoTest tc;
0020 
0021     auto arguments = app.arguments();
0022     if (arguments.contains(QStringLiteral("--localized"))) {
0023         arguments.removeAll(QStringLiteral("--localized"));
0024         tc.setLocalized(true);
0025     }
0026 
0027     return QTest::qExec(&tc, arguments);
0028 }
0029 
0030 void PropertyInfoTest::setLocalized(bool localized)
0031 {
0032     m_useLocalization = localized;
0033 }
0034 
0035 void PropertyInfoTest::init()
0036 {
0037     if (!m_useLocalization) {
0038         QLocale().setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
0039     }
0040 }
0041 
0042 void PropertyInfoTest::testNameIdMapping()
0043 {
0044     const auto names = PropertyInfo::allNames();
0045 
0046     for (const auto& name : names) {
0047         const auto pi = PropertyInfo::fromName(name);
0048 
0049         QVERIFY(!pi.name().isEmpty());
0050         QVERIFY(!pi.displayName().isEmpty());
0051         QCOMPARE(name, pi.name());
0052 
0053         const auto pi2 = PropertyInfo::fromName(pi.name());
0054         QCOMPARE(pi.property(), pi2.property());
0055         QCOMPARE(pi, pi2);
0056         QCOMPARE(pi, PropertyInfo::fromName(pi.name().toLower()));
0057         QCOMPARE(pi, PropertyInfo::fromName(pi.name().toUpper()));
0058     }
0059 }
0060 
0061 void PropertyInfoTest::testFormatAsDisplayString() {
0062     QFETCH(KFileMetaData::PropertyInfo, propertyInfo);
0063     QFETCH(QVariant, value);
0064     QFETCH(QString, expected);
0065     QFETCH(bool, maybeLocalized);
0066 
0067     if (m_useLocalization && maybeLocalized) {
0068         qDebug() << "Expected:" << expected << ", formatted/localized:" << propertyInfo.formatAsDisplayString(value);
0069         if (expected != propertyInfo.formatAsDisplayString(value)) {
0070             QEXPECT_FAIL("", "Expected value not localized", Continue);
0071         }
0072     }
0073     QCOMPARE(propertyInfo.formatAsDisplayString(value), expected);
0074 }
0075 
0076 void PropertyInfoTest::testFormatAsDisplayString_data()
0077 {
0078     QTest::addColumn<KFileMetaData::PropertyInfo>("propertyInfo");
0079     QTest::addColumn<QVariant>("value");
0080     // expected values for an en_US locale
0081     QTest::addColumn<QString>("expected");
0082     QTest::addColumn<bool>("maybeLocalized");
0083 
0084     auto emptyProperty = PropertyInfo::fromName(QStringLiteral("no valid property name"));
0085     QTest::addRow("<invalid>")
0086         << emptyProperty << QVariant(QStringLiteral("empty")) << QStringLiteral("empty") << true;
0087 
0088     QStringList artistList = {QStringLiteral("Artist1"), QStringLiteral("Artist2"), QStringLiteral("Artist3")};
0089     QStringList authorList = {QStringLiteral("Author1")};
0090     QVariantList arrangerList = {QStringLiteral("Arranger1"), QStringLiteral("Arranger2")};
0091     QVariantList bitRateList = {128000, 130000};
0092     QVariantList titleList = {QStringLiteral("Title1"), QStringLiteral("Title2")};
0093 
0094     struct {
0095         KFileMetaData::Property::Property property;
0096         bool maybeLocalized;
0097         QVariant value;
0098         QString expected;
0099     } rows[] = {
0100         { Property::DiscNumber,             true,  2018,                    QStringLiteral("2018")},
0101         { Property::Title,                  false, QStringLiteral("Title"), QStringLiteral("Title")},
0102         { Property::Title,                  true,  titleList,               QStringLiteral("Title1 and Title2")},
0103         { Property::Artist,                 true,  artistList,              QStringLiteral("Artist1, Artist2, and Artist3")},
0104         { Property::Author,                 true,  authorList,              QStringLiteral("Author1")},
0105         { Property::Arranger,               true,  arrangerList,            QStringLiteral("Arranger1 and Arranger2")},
0106         { Property::Duration,               true,  1800,                    QStringLiteral("0:30:00")},
0107         { Property::SampleRate,             true,  44100,                   QStringLiteral("44.1 kHz")},
0108         { Property::BitRate,                true,  128000,                  QStringLiteral("128 kbit/s")},
0109         { Property::BitRate,                true,  1350000,                 QStringLiteral("1.35 Mbit/s")},
0110         { Property::BitRate,                true,  14700000,                QStringLiteral("14.7 Mbit/s")},
0111         { Property::BitRate,                true,  bitRateList,             QStringLiteral("128 kbit/s and 130 kbit/s")},
0112         { Property::ImageOrientation,       true,  5,                       QStringLiteral("Transposed")},
0113         { Property::PhotoFlash,             true,  0x00,                    QStringLiteral("No flash")},
0114         { Property::PhotoFlash,             true,  0x50,                    QStringLiteral("No, red-eye reduction")},
0115         { Property::PhotoGpsAltitude,       true,  1.1,                     QStringLiteral("1.1 m")},
0116         // make VisualStudio compiler happy: QChar(0x00B0) = "°"
0117         { Property::PhotoGpsLatitude,       true,  25,                      QStringLiteral("25") + QChar(0x00B0)},
0118         { Property::PhotoGpsLongitude,      true,  13.5,                    QStringLiteral("13.5") + QChar(0x00B0)},
0119         { Property::PhotoExposureTime,      true,  0.0015625,               QStringLiteral("1/640 s")},
0120         { Property::PhotoExposureTime,      true,  0.5,                     QStringLiteral("0.5 s")},
0121         { Property::PhotoExposureTime,      true,  0.15,                    QStringLiteral("0.15 s")},
0122         { Property::PhotoExposureBiasValue, true,  0.33333,                 QStringLiteral("1/3 EV")},
0123         { Property::PhotoExposureBiasValue, true,  0.66667,                 QStringLiteral("2/3 EV")},
0124         { Property::PhotoExposureBiasValue, true,  1,                       QStringLiteral("1 EV")},
0125         { Property::PhotoExposureBiasValue, true,  1.66667,                 QStringLiteral("1 2/3 EV")},
0126         { Property::PhotoExposureBiasValue, true,  0.1888,                  QStringLiteral("0.189 EV")},
0127         { Property::PhotoExposureBiasValue, true,  -0.33333,                QStringLiteral("-1/3 EV")},
0128         { Property::PhotoExposureBiasValue, true,  0,                       QStringLiteral("0 EV")},
0129         { Property::PhotoExposureBiasValue, true,  -1.5,                    QStringLiteral("-1 1/2 EV")},
0130         { Property::PhotoFNumber,           true,  4.0,                     QStringLiteral("f/4")},
0131         { Property::PhotoFNumber,           true,  2.8,                     QStringLiteral("f/2.8")},
0132         { Property::ReplayGainAlbumGain,    true,  -9.90,                   QStringLiteral("-9.9")},
0133         { Property::ReplayGainAlbumPeak,    true,  1.512,                   QStringLiteral("1.51")},
0134         { Property::ReplayGainAlbumGain,    true,  10.44,                   QStringLiteral("10.4")},
0135         { Property::ReplayGainAlbumPeak,    true,  1.306,                   QStringLiteral("1.31")},
0136         { Property::FrameRate,              true,  23,                      QStringLiteral("23 fps")},
0137         { Property::FrameRate,              true,  23.976,                  QStringLiteral("23.98 fps")},
0138         { Property::AspectRatio,            true,  1.77778,                 QStringLiteral("1.78:1")},
0139         { Property::PhotoFocalLength,       true,  2.0,                     QStringLiteral("2 mm")},
0140         { Property::PhotoFocalLength,       true,  2.4,                     QStringLiteral("2.4 mm")},
0141     };
0142 
0143     for (auto row : rows) {
0144         PropertyInfo info(row.property);
0145         QTest::addRow("%s", info.displayName().toUtf8().constData())
0146             << info << row.value << row.expected << row.maybeLocalized;
0147     }
0148 }
0149 
0150 void PropertyInfoTest::benchmarkPropertyInfo()
0151 {
0152     QFETCH(QString, propertyName);
0153     QFETCH(KFileMetaData::Property::Property, propertyId);
0154 
0155     QVERIFY(PropertyInfo::fromName(propertyName).property() == propertyId);
0156 
0157     auto property = PropertyInfo(propertyId);
0158 
0159     QBENCHMARK {
0160         // Instantiate a PropertyInfo from Id
0161         property = PropertyInfo(propertyId);
0162     }
0163 }
0164 
0165 void PropertyInfoTest::benchmarkPropertyInfoFromName()
0166 {
0167     QFETCH(QString, propertyName);
0168     auto property = PropertyInfo::fromName(propertyName);
0169 
0170     QBENCHMARK {
0171         // Instantiate a PropertyInfo from its name
0172         property = PropertyInfo::fromName(propertyName);
0173     }
0174 }
0175 
0176 void PropertyInfoTest::benchmarkPropertyInfoDisplayName()
0177 {
0178     QFETCH(KFileMetaData::Property::Property, propertyId);
0179 
0180     auto displayName = PropertyInfo(propertyId).displayName();
0181 
0182     QBENCHMARK {
0183         // Instantiate a PropertyInfo and get the displayName;
0184         auto property = PropertyInfo(propertyId);
0185         displayName = property.displayName();
0186     }
0187     QVERIFY(!displayName.isEmpty());
0188 }
0189 
0190 static void benchmarkTestData()
0191 {
0192     QTest::addColumn<QString>("propertyName");
0193     QTest::addColumn<KFileMetaData::Property::Property>("propertyId");
0194 
0195     QTest::addRow("album") << QStringLiteral("album") << KFileMetaData::Property::Album;
0196     QTest::addRow("width") << QStringLiteral("width") << KFileMetaData::Property::Width;
0197     QTest::addRow("originUrl") << QStringLiteral("originUrl") << KFileMetaData::Property::OriginUrl;
0198 }
0199 
0200 void PropertyInfoTest::benchmarkPropertyInfo_data()
0201 {
0202     benchmarkTestData();
0203 }
0204 
0205 void PropertyInfoTest::benchmarkPropertyInfoFromName_data()
0206 {
0207     benchmarkTestData();
0208 }
0209 
0210 void PropertyInfoTest::benchmarkPropertyInfoDisplayName_data()
0211 {
0212     benchmarkTestData();
0213 }
0214 
0215 #include "moc_propertyinfotest.cpp"