1/****************************************************************************
2**
3** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtDocGallery module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42//TESTED_COMPONENT=src/gallery
43
44#include <qgalleryabstractresponse.h>
45
46#include <QtTest/QtTest>
47
48QT_USE_DOCGALLERY_NAMESPACE
49
50class tst_QGalleryAbstractResponse : public QObject
51{
52 Q_OBJECT
53
54private Q_SLOTS:
55 void finish();
56 void cancel();
57 void cancelIdle();
58 void finishIdle();
59 void errorActive();
60 void errorIdle();
61 void errorFinished();
62 void errorCanceled();
63 void immediateError();
64 void waitForFinishedImmediate();
65 void waitForFinishedTimeout();
66 void waitForFinishedNoTimeout();
67 void waitForCanceledImmediate();
68 void waitForCanceledNoTimeout();
69 void waitForErrorImmediate();
70 void waitForErrorNoTimeout();
71 void progressChangedSignal();
72};
73
74class QtGalleryTestResponse : public QGalleryAbstractResponse
75{
76 Q_OBJECT
77public:
78 using QGalleryAbstractResponse::error;
79 using QGalleryAbstractResponse::finish;
80 using QGalleryAbstractResponse::resume;
81 using QGalleryAbstractResponse::progressChanged;
82
83public Q_SLOTS:
84 void doFinish() { finish(); }
85 void doCancel() { cancel(); }
86 void doError(int error) { QGalleryAbstractResponse::error(error); }
87};
88
89void tst_QGalleryAbstractResponse::finish()
90{
91 QtGalleryTestResponse response;
92
93 QSignalSpy finishedSpy(&response, SIGNAL(finished()));
94 QSignalSpy canceledSpy(&response, SIGNAL(canceled()));
95 QSignalSpy resumedSpy(&response, SIGNAL(resumed()));
96
97 QCOMPARE(response.isActive(), true);
98 QCOMPARE(response.isIdle(), false);
99
100 // The first call to finish with a valid result emits finished.
101 response.finish(idle: false);
102 QCOMPARE(response.isActive(), false);
103 QCOMPARE(response.isIdle(), false);
104 QCOMPARE(finishedSpy.count(), 1);
105 QCOMPARE(canceledSpy.count(), 0);
106 QCOMPARE(resumedSpy.count(), 0);
107
108 // Subsequent calls to finish do nothing.
109 response.finish(idle: false);
110 QCOMPARE(response.isActive(), false);
111 QCOMPARE(response.isIdle(), false);
112 QCOMPARE(finishedSpy.count(), 1);
113 QCOMPARE(canceledSpy.count(), 0);
114 QCOMPARE(resumedSpy.count(), 0);
115
116 response.finish(idle: true);
117 QCOMPARE(response.isActive(), false);
118 QCOMPARE(response.isIdle(), false);
119 QCOMPARE(finishedSpy.count(), 1);
120 QCOMPARE(canceledSpy.count(), 0);
121 QCOMPARE(resumedSpy.count(), 0);
122
123 // Subsequent calls to cancel do nothing.
124 response.cancel();
125 QCOMPARE(response.isActive(), false);
126 QCOMPARE(response.isIdle(), false);
127 QCOMPARE(finishedSpy.count(), 1);
128 QCOMPARE(canceledSpy.count(), 0);
129 QCOMPARE(resumedSpy.count(), 0);
130
131 // Subsequent calls to resume do nothing.
132 response.resume();
133 QCOMPARE(response.isActive(), false);
134 QCOMPARE(response.isIdle(), false);
135 QCOMPARE(finishedSpy.count(), 1);
136 QCOMPARE(canceledSpy.count(), 0);
137 QCOMPARE(resumedSpy.count(), 0);
138}
139
140void tst_QGalleryAbstractResponse::cancel()
141{
142 QtGalleryTestResponse response;
143
144 QSignalSpy finishedSpy(&response, SIGNAL(finished()));
145 QSignalSpy canceledSpy(&response, SIGNAL(canceled()));
146 QSignalSpy resumedSpy(&response, SIGNAL(resumed()));
147
148 QCOMPARE(response.isActive(), true);
149 QCOMPARE(response.isIdle(), false);
150
151 // Calling cancel on an unfinished will emit canceled, and set isActive false.
152 response.cancel();
153 QCOMPARE(response.isActive(), false);
154 QCOMPARE(response.isIdle(), false);
155 QCOMPARE(finishedSpy.count(), 0);
156 QCOMPARE(canceledSpy.count(), 1);
157 QCOMPARE(resumedSpy.count(), 0);
158
159 // Subsequent calls to finish do nothing.
160 response.finish(idle: false);
161 QCOMPARE(response.isActive(), false);
162 QCOMPARE(response.isIdle(), false);
163 QCOMPARE(finishedSpy.count(), 0);
164 QCOMPARE(canceledSpy.count(), 1);
165 QCOMPARE(resumedSpy.count(), 0);
166
167 response.finish(idle: true);
168 QCOMPARE(response.isActive(), false);
169 QCOMPARE(response.isIdle(), false);
170 QCOMPARE(finishedSpy.count(), 0);
171 QCOMPARE(canceledSpy.count(), 1);
172 QCOMPARE(resumedSpy.count(), 0);
173
174 // Subsequent call to cancel do nothing.
175 response.cancel();
176 QCOMPARE(response.isActive(), false);
177 QCOMPARE(response.isIdle(), false);
178 QCOMPARE(finishedSpy.count(), 0);
179 QCOMPARE(canceledSpy.count(), 1);
180 QCOMPARE(resumedSpy.count(), 0);
181
182 // Subsequent call to resume do nothing.
183 response.resume();
184 QCOMPARE(response.isActive(), false);
185 QCOMPARE(response.isIdle(), false);
186 QCOMPARE(finishedSpy.count(), 0);
187 QCOMPARE(canceledSpy.count(), 1);
188 QCOMPARE(resumedSpy.count(), 0);
189}
190
191void tst_QGalleryAbstractResponse::cancelIdle()
192{
193 QtGalleryTestResponse response;
194
195 QSignalSpy finishedSpy(&response, SIGNAL(finished()));
196 QSignalSpy canceledSpy(&response, SIGNAL(canceled()));
197 QSignalSpy resumedSpy(&response, SIGNAL(resumed()));
198
199 QCOMPARE(response.isActive(), true);
200 QCOMPARE(response.isIdle(), false);
201
202 // Calling resume while active does nothing.
203 response.resume();
204 QCOMPARE(response.isActive(), true);
205 QCOMPARE(response.isIdle(), false);
206 QCOMPARE(finishedSpy.count(), 0);
207 QCOMPARE(canceledSpy.count(), 0);
208 QCOMPARE(resumedSpy.count(), 0);
209
210 // The first call to finish with a valid result emits finished.
211 response.finish(idle: true);
212 QCOMPARE(response.isActive(), false);
213 QCOMPARE(response.isIdle(), true);
214 QCOMPARE(finishedSpy.count(), 1);
215 QCOMPARE(canceledSpy.count(), 0);
216 QCOMPARE(resumedSpy.count(), 0);
217
218 // Calling resume set isActive to true and emits resumed.
219 response.resume();
220 QCOMPARE(response.isActive(), true);
221 QCOMPARE(response.isIdle(), false);
222 QCOMPARE(finishedSpy.count(), 1);
223 QCOMPARE(canceledSpy.count(), 0);
224 QCOMPARE(resumedSpy.count(), 1);
225
226 // Calling resume while active does nothing.
227 response.resume();
228 QCOMPARE(response.isActive(), true);
229 QCOMPARE(response.isIdle(), false);
230 QCOMPARE(finishedSpy.count(), 1);
231 QCOMPARE(canceledSpy.count(), 0);
232 QCOMPARE(resumedSpy.count(), 1);
233
234 // Finishing again works as before.
235 response.finish(idle: true);
236 QCOMPARE(response.isActive(), false);
237 QCOMPARE(response.isIdle(), true);
238 QCOMPARE(finishedSpy.count(), 2);
239 QCOMPARE(canceledSpy.count(), 0);
240 QCOMPARE(resumedSpy.count(), 1);
241
242
243 // Canceling sets isIdle to false and emits canceled.
244 response.cancel();
245 QCOMPARE(response.isActive(), false);
246 QCOMPARE(response.isIdle(), false);
247 QCOMPARE(finishedSpy.count(), 2);
248 QCOMPARE(canceledSpy.count(), 1);
249 QCOMPARE(resumedSpy.count(), 1);
250}
251
252void tst_QGalleryAbstractResponse::finishIdle()
253{
254 QtGalleryTestResponse response;
255
256 QSignalSpy finishedSpy(&response, SIGNAL(finished()));
257 QSignalSpy canceledSpy(&response, SIGNAL(canceled()));
258 QSignalSpy resumedSpy(&response, SIGNAL(resumed()));
259
260 QCOMPARE(response.isActive(), true);
261 QCOMPARE(response.isIdle(), false);
262
263 // Calling resume while active does nothing.
264 response.resume();
265 QCOMPARE(response.isActive(), true);
266 QCOMPARE(response.isIdle(), false);
267 QCOMPARE(finishedSpy.count(), 0);
268 QCOMPARE(canceledSpy.count(), 0);
269 QCOMPARE(resumedSpy.count(), 0);
270
271 // The first call to finish with a valid result emits finished.
272 response.finish(idle: true);
273 QCOMPARE(response.isActive(), false);
274 QCOMPARE(response.isIdle(), true);
275 QCOMPARE(finishedSpy.count(), 1);
276 QCOMPARE(canceledSpy.count(), 0);
277 QCOMPARE(resumedSpy.count(), 0);
278
279 // Calling resume set isActive to true and emits resumed.
280 response.resume();
281 QCOMPARE(response.isActive(), true);
282 QCOMPARE(response.isIdle(), false);
283 QCOMPARE(finishedSpy.count(), 1);
284 QCOMPARE(canceledSpy.count(), 0);
285 QCOMPARE(resumedSpy.count(), 1);
286
287 // Calling resume while active does nothing.
288 response.resume();
289 QCOMPARE(response.isActive(), true);
290 QCOMPARE(response.isIdle(), false);
291 QCOMPARE(finishedSpy.count(), 1);
292 QCOMPARE(canceledSpy.count(), 0);
293 QCOMPARE(resumedSpy.count(), 1);
294
295 // Finishing again works as before.
296 response.finish(idle: true);
297 QCOMPARE(response.isActive(), false);
298 QCOMPARE(response.isIdle(), true);
299 QCOMPARE(finishedSpy.count(), 2);
300 QCOMPARE(canceledSpy.count(), 0);
301 QCOMPARE(resumedSpy.count(), 1);
302
303 // Finishing with idle = true does nothing while idle.
304 response.finish(idle: true);
305 QCOMPARE(response.isActive(), false);
306 QCOMPARE(response.isIdle(), true);
307 QCOMPARE(finishedSpy.count(), 2);
308 QCOMPARE(canceledSpy.count(), 0);
309 QCOMPARE(resumedSpy.count(), 1);
310
311 // Finishing with idle = false sets isIdle to false and emits finished.
312 response.finish(idle: false);
313 QCOMPARE(response.isActive(), false);
314 QCOMPARE(response.isIdle(), false);
315 QCOMPARE(finishedSpy.count(), 3);
316 QCOMPARE(canceledSpy.count(), 0);
317 QCOMPARE(resumedSpy.count(), 1);
318}
319
320void tst_QGalleryAbstractResponse::errorActive()
321{
322 QtGalleryTestResponse response;
323
324 QSignalSpy finishedSpy(&response, SIGNAL(finished()));
325 QSignalSpy canceledSpy(&response, SIGNAL(canceled()));
326 QSignalSpy resumedSpy(&response, SIGNAL(resumed()));
327
328 QCOMPARE(response.isActive(), true);
329 QCOMPARE(response.isIdle(), false);
330 QCOMPARE(response.error(), int(QGalleryAbstractRequest::NoError));
331 QCOMPARE(response.errorString(), QString());
332
333 response.error(error: 300, errorString: QLatin1String("error"));
334
335 QCOMPARE(response.error(), 300);
336 QCOMPARE(response.errorString(), QLatin1String("error"));
337 QCOMPARE(response.isActive(), false);
338 QCOMPARE(response.isIdle(), false);
339 QCOMPARE(finishedSpy.count(), 1);
340 QCOMPARE(canceledSpy.count(), 0);
341 QCOMPARE(resumedSpy.count(), 0);
342}
343
344void tst_QGalleryAbstractResponse::errorIdle()
345{
346 QtGalleryTestResponse response;
347 response.finish(idle: true);
348
349 QSignalSpy finishedSpy(&response, SIGNAL(finished()));
350 QSignalSpy canceledSpy(&response, SIGNAL(canceled()));
351 QSignalSpy resumedSpy(&response, SIGNAL(resumed()));
352
353 QCOMPARE(response.isActive(), false);
354 QCOMPARE(response.isIdle(), true);
355 QCOMPARE(response.error(), int(QGalleryAbstractRequest::NoError));
356 QCOMPARE(response.errorString(), QString());
357
358 response.error(error: 300, errorString: QLatin1String("error"));
359
360 QCOMPARE(response.isActive(), false);
361 QCOMPARE(response.isIdle(), false);
362 QCOMPARE(response.error(), 300);
363 QCOMPARE(response.errorString(), QLatin1String("error"));
364 QCOMPARE(finishedSpy.count(), 1);
365 QCOMPARE(canceledSpy.count(), 0);
366 QCOMPARE(resumedSpy.count(), 0);
367}
368
369void tst_QGalleryAbstractResponse::errorFinished()
370{
371 QtGalleryTestResponse response;
372 response.finish(idle: false);
373
374 QSignalSpy finishedSpy(&response, SIGNAL(finished()));
375 QSignalSpy canceledSpy(&response, SIGNAL(canceled()));
376 QSignalSpy resumedSpy(&response, SIGNAL(resumed()));
377
378 QCOMPARE(response.isActive(), false);
379 QCOMPARE(response.isIdle(), false);
380 QCOMPARE(response.error(), int(QGalleryAbstractRequest::NoError));
381 QCOMPARE(response.errorString(), QString());
382
383 response.error(error: 300, errorString: QLatin1String("error"));
384
385 QCOMPARE(response.isActive(), false);
386 QCOMPARE(response.isIdle(), false);
387 QCOMPARE(response.error(), int(QGalleryAbstractRequest::NoError));
388 QCOMPARE(response.errorString(), QString());
389 QCOMPARE(finishedSpy.count(), 0);
390 QCOMPARE(canceledSpy.count(), 0);
391 QCOMPARE(resumedSpy.count(), 0);
392}
393
394void tst_QGalleryAbstractResponse::errorCanceled()
395{
396 QtGalleryTestResponse response;
397 response.cancel();
398
399 QSignalSpy finishedSpy(&response, SIGNAL(finished()));
400 QSignalSpy canceledSpy(&response, SIGNAL(canceled()));
401 QSignalSpy resumedSpy(&response, SIGNAL(resumed()));
402
403 QCOMPARE(response.isActive(), false);
404 QCOMPARE(response.isIdle(), false);
405 QCOMPARE(response.error(), int(QGalleryAbstractRequest::NoError));
406 QCOMPARE(response.errorString(), QString());
407
408 response.error(error: 300, errorString: QLatin1String("error"));
409
410 QCOMPARE(response.isActive(), false);
411 QCOMPARE(response.isIdle(), false);
412 QCOMPARE(response.error(), int(QGalleryAbstractRequest::NoError));
413 QCOMPARE(response.errorString(), QString());
414 QCOMPARE(finishedSpy.count(), 0);
415 QCOMPARE(canceledSpy.count(), 0);
416 QCOMPARE(resumedSpy.count(), 0);
417}
418
419void tst_QGalleryAbstractResponse::immediateError()
420{
421 QGalleryAbstractResponse response(300, QLatin1String("error"));
422
423 QCOMPARE(response.isActive(), false);
424 QCOMPARE(response.isIdle(), false);
425 QCOMPARE(response.error(), 300);
426 QCOMPARE(response.errorString(), QLatin1String("error"));
427 QCOMPARE(response.waitForFinished(300), true);
428}
429
430void tst_QGalleryAbstractResponse::waitForFinishedImmediate()
431{
432 QtGalleryTestResponse response;
433
434 QMetaObject::invokeMethod(obj: &response, member: "doFinish", type: Qt::QueuedConnection);
435 QCOMPARE(response.isActive(), true);
436 QCOMPARE(response.waitForFinished(0), true);
437 QCOMPARE(response.isActive(), false);
438}
439
440void tst_QGalleryAbstractResponse::waitForFinishedTimeout()
441{
442 QtGalleryTestResponse response;
443
444 QCOMPARE(response.waitForFinished(300), false);
445
446 QTimer::singleShot(msec: 250, receiver: &response, SLOT(doFinish()));
447 QCOMPARE(response.isActive(), true);
448 QCOMPARE(response.waitForFinished(100), false);
449 QCOMPARE(response.isActive(), true);
450 QCOMPARE(response.waitForFinished(500), true);
451 QCOMPARE(response.isActive(), false);
452
453}
454
455void tst_QGalleryAbstractResponse::waitForFinishedNoTimeout()
456{
457 QtGalleryTestResponse response;
458
459 QTimer::singleShot(msec: 250, receiver: &response, SLOT(doFinish()));
460 QCOMPARE(response.isActive(), true);
461 QCOMPARE(response.waitForFinished(-1), true);
462 QCOMPARE(response.isActive(), false);
463}
464
465void tst_QGalleryAbstractResponse::waitForCanceledImmediate()
466{
467 QtGalleryTestResponse response;
468
469 QMetaObject::invokeMethod(obj: &response, member: "doCancel", type: Qt::QueuedConnection);
470 QCOMPARE(response.isActive(), true);
471 QCOMPARE(response.waitForFinished(0), true);
472 QCOMPARE(response.isActive(), false);
473}
474
475void tst_QGalleryAbstractResponse::waitForCanceledNoTimeout()
476{
477 QtGalleryTestResponse response;
478
479 QMetaObject::invokeMethod(obj: &response, member: "doCancel", type: Qt::QueuedConnection);
480 QCOMPARE(response.isActive(), true);
481 QCOMPARE(response.waitForFinished(-1), true);
482 QCOMPARE(response.isActive(), false);
483}
484
485void tst_QGalleryAbstractResponse::waitForErrorImmediate()
486{
487 QtGalleryTestResponse response;
488 QMetaObject::invokeMethod(obj: &response, member: "doError", type: Qt::QueuedConnection, Q_ARG(int, 1));
489 QCOMPARE(response.isActive(), true);
490 QCOMPARE(response.waitForFinished(0), true);
491 QCOMPARE(response.isActive(), false);
492}
493
494void tst_QGalleryAbstractResponse::waitForErrorNoTimeout()
495{
496 QtGalleryTestResponse response;
497 QMetaObject::invokeMethod(obj: &response, member: "doError", type: Qt::QueuedConnection, Q_ARG(int, 1));
498 QCOMPARE(response.isActive(), true);
499 QCOMPARE(response.waitForFinished(-1), true);
500 QCOMPARE(response.isActive(), false);
501}
502
503void tst_QGalleryAbstractResponse::progressChangedSignal()
504{
505 QtGalleryTestResponse response;
506
507 QSignalSpy progressChangedSpy(&response, SIGNAL(progressChanged(int,int)));
508 QVERIFY(progressChangedSpy.count() == 0);
509 QCOMPARE(response.isActive(), true);
510 QCOMPARE(response.isIdle(), false);
511
512 response.progressChanged(current: 10, maximum: 100);
513 QVERIFY(progressChangedSpy.count() == 1);
514
515 response.cancel();
516 QVERIFY(progressChangedSpy.count() == 1);
517
518 response.progressChanged(current: 20, maximum: 200);
519 QVERIFY(progressChangedSpy.count() == 2);
520
521 response.progressChanged(current: 20, maximum: 200);
522 QVERIFY(progressChangedSpy.count() == 3);
523}
524
525#include "tst_qgalleryabstractresponse.moc"
526
527QTEST_MAIN(tst_QGalleryAbstractResponse)
528

source code of qtdocgallery/tests/auto/qgalleryabstractresponse/tst_qgalleryabstractresponse.cpp