2
Gwenview: an image viewer
3
Copyright 2007 AurĆ©lien GĆ¢teau <agateau@kde.org>
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU General Public License
7
as published by the Free Software Foundation; either version 2
8
of the License, or (at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include "jpegcontenttest.moc"
30
#include <qtest_kde.h>
32
#include <kfilemetainfo.h>
35
#include "../lib/orientation.h"
36
#include "../lib/jpegcontent.h"
37
#include "testutils.h"
41
const char* ORIENT6_FILE = "orient6.jpg";
42
const char* ORIENT1_VFLIP_FILE = "orient1_vflip.jpg";
43
const char* CUT_FILE = "cut.jpg";
44
const char* TMP_FILE = "tmp.jpg";
45
const char* THUMBNAIL_FILE = "test_thumbnail.jpg";
47
const int ORIENT6_WIDTH = 128; // This size is the size *after* orientation
48
const int ORIENT6_HEIGHT = 256; // has been applied
49
const QString ORIENT6_COMMENT = "a comment";
51
QTEST_KDEMAIN(JpegContentTest, GUI)
53
void JpegContentTest::initTestCase()
56
QFile in(pathForTestFile(ORIENT6_FILE));
57
result = in.open(QIODevice::ReadOnly);
61
int size = info.size() / 2;
63
char* data = new char[size];
64
int readSize = in.read(data, size);
65
QCOMPARE(size, readSize);
68
result = out.open(QIODevice::WriteOnly);
71
int wroteSize = out.write(data, size);
72
QCOMPARE(size, wroteSize);
76
void JpegContentTest::cleanupTestCase()
78
QDir::current().remove(CUT_FILE);
81
typedef QMap<QString, QString> MetaInfoMap;
83
MetaInfoMap getMetaInfo(const QString& path)
85
KFileMetaInfo fmi(path);
86
QStringList list = fmi.supportedKeys();
87
QStringList::ConstIterator it = list.constBegin();
90
for (; it != list.constEnd(); ++it) {
91
KFileMetaInfoItem item = fmi.item(*it);
92
map[*it] = item.value().toString();
98
void compareMetaInfo(const QString& path1, const QString& path2, const QStringList& ignoredKeys)
100
MetaInfoMap mim1 = getMetaInfo(path1);
101
MetaInfoMap mim2 = getMetaInfo(path2);
103
QCOMPARE(mim1.keys(), mim2.keys());
104
QList<QString> keys = mim1.keys();
105
QList<QString>::ConstIterator it = keys.constBegin();
106
for (; it != keys.constEnd(); ++it) {
108
if (ignoredKeys.contains(key)) continue;
111
QString("Meta info differs for key '%1': v1=%2 v2=%3")
116
QVERIFY2(mim1[key] == mim2[key], msg.toUtf8());
120
void JpegContentTest::testResetOrientation()
122
Gwenview::JpegContent content;
125
// Test resetOrientation without transform
126
result = content.load(pathForTestFile(ORIENT6_FILE));
129
content.resetOrientation();
131
result = content.save(TMP_FILE);
134
result = content.load(TMP_FILE);
136
QCOMPARE(content.orientation(), Gwenview::NORMAL);
138
// Test resetOrientation with transform
139
result = content.load(pathForTestFile(ORIENT6_FILE));
142
content.resetOrientation();
143
content.transform(Gwenview::ROT_90);
145
result = content.save(TMP_FILE);
148
result = content.load(TMP_FILE);
150
QCOMPARE(content.orientation(), Gwenview::NORMAL);
154
* This function tests JpegContent::transform() by applying a ROT_90
155
* transformation, saving, reloading and applying a ROT_270 to undo the ROT_90.
156
* Saving and reloading are necessary because lossless transformation only
157
* happens in JpegContent::save()
159
void JpegContentTest::testTransform()
162
QImage finalImage, expectedImage;
164
Gwenview::JpegContent content;
165
result = content.load(pathForTestFile(ORIENT6_FILE));
168
content.transform(Gwenview::ROT_90);
169
result = content.save(TMP_FILE);
172
result = content.load(TMP_FILE);
174
content.transform(Gwenview::ROT_270);
175
result = content.save(TMP_FILE);
178
result = finalImage.load(TMP_FILE);
181
result = expectedImage.load(pathForTestFile(ORIENT6_FILE));
184
QCOMPARE(finalImage , expectedImage);
187
void JpegContentTest::testSetComment()
189
QString comment = "test comment";
190
Gwenview::JpegContent content;
192
result = content.load(pathForTestFile(ORIENT6_FILE));
195
content.setComment(comment);
196
QCOMPARE(content.comment() , comment);
197
result = content.save(TMP_FILE);
200
result = content.load(TMP_FILE);
202
QCOMPARE(content.comment() , comment);
205
void JpegContentTest::testReadInfo()
207
Gwenview::JpegContent content;
208
bool result = content.load(pathForTestFile(ORIENT6_FILE));
210
QCOMPARE(int(content.orientation()), 6);
211
QCOMPARE(content.comment() , ORIENT6_COMMENT);
212
QCOMPARE(content.size() , QSize(ORIENT6_WIDTH, ORIENT6_HEIGHT));
215
void JpegContentTest::testThumbnail()
217
Gwenview::JpegContent content;
218
bool result = content.load(pathForTestFile(ORIENT6_FILE));
220
QImage thumbnail = content.thumbnail();
221
result = thumbnail.save(THUMBNAIL_FILE, "JPEG");
225
void JpegContentTest::testMultipleRotations()
227
// Test that rotating a file a lot of times does not cause findJxform() to fail
228
Gwenview::JpegContent content;
229
bool result = content.load(pathForTestFile(ORIENT6_FILE));
231
result = content.load(pathForTestFile(ORIENT6_FILE));
234
// 12*4 + 1 is the same as 1, since rotating four times brings you back
235
for (int loop = 0; loop < 12 * 4 + 1; ++loop) {
236
content.transform(Gwenview::ROT_90);
238
result = content.save(TMP_FILE);
241
result = content.load(TMP_FILE);
244
QCOMPARE(content.size() , QSize(ORIENT6_HEIGHT, ORIENT6_WIDTH));
246
// Check the other meta info are still here
247
QStringList ignoredKeys;
248
ignoredKeys << "Orientation" << "Comment";
249
compareMetaInfo(pathForTestFile(ORIENT6_FILE), pathForTestFile(ORIENT1_VFLIP_FILE), ignoredKeys);
252
void JpegContentTest::testLoadTruncated()
254
// Test that loading and manipulating a truncated file does not crash
255
Gwenview::JpegContent content;
256
bool result = content.load(CUT_FILE);
258
QCOMPARE(int(content.orientation()), 6);
259
QCOMPARE(content.comment() , ORIENT6_COMMENT);
260
content.transform(Gwenview::VFLIP);
261
kWarning() << "# Next function should output errors about incomplete image" ;
262
content.save(TMP_FILE);
266
void JpegContentTest::testRawData()
268
Gwenview::JpegContent content;
269
bool result = content.load(pathForTestFile(ORIENT6_FILE));
273
QFile file(pathForTestFile(ORIENT6_FILE));
274
result = file.open(QIODevice::ReadOnly);
276
fileData = file.readAll();
278
QCOMPARE(content.rawData(), fileData);
281
void JpegContentTest::testSetImage()
283
Gwenview::JpegContent content;
284
bool result = content.load(pathForTestFile(ORIENT6_FILE));
287
QImage image = QImage(400, 300, QImage::Format_RGB32);
290
content.setImage(image);
292
result = content.save(TMP_FILE);
295
result = content.load(TMP_FILE);
298
QCOMPARE(content.size(), image.size());
300
QStringList ignoredKeys;
301
ignoredKeys << "Orientation";
302
compareMetaInfo(pathForTestFile(ORIENT6_FILE), pathForTestFile(TMP_FILE), ignoredKeys);