2
Gwenview: an image viewer
3
Copyright 2009 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 "importertest.moc"
29
#include <kdatetime.h>
31
#include <qtest_kde.h>
34
#include "../importer/fileutils.h"
35
#include "../importer/importer.h"
36
#include "../importer/filenameformater.h"
37
#include "testutils.h"
39
QTEST_KDEMAIN(ImporterTest, GUI)
41
using namespace Gwenview;
43
void ImporterTest::init()
45
mDocumentList = KUrl::List()
46
<< urlForTestFile("import/pict0001.jpg")
47
<< urlForTestFile("import/pict0002.jpg")
48
<< urlForTestFile("import/pict0003.jpg")
51
mTempDir.reset(new KTempDir());
54
void ImporterTest::testContentsAreIdentical()
56
QVERIFY(!FileUtils::contentsAreIdentical(mDocumentList[0], mDocumentList[1]));
57
QVERIFY(FileUtils::contentsAreIdentical(mDocumentList[0], mDocumentList[0]));
59
KUrl url1 = mDocumentList[0];
60
KUrl url2 = urlForTestOutputFile("foo");
62
// Test on a copy of a file
63
QFile::remove(url2.toLocalFile());
64
QFile::copy(url1.toLocalFile(), url2.toLocalFile());
66
QVERIFY(FileUtils::contentsAreIdentical(url1, url2));
68
// Alter one byte of the copy and test again
69
QFile file(url2.toLocalFile());
70
QVERIFY(file.open(QIODevice::ReadOnly));
71
QByteArray data = file.readAll();
73
data[data.size() / 2] = 255 - data[data.size() / 2];
75
file.open(QIODevice::WriteOnly);
79
QVERIFY(!FileUtils::contentsAreIdentical(url1, url2));
82
void ImporterTest::testSuccessfulImport()
84
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
87
QSignalSpy maximumChangedSpy(&importer, SIGNAL(maximumChanged(int)));
88
QSignalSpy errorSpy(&importer, SIGNAL(error(QString)));
90
KUrl::List list = mDocumentList;
93
connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
94
importer.start(list, destUrl);
97
QCOMPARE(maximumChangedSpy.count(), 1);
98
QCOMPARE(maximumChangedSpy.takeFirst().at(0).toInt(), list.count() * 100);
99
QCOMPARE(errorSpy.count(), 0);
101
QCOMPARE(importer.importedUrlList().count(), list.count());
102
QCOMPARE(importer.importedUrlList(), list);
103
QCOMPARE(importer.skippedUrlList().count(), 0);
104
QCOMPARE(importer.renamedCount(), 0);
106
Q_FOREACH(const KUrl & src, list) {
108
dst.addPath(src.fileName());
109
QVERIFY(FileUtils::contentsAreIdentical(src, dst));
113
void ImporterTest::testSkippedUrlList()
115
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
117
Importer importer(0);
119
KUrl::List list = mDocumentList.mid(0, 1);
122
connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
123
importer.start(list, destUrl);
126
QCOMPARE(importer.importedUrlList().count(), 1);
127
QCOMPARE(importer.importedUrlList(), list);
129
list = mDocumentList;
130
KUrl::List expectedImportedList = mDocumentList.mid(1);
131
KUrl::List expectedSkippedList = mDocumentList.mid(0, 1);
132
importer.start(list, destUrl);
135
QCOMPARE(importer.importedUrlList().count(), 2);
136
QCOMPARE(importer.importedUrlList(), expectedImportedList);
137
QCOMPARE(importer.skippedUrlList(), expectedSkippedList);
138
QCOMPARE(importer.renamedCount(), 0);
141
void ImporterTest::testRenamedCount()
143
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
145
Importer importer(0);
148
list << mDocumentList.first();
151
connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
152
importer.start(list, destUrl);
155
QCOMPARE(importer.importedUrlList().count(), 1);
156
QCOMPARE(importer.importedUrlList(), list);
158
// Modify imported document so that next import does not skip it
161
url.addPath(mDocumentList.first().fileName());
162
QFile file(url.toLocalFile());
163
QVERIFY(file.open(QIODevice::Append));
167
list = mDocumentList;
168
importer.start(list, destUrl);
171
QCOMPARE(importer.importedUrlList().count(), 3);
172
QCOMPARE(importer.importedUrlList(), mDocumentList);
173
QCOMPARE(importer.skippedUrlList().count(), 0);
174
QCOMPARE(importer.renamedCount(), 1);
177
void ImporterTest::testFileNameFormater()
179
QFETCH(QString, fileName);
180
QFETCH(QString, dateTime);
181
QFETCH(QString, format);
182
QFETCH(QString, expected);
184
KUrl url = KUrl("file://foo/bar/" + fileName);
185
FileNameFormater fileNameFormater(format);
186
QCOMPARE(fileNameFormater.format(url, KDateTime::fromString(dateTime)), expected);
189
#define NEW_ROW(fileName, dateTime, format, expected) QTest::newRow(fileName) << fileName << dateTime << format << expected
190
void ImporterTest::testFileNameFormater_data()
192
QTest::addColumn<QString>("fileName");
193
QTest::addColumn<QString>("dateTime");
194
QTest::addColumn<QString>("format");
195
QTest::addColumn<QString>("expected");
197
NEW_ROW("PICT0001.JPG", "20091024T225049", "{date}_{time}.{ext}", "2009-10-24_22-50-49.JPG");
198
NEW_ROW("PICT0001.JPG", "20091024T225049", "{date}_{time}.{ext.lower}", "2009-10-24_22-50-49.jpg");
199
NEW_ROW("PICT0001.JPG", "20091024T225049", "{name}.{ext}", "PICT0001.JPG");
200
NEW_ROW("PICT0001.JPG", "20091024T225049", "{name.lower}.{ext.lower}", "pict0001.jpg");
201
NEW_ROW("iLikeCurlies", "20091024T225049", "{{{name}}", "{iLikeCurlies}");
202
NEW_ROW("UnknownKeyword", "20091024T225049", "foo{unknown}bar", "foobar");
203
NEW_ROW("MissingClosingCurly", "20091024T225049", "foo{date", "foo");
206
void ImporterTest::testAutoRenameFormat()
208
QStringList dates = QStringList()
209
<< "1979-02-23_10-20-00"
210
<< "2006-04-01_11-30-15"
211
<< "2009-10-01_21-15-27";
212
QCOMPARE(dates.count(), mDocumentList.count());
214
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "foo");
216
Importer importer(0);
217
importer.setAutoRenameFormat("{date}_{time}.{ext}");
218
KUrl::List list = mDocumentList;
221
connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
222
importer.start(list, destUrl);
225
QCOMPARE(importer.importedUrlList().count(), list.count());
226
QCOMPARE(importer.importedUrlList(), list);
228
for (int pos = 0; pos < dates.count(); ++pos) {
229
KUrl src = list[pos];
231
dst.addPath(dates[pos] + ".jpg");
232
QVERIFY(FileUtils::contentsAreIdentical(src, dst));
236
void ImporterTest::testReadOnlyDestination()
238
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
239
chmod(QFile::encodeName(mTempDir->name()), 0555);
241
Importer importer(0);
242
QSignalSpy errorSpy(&importer, SIGNAL(error(QString)));
243
importer.start(mDocumentList, destUrl);
245
QCOMPARE(errorSpy.count(), 1);
246
QVERIFY(importer.importedUrlList().isEmpty());