~ubuntu-branches/ubuntu/trusty/gwenview/trusty

« back to all changes in this revision

Viewing changes to tests/thumbnailloadjobtest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2011-07-20 13:46:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110720134634-92930fdjeed4gdc9
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

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 "thumbnailloadjobtest.moc"
 
21
 
 
22
// Qt
 
23
#include <QDir>
 
24
#include <QImage>
 
25
#include <QPainter>
 
26
 
 
27
// KDE
 
28
#include <qtest_kde.h>
 
29
#include <kdebug.h>
 
30
#include <kio/copyjob.h>
 
31
#include <kio/deletejob.h>
 
32
 
 
33
// Local
 
34
#include "../lib/imageformats/imageformats.h"
 
35
#include "../lib/thumbnailloadjob.h"
 
36
#include "testutils.h"
 
37
 
 
38
 
 
39
using namespace Gwenview;
 
40
 
 
41
 
 
42
QTEST_KDEMAIN(ThumbnailLoadJobTest, GUI)
 
43
 
 
44
 
 
45
SandBox::SandBox()
 
46
: mPath(QDir::currentPath() + "/sandbox")
 
47
{}
 
48
 
 
49
 
 
50
void SandBox::initDir() {
 
51
        KIO::Job* job;
 
52
        QDir dir(mPath);
 
53
        if (dir.exists()) {
 
54
                KUrl sandBoxUrl("file://" + mPath);
 
55
                job = KIO::del(sandBoxUrl);
 
56
                QVERIFY2(job->exec(), "Couldn't delete sandbox");
 
57
        }
 
58
        dir.mkpath(".");
 
59
}
 
60
 
 
61
 
 
62
void SandBox::fill() {
 
63
        initDir();
 
64
        createTestImage("red.png", 300, 200, Qt::red);
 
65
        createTestImage("blue.png", 200, 300, Qt::blue);
 
66
        createTestImage("small.png", 50, 50, Qt::green);
 
67
 
 
68
        copyTestImage("orient6.jpg", 128, 256);
 
69
        copyTestImage("orient6-small.jpg", 32, 64);
 
70
}
 
71
 
 
72
 
 
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));
 
77
}
 
78
 
 
79
 
 
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);
 
84
        return image;
 
85
}
 
86
 
 
87
 
 
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));
 
92
}
 
93
 
 
94
 
 
95
void ThumbnailLoadJobTest::initTestCase() {
 
96
        qRegisterMetaType<KFileItem>("KFileItem");
 
97
        Gwenview::ImageFormats::registerPlugins();
 
98
}
 
99
 
 
100
 
 
101
void ThumbnailLoadJobTest::init() {
 
102
        ThumbnailLoadJob::setThumbnailBaseDir(mSandBox.mPath + "/thumbnails/");
 
103
        mSandBox.fill();
 
104
}
 
105
 
 
106
 
 
107
void ThumbnailLoadJobTest::testLoadLocal() {
 
108
        QDir dir(mSandBox.mPath);
 
109
 
 
110
    // Create a list of items which will be thumbnailed
 
111
        KFileItemList list;
 
112
        Q_FOREACH(const QFileInfo& info, dir.entryInfoList(QDir::Files)) {
 
113
                KUrl url("file://" + info.absoluteFilePath());
 
114
                KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
 
115
                list << item;
 
116
        }
 
117
 
 
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
 
122
        // deleted instead
 
123
        //job->exec();
 
124
        job->start();
 
125
        while (job) {
 
126
                QTest::qWait(100);
 
127
        }
 
128
 
 
129
        while (!ThumbnailLoadJob::isPendingThumbnailCacheEmpty()) {
 
130
                QTest::qWait(100);
 
131
        }
 
132
 
 
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);
 
139
 
 
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);
 
150
        }
 
151
}
 
152
 
 
153
 
 
154
void ThumbnailLoadJobTest::testUseEmbeddedOrNot() {
 
155
        QImage expectedThumbnail;
 
156
        QPointer<ThumbnailLoadJob> job;
 
157
        QPixmap thumbnailPix;
 
158
        SandBox sandBox;
 
159
        sandBox.initDir();
 
160
        // This image is red and 256x128 but contains a white 128x64 thumbnail
 
161
        sandBox.copyTestImage("embedded-thumbnail.jpg", 256, 128);
 
162
 
 
163
        KFileItemList list;
 
164
        KUrl url("file://" + QDir(sandBox.mPath).absoluteFilePath("embedded-thumbnail.jpg"));
 
165
        list << KFileItem(KFileItem::Unknown, KFileItem::Unknown, url);
 
166
 
 
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
 
171
        // deleted instead
 
172
        //job->exec();
 
173
        job->start();
 
174
        while (job) {
 
175
                QTest::qWait(100);
 
176
        }
 
177
 
 
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());
 
182
 
 
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&)));
 
186
        job->start();
 
187
        while (job) {
 
188
                QTest::qWait(100);
 
189
        }
 
190
 
 
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());
 
195
}
 
196
 
 
197
 
 
198
void ThumbnailLoadJobTest::testLoadRemote() {
 
199
        KUrl url = setUpRemoteTestDir("test.png");
 
200
        if (!url.isValid()) {
 
201
                return;
 
202
        }
 
203
        url.addPath("test.png");
 
204
 
 
205
        KFileItemList list;
 
206
        KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
 
207
        list << item;
 
208
 
 
209
        QPointer<ThumbnailLoadJob> job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
 
210
        // FIXME: job->exec() causes a double free(), so wait for the job to be
 
211
        // deleted instead
 
212
        //job->exec();
 
213
        job->start();
 
214
        while (job) {
 
215
                QTest::qWait(100);
 
216
        }
 
217
        while (!ThumbnailLoadJob::isPendingThumbnailCacheEmpty()) {
 
218
                QTest::qWait(100);
 
219
        }
 
220
 
 
221
        QDir thumbnailDir = ThumbnailLoadJob::thumbnailBaseDir(ThumbnailGroup::Normal);
 
222
        QStringList entryList = thumbnailDir.entryList(QStringList("*.png"));
 
223
        QCOMPARE(entryList.count(), 1);
 
224
}