compile
[kdegraphics.git] / gwenview / tests / thumbnailloadjobtest.cpp
blobed60633d1148ad4ad942c2c0cf97f5e0c4bba5ff
1 /*
2 Gwenview: an image viewer
3 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
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"
22 // Qt
23 #include <QDir>
24 #include <QImage>
25 #include <QPainter>
27 // KDE
28 #include <qtest_kde.h>
29 #include <kdebug.h>
30 #include <kio/deletejob.h>
32 // Local
33 #include "../lib/thumbnailloadjob.h"
34 #include "testutils.h"
37 using namespace Gwenview;
40 QTEST_KDEMAIN(ThumbnailLoadJobTest, GUI)
43 QString sandBoxPath() {
44 return QDir::currentPath() + "/sandbox";
48 void createTestImage(const QString& name, int width, int height, const QColor& color) {
49 QImage image(width, height, QImage::Format_RGB32);
50 QPainter painter(&image);
51 painter.fillRect(image.rect(), color);
52 painter.end();
53 image.save(sandBoxPath() + '/' + name, "png");
57 void ThumbnailLoadJobTest::init() {
58 ThumbnailLoadJob::setThumbnailBaseDir(sandBoxPath() + "/thumbnails/");
59 QDir dir(sandBoxPath());
60 if (dir.exists()) {
61 KUrl sandBoxUrl("file://" + sandBoxPath());
62 KIO::Job* job = KIO::del(sandBoxUrl);
63 QVERIFY2(job->exec(), "Couldn't delete sandbox");
65 dir.mkpath(".");
66 createTestImage("red.png", 300, 200, Qt::red);
67 createTestImage("blue.png", 200, 300, Qt::blue);
68 createTestImage("small.png", 50, 50, Qt::green);
72 void ThumbnailLoadJobTest::testLoadLocal() {
73 QDir dir(sandBoxPath());
75 KFileItemList list;
76 Q_FOREACH(const QFileInfo& info, dir.entryInfoList()) {
77 KUrl url("file://" + info.absoluteFilePath());
78 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
79 list << item;
81 QPointer<ThumbnailLoadJob> job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
82 // FIXME: job->exec() causes a double free(), so wait for the job to be
83 // deleted instead
84 //job->exec();
85 job->start();
86 while (job) {
87 QTest::qWait(100);
90 QDir thumbnailDir = ThumbnailLoadJob::thumbnailBaseDir(ThumbnailGroup::Normal);
91 // There should be 2 files, because small.png is too small to have a
92 // thumbnail
93 QStringList entryList = thumbnailDir.entryList(QStringList("*.png"));
94 QCOMPARE(entryList.count(), 2);
98 void ThumbnailLoadJobTest::testLoadRemote() {
99 QString testTarGzPath = pathForTestFile("test.tar.gz");
100 KUrl url;
101 url.setProtocol("tar");
102 url.setPath(testTarGzPath + "/test.png");
104 KFileItemList list;
105 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
106 list << item;
108 QPointer<ThumbnailLoadJob> job = new ThumbnailLoadJob(list, ThumbnailGroup::Normal);
109 // FIXME: job->exec() causes a double free(), so wait for the job to be
110 // deleted instead
111 //job->exec();
112 job->start();
113 while (job) {
114 QTest::qWait(100);
117 QDir thumbnailDir = ThumbnailLoadJob::thumbnailBaseDir(ThumbnailGroup::Normal);
118 QStringList entryList = thumbnailDir.entryList(QStringList("*.png"));
119 QCOMPARE(entryList.count(), 1);