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

« back to all changes in this revision

Viewing changes to tests/importertest.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 2009 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 "importertest.moc"
 
21
 
 
22
// stdlib
 
23
#include <sys/stat.h>
 
24
 
 
25
// Qt
 
26
#include <QSignalSpy>
 
27
 
 
28
// KDE
 
29
#include <kdatetime.h>
 
30
#include <kdebug.h>
 
31
#include <qtest_kde.h>
 
32
 
 
33
// Local
 
34
#include "../importer/fileutils.h"
 
35
#include "../importer/importer.h"
 
36
#include "../importer/filenameformater.h"
 
37
#include "testutils.h"
 
38
 
 
39
QTEST_KDEMAIN(ImporterTest, GUI)
 
40
 
 
41
using namespace Gwenview;
 
42
 
 
43
void ImporterTest::init() {
 
44
        mDocumentList = KUrl::List()
 
45
                << urlForTestFile("import/pict0001.jpg")
 
46
                << urlForTestFile("import/pict0002.jpg")
 
47
                << urlForTestFile("import/pict0003.jpg")
 
48
                ;
 
49
 
 
50
        mTempDir.reset(new KTempDir());
 
51
}
 
52
 
 
53
void ImporterTest::testContentsAreIdentical() {
 
54
        QVERIFY(!FileUtils::contentsAreIdentical(mDocumentList[0], mDocumentList[1]));
 
55
        QVERIFY(FileUtils::contentsAreIdentical(mDocumentList[0], mDocumentList[0]));
 
56
 
 
57
        KUrl url1 = mDocumentList[0];
 
58
        KUrl url2 = urlForTestOutputFile("foo");
 
59
 
 
60
        // Test on a copy of a file
 
61
        QFile::remove(url2.toLocalFile());
 
62
        QFile::copy(url1.toLocalFile(), url2.toLocalFile());
 
63
 
 
64
        QVERIFY(FileUtils::contentsAreIdentical(url1, url2));
 
65
 
 
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();
 
70
        file.close();
 
71
        data[data.size() / 2] = 255 - data[data.size() / 2];
 
72
 
 
73
        file.open(QIODevice::WriteOnly);
 
74
        file.write(data);
 
75
        file.close();
 
76
 
 
77
        QVERIFY(!FileUtils::contentsAreIdentical(url1, url2));
 
78
}
 
79
 
 
80
void ImporterTest::testSuccessfulImport() {
 
81
        KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
 
82
 
 
83
        Importer importer(0);
 
84
        QSignalSpy maximumChangedSpy(&importer, SIGNAL(maximumChanged(int)));
 
85
        QSignalSpy errorSpy(&importer, SIGNAL(error(const QString&)));
 
86
 
 
87
        KUrl::List list = mDocumentList;
 
88
 
 
89
        QEventLoop loop;
 
90
        connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
 
91
        importer.start(list, destUrl);
 
92
        loop.exec();
 
93
 
 
94
        QCOMPARE(maximumChangedSpy.count(), 1);
 
95
        QCOMPARE(maximumChangedSpy.takeFirst().at(0).toInt(), list.count() * 100);
 
96
        QCOMPARE(errorSpy.count(), 0);
 
97
 
 
98
        QCOMPARE(importer.importedUrlList().count(), list.count());
 
99
        QCOMPARE(importer.importedUrlList(), list);
 
100
        QCOMPARE(importer.skippedUrlList().count(), 0);
 
101
        QCOMPARE(importer.renamedCount(), 0);
 
102
 
 
103
        Q_FOREACH(const KUrl& src, list) {
 
104
                KUrl dst = destUrl;
 
105
                dst.addPath(src.fileName());
 
106
                QVERIFY(FileUtils::contentsAreIdentical(src, dst));
 
107
        }
 
108
}
 
109
 
 
110
void ImporterTest::testSkippedUrlList() {
 
111
        KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
 
112
 
 
113
        Importer importer(0);
 
114
 
 
115
        KUrl::List list = mDocumentList.mid(0, 1);
 
116
 
 
117
        QEventLoop loop;
 
118
        connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
 
119
        importer.start(list, destUrl);
 
120
        loop.exec();
 
121
 
 
122
        QCOMPARE(importer.importedUrlList().count(), 1);
 
123
        QCOMPARE(importer.importedUrlList(), list);
 
124
 
 
125
        list = mDocumentList;
 
126
        KUrl::List expectedImportedList = mDocumentList.mid(1);
 
127
        KUrl::List expectedSkippedList = mDocumentList.mid(0, 1);
 
128
        importer.start(list, destUrl);
 
129
        loop.exec();
 
130
 
 
131
        QCOMPARE(importer.importedUrlList().count(), 2);
 
132
        QCOMPARE(importer.importedUrlList(), expectedImportedList);
 
133
        QCOMPARE(importer.skippedUrlList(), expectedSkippedList);
 
134
        QCOMPARE(importer.renamedCount(), 0);
 
135
}
 
136
 
 
137
void ImporterTest::testRenamedCount() {
 
138
        KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
 
139
 
 
140
        Importer importer(0);
 
141
 
 
142
        KUrl::List list;
 
143
        list << mDocumentList.first();
 
144
 
 
145
        QEventLoop loop;
 
146
        connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
 
147
        importer.start(list, destUrl);
 
148
        loop.exec();
 
149
 
 
150
        QCOMPARE(importer.importedUrlList().count(), 1);
 
151
        QCOMPARE(importer.importedUrlList(), list);
 
152
 
 
153
        // Modify imported document so that next import does not skip it
 
154
        {
 
155
                KUrl url = destUrl;
 
156
                url.addPath(mDocumentList.first().fileName());
 
157
                QFile file(url.toLocalFile());
 
158
                QVERIFY(file.open(QIODevice::Append));
 
159
                file.write("foo");
 
160
        }
 
161
 
 
162
        list = mDocumentList;
 
163
        importer.start(list, destUrl);
 
164
        loop.exec();
 
165
 
 
166
        QCOMPARE(importer.importedUrlList().count(), 3);
 
167
        QCOMPARE(importer.importedUrlList(), mDocumentList);
 
168
        QCOMPARE(importer.skippedUrlList().count(), 0);
 
169
        QCOMPARE(importer.renamedCount(), 1);
 
170
}
 
171
 
 
172
void ImporterTest::testFileNameFormater() {
 
173
        QFETCH(QString, fileName);
 
174
        QFETCH(QString, dateTime);
 
175
        QFETCH(QString, format);
 
176
        QFETCH(QString, expected);
 
177
 
 
178
        KUrl url = KUrl("file://foo/bar/" + fileName);
 
179
        FileNameFormater fileNameFormater(format);
 
180
        QCOMPARE(fileNameFormater.format(url, KDateTime::fromString(dateTime)), expected);
 
181
}
 
182
 
 
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");
 
189
 
 
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");
 
197
}
 
198
 
 
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());
 
205
 
 
206
        KUrl destUrl = KUrl::fromPath(mTempDir->name() + "foo");
 
207
 
 
208
        Importer importer(0);
 
209
        importer.setAutoRenameFormat("{date}_{time}.{ext}");
 
210
        KUrl::List list = mDocumentList;
 
211
 
 
212
        QEventLoop loop;
 
213
        connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
 
214
        importer.start(list, destUrl);
 
215
        loop.exec();
 
216
 
 
217
        QCOMPARE(importer.importedUrlList().count(), list.count());
 
218
        QCOMPARE(importer.importedUrlList(), list);
 
219
 
 
220
        for (int pos=0; pos < dates.count(); ++pos) {
 
221
                KUrl src = list[pos];
 
222
                KUrl dst = destUrl;
 
223
                dst.addPath(dates[pos] + ".jpg");
 
224
                QVERIFY(FileUtils::contentsAreIdentical(src, dst));
 
225
        }
 
226
}
 
227
 
 
228
void ImporterTest::testReadOnlyDestination() {
 
229
        KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
 
230
        chmod(QFile::encodeName(mTempDir->name()), 0555);
 
231
 
 
232
        Importer importer(0);
 
233
        QSignalSpy errorSpy(&importer, SIGNAL(error(const QString&)));
 
234
        importer.start(mDocumentList, destUrl);
 
235
 
 
236
        QCOMPARE(errorSpy.count(), 1);
 
237
        QVERIFY(importer.importedUrlList().isEmpty());
 
238
}