mpdata/tsrc/unittest_mpplaybackdata/src/unittest_mpplaybackdata.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 MpPlaybackData
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QSignalSpy>
       
    19 #include <qnamespace.h>
       
    20 #include <hbapplication.h>
       
    21 #include <hbmainwindow.h>
       
    22 #include <hbInstance.h>
       
    23 
       
    24 #include "unittest_mpplaybackdata.h"
       
    25 #include "stub/inc/thumbnailmanager_qt.h"
       
    26 
       
    27 
       
    28 // Do this so we can access all member variables.
       
    29 #define private public
       
    30 #include "mpplaybackdata.h"
       
    31 #undef private
       
    32 
       
    33 const int KUndefined = -1;
       
    34 
       
    35 /*!
       
    36  Make our test case a stand-alone executable that runs all the test functions.
       
    37  */
       
    38 int main(int argc, char *argv[])
       
    39 {
       
    40     HbApplication app(argc, argv);
       
    41     HbMainWindow window;
       
    42     TestMpPlaybackData tv;
       
    43 
       
    44     char *pass[3];
       
    45     pass[0] = argv[0];
       
    46     pass[1] = "-o";
       
    47     pass[2] = "c:\\data\\unittest_mpplaybackdata.txt";
       
    48 
       
    49     int res = QTest::qExec(&tv, 3, pass);
       
    50 
       
    51     return res;
       
    52 }
       
    53 
       
    54 TestMpPlaybackData::TestMpPlaybackData()
       
    55     : mTest(0)
       
    56 {
       
    57 }
       
    58 
       
    59 TestMpPlaybackData::~TestMpPlaybackData()
       
    60 {
       
    61     delete mTest;
       
    62 }
       
    63 
       
    64 /*!
       
    65  Called before the first testfunction is executed.
       
    66  */
       
    67 void TestMpPlaybackData::initTestCase()
       
    68 {
       
    69 }
       
    70 
       
    71 /*!
       
    72  Called after the last testfunction was executed.
       
    73  */
       
    74 void TestMpPlaybackData::cleanupTestCase()
       
    75 {
       
    76 }
       
    77 
       
    78 /*!
       
    79  Called before each testfunction is executed.
       
    80  */
       
    81 void TestMpPlaybackData::init()
       
    82 {
       
    83     mTest = new MpPlaybackData();
       
    84 }
       
    85 
       
    86 /*!
       
    87  Called after every testfunction.
       
    88  */
       
    89 void TestMpPlaybackData::cleanup()
       
    90 {
       
    91     delete mTest;
       
    92     mTest = 0;
       
    93 }
       
    94 
       
    95 /*!
       
    96  testConstructor
       
    97  */
       
    98 void TestMpPlaybackData::testConstructor()
       
    99 {
       
   100     QVERIFY(mTest->mThumbnailManager != 0);
       
   101     QCOMPARE(mTest->mThumbnailManager->getInitCounter(), 1);
       
   102     QVERIFY(mTest->mDuration == 0);
       
   103     QVERIFY(mTest->mPosition == 0);
       
   104     QVERIFY(!mTest->mDefaultAlbumArt);
       
   105 
       
   106 }
       
   107 
       
   108 /*!
       
   109  test setDuration and duration
       
   110  */
       
   111 void TestMpPlaybackData::testSetDuration()
       
   112 {
       
   113     QSignalSpy spy(mTest, SIGNAL(durationChanged()));
       
   114     for(int i= 0; i < 10; i++){
       
   115         mTest->setDuration(i);
       
   116         QCOMPARE(mTest->mDuration, i);
       
   117         QCOMPARE(mTest->duration(),i);
       
   118         QCOMPARE(spy.count(),i+1);
       
   119     }
       
   120 }
       
   121 
       
   122 /*!
       
   123  test setPosition and position
       
   124  */
       
   125 void TestMpPlaybackData::testSetPosition()
       
   126 {
       
   127     QSignalSpy spy(mTest, SIGNAL(positionChanged()));
       
   128     for(int i= 0; i < 10; i++){
       
   129         mTest->setPosition(i);
       
   130         QCOMPARE(mTest->mPosition, i);
       
   131         QCOMPARE(mTest->position(),i);
       
   132         QCOMPARE(spy.count(),i+1);
       
   133     }
       
   134 }
       
   135 
       
   136 /*!
       
   137  test setTitle and title
       
   138  */
       
   139 void TestMpPlaybackData::testSetTitle()
       
   140 {
       
   141     bool changed;
       
   142     
       
   143     changed = mTest->setTitle(QString("Title 1"));
       
   144     QCOMPARE(changed, true);
       
   145     QCOMPARE(mTest->mTitle, QString("Title 1"));
       
   146     QCOMPARE(mTest->title(), QString("Title 1"));
       
   147     changed = mTest->setTitle(QString("Title 1"));
       
   148     QCOMPARE(changed, false);
       
   149     QCOMPARE(mTest->title(), QString("Title 1"));
       
   150     changed = mTest->setTitle(QString("Title 1"));
       
   151     changed = mTest->setTitle(QString("Title 2"));
       
   152     QCOMPARE(changed, true);
       
   153     QCOMPARE(mTest->mTitle, QString("Title 2"));
       
   154     QCOMPARE(mTest->title(), QString("Title 2"));
       
   155     
       
   156 }
       
   157 
       
   158 /*!
       
   159  test setArtist and artist
       
   160  */
       
   161 void TestMpPlaybackData::testSetArtist()
       
   162 {
       
   163     bool changed;
       
   164     
       
   165     changed = mTest->setArtist(QString("Artist 1"));
       
   166     QCOMPARE(changed, true);
       
   167     QCOMPARE(mTest->mArtist, QString("Artist 1"));
       
   168     QCOMPARE(mTest->artist(), QString("Artist 1"));
       
   169     changed = mTest->setArtist(QString("Artist 1"));
       
   170     QCOMPARE(changed, false);
       
   171     QCOMPARE(mTest->mArtist, QString("Artist 1"));
       
   172     QCOMPARE(mTest->artist(), QString("Artist 1"));  
       
   173     changed = mTest->setArtist(QString("Artist 2"));
       
   174     QCOMPARE(changed, true);
       
   175     QCOMPARE(mTest->mArtist, QString("Artist 2"));
       
   176     QCOMPARE(mTest->artist(), QString("Artist 2"));
       
   177     
       
   178 }
       
   179 
       
   180 /*!
       
   181  test setAlbum and album
       
   182  */
       
   183 void TestMpPlaybackData::testSetAlbum()
       
   184 {
       
   185     bool changed;
       
   186     
       
   187     changed = mTest->setAlbum(QString("Album 1"));
       
   188     QCOMPARE(changed, true);
       
   189     QCOMPARE(mTest->mAlbum, QString("Album 1"));
       
   190     QCOMPARE(mTest->album(), QString("Album 1"));
       
   191     changed = mTest->setAlbum(QString("Album 1"));
       
   192     QCOMPARE(changed, false);
       
   193     QCOMPARE(mTest->mAlbum, QString("Album 1"));
       
   194     QCOMPARE(mTest->album(), QString("Album 1"));
       
   195     changed = mTest->setAlbum(QString("Album 2"));
       
   196     QCOMPARE(changed, true);
       
   197     QCOMPARE(mTest->mAlbum, QString("Album 2"));
       
   198     QCOMPARE(mTest->album(), QString("Album 2"));
       
   199     
       
   200 }
       
   201 
       
   202 /*!
       
   203  test setUri and uri
       
   204  */
       
   205 void TestMpPlaybackData::testSetUri()
       
   206 {
       
   207     bool changed;
       
   208     
       
   209     changed = mTest->setUri(QString("Uri 1"));
       
   210     QCOMPARE(changed, true);
       
   211     QCOMPARE(mTest->mUri, QString("Uri 1"));
       
   212     QCOMPARE(mTest->uri(), QString("Uri 1"));
       
   213     changed = mTest->setUri(QString("Uri 1"));
       
   214     QCOMPARE(changed, false);
       
   215     QCOMPARE(mTest->mUri, QString("Uri 1"));
       
   216     QCOMPARE(mTest->uri(), QString("Uri 1"));
       
   217     changed = mTest->setUri(QString("Uri 2"));
       
   218     QCOMPARE(changed, true);
       
   219     QCOMPARE(mTest->mUri, QString("Uri 2"));
       
   220     QCOMPARE(mTest->uri(), QString("Uri 2"));
       
   221     
       
   222 }
       
   223 
       
   224 /*!
       
   225  test setAlbumArtUri
       
   226  */
       
   227 void TestMpPlaybackData::testSetAlbumArtUri()
       
   228 {
       
   229     QSignalSpy spy(mTest, SIGNAL(albumArtReady()));
       
   230     //Empty string
       
   231     mTest->setAlbumArtUri(QString(""));
       
   232     QCOMPARE(mTest->mAlbumArt, mTest->mDefaultAlbumArt);
       
   233     QCOMPARE(spy.count(),1);
       
   234     spy.clear();
       
   235     
       
   236     //Request failed
       
   237     mTest->mThumbnailManager->mGetThumbFails = true;
       
   238     mTest->setAlbumArtUri(QString("AlbumArt"));
       
   239     QCOMPARE(mTest->mAlbumArt, mTest->mDefaultAlbumArt);
       
   240     QCOMPARE(spy.count(),1);
       
   241     spy.clear();
       
   242     
       
   243     //Succesful requests
       
   244     mTest->mThumbnailManager->mGetThumbFails = false;
       
   245     for(int i = 1; i < 10; i++){
       
   246         mTest->setAlbumArtUri(QString("AlbumArt"));
       
   247         QCOMPARE(mTest->mReqId, i);
       
   248         QCOMPARE(spy.count(),0);
       
   249         mTest->mReqId = KUndefined;
       
   250     }
       
   251     
       
   252     //Pending request
       
   253     mTest->mReqId = 1;
       
   254     for(int i = 1; i < 10; i++){
       
   255         mTest->setAlbumArtUri(QString("AlbumArt"));
       
   256         QCOMPARE(spy.count(),0);
       
   257         QCOMPARE(mTest->mThumbnailManager->mCancelCounter, i);
       
   258     }
       
   259 }
       
   260 
       
   261 /*!
       
   262  test ThumbnailReady and albumArt
       
   263  */
       
   264 void TestMpPlaybackData::testThumbnailReady()
       
   265 {
       
   266     void* temp;
       
   267     temp=0;
       
   268     QSignalSpy spy(mTest, SIGNAL(albumArtReady()));
       
   269     QPixmap albumArt(100,100);
       
   270     QPixmap albumArt2;
       
   271    
       
   272     //Error
       
   273     mTest->thumbnailReady(albumArt,temp,KUndefined,KUndefined);
       
   274     QCOMPARE(mTest->mAlbumArt, mTest->mDefaultAlbumArt); 
       
   275     QCOMPARE(mTest->mReqId, KUndefined);
       
   276     QCOMPARE(spy.count(),1);
       
   277     spy.clear();
       
   278     
       
   279     //Wrong id
       
   280     mTest->mReqId = 1;
       
   281     mTest->thumbnailReady(albumArt,temp,KUndefined,0);
       
   282     QCOMPARE(mTest->mAlbumArt, mTest->mDefaultAlbumArt); 
       
   283     QCOMPARE(mTest->mReqId, KUndefined);
       
   284     QCOMPARE(spy.count(),1);
       
   285     spy.clear();
       
   286     
       
   287     //Update album art
       
   288     mTest->mReqId = 1;
       
   289     mTest->thumbnailReady(albumArt,temp,1,0);
       
   290     QCOMPARE(mTest->mReqId, KUndefined);
       
   291     QCOMPARE(spy.count(),1);
       
   292     mTest->albumArt(albumArt2);
       
   293     QCOMPARE(albumArt,albumArt2);
       
   294 }
       
   295 
       
   296 /*!
       
   297  test setPlaybackState and playbackState
       
   298  */
       
   299 void TestMpPlaybackData::testSetPlaybackState()
       
   300 {
       
   301     QSignalSpy spy(mTest, SIGNAL(playbackStateChanged()));
       
   302     
       
   303     mTest->mPlaybackState = MpPlaybackData::Paused;
       
   304     
       
   305     mTest->setPlaybackState(MpPlaybackData::Playing);
       
   306     QCOMPARE(mTest->playbackState(),MpPlaybackData::Playing);
       
   307     QCOMPARE(spy.count(),1);
       
   308     spy.clear();
       
   309     
       
   310     mTest->setPlaybackState(MpPlaybackData::Stopped);
       
   311     QCOMPARE(mTest->playbackState(),MpPlaybackData::Stopped);
       
   312     QCOMPARE(spy.count(),1);
       
   313     spy.clear();
       
   314     
       
   315     mTest->setPlaybackState(MpPlaybackData::Paused);
       
   316     QCOMPARE(mTest->playbackState(),MpPlaybackData::Paused);
       
   317     QCOMPARE(spy.count(),1);
       
   318     spy.clear();
       
   319     
       
   320     mTest->setPlaybackState(MpPlaybackData::Paused);
       
   321     QCOMPARE(mTest->playbackState(),MpPlaybackData::Paused);
       
   322     QCOMPARE(spy.count(),0);
       
   323     
       
   324 }
       
   325 
       
   326 
       
   327 /*!
       
   328  test commitPlaybackInfo
       
   329  */
       
   330 void TestMpPlaybackData::testCommitPlaybackInfo()
       
   331 {
       
   332     QSignalSpy spy(mTest, SIGNAL(playbackInfoChanged()));
       
   333     
       
   334     for(int i = 1; i < 10; i++){
       
   335         mTest->commitPlaybackInfo();
       
   336         QCOMPARE(spy.count(),i);
       
   337     }
       
   338 }
       
   339 
       
   340 // End of file