File indexing completed on 2024-09-15 03:52:55

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 28/08/2021
0007  * Description : an unit-test to detect image quality level
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2021-2022 by Phuoc Khanh Le <phuockhanhnk94 at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_IMGQSORT_TEST_H
0017 #define DIGIKAM_IMGQSORT_TEST_H
0018 
0019 // Qt includes
0020 
0021 #include <QObject>
0022 #include <QDir>
0023 #include <QMultiMap>
0024 #include <QString>
0025 
0026 // Local includes
0027 
0028 #include "imgqsorttest_shared.h"
0029 #include "digikam_globals.h"
0030 #include "digikam_debug.h"
0031 #include "imagequalitycontainer.h"
0032 #include "exiftoolparser.h"
0033 #include "dpluginloader.h"
0034 #include "dtestdatadir.h"
0035 
0036 // Shared class for utest
0037 
0038 using namespace Digikam;
0039 
0040 using PairImageQuality = QPair<QString, int>;
0041 using DataTestCases    = QMultiMap<QString, PairImageQuality>;
0042 
0043 class ImgQSortTest : public QObject
0044 {
0045     Q_OBJECT
0046 
0047 public:
0048 
0049     explicit ImgQSortTest(QObject* const parent = nullptr)
0050         : QObject      (parent),
0051           m_hasExifTool(false)
0052     {
0053     }
0054 
0055 protected:
0056 
0057     QDir imageDir() const
0058     {
0059         QDir dir = DTestDataDir::TestData(QString::fromUtf8("core/tests/imgqsort")).root();
0060         qCDebug(DIGIKAM_TESTS_LOG) << "Test Data Dir:" << dir;
0061 
0062         return dir;
0063     }
0064 
0065     template <typename Function, typename Parameter>
0066     QHash<QString, bool> testParseTestImages(const QString& testcase_name, Function ParseTestFunc, Parameter parameter)
0067     {
0068         QStringList imageNames;
0069 
0070         QList<PairImageQuality> dataTest = getDataTestCases().values(testcase_name);
0071 
0072         for (const auto& image_refQuality : dataTest)
0073         {
0074             imageNames << image_refQuality.first;
0075         }
0076 
0077         QFileInfoList list                    = imageDir().entryInfoList(imageNames, QDir::Files, QDir::Name);
0078         QHash<QString, int> results_detection = ParseTestFunc(parameter, list);
0079         QHash<QString, bool> results_test;
0080 
0081         for (const auto& image_refQuality : dataTest)
0082         {
0083             results_test.insert(image_refQuality.first, results_detection.value(image_refQuality.first) == image_refQuality.second);
0084         }
0085 
0086         return results_test;
0087     }
0088 
0089     DataTestCases getDataTestCases() const
0090     {
0091         return m_dataTestCases;
0092     }
0093 
0094 protected Q_SLOTS:
0095 
0096     void initTestCase()
0097     {
0098         QDir dir(qApp->applicationDirPath());
0099         qputenv("DK_PLUGIN_PATH", dir.canonicalPath().toUtf8());
0100         DPluginLoader::instance()->init();
0101 
0102         QScopedPointer<ExifToolParser> const parser(new ExifToolParser(nullptr));
0103         m_hasExifTool = parser->exifToolAvailable();
0104     }
0105 
0106     void cleanupTestCase()
0107     {
0108         DPluginLoader::instance()->cleanUp();
0109     }
0110 
0111 protected:
0112 
0113     DataTestCases m_dataTestCases;
0114     bool          m_hasExifTool;
0115 };
0116 
0117 #endif // DIGIKAM_IMGQSORT_TEST_H