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 "thumbnailloadjobtest.moc"
28
#include <qtest_kde.h>
30
#include <kio/copyjob.h>
31
#include <kio/deletejob.h>
34
#include "../lib/imageformats/imageformats.h"
35
#include "../lib/thumbnailloadjob.h"
36
#include "testutils.h"
38
using namespace Gwenview;
40
QTEST_KDEMAIN(ThumbnailLoadJobTest, GUI)
43
: mPath(QDir::currentPath() + "/sandbox")
46
void SandBox::initDir()
51
KUrl sandBoxUrl("file://" + mPath);
52
job = KIO::del(sandBoxUrl);
53
QVERIFY2(job->exec(), "Couldn't delete sandbox");
61
createTestImage("red.png", 300, 200, Qt::red);
62
createTestImage("blue.png", 200, 300, Qt::blue);
63
createTestImage("small.png", 50, 50, Qt::green);
65
copyTestImage("orient6.jpg", 128, 256);
66
copyTestImage("orient6-small.jpg", 32, 64);
69
void SandBox::copyTestImage(const QString& testFileName, int width, int height)
71
QString testPath = pathForTestFile(testFileName);
72
KIO::Job* job = KIO::copy(testPath, KUrl(mPath + '/' + testFileName));
73
QVERIFY2(job->exec(), "Couldn't copy test image");
74
mSizeHash.insert(testFileName, QSize(width, height));
77
static QImage createColoredImage(int width, int height, const QColor& color)
79
QImage image(width, height, QImage::Format_RGB32);
80
QPainter painter(&image);
81
painter.fillRect(image.rect(), color);
85
void SandBox::createTestImage(const QString& name, int width, int height, const QColor& color)
87
QImage image = createColoredImage(width, height, color);
88
image.save(mPath + '/' + name, "png");
89
mSizeHash.insert(name, QSize(width, height));
92
void ThumbnailLoadJobTest::initTestCase()
94
qRegisterMetaType<KFileItem>("KFileItem");
95
Gwenview::ImageFormats::registerPlugins();
98
void ThumbnailLoadJobTest::init()
100
ThumbnailLoadJob::setThumbnailBaseDir(mSandBox.mPath + "/thumbnails/");
104
void ThumbnailLoadJobTest::testLoadLocal()
106
QDir dir(mSandBox.mPath);
108
// Create a list of items which will be thumbnailed
110
Q_FOREACH(const QFileInfo & info, dir.entryInfoList(QDir::Files)) {
111
KUrl url("file://" + info.absoluteFilePath());
112
KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
116
// Generate the thumbnails
117
QPointer<ThumbnailLoadJob> job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
118
QSignalSpy spy(job, SIGNAL(thumbnailLoaded(KFileItem, QPixmap, QSize)));
119
// FIXME: job->exec() causes a double free(), so wait for the job to be
127
while (!ThumbnailLoadJob::isPendingThumbnailCacheEmpty()) {
131
// Check we generated the correct number of thumbnails
132
QDir thumbnailDir = ThumbnailLoadJob::thumbnailBaseDir(ThumbnailGroup::Normal);
133
// There should be one file less because small.png is a png and is too
134
// small to have a thumbnail
135
QStringList entryList = thumbnailDir.entryList(QStringList("*.png"));
136
QCOMPARE(entryList.count(), mSandBox.mSizeHash.size() - 1);
138
// Check what was in the thumbnailLoaded() signals
139
QCOMPARE(spy.count(), mSandBox.mSizeHash.size());
140
QSignalSpy::ConstIterator it = spy.constBegin(),
141
end = spy.constEnd();
142
for (; it != end; ++it) {
143
const QVariantList args = *it;
144
const KFileItem item = qvariant_cast<KFileItem>(args.at(0));
145
const QSize size = args.at(2).toSize();
146
const QSize expectedSize = mSandBox.mSizeHash.value(item.url().fileName());
147
QCOMPARE(size, expectedSize);
151
void ThumbnailLoadJobTest::testUseEmbeddedOrNot()
153
QImage expectedThumbnail;
154
QPointer<ThumbnailLoadJob> job;
155
QPixmap thumbnailPix;
158
// This image is red and 256x128 but contains a white 128x64 thumbnail
159
sandBox.copyTestImage("embedded-thumbnail.jpg", 256, 128);
162
KUrl url("file://" + QDir(sandBox.mPath).absoluteFilePath("embedded-thumbnail.jpg"));
163
list << KFileItem(KFileItem::Unknown, KFileItem::Unknown, url);
165
// Loading a normal thumbnail should bring the white one
166
job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
167
QSignalSpy spy1(job, SIGNAL(thumbnailLoaded(KFileItem, QPixmap, QSize)));
168
// FIXME: job->exec() causes a double free(), so wait for the job to be
176
QCOMPARE(spy1.count(), 1);
177
expectedThumbnail = createColoredImage(128, 64, Qt::white);
178
thumbnailPix = qvariant_cast<QPixmap>(spy1.at(0).at(1));
179
fuzzyImageCompare(expectedThumbnail, thumbnailPix.toImage());
181
// Loading a large thumbnail should bring the red one
182
job = new ThumbnailLoadJob(list, ThumbnailGroup::Large);
183
QSignalSpy spy2(job, SIGNAL(thumbnailLoaded(KFileItem, QPixmap, QSize)));
189
QCOMPARE(spy2.count(), 1);
190
expectedThumbnail = createColoredImage(256, 128, Qt::red);
191
thumbnailPix = qvariant_cast<QPixmap>(spy2.at(0).at(1));
192
fuzzyImageCompare(expectedThumbnail, thumbnailPix.toImage());
195
void ThumbnailLoadJobTest::testLoadRemote()
197
KUrl url = setUpRemoteTestDir("test.png");
198
if (!url.isValid()) {
201
url.addPath("test.png");
204
KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
207
QPointer<ThumbnailLoadJob> job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
208
// FIXME: job->exec() causes a double free(), so wait for the job to be
215
while (!ThumbnailLoadJob::isPendingThumbnailCacheEmpty()) {
219
QDir thumbnailDir = ThumbnailLoadJob::thumbnailBaseDir(ThumbnailGroup::Normal);
220
QStringList entryList = thumbnailDir.entryList(QStringList("*.png"));
221
QCOMPARE(entryList.count(), 1);