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