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 <qgalleryitemrequest.h>
45
46#include <qabstractgallery.h>
47#include <qgalleryabstractresponse.h>
48#include <qgalleryresultset.h>
49#include <qgalleryresource.h>
50#include <qgallerytype.h>
51
52#include <QtTest/QtTest>
53
54Q_DECLARE_METATYPE(QList<int>)
55
56QT_USE_DOCGALLERY_NAMESPACE
57
58Q_DECLARE_METATYPE(QGalleryResultSet*)
59
60class tst_QGalleryItemRequest : public QObject
61{
62 Q_OBJECT
63public Q_SLOTS:
64 void initTestCase();
65
66private Q_SLOTS:
67 void propertyNames();
68 void autoUpdate();
69 void itemId();
70 void executeSynchronous();
71 void executeAsynchronous();
72 void noResponse();
73 void multipleResults();
74};
75
76class QtGalleryTestResponse : public QGalleryResultSet
77{
78 Q_OBJECT
79public:
80 QtGalleryTestResponse(
81 const QStringList &propertyNames,
82 int count,
83 QGalleryAbstractRequest::State state,
84 int error,
85 const QString &errorString)
86 : m_count(count)
87 , m_currentIndex(-1)
88 , m_propertyNames(propertyNames)
89 {
90 m_propertyNames.removeAll(t: QLatin1String("turtle"));
91
92 if (error != QGalleryAbstractRequest::NoError)
93 QGalleryAbstractResponse::error(error, errorString);
94 else if (state == QGalleryAbstractRequest::Finished)
95 finish();
96 else if (state == QGalleryAbstractRequest::Idle)
97 finish(idle: true);
98 }
99
100 int propertyKey(const QString &propertyName) const {
101 return m_propertyNames.indexOf(t: propertyName); }
102 QGalleryProperty::Attributes propertyAttributes(int) const {
103 return QGalleryProperty::CanRead | QGalleryProperty::CanWrite; }
104 QVariant::Type propertyType(int) const { return QVariant::String; }
105
106 int itemCount() const { return m_count; }
107
108 int currentIndex() const { return m_currentIndex; }
109
110 bool fetch(int index)
111 {
112 emit currentIndexChanged(index: m_currentIndex = index);
113 emit currentItemChanged();
114
115 return isValid();
116 }
117
118 QVariant itemId() const { return isValid() ? QVariant(1) : QVariant(); }
119 QUrl itemUrl() const { return isValid() ? QUrl("http://example.com") : QUrl(); }
120 QString itemType() const { return isValid() ? QLatin1String("WebPage") : QString(); }
121
122 QVariant metaData(int key) const { return isValid() ? m_metaData.value(key) : QVariant(); }
123 bool setMetaData(int key, const QVariant &value)
124 {
125 if (isValid()) {
126 m_metaData.insert(key, value);
127 emit metaDataChanged(index: m_currentIndex, count: 1, keys: QList<int>() << key);
128 return true;
129 } else {
130 return false;
131 }
132 }
133
134 void setCount(int count) { m_count = count; }
135
136 using QGalleryResultSet::finish;
137 using QGalleryResultSet::resume;
138 using QGalleryResultSet::cancel;
139 using QGalleryResultSet::error;
140 using QGalleryResultSet::itemsInserted;
141 using QGalleryResultSet::itemsRemoved;
142 using QGalleryResultSet::itemsMoved;
143 using QGalleryResultSet::metaDataChanged;
144
145private:
146 int m_count;
147 int m_currentIndex;
148 QStringList m_propertyNames;
149 QHash<int, QVariant> m_metaData;
150};
151
152class QtTestGallery : public QAbstractGallery
153{
154public:
155 QtTestGallery()
156 : m_count(0)
157 , m_state(QGalleryAbstractRequest::Active)
158 , m_error(QGalleryAbstractRequest::NoError)
159 {}
160
161 bool isRequestSupported(QGalleryAbstractRequest::RequestType type) const {
162 return type == QGalleryAbstractRequest::ItemRequest; }
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 setCount(int count) { m_count = count; }
169
170protected:
171 QGalleryAbstractResponse *createResponse(QGalleryAbstractRequest *request)
172 {
173 if (request->type() == QGalleryAbstractRequest::ItemRequest) {
174 return new QtGalleryTestResponse(
175 static_cast<QGalleryItemRequest *>(request)->propertyNames(),
176 m_count,
177 m_state,
178 m_error,
179 m_errorString);
180 }
181 return 0;
182 }
183
184private:
185 int m_count;
186 QGalleryAbstractRequest::State m_state;
187 int m_error;
188 QString m_errorString;
189};
190
191void tst_QGalleryItemRequest::initTestCase()
192{
193 qRegisterMetaType<QGalleryResultSet*>();
194 qRegisterMetaType<QList<int> >();
195}
196
197void tst_QGalleryItemRequest::propertyNames()
198{
199 const QGalleryProperty titleProperty = {.m_name: "title", .m_length: sizeof("title")};
200 const QGalleryProperty artistProperty = {.m_name: "artist", .m_length: sizeof("artist")};
201
202 const QStringList propertyNames = QStringList()
203 << titleProperty
204 << artistProperty.name()
205 << QLatin1String("album")
206 << QLatin1String("trackNumber");
207
208 QGalleryItemRequest request;
209
210 QSignalSpy spy(&request, SIGNAL(propertyNamesChanged()));
211
212 QCOMPARE(request.propertyNames(), QStringList());
213
214 request.setPropertyNames(QStringList());
215 QCOMPARE(request.propertyNames(), QStringList());
216 QCOMPARE(spy.count(), 0);
217
218 request.setPropertyNames(propertyNames);
219 QCOMPARE(request.propertyNames(), propertyNames);
220 QCOMPARE(spy.count(), 1);
221
222 request.setPropertyNames(propertyNames);
223 QCOMPARE(request.propertyNames(), propertyNames);
224 QCOMPARE(spy.count(), 1);
225
226 request.setPropertyNames(QStringList());
227 QCOMPARE(request.propertyNames(), QStringList());
228 QCOMPARE(spy.count(), 2);
229}
230
231void tst_QGalleryItemRequest::autoUpdate()
232{
233 QGalleryItemRequest request;
234
235 QSignalSpy spy(&request, SIGNAL(autoUpdateChanged()));
236
237 QCOMPARE(request.autoUpdate(), false);
238
239 request.setAutoUpdate(false);
240 QCOMPARE(request.autoUpdate(), false);
241 QCOMPARE(spy.count(), 0);
242
243 request.setAutoUpdate(true);
244 QCOMPARE(request.autoUpdate(), true);
245 QCOMPARE(spy.count(), 1);
246
247 request.setAutoUpdate(true);
248 QCOMPARE(request.autoUpdate(), true);
249 QCOMPARE(spy.count(), 1);
250
251 request.setAutoUpdate(false);
252 QCOMPARE(request.autoUpdate(), false);
253 QCOMPARE(spy.count(), 2);
254}
255
256void tst_QGalleryItemRequest::itemId()
257{
258 QGalleryItemRequest request;
259
260 QSignalSpy spy(&request, SIGNAL(itemIdChanged()));
261
262 QCOMPARE(request.itemId(), QVariant());
263
264 request.setItemId(QVariant());
265 QCOMPARE(request.itemId(), QVariant());
266 QCOMPARE(spy.count(), 0);
267
268 request.setItemId(76);
269 QCOMPARE(request.itemId(), QVariant(76));
270 QCOMPARE(spy.count(), 1);
271
272 request.setItemId(76);
273 QCOMPARE(request.itemId(), QVariant(76));
274 QCOMPARE(spy.count(), 1);
275
276 request.setItemId(QLatin1String("65"));
277 QCOMPARE(request.itemId(), QVariant(QLatin1String("65")));
278 QCOMPARE(spy.count(), 2);
279
280 request.setItemId(QLatin1String("65"));
281 QCOMPARE(request.itemId(), QVariant(QLatin1String("65")));
282 QCOMPARE(spy.count(), 2);
283
284 request.setItemId(QVariant());
285 QCOMPARE(request.itemId(), QVariant());
286 QCOMPARE(spy.count(), 3);
287}
288
289void tst_QGalleryItemRequest::executeSynchronous()
290{
291 QtTestGallery gallery;
292 gallery.setState(QGalleryAbstractRequest::Finished);
293 gallery.setCount(1);
294 gallery.setError(error: 80, errorString: QString());
295
296 QGalleryItemRequest request(&gallery);
297 QVERIFY(request.resultSet() == 0);
298
299 request.setPropertyNames(QStringList()
300 << QLatin1String("album")
301 << QLatin1String("trackNumber")
302 << QLatin1String("turtle"));
303
304 QSignalSpy resultSetSpy(&request, SIGNAL(resultSetChanged(QGalleryResultSet*)));
305 QSignalSpy itemChangedSpy(&request, SIGNAL(itemChanged()));
306 QSignalSpy metaDataSpy(&request, SIGNAL(metaDataChanged(QList<int>)));
307
308 request.execute();
309
310 QCOMPARE(request.error(), 80);
311 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
312 QCOMPARE(resultSetSpy.count(), 0);
313 QVERIFY(qobject_cast<QtGalleryTestResponse *>(request.resultSet()) == 0);
314
315 gallery.setError(error: QGalleryAbstractRequest::NoError, errorString: QString());
316 request.execute();
317 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
318 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
319 QCOMPARE(resultSetSpy.count(), 1);
320 QVERIFY(qobject_cast<QtGalleryTestResponse *>(request.resultSet()) != 0);
321 QCOMPARE(resultSetSpy.last().at(0).value<QGalleryResultSet*>(), request.resultSet());
322
323 QCOMPARE(request.propertyKey(QLatin1String("title")), -1);
324 QCOMPARE(request.propertyKey(QLatin1String("album")), 0);
325 QCOMPARE(request.propertyKey(QLatin1String("trackNumber")), 1);
326 QCOMPARE(request.propertyKey(QLatin1String("turtle")), -1);
327
328 QCOMPARE(request.propertyAttributes(0), QGalleryProperty::CanRead | QGalleryProperty::CanWrite);
329 QCOMPARE(request.propertyType(0), QVariant::String);
330
331 const QList<int> propertyKeys = QList<int>()
332 << request.propertyKey(property: QLatin1String("album"))
333 << request.propertyKey(property: QLatin1String("trackNumber"));
334
335 QCOMPARE(itemChangedSpy.count(), 1);
336 QCOMPARE(metaDataSpy.count(), 1);
337 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
338
339 QCOMPARE(request.isValid(), true);
340 QCOMPARE(request.itemUrl(), QUrl(QLatin1String("http://example.com")));
341 QCOMPARE(request.itemType(), QLatin1String("WebPage"));
342 QCOMPARE(request.metaData(1), QVariant());
343 QCOMPARE(request.setMetaData(1, 12), true);
344 QCOMPARE(request.metaData(1), QVariant(12));
345 QCOMPARE(metaDataSpy.count(), 2);
346 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant(12));
347 QCOMPARE(request.setMetaData(QLatin1String("trackNumber"), 5), true);
348 QCOMPARE(metaDataSpy.count(), 3);
349 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant(5));
350 QCOMPARE(request.resources(), QList<QGalleryResource>()
351 << QGalleryResource(QUrl(QLatin1String("http://example.com"))));
352
353 request.clear();
354 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
355 QCOMPARE(resultSetSpy.count(), 2);
356 QVERIFY(request.resultSet() == 0);
357 QCOMPARE(resultSetSpy.last().at(0).value<QGalleryResultSet*>(), request.resultSet());
358 QCOMPARE(itemChangedSpy.count(), 2);
359 QCOMPARE(metaDataSpy.count(), 3);
360}
361
362void tst_QGalleryItemRequest::executeAsynchronous()
363{
364 QtTestGallery gallery;
365 gallery.setState(QGalleryAbstractRequest::Active);
366
367 QGalleryItemRequest request(&gallery);
368 QVERIFY(request.resultSet() == 0);
369
370 request.setPropertyNames(QStringList()
371 << QLatin1String("album")
372 << QLatin1String("trackNumber")
373 << QLatin1String("turtle"));
374
375 QSignalSpy resultSetSpy(&request, SIGNAL(resultSetChanged(QGalleryResultSet*)));
376 QSignalSpy itemChangedSpy(&request, SIGNAL(itemChanged()));
377 QSignalSpy metaDataSpy(&request, SIGNAL(metaDataChanged(QList<int>)));
378
379 request.execute();
380 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
381 QCOMPARE(resultSetSpy.count(), 1);
382 QVERIFY(qobject_cast<QtGalleryTestResponse *>(request.resultSet()) != 0);
383 QCOMPARE(resultSetSpy.last().at(0).value<QGalleryResultSet*>(), request.resultSet());
384
385 QCOMPARE(request.propertyKey(QLatin1String("title")), -1);
386 QCOMPARE(request.propertyKey(QLatin1String("album")), 0);
387 QCOMPARE(request.propertyKey(QLatin1String("trackNumber")), 1);
388 QCOMPARE(request.propertyKey(QLatin1String("turtle")), -1);
389
390 QCOMPARE(request.propertyAttributes(0), QGalleryProperty::CanRead | QGalleryProperty::CanWrite);
391 QCOMPARE(request.propertyType(0), QVariant::String);
392
393 const QList<int> propertyKeys = QList<int>()
394 << request.propertyKey(property: QLatin1String("album"))
395 << request.propertyKey(property: QLatin1String("trackNumber"));
396
397 QCOMPARE(itemChangedSpy.count(), 0);
398 QCOMPARE(metaDataSpy.count(), 0);
399
400 QCOMPARE(request.isValid(), false);
401 QCOMPARE(request.itemUrl(), QUrl());
402 QCOMPARE(request.itemType(), QString());
403 QCOMPARE(request.metaData(1), QVariant());
404 QCOMPARE(request.setMetaData(1, 12), false);
405 QCOMPARE(request.metaData(1), QVariant());
406 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant());
407 QCOMPARE(request.setMetaData(QLatin1String("trackNumber"), 12), false);
408 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant());
409 QCOMPARE(request.resources(), QList<QGalleryResource>());
410
411 QtGalleryTestResponse *resultSet = qobject_cast<QtGalleryTestResponse *>(object: request.resultSet());
412 QVERIFY(resultSet != 0);
413
414 resultSet->setCount(1);
415 resultSet->itemsInserted(index: 0, count: 1);
416
417 QCOMPARE(itemChangedSpy.count(), 1);
418 QCOMPARE(metaDataSpy.count(), 1);
419 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
420
421 QCOMPARE(request.isValid(), true);
422 QCOMPARE(request.itemUrl(), QUrl(QLatin1String("http://example.com")));
423 QCOMPARE(request.itemType(), QLatin1String("WebPage"));
424 QCOMPARE(request.metaData(1), QVariant());
425 QCOMPARE(request.setMetaData(1, 12), true);
426 QCOMPARE(metaDataSpy.count(), 2);
427 QCOMPARE(request.metaData(1), QVariant(12));
428 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant(12));
429 QCOMPARE(request.setMetaData(QLatin1String("trackNumber"), 5), true);
430 QCOMPARE(metaDataSpy.count(), 3);
431 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant(5));
432 QCOMPARE(request.resources(), QList<QGalleryResource>()
433 << QGalleryResource(QUrl(QLatin1String("http://example.com"))));
434
435 resultSet->finish(idle: false);
436
437 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
438 QCOMPARE(resultSetSpy.count(), 1);
439 QVERIFY(qobject_cast<QtGalleryTestResponse *>(request.resultSet()) != 0);
440
441 resultSet->setCount(0);
442 resultSet->itemsRemoved(index: 0, count: 1);
443
444 QCOMPARE(itemChangedSpy.count(), 2);
445 QCOMPARE(metaDataSpy.count(), 4);
446 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
447
448 QCOMPARE(request.isValid(), false);
449 QCOMPARE(request.itemUrl(), QUrl());
450 QCOMPARE(request.itemType(), QString());
451 QCOMPARE(request.metaData(1), QVariant());
452 QCOMPARE(request.setMetaData(1, 12), false);
453 QCOMPARE(request.metaData(1), QVariant());
454 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant());
455 QCOMPARE(request.setMetaData(QLatin1String("trackNumber"), 12), false);
456 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant());
457 QCOMPARE(request.resources(), QList<QGalleryResource>());
458
459 request.clear();
460 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
461 QCOMPARE(resultSetSpy.count(), 2);
462 QVERIFY(request.resultSet() == 0);
463 QCOMPARE(resultSetSpy.last().at(0).value<QGalleryResultSet*>(), request.resultSet());
464
465 QCOMPARE(itemChangedSpy.count(), 2);
466 QCOMPARE(metaDataSpy.count(), 4);
467}
468
469void tst_QGalleryItemRequest::noResponse()
470{
471 QGalleryItemRequest request;
472
473 QCOMPARE(request.propertyKey(QLatin1String("title")), -1);
474 QCOMPARE(request.propertyKey(QLatin1String("album")), -1);
475 QCOMPARE(request.propertyKey(QLatin1String("trackNumber")), -1);
476
477 QCOMPARE(request.propertyAttributes(0), QGalleryProperty::Attributes());
478 QCOMPARE(request.propertyType(0), QVariant::Invalid);
479
480 QCOMPARE(request.isValid(), false);
481 QCOMPARE(request.itemUrl(), QUrl());
482 QCOMPARE(request.itemType(), QString());
483 QCOMPARE(request.metaData(1), QVariant());
484 QCOMPARE(request.setMetaData(1, QLatin1String("hello")), false);
485 QCOMPARE(request.metaData(1), QVariant());
486 QCOMPARE(request.metaData(QLatin1String("title")), QVariant());
487 QCOMPARE(request.setMetaData(QLatin1String("title"), QLatin1String("hello")), false);
488 QCOMPARE(request.metaData(QLatin1String("title")), QVariant());
489 QCOMPARE(request.resources(), QList<QGalleryResource>());
490}
491
492void tst_QGalleryItemRequest::multipleResults()
493{
494 const QList<int> propertyKeys = QList<int>() << 2 << 15;
495
496 QtTestGallery gallery;
497 gallery.setCount(1);
498
499 QGalleryItemRequest request(&gallery);
500
501 QSignalSpy itemChangedSpy(&request, SIGNAL(itemChanged()));
502 QSignalSpy metaDataSpy(&request, SIGNAL(metaDataChanged(QList<int>)));
503
504 gallery.setState(QGalleryAbstractRequest::Active);
505 request.execute();
506
507 QCOMPARE(request.isValid(), true);
508 QCOMPARE(itemChangedSpy.count(), 1);
509 QCOMPARE(metaDataSpy.count(), 0);
510
511 QtGalleryTestResponse *resultSet = qobject_cast<QtGalleryTestResponse *>(
512 object: request.resultSet());
513 QVERIFY(resultSet != 0);
514
515 resultSet->metaDataChanged(index: 0, count: 1, keys: propertyKeys);
516 QCOMPARE(metaDataSpy.count(), 1);
517 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
518
519 resultSet->setCount(3);
520 resultSet->itemsInserted(index: 1, count: 2);
521
522 QCOMPARE(itemChangedSpy.count(), 1);
523 QCOMPARE(metaDataSpy.count(), 1);
524
525 resultSet->metaDataChanged(index: 0, count: 1, keys: propertyKeys);
526 QCOMPARE(metaDataSpy.count(), 2);
527 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
528
529 resultSet->metaDataChanged(index: 1, count: 1, keys: propertyKeys);
530 QCOMPARE(metaDataSpy.count(), 2);
531
532 resultSet->itemsMoved(from: 0, to: 1, count: 1);
533 QCOMPARE(request.isValid(), true);
534 QCOMPARE(itemChangedSpy.count(), 2);
535 QCOMPARE(metaDataSpy.count(), 2);
536
537 resultSet->itemsMoved(from: 2, to: 0, count: 1);
538 QCOMPARE(request.isValid(), true);
539 QCOMPARE(itemChangedSpy.count(), 3);
540 QCOMPARE(metaDataSpy.count(), 2);
541
542 resultSet->itemsMoved(from: 1, to: 2, count: 1);
543 QCOMPARE(request.isValid(), true);
544 QCOMPARE(itemChangedSpy.count(), 3);
545 QCOMPARE(metaDataSpy.count(), 2);
546
547 resultSet->setCount(1);
548 resultSet->itemsRemoved(index: 1, count: 1);
549
550 QCOMPARE(request.isValid(), true);
551 QCOMPARE(itemChangedSpy.count(), 3);
552 QCOMPARE(metaDataSpy.count(), 2);
553
554}
555
556QTEST_MAIN(tst_QGalleryItemRequest)
557
558#include "tst_qgalleryitemrequest.moc"
559

source code of qtdocgallery/tests/auto/qgalleryitemrequest/tst_qgalleryitemrequest.cpp