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        : 2015-08-12
0007  * Description : metadata settings tests for getImageComment and setImageComment.
0008  *
0009  * SPDX-FileCopyrightText: 2015 by Veaceslav Munteanu <veaceslav dot munteanu90 at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "commentreadwrite_utest.h"
0016 
0017 // Qt includes
0018 
0019 #include <QTest>
0020 #include <QString>
0021 
0022 // Local includes
0023 
0024 #include "dmetadata.h"
0025 #include "digikam_debug.h"
0026 
0027 QTEST_GUILESS_MAIN(CommentReadWriteTest)
0028 
0029 using namespace Digikam;
0030 
0031 CommentReadWriteTest::CommentReadWriteTest(QObject* const parent)
0032     : QObject(parent)
0033 {
0034 }
0035 
0036 void CommentReadWriteTest::initTestCase()
0037 {
0038     MetaEngine::initializeExiv2();
0039     qCDebug(DIGIKAM_TESTS_LOG) << "Using Exiv2 Version:" << MetaEngine::Exiv2Version();
0040 
0041     MetaEngine::AltLangMap authorsMap,  authorsMap2;
0042     MetaEngine::AltLangMap datesMap,    datesMap2;
0043     MetaEngine::AltLangMap commentsMap, commentsMap2;
0044     QString commonAuthor, commonAuthor2;
0045 
0046     authorsMap.insert(QLatin1String("x-default"), QLatin1String("Veaceslav"));
0047     commentsMap.insert(QLatin1String("x-default"), QLatin1String("Veaceslav's comment"));
0048     commonAuthor  = QLatin1String("Veaceslav");
0049 
0050     commentSet1.setData(commentsMap, authorsMap, commonAuthor, datesMap);
0051 
0052     authorsMap2.insert(QLatin1String("x-default"), QLatin1String("Munteanu"));
0053     commentsMap2.insert(QLatin1String("x-default"), QLatin1String("Munteanu's comment"));
0054     commonAuthor2 = QLatin1String("Munteanu");
0055 
0056     commentSet2.setData(commentsMap2, authorsMap2, commonAuthor2, datesMap2);
0057 }
0058 
0059 void CommentReadWriteTest::cleanupTestCase()
0060 {
0061 }
0062 
0063 void CommentReadWriteTest::testSimpleReadAfterWrite()
0064 {
0065     QScopedPointer<DMetadata> dmeta(new DMetadata);
0066     CaptionsMap result;
0067 
0068     // Trick dmetadata, so it will think that we have a file path
0069     dmeta->setFilePath(QLatin1String("random.org"));
0070 
0071     dmeta->setItemComments(commentSet1);
0072 
0073     result = dmeta->getItemComments();
0074 
0075     QString rezAuthor  = result.value(QLatin1String("x-default")).author;
0076     QString rezComment = result.value(QLatin1String("x-default")).caption;
0077 
0078     QCOMPARE(rezAuthor,  commentSet1.value(QLatin1String("x-default")).author);
0079     QCOMPARE(rezComment, commentSet1.value(QLatin1String("x-default")).caption);
0080 }
0081 
0082 void CommentReadWriteTest::testWriteToDisabledNamespaces()
0083 {
0084     QScopedPointer<DMetadata> dmeta(new DMetadata);
0085     dmeta->setFilePath(QLatin1String("random.org"));
0086 
0087     MetaEngine::AltLangMap commentsMap;
0088     QString commentString;
0089 
0090     DMetadataSettingsContainer dmsettings;
0091 
0092     NamespaceEntry commNs1;
0093     commNs1.namespaceName   = QLatin1String("Xmp.dc.description");
0094     commNs1.nsType          = NamespaceEntry::COMMENT;
0095     commNs1.specialOpts     = NamespaceEntry::COMMENT_ATLLANGLIST;
0096     commNs1.index           = 0;
0097     commNs1.subspace        = NamespaceEntry::XMP;
0098     commNs1.isDisabled      = true;
0099 
0100     NamespaceEntry commNs2;
0101     commNs2.namespaceName   = QLatin1String("Xmp.exif.UserComment");
0102     commNs2.nsType          = NamespaceEntry::COMMENT;
0103     commNs2.specialOpts     = NamespaceEntry::COMMENT_ALTLANG;
0104     commNs2.index           = 1;
0105     commNs2.subspace        = NamespaceEntry::XMP;
0106 
0107     dmsettings.setUnifyReadWrite(false);
0108 
0109     dmsettings.getWriteMapping(NamespaceEntry::DM_COMMENT_CONTAINER()).clear();
0110     dmsettings.getWriteMapping(NamespaceEntry::DM_COMMENT_CONTAINER())
0111              << commNs1
0112              << commNs2;
0113 
0114     bool rez = dmeta->setItemComments(commentSet1, dmsettings);
0115 
0116     QVERIFY(rez);
0117 
0118     commentsMap   = dmeta->getXmpTagStringListLangAlt("Xmp.dc.description", false);
0119 
0120     QCOMPARE(commentsMap.value(QLatin1String("x-default")), QString());
0121 
0122     commentString = dmeta->getXmpTagStringLangAlt("Xmp.exif.UserComment", QString(), false);
0123 
0124     QCOMPARE(commentString, commentSet1.value(QLatin1String("x-default")).caption);
0125 }
0126 
0127 void CommentReadWriteTest::testReadFromDisabledNamespaces()
0128 {
0129     QScopedPointer<DMetadata> dmeta(new DMetadata);
0130     dmeta->setFilePath(QLatin1String("random.org"));
0131 
0132     CaptionsMap rez;
0133 
0134     DMetadataSettingsContainer dmsettings;
0135 
0136     NamespaceEntry commNs1;
0137     commNs1.namespaceName   = QLatin1String("Xmp.dc.description");
0138     commNs1.nsType          = NamespaceEntry::COMMENT;
0139     commNs1.specialOpts     = NamespaceEntry::COMMENT_ATLLANGLIST;
0140     commNs1.index           = 0;
0141     commNs1.subspace        = NamespaceEntry::XMP;
0142     commNs1.isDisabled      = true;
0143 
0144     NamespaceEntry commNs2;
0145     commNs2.namespaceName   = QLatin1String("Xmp.exif.UserComment");
0146     commNs2.nsType          = NamespaceEntry::COMMENT;
0147     commNs2.specialOpts     = NamespaceEntry::COMMENT_ALTLANG;
0148     commNs2.index           = 1;
0149     commNs2.subspace        = NamespaceEntry::XMP;
0150 
0151     dmsettings.setUnifyReadWrite(false);
0152     dmsettings.getReadMapping(NamespaceEntry::DM_COMMENT_CONTAINER()).clear();
0153     dmsettings.getReadMapping(NamespaceEntry::DM_COMMENT_CONTAINER())
0154              << commNs1
0155              << commNs2;
0156 
0157     dmeta->setXmpTagStringListLangAlt("Xmp.dc.description", commentSet1.toAltLangMap());
0158     dmeta->setXmpTagStringLangAlt("Xmp.exif.UserComment", commentSet2.value(QLatin1String("x-default")).caption, QString());
0159 
0160     rez = dmeta->getItemComments(dmsettings);
0161     QCOMPARE(rez.value(QLatin1String("x-default")).caption, commentSet2.value(QLatin1String("x-default")).caption);
0162 }
0163 
0164 #include "moc_commentreadwrite_utest.cpp"