mpdata/tsrc/unittest_mpcollectionalbumartmanager/src/unittest_mpcollectionalbumartmanager.cpp
changeset 22 ecf06a08d4d9
child 29 8192e5b5c935
equal deleted inserted replaced
20:82baf59ce8dd 22:ecf06a08d4d9
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Unit test for mpnowplayingbackend
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QSignalSpy>
       
    19 #include <hbapplication.h>
       
    20 #include <hbmainwindow.h>
       
    21 #include <hbicon.h>
       
    22 
       
    23 #include "unittest_mpcollectionalbumartmanager.h"
       
    24 #include "stub/inc/thumbnailmanager_qt.h"
       
    25 #include "stub/inc/mpmpxcollectiondata.h"
       
    26 
       
    27 // Do this so we can access all member variables.
       
    28 #define private public
       
    29 #include "mpcollectionalbumartmanager.h"
       
    30 #undef private
       
    31 
       
    32 /*!
       
    33  Make our test case a stand-alone executable that runs all the test functions.
       
    34  */
       
    35 int main(int argc, char *argv[])
       
    36 {
       
    37     HbApplication app(argc, argv);
       
    38     HbMainWindow window;
       
    39 
       
    40     TestMpCollectionAlbumArtManager tv;
       
    41 
       
    42     char *pass[3];
       
    43     pass[0] = argv[0];
       
    44     pass[1] = "-o";
       
    45     pass[2] = "c:\\data\\unittest_mpcollectionalbumartmanager.txt";
       
    46 
       
    47     int res = QTest::qExec(&tv, 3, pass);
       
    48 
       
    49     return res;
       
    50 }
       
    51 
       
    52 TestMpCollectionAlbumArtManager::TestMpCollectionAlbumArtManager()
       
    53     : mTest(0),
       
    54       mStubData(0)
       
    55 {
       
    56 }
       
    57 
       
    58 TestMpCollectionAlbumArtManager::~TestMpCollectionAlbumArtManager()
       
    59 {
       
    60     delete mTest;
       
    61     delete mStubData;
       
    62 }
       
    63 
       
    64 /*!
       
    65  Called before the first testfunction is executed.
       
    66  */
       
    67 void TestMpCollectionAlbumArtManager::initTestCase()
       
    68 {
       
    69     mStubData = new MpMpxCollectionData();
       
    70 
       
    71     // Create an icon (any icon will do) to help with testing.
       
    72     mIcon = QIcon(":/testicons/default_album.png");
       
    73 }
       
    74 
       
    75 /*!
       
    76  Called after the last testfunction was executed.
       
    77  */
       
    78 void TestMpCollectionAlbumArtManager::cleanupTestCase()
       
    79 {
       
    80 }
       
    81 
       
    82 /*!
       
    83  Called before each testfunction is executed.
       
    84  */
       
    85 void TestMpCollectionAlbumArtManager::init()
       
    86 {
       
    87     mTest = new MpCollectionAlbumArtManager(mStubData);
       
    88     mStubData->mCount = 100;
       
    89     mStubData->mItemDataReturn = true;
       
    90 }
       
    91 
       
    92 /*!
       
    93  Called after every testfunction.
       
    94  */
       
    95 void TestMpCollectionAlbumArtManager::cleanup()
       
    96 {
       
    97     delete mTest;
       
    98     mTest = 0;
       
    99 }
       
   100 
       
   101 /*!
       
   102  Tests correct cleanup of member variables.
       
   103  */
       
   104 void TestMpCollectionAlbumArtManager::testMemberCleanup()
       
   105 {
       
   106     cleanup();
       
   107     ThumbnailManager::resetInitCounter();
       
   108     init();
       
   109     cleanup();
       
   110     QCOMPARE(ThumbnailManager::getInitCounter(), 0);
       
   111 }
       
   112 
       
   113 /*!
       
   114  Tests albumArt() request when album art is not in local cache.
       
   115  Single request.
       
   116  */
       
   117 void TestMpCollectionAlbumArtManager::testAlbumArtNoCache()
       
   118 {
       
   119     const QIcon* icon = mTest->albumArt(1);
       
   120     // Verify that:
       
   121     // - It returned default icon
       
   122     // - A request has been made to thumbnail manager
       
   123     QVERIFY(icon->isNull() == false);
       
   124     QCOMPARE(mTest->mPendingRequest, true);
       
   125     QCOMPARE(mTest->mThumbnailManager->mThumbnailReqCounter, 1);
       
   126 }
       
   127 
       
   128 /*!
       
   129  Tests albumArt() request when art is not in local cache.
       
   130  Request more than 1 to test request queue.
       
   131  */
       
   132 void TestMpCollectionAlbumArtManager::testAlbumArtNoCacheQueue()
       
   133 {
       
   134     for ( int i = 0; i < 3; i++) {
       
   135         const QIcon* icon = mTest->albumArt(i);
       
   136         QVERIFY(icon->isNull() == false);
       
   137     }
       
   138     // Verify that:
       
   139     // - Max number of requests were sent to thumbnail manager
       
   140     // - A request has been queued
       
   141     QCOMPARE(mTest->mPendingRequest, true);
       
   142     QCOMPARE(mTest->mRequestQueue.count(), 2);
       
   143     QCOMPARE(mTest->mThumbnailManager->mThumbnailReqCounter, 1);
       
   144 }
       
   145 
       
   146 /*!
       
   147  Tests albumArt() request when art is in local cache.
       
   148  */
       
   149 void TestMpCollectionAlbumArtManager::testAlbumArtCache()
       
   150 {
       
   151     mTest->mImageCache.insert(0, new QIcon(mIcon));
       
   152 
       
   153     const QIcon* icon = mTest->albumArt(0);
       
   154     // Verify that:
       
   155     // - A valid icon has been returned
       
   156     // - No request has been sent to thumbnail manager
       
   157     QVERIFY(icon->isNull() == false);
       
   158     QCOMPARE(mTest->mPendingRequest, false);
       
   159     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   160     QCOMPARE(mTest->mThumbnailManager->mThumbnailReqCounter, 0);
       
   161 }
       
   162 
       
   163 /*!
       
   164  Tests albumArt() request when item doesn't have AlbumArtUri.
       
   165  */
       
   166 void TestMpCollectionAlbumArtManager::testAlbumArtNoUri()
       
   167 {
       
   168     mTest->mCollectionData->mItemDataReturn = false;
       
   169     const QIcon* icon = mTest->albumArt(0);
       
   170     // Verify that:
       
   171     // - It returned default icon
       
   172     // - There is no request pending from thumbnail manager
       
   173     QVERIFY(icon->isNull() == false);
       
   174     QCOMPARE(mTest->mPendingRequest, false);
       
   175     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   176 }
       
   177 
       
   178 /*!
       
   179  Tests albumArt() request when thumbnail request fails.
       
   180  */
       
   181 void TestMpCollectionAlbumArtManager::testAlbumArtFail()
       
   182 {
       
   183     mTest->mThumbnailManager->mGetThumbFails = true;
       
   184     const QIcon* icon = mTest->albumArt(0);
       
   185     // Verify that:
       
   186     // - It returned default icon
       
   187     // - There is no request pending from thumbnail manager
       
   188     QVERIFY(icon->isNull() == false);
       
   189     QCOMPARE(mTest->mPendingRequest, false);
       
   190     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   191 }
       
   192 
       
   193 /*!
       
   194  Tests cacheFirstScreen() request with empty list.
       
   195  */
       
   196 void TestMpCollectionAlbumArtManager::testCacheFirstScreenEmpty()
       
   197 {
       
   198     mTest->mCollectionData->mCount = 0;
       
   199     mTest->cacheFirstScreen();
       
   200     QCOMPARE(mTest->mPendingRequest, false);
       
   201     QCOMPARE(mTest->mCachingInProgress, false);
       
   202     QCOMPARE(mTest->mThumbnailManager->mThumbnailReqCounter, 0);
       
   203 }
       
   204 
       
   205 /*!
       
   206  Tests cacheFirstScreen() request with 1 item in the list.
       
   207  */
       
   208 void TestMpCollectionAlbumArtManager::testCacheFirstScreen()
       
   209 {
       
   210     mTest->mCollectionData->mCount = 1;
       
   211     mTest->cacheFirstScreen();
       
   212     QCOMPARE(mTest->mCachingInProgress, true);
       
   213     QCOMPARE(mTest->mPendingRequest, true);
       
   214     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   215     QCOMPARE(mTest->mThumbnailManager->mThumbnailReqCounter, 1);
       
   216 }
       
   217 
       
   218 /*!
       
   219  Tests cacheFirstScreen() request with all items already in local cache.
       
   220  */
       
   221 void TestMpCollectionAlbumArtManager::testCacheFirstScreenAllCached()
       
   222 {
       
   223     mTest->mCollectionData->mCount = 3;
       
   224     mTest->mImageCache.insert(0, new QIcon(mIcon));
       
   225     mTest->mImageCache.insert(1, new QIcon(mIcon));
       
   226     mTest->mImageCache.insert(2, new QIcon(mIcon));
       
   227     mTest->cacheFirstScreen();
       
   228     QCOMPARE(mTest->mPendingRequest, false);
       
   229     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   230     QCOMPARE(mTest->mThumbnailManager->mThumbnailReqCounter, 0);
       
   231 }
       
   232 
       
   233 /*!
       
   234  Tests cancel() request.
       
   235  */
       
   236 void TestMpCollectionAlbumArtManager::testCancel()
       
   237 {
       
   238     // First send enough requests to trigger requests to be queued.
       
   239     for ( int i = 0; i < 3; i++) {
       
   240         const QIcon* icon = mTest->albumArt(i);
       
   241         QVERIFY(icon->isNull() == false);
       
   242     }
       
   243     QCOMPARE(mTest->mPendingRequest, true);
       
   244     QCOMPARE(mTest->mRequestQueue.count(), 2);
       
   245     QCOMPARE(mTest->mThumbnailManager->mThumbnailReqCounter, 1);
       
   246 
       
   247     // Verify that:
       
   248     // - All requests to thumbnail manager are cancelled
       
   249     // - Queue is emptied
       
   250     mTest->cancel();
       
   251     QCOMPARE(mTest->mPendingRequest, false);
       
   252     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   253     QCOMPARE(mTest->mThumbnailManager->mCancelCounter, 1);
       
   254 }
       
   255 
       
   256 /*!
       
   257  Tests thumbnailReady() slot.
       
   258  Tests completion of cacheFirstScreen() request.
       
   259  */
       
   260 void TestMpCollectionAlbumArtManager::testThumbnailReadyCache()
       
   261 {
       
   262     connect( this, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
   263         mTest->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)) );
       
   264     QSignalSpy spy(mTest, SIGNAL(albumArtReady(int)));
       
   265 
       
   266     // First send cacheAlbumArt() request with 3 items.
       
   267     mTest->mCollectionData->mCount = 3;
       
   268     mTest->cacheFirstScreen();
       
   269     QCOMPARE(mTest->mCachingInProgress, true);
       
   270     QCOMPARE(mTest->mPendingRequest, true);
       
   271     QCOMPARE(mTest->mRequestQueue.count(), 2);
       
   272 
       
   273     // Emit thumbnailReady() signal for each request. Then verify that:
       
   274     // - Test object emitted signal albumArtReady() for 3 items
       
   275     // - 3 items are present in local cache
       
   276     for ( int i = 0; i < 3; i++ ) {
       
   277         void *clientData = reinterpret_cast<void *>(i);
       
   278         emit thumbnailReady(mIcon.pixmap(50,50), clientData, i+1, 0);
       
   279     }
       
   280 
       
   281     QCOMPARE(spy.count(), 3);
       
   282     QCOMPARE(mTest->mCachingInProgress, false);
       
   283     QCOMPARE(mTest->mPendingRequest, false);
       
   284     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   285     QCOMPARE(mTest->mImageCache.count(), 3);
       
   286     disconnect( this, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
   287         mTest->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)) );
       
   288 }
       
   289 
       
   290 /*!
       
   291  Tests thumbnailReady() slot.
       
   292  Tests completion of cacheFirstScreen() request with error code returned.
       
   293  */
       
   294 void TestMpCollectionAlbumArtManager::testThumbnailReadyCacheError()
       
   295 {
       
   296     connect( this, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
   297         mTest->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)) );
       
   298     QSignalSpy spy(mTest, SIGNAL(albumArtReady(int)));
       
   299 
       
   300     // First send cacheAlbumArt() request with 3 items.
       
   301     mTest->mCollectionData->mCount = 3;
       
   302     mTest->cacheFirstScreen();
       
   303     QCOMPARE(mTest->mCachingInProgress, true);
       
   304     QCOMPARE(mTest->mPendingRequest, true);
       
   305     QCOMPARE(mTest->mRequestQueue.count(), 2);
       
   306 
       
   307     // Emit thumbnailReady() signal for each request with error. Then verify that:
       
   308     // - Test object doesn't emit signal albumArtReady()
       
   309     // - 0 item is present in local cache
       
   310     for ( int i = 0; i < 3; i++ ) {
       
   311         void *clientData = reinterpret_cast<void *>(i);
       
   312         emit thumbnailReady(mIcon.pixmap(50,50), clientData, i+1, -1);
       
   313     }
       
   314     QCOMPARE(spy.count(), 0);
       
   315     QCOMPARE(mTest->mCachingInProgress, false);
       
   316     QCOMPARE(mTest->mPendingRequest, false);
       
   317     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   318     QCOMPARE(mTest->mImageCache.count(), 0);
       
   319     disconnect( this, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
   320         mTest->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)) );
       
   321 }
       
   322 
       
   323 /*!
       
   324  Tests thumbnailReady() slot.
       
   325  Tests completion of albumArt() request with empty request queue.
       
   326  */
       
   327 void TestMpCollectionAlbumArtManager::testThumbnailReadyAlbumArt()
       
   328 {
       
   329     connect( this, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
   330         mTest->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)) );
       
   331     QSignalSpy spy(mTest, SIGNAL(albumArtReady(int)));
       
   332 
       
   333     // First send 3 albumArt() requests.
       
   334     for ( int i = 0; i < 3; i++) {
       
   335         const QIcon* icon = mTest->albumArt(i);
       
   336         QVERIFY(icon->isNull() == false);
       
   337     }
       
   338     QCOMPARE(mTest->mPendingRequest, true);
       
   339     QCOMPARE(mTest->mRequestQueue.count(), 2);
       
   340 
       
   341     // Emit thumbnailReady() signal for each request. Then verify that:
       
   342     // - Test object emitted signal albumArtReady() 3 times
       
   343     // - 3 items are present in local cache
       
   344     for ( int i = 0; i < 3; i++ ) {
       
   345         void *clientData = reinterpret_cast<void *>(i);
       
   346         emit thumbnailReady(mIcon.pixmap(50,50), clientData, i+1, 0);
       
   347     }
       
   348     QCOMPARE(spy.count(), 3);
       
   349     QCOMPARE(mTest->mPendingRequest, false);
       
   350     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   351     QCOMPARE(mTest->mImageCache.count(), 3);
       
   352     disconnect( this, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
   353         mTest->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)) );
       
   354 }
       
   355 
       
   356 /*!
       
   357  Tests thumbnailReady() slot.
       
   358  Tests completion of albumArt() request with error code returned.
       
   359  This covers a case with no pending items in the queue.
       
   360  */
       
   361 void TestMpCollectionAlbumArtManager::testThumbnailReadyAlbumArtError()
       
   362 {
       
   363     connect( this, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
   364         mTest->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)) );
       
   365     QSignalSpy spy(mTest, SIGNAL(albumArtReady(int)));
       
   366 
       
   367     // First send 3 albumArt() requests.
       
   368     for ( int i = 0; i < 3; i++) {
       
   369         const QIcon* icon = mTest->albumArt(i);
       
   370         QVERIFY(icon->isNull() == false);
       
   371     }
       
   372     QCOMPARE(mTest->mPendingRequest, true);
       
   373     QCOMPARE(mTest->mRequestQueue.count(), 2);
       
   374 
       
   375     // Emit thumbnailReady() signal for each request with error. Then verify that:
       
   376     // - Test object doesn't emitted signal albumArtReady()
       
   377     // - 0 items are present in local cache
       
   378     for ( int i = 0; i < 3; i++ ) {
       
   379         void *clientData = reinterpret_cast<void *>(i);
       
   380         emit thumbnailReady(mIcon.pixmap(50,50), clientData, i+1, -1);
       
   381     }
       
   382     QCOMPARE(spy.count(), 0);
       
   383     QCOMPARE(mTest->mPendingRequest, false);
       
   384     QCOMPARE(mTest->mRequestQueue.count(), 0);
       
   385     QCOMPARE(mTest->mImageCache.count(), 0);
       
   386     disconnect( this, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
   387         mTest->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)) );
       
   388 }
       
   389 
       
   390 // End of file