File indexing completed on 2025-01-05 03:58:14
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2009-06-11 0007 * Description : An unit-test to print metadata tags from file using DMetadata. 0008 * 0009 * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #include "printmetadata_utest.h" 0016 0017 // Qt includes 0018 0019 #include <QTextStream> 0020 0021 // Local includes 0022 0023 #include "digikam_globals.h" 0024 0025 QTEST_MAIN(PrintMetadataTest) 0026 0027 PrintMetadataTest::PrintMetadataTest(QObject* const parent) 0028 : AbstractUnitTest(parent) 0029 { 0030 } 0031 0032 void PrintMetadataTest::printMetadataMap(const DMetadata::MetaDataMap& map) 0033 { 0034 QString output; 0035 QTextStream stream(&output); 0036 stream << QT_ENDL; 0037 0038 qCDebug(DIGIKAM_TESTS_LOG) << "Found" << map.size() << "tags:" << QT_ENDL; 0039 0040 for (DMetadata::MetaDataMap::const_iterator it = map.constBegin() ; 0041 it != map.constEnd() ; ++it) 0042 { 0043 QString key = it.key(); 0044 QString value = it.value(); 0045 0046 // None of these strings can be null, event if strings are translated. 0047 0048 QVERIFY(!key.isNull()); 0049 0050 if (key != QLatin1String("Exif.Photo.UserComment")) 0051 { 0052 QVERIFY(!value.isNull()); 0053 } 0054 0055 QString tagName = key.simplified(); 0056 tagName.append(QString().fill(QLatin1Char(' '), 48 - tagName.length())); 0057 0058 QString tagVal = value.simplified(); 0059 0060 if (tagVal.length() > 48) 0061 { 0062 tagVal.truncate(48); 0063 tagVal.append(QString::fromLatin1("... (%1 bytes)").arg(value.length())); 0064 } 0065 0066 stream << tagName << " : " << tagVal << QT_ENDL; 0067 } 0068 0069 qCDebug(DIGIKAM_TESTS_LOG).noquote() << output; 0070 } 0071 0072 void PrintMetadataTest::testPrintMetadata() 0073 { 0074 // Expected tags to found in Exif, Iptc, Xmp, expectedRead 0075 printMetadata(m_originalImageFolder + QLatin1String("nikon-e2100.jpg"), true, true, true, true); 0076 printMetadata(m_originalImageFolder + QLatin1String("_27A1417.CR2"), true, false, true, true); 0077 printMetadata(m_originalImageFolder + QLatin1String("2008-05_DSC_0294.JPG"), true, true, true, true); 0078 0079 if (m_hasExifTool) 0080 { 0081 printMetadata(m_originalImageFolder + QLatin1String("kepler_xray_he.fits"), false, false, false, true); 0082 } 0083 0084 // The file cannot be loaded with Exiv2-0.26, only test the newer versions 0085 0086 bool ok = true; 0087 0088 if ((MetaEngine::Exiv2Version().section(QLatin1Char('.'), 0, 1).toDouble(&ok) > 0.26) && ok) 0089 { 0090 // printMetadata(m_originalImageFolder + QLatin1String("20160821035715.jpg"), false, false, false, false); 0091 } 0092 } 0093 0094 void PrintMetadataTest::printMetadata(const QString& filePath, bool exif, bool iptc, bool xmp, bool expectedRead) 0095 { 0096 QScopedPointer<DMetadata> meta(new DMetadata); 0097 0098 bool ret = meta->load(filePath); 0099 QCOMPARE(ret, expectedRead); 0100 0101 loadExif(*meta, exif); 0102 loadIptc(*meta, iptc); 0103 loadXmp(*meta, xmp); 0104 } 0105 0106 void PrintMetadataTest::loadExif(const DMetadata& meta, bool expected) 0107 { 0108 qCDebug(DIGIKAM_TESTS_LOG) << QString::fromUtf8("-- Exif metadata from %1 --").arg(meta.getFilePath()); 0109 0110 DMetadata::MetaDataMap map = meta.getExifTagsDataList(); 0111 QCOMPARE(!map.isEmpty(), expected); 0112 0113 printMetadataMap(map); 0114 } 0115 0116 void PrintMetadataTest::loadIptc(const DMetadata& meta, bool expected) 0117 { 0118 qCDebug(DIGIKAM_TESTS_LOG) << QString::fromUtf8("-- Iptc metadata from %1 --").arg(meta.getFilePath()); 0119 0120 DMetadata::MetaDataMap map = meta.getIptcTagsDataList(); 0121 QCOMPARE(!map.isEmpty(), expected); 0122 0123 printMetadataMap(map); 0124 } 0125 0126 void PrintMetadataTest::loadXmp(const DMetadata& meta, bool expected) 0127 { 0128 qCDebug(DIGIKAM_TESTS_LOG) << QString::fromUtf8("-- Xmp metadata from %1 --").arg(meta.getFilePath()); 0129 0130 if (meta.supportXmp()) 0131 { 0132 DMetadata::MetaDataMap map = meta.getXmpTagsDataList(); 0133 QCOMPARE(!map.isEmpty(), expected); 0134 0135 printMetadataMap(map); 0136 } 0137 else 0138 { 0139 QWARN("Exiv2 has no XMP support..."); 0140 } 0141 } 0142 0143 #include "moc_printmetadata_utest.cpp"