utilities/mpnowplayingbanner/tsrc/unittest_mpnowplayingbackend/src/unittest_mpnowplayingbackend.cpp
changeset 43 0f32e550d9d8
parent 38 b93f525c9244
child 45 612c4815aebe
equal deleted inserted replaced
38:b93f525c9244 43:0f32e550d9d8
     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 <QMetaType>
       
    20 
       
    21 #include "unittest_mpnowplayingbackend.h"
       
    22 #include "mpcommondefs.h"
       
    23 #include "stub/inc/mpxplaybackutility.h"
       
    24 
       
    25 // Do this so we can access all member variables.
       
    26 #define private public
       
    27 #include "mpnowplayingbackend.h"
       
    28 #include "mpnowplayingbackend_p.h"
       
    29 #undef private
       
    30 
       
    31 /*!
       
    32  Make our test case a stand-alone executable that runs all the test functions.
       
    33  */
       
    34 int main(int argc, char *argv[])
       
    35 {
       
    36 
       
    37     TestMpNowPlayingBackEnd tv;
       
    38 
       
    39     if ( argc > 1 ) {
       
    40         return QTest::qExec( &tv, argc, argv);
       
    41     }
       
    42     else {
       
    43         char *pass[3];
       
    44         pass[0] = argv[0];
       
    45         pass[1] = "-o";
       
    46         pass[2] = "c:\\data\\unittest_mpnowplayingbackend.txt";
       
    47 
       
    48         return QTest::qExec(&tv, 3, pass);
       
    49     }
       
    50 }
       
    51 
       
    52 TestMpNowPlayingBackEnd::TestMpNowPlayingBackEnd()
       
    53     : mTest(0)
       
    54 {
       
    55 }
       
    56 
       
    57 TestMpNowPlayingBackEnd::~TestMpNowPlayingBackEnd()
       
    58 {
       
    59     delete mTest;
       
    60 }
       
    61 
       
    62 /*!
       
    63  Called before the first testfunction is executed.
       
    64  */
       
    65 void TestMpNowPlayingBackEnd::initTestCase()
       
    66 {
       
    67 }
       
    68 
       
    69 /*!
       
    70  Called after the last testfunction was executed.
       
    71  */
       
    72 void TestMpNowPlayingBackEnd::cleanupTestCase()
       
    73 {
       
    74 }
       
    75 
       
    76 /*!
       
    77  Called before each testfunction is executed.
       
    78  */
       
    79 void TestMpNowPlayingBackEnd::init()
       
    80 {
       
    81     mTest = new MpNowPlayingBackEnd(MpCommon::KMusicPlayerUid);
       
    82     mTestPrivate = mTest->d_ptr;
       
    83 
       
    84     MMPXSource::setMediaLeave(false);
       
    85     MMPXPlaybackUtility::setUtilityLeave(false);
       
    86     MMPXPlaybackUtility::setAddObserverLeave(false);
       
    87     MMPXPlaybackUtility::setRemoveObserverLeave(false);
       
    88     MMPXPlaybackUtility::setCommandLeave(false);
       
    89     MMPXPlaybackUtility::setStateLeave(false);
       
    90     MMPXPlaybackUtility::setSource(true);
       
    91 }
       
    92 
       
    93 /*!
       
    94  Called after every testfunction.
       
    95  */
       
    96 void TestMpNowPlayingBackEnd::cleanup()
       
    97 {
       
    98     delete mTest;
       
    99     mTest = 0;
       
   100 }
       
   101 
       
   102 /*!
       
   103  Tests constructor. Good case.
       
   104  */
       
   105 void TestMpNowPlayingBackEnd::testConstructor()
       
   106 {
       
   107     QVERIFY(mTestPrivate->mPlaybackUtility != 0);
       
   108     QCOMPARE(mTestPrivate->mPreviousState, NotPlaying);
       
   109 }
       
   110 
       
   111 /*!
       
   112  Tests constructor.
       
   113  - MMPXPlaybackUtility::UtilityL leaves.
       
   114  */
       
   115 void TestMpNowPlayingBackEnd::testInitUtilCreateFail()
       
   116 {
       
   117     // AK - There is no way for the client to tell if constructor fails.
       
   118     // This should be split into constructor and initialize.
       
   119     cleanup();
       
   120     MMPXPlaybackUtility::setUtilityLeave(true);
       
   121     init();
       
   122     QVERIFY(mTestPrivate->mPlaybackUtility == 0);
       
   123 }
       
   124 
       
   125 /*!
       
   126  Tests constructor.
       
   127  - MMPXPlaybackUtility::AddObserverL leaves.
       
   128  */
       
   129 void TestMpNowPlayingBackEnd::testInitAddObserverFail()
       
   130 {
       
   131     cleanup();
       
   132     MMPXPlaybackUtility::resetCloseCounter();
       
   133     MMPXPlaybackUtility::setAddObserverLeave(true);
       
   134     init();
       
   135     QVERIFY(mTestPrivate->mPlaybackUtility == 0);
       
   136     QCOMPARE(MMPXPlaybackUtility::getCloseCounter(), 1);
       
   137 }
       
   138 
       
   139 /*!
       
   140  Tests destructor. Good case.
       
   141  */
       
   142 void TestMpNowPlayingBackEnd::testDestructor()
       
   143 {
       
   144     MMPXPlaybackUtility::resetCloseCounter();
       
   145     MMPXPlaybackUtility::resetRemoveObserverCounter();
       
   146 
       
   147     QVERIFY(mTestPrivate->mPlaybackUtility->iObserver != 0);
       
   148 
       
   149     cleanup();
       
   150     QCOMPARE(MMPXPlaybackUtility::getRemoveObserverCounter(), 1);
       
   151     QCOMPARE(MMPXPlaybackUtility::getCloseCounter(), 1);
       
   152 }
       
   153 
       
   154 /*!
       
   155  Tests destructor.
       
   156  - MMPXPlaybackUtility::RemoveObserverL leaves.
       
   157  */
       
   158 void TestMpNowPlayingBackEnd::testDestructorRemoveObserverFail()
       
   159 {
       
   160     MMPXPlaybackUtility::resetCloseCounter();
       
   161     MMPXPlaybackUtility::resetRemoveObserverCounter();
       
   162     MMPXPlaybackUtility::setRemoveObserverLeave(true);
       
   163 
       
   164     QVERIFY(mTestPrivate->mPlaybackUtility->iObserver != 0);
       
   165 
       
   166     cleanup();
       
   167     QCOMPARE(MMPXPlaybackUtility::getRemoveObserverCounter(), 0);
       
   168     QCOMPARE(MMPXPlaybackUtility::getCloseCounter(), 1);
       
   169 }
       
   170 
       
   171 /*!
       
   172  Tests update() slot. Good case.
       
   173  */
       
   174 void TestMpNowPlayingBackEnd::testUpdateSuccess()
       
   175 {
       
   176     qRegisterMetaType<SimplifiedPlayerState>();
       
   177     QSignalSpy spy(mTest, SIGNAL(stateUpdate(SimplifiedPlayerState)));
       
   178     connect(this, SIGNAL(update()), mTest, SLOT(update()));
       
   179     MMPXSource::resetMediaCounter();
       
   180 
       
   181 
       
   182     MMPXPlaybackUtility::setState(EPbStatePlaying);
       
   183     // This should trigger stateUpdate(Playing) signal
       
   184     emit update();
       
   185     // Same state; This shouldn't trigger stateUpdate() signal
       
   186     emit update();
       
   187 
       
   188     MMPXPlaybackUtility::setState(EPbStatePaused);
       
   189     // This should trigger stateUpdate(Paused) signal
       
   190     emit update();
       
   191     // Same state; This shouldn't trigger stateUpdate() signal
       
   192     emit update();
       
   193     
       
   194     MMPXPlaybackUtility::setState(EPbStateStopped);
       
   195     //Stop is reinterpreted as Pause.
       
   196     // Same state; This shouldn't trigger stateUpdate() signal
       
   197     emit update();
       
   198 
       
   199 
       
   200     QCOMPARE(spy.count(), 2);
       
   201     SimplifiedPlayerState state = qvariant_cast<SimplifiedPlayerState>(spy.at(0).at(0));
       
   202     QCOMPARE(state, Playing);
       
   203     state = qvariant_cast<SimplifiedPlayerState>(spy.at(1).at(0));
       
   204     QCOMPARE(state, Paused);
       
   205 
       
   206 
       
   207     QCOMPARE(MMPXSource::getMediaCounter(), 5);
       
   208 
       
   209     disconnect(this, SIGNAL(update()), mTest, SLOT(update()));
       
   210 }
       
   211 
       
   212 /*!
       
   213  Tests update() slot.
       
   214  - MMPXPlaybackUtility::Source returns NULL.
       
   215  */
       
   216 void TestMpNowPlayingBackEnd::testUpdateNoSource()
       
   217 {
       
   218     MMPXPlaybackUtility::setSource(false);
       
   219     qRegisterMetaType<SimplifiedPlayerState>();
       
   220     QSignalSpy spy(mTest, SIGNAL(stateUpdate(SimplifiedPlayerState)));
       
   221     connect(this, SIGNAL(update()), mTest, SLOT(update()));
       
   222     MMPXSource::resetMediaCounter();
       
   223 
       
   224     MMPXPlaybackUtility::setState(EPbStateStopped);
       
   225     emit update();
       
   226 
       
   227     QCOMPARE(spy.count(), 1);
       
   228     SimplifiedPlayerState state = qvariant_cast<SimplifiedPlayerState>(spy.at(0).at(0));
       
   229     QCOMPARE(state, NotPlaying);
       
   230 
       
   231     QCOMPARE(MMPXSource::getMediaCounter(), 0);
       
   232     disconnect(this, SIGNAL(update()), mTest, SLOT(update()));
       
   233 }
       
   234 
       
   235 /*!
       
   236  Tests update() slot.
       
   237  - MMPXPlaybackUtility::StateL leaves.
       
   238  */
       
   239 void TestMpNowPlayingBackEnd::testUpdateStateFail()
       
   240 {
       
   241     MMPXPlaybackUtility::setStateLeave(true);
       
   242     qRegisterMetaType<SimplifiedPlayerState>();
       
   243     QSignalSpy spy(mTest, SIGNAL(stateUpdate(SimplifiedPlayerState)));
       
   244     connect(this, SIGNAL(update()), mTest, SLOT(update()));
       
   245     MMPXSource::resetMediaCounter();
       
   246 
       
   247     MMPXPlaybackUtility::setState(EPbStatePaused);
       
   248     emit update();
       
   249 
       
   250     QCOMPARE(spy.count(), 0);
       
   251     QCOMPARE(MMPXSource::getMediaCounter(), 0);
       
   252     disconnect(this, SIGNAL(update()), mTest, SLOT(update()));
       
   253 }
       
   254 
       
   255 /*!
       
   256  Tests update() slot.
       
   257  - MMPXSource::MediaL leaves.
       
   258  */
       
   259 void TestMpNowPlayingBackEnd::testUpdateMediaFail()
       
   260 {
       
   261     MMPXSource::setMediaLeave(true);
       
   262     qRegisterMetaType<SimplifiedPlayerState>();
       
   263     QSignalSpy spy(mTest, SIGNAL(stateUpdate(SimplifiedPlayerState)));
       
   264     connect(this, SIGNAL(update()), mTest, SLOT(update()));
       
   265     MMPXSource::resetMediaCounter();
       
   266 
       
   267     MMPXPlaybackUtility::setState(EPbStatePaused);
       
   268     emit update();
       
   269 
       
   270     QCOMPARE(spy.count(), 1);
       
   271     QCOMPARE(MMPXSource::getMediaCounter(), 0);
       
   272     disconnect(this, SIGNAL(update()), mTest, SLOT(update()));
       
   273 }
       
   274 
       
   275 /*!
       
   276  Tests playPause() slot. Good case.
       
   277  */
       
   278 void TestMpNowPlayingBackEnd::testPlayPause()
       
   279 {
       
   280     MMPXPlaybackUtility::setState(EPbStatePlaying);
       
   281     connect(this, SIGNAL(playPause()), mTest, SLOT(playPause()));
       
   282 
       
   283     emit playPause();
       
   284     QCOMPARE(MMPXPlaybackUtility::getState(), EPbStatePaused);
       
   285 
       
   286     emit playPause();
       
   287     QCOMPARE(MMPXPlaybackUtility::getState(), EPbStatePlaying);
       
   288 
       
   289     disconnect(this, SIGNAL(playPause()), mTest, SLOT(playPause()));
       
   290 }
       
   291 
       
   292 /*!
       
   293  Tests playPause() slot.
       
   294  - MMPXPlaybackUtility::CommandL leaves.
       
   295  */
       
   296 void TestMpNowPlayingBackEnd::testPlayPauseFail()
       
   297 {
       
   298     MMPXPlaybackUtility::setCommandLeave(true);
       
   299     MMPXPlaybackUtility::setState(EPbStatePlaying);
       
   300     connect(this, SIGNAL(playPause()), mTest, SLOT(playPause()));
       
   301     emit playPause();
       
   302     // Just need to make sure nothing crashes.
       
   303     QCOMPARE(MMPXPlaybackUtility::getState(), EPbStatePlaying);
       
   304     disconnect(this, SIGNAL(playPause()), mTest, SLOT(playPause()));
       
   305 }
       
   306 
       
   307 /*!
       
   308  Tests HandlePlaybackMessage callback with StateChanged event. Good case.
       
   309  */
       
   310 void TestMpNowPlayingBackEnd::testHandlePlaybackMessageStateChanged()
       
   311 {
       
   312     MMPXPlaybackUtility::setState(EPbStatePlaying);
       
   313     qRegisterMetaType<SimplifiedPlayerState>();
       
   314     QSignalSpy spy(mTest, SIGNAL(stateUpdate(SimplifiedPlayerState)));
       
   315     mTestPrivate->mPlaybackUtility->sendHandlePlaybackMessage(TMPXPlaybackMessage::EStateChanged);
       
   316 
       
   317     QCOMPARE(spy.count(), 1);
       
   318     SimplifiedPlayerState state = qvariant_cast<SimplifiedPlayerState>(spy.at(0).at(0));
       
   319     QCOMPARE(state, Playing);
       
   320 }
       
   321 
       
   322 /*!
       
   323  Tests HandlePlaybackMessage callback with StateChanged event.
       
   324  - MMPXPlaybackUtility::StateL leaves.
       
   325  */
       
   326 void TestMpNowPlayingBackEnd::testHandlePlaybackMessageStateChangedFail()
       
   327 {
       
   328     MMPXPlaybackUtility::setStateLeave(true);
       
   329     qRegisterMetaType<SimplifiedPlayerState>();
       
   330     QSignalSpy spy(mTest, SIGNAL(stateUpdate(SimplifiedPlayerState)));
       
   331     mTestPrivate->mPlaybackUtility->sendHandlePlaybackMessage(TMPXPlaybackMessage::EStateChanged);
       
   332 
       
   333     QCOMPARE(spy.count(), 0);
       
   334 }
       
   335 
       
   336 /*!
       
   337  Tests HandlePlaybackMessage callback with PlaylistUpdated event. Good case.
       
   338  */
       
   339 void TestMpNowPlayingBackEnd::testHandlePlaybackMessagePlaylistUpdated()
       
   340 {
       
   341     MMPXSource::resetMediaCounter();
       
   342     mTestPrivate->mPlaybackUtility->sendHandlePlaybackMessage(TMPXPlaybackMessage::EPlaylistUpdated);
       
   343     QCOMPARE(MMPXSource::getMediaCounter(), 1);
       
   344 }
       
   345 
       
   346 /*!
       
   347  Tests HandlePlaybackMessage callback with MediaChanged event. Good case.
       
   348  */
       
   349 void TestMpNowPlayingBackEnd::testHandlePlaybackMessageMediaChanged()
       
   350 {
       
   351     MMPXSource::resetMediaCounter();
       
   352     mTestPrivate->mPlaybackUtility->sendHandlePlaybackMessage(TMPXPlaybackMessage::EMediaChanged);
       
   353     QCOMPARE(MMPXSource::getMediaCounter(), 1);
       
   354 }
       
   355 
       
   356 /*!
       
   357  Tests HandlePlaybackMessage callback with PlayerChanged event. Good case.
       
   358  */
       
   359 void TestMpNowPlayingBackEnd::testHandlePlaybackMessagePlayerChanged()
       
   360 {
       
   361     MMPXPlaybackUtility::setState(EPbStatePlaying);
       
   362     MMPXSource::resetMediaCounter();
       
   363     qRegisterMetaType<SimplifiedPlayerState>();
       
   364     QSignalSpy spy(mTest, SIGNAL(stateUpdate(SimplifiedPlayerState)));
       
   365     mTestPrivate->mPlaybackUtility->sendHandlePlaybackMessage(TMPXPlaybackMessage::EActivePlayerChanged);
       
   366 
       
   367     QCOMPARE(spy.count(), 1);
       
   368     SimplifiedPlayerState state = qvariant_cast<SimplifiedPlayerState>(spy.at(0).at(0));
       
   369     QCOMPARE(state, Playing);
       
   370     QCOMPARE(MMPXSource::getMediaCounter(), 1);
       
   371 }
       
   372 
       
   373 /*!
       
   374  Tests HandlePlaybackMessage callback with PropertyChanged event. Good case.
       
   375  Tests receiving events that it doesn't care about.
       
   376  */
       
   377 void TestMpNowPlayingBackEnd::testHandlePlaybackMessagePropertyChanged()
       
   378 {
       
   379     MMPXPlaybackUtility::setState(EPbStatePlaying);
       
   380     MMPXSource::resetMediaCounter();
       
   381     qRegisterMetaType<SimplifiedPlayerState>();
       
   382     QSignalSpy spy(mTest, SIGNAL(stateUpdate(SimplifiedPlayerState)));
       
   383     mTestPrivate->mPlaybackUtility->sendHandlePlaybackMessage(TMPXPlaybackMessage::EPropertyChanged);
       
   384 
       
   385     QCOMPARE(spy.count(), 0);
       
   386     QCOMPARE(MMPXSource::getMediaCounter(), 0);
       
   387 }
       
   388 
       
   389 /*!
       
   390  Tests HandleMediaL callback with new title. Good case.
       
   391  */
       
   392 void TestMpNowPlayingBackEnd::testHandleMediaLTitle()
       
   393 {
       
   394     QSignalSpy spyTitle(mTest, SIGNAL(titleChanged(QString)));
       
   395     QSignalSpy spyArtist(mTest, SIGNAL(artistChanged(QString)));
       
   396     mTestPrivate->mPreviousState = Playing;
       
   397     mTestPrivate->mPlaybackUtility->Source()->setObserver(mTestPrivate);
       
   398     mTestPrivate->mPlaybackUtility->Source()->sendHandleMediaL(true, false, false);
       
   399 
       
   400     QCOMPARE(spyTitle.count(), 1);
       
   401     QCOMPARE(spyTitle.at(0).at(0).toString(), QString("Title"));
       
   402     QCOMPARE(spyArtist.count(), 1);
       
   403     QCOMPARE(spyArtist.at(0).at(0).toString(), QString());
       
   404 }
       
   405 
       
   406 /*!
       
   407  Tests HandleMediaL callback with new title and uri. Good case.
       
   408  When both title and uri exist, only title is used.
       
   409  */
       
   410 void TestMpNowPlayingBackEnd::testHandleMediaLTitleUri()
       
   411 {
       
   412     QSignalSpy spyTitle(mTest, SIGNAL(titleChanged(QString)));
       
   413     QSignalSpy spyArtist(mTest, SIGNAL(artistChanged(QString)));
       
   414     mTestPrivate->mPreviousState = Playing;
       
   415     mTestPrivate->mPlaybackUtility->Source()->setObserver(mTestPrivate);
       
   416     mTestPrivate->mPlaybackUtility->Source()->sendHandleMediaL(true, true, false);
       
   417 
       
   418     QCOMPARE(spyTitle.count(), 1);
       
   419     QCOMPARE(spyTitle.at(0).at(0).toString(), QString("Title"));
       
   420     QCOMPARE(spyArtist.count(), 1);
       
   421     QCOMPARE(spyArtist.at(0).at(0).toString(), QString());
       
   422 }
       
   423 
       
   424 /*!
       
   425  Tests HandleMediaL callback with new uri. Good case.
       
   426  */
       
   427 void TestMpNowPlayingBackEnd::testHandleMediaLUri()
       
   428 {
       
   429     QSignalSpy spyTitle(mTest, SIGNAL(titleChanged(QString)));
       
   430     QSignalSpy spyArtist(mTest, SIGNAL(artistChanged(QString)));
       
   431     mTestPrivate->mPreviousState = Playing;
       
   432     mTestPrivate->mPlaybackUtility->Source()->setObserver(mTestPrivate);
       
   433     mTestPrivate->mPlaybackUtility->Source()->sendHandleMediaL(false, true, false);
       
   434 
       
   435     QCOMPARE(spyTitle.count(), 1);
       
   436     QCOMPARE(spyTitle.at(0).at(0).toString(), QString("Uri"));
       
   437     QCOMPARE(spyArtist.count(), 1);
       
   438     QCOMPARE(spyArtist.at(0).at(0).toString(), QString());
       
   439 }
       
   440 
       
   441 /*!
       
   442  Tests HandleMediaL callback with new artist. Good case.
       
   443  */
       
   444 void TestMpNowPlayingBackEnd::testHandleMediaLArtist()
       
   445 {
       
   446     QSignalSpy spyTitle(mTest, SIGNAL(titleChanged(QString)));
       
   447     QSignalSpy spyArtist(mTest, SIGNAL(artistChanged(QString)));
       
   448     mTestPrivate->mPreviousState = Playing;
       
   449     mTestPrivate->mPlaybackUtility->Source()->setObserver(mTestPrivate);
       
   450     mTestPrivate->mPlaybackUtility->Source()->sendHandleMediaL(false, false, true);
       
   451 
       
   452     QCOMPARE(spyTitle.count(), 1);
       
   453     QCOMPARE(spyTitle.at(0).at(0).toString(), QString());
       
   454     QCOMPARE(spyArtist.count(), 1);
       
   455     QCOMPARE(spyArtist.at(0).at(0).toString(), QString("Artist"));
       
   456 }
       
   457 
       
   458 // End of file