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 <qgalleryabstractrequest.h>
45
46#include <qabstractgallery.h>
47#include <qgalleryabstractresponse.h>
48
49#include <QtTest/QtTest>
50
51QT_USE_DOCGALLERY_NAMESPACE
52
53Q_DECLARE_METATYPE(QGalleryAbstractRequest::State)
54
55class tst_QGalleryAbstractRequest : public QObject
56{
57 Q_OBJECT
58
59public Q_SLOTS:
60 void initTestCase();
61
62private Q_SLOTS:
63 void type();
64 void isSupported();
65 void executeNoGallery();
66 void executeUnsupported();
67 void executeSync();
68 void executeAsync();
69 void resume();
70 void cancelActive();
71 void cancelIdle();
72 void clear();
73 void waitForFinished();
74 void progress();
75 void setGallery();
76 void clearGallery();
77 void deleteGallery();
78};
79
80class QtGalleryTestResponse : public QGalleryAbstractResponse
81{
82 Q_OBJECT
83public:
84 QtGalleryTestResponse(
85 QGalleryAbstractRequest::State state, int error, const QString &errorString)
86 : m_ignoreCancel(false)
87 , m_finishInWait(false)
88 {
89 if (error != QGalleryAbstractRequest::NoError)
90 QGalleryAbstractResponse::error(error, errorString);
91 else if (state == QGalleryAbstractRequest::Finished)
92 finish();
93 else if (state == QGalleryAbstractRequest::Idle)
94 finish(idle: true);
95 }
96
97 bool waitForFinished(int)
98 {
99 if (m_finishInWait) {
100 finish(idle: true);
101 return true;
102 } else {
103 return false;
104 }
105 }
106
107 void cancel() { if (!m_ignoreCancel) QGalleryAbstractResponse::cancel(); }
108
109 using QGalleryAbstractResponse::finished;
110 using QGalleryAbstractResponse::finish;
111 using QGalleryAbstractResponse::error;
112 using QGalleryAbstractResponse::resume;
113 using QGalleryAbstractResponse::resumed;
114 using QGalleryAbstractResponse::canceled;
115 using QGalleryAbstractResponse::progressChanged;
116
117 void setIgnoreCancel(bool ignore) { m_ignoreCancel = ignore; }
118 void setFinishInWait(bool finish) { m_finishInWait = finish; }
119
120private:
121 bool m_ignoreCancel;
122 bool m_finishInWait;
123};
124
125class QtGalleryTestRequest : public QGalleryAbstractRequest
126{
127 Q_OBJECT
128public:
129 QtGalleryTestRequest(QGalleryAbstractRequest::RequestType type)
130 : QGalleryAbstractRequest(type)
131 , m_response(0)
132 {
133 }
134
135 QtGalleryTestRequest(
136 QAbstractGallery *gallery, QGalleryAbstractRequest::RequestType type)
137 : QGalleryAbstractRequest(gallery, type)
138 , m_response(0)
139 {
140 }
141
142 QtGalleryTestResponse *response() const { return m_response; }
143
144protected:
145 void setResponse(QGalleryAbstractResponse *response) {
146 m_response = static_cast<QtGalleryTestResponse *>(response); }
147
148private:
149 QtGalleryTestResponse *m_response;
150};
151
152class QtTestGallery : public QAbstractGallery
153{
154 Q_OBJECT
155public:
156 QtTestGallery()
157 : m_state(QGalleryAbstractRequest::Active)
158 , m_error(QGalleryAbstractRequest::NoError)
159 {}
160
161 bool isRequestSupported(QGalleryAbstractRequest::RequestType type) const {
162 return m_supportedRequests.contains(t: type); }
163
164 void setState(QGalleryAbstractRequest::State state) { m_state = state; }
165 void setError(int error, const QString &errorString) {
166 m_error = error; m_errorString = errorString; }
167
168 void setSupportedRequests(const QList<QGalleryAbstractRequest::RequestType> &requests) {
169 m_supportedRequests = requests; }
170
171protected:
172 QGalleryAbstractResponse *createResponse(QGalleryAbstractRequest *request)
173 {
174 if (m_supportedRequests.contains(t: request->type())) {
175 return new QtGalleryTestResponse(m_state, m_error, m_errorString);
176 }
177 return 0;
178 }
179
180private:
181 QList<QGalleryAbstractRequest::RequestType> m_supportedRequests;
182 QGalleryAbstractRequest::State m_state;
183 int m_error;
184 QString m_errorString;
185};
186
187
188void tst_QGalleryAbstractRequest::type()
189{
190 QCOMPARE(QtGalleryTestRequest(QGalleryAbstractRequest::QueryRequest).type(),
191 QGalleryAbstractRequest::QueryRequest);
192
193 QCOMPARE(QtGalleryTestRequest(QGalleryAbstractRequest::ItemRequest).type(),
194 QGalleryAbstractRequest::ItemRequest);
195 QCOMPARE(QtGalleryTestRequest(QGalleryAbstractRequest::QueryRequest).type(),
196 QGalleryAbstractRequest::QueryRequest);
197 //Added Enum
198 QCOMPARE(QtGalleryTestRequest(QGalleryAbstractRequest::TypeRequest).type(),
199 QGalleryAbstractRequest::TypeRequest);
200}
201
202void tst_QGalleryAbstractRequest::initTestCase()
203{
204 qRegisterMetaType<QGalleryAbstractRequest::State>();
205}
206
207void tst_QGalleryAbstractRequest::isSupported()
208{
209 QtTestGallery gallery;
210 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
211 << QGalleryAbstractRequest::ItemRequest);
212
213 QCOMPARE(QtGalleryTestRequest(&gallery, QGalleryAbstractRequest::QueryRequest).isSupported(), false);
214 QCOMPARE(QtGalleryTestRequest(&gallery, QGalleryAbstractRequest::ItemRequest).isSupported(), true);
215}
216
217void tst_QGalleryAbstractRequest::executeNoGallery()
218{
219 QtGalleryTestRequest request(QGalleryAbstractRequest::ItemRequest);
220
221 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
222 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
223 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
224 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
225
226 request.execute();
227 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
228 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoGallery));
229 QCOMPARE(request.errorString(), QLatin1String("No gallery has been set on the QtGalleryTestRequest."));
230 QCOMPARE(finishedSpy.count(), 0);
231 QCOMPARE(canceledSpy.count(), 0);
232 QCOMPARE(errorSpy.count(), 1);
233 QCOMPARE(stateSpy.count(), 1);
234 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
235 QCOMPARE(errorSpy.last().value(1).toString(), request.errorString());
236 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
237}
238
239void tst_QGalleryAbstractRequest::executeUnsupported()
240{
241 QtTestGallery gallery;
242 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
243
244 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
245 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
246 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
247 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
248 QSignalSpy errorchangedspy(&request,SIGNAL(errorChanged()));//Added Signal
249 request.execute();
250 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
251 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NotSupported));
252 QCOMPARE(request.errorString(), QLatin1String("QtGalleryTestRequest is not supported by QtTestGallery."));
253 QCOMPARE(finishedSpy.count(), 0);
254 QCOMPARE(canceledSpy.count(), 0);
255 QCOMPARE(errorSpy.count(), 1);
256 QCOMPARE(stateSpy.count(), 1);
257 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
258 QCOMPARE(errorSpy.last().value(1).toString(), request.errorString());
259 QCOMPARE(errorchangedspy.count(),1);
260 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
261}
262
263void tst_QGalleryAbstractRequest::executeSync()
264{
265 QtTestGallery gallery;
266 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
267 << QGalleryAbstractRequest::ItemRequest);
268
269 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
270
271 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
272 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
273 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
274 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
275
276 // Successful execution.
277 gallery.setState(QGalleryAbstractRequest::Finished);
278 request.execute();
279 QVERIFY(request.response() != 0);
280 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
281 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
282 QCOMPARE(finishedSpy.count(), 1);
283 QCOMPARE(canceledSpy.count(), 0);
284 QCOMPARE(errorSpy.count(), 0);
285 QCOMPARE(stateSpy.count(), 1);
286 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
287
288 // Unsuccessful execution.
289 gallery.setError(error: 121, errorString: QLatin1String("bad input"));
290 request.execute();
291 QVERIFY(request.response() == 0);
292 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
293 QCOMPARE(request.error(), 121);
294 QCOMPARE(request.errorString(), QLatin1String("bad input"));
295 QCOMPARE(finishedSpy.count(), 1);
296 QCOMPARE(canceledSpy.count(), 0);
297 QCOMPARE(errorSpy.count(), 1);
298 QCOMPARE(stateSpy.count(), 2);
299 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
300 QCOMPARE(errorSpy.last().value(1).toString(), request.errorString());
301 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
302
303 gallery.setError(error: 65, errorString: QLatin1String("bad connection"));
304 request.execute();
305 QVERIFY(request.response() == 0);
306 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
307 QCOMPARE(request.error(), 65);
308 QCOMPARE(request.errorString(), QLatin1String("bad connection"));
309 QCOMPARE(finishedSpy.count(), 1);
310 QCOMPARE(canceledSpy.count(), 0);
311 QCOMPARE(errorSpy.count(), 2);
312 QCOMPARE(stateSpy.count(), 3);
313 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
314 QCOMPARE(errorSpy.last().value(1).toString(), request.errorString());
315 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
316
317 //Added enum gallery error
318 gallery.setError(error: 100, errorString: QLatin1String("gallery Corrupted"));
319 request.execute();
320 QVERIFY(request.response() == 0);
321 QCOMPARE(request.error(), int(QGalleryAbstractRequest::GalleryError));
322 QCOMPARE(request.error(), 100);
323 QCOMPARE(request.errorString(), QLatin1String("gallery Corrupted"));
324 QCOMPARE(finishedSpy.count(), 1);
325 QCOMPARE(canceledSpy.count(), 0);
326 QCOMPARE(errorSpy.count(), 3);
327 QCOMPARE(stateSpy.count(), 4);
328 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
329 QCOMPARE(errorSpy.last().value(1).toString(), request.errorString());
330 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
331
332 // Successful execution, to idle.
333 gallery.setError(error: QGalleryAbstractRequest::NoError, errorString: QString());
334 gallery.setState(QGalleryAbstractRequest::Idle);
335 request.execute();
336 QVERIFY(request.response() != 0);
337 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
338 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
339 QCOMPARE(finishedSpy.count(), 2);
340 QCOMPARE(canceledSpy.count(), 0);
341 QCOMPARE(errorSpy.count(), 3);
342 QCOMPARE(stateSpy.count(), 5);
343 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
344
345 // Execute unsupported.
346 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>());
347 request.execute();
348 QVERIFY(request.response() == 0);
349 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
350 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NotSupported));
351 QCOMPARE(finishedSpy.count(), 2);
352 QCOMPARE(canceledSpy.count(), 0);
353 QCOMPARE(errorSpy.count(), 4);
354 QCOMPARE(stateSpy.count(), 6);
355 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
356 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
357}
358void tst_QGalleryAbstractRequest::executeAsync()
359{
360 QtTestGallery gallery;
361 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
362 << QGalleryAbstractRequest::ItemRequest);
363
364 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
365
366 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
367 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
368 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
369 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
370
371 // Successful execution.
372 gallery.setState(QGalleryAbstractRequest::Active);
373 request.execute();
374 QVERIFY(request.response() != 0);
375 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
376 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
377 QCOMPARE(finishedSpy.count(), 0);
378 QCOMPARE(canceledSpy.count(), 0);
379 QCOMPARE(errorSpy.count(), 0);
380 QCOMPARE(stateSpy.count(), 1);
381 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
382
383 request.response()->finish(idle: false);
384 QVERIFY(request.response() != 0);
385 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
386 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
387 QCOMPARE(finishedSpy.count(), 1);
388 QCOMPARE(canceledSpy.count(), 0);
389 QCOMPARE(errorSpy.count(), 0);
390 QCOMPARE(stateSpy.count(), 2);
391 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
392
393 // Unsuccessful execution.
394 request.execute();
395 QVERIFY(request.response() != 0);
396 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
397 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
398 QCOMPARE(finishedSpy.count(), 1);
399 QCOMPARE(canceledSpy.count(), 0);
400 QCOMPARE(errorSpy.count(), 0);
401 QCOMPARE(stateSpy.count(), 3);
402 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
403
404 request.response()->error(error: 80, errorString: QString());
405 QVERIFY(request.response() != 0);
406 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
407 QCOMPARE(request.error(), 80);
408 QCOMPARE(finishedSpy.count(), 1);
409 QCOMPARE(canceledSpy.count(), 0);
410 QCOMPARE(errorSpy.count(), 1);
411 QCOMPARE(stateSpy.count(), 4);
412 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
413 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
414
415 // Successful execution, to active.
416 gallery.setError(error: QGalleryAbstractRequest::NoError, errorString: QString());
417 request.execute();
418 QVERIFY(request.response() != 0);
419 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
420 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
421 QCOMPARE(finishedSpy.count(), 1);
422 QCOMPARE(canceledSpy.count(), 0);
423 QCOMPARE(errorSpy.count(), 1);
424 QCOMPARE(stateSpy.count(), 5);
425 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
426
427 // Response emits finish without changing the error.
428 request.response()->finished();
429 QVERIFY(request.response() != 0);
430 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
431 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
432 QCOMPARE(finishedSpy.count(), 1);
433 QCOMPARE(canceledSpy.count(), 0);
434 QCOMPARE(errorSpy.count(), 1);
435 QCOMPARE(stateSpy.count(), 5);
436
437 // Response finishes correctly.
438 request.response()->finish(idle: true);
439 QVERIFY(request.response() != 0);
440 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
441 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
442 QCOMPARE(finishedSpy.count(), 2);
443 QCOMPARE(canceledSpy.count(), 0);
444 QCOMPARE(errorSpy.count(), 1);
445 QCOMPARE(stateSpy.count(), 6);
446 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
447
448 // Response emits finish without changing the idle state.
449 request.response()->finished();
450 QVERIFY(request.response() != 0);
451 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
452 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
453 QCOMPARE(finishedSpy.count(), 2);
454 QCOMPARE(canceledSpy.count(), 0);
455 QCOMPARE(errorSpy.count(), 1);
456 QCOMPARE(stateSpy.count(), 6);
457
458 // Response cancels itself.
459 QVERIFY(request.response() != 0);
460 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
461 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
462 QCOMPARE(finishedSpy.count(), 2);
463 QCOMPARE(canceledSpy.count(), 0);
464 QCOMPARE(errorSpy.count(), 1);
465 QCOMPARE(stateSpy.count(), 6);
466
467 // Response exits idle state correctly.
468 request.response()->finish(idle: false);
469 QVERIFY(request.response() != 0);
470 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
471 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
472 QCOMPARE(finishedSpy.count(), 2);
473 QCOMPARE(canceledSpy.count(), 0);
474 QCOMPARE(errorSpy.count(), 1);
475 QCOMPARE(stateSpy.count(), 7);
476 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
477
478 // Response emits finish from finished state.
479 request.response()->finished();
480 QVERIFY(request.response() != 0);
481 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
482 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
483 QCOMPARE(finishedSpy.count(), 2);
484 QCOMPARE(canceledSpy.count(), 0);
485 QCOMPARE(errorSpy.count(), 1);
486 QCOMPARE(stateSpy.count(), 7);
487 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
488}
489
490void tst_QGalleryAbstractRequest::resume()
491{
492 QtTestGallery gallery;
493 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
494 << QGalleryAbstractRequest::ItemRequest);
495 gallery.setState(QGalleryAbstractRequest::Idle);
496
497 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
498
499 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
500 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
501 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
502 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
503
504 request.execute();
505 QVERIFY(request.response() != 0);
506 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
507 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
508 QCOMPARE(finishedSpy.count(), 1);
509 QCOMPARE(canceledSpy.count(), 0);
510 QCOMPARE(errorSpy.count(), 0);
511 QCOMPARE(stateSpy.count(), 1);
512 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
513
514 // Response emits resumed without changing idle.
515 request.response()->resumed();
516 QVERIFY(request.response() != 0);
517 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
518 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
519 QCOMPARE(finishedSpy.count(), 1);
520 QCOMPARE(canceledSpy.count(), 0);
521 QCOMPARE(errorSpy.count(), 0);
522 QCOMPARE(stateSpy.count(), 1);
523
524 // Response resumes correctly.
525 request.response()->resume();
526 QVERIFY(request.response() != 0);
527 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
528 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
529 QCOMPARE(finishedSpy.count(), 1);
530 QCOMPARE(canceledSpy.count(), 0);
531 QCOMPARE(errorSpy.count(), 0);
532 QCOMPARE(stateSpy.count(), 2);
533 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
534
535 // Resuming while active does nothing.
536 request.response()->resume();
537 QVERIFY(request.response() != 0);
538 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
539 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
540 QCOMPARE(finishedSpy.count(), 1);
541 QCOMPARE(canceledSpy.count(), 0);
542 QCOMPARE(errorSpy.count(), 0);
543 QCOMPARE(stateSpy.count(), 2);
544
545 // Emitting resumed while active does nothing.
546 request.response()->resumed();
547 QVERIFY(request.response() != 0);
548 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
549 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
550 QCOMPARE(finishedSpy.count(), 1);
551 QCOMPARE(canceledSpy.count(), 0);
552 QCOMPARE(errorSpy.count(), 0);
553 QCOMPARE(stateSpy.count(), 2);
554
555 // Returning to idle again doesn't emit finished.
556 request.response()->finish(idle: true);
557 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
558 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
559 QCOMPARE(finishedSpy.count(), 1);
560 QCOMPARE(canceledSpy.count(), 0);
561 QCOMPARE(errorSpy.count(), 0);
562 QCOMPARE(stateSpy.count(), 3);
563 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
564
565 // Finishing doesn't emit finished.
566 request.response()->resume();
567 QVERIFY(request.response() != 0);
568 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
569 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
570 QCOMPARE(finishedSpy.count(), 1);
571 QCOMPARE(canceledSpy.count(), 0);
572 QCOMPARE(errorSpy.count(), 0);
573 QCOMPARE(stateSpy.count(), 4);
574 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
575
576 request.response()->finish(idle: false);
577 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
578 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
579 QCOMPARE(finishedSpy.count(), 1);
580 QCOMPARE(canceledSpy.count(), 0);
581 QCOMPARE(errorSpy.count(), 0);
582 QCOMPARE(stateSpy.count(), 5);
583 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
584
585 // Emitting resumed when finished does nothing
586 request.response()->resumed();
587 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
588 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
589 QCOMPARE(finishedSpy.count(), 1);
590 QCOMPARE(canceledSpy.count(), 0);
591 QCOMPARE(errorSpy.count(), 0);
592 QCOMPARE(stateSpy.count(), 5);
593}
594
595void tst_QGalleryAbstractRequest::cancelActive()
596{
597 QtTestGallery gallery;
598 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
599 << QGalleryAbstractRequest::ItemRequest);
600
601 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
602
603 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
604 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
605 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
606 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
607
608 // Cancel synchronously.
609 request.execute();
610 QVERIFY(request.response() != 0);
611 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
612 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
613 QCOMPARE(finishedSpy.count(), 0);
614 QCOMPARE(canceledSpy.count(), 0);
615 QCOMPARE(errorSpy.count(), 0);
616 QCOMPARE(stateSpy.count(), 1);
617 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
618
619 request.cancel();
620 QVERIFY(request.response() != 0);
621 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceled);
622 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
623 QCOMPARE(finishedSpy.count(), 0);
624 QCOMPARE(canceledSpy.count(), 1);
625 QCOMPARE(errorSpy.count(), 0);
626 QCOMPARE(stateSpy.count(), 2);
627 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
628
629 // Cancel asynchronously.
630 request.execute();
631 QVERIFY(request.response() != 0);
632 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
633 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
634 QCOMPARE(finishedSpy.count(), 0);
635 QCOMPARE(canceledSpy.count(), 1);
636 QCOMPARE(errorSpy.count(), 0);
637 QCOMPARE(stateSpy.count(), 3);
638 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
639
640 // Response emits canceled unprovoked.
641 request.response()->canceled();
642 QVERIFY(request.response() != 0);
643 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
644 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
645 QCOMPARE(finishedSpy.count(), 0);
646 QCOMPARE(canceledSpy.count(), 1);
647 QCOMPARE(errorSpy.count(), 0);
648 QCOMPARE(stateSpy.count(), 3);
649
650 request.response()->setIgnoreCancel(true);
651 request.cancel();
652 QVERIFY(request.response() != 0);
653 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceling);
654 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
655 QCOMPARE(finishedSpy.count(), 0);
656 QCOMPARE(canceledSpy.count(), 1);
657 QCOMPARE(errorSpy.count(), 0);
658 QCOMPARE(stateSpy.count(), 4);
659 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
660
661 // No state changes while cancel in pending
662 request.cancel();
663 QVERIFY(request.response() != 0);
664 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceling);
665 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
666 QCOMPARE(finishedSpy.count(), 0);
667 QCOMPARE(canceledSpy.count(), 1);
668 QCOMPARE(errorSpy.count(), 0);
669 QCOMPARE(stateSpy.count(), 4);
670
671 request.response()->setIgnoreCancel(false);
672 request.response()->cancel();
673 QVERIFY(request.response() != 0);
674 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceled);
675 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
676 QCOMPARE(finishedSpy.count(), 0);
677 QCOMPARE(canceledSpy.count(), 2);
678 QCOMPARE(errorSpy.count(), 0);
679 QCOMPARE(stateSpy.count(), 5);
680 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
681
682 // No state changes after cancel is finished.
683 request.cancel();
684 QVERIFY(request.response() != 0);
685 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceled);
686 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
687 QCOMPARE(finishedSpy.count(), 0);
688 QCOMPARE(canceledSpy.count(), 2);
689 QCOMPARE(errorSpy.count(), 0);
690 QCOMPARE(stateSpy.count(), 5);
691
692 // Finish as normal if response finishes after being canceled.
693 request.execute();
694 QVERIFY(request.response() != 0);
695 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
696 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
697 QCOMPARE(finishedSpy.count(), 0);
698 QCOMPARE(canceledSpy.count(), 2);
699 QCOMPARE(errorSpy.count(), 0);
700 QCOMPARE(stateSpy.count(), 6);
701 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
702
703 request.response()->setIgnoreCancel(true);
704 request.cancel();
705 QVERIFY(request.response() != 0);
706 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceling);
707 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
708 QCOMPARE(finishedSpy.count(), 0);
709 QCOMPARE(canceledSpy.count(), 2);
710 QCOMPARE(errorSpy.count(), 0);
711 QCOMPARE(stateSpy.count(), 7);
712 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
713
714 request.response()->finish(idle: false);
715 QVERIFY(request.response() != 0);
716 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
717 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
718 QCOMPARE(finishedSpy.count(), 1);
719 QCOMPARE(canceledSpy.count(), 2);
720 QCOMPARE(errorSpy.count(), 0);
721 QCOMPARE(stateSpy.count(), 8);
722 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
723}
724
725void tst_QGalleryAbstractRequest::cancelIdle()
726{
727 QtTestGallery gallery;
728 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
729 << QGalleryAbstractRequest::ItemRequest);
730 gallery.setState(QGalleryAbstractRequest::Idle);
731
732 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
733
734 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
735 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
736 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
737 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
738
739 // Cancel synchronously.
740 request.execute();
741 QVERIFY(request.response() != 0);
742 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
743 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
744 QCOMPARE(finishedSpy.count(), 1);
745 QCOMPARE(canceledSpy.count(), 0);
746 QCOMPARE(errorSpy.count(), 0);
747 QCOMPARE(stateSpy.count(), 1);
748 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
749
750 request.cancel();
751 QVERIFY(request.response() != 0);
752 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
753 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
754 QCOMPARE(finishedSpy.count(), 1);
755 QCOMPARE(canceledSpy.count(), 0);
756 QCOMPARE(errorSpy.count(), 0);
757 QCOMPARE(stateSpy.count(), 2);
758 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
759
760 // Cancel asynchronously.
761 request.execute();
762 QVERIFY(request.response() != 0);
763 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
764 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
765 QCOMPARE(finishedSpy.count(), 2);
766 QCOMPARE(canceledSpy.count(), 0);
767 QCOMPARE(errorSpy.count(), 0);
768 QCOMPARE(stateSpy.count(), 3);
769 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
770
771 request.response()->setIgnoreCancel(true);
772 request.cancel();
773 QVERIFY(request.response() != 0);
774 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceling);
775 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
776 QCOMPARE(finishedSpy.count(), 2);
777 QCOMPARE(canceledSpy.count(), 0);
778 QCOMPARE(errorSpy.count(), 0);
779 QCOMPARE(stateSpy.count(), 4);
780 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
781
782 request.response()->setIgnoreCancel(false);
783 request.response()->cancel();
784 QVERIFY(request.response() != 0);
785 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
786 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
787 QCOMPARE(finishedSpy.count(), 2);
788 QCOMPARE(canceledSpy.count(), 0);
789 QCOMPARE(errorSpy.count(), 0);
790 QCOMPARE(stateSpy.count(), 5);
791 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
792}
793
794void tst_QGalleryAbstractRequest::clear()
795{
796 QtTestGallery gallery;
797 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
798 << QGalleryAbstractRequest::ItemRequest);
799
800 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
801
802 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
803 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
804 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
805 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
806
807 // Clear no response.
808 request.clear();
809 QVERIFY(request.response() == 0);
810 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
811 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
812 QCOMPARE(finishedSpy.count(), 0);
813 QCOMPARE(canceledSpy.count(), 0);
814 QCOMPARE(errorSpy.count(), 0);
815 QCOMPARE(stateSpy.count(), 0);
816
817 // Clear finished response.
818 gallery.setState(QGalleryAbstractRequest::Finished);
819 request.execute();
820 QVERIFY(request.response() != 0);
821 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
822 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
823 QCOMPARE(finishedSpy.count(), 1);
824 QCOMPARE(canceledSpy.count(), 0);
825 QCOMPARE(errorSpy.count(), 0);
826 QCOMPARE(stateSpy.count(), 1);
827 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
828
829 request.clear();
830 QVERIFY(request.response() == 0);
831 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
832 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
833 QCOMPARE(finishedSpy.count(), 1);
834 QCOMPARE(canceledSpy.count(), 0);
835 QCOMPARE(errorSpy.count(), 0);
836 QCOMPARE(stateSpy.count(), 2);
837 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
838
839 // Clear idle response.
840 gallery.setState(QGalleryAbstractRequest::Idle);
841 request.execute();
842 QVERIFY(request.response() != 0);
843 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
844 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
845 QCOMPARE(finishedSpy.count(), 2);
846 QCOMPARE(canceledSpy.count(), 0);
847 QCOMPARE(errorSpy.count(), 0);
848 QCOMPARE(stateSpy.count(), 3);
849 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
850
851 request.clear();
852 QVERIFY(request.response() == 0);
853 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
854 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
855 QCOMPARE(finishedSpy.count(), 2);
856 QCOMPARE(canceledSpy.count(), 0);
857 QCOMPARE(errorSpy.count(), 0);
858 QCOMPARE(stateSpy.count(), 4);
859 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
860
861 // Clear active response.
862 gallery.setState(QGalleryAbstractRequest::Active);
863 request.execute();
864 QVERIFY(request.response() != 0);
865 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
866 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
867 QCOMPARE(finishedSpy.count(), 2);
868 QCOMPARE(canceledSpy.count(), 0);
869 QCOMPARE(errorSpy.count(), 0);
870 QCOMPARE(stateSpy.count(), 5);
871 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
872
873 request.clear();
874 QVERIFY(request.response() == 0);
875 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
876 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
877 QCOMPARE(finishedSpy.count(), 2);
878 QCOMPARE(canceledSpy.count(), 0);
879 QCOMPARE(errorSpy.count(), 0);
880 QCOMPARE(stateSpy.count(), 6);
881 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
882
883 // Clear error response.
884 gallery.setError(error: 65, errorString: QLatin1String("bad connection"));
885 request.execute();
886 QVERIFY(request.response() == 0);
887 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
888 QCOMPARE(request.error(), 65);
889 QCOMPARE(request.errorString(), QLatin1String("bad connection"));
890 QCOMPARE(finishedSpy.count(), 2);
891 QCOMPARE(canceledSpy.count(), 0);
892 QCOMPARE(errorSpy.count(), 1);
893 QCOMPARE(stateSpy.count(), 7);
894 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
895 QCOMPARE(errorSpy.last().value(1).toString(), request.errorString());
896 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
897
898 request.clear();
899 QVERIFY(request.response() == 0);
900 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
901 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
902 QCOMPARE(request.errorString(), QString());
903 QCOMPARE(finishedSpy.count(), 2);
904 QCOMPARE(canceledSpy.count(), 0);
905 QCOMPARE(errorSpy.count(), 1);
906 QCOMPARE(stateSpy.count(), 8);
907 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
908}
909
910void tst_QGalleryAbstractRequest::waitForFinished()
911{
912 QtTestGallery gallery;
913 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
914 << QGalleryAbstractRequest::ItemRequest);
915
916 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
917
918 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
919 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
920 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
921 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
922
923 // Wait while inactive, no error.
924 QCOMPARE(request.waitForFinished(300), true);
925 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
926 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
927 QCOMPARE(finishedSpy.count(), 0);
928 QCOMPARE(canceledSpy.count(), 0);
929 QCOMPARE(errorSpy.count(), 0);
930 QCOMPARE(stateSpy.count(), 0);
931
932 request.execute();
933 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
934 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
935 QCOMPARE(finishedSpy.count(), 0);
936 QCOMPARE(canceledSpy.count(), 0);
937 QCOMPARE(errorSpy.count(), 0);
938 QCOMPARE(stateSpy.count(), 1);
939 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
940
941 // Timeout while waiting.
942 QCOMPARE(request.waitForFinished(300), false);
943 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
944 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
945 QCOMPARE(finishedSpy.count(), 0);
946 QCOMPARE(canceledSpy.count(), 0);
947 QCOMPARE(errorSpy.count(), 0);
948 QCOMPARE(stateSpy.count(), 1);
949
950 // Finish while waiting.
951 request.response()->setFinishInWait(true);
952 QCOMPARE(request.waitForFinished(300), true);
953 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
954 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
955 QCOMPARE(finishedSpy.count(), 1);
956 QCOMPARE(canceledSpy.count(), 0);
957 QCOMPARE(errorSpy.count(), 0);
958 QCOMPARE(stateSpy.count(), 2);
959 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
960
961 // Wait while idle.
962 request.response()->setFinishInWait(false);
963 QCOMPARE(request.waitForFinished(300), true);
964 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
965 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
966 QCOMPARE(finishedSpy.count(), 1);
967 QCOMPARE(canceledSpy.count(), 0);
968 QCOMPARE(errorSpy.count(), 0);
969 QCOMPARE(stateSpy.count(), 2);
970
971 // Wait while finished.
972 request.response()->finish(idle: false);
973 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
974 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
975 QCOMPARE(finishedSpy.count(), 1);
976 QCOMPARE(canceledSpy.count(), 0);
977 QCOMPARE(errorSpy.count(), 0);
978 QCOMPARE(stateSpy.count(), 3);
979 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
980
981 QCOMPARE(request.waitForFinished(300), true);
982 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
983 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
984 QCOMPARE(finishedSpy.count(), 1);
985 QCOMPARE(canceledSpy.count(), 0);
986 QCOMPARE(errorSpy.count(), 0);
987 QCOMPARE(stateSpy.count(), 3);
988
989 // Wait while canceled.
990 gallery.setState(QGalleryAbstractRequest::Active);
991 request.execute();
992 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
993 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
994 QCOMPARE(finishedSpy.count(), 1);
995 QCOMPARE(canceledSpy.count(), 0);
996 QCOMPARE(errorSpy.count(), 0);
997 QCOMPARE(stateSpy.count(), 4);
998 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
999
1000 request.cancel();
1001 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceled);
1002 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1003 QCOMPARE(finishedSpy.count(), 1);
1004 QCOMPARE(canceledSpy.count(), 1);
1005 QCOMPARE(errorSpy.count(), 0);
1006 QCOMPARE(stateSpy.count(), 5);
1007 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1008
1009 QCOMPARE(request.waitForFinished(300), true);
1010 QCOMPARE(request.state(), QGalleryAbstractRequest::Canceled);
1011 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1012 QCOMPARE(finishedSpy.count(), 1);
1013 QCOMPARE(canceledSpy.count(), 1);
1014 QCOMPARE(errorSpy.count(), 0);
1015 QCOMPARE(stateSpy.count(), 5);
1016}
1017
1018void tst_QGalleryAbstractRequest::progress()
1019{
1020 QtTestGallery gallery;
1021 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
1022 << QGalleryAbstractRequest::ItemRequest);
1023
1024 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
1025
1026 QSignalSpy spy(&request, SIGNAL(progressChanged(int,int)));
1027
1028 QCOMPARE(request.currentProgress(), 0);
1029 QCOMPARE(request.maximumProgress(), 0);
1030
1031 request.execute();
1032 QCOMPARE(spy.count(), 0);
1033 QVERIFY(request.response() != 0);
1034
1035 request.response()->progressChanged(current: 120, maximum: 1000);
1036 QCOMPARE(request.currentProgress(), 120);
1037 QCOMPARE(request.maximumProgress(), 1000);
1038 QCOMPARE(spy.count(), 1);
1039 QCOMPARE(spy.last().value(0).toInt(), 120);
1040 QCOMPARE(spy.last().value(1).toInt(), 1000);
1041
1042 request.response()->progressChanged(current: 5600, maximum: 95);
1043 QCOMPARE(request.currentProgress(), 5600);
1044 QCOMPARE(request.maximumProgress(), 95);
1045 QCOMPARE(spy.count(), 2);
1046 QCOMPARE(spy.last().value(0).toInt(), 5600);
1047 QCOMPARE(spy.last().value(1).toInt(), 95);
1048
1049 request.cancel();
1050 QCOMPARE(request.currentProgress(), 5600);
1051 QCOMPARE(request.maximumProgress(), 95);
1052 QCOMPARE(spy.count(), 2);
1053
1054 request.execute();
1055 QCOMPARE(request.currentProgress(), 0);
1056 QCOMPARE(request.currentProgress(), 0);
1057 QCOMPARE(spy.count(), 3);
1058 QCOMPARE(spy.last().value(0).toInt(), 0);
1059 QCOMPARE(spy.last().value(1).toInt(), 0);
1060
1061 request.response()->progressChanged(current: -2, maximum: 10);
1062 QCOMPARE(request.currentProgress(), -2);
1063 QCOMPARE(request.maximumProgress(), 10);
1064 QCOMPARE(spy.count(), 4);
1065 QCOMPARE(spy.last().value(0).toInt(), -2);
1066 QCOMPARE(spy.last().value(1).toInt(), 10);
1067
1068 request.clear();
1069 QCOMPARE(request.currentProgress(), 0);
1070 QCOMPARE(request.currentProgress(), 0);
1071 QCOMPARE(spy.count(), 5);
1072 QCOMPARE(spy.last().value(0).toInt(), 0);
1073 QCOMPARE(spy.last().value(1).toInt(), 0);
1074
1075 request.execute();
1076 QCOMPARE(request.currentProgress(), 0);
1077 QCOMPARE(request.currentProgress(), 0);
1078 QCOMPARE(spy.count(), 5);
1079
1080 request.response()->progressChanged(current: 0, maximum: 75);
1081 QCOMPARE(request.currentProgress(), 0);
1082 QCOMPARE(request.maximumProgress(), 75);
1083 QCOMPARE(spy.count(), 6);
1084 QCOMPARE(spy.last().value(0).toInt(), 0);
1085 QCOMPARE(spy.last().value(1).toInt(), 75);
1086
1087 request.execute();
1088 QCOMPARE(request.currentProgress(), 0);
1089 QCOMPARE(request.currentProgress(), 0);
1090 QCOMPARE(spy.count(), 7);
1091 QCOMPARE(spy.last().value(0).toInt(), 0);
1092 QCOMPARE(spy.last().value(1).toInt(), 0);
1093
1094 request.response()->progressChanged(current: 0, maximum: -23);
1095 QCOMPARE(request.currentProgress(), 0);
1096 QCOMPARE(request.maximumProgress(), -23);
1097 QCOMPARE(spy.count(), 8);
1098 QCOMPARE(spy.last().value(0).toInt(), 0);
1099 QCOMPARE(spy.last().value(1).toInt(), -23);
1100
1101 request.clear();
1102 QCOMPARE(request.currentProgress(), 0);
1103 QCOMPARE(request.currentProgress(), 0);
1104 QCOMPARE(spy.count(), 9);
1105 QCOMPARE(spy.last().value(0).toInt(), 0);
1106 QCOMPARE(spy.last().value(1).toInt(), 0);
1107}
1108
1109void tst_QGalleryAbstractRequest::setGallery()
1110{
1111 QtTestGallery copyGallery;
1112 copyGallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
1113 << QGalleryAbstractRequest::ItemRequest);
1114
1115 QtTestGallery moveGallery;
1116 moveGallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
1117 << QGalleryAbstractRequest::QueryRequest);
1118
1119 QtGalleryTestRequest copyRequest(QGalleryAbstractRequest::ItemRequest);
1120 QtGalleryTestRequest moveRequest(QGalleryAbstractRequest::QueryRequest);
1121
1122 QSignalSpy copyGallerySpy(&copyRequest, SIGNAL(galleryChanged()));
1123 QSignalSpy moveGallerySpy(&moveRequest, SIGNAL(galleryChanged()));
1124 QSignalSpy copySupportedSpy(&copyRequest, SIGNAL(supportedChanged()));
1125 QSignalSpy moveSupportedSpy(&moveRequest, SIGNAL(supportedChanged()));
1126
1127 QVERIFY(copyRequest.gallery() == 0);
1128 QVERIFY(moveRequest.gallery() == 0);
1129 QCOMPARE(copyRequest.isSupported(), false);
1130 QCOMPARE(moveRequest.isSupported(), false);
1131
1132 copyRequest.setGallery(&copyGallery);
1133 moveRequest.setGallery(&moveGallery);
1134
1135 QVERIFY(copyRequest.gallery() == &copyGallery);
1136 QVERIFY(moveRequest.gallery() == &moveGallery);
1137 QCOMPARE(copyRequest.isSupported(), true);
1138 QCOMPARE(moveRequest.isSupported(), true);
1139 QCOMPARE(copyGallerySpy.count(), 1);
1140 QCOMPARE(moveGallerySpy.count(), 1);
1141 QCOMPARE(copySupportedSpy.count(), 1);
1142 QCOMPARE(moveSupportedSpy.count(), 1);
1143
1144 copyRequest.setGallery(&copyGallery);
1145 moveRequest.setGallery(&moveGallery);
1146 QVERIFY(copyRequest.gallery() == &copyGallery);
1147 QVERIFY(moveRequest.gallery() == &moveGallery);
1148 QCOMPARE(copyGallerySpy.count(), 1);
1149 QCOMPARE(moveGallerySpy.count(), 1);
1150 QCOMPARE(copySupportedSpy.count(), 1);
1151 QCOMPARE(moveSupportedSpy.count(), 1);
1152
1153 copyRequest.setGallery(&moveGallery);
1154 moveRequest.setGallery(&copyGallery);
1155
1156 QVERIFY(copyRequest.gallery() == &moveGallery);
1157 QVERIFY(moveRequest.gallery() == &copyGallery);
1158 QCOMPARE(copyRequest.isSupported(), false);
1159 QCOMPARE(moveRequest.isSupported(), false);
1160 QCOMPARE(copyGallerySpy.count(), 2);
1161 QCOMPARE(moveGallerySpy.count(), 2);
1162 QCOMPARE(copySupportedSpy.count(), 2);
1163 QCOMPARE(moveSupportedSpy.count(), 2);
1164
1165 copyRequest.setGallery(0);
1166 moveRequest.setGallery(0);
1167
1168 QVERIFY(copyRequest.gallery() == 0);
1169 QVERIFY(moveRequest.gallery() == 0);
1170 QCOMPARE(copyRequest.isSupported(), false);
1171 QCOMPARE(moveRequest.isSupported(), false);
1172 QCOMPARE(copyGallerySpy.count(), 3);
1173 QCOMPARE(moveGallerySpy.count(), 3);
1174 QCOMPARE(copySupportedSpy.count(), 3);
1175 QCOMPARE(moveSupportedSpy.count(), 3);
1176}
1177
1178void tst_QGalleryAbstractRequest::clearGallery()
1179{
1180 QtTestGallery gallery;
1181 gallery.setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
1182 << QGalleryAbstractRequest::ItemRequest);
1183
1184 QtGalleryTestRequest request(&gallery, QGalleryAbstractRequest::ItemRequest);
1185
1186 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
1187 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
1188 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
1189 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
1190
1191 // Clear no response.
1192 request.setGallery(0);
1193 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
1194 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1195 QCOMPARE(finishedSpy.count(), 0);
1196 QCOMPARE(canceledSpy.count(), 0);
1197 QCOMPARE(errorSpy.count(), 0);
1198 QCOMPARE(stateSpy.count(), 0);
1199
1200 // Clear finished response.
1201 gallery.setState(QGalleryAbstractRequest::Finished);
1202 request.setGallery(&gallery);
1203 request.execute();
1204 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
1205 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1206 QCOMPARE(finishedSpy.count(), 1);
1207 QCOMPARE(canceledSpy.count(), 0);
1208 QCOMPARE(errorSpy.count(), 0);
1209 QCOMPARE(stateSpy.count(), 1);
1210 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1211
1212 request.setGallery(0);
1213 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
1214 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1215 QCOMPARE(finishedSpy.count(), 1);
1216 QCOMPARE(canceledSpy.count(), 0);
1217 QCOMPARE(errorSpy.count(), 0);
1218 QCOMPARE(stateSpy.count(), 1);
1219
1220 // Clear idle response.
1221 gallery.setState(QGalleryAbstractRequest::Idle);
1222 request.setGallery(&gallery);
1223 request.execute();
1224 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
1225 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1226 QCOMPARE(finishedSpy.count(), 2);
1227 QCOMPARE(canceledSpy.count(), 0);
1228 QCOMPARE(errorSpy.count(), 0);
1229 QCOMPARE(stateSpy.count(), 2);
1230 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1231
1232 request.setGallery(0);
1233 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
1234 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1235 QCOMPARE(finishedSpy.count(), 2);
1236 QCOMPARE(canceledSpy.count(), 0);
1237 QCOMPARE(errorSpy.count(), 0);
1238 QCOMPARE(stateSpy.count(), 2);
1239
1240 // Clear active response.
1241 gallery.setState(QGalleryAbstractRequest::Active);
1242 request.setGallery(&gallery);
1243 request.execute();
1244 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
1245 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1246 QCOMPARE(finishedSpy.count(), 2);
1247 QCOMPARE(canceledSpy.count(), 0);
1248 QCOMPARE(errorSpy.count(), 0);
1249 QCOMPARE(stateSpy.count(), 3);
1250 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1251
1252 request.setGallery(0);
1253 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
1254 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1255 QCOMPARE(finishedSpy.count(), 2);
1256 QCOMPARE(canceledSpy.count(), 0);
1257 QCOMPARE(errorSpy.count(), 0);
1258 QCOMPARE(stateSpy.count(), 3);
1259
1260 // execute after clearing gallery.
1261 request.execute();
1262 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
1263 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoGallery));
1264 QCOMPARE(finishedSpy.count(), 2);
1265 QCOMPARE(canceledSpy.count(), 0);
1266 QCOMPARE(errorSpy.count(), 1);
1267 QCOMPARE(stateSpy.count(), 4);
1268 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
1269 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1270}
1271
1272void tst_QGalleryAbstractRequest::deleteGallery()
1273{
1274 QtTestGallery *gallery = 0;
1275
1276 QtGalleryTestRequest request(QGalleryAbstractRequest::ItemRequest);
1277
1278 QSignalSpy finishedSpy(&request, SIGNAL(finished()));
1279 QSignalSpy canceledSpy(&request, SIGNAL(canceled()));
1280 QSignalSpy errorSpy(&request, SIGNAL(error(int,QString)));
1281 QSignalSpy stateSpy(&request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)));
1282
1283 // No response.
1284 gallery = new QtTestGallery;
1285 gallery->setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
1286 << QGalleryAbstractRequest::ItemRequest);
1287
1288 request.setGallery(gallery);
1289 delete gallery;
1290 QVERIFY(request.gallery() == 0);
1291 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
1292 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1293 QCOMPARE(finishedSpy.count(), 0);
1294 QCOMPARE(canceledSpy.count(), 0);
1295 QCOMPARE(errorSpy.count(), 0);
1296 QCOMPARE(stateSpy.count(), 0);
1297
1298 // Finished response.
1299 gallery = new QtTestGallery;
1300 gallery->setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
1301 << QGalleryAbstractRequest::ItemRequest);
1302 gallery->setState(QGalleryAbstractRequest::Finished);
1303
1304 request.setGallery(gallery);
1305 request.execute();
1306 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
1307 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1308 QCOMPARE(finishedSpy.count(), 1);
1309 QCOMPARE(canceledSpy.count(), 0);
1310 QCOMPARE(errorSpy.count(), 0);
1311 QCOMPARE(stateSpy.count(), 1);
1312 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1313
1314 delete gallery;
1315 QVERIFY(request.gallery() == 0);
1316 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
1317 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1318 QCOMPARE(finishedSpy.count(), 1);
1319 QCOMPARE(canceledSpy.count(), 0);
1320 QCOMPARE(errorSpy.count(), 0);
1321 QCOMPARE(stateSpy.count(), 1);
1322
1323 // Idle response.
1324 gallery = new QtTestGallery;
1325 gallery->setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
1326 << QGalleryAbstractRequest::ItemRequest);
1327 gallery->setState(QGalleryAbstractRequest::Idle);
1328
1329 request.setGallery(gallery);
1330 request.execute();
1331 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
1332 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1333 QCOMPARE(finishedSpy.count(), 2);
1334 QCOMPARE(canceledSpy.count(), 0);
1335 QCOMPARE(errorSpy.count(), 0);
1336 QCOMPARE(stateSpy.count(), 2);
1337 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1338
1339 delete gallery;
1340 QVERIFY(request.gallery() == 0);
1341 QCOMPARE(request.state(), QGalleryAbstractRequest::Idle);
1342 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1343 QCOMPARE(finishedSpy.count(), 2);
1344 QCOMPARE(canceledSpy.count(), 0);
1345 QCOMPARE(errorSpy.count(), 0);
1346 QCOMPARE(stateSpy.count(), 2);
1347
1348 // Clear active response.
1349 gallery = new QtTestGallery;
1350 gallery->setSupportedRequests(QList<QGalleryAbstractRequest::RequestType>()
1351 << QGalleryAbstractRequest::ItemRequest);
1352
1353 request.setGallery(gallery);
1354 request.execute();
1355 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
1356 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1357 QCOMPARE(finishedSpy.count(), 2);
1358 QCOMPARE(canceledSpy.count(), 0);
1359 QCOMPARE(errorSpy.count(), 0);
1360 QCOMPARE(stateSpy.count(), 3);
1361 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1362
1363 delete gallery;
1364 QVERIFY(request.gallery() == 0);
1365 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
1366 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
1367 QCOMPARE(finishedSpy.count(), 2);
1368 QCOMPARE(canceledSpy.count(), 0);
1369 QCOMPARE(errorSpy.count(), 0);
1370 QCOMPARE(stateSpy.count(), 3);
1371
1372 // execute after deleting gallery.
1373 request.execute();
1374 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
1375 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoGallery));
1376 QCOMPARE(finishedSpy.count(), 2);
1377 QCOMPARE(canceledSpy.count(), 0);
1378 QCOMPARE(errorSpy.count(), 1);
1379 QCOMPARE(stateSpy.count(), 4);
1380 QCOMPARE(errorSpy.last().value(0).toInt(), request.error());
1381 QCOMPARE(stateSpy.last().value(0).value<QGalleryAbstractRequest::State>(), request.state());
1382}
1383
1384QTEST_MAIN(tst_QGalleryAbstractRequest)
1385
1386#include "tst_qgalleryabstractrequest.moc"
1387

source code of qtdocgallery/tests/auto/qgalleryabstractrequest/tst_qgalleryabstractrequest.cpp