File: imagejobtest.cpp

package info (click to toggle)
tellico 3.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 23,468 kB
  • sloc: cpp: 73,731; xml: 8,383; ansic: 7,003; javascript: 3,059; python: 603; perl: 63; ada: 32; sh: 28; makefile: 22
file content (360 lines) | stat: -rw-r--r-- 12,376 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/***************************************************************************
    Copyright (C) 2017 Robby Stephenson <robby@periapsis.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as        *
 *   published by the Free Software Foundation; either version 2 of        *
 *   the License or (at your option) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *                                                                         *
 ***************************************************************************/

#include "imagejobtest.h"

#include "../images/imagejob.h"
#include "../images/imagefactory.h"
#include "../images/imageinfo.h"

#include <QTest>
#include <QEventLoop>
#include <QTemporaryFile>
#include <QNetworkInterface>
#include <QSignalSpy>

QTEST_GUILESS_MAIN( ImageJobTest )

static bool hasNetwork() {
#if 0
  foreach(const QNetworkInterface& net, QNetworkInterface::allInterfaces()) {
    if(net.flags().testFlag(QNetworkInterface::IsUp) && !net.flags().testFlag(QNetworkInterface::IsLoopBack)) {
      return true;
    }
  }
#endif
  return false;
}

void ImageJobTest::initTestCase() {
  Tellico::ImageFactory::init();
}

void ImageJobTest::cleanupTestCase() {
}

void ImageJobTest::init() {
  m_result = -1;
  m_imageId.clear();
}

void ImageJobTest::enterLoop() {
  QEventLoop eventLoop;
  connect(this, &ImageJobTest::exitLoop, &eventLoop, &QEventLoop::quit);
  eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
}

void ImageJobTest::slotGetResult(KJob* job) {
  m_result = job->error();
  emit exitLoop();
}

void ImageJobTest::slotAvailable(const QString& id_) {
  m_result = 0;
  m_imageId = id_;
  emit exitLoop();
}

void ImageJobTest::testInvalidUrl() {
  QUrl u;

  Tellico::ImageJob* job = new Tellico::ImageJob(u);
  connect(job, &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  QCOMPARE(m_result, int(KIO::ERR_MALFORMED_URL));
}

void ImageJobTest::testNonexistant() {
  QUrl u(QStringLiteral("file:///non-existent-location"));

  Tellico::ImageJob* job = new Tellico::ImageJob(u);
  connect(job, &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  QCOMPARE(m_result, int(KIO::ERR_CANNOT_OPEN_FOR_READING));
}

void ImageJobTest::testUnreadable() {
  QTemporaryFile tmpFile;
  QVERIFY(tmpFile.open());
  QVERIFY(tmpFile.setPermissions(QFileDevice::Permissions()));
  QUrl u = QUrl::fromLocalFile(tmpFile.fileName());

  Tellico::ImageJob* job = new Tellico::ImageJob(u);
  connect(job, &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  QCOMPARE(m_result, int(KIO::ERR_CANNOT_OPEN_FOR_READING));

  const Tellico::Data::Image& img = job->image();
  QVERIFY(img.isNull());
}

void ImageJobTest::testImageInvalid() {
  // text file is an invalid image
  QUrl u = QUrl::fromLocalFile(QFINDTESTDATA("imagejobtest.cpp"));

  QPointer<Tellico::ImageJob> job = new Tellico::ImageJob(u);
  connect(job.data(), &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  QCOMPARE(m_result, int(KIO::ERR_UNKNOWN));

  const Tellico::Data::Image& img = job->image();
  QVERIFY(img.isNull());
  QCOMPARE(img.linkOnly(), false);
}

void ImageJobTest::testImageLoad() {
  QUrl u = QUrl::fromLocalFile(QFINDTESTDATA("../../icons/tellico.png"));

  QPointer<Tellico::ImageJob> job = new Tellico::ImageJob(u);
  connect(job.data(), &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  // success!
  QCOMPARE(m_result, 0);

  const Tellico::Data::Image& img = job->image();
  QVERIFY(!img.isNull());
  QCOMPARE(img.id(), QStringLiteral("dde5bf2cbd90fad8635a26dfb362e0ff.png"));
  QCOMPARE(img.format(), QByteArray("png"));
  QCOMPARE(img.linkOnly(), false);

  // check that the job is automatically deleted
  qApp->processEvents();
  QVERIFY(!job);
}

void ImageJobTest::testImageLoadWithId() {
  QUrl u = QUrl::fromLocalFile(QFINDTESTDATA("../../icons/tellico.png"));

  QPointer<Tellico::ImageJob> job = new Tellico::ImageJob(u, QStringLiteral("tellico-rocks"));
  connect(job.data(), &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  // success!
  QCOMPARE(m_result, 0);

  const Tellico::Data::Image& img = job->image();
  QVERIFY(!img.isNull());
  QCOMPARE(img.id(), QStringLiteral("tellico-rocks"));
  QCOMPARE(img.format(), QByteArray("png"));
  QCOMPARE(img.linkOnly(), false);
}

void ImageJobTest::testImageLink() {
  QUrl u = QUrl::fromLocalFile(QFINDTESTDATA("../../icons/tellico.png"));

  QPointer<Tellico::ImageJob> job = new Tellico::ImageJob(u,
                                                          QString() /* id */,
                                                          false /* quiet */);
  job->setLinkOnly(true);
  connect(job.data(), &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  // success!
  QCOMPARE(m_result, 0);

  const Tellico::Data::Image& img = job->image();
  QVERIFY(!img.isNull());
  // id is not the MD5 hash
  QVERIFY(img.id() != QStringLiteral("dde5bf2cbd90fad8635a26dfb362e0ff.png"));
  QCOMPARE(img.format(), QByteArray("png"));
  QCOMPARE(img.linkOnly(), true);
}

void ImageJobTest::testNetworkImage() {
  if(!hasNetwork()) QSKIP("This test requires network access", SkipSingle);

  QUrl u(QStringLiteral("https://tellico-project.org/wp-content/uploads/96-tellico.png"));

  QPointer<Tellico::ImageJob> job = new Tellico::ImageJob(u);
  connect(job.data(), &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  // success!
  QCOMPARE(m_result, 0);

  const Tellico::Data::Image& img = job->image();
  QVERIFY(!img.isNull());
  QCOMPARE(img.id(), QStringLiteral("ecaf5185c4016881aaabb4933211d5d6.png"));
  QCOMPARE(img.format(), QByteArray("png"));
  QCOMPARE(img.linkOnly(), false);

  // check that the job is automatically deleted
  qApp->processEvents();
  QVERIFY(!job);
}

void ImageJobTest::testNetworkImageLink() {
  if(!hasNetwork()) QSKIP("This test requires network access", SkipSingle);

  QUrl u(QStringLiteral("https://tellico-project.org/wp-content/uploads/96-tellico.png"));

  QPointer<Tellico::ImageJob> job = new Tellico::ImageJob(u,
                                                          QString() /* id */,
                                                          false /* quiet */);
  job->setLinkOnly(true);
  connect(job.data(), &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  // success!
  QCOMPARE(m_result, 0);

  const Tellico::Data::Image& img = job->image();
  QVERIFY(!img.isNull());
  QCOMPARE(img.id(), u.url());
  QCOMPARE(img.format(), QByteArray("png"));
  QCOMPARE(img.linkOnly(), true);
}

void ImageJobTest::testNetworkImageInvalid() {
  if(!hasNetwork()) QSKIP("This test requires network access", SkipSingle);

  QUrl u(QStringLiteral("https://tellico-project.org"));

  QPointer<Tellico::ImageJob> job = new Tellico::ImageJob(u);
  connect(job.data(), &KJob::result,
          this, &ImageJobTest::slotGetResult);

  enterLoop();
  QCOMPARE(m_result, int(KIO::ERR_UNKNOWN));

  const Tellico::Data::Image& img = job->image();
  QVERIFY(img.isNull());
}

void ImageJobTest::testFactoryRequestLocal() {
  QVERIFY(m_imageId.isEmpty());
  connect(Tellico::ImageFactory::self(), &Tellico::ImageFactory::imageAvailable,
          this, &ImageJobTest::slotAvailable);

  QUrl u = QUrl::fromLocalFile(QFINDTESTDATA("../../icons/tellico.png"));
  Tellico::ImageFactory::requestImageById(u.url());

  // don't need to enter loop since the image is local and signal fires immediately
  QVERIFY(!m_imageId.isEmpty());
  // success!
  QCOMPARE(m_result, 0);

  const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(m_imageId);
  QVERIFY(!img.isNull());
  // id is not the MD5 hash
  QVERIFY(img.id() != QStringLiteral("dde5bf2cbd90fad8635a26dfb362e0ff.png"));
  QCOMPARE(img.format(), QByteArray("png"));
  QCOMPARE(img.linkOnly(), true);
}

void ImageJobTest::testFactoryRequestLocalInvalid() {
  QVERIFY(m_imageId.isEmpty());
  QSignalSpy spy(Tellico::ImageFactory::self(), &Tellico::ImageFactory::imageAvailable);
  connect(Tellico::ImageFactory::self(), &Tellico::ImageFactory::imageAvailable,
          this, &ImageJobTest::slotAvailable);

  // text file is an invalid image
  QUrl u = QUrl::fromLocalFile(QFINDTESTDATA("imagejobtest.cpp"));
  Tellico::ImageFactory::requestImageById(u.url());

  // it will be a null image, but a local url, so image is still loaded with immediate signal
  QCOMPARE(spy.count(), 1);
  // the available image id is the url
  QCOMPARE(m_imageId, u.url());

  // now try to load it
  const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(m_imageId);
  QVERIFY(img.isNull());
  QCOMPARE(img.linkOnly(), false);
  // make sure the null image list is updated
  QVERIFY(Tellico::ImageFactory::self()->hasNullImage(m_imageId));
  // the image should not be in local memory now
  QVERIFY(!Tellico::ImageFactory::self()->hasImageInMemory(m_imageId));
  QVERIFY(!Tellico::ImageFactory::self()->hasImageInfo(m_imageId));
}

void ImageJobTest::testFactoryRequestNetwork() {
  if(!hasNetwork()) QSKIP("This test requires network access", SkipSingle);

  QVERIFY(m_imageId.isEmpty());
  connect(Tellico::ImageFactory::self(), &Tellico::ImageFactory::imageAvailable,
          this, &ImageJobTest::slotAvailable);

  QUrl u(QStringLiteral("https://tellico-project.org/wp-content/uploads/96-tellico.png"));
  Tellico::ImageFactory::requestImageById(u.url());

  enterLoop();
  QVERIFY(!m_imageId.isEmpty());
  // success!
  QCOMPARE(m_result, 0);
  // the image should be in local memory now
  QVERIFY(Tellico::ImageFactory::self()->hasImageInMemory(m_imageId));
  QVERIFY(Tellico::ImageFactory::self()->hasImageInfo(m_imageId));

  const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(m_imageId);
  QVERIFY(!img.isNull());
  // id is the MD5 hash, since it's not link only
  QCOMPARE(img.id(), QStringLiteral("ecaf5185c4016881aaabb4933211d5d6.png"));
  QCOMPARE(img.format(), QByteArray("png"));
  QCOMPARE(img.linkOnly(), false);
}

void ImageJobTest::testFactoryRequestNetworkLinkOnly() {
  if(!hasNetwork()) QSKIP("This test requires network access", SkipSingle);

  QVERIFY(m_imageId.isEmpty());
  connect(Tellico::ImageFactory::self(), &Tellico::ImageFactory::imageAvailable,
          this, &ImageJobTest::slotAvailable);

  QUrl u(QStringLiteral("https://tellico-project.org/wp-content/uploads/96-tellico.png"));
  // first, tell the image factory that the image is link only
  Tellico::Data::ImageInfo info(u.url(), "PNG", 64, 64, true /* link only */);
  Tellico::ImageFactory::cacheImageInfo(info);

  Tellico::ImageFactory::requestImageById(u.url());

  enterLoop();
  QVERIFY(!m_imageId.isEmpty());
  // success!
  QCOMPARE(m_result, 0);
  // the image should be in local memory now
  QVERIFY(Tellico::ImageFactory::self()->hasImageInMemory(m_imageId));
  QVERIFY(Tellico::ImageFactory::self()->hasImageInfo(m_imageId));

  const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(m_imageId);
  QVERIFY(!img.isNull());
  // id is not the MD5 hash
  QCOMPARE(img.id(), u.url());
  QCOMPARE(img.format(), QByteArray("png"));
  QCOMPARE(img.linkOnly(), true);
}