1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2009-06-11
7  * Description : An unit-test to print metadata tags from file using DMetadata.
8  *
9  * Copyright (C) 2009-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "printmetadata_utest.h"
25 
26 // Qt includes
27 
28 #include <QTextStream>
29 
QTEST_MAIN(PrintMetadataTest)30 QTEST_MAIN(PrintMetadataTest)
31 
32 PrintMetadataTest::PrintMetadataTest(QObject* const parent)
33     : AbstractUnitTest(parent)
34 {
35 }
36 
printMetadataMap(const DMetadata::MetaDataMap & map)37 void PrintMetadataTest::printMetadataMap(const DMetadata::MetaDataMap& map)
38 {
39     QString output;
40     QTextStream stream(&output);
41     stream << endl;
42 
43     qCDebug(DIGIKAM_TESTS_LOG) << "Found" << map.size() << "tags:" << endl;
44 
45     for (DMetadata::MetaDataMap::const_iterator it = map.constBegin() ;
46          it != map.constEnd() ; ++it)
47     {
48         QString key   = it.key();
49         QString value = it.value();
50 
51         // None of these strings can be null, event if strings are translated.
52         QVERIFY(!key.isNull());
53         QVERIFY(!value.isNull());
54 
55         QString tagName = key.simplified();
56         tagName.append(QString().fill(QLatin1Char(' '), 48 - tagName.length()));
57 
58         QString tagVal  = value.simplified();
59 
60         if (tagVal.length() > 48)
61         {
62             tagVal.truncate(48);
63             tagVal.append(QString::fromLatin1("... (%1 bytes)").arg(value.length()));
64         }
65 
66         stream << tagName << " : " << tagVal << endl;
67     }
68 
69     qCDebug(DIGIKAM_TESTS_LOG).noquote() << output;
70 }
71 
testPrintMetadata()72 void PrintMetadataTest::testPrintMetadata()
73 {
74     //                                                   Expected tags to found in Exif,  Iptc,  Xmp,   expectedRead
75     printMetadata(m_originalImageFolder + QLatin1String("nikon-e2100.jpg"),        true,  true,  true,  true);
76     printMetadata(m_originalImageFolder + QLatin1String("_27A1417.CR2"),           true,  false, true,  true);
77     printMetadata(m_originalImageFolder + QLatin1String("2008-05_DSC_0294.JPG"),   true,  true,  true,  true);
78 
79 #ifdef HAVE_IMAGE_MAGICK
80 
81     printMetadata(m_originalImageFolder + QLatin1String("kepler_xray_he.fits"),    false, false, true,  true);
82 
83 #endif
84 
85     // The file cannot be loaded with Exiv2-0.26, only test the newer versions
86 
87     bool ok = true;
88 
89     if ((MetaEngine::Exiv2Version().section(QLatin1Char('.'), 0, 1).toDouble(&ok) > 0.26) && ok)
90     {
91 //        printMetadata(m_originalImageFolder + QLatin1String("20160821035715.jpg"), false, false, false, false);
92     }
93 }
94 
printMetadata(const QString & filePath,bool exif,bool iptc,bool xmp,bool expectedRead)95 void PrintMetadataTest::printMetadata(const QString& filePath, bool exif, bool iptc, bool xmp, bool expectedRead)
96 {
97     QScopedPointer<DMetadata> meta(new DMetadata);
98 
99     bool ret = meta->load(filePath);
100     QCOMPARE(ret, expectedRead);
101 
102     loadExif(*meta, exif);
103     loadIptc(*meta, iptc);
104     loadXmp(*meta,  xmp);
105 }
106 
loadExif(const DMetadata & meta,bool expected)107 void PrintMetadataTest::loadExif(const DMetadata& meta, bool expected)
108 {
109     qCDebug(DIGIKAM_TESTS_LOG) << QString::fromUtf8("-- Exif metadata from %1 --").arg(meta.getFilePath());
110 
111     DMetadata::MetaDataMap map = meta.getExifTagsDataList();
112     QCOMPARE(!map.isEmpty(), expected);
113 
114     printMetadataMap(map);
115 }
116 
loadIptc(const DMetadata & meta,bool expected)117 void PrintMetadataTest::loadIptc(const DMetadata& meta, bool expected)
118 {
119     qCDebug(DIGIKAM_TESTS_LOG) << QString::fromUtf8("-- Iptc metadata from %1 --").arg(meta.getFilePath());
120 
121     DMetadata::MetaDataMap map = meta.getIptcTagsDataList();
122     QCOMPARE(!map.isEmpty(), expected);
123 
124     printMetadataMap(map);
125 }
126 
loadXmp(const DMetadata & meta,bool expected)127 void PrintMetadataTest::loadXmp(const DMetadata& meta, bool expected)
128 {
129     qCDebug(DIGIKAM_TESTS_LOG) << QString::fromUtf8("-- Xmp metadata from %1 --").arg(meta.getFilePath());
130 
131     if (meta.supportXmp())
132     {
133         DMetadata::MetaDataMap map = meta.getXmpTagsDataList();
134         QCOMPARE(!map.isEmpty(), expected);
135 
136         printMetadataMap(map);
137     }
138     else
139     {
140         QWARN("Exiv2 has no XMP support...");
141     }
142 }
143