File indexing completed on 2025-01-19 03:57:50

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2015
0007  * Description : a test for timestamp trigger for re-reading metadata from image
0008  *
0009  * SPDX-FileCopyrightText: 2015 by Kristian Karl <kristian dot hermann dot karl at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "timestampupdate_utest.h"
0016 
0017 // Qt includes
0018 
0019 #include <QFileInfo>
0020 #include <QScopedPointer>
0021 
0022 // Local includes
0023 
0024 #include "dmetadata.h"
0025 #include "coredb.h"
0026 #include "collectionlocation.h"
0027 #include "collectionmanager.h"
0028 #include "collectionscanner.h"
0029 #include "iteminfo.h"
0030 #include "metaenginesettings.h"
0031 #include "dtestdatadir.h"
0032 #include "digikam_debug.h"
0033 
0034 const QString originalImageFolder(DTestDataDir::TestData(QString::fromUtf8("core/tests/timestampupdate"))
0035                                   .root().path() + QLatin1Char('/'));
0036 const QString originalImageFile(DTestDataDir::TestData(QString::fromUtf8("core/tests/timestampupdate"))
0037                                 .root().path() + QLatin1String("/1.jpg"));
0038 
0039 QTEST_GUILESS_MAIN(TimeStampUpdateTest)
0040 
0041 using namespace Digikam;
0042 
0043 QString TimeStampUpdateTest::tempFileName(const QString& purpose) const
0044 {
0045     return QLatin1String("digikamtests-") + QLatin1String(metaObject()->className()) + QLatin1Char('-') + purpose + QLatin1Char('-') + QTime::currentTime().toString();
0046 }
0047 
0048 QString TimeStampUpdateTest::tempFilePath(const QString& purpose) const
0049 {
0050     return QDir::tempPath() + QLatin1Char('/') + tempFileName(purpose);
0051 }
0052 
0053 /*
0054  * Create a nd simulate a new startup of Digikam.
0055  * A new temporary database is created.
0056  * A collection is added, and scanned.
0057  */
0058 void TimeStampUpdateTest::initTestCase()
0059 {
0060     // Setup the collection folder
0061 
0062     QDir collectionDir = QDir(originalImageFolder);
0063     QVERIFY(collectionDir.exists());
0064 
0065     // Create new temporary database
0066 
0067     dbFile = tempFilePath(QLatin1String("database"));
0068     DbEngineParameters params(QLatin1String("QSQLITE"), dbFile, QLatin1String("QSQLITE"), dbFile);
0069     CoreDbAccess::setParameters(params, CoreDbAccess::MainApplication);
0070     QVERIFY(CoreDbAccess::checkReadyForUse(nullptr));
0071     QVERIFY(QFile(dbFile).exists());
0072 
0073     // Add collection and scan
0074 
0075     CollectionManager::instance()->addLocation(QUrl::fromLocalFile(collectionDir.path()));
0076     CollectionScanner().completeScan();
0077 
0078     // Verify that the scanned collection is correct
0079 
0080     QList<AlbumShortInfo> albums = CoreDbAccess().db()->getAlbumShortInfos();
0081     QVERIFY(albums.size() == 1);
0082     QStringList readOnlyImages;
0083 
0084     Q_FOREACH (const AlbumShortInfo& album, albums)
0085     {
0086         readOnlyImages << CoreDbAccess().db()->getItemURLsInAlbum(album.id);
0087     }
0088 
0089     Q_FOREACH (const QString& file, readOnlyImages)
0090     {
0091         ids << ItemInfo::fromLocalFile(file).id();
0092     }
0093 
0094     QVERIFY(!ids.contains(-1));
0095     QVERIFY(ids.size() == 1);
0096 }
0097 
0098 /*
0099  * Remove the database file
0100  */
0101 void TimeStampUpdateTest::cleanupTestCase()
0102 {
0103     QFile(dbFile).remove();
0104 }
0105 
0106 /*
0107  * Re-set the database and image file to it's original metadata state
0108  */
0109 void TimeStampUpdateTest::cleanup()
0110 {
0111     QScopedPointer<DMetadata> meta(new DMetadata);
0112     meta->setMetadataWritingMode(MetaEngine::WRITE_TO_FILE_ONLY);
0113     meta->setUpdateFileTimeStamp(true);
0114     meta->load(originalImageFile);
0115     meta->removeExifTag("Exif.Image.Model");
0116     QVERIFY2(meta->applyChanges(), "Exif.Image.Model is removed");
0117     QVERIFY(meta->getExifTagString("Exif.Image.Model").isEmpty());
0118 
0119     CollectionScanner().scanFile(originalImageFile, CollectionScanner::Rescan);
0120 
0121     // Check that Exif.Image.Model in database is empty
0122 
0123     QVariantList dbModel = CoreDbAccess().db()->getImageMetadata(ids[0], DatabaseFields::Model);
0124     QVERIFY2(dbModel.at(0).toString().isEmpty(), "Exif.Image.Model should be empty");
0125 }
0126 
0127 /*
0128  * This test manipulates the Exif.Image.Model without updating
0129  * the database.
0130  * A CollectionScanner().completeScan() is then launched, simulating a
0131  * startup of Digikam.
0132  * The test verifies that the change in the file is detected and
0133  * that new value of Exif.Image.Model is read into the database.
0134  */
0135 void TimeStampUpdateTest::testRescanImageIfModifiedSet2True()
0136 {
0137     // Setup metadata settings
0138 
0139     MetaEngineSettingsContainer set;
0140     set.updateFileTimeStamp   = true; // Default value
0141     set.rescanImageIfModified = true;
0142     MetaEngineSettings::instance()->setSettings(set);
0143 
0144     // Load the test image and verify that it's there
0145 
0146     QFileInfo originalFileInfo(originalImageFile);
0147     QVERIFY(originalFileInfo.isReadable());
0148 
0149     // Check that Exif.Image.Model in database is empty
0150 
0151     QVariantList dbModel = CoreDbAccess().db()->getImageMetadata(ids[0], DatabaseFields::Model);
0152     QVERIFY2(dbModel.at(0).toString().isEmpty(), "Exif.Image.Model should be empty");
0153 
0154     // Verify that Exif.Image.Model in image file is empty
0155 
0156     QScopedPointer<DMetadata> meta(new DMetadata);
0157     meta->setMetadataWritingMode(MetaEngine::WRITE_TO_FILE_ONLY);
0158     meta->setUpdateFileTimeStamp(true);
0159     meta->load(originalImageFile);
0160     QString model = meta->getExifTagString("Exif.Image.Model");
0161     QVERIFY(model.isEmpty());
0162 
0163     // Change the metadata in image file
0164 
0165     meta->setExifTagString("Exif.Image.Model", QLatin1String("TimeStampUpdateTestCamera"));
0166     QVERIFY2(meta->applyChanges(), "Exif.Image.Model is added");
0167     QVERIFY(meta->getExifTagString("Exif.Image.Model") == QLatin1String("TimeStampUpdateTestCamera"));
0168 
0169     // Simulate restart of Digikam
0170     // The scan should detect that image file has changed
0171 
0172     CollectionScanner().completeScan();
0173 
0174     // Verify that the change is detected, and no exists in the database
0175 
0176     dbModel = CoreDbAccess().db()->getImageMetadata(ids[0], DatabaseFields::Model);
0177     QVERIFY(dbModel.at(0).toString() == QLatin1String("TimeStampUpdateTestCamera"));
0178 }
0179 
0180 /*
0181  * This test manipulates the Exif.Image.Model without updating
0182  * the database.
0183  * A CollectionScanner().completeScan() is then launched, simulating a
0184  * startup of Digikam.
0185  * The test verifies that the change in the file is disregarded and
0186  * that the value of Exif.Image.Model is unchanged the database.
0187  */
0188 void TimeStampUpdateTest::testRescanImageIfModifiedSet2False()
0189 {
0190     // Setup metadata settings
0191 
0192     MetaEngineSettingsContainer set;
0193     set.updateFileTimeStamp   = true;  // Default value
0194     set.rescanImageIfModified = false; // Default value
0195     MetaEngineSettings::instance()->setSettings(set);
0196 
0197     // Load the test image and verify that it's there
0198 
0199     QFileInfo originalFileInfo(originalImageFile);
0200     QVERIFY(originalFileInfo.isReadable());
0201 
0202     // Check that Exif.Image.Model in database is empty
0203 
0204     QVariantList dbModel = CoreDbAccess().db()->getImageMetadata(ids[0], DatabaseFields::Model);
0205     QVERIFY2(dbModel.at(0).toString().isEmpty(), "Exif.Image.Model should be empty");
0206 
0207     // Verify that Exif.Image.Model in image file is empty
0208 
0209     QScopedPointer<DMetadata> meta(new DMetadata);
0210     meta->setMetadataWritingMode(MetaEngine::WRITE_TO_FILE_ONLY);
0211     meta->setUpdateFileTimeStamp(true);
0212     meta->load(originalImageFile);
0213     QString model = meta->getExifTagString("Exif.Image.Model");
0214     QVERIFY(model.isEmpty());
0215 
0216     // Change the metadata in image file
0217 
0218     meta->setExifTagString("Exif.Image.Model", QLatin1String("TimeStampUpdateTestCamera"));
0219     QVERIFY2(meta->applyChanges(), "Exif.Image.Model is added");
0220     QVERIFY(meta->getExifTagString("Exif.Image.Model") == QLatin1String("TimeStampUpdateTestCamera"));
0221 
0222     // Simulate restart of Digikam
0223     // The scan should detect that image file has changed
0224 
0225     CollectionScanner().completeScan();
0226 
0227     // Verify that the changed image did not change the database
0228 
0229     dbModel = CoreDbAccess().db()->getImageMetadata(ids[0], DatabaseFields::Model);
0230     QVERIFY(dbModel.at(0).toString().isEmpty());
0231 }
0232 
0233 #include "moc_timestampupdate_utest.cpp"