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"
39
using namespace Gwenview;
42
QTEST_KDEMAIN(ThumbnailLoadJobTest, GUI)
46
: mPath(QDir::currentPath() + "/sandbox")
50
void SandBox::initDir() {
54
KUrl sandBoxUrl("file://" + mPath);
55
job = KIO::del(sandBoxUrl);
56
QVERIFY2(job->exec(), "Couldn't delete sandbox");
62
void SandBox::fill() {
64
createTestImage("red.png", 300, 200, Qt::red);
65
createTestImage("blue.png", 200, 300, Qt::blue);
66
createTestImage("small.png", 50, 50, Qt::green);
68
copyTestImage("orient6.jpg", 128, 256);
69
copyTestImage("orient6-small.jpg", 32, 64);
73
void SandBox::copyTestImage(const QString& testFileName, int width, int height) {
74
KIO::Job* job = KIO::copy(pathForTestFile(testFileName), KUrl(mPath + '/' + testFileName));
75
QVERIFY2(job->exec(), "Couldn't copy test image");
76
mSizeHash.insert(testFileName, QSize(width, height));
80
static QImage createColoredImage(int width, int height, const QColor& color) {
81
QImage image(width, height, QImage::Format_RGB32);
82
QPainter painter(&image);
83
painter.fillRect(image.rect(), color);
88
void SandBox::createTestImage(const QString& name, int width, int height, const QColor& color) {
89
QImage image = createColoredImage(width, height, color);
90
image.save(mPath + '/' + name, "png");
91
mSizeHash.insert(name, QSize(width, height));
95
void ThumbnailLoadJobTest::initTestCase() {
96
qRegisterMetaType<KFileItem>("KFileItem");
97
Gwenview::ImageFormats::registerPlugins();
101
void ThumbnailLoadJobTest::init() {
102
ThumbnailLoadJob::setThumbnailBaseDir(mSandBox.mPath + "/thumbnails/");
107
void ThumbnailLoadJobTest::testLoadLocal() {
108
QDir dir(mSandBox.mPath);
110
// Create a list of items which will be thumbnailed
112
Q_FOREACH(const QFileInfo& info, dir.entryInfoList(QDir::Files)) {
113
KUrl url("file://" + info.absoluteFilePath());
114
KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
118
// Generate the thumbnails
119
QPointer<ThumbnailLoadJob> job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
120
QSignalSpy spy(job, SIGNAL(thumbnailLoaded(const KFileItem&, const QPixmap&, const QSize&)));
121
// FIXME: job->exec() causes a double free(), so wait for the job to be
129
while (!ThumbnailLoadJob::isPendingThumbnailCacheEmpty()) {
133
// Check we generated the correct number of thumbnails
134
QDir thumbnailDir = ThumbnailLoadJob::thumbnailBaseDir(ThumbnailGroup::Normal);
135
// There should be one file less because small.png is a png and is too
136
// small to have a thumbnail
137
QStringList entryList = thumbnailDir.entryList(QStringList("*.png"));
138
QCOMPARE(entryList.count(), mSandBox.mSizeHash.size() - 1);
140
// Check what was in the thumbnailLoaded() signals
141
QCOMPARE(spy.count(), mSandBox.mSizeHash.size());
142
QSignalSpy::ConstIterator it = spy.constBegin(),
143
end = spy.constEnd();
144
for (;it != end; ++it) {
145
const QVariantList args = *it;
146
const KFileItem item = qvariant_cast<KFileItem>(args.at(0));
147
const QSize size = args.at(2).toSize();
148
const QSize expectedSize = mSandBox.mSizeHash.value(item.url().fileName());
149
QCOMPARE(size, expectedSize);
154
void ThumbnailLoadJobTest::testUseEmbeddedOrNot() {
155
QImage expectedThumbnail;
156
QPointer<ThumbnailLoadJob> job;
157
QPixmap thumbnailPix;
160
// This image is red and 256x128 but contains a white 128x64 thumbnail
161
sandBox.copyTestImage("embedded-thumbnail.jpg", 256, 128);
164
KUrl url("file://" + QDir(sandBox.mPath).absoluteFilePath("embedded-thumbnail.jpg"));
165
list << KFileItem(KFileItem::Unknown, KFileItem::Unknown, url);
167
// Loading a normal thumbnail should bring the white one
168
job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
169
QSignalSpy spy1(job, SIGNAL(thumbnailLoaded(const KFileItem&, const QPixmap&, const QSize&)));
170
// FIXME: job->exec() causes a double free(), so wait for the job to be
178
QCOMPARE(spy1.count(), 1);
179
expectedThumbnail = createColoredImage(128, 64, Qt::white);
180
thumbnailPix = qvariant_cast<QPixmap>(spy1.at(0).at(1));
181
fuzzyImageCompare(expectedThumbnail, thumbnailPix.toImage());
183
// Loading a large thumbnail should bring the red one
184
job = new ThumbnailLoadJob(list, ThumbnailGroup::Large);
185
QSignalSpy spy2(job, SIGNAL(thumbnailLoaded(const KFileItem&, const QPixmap&, const QSize&)));
191
QCOMPARE(spy2.count(), 1);
192
expectedThumbnail = createColoredImage(256, 128, Qt::red);
193
thumbnailPix = qvariant_cast<QPixmap>(spy2.at(0).at(1));
194
fuzzyImageCompare(expectedThumbnail, thumbnailPix.toImage());
198
void ThumbnailLoadJobTest::testLoadRemote() {
199
KUrl url = setUpRemoteTestDir("test.png");
200
if (!url.isValid()) {
203
url.addPath("test.png");
206
KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
209
QPointer<ThumbnailLoadJob> job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
210
// FIXME: job->exec() causes a double free(), so wait for the job to be
217
while (!ThumbnailLoadJob::isPendingThumbnailCacheEmpty()) {
221
QDir thumbnailDir = ThumbnailLoadJob::thumbnailBaseDir(ThumbnailGroup::Normal);
222
QStringList entryList = thumbnailDir.entryList(QStringList("*.png"));
223
QCOMPARE(entryList.count(), 1);