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