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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2012-10-23
0007  * Description : unit test for DImg image loader with multithreading
0008  *
0009  * SPDX-FileCopyrightText: 2012-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 "loadsavethread_utest.h"
0016 
0017 // Qt includes
0018 
0019 #include <QTest>
0020 #include <QFileInfo>
0021 #include <QList>
0022 #include <QDir>
0023 
0024 // Local includes
0025 
0026 #include "digikam_debug.h"
0027 #include "metaengine.h"
0028 #include "dpluginloader.h"
0029 #include "dtestdatadir.h"
0030 #include "drawdecoding.h"
0031 
0032 QTEST_GUILESS_MAIN(LoadSaveThreadTest)
0033 
0034 LoadSaveThreadTest::LoadSaveThreadTest(QObject* const parent)
0035     : QObject(parent)
0036 {
0037 }
0038 
0039 void LoadSaveThreadTest::testLoadSaveThread()
0040 {
0041     MetaEngine::initializeExiv2();
0042     QDir dir(qApp->applicationDirPath());
0043     qputenv("DK_PLUGIN_PATH", dir.canonicalPath().toUtf8());
0044     DPluginLoader::instance()->init();
0045 
0046     if (DPluginLoader::instance()->allPlugins().isEmpty())
0047     {
0048         QWARN("Not able to found digiKam plugin in standard paths. Test is aborted...");
0049         return;
0050     }
0051 
0052     qRegisterMetaType<LoadingDescription>("LoadingDescription");
0053     qRegisterMetaType<DImg>("DImg");
0054 
0055     m_thread = new LoadSaveThread;
0056 
0057     connect(m_thread, SIGNAL(signalImageLoaded(LoadingDescription,DImg)),
0058             this, SLOT(slotImageLoaded(LoadingDescription,DImg)));
0059 
0060     connect(m_thread, SIGNAL(signalImageSaved(QString,bool)),
0061             this, SLOT(slotImageSaved(QString,bool)));
0062 
0063     connect(m_thread, SIGNAL(signalLoadingProgress(LoadingDescription,float)),
0064             this, SLOT(slotLoadingProgress(LoadingDescription,float)));
0065 
0066     connect(m_thread, SIGNAL(signalSavingProgress(QString,float)),
0067             this, SLOT(slotSavingProgress(QString,float)));
0068 
0069     DRawDecoderSettings settings;
0070     settings.halfSizeColorImage    = false;
0071     settings.sixteenBitsImage      = false;
0072     settings.RGBInterpolate4Colors = false;
0073     settings.RAWQuality            = DRawDecoderSettings::BILINEAR;
0074 
0075     m_fname = DTestDataDir::TestData(QString::fromUtf8("core/tests/fileio"))
0076                   .root().path() + QLatin1String("/4324477432.pgf");
0077     qCDebug(DIGIKAM_TESTS_LOG) << "Test Data File:" << m_fname;
0078 
0079     LoadingDescription desc(m_fname, DRawDecoding(settings));
0080 
0081     m_thread->load(desc);
0082 
0083     int i = 0;
0084 
0085     do
0086     {
0087         QTest::qWait(1000);
0088         i++;
0089     }
0090     while ((i < 10) && !(m_loaded && m_saved));
0091 
0092     QVERIFY2(m_loadedProgress, "Failed to progress image loading");
0093     QVERIFY2(m_savedProgress,  "Failed to progress image saving");
0094     QVERIFY2(m_loaded,         "Failed to load image");
0095     QVERIFY2(m_saved,          "Failed to save image");
0096 
0097     DPluginLoader::instance()->cleanUp();
0098 }
0099 
0100 void LoadSaveThreadTest::slotLoadingProgress(const LoadingDescription& desc, float p)
0101 {
0102     QFileInfo fi(desc.filePath);
0103     qCDebug(DIGIKAM_TESTS_LOG) << "Loading " << fi.baseName() << " : " << p << " %";
0104     QVERIFY2(desc.filePath == m_fname, "File to load do not match");
0105     m_loadedProgress = true;
0106 }
0107 
0108 void LoadSaveThreadTest::slotImageLoaded(const LoadingDescription& desc, const DImg& img)
0109 {
0110     QFileInfo fi(desc.filePath);
0111     qCDebug(DIGIKAM_TESTS_LOG) << "Image " << fi.baseName() << " loaded";
0112 
0113     QVERIFY2(desc.filePath == m_fname, "File loaded do not match");
0114 
0115     qCDebug(DIGIKAM_TESTS_LOG) << "DImg image size:"         << img.size();
0116 
0117     QVERIFY2(!img.isNull(), "DImg image is null...");
0118     QVERIFY2(img.size()  == QSize(4032, 3024), "Incorrect DImg image size...");
0119 
0120     m_loaded      = true;
0121     m_outFilePath = fi.baseName() + QString::fromUtf8(".out.png");
0122     DImg image    = img;
0123     m_thread->save(image, m_outFilePath, QLatin1String("PNG"));
0124 }
0125 
0126 void LoadSaveThreadTest::slotSavingProgress(const QString& filePath, float p)
0127 {
0128     QFileInfo fi(filePath);
0129     qCDebug(DIGIKAM_TESTS_LOG) << "Saving " << fi.baseName() << " : " << p << " %";
0130     QVERIFY2(filePath == m_outFilePath, "File to save do not match");
0131     m_savedProgress = true;
0132 }
0133 
0134 void LoadSaveThreadTest::slotImageSaved(const QString& filePath, bool b)
0135 {
0136     QFileInfo fi(filePath);
0137     qCDebug(DIGIKAM_TESTS_LOG) << fi.baseName() << " saved : " << (b ? "ok" : "pb");
0138 
0139     QVERIFY2(filePath == m_outFilePath, "File saved do not match");
0140     QVERIFY2(b, "Failed to save PNG file");
0141 
0142     m_saved = true;
0143 }
0144 
0145 #include "moc_loadsavethread_utest.cpp"