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 load metadata from byte array
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 "loadfromba_utest.h"
25 
26 // Qt includes
27 
28 #include <QFile>
29 #include <QDataStream>
30 #include <QImage>
31 #include <QByteArray>
32 
QTEST_MAIN(LoadFromBATest)33 QTEST_MAIN(LoadFromBATest)
34 
35 LoadFromBATest::LoadFromBATest(QObject* const parent)
36     : AbstractUnitTest(parent)
37 {
38 }
39 
testLoadFromByteArray()40 void LoadFromBATest::testLoadFromByteArray()
41 {
42     loadFromByteArray(m_originalImageFolder + QLatin1String("nikon-e2100.jpg"));
43 }
44 
loadFromByteArray(const QString & file)45 void LoadFromBATest::loadFromByteArray(const QString& file)
46 {
47     qCDebug(DIGIKAM_TESTS_LOG) << "File to process:" << file;
48     QString path = m_tempDir.filePath(QFileInfo(file).fileName().trimmed());
49 
50     qCDebug(DIGIKAM_TESTS_LOG) << "Temporary target file:" << path;
51 
52     bool ret = !path.isNull();
53     QVERIFY(ret);
54 
55     QFile target(file);
56     ret = target.copy(path);
57     QVERIFY(ret);
58 
59     QString baFile(path + QLatin1String("ba.dat"));
60 
61     QImage image(file);
62     ret = image.save(baFile, "PNG");
63     QVERIFY(ret);
64 
65     QFile baf(baFile);
66     ret = baf.open(QIODevice::ReadOnly);
67     QVERIFY(ret);
68 
69     QByteArray data;
70     data.resize(baf.size());
71     QDataStream stream(&baf);
72     ret = stream.readRawData(data.data(), data.size());
73     baf.close();
74     QVERIFY(ret);
75 
76     QScopedPointer<DMetadata> meta(new DMetadata);
77     ret = meta->loadFromData(data);
78     QVERIFY(ret);
79 }
80