~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to tests/auto/jpegcontenttest.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Gwenview: an image viewer
 
3
Copyright 2007 AurĆ©lien GĆ¢teau <agateau@kde.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
18
 
 
19
*/
 
20
#include "jpegcontenttest.moc"
 
21
#include <iostream>
 
22
 
 
23
// Qt
 
24
#include <QDir>
 
25
#include <QFile>
 
26
#include <QImage>
 
27
#include <QString>
 
28
 
 
29
// KDE
 
30
#include <qtest_kde.h>
 
31
#include <kdebug.h>
 
32
#include <kfilemetainfo.h>
 
33
 
 
34
// Local
 
35
#include "../lib/orientation.h"
 
36
#include "../lib/jpegcontent.h"
 
37
#include "testutils.h"
 
38
 
 
39
using namespace std;
 
40
 
 
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";
 
46
 
 
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";
 
50
 
 
51
QTEST_KDEMAIN(JpegContentTest, GUI)
 
52
 
 
53
void JpegContentTest::initTestCase()
 
54
{
 
55
    bool result;
 
56
    QFile in(pathForTestFile(ORIENT6_FILE));
 
57
    result = in.open(QIODevice::ReadOnly);
 
58
    QVERIFY(result);
 
59
 
 
60
    QFileInfo info(in);
 
61
    int size = info.size() / 2;
 
62
 
 
63
    char* data = new char[size];
 
64
    int readSize = in.read(data, size);
 
65
    QCOMPARE(size, readSize);
 
66
 
 
67
    QFile out(CUT_FILE);
 
68
    result = out.open(QIODevice::WriteOnly);
 
69
    QVERIFY(result);
 
70
 
 
71
    int wroteSize = out.write(data, size);
 
72
    QCOMPARE(size, wroteSize);
 
73
    delete []data;
 
74
}
 
75
 
 
76
void JpegContentTest::cleanupTestCase()
 
77
{
 
78
    QDir::current().remove(CUT_FILE);
 
79
}
 
80
 
 
81
typedef QMap<QString, QString> MetaInfoMap;
 
82
 
 
83
MetaInfoMap getMetaInfo(const QString& path)
 
84
{
 
85
    KFileMetaInfo fmi(path);
 
86
    QStringList list = fmi.supportedKeys();
 
87
    QStringList::ConstIterator it = list.constBegin();
 
88
    MetaInfoMap map;
 
89
 
 
90
    for (; it != list.constEnd(); ++it) {
 
91
        KFileMetaInfoItem item = fmi.item(*it);
 
92
        map[*it] = item.value().toString();
 
93
    }
 
94
 
 
95
    return map;
 
96
}
 
97
 
 
98
void compareMetaInfo(const QString& path1, const QString& path2, const QStringList& ignoredKeys)
 
99
{
 
100
    MetaInfoMap mim1 = getMetaInfo(path1);
 
101
    MetaInfoMap mim2 = getMetaInfo(path2);
 
102
 
 
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) {
 
107
        QString key = *it;
 
108
        if (ignoredKeys.contains(key)) continue;
 
109
 
 
110
        QString msg =
 
111
            QString("Meta info differs for key '%1': v1=%2 v2=%3")
 
112
            .arg(key)
 
113
            .arg(mim1[key])
 
114
            .arg(mim2[key]);
 
115
 
 
116
        QVERIFY2(mim1[key] == mim2[key], msg.toUtf8());
 
117
    }
 
118
}
 
119
 
 
120
void JpegContentTest::testResetOrientation()
 
121
{
 
122
    Gwenview::JpegContent content;
 
123
    bool result;
 
124
 
 
125
    // Test resetOrientation without transform
 
126
    result = content.load(pathForTestFile(ORIENT6_FILE));
 
127
    QVERIFY(result);
 
128
 
 
129
    content.resetOrientation();
 
130
 
 
131
    result = content.save(TMP_FILE);
 
132
    QVERIFY(result);
 
133
 
 
134
    result = content.load(TMP_FILE);
 
135
    QVERIFY(result);
 
136
    QCOMPARE(content.orientation(), Gwenview::NORMAL);
 
137
 
 
138
    // Test resetOrientation with transform
 
139
    result = content.load(pathForTestFile(ORIENT6_FILE));
 
140
    QVERIFY(result);
 
141
 
 
142
    content.resetOrientation();
 
143
    content.transform(Gwenview::ROT_90);
 
144
 
 
145
    result = content.save(TMP_FILE);
 
146
    QVERIFY(result);
 
147
 
 
148
    result = content.load(TMP_FILE);
 
149
    QVERIFY(result);
 
150
    QCOMPARE(content.orientation(), Gwenview::NORMAL);
 
151
}
 
152
 
 
153
/**
 
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()
 
158
 */
 
159
void JpegContentTest::testTransform()
 
160
{
 
161
    bool result;
 
162
    QImage finalImage, expectedImage;
 
163
 
 
164
    Gwenview::JpegContent content;
 
165
    result = content.load(pathForTestFile(ORIENT6_FILE));
 
166
    QVERIFY(result);
 
167
 
 
168
    content.transform(Gwenview::ROT_90);
 
169
    result = content.save(TMP_FILE);
 
170
    QVERIFY(result);
 
171
 
 
172
    result = content.load(TMP_FILE);
 
173
    QVERIFY(result);
 
174
    content.transform(Gwenview::ROT_270);
 
175
    result = content.save(TMP_FILE);
 
176
    QVERIFY(result);
 
177
 
 
178
    result = finalImage.load(TMP_FILE);
 
179
    QVERIFY(result);
 
180
 
 
181
    result = expectedImage.load(pathForTestFile(ORIENT6_FILE));
 
182
    QVERIFY(result);
 
183
 
 
184
    QCOMPARE(finalImage , expectedImage);
 
185
}
 
186
 
 
187
void JpegContentTest::testSetComment()
 
188
{
 
189
    QString comment = "test comment";
 
190
    Gwenview::JpegContent content;
 
191
    bool result;
 
192
    result = content.load(pathForTestFile(ORIENT6_FILE));
 
193
    QVERIFY(result);
 
194
 
 
195
    content.setComment(comment);
 
196
    QCOMPARE(content.comment() , comment);
 
197
    result = content.save(TMP_FILE);
 
198
    QVERIFY(result);
 
199
 
 
200
    result = content.load(TMP_FILE);
 
201
    QVERIFY(result);
 
202
    QCOMPARE(content.comment() , comment);
 
203
}
 
204
 
 
205
void JpegContentTest::testReadInfo()
 
206
{
 
207
    Gwenview::JpegContent content;
 
208
    bool result = content.load(pathForTestFile(ORIENT6_FILE));
 
209
    QVERIFY(result);
 
210
    QCOMPARE(int(content.orientation()), 6);
 
211
    QCOMPARE(content.comment() , ORIENT6_COMMENT);
 
212
    QCOMPARE(content.size() , QSize(ORIENT6_WIDTH, ORIENT6_HEIGHT));
 
213
}
 
214
 
 
215
void JpegContentTest::testThumbnail()
 
216
{
 
217
    Gwenview::JpegContent content;
 
218
    bool result = content.load(pathForTestFile(ORIENT6_FILE));
 
219
    QVERIFY(result);
 
220
    QImage thumbnail = content.thumbnail();
 
221
    result = thumbnail.save(THUMBNAIL_FILE, "JPEG");
 
222
    QVERIFY(result);
 
223
}
 
224
 
 
225
void JpegContentTest::testMultipleRotations()
 
226
{
 
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));
 
230
    QVERIFY(result);
 
231
    result = content.load(pathForTestFile(ORIENT6_FILE));
 
232
    QVERIFY(result);
 
233
 
 
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);
 
237
    }
 
238
    result = content.save(TMP_FILE);
 
239
    QVERIFY(result);
 
240
 
 
241
    result = content.load(TMP_FILE);
 
242
    QVERIFY(result);
 
243
 
 
244
    QCOMPARE(content.size() , QSize(ORIENT6_HEIGHT, ORIENT6_WIDTH));
 
245
 
 
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);
 
250
}
 
251
 
 
252
void JpegContentTest::testLoadTruncated()
 
253
{
 
254
    // Test that loading and manipulating a truncated file does not crash
 
255
    Gwenview::JpegContent content;
 
256
    bool result = content.load(CUT_FILE);
 
257
    QVERIFY(result);
 
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);
 
263
    kWarning() << "#" ;
 
264
}
 
265
 
 
266
void JpegContentTest::testRawData()
 
267
{
 
268
    Gwenview::JpegContent content;
 
269
    bool result = content.load(pathForTestFile(ORIENT6_FILE));
 
270
    QVERIFY(result);
 
271
 
 
272
    QByteArray fileData;
 
273
    QFile file(pathForTestFile(ORIENT6_FILE));
 
274
    result = file.open(QIODevice::ReadOnly);
 
275
    QVERIFY(result);
 
276
    fileData = file.readAll();
 
277
 
 
278
    QCOMPARE(content.rawData(), fileData);
 
279
}
 
280
 
 
281
void JpegContentTest::testSetImage()
 
282
{
 
283
    Gwenview::JpegContent content;
 
284
    bool result = content.load(pathForTestFile(ORIENT6_FILE));
 
285
    QVERIFY(result);
 
286
 
 
287
    QImage image = QImage(400, 300, QImage::Format_RGB32);
 
288
    image.fill(Qt::red);
 
289
 
 
290
    content.setImage(image);
 
291
 
 
292
    result = content.save(TMP_FILE);
 
293
    QVERIFY(result);
 
294
 
 
295
    result = content.load(TMP_FILE);
 
296
    QVERIFY(result);
 
297
 
 
298
    QCOMPARE(content.size(), image.size());
 
299
 
 
300
    QStringList ignoredKeys;
 
301
    ignoredKeys << "Orientation";
 
302
    compareMetaInfo(pathForTestFile(ORIENT6_FILE), pathForTestFile(TMP_FILE), ignoredKeys);
 
303
}