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 <qgallerytyperequest.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_QGalleryTypeRequest : public QObject
61{
62 Q_OBJECT
63public Q_SLOTS:
64 void initTestCase();
65
66private Q_SLOTS:
67 void propertyNames();
68 void autoUpdate();
69 void itemType();
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::itemsInserted;
139 using QGalleryResultSet::itemsRemoved;
140 using QGalleryResultSet::itemsMoved;
141 using QGalleryResultSet::metaDataChanged;
142
143private:
144 int m_count;
145 int m_currentIndex;
146 QStringList m_propertyNames;
147 QHash<int, QVariant> m_metaData;
148};
149
150class QtTestGallery : public QAbstractGallery
151{
152public:
153 QtTestGallery()
154 : m_count(0)
155 , m_state(QGalleryAbstractRequest::Active)
156 , m_error(QGalleryAbstractRequest::NoError)
157 {}
158
159 bool isRequestSupported(QGalleryAbstractRequest::RequestType type) const {
160 return type == QGalleryAbstractRequest::TypeRequest; }
161
162 void setState(QGalleryAbstractRequest::State state) { m_state = state; }
163 void setError(int error, const QString &errorString) {
164 m_error = error; m_errorString = errorString; }
165
166 void setCount(int count) { m_count = count; }
167
168protected:
169 QGalleryAbstractResponse *createResponse(QGalleryAbstractRequest *request)
170 {
171 if (request->type() == QGalleryAbstractRequest::TypeRequest) {
172 return new QtGalleryTestResponse(
173 static_cast<QGalleryTypeRequest *>(request)->propertyNames(),
174 m_count,
175 m_state,
176 m_error,
177 m_errorString);
178 }
179 return 0;
180 }
181
182private:
183 int m_count;
184 QGalleryAbstractRequest::State m_state;
185 int m_error;
186 QString m_errorString;
187};
188
189void tst_QGalleryTypeRequest::initTestCase()
190{
191 qRegisterMetaType<QGalleryResultSet*>();
192 qRegisterMetaType<QList<int> >();
193}
194
195void tst_QGalleryTypeRequest::propertyNames()
196{
197 const QGalleryProperty titleProperty = {.m_name: "title", .m_length: sizeof("title")};
198 const QGalleryProperty artistProperty = {.m_name: "artist", .m_length: sizeof("artist")};
199
200 const QStringList propertyNames = QStringList()
201 << titleProperty
202 << artistProperty.name()
203 << QLatin1String("album")
204 << QLatin1String("trackNumber");
205
206 QGalleryTypeRequest request;
207
208 QSignalSpy spy(&request, SIGNAL(propertyNamesChanged()));
209
210 QCOMPARE(request.propertyNames(), QStringList());
211
212 request.setPropertyNames(QStringList());
213 QCOMPARE(request.propertyNames(), QStringList());
214 QCOMPARE(spy.count(), 0);
215
216 request.setPropertyNames(propertyNames);
217 QCOMPARE(request.propertyNames(), propertyNames);
218 QCOMPARE(spy.count(), 1);
219
220 request.setPropertyNames(propertyNames);
221 QCOMPARE(request.propertyNames(), propertyNames);
222 QCOMPARE(spy.count(), 1);
223
224 request.setPropertyNames(QStringList());
225 QCOMPARE(request.propertyNames(), QStringList());
226 QCOMPARE(spy.count(), 2);
227}
228
229void tst_QGalleryTypeRequest::autoUpdate()
230{
231 QGalleryTypeRequest request;
232
233 QSignalSpy spy(&request, SIGNAL(autoUpdateChanged()));
234
235 QCOMPARE(request.autoUpdate(), false);
236
237 request.setAutoUpdate(false);
238 QCOMPARE(request.autoUpdate(), false);
239 QCOMPARE(spy.count(), 0);
240
241 request.setAutoUpdate(true);
242 QCOMPARE(request.autoUpdate(), true);
243 QCOMPARE(spy.count(), 1);
244
245 request.setAutoUpdate(true);
246 QCOMPARE(request.autoUpdate(), true);
247 QCOMPARE(spy.count(), 1);
248
249 request.setAutoUpdate(false);
250 QCOMPARE(request.autoUpdate(), false);
251 QCOMPARE(spy.count(), 2);
252}
253
254void tst_QGalleryTypeRequest::itemType()
255{
256 const QString itemType = QLatin1String("Audio");
257
258 QGalleryTypeRequest request;
259
260 QSignalSpy spy(&request, SIGNAL(itemTypeChanged()));
261
262 QCOMPARE(request.itemType(), QString());
263
264 request.setItemType(QString());
265 QCOMPARE(request.itemType(), QString());
266 QCOMPARE(spy.count(), 0);
267
268 request.setItemType(itemType);
269 QCOMPARE(request.itemType(), itemType);
270 QCOMPARE(spy.count(), 1);
271
272 request.setItemType(itemType);
273 QCOMPARE(request.itemType(), itemType);
274 QCOMPARE(spy.count(), 1);
275
276 request.setItemType(QString());
277 QCOMPARE(request.itemType(), QString());
278 QCOMPARE(spy.count(), 2);
279}
280
281void tst_QGalleryTypeRequest::executeSynchronous()
282{
283 QtTestGallery gallery;
284 gallery.setState(QGalleryAbstractRequest::Finished);
285 gallery.setCount(1);
286 gallery.setError(error: 80, errorString: QString());
287
288 QGalleryTypeRequest request(&gallery);
289 QVERIFY(request.resultSet() == 0);
290
291 request.setPropertyNames(QStringList()
292 << QLatin1String("album")
293 << QLatin1String("trackNumber")
294 << QLatin1String("turtle"));
295
296 QSignalSpy resultSetSpy(&request, SIGNAL(resultSetChanged(QGalleryResultSet*)));
297 QSignalSpy typeChangedSpy(&request, SIGNAL(typeChanged()));
298 QSignalSpy metaDataSpy(&request, SIGNAL(metaDataChanged(QList<int>)));
299
300 request.execute();
301
302 QCOMPARE(request.error(), 80);
303 QCOMPARE(request.state(), QGalleryAbstractRequest::Error);
304 QCOMPARE(resultSetSpy.count(), 0);
305 QVERIFY(qobject_cast<QtGalleryTestResponse *>(request.resultSet()) == 0);
306
307 gallery.setError(error: QGalleryAbstractRequest::NoError, errorString: QString());
308 request.execute();
309 QCOMPARE(request.error(), int(QGalleryAbstractRequest::NoError));
310 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
311 QCOMPARE(resultSetSpy.count(), 1);
312 QVERIFY(qobject_cast<QtGalleryTestResponse *>(request.resultSet()) != 0);
313 QCOMPARE(resultSetSpy.last().at(0).value<QGalleryResultSet*>(), request.resultSet());
314
315 QtGalleryTestResponse *resultSet = qobject_cast<QtGalleryTestResponse *>(
316 object: request.resultSet());
317 QVERIFY(resultSet != 0);
318
319 QCOMPARE(request.propertyKey(QLatin1String("title")), -1);
320 QCOMPARE(request.propertyKey(QLatin1String("album")), 0);
321 QCOMPARE(request.propertyKey(QLatin1String("trackNumber")), 1);
322 QCOMPARE(request.propertyKey(QLatin1String("turtle")), -1);
323
324 QCOMPARE(request.propertyAttributes(0), QGalleryProperty::CanRead | QGalleryProperty::CanWrite);
325 QCOMPARE(request.propertyType(0), QVariant::String);
326
327 const QList<int> propertyKeys = QList<int>()
328 << request.propertyKey(property: QLatin1String("album"))
329 << request.propertyKey(property: QLatin1String("trackNumber"));
330
331 QCOMPARE(typeChangedSpy.count(), 1);
332 QCOMPARE(metaDataSpy.count(), 1);
333 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
334
335 QCOMPARE(request.isValid(), true);
336 QCOMPARE(request.metaData(1), QVariant());
337 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant());
338 QCOMPARE(resultSet->setMetaData(1, 12), true);
339 QCOMPARE(request.metaData(1), QVariant(12));
340 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant(12));
341 QCOMPARE(metaDataSpy.count(), 2);
342
343 request.clear();
344 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
345 QCOMPARE(resultSetSpy.count(), 2);
346 QVERIFY(request.resultSet() == 0);
347 QCOMPARE(resultSetSpy.last().at(0).value<QGalleryResultSet*>(), request.resultSet());
348 QCOMPARE(typeChangedSpy.count(), 2);
349 QCOMPARE(metaDataSpy.count(), 2);
350}
351
352void tst_QGalleryTypeRequest::executeAsynchronous()
353{
354 QtTestGallery gallery;
355 gallery.setState(QGalleryAbstractRequest::Active);
356
357 QGalleryTypeRequest request(&gallery);
358 QVERIFY(request.resultSet() == 0);
359
360 request.setPropertyNames(QStringList()
361 << QLatin1String("album")
362 << QLatin1String("trackNumber")
363 << QLatin1String("turtle"));
364
365 QSignalSpy resultSetSpy(&request, SIGNAL(resultSetChanged(QGalleryResultSet*)));
366 QSignalSpy typeChangedSpy(&request, SIGNAL(typeChanged()));
367 QSignalSpy metaDataSpy(&request, SIGNAL(metaDataChanged(QList<int>)));
368
369 request.execute();
370 QCOMPARE(request.state(), QGalleryAbstractRequest::Active);
371 QCOMPARE(resultSetSpy.count(), 1);
372 QVERIFY(qobject_cast<QtGalleryTestResponse *>(request.resultSet()) != 0);
373 QCOMPARE(resultSetSpy.last().at(0).value<QGalleryResultSet*>(), request.resultSet());
374
375 QtGalleryTestResponse *resultSet = qobject_cast<QtGalleryTestResponse *>(
376 object: request.resultSet());
377 QVERIFY(resultSet != 0);
378
379 QCOMPARE(request.propertyKey(QLatin1String("title")), -1);
380 QCOMPARE(request.propertyKey(QLatin1String("album")), 0);
381 QCOMPARE(request.propertyKey(QLatin1String("trackNumber")), 1);
382 QCOMPARE(request.propertyKey(QLatin1String("turtle")), -1);
383
384 QCOMPARE(request.propertyAttributes(0), QGalleryProperty::CanRead | QGalleryProperty::CanWrite);
385 QCOMPARE(request.propertyType(0), QVariant::String);
386
387 const QList<int> propertyKeys = QList<int>()
388 << request.propertyKey(property: QLatin1String("album"))
389 << request.propertyKey(property: QLatin1String("trackNumber"));
390
391 QCOMPARE(typeChangedSpy.count(), 0);
392 QCOMPARE(metaDataSpy.count(), 0);
393
394 QCOMPARE(request.isValid(), false);
395 QCOMPARE(request.metaData(1), QVariant());
396
397 resultSet->setCount(1);
398 resultSet->itemsInserted(index: 0, count: 1);
399
400 QCOMPARE(typeChangedSpy.count(), 1);
401 QCOMPARE(metaDataSpy.count(), 1);
402 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
403
404 QCOMPARE(request.isValid(), true);
405 QCOMPARE(request.metaData(1), QVariant());
406 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant());
407 QCOMPARE(resultSet->setMetaData(1, 12), true);
408 QCOMPARE(request.metaData(1), QVariant(12));
409 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant(12));
410 QCOMPARE(metaDataSpy.count(), 2);
411
412 resultSet->finish(idle: false);
413
414 QCOMPARE(request.state(), QGalleryAbstractRequest::Finished);
415 QCOMPARE(resultSetSpy.count(), 1);
416 QVERIFY(qobject_cast<QtGalleryTestResponse *>(request.resultSet()) != 0);
417
418 resultSet->setCount(0);
419 resultSet->itemsRemoved(index: 0, count: 1);
420
421 QCOMPARE(typeChangedSpy.count(), 2);
422 QCOMPARE(metaDataSpy.count(), 3);
423 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
424
425 QCOMPARE(request.isValid(), false);
426 QCOMPARE(request.metaData(1), QVariant());
427 QCOMPARE(request.metaData(QLatin1String("trackNumber")), QVariant());
428
429
430 request.clear();
431 QCOMPARE(request.state(), QGalleryAbstractRequest::Inactive);
432 QCOMPARE(resultSetSpy.count(), 2);
433 QVERIFY(request.resultSet() == 0);
434 QCOMPARE(resultSetSpy.last().at(0).value<QGalleryResultSet*>(), request.resultSet());
435
436 QCOMPARE(typeChangedSpy.count(), 2);
437 QCOMPARE(metaDataSpy.count(), 3);
438}
439
440void tst_QGalleryTypeRequest::noResponse()
441{
442 QGalleryTypeRequest request;
443
444 QCOMPARE(request.propertyKey(QLatin1String("title")), -1);
445 QCOMPARE(request.propertyKey(QLatin1String("album")), -1);
446 QCOMPARE(request.propertyKey(QLatin1String("trackNumber")), -1);
447
448 QCOMPARE(request.propertyAttributes(0), QGalleryProperty::Attributes());
449 QCOMPARE(request.propertyType(0), QVariant::Invalid);
450
451 QCOMPARE(request.isValid(), false);
452 QCOMPARE(request.metaData(1), QVariant());
453 QCOMPARE(request.metaData(QLatin1String("title")), QVariant());
454}
455
456void tst_QGalleryTypeRequest::multipleResults()
457{
458 const QList<int> propertyKeys = QList<int>() << 2 << 15;
459
460 QtTestGallery gallery;
461 gallery.setCount(1);
462
463 QGalleryTypeRequest request(&gallery);
464
465 QSignalSpy typeChangedSpy(&request, SIGNAL(typeChanged()));
466 QSignalSpy metaDataSpy(&request, SIGNAL(metaDataChanged(QList<int>)));
467
468 request.execute();
469
470 QCOMPARE(request.isValid(), true);
471 QCOMPARE(typeChangedSpy.count(), 1);
472 QCOMPARE(metaDataSpy.count(), 0);
473
474 QtGalleryTestResponse *resultSet = qobject_cast<QtGalleryTestResponse *>(
475 object: request.resultSet());
476 QVERIFY(resultSet != 0);
477
478 resultSet->metaDataChanged(index: 0, count: 1, keys: propertyKeys);
479 QCOMPARE(metaDataSpy.count(), 1);
480 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
481
482 resultSet->setCount(3);
483 resultSet->itemsInserted(index: 1, count: 2);
484
485 QCOMPARE(typeChangedSpy.count(), 1);
486 QCOMPARE(metaDataSpy.count(), 1);
487
488 resultSet->metaDataChanged(index: 0, count: 1, keys: propertyKeys);
489 QCOMPARE(metaDataSpy.count(), 2);
490 QCOMPARE(metaDataSpy.last().value(0).value<QList<int> >(), propertyKeys);
491
492 resultSet->metaDataChanged(index: 1, count: 1, keys: propertyKeys);
493 QCOMPARE(metaDataSpy.count(), 2);
494
495 resultSet->itemsMoved(from: 0, to: 1, count: 1);
496 QCOMPARE(request.isValid(), true);
497 QCOMPARE(typeChangedSpy.count(), 2);
498 QCOMPARE(metaDataSpy.count(), 2);
499
500 resultSet->itemsMoved(from: 2, to: 0, count: 1);
501 QCOMPARE(request.isValid(), true);
502 QCOMPARE(typeChangedSpy.count(), 3);
503 QCOMPARE(metaDataSpy.count(), 2);
504
505 resultSet->itemsMoved(from: 1, to: 2, count: 1);
506 QCOMPARE(request.isValid(), true);
507 QCOMPARE(typeChangedSpy.count(), 3);
508 QCOMPARE(metaDataSpy.count(), 2);
509
510 resultSet->setCount(1);
511 resultSet->itemsRemoved(index: 1, count: 1);
512
513 QCOMPARE(request.isValid(), true);
514 QCOMPARE(typeChangedSpy.count(), 3);
515 QCOMPARE(metaDataSpy.count(), 2);
516}
517
518QTEST_MAIN(tst_QGalleryTypeRequest)
519
520#include "tst_qgallerytyperequest.moc"
521

source code of qtdocgallery/tests/auto/qgallerytyperequest/tst_qgallerytyperequest.cpp