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

« back to all changes in this revision

Viewing changes to tests/auto/importertest.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 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
{
 
45
    mDocumentList = KUrl::List()
 
46
                    << urlForTestFile("import/pict0001.jpg")
 
47
                    << urlForTestFile("import/pict0002.jpg")
 
48
                    << urlForTestFile("import/pict0003.jpg")
 
49
                    ;
 
50
 
 
51
    mTempDir.reset(new KTempDir());
 
52
}
 
53
 
 
54
void ImporterTest::testContentsAreIdentical()
 
55
{
 
56
    QVERIFY(!FileUtils::contentsAreIdentical(mDocumentList[0], mDocumentList[1]));
 
57
    QVERIFY(FileUtils::contentsAreIdentical(mDocumentList[0], mDocumentList[0]));
 
58
 
 
59
    KUrl url1 = mDocumentList[0];
 
60
    KUrl url2 = urlForTestOutputFile("foo");
 
61
 
 
62
    // Test on a copy of a file
 
63
    QFile::remove(url2.toLocalFile());
 
64
    QFile::copy(url1.toLocalFile(), url2.toLocalFile());
 
65
 
 
66
    QVERIFY(FileUtils::contentsAreIdentical(url1, url2));
 
67
 
 
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();
 
72
    file.close();
 
73
    data[data.size() / 2] = 255 - data[data.size() / 2];
 
74
 
 
75
    file.open(QIODevice::WriteOnly);
 
76
    file.write(data);
 
77
    file.close();
 
78
 
 
79
    QVERIFY(!FileUtils::contentsAreIdentical(url1, url2));
 
80
}
 
81
 
 
82
void ImporterTest::testSuccessfulImport()
 
83
{
 
84
    KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
 
85
 
 
86
    Importer importer(0);
 
87
    QSignalSpy maximumChangedSpy(&importer, SIGNAL(maximumChanged(int)));
 
88
    QSignalSpy errorSpy(&importer, SIGNAL(error(QString)));
 
89
 
 
90
    KUrl::List list = mDocumentList;
 
91
 
 
92
    QEventLoop loop;
 
93
    connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
 
94
    importer.start(list, destUrl);
 
95
    loop.exec();
 
96
 
 
97
    QCOMPARE(maximumChangedSpy.count(), 1);
 
98
    QCOMPARE(maximumChangedSpy.takeFirst().at(0).toInt(), list.count() * 100);
 
99
    QCOMPARE(errorSpy.count(), 0);
 
100
 
 
101
    QCOMPARE(importer.importedUrlList().count(), list.count());
 
102
    QCOMPARE(importer.importedUrlList(), list);
 
103
    QCOMPARE(importer.skippedUrlList().count(), 0);
 
104
    QCOMPARE(importer.renamedCount(), 0);
 
105
 
 
106
    Q_FOREACH(const KUrl & src, list) {
 
107
        KUrl dst = destUrl;
 
108
        dst.addPath(src.fileName());
 
109
        QVERIFY(FileUtils::contentsAreIdentical(src, dst));
 
110
    }
 
111
}
 
112
 
 
113
void ImporterTest::testSkippedUrlList()
 
114
{
 
115
    KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
 
116
 
 
117
    Importer importer(0);
 
118
 
 
119
    KUrl::List list = mDocumentList.mid(0, 1);
 
120
 
 
121
    QEventLoop loop;
 
122
    connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
 
123
    importer.start(list, destUrl);
 
124
    loop.exec();
 
125
 
 
126
    QCOMPARE(importer.importedUrlList().count(), 1);
 
127
    QCOMPARE(importer.importedUrlList(), list);
 
128
 
 
129
    list = mDocumentList;
 
130
    KUrl::List expectedImportedList = mDocumentList.mid(1);
 
131
    KUrl::List expectedSkippedList = mDocumentList.mid(0, 1);
 
132
    importer.start(list, destUrl);
 
133
    loop.exec();
 
134
 
 
135
    QCOMPARE(importer.importedUrlList().count(), 2);
 
136
    QCOMPARE(importer.importedUrlList(), expectedImportedList);
 
137
    QCOMPARE(importer.skippedUrlList(), expectedSkippedList);
 
138
    QCOMPARE(importer.renamedCount(), 0);
 
139
}
 
140
 
 
141
void ImporterTest::testRenamedCount()
 
142
{
 
143
    KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
 
144
 
 
145
    Importer importer(0);
 
146
 
 
147
    KUrl::List list;
 
148
    list << mDocumentList.first();
 
149
 
 
150
    QEventLoop loop;
 
151
    connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
 
152
    importer.start(list, destUrl);
 
153
    loop.exec();
 
154
 
 
155
    QCOMPARE(importer.importedUrlList().count(), 1);
 
156
    QCOMPARE(importer.importedUrlList(), list);
 
157
 
 
158
    // Modify imported document so that next import does not skip it
 
159
    {
 
160
        KUrl url = destUrl;
 
161
        url.addPath(mDocumentList.first().fileName());
 
162
        QFile file(url.toLocalFile());
 
163
        QVERIFY(file.open(QIODevice::Append));
 
164
        file.write("foo");
 
165
    }
 
166
 
 
167
    list = mDocumentList;
 
168
    importer.start(list, destUrl);
 
169
    loop.exec();
 
170
 
 
171
    QCOMPARE(importer.importedUrlList().count(), 3);
 
172
    QCOMPARE(importer.importedUrlList(), mDocumentList);
 
173
    QCOMPARE(importer.skippedUrlList().count(), 0);
 
174
    QCOMPARE(importer.renamedCount(), 1);
 
175
}
 
176
 
 
177
void ImporterTest::testFileNameFormater()
 
178
{
 
179
    QFETCH(QString, fileName);
 
180
    QFETCH(QString, dateTime);
 
181
    QFETCH(QString, format);
 
182
    QFETCH(QString, expected);
 
183
 
 
184
    KUrl url = KUrl("file://foo/bar/" + fileName);
 
185
    FileNameFormater fileNameFormater(format);
 
186
    QCOMPARE(fileNameFormater.format(url, KDateTime::fromString(dateTime)), expected);
 
187
}
 
188
 
 
189
#define NEW_ROW(fileName, dateTime, format, expected) QTest::newRow(fileName) << fileName << dateTime << format << expected
 
190
void ImporterTest::testFileNameFormater_data()
 
191
{
 
192
    QTest::addColumn<QString>("fileName");
 
193
    QTest::addColumn<QString>("dateTime");
 
194
    QTest::addColumn<QString>("format");
 
195
    QTest::addColumn<QString>("expected");
 
196
 
 
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");
 
204
}
 
205
 
 
206
void ImporterTest::testAutoRenameFormat()
 
207
{
 
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());
 
213
 
 
214
    KUrl destUrl = KUrl::fromPath(mTempDir->name() + "foo");
 
215
 
 
216
    Importer importer(0);
 
217
    importer.setAutoRenameFormat("{date}_{time}.{ext}");
 
218
    KUrl::List list = mDocumentList;
 
219
 
 
220
    QEventLoop loop;
 
221
    connect(&importer, SIGNAL(importFinished()), &loop, SLOT(quit()));
 
222
    importer.start(list, destUrl);
 
223
    loop.exec();
 
224
 
 
225
    QCOMPARE(importer.importedUrlList().count(), list.count());
 
226
    QCOMPARE(importer.importedUrlList(), list);
 
227
 
 
228
    for (int pos = 0; pos < dates.count(); ++pos) {
 
229
        KUrl src = list[pos];
 
230
        KUrl dst = destUrl;
 
231
        dst.addPath(dates[pos] + ".jpg");
 
232
        QVERIFY(FileUtils::contentsAreIdentical(src, dst));
 
233
    }
 
234
}
 
235
 
 
236
void ImporterTest::testReadOnlyDestination()
 
237
{
 
238
    KUrl destUrl = KUrl::fromPath(mTempDir->name() + "/foo");
 
239
    chmod(QFile::encodeName(mTempDir->name()), 0555);
 
240
 
 
241
    Importer importer(0);
 
242
    QSignalSpy errorSpy(&importer, SIGNAL(error(QString)));
 
243
    importer.start(mDocumentList, destUrl);
 
244
 
 
245
    QCOMPARE(errorSpy.count(), 1);
 
246
    QVERIFY(importer.importedUrlList().isEmpty());
 
247
}