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() {
44
mDocumentList = KUrl::List()
45
<< urlForTestFile("import/pict0001.jpg")
46
<< urlForTestFile("import/pict0002.jpg")
47
<< urlForTestFile("import/pict0003.jpg")
50
mTempDir.reset(new KTempDir());
53
void ImporterTest::testContentsAreIdentical() {
54
QVERIFY(!FileUtils::contentsAreIdentical(mDocumentList[0], mDocumentList[1]));
55
QVERIFY(FileUtils::contentsAreIdentical(mDocumentList[0], mDocumentList[0]));
57
KUrl url1 = mDocumentList[0];
58
KUrl url2 = urlForTestOutputFile("foo");
60
// Test on a copy of a file
61
QFile::remove(url2.toLocalFile());
62
QFile::copy(url1.toLocalFile(), url2.toLocalFile());
64
QVERIFY(FileUtils::contentsAreIdentical(url1, url2));
66
// Alter one byte of the copy and test again
67
QFile file(url2.toLocalFile());
68
QVERIFY(file.open(QIODevice::ReadOnly));
69
QByteArray data = file.readAll();
71
data[data.size() / 2] = 255 - data[data.size() / 2];
73
file.open(QIODevice::WriteOnly);
77
QVERIFY(!FileUtils::contentsAreIdentical(url1, url2));
80
void ImporterTest::testSuccessfulImport() {
81
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
84
QSignalSpy maximumChangedSpy(&importer, SIGNAL(maximumChanged(int)));
85
QSignalSpy errorSpy(&importer, SIGNAL(error(const QString&)));
87
KUrl::List list = mDocumentList;
90
connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
91
importer.start(list, destUrl);
94
QCOMPARE(maximumChangedSpy.count(), 1);
95
QCOMPARE(maximumChangedSpy.takeFirst().at(0).toInt(), list.count() * 100);
96
QCOMPARE(errorSpy.count(), 0);
98
QCOMPARE(importer.importedUrlList().count(), list.count());
99
QCOMPARE(importer.importedUrlList(), list);
100
QCOMPARE(importer.skippedUrlList().count(), 0);
101
QCOMPARE(importer.renamedCount(), 0);
103
Q_FOREACH(const KUrl& src, list) {
105
dst.addPath(src.fileName());
106
QVERIFY(FileUtils::contentsAreIdentical(src, dst));
110
void ImporterTest::testSkippedUrlList() {
111
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
113
Importer importer(0);
115
KUrl::List list = mDocumentList.mid(0, 1);
118
connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
119
importer.start(list, destUrl);
122
QCOMPARE(importer.importedUrlList().count(), 1);
123
QCOMPARE(importer.importedUrlList(), list);
125
list = mDocumentList;
126
KUrl::List expectedImportedList = mDocumentList.mid(1);
127
KUrl::List expectedSkippedList = mDocumentList.mid(0, 1);
128
importer.start(list, destUrl);
131
QCOMPARE(importer.importedUrlList().count(), 2);
132
QCOMPARE(importer.importedUrlList(), expectedImportedList);
133
QCOMPARE(importer.skippedUrlList(), expectedSkippedList);
134
QCOMPARE(importer.renamedCount(), 0);
137
void ImporterTest::testRenamedCount() {
138
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
140
Importer importer(0);
143
list << mDocumentList.first();
146
connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
147
importer.start(list, destUrl);
150
QCOMPARE(importer.importedUrlList().count(), 1);
151
QCOMPARE(importer.importedUrlList(), list);
153
// Modify imported document so that next import does not skip it
156
url.addPath(mDocumentList.first().fileName());
157
QFile file(url.toLocalFile());
158
QVERIFY(file.open(QIODevice::Append));
162
list = mDocumentList;
163
importer.start(list, destUrl);
166
QCOMPARE(importer.importedUrlList().count(), 3);
167
QCOMPARE(importer.importedUrlList(), mDocumentList);
168
QCOMPARE(importer.skippedUrlList().count(), 0);
169
QCOMPARE(importer.renamedCount(), 1);
172
void ImporterTest::testFileNameFormater() {
173
QFETCH(QString, fileName);
174
QFETCH(QString, dateTime);
175
QFETCH(QString, format);
176
QFETCH(QString, expected);
178
KUrl url = KUrl("file://foo/bar/" + fileName);
179
FileNameFormater fileNameFormater(format);
180
QCOMPARE(fileNameFormater.format(url, KDateTime::fromString(dateTime)), expected);
183
#define NEW_ROW(fileName, dateTime, format, expected) QTest::newRow(fileName) << fileName << dateTime << format << expected
184
void ImporterTest::testFileNameFormater_data() {
185
QTest::addColumn<QString>("fileName");
186
QTest::addColumn<QString>("dateTime");
187
QTest::addColumn<QString>("format");
188
QTest::addColumn<QString>("expected");
190
NEW_ROW("PICT0001.JPG", "20091024T225049", "{date}_{time}.{ext}", "2009-10-24_22-50-49.JPG");
191
NEW_ROW("PICT0001.JPG", "20091024T225049", "{date}_{time}.{ext.lower}", "2009-10-24_22-50-49.jpg");
192
NEW_ROW("PICT0001.JPG", "20091024T225049", "{name}.{ext}", "PICT0001.JPG");
193
NEW_ROW("PICT0001.JPG", "20091024T225049", "{name.lower}.{ext.lower}", "pict0001.jpg");
194
NEW_ROW("iLikeCurlies", "20091024T225049", "{{{name}}", "{iLikeCurlies}");
195
NEW_ROW("UnknownKeyword", "20091024T225049", "foo{unknown}bar", "foobar");
196
NEW_ROW("MissingClosingCurly", "20091024T225049", "foo{date", "foo");
199
void ImporterTest::testAutoRenameFormat() {
200
QStringList dates = QStringList()
201
<< "1979-02-23_10-20-00"
202
<< "2006-04-01_11-30-15"
203
<< "2009-10-01_21-15-27";
204
QCOMPARE(dates.count(), mDocumentList.count());
206
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "foo");
208
Importer importer(0);
209
importer.setAutoRenameFormat("{date}_{time}.{ext}");
210
KUrl::List list = mDocumentList;
213
connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
214
importer.start(list, destUrl);
217
QCOMPARE(importer.importedUrlList().count(), list.count());
218
QCOMPARE(importer.importedUrlList(), list);
220
for (int pos=0; pos < dates.count(); ++pos) {
221
KUrl src = list[pos];
223
dst.addPath(dates[pos] + ".jpg");
224
QVERIFY(FileUtils::contentsAreIdentical(src, dst));
228
void ImporterTest::testReadOnlyDestination() {
229
KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
230
chmod(QFile::encodeName(mTempDir->name()), 0555);
232
Importer importer(0);
233
QSignalSpy errorSpy(&importer, SIGNAL(error(const QString&)));
234
importer.start(mDocumentList, destUrl);
236
QCOMPARE(errorSpy.count(), 1);
237
QVERIFY(importer.importedUrlList().isEmpty());