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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "exiv2extractortest.h"
0008 #include "simpleextractionresult.h"
0009 #include "indexerextractortestsconfig.h"
0010 #include "extractors/exiv2extractor.h"
0011 #include "mimeutils.h"
0012 
0013 #include <QTest>
0014 #include <QMimeDatabase>
0015 
0016 using namespace KFileMetaData;
0017 
0018 namespace {
0019 QString testFilePath(const QString& fileName)
0020 {
0021     return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH) + QLatin1Char('/') + fileName;
0022 }
0023 
0024 }
0025 
0026 void Exiv2ExtractorTest::testNoExtraction()
0027 {
0028     Exiv2Extractor plugin{this};
0029 
0030     QString fileName = testFilePath(QStringLiteral("test.jpg"));
0031     QMimeDatabase mimeDb;
0032     QString mimeType = MimeUtils::strictMimeType(fileName, mimeDb).name();
0033     QVERIFY(plugin.mimetypes().contains(mimeType));
0034 
0035     SimpleExtractionResult result(fileName, mimeType, ExtractionResult::ExtractNothing);
0036     plugin.extract(&result);
0037 
0038     QCOMPARE(result.types().size(), 1);
0039     QCOMPARE(result.types().constFirst(), Type::Image);
0040     QCOMPARE(result.properties().size(), 0);
0041 }
0042 
0043 void Exiv2ExtractorTest::test()
0044 {
0045     using namespace KFileMetaData::Property;
0046 
0047     Exiv2Extractor plugin{this};
0048 
0049     QString fileName = testFilePath(QStringLiteral("test.jpg"));
0050     QMimeDatabase mimeDb;
0051     QString mimeType = MimeUtils::strictMimeType(fileName, mimeDb).name();
0052     QVERIFY(plugin.mimetypes().contains(mimeType));
0053 
0054     SimpleExtractionResult result(fileName, mimeType);
0055     plugin.extract(&result);
0056 
0057     QCOMPARE(result.types().size(), 1);
0058     QCOMPARE(result.types().constFirst(), Type::Image);
0059 
0060     QCOMPARE(result.properties().value(Artist).toString(), QStringLiteral("Artist"));
0061     QCOMPARE(result.properties().value(Description).toString(), QStringLiteral("Description"));
0062     QCOMPARE(result.properties().value(Copyright).toString(), QStringLiteral("Copyright"));
0063     QCOMPARE(result.properties().value(Generator).toString(), QStringLiteral("digiKam-5.9.0"));
0064 }
0065 
0066 void Exiv2ExtractorTest::testGPS()
0067 {
0068     using namespace KFileMetaData::Property;
0069 
0070     Exiv2Extractor plugin{this};
0071 
0072     SimpleExtractionResult result(testFilePath("test.jpg"), "image/jpeg");
0073     plugin.extract(&result);
0074 
0075     QCOMPARE(result.properties().value(PhotoGpsLatitude).toDouble(), 41.411);
0076     QCOMPARE(result.properties().value(PhotoGpsLongitude).toDouble(), 2.173);
0077     QCOMPARE(result.properties().value(PhotoGpsAltitude).toDouble(), 12.2);
0078 
0079     SimpleExtractionResult resultEmpty(testFilePath("test_no_gps.jpg"), "image/jpeg");
0080     plugin.extract(&resultEmpty);
0081     QVERIFY(!resultEmpty.properties().contains(PhotoGpsLatitude));
0082     QVERIFY(!resultEmpty.properties().contains(PhotoGpsLongitude));
0083     QVERIFY(!resultEmpty.properties().contains(PhotoGpsAltitude));
0084 
0085     SimpleExtractionResult resultZero(testFilePath("test_zero_gps.jpg"), "image/jpeg");
0086     plugin.extract(&resultZero);
0087     QVERIFY(resultZero.properties().contains(PhotoGpsLatitude));
0088     QVERIFY(resultZero.properties().contains(PhotoGpsLongitude));
0089     QVERIFY(resultZero.properties().contains(PhotoGpsAltitude));
0090     QCOMPARE(resultZero.properties().value(PhotoGpsLatitude).toDouble(), 0.0);
0091     QCOMPARE(resultZero.properties().value(PhotoGpsLongitude).toDouble(), 0.0);
0092     QCOMPARE(resultZero.properties().value(PhotoGpsAltitude).toDouble(), 0.0);
0093 }
0094 
0095 void Exiv2ExtractorTest::testJpegJxlProperties()
0096 {
0097     QFETCH(QString, fileName);
0098     QFETCH(QString, mimeType);
0099 
0100 #ifndef EXV_ENABLE_BMFF
0101     if (mimeType == QStringLiteral("image/jxl"))
0102         QSKIP("BMFF support required");
0103 #endif
0104 
0105     Exiv2Extractor plugin{this};
0106 
0107     SimpleExtractionResult result(testFilePath(fileName), mimeType);
0108     plugin.extract(&result);
0109 
0110     QCOMPARE(result.types().size(), 1);
0111     QCOMPARE(result.types().constFirst(), Type::Image);
0112 
0113     const auto properties = result.properties();
0114     QCOMPARE(properties.size(), 27);
0115 
0116     auto verifyProperty = [&properties](KFileMetaData::Property::Property prop, const QVariant &value)
0117     {
0118     if (value.canConvert<float>()) {
0119         QCOMPARE(properties.value(prop).toFloat(), value.toFloat());
0120     } else {
0121         QCOMPARE(properties.value(prop), value);
0122     }
0123     };
0124 
0125     verifyProperty(Property::Artist, QStringLiteral("Artist"));
0126     verifyProperty(Property::Description, QStringLiteral("Description"));
0127     verifyProperty(Property::Copyright, QStringLiteral("Copyright"));
0128     verifyProperty(Property::Generator, QStringLiteral("digikam-5.9.0"));
0129     verifyProperty(Property::PhotoGpsLatitude, 41.411f);
0130     verifyProperty(Property::PhotoGpsLongitude, 2.173f);
0131     verifyProperty(Property::PhotoGpsAltitude, 12.2f);
0132     verifyProperty(Property::Width, 8);
0133     verifyProperty(Property::Height, 14);
0134     verifyProperty(Property::Manufacturer, QStringLiteral("LGE"));
0135     verifyProperty(Property::Model, QStringLiteral("Nexus 5"));
0136     verifyProperty(Property::ImageDateTime, QDateTime(QDate(2014, 10, 04), QTime(13, 47, 43.000), Qt::UTC));
0137     verifyProperty(Property::PhotoFlash, 0);
0138     verifyProperty(Property::PhotoPixelXDimension, 8);
0139     verifyProperty(Property::PhotoPixelYDimension, 14);
0140     verifyProperty(Property::PhotoDateTimeOriginal, QDateTime(QDate(2014, 10, 04), QTime(13, 47, 43.000), Qt::UTC));
0141     verifyProperty(Property::PhotoFocalLength, 4.f);
0142     verifyProperty(Property::PhotoFocalLengthIn35mmFilm, 10.f);
0143     verifyProperty(Property::PhotoExposureTime, 0.0027027f);
0144     verifyProperty(Property::PhotoFNumber, 1.f);
0145     verifyProperty(Property::PhotoApertureValue, 0.f);
0146     verifyProperty(Property::PhotoExposureBiasValue, 0.f);
0147     verifyProperty(Property::PhotoWhiteBalance, 0);
0148     verifyProperty(Property::PhotoMeteringMode, 1);
0149     verifyProperty(Property::PhotoISOSpeedRatings, 100);
0150     verifyProperty(Property::PhotoSaturation, 0);
0151     verifyProperty(Property::PhotoSharpness, 0);
0152 }
0153 
0154 void Exiv2ExtractorTest::testJpegJxlProperties_data()
0155 {
0156     QTest::addColumn<QString>("fileName");
0157     QTest::addColumn<QString>("mimeType");
0158 
0159     QTest::addRow("jpeg") << QStringLiteral("test.jpg") << QStringLiteral("image/jpeg");
0160     QTest::addRow("jxl") << QStringLiteral("test.jxl") << QStringLiteral("image/jxl");
0161 }
0162 
0163 void Exiv2ExtractorTest::testHeifProperties()
0164 {
0165 #ifndef EXV_ENABLE_BMFF
0166     QSKIP("BMFF support required");
0167 #endif
0168 
0169     Exiv2Extractor plugin{this};
0170 
0171     SimpleExtractionResult result(testFilePath("test.heif"), "image/heif");
0172     plugin.extract(&result);
0173 
0174     QCOMPARE(result.types().size(), 1);
0175     QCOMPARE(result.types().constFirst(), Type::Image);
0176 
0177     const auto properties = result.properties();
0178     QCOMPARE(properties.size(), 23);
0179 
0180     auto verifyProperty = [&properties](KFileMetaData::Property::Property prop, const QVariant &value)
0181     {
0182     if (value.canConvert<float>()) {
0183         QCOMPARE(properties.value(prop).toFloat(), value.toFloat());
0184     } else {
0185         QCOMPARE(properties.value(prop), value);
0186     }
0187     };
0188 
0189     verifyProperty(Property::Artist, QStringLiteral("Artist"));
0190     verifyProperty(Property::Description, QStringLiteral("Description"));
0191     verifyProperty(Property::Copyright, QStringLiteral("Copyright"));
0192     verifyProperty(Property::Generator, QStringLiteral("digikam-5.9.0"));
0193     verifyProperty(Property::PhotoGpsLatitude, 51.3331f);
0194     verifyProperty(Property::PhotoGpsLongitude, -0.705575f);
0195     verifyProperty(Property::PhotoGpsAltitude, 0.f);
0196     verifyProperty(Property::Width, 750);
0197     verifyProperty(Property::Height, 1000);
0198     verifyProperty(Property::Manufacturer, QStringLiteral("samsung"));
0199     verifyProperty(Property::Model, QStringLiteral("SM-J610N"));
0200     verifyProperty(Property::ImageDateTime, QDateTime(QDate(2022, 03, 24), QTime(18, 20, 07.000), Qt::UTC));
0201     verifyProperty(Property::PhotoFlash, 0);
0202     verifyProperty(Property::PhotoPixelXDimension, 750);
0203     verifyProperty(Property::PhotoPixelYDimension, 1000);
0204     verifyProperty(Property::PhotoDateTimeOriginal, QDateTime(QDate(2020, 03, 31), QTime(11, 14, 30.000), Qt::UTC));
0205     verifyProperty(Property::PhotoFocalLength, 3.6f);
0206     verifyProperty(Property::PhotoFocalLengthIn35mmFilm, 26.f);
0207     verifyProperty(Property::PhotoExposureTime, 0.00429185f);
0208     verifyProperty(Property::PhotoFNumber, 1.9f);
0209     verifyProperty(Property::PhotoApertureValue, 1.85f);
0210     verifyProperty(Property::PhotoExposureBiasValue, 0.f);
0211     verifyProperty(Property::PhotoWhiteBalance, 0);
0212     verifyProperty(Property::PhotoMeteringMode, 2);
0213     verifyProperty(Property::PhotoISOSpeedRatings, 40);
0214     verifyProperty(Property::PhotoSaturation, 0);
0215     verifyProperty(Property::PhotoSharpness, 0);
0216 }
0217 
0218 QTEST_GUILESS_MAIN(Exiv2ExtractorTest)
0219 
0220 #include "moc_exiv2extractortest.cpp"