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

« back to all changes in this revision

Viewing changes to tests/auto/thumbnailloadjobtest.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 "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
using namespace Gwenview;
 
39
 
 
40
QTEST_KDEMAIN(ThumbnailLoadJobTest, GUI)
 
41
 
 
42
SandBox::SandBox()
 
43
: mPath(QDir::currentPath() + "/sandbox")
 
44
{}
 
45
 
 
46
void SandBox::initDir()
 
47
{
 
48
    KIO::Job* job;
 
49
    QDir dir(mPath);
 
50
    if (dir.exists()) {
 
51
        KUrl sandBoxUrl("file://" + mPath);
 
52
        job = KIO::del(sandBoxUrl);
 
53
        QVERIFY2(job->exec(), "Couldn't delete sandbox");
 
54
    }
 
55
    dir.mkpath(".");
 
56
}
 
57
 
 
58
void SandBox::fill()
 
59
{
 
60
    initDir();
 
61
    createTestImage("red.png", 300, 200, Qt::red);
 
62
    createTestImage("blue.png", 200, 300, Qt::blue);
 
63
    createTestImage("small.png", 50, 50, Qt::green);
 
64
 
 
65
    copyTestImage("orient6.jpg", 128, 256);
 
66
    copyTestImage("orient6-small.jpg", 32, 64);
 
67
}
 
68
 
 
69
void SandBox::copyTestImage(const QString& testFileName, int width, int height)
 
70
{
 
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));
 
75
}
 
76
 
 
77
static QImage createColoredImage(int width, int height, const QColor& color)
 
78
{
 
79
    QImage image(width, height, QImage::Format_RGB32);
 
80
    QPainter painter(&image);
 
81
    painter.fillRect(image.rect(), color);
 
82
    return image;
 
83
}
 
84
 
 
85
void SandBox::createTestImage(const QString& name, int width, int height, const QColor& color)
 
86
{
 
87
    QImage image = createColoredImage(width, height, color);
 
88
    image.save(mPath + '/' + name, "png");
 
89
    mSizeHash.insert(name, QSize(width, height));
 
90
}
 
91
 
 
92
void ThumbnailLoadJobTest::initTestCase()
 
93
{
 
94
    qRegisterMetaType<KFileItem>("KFileItem");
 
95
    Gwenview::ImageFormats::registerPlugins();
 
96
}
 
97
 
 
98
void ThumbnailLoadJobTest::init()
 
99
{
 
100
    ThumbnailLoadJob::setThumbnailBaseDir(mSandBox.mPath + "/thumbnails/");
 
101
    mSandBox.fill();
 
102
}
 
103
 
 
104
void ThumbnailLoadJobTest::testLoadLocal()
 
105
{
 
106
    QDir dir(mSandBox.mPath);
 
107
 
 
108
    // Create a list of items which will be thumbnailed
 
109
    KFileItemList list;
 
110
    Q_FOREACH(const QFileInfo & info, dir.entryInfoList(QDir::Files)) {
 
111
        KUrl url("file://" + info.absoluteFilePath());
 
112
        KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
 
113
        list << item;
 
114
    }
 
115
 
 
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
 
120
    // deleted instead
 
121
    //job->exec();
 
122
    job->start();
 
123
    while (job) {
 
124
        QTest::qWait(100);
 
125
    }
 
126
 
 
127
    while (!ThumbnailLoadJob::isPendingThumbnailCacheEmpty()) {
 
128
        QTest::qWait(100);
 
129
    }
 
130
 
 
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);
 
137
 
 
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);
 
148
    }
 
149
}
 
150
 
 
151
void ThumbnailLoadJobTest::testUseEmbeddedOrNot()
 
152
{
 
153
    QImage expectedThumbnail;
 
154
    QPointer<ThumbnailLoadJob> job;
 
155
    QPixmap thumbnailPix;
 
156
    SandBox sandBox;
 
157
    sandBox.initDir();
 
158
    // This image is red and 256x128 but contains a white 128x64 thumbnail
 
159
    sandBox.copyTestImage("embedded-thumbnail.jpg", 256, 128);
 
160
 
 
161
    KFileItemList list;
 
162
    KUrl url("file://" + QDir(sandBox.mPath).absoluteFilePath("embedded-thumbnail.jpg"));
 
163
    list << KFileItem(KFileItem::Unknown, KFileItem::Unknown, url);
 
164
 
 
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
 
169
    // deleted instead
 
170
    //job->exec();
 
171
    job->start();
 
172
    while (job) {
 
173
        QTest::qWait(100);
 
174
    }
 
175
 
 
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());
 
180
 
 
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)));
 
184
    job->start();
 
185
    while (job) {
 
186
        QTest::qWait(100);
 
187
    }
 
188
 
 
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());
 
193
}
 
194
 
 
195
void ThumbnailLoadJobTest::testLoadRemote()
 
196
{
 
197
    KUrl url = setUpRemoteTestDir("test.png");
 
198
    if (!url.isValid()) {
 
199
        return;
 
200
    }
 
201
    url.addPath("test.png");
 
202
 
 
203
    KFileItemList list;
 
204
    KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
 
205
    list << item;
 
206
 
 
207
    QPointer<ThumbnailLoadJob> job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
 
208
    // FIXME: job->exec() causes a double free(), so wait for the job to be
 
209
    // deleted instead
 
210
    //job->exec();
 
211
    job->start();
 
212
    while (job) {
 
213
        QTest::qWait(100);
 
214
    }
 
215
    while (!ThumbnailLoadJob::isPendingThumbnailCacheEmpty()) {
 
216
        QTest::qWait(100);
 
217
    }
 
218
 
 
219
    QDir thumbnailDir = ThumbnailLoadJob::thumbnailBaseDir(ThumbnailGroup::Normal);
 
220
    QStringList entryList = thumbnailDir.entryList(QStringList("*.png"));
 
221
    QCOMPARE(entryList.count(), 1);
 
222
}