photosgallery/slideshow/engine/tsrc/t_cshwmusiccontrol/t_cshwmusiccontrol.cpp
changeset 0 4e91876724a2
child 13 bcb43dc84c44
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2007-2007 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:  Test for music control for the slideshow
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 //  CLASS HEADER
       
    21 #include "t_cshwmusiccontrol.h"
       
    22 
       
    23 //  EXTERNAL INCLUDES
       
    24 #include <EUnitMacros.h>
       
    25 #include <EUnitDecorators.h>
       
    26 
       
    27 //  INTERNAL INCLUDES
       
    28 #include "shwevent.h"
       
    29 
       
    30 namespace
       
    31 	{
       
    32 	_LIT(KFileName, "c:\\knightrider.mp3");	
       
    33 	}
       
    34 
       
    35 // CONSTRUCTION
       
    36 T_CShwMusicControl* T_CShwMusicControl::NewL()
       
    37     {
       
    38     T_CShwMusicControl* self = T_CShwMusicControl::NewLC();
       
    39     CleanupStack::Pop();
       
    40 
       
    41     return self;
       
    42     }
       
    43 
       
    44 T_CShwMusicControl* T_CShwMusicControl::NewLC()
       
    45     {
       
    46     T_CShwMusicControl* self = new( ELeave ) T_CShwMusicControl;
       
    47     CleanupStack::PushL( self );
       
    48 
       
    49     self->ConstructL();
       
    50 
       
    51     return self;
       
    52     }
       
    53 
       
    54 
       
    55 
       
    56 // Destructor (virtual by CBase)
       
    57 T_CShwMusicControl::~T_CShwMusicControl()
       
    58     {
       
    59     }
       
    60 
       
    61 
       
    62 
       
    63 // Default constructor
       
    64 T_CShwMusicControl::T_CShwMusicControl() 
       
    65 				   :iMusicOn(EFalse),
       
    66 				    iCurrentVolume(KErrNotFound),
       
    67 				    iMaxVolume(KErrNotFound),
       
    68 				    iPrevVolume(KErrNotFound)
       
    69     {
       
    70     }
       
    71 
       
    72 // Second phase construct
       
    73 void T_CShwMusicControl::ConstructL()
       
    74     {
       
    75     // The ConstructL from the base class CEUnitTestSuiteClass must be called.
       
    76     // It generates the test case table.
       
    77     CEUnitTestSuiteClass::ConstructL();
       
    78     }
       
    79 
       
    80 void T_CShwMusicControl::SendEventL(MShwEvent* aEvent)
       
    81 	{
       
    82 	iEvent = aEvent->CloneLC();
       
    83 	CleanupStack::Pop();
       
    84 	}
       
    85 
       
    86 TBool gMusicOnCalled = EFalse;
       
    87 void T_CShwMusicControl::MusicOnL()
       
    88 	{
       
    89 	EUNIT_PRINT(_L("Music is ON"));
       
    90 	iMusicOn = ETrue;
       
    91 	gMusicOnCalled = ETrue;
       
    92 	// leave if error code set
       
    93     User::LeaveIfError( iLeaveInObserver );
       
    94 	}
       
    95 	
       
    96 TBool gMusicOffCalled = EFalse;
       
    97 void T_CShwMusicControl::MusicOff()
       
    98 	{
       
    99 	EUNIT_PRINT(_L("Music is OFF"));
       
   100 	iMusicOn = EFalse;
       
   101 	gMusicOffCalled = ETrue;
       
   102 	}
       
   103 
       
   104 
       
   105 void T_CShwMusicControl::MusicVolume(TInt aCurrentVolume, TInt aMaxVolume)
       
   106 	{
       
   107 	iPrevVolume    = iCurrentVolume;
       
   108 	iCurrentVolume = aCurrentVolume;
       
   109 	iMaxVolume     = aMaxVolume;
       
   110 	EUNIT_PRINT(_L("current volume %d"),aCurrentVolume);
       
   111 	if(iWait.IsStarted())
       
   112 		{
       
   113 		iWait.AsyncStop();
       
   114 		}
       
   115 	}
       
   116 
       
   117 void T_CShwMusicControl::ErrorWithTrackL( TInt aErrorCode )
       
   118 	{
       
   119 	iErrorCode = aErrorCode;
       
   120 	}
       
   121 
       
   122 void T_CShwMusicControl::Empty()
       
   123 	{
       
   124 	}
       
   125 
       
   126 void T_CShwMusicControl::SetupL()
       
   127     {
       
   128     // reset current and max volume
       
   129     iCurrentVolume = KErrNotFound;
       
   130     iMaxVolume = KErrNotFound;
       
   131     // reset error code
       
   132     iErrorCode = KErrNone;
       
   133     // reset leave flag
       
   134     iLeaveInObserver = KErrNone;
       
   135     // reset state flags
       
   136     gMusicOnCalled = EFalse;
       
   137     gMusicOffCalled = EFalse;
       
   138     
       
   139     iCShwMusicControl = CShwMusicControl::NewL( *this, KFileName );
       
   140 	// set the event queue - inherited from parent CShwEventPublisherBase
       
   141 	iCShwMusicControl->SetEventQueue(this);
       
   142     }
       
   143 
       
   144 
       
   145 void T_CShwMusicControl::Teardown()
       
   146     {
       
   147     delete iCShwMusicControl;
       
   148     iCShwMusicControl = NULL;
       
   149     
       
   150     delete iEvent;
       
   151     iEvent = NULL;
       
   152     }
       
   153 
       
   154 void T_CShwMusicControl::TestConstructL()
       
   155 	{
       
   156 	EUNIT_PRINT(_L("CShwMusicControl::NewL"));
       
   157 	// create 
       
   158     iCShwMusicControl = CShwMusicControl::NewL(*this, KFileName);
       
   159 	// set the event queue
       
   160 	iCShwMusicControl->SetEventQueue(this);
       
   161 	// test that object exists
       
   162 	EUNIT_ASSERT_DESC(iCShwMusicControl, "object created");
       
   163 	// test that no error
       
   164 	EUNIT_ASSERT_EQUALS_DESC( 
       
   165 	    KErrNone, iErrorCode, "if there was error, make sure track file exists");
       
   166 	// delete music control
       
   167     delete iCShwMusicControl;
       
   168     iCShwMusicControl = NULL;
       
   169     // EUnit checks for memory leaks
       
   170 	}
       
   171 
       
   172 void T_CShwMusicControl::T_NotifyLL()
       
   173     {
       
   174 	// test that no error
       
   175 	EUNIT_ASSERT_EQUALS_DESC( 
       
   176 	    KErrNone, iErrorCode, "if there was error, make sure track file exists");
       
   177     // send start event
       
   178     TShwEventStart start;
       
   179     iCShwMusicControl->NotifyL(&start);
       
   180 	// Need to let the scheduler loop to get first volume callback
       
   181 	iWait.Start(); // Wait for notification of volume
       
   182 	EUNIT_ASSERT(iMusicOn == ETrue);
       
   183     EUNIT_ASSERT(iCurrentVolume != KErrNotFound && iMaxVolume != KErrNotFound);
       
   184     }
       
   185 
       
   186 void T_CShwMusicControl::T_NotifyL1L()
       
   187     {
       
   188 	// test that no error
       
   189 	EUNIT_ASSERT_EQUALS_DESC( 
       
   190 	    KErrNone, iErrorCode, "if there was error, make sure track file exists");
       
   191     // if should evaluate false: if (event)
       
   192     MShwEvent* iParam1 = NULL;
       
   193     iCShwMusicControl->NotifyL(iParam1);
       
   194 
       
   195 	EUNIT_ASSERT_DESC( !iEvent, "event was not received" );
       
   196 
       
   197     // test also HandlePropertyL with unsupported property
       
   198     iCShwMusicControl->HandlePropertyL( EPbPropertyMute, 0, 0 );
       
   199     // test that volume was not send
       
   200     EUNIT_ASSERT( iCurrentVolume == KErrNotFound && iMaxVolume == KErrNotFound );
       
   201     }
       
   202     
       
   203 void T_CShwMusicControl::T_NotifyL2L()
       
   204     {
       
   205 	// test that no error
       
   206 	EUNIT_ASSERT_EQUALS_DESC( 
       
   207 	    KErrNone, iErrorCode, "if there was error, make sure track file exists");
       
   208     // send start event
       
   209     TShwEventStart start;
       
   210     iCShwMusicControl->NotifyL( &start );
       
   211 	// Need to let the scheduler loop to get first volume callback
       
   212 	iWait.Start();
       
   213 	// check that we got the volume
       
   214     EUNIT_ASSERT(iCurrentVolume != KErrNotFound && iMaxVolume != KErrNotFound);
       
   215 
       
   216     // send pause
       
   217     TShwEventPause pause;
       
   218     iCShwMusicControl->NotifyL( &pause );
       
   219 	EUNIT_ASSERT( iMusicOn == EFalse );
       
   220 
       
   221     // send resume
       
   222     TShwEventResume resume;
       
   223     iCShwMusicControl->NotifyL( &resume );
       
   224 
       
   225 	EUNIT_ASSERT(iMusicOn != EFalse );
       
   226 	}
       
   227 
       
   228 void T_CShwMusicControl::T_NotifyL3L()
       
   229     {
       
   230 	// test that no error
       
   231 	EUNIT_ASSERT_EQUALS_DESC( 
       
   232 	    KErrNone, iErrorCode, "if there was error, make sure track file exists");
       
   233     // send resume before start
       
   234     TShwEventResume resume;
       
   235     iCShwMusicControl->NotifyL( &resume );
       
   236 	EUNIT_ASSERT_DESC( iMusicOn == ETrue, "music is on" );
       
   237 
       
   238     // Send pause before start, does not pause
       
   239     TShwEventPause pause;
       
   240     iCShwMusicControl->NotifyL( &pause );
       
   241 	EUNIT_ASSERT_DESC( iMusicOn == EFalse, "music is off" );
       
   242 
       
   243     // send start event
       
   244     TShwEventStart start;
       
   245     iCShwMusicControl->NotifyL( &start );
       
   246 	// Need to let the scheduler loop to get first volume callback
       
   247 	iWait.Start();
       
   248 
       
   249     // Second subsequent pause request 
       
   250     iCShwMusicControl->NotifyL( &pause );
       
   251 	EUNIT_ASSERT_DESC( iMusicOn == EFalse, "music is now paused" );
       
   252 	}
       
   253 
       
   254 void T_CShwMusicControl::T_NotifyL4L()
       
   255     {
       
   256 	// test that no error
       
   257 	EUNIT_ASSERT_EQUALS_DESC( 
       
   258 	    KErrNone, iErrorCode, "if there was error, make sure track file exists");
       
   259     // send start event
       
   260     TShwEventStart start;
       
   261     iCShwMusicControl->NotifyL(&start);
       
   262 	// Need to let the scheduler loop to get first volume callback
       
   263 	iWait.Start();
       
   264 	
       
   265     // if should evaluate false: if (event)
       
   266     TShwEventVolumeDown vol;
       
   267     iCShwMusicControl->NotifyL(&vol);
       
   268 
       
   269 	// Need to let the scheduler loop to get another volume callback
       
   270 	iWait.Start();
       
   271 	EUNIT_ASSERT_GREATER_DESC( iPrevVolume, iCurrentVolume, "volume should decrease" );
       
   272 	EUNIT_ASSERT_NOT_EQUALS( KErrNotFound, iMaxVolume );
       
   273 	 
       
   274 	iMaxVolume = KErrNotFound;
       
   275     }
       
   276     
       
   277 void T_CShwMusicControl::T_NotifyL5L()
       
   278     {
       
   279 	// test that no error
       
   280 	EUNIT_ASSERT_EQUALS_DESC( 
       
   281 	    KErrNone, iErrorCode, "if there was error, make sure track file exists");
       
   282     // send start event
       
   283     TShwEventStart start;
       
   284     iCShwMusicControl->NotifyL(&start);
       
   285 	// Need to let the scheduler loop to get first volume callback
       
   286 	iWait.Start();
       
   287 	
       
   288     // if should evaluate false: if (event)
       
   289     TShwEventVolumeUp vol;
       
   290     iCShwMusicControl->NotifyL(&vol);
       
   291 
       
   292 	// Need to let the scheduler loop to get another volume callback
       
   293 	iWait.Start();
       
   294 	EUNIT_ASSERT_GREATER_DESC( iCurrentVolume, iPrevVolume, "volume should increase" );
       
   295 	EUNIT_ASSERT_NOT_EQUALS( KErrNotFound, iMaxVolume );
       
   296 	iMaxVolume = KErrNotFound;
       
   297 	}
       
   298         
       
   299 void T_CShwMusicControl::T_ExtendedPlayL()
       
   300     {
       
   301 	// test that no error
       
   302 	EUNIT_ASSERT_EQUALS_DESC( 
       
   303 	    KErrNone, iErrorCode, "if there was error, make sure track file exists");
       
   304 
       
   305     TShwEventStart start;
       
   306     iCShwMusicControl->NotifyL( &start );
       
   307 	
       
   308 	if (!iTimer)
       
   309 		{
       
   310 		iTimer = CPeriodic::NewL(EPriorityNormal);
       
   311 		}
       
   312 
       
   313     // play for 10 seconds
       
   314 	const TInt KPlayPeriod = 10 * 1000000;    // Microseconds
       
   315 	TCallBack callBack(StopPlaying, this);
       
   316 	iTimer->Start(KPlayPeriod, KPlayPeriod, callBack);		
       
   317 	
       
   318 	iPlayWait.Start();
       
   319 	
       
   320 	delete iTimer;
       
   321     iTimer = NULL;
       
   322 	
       
   323     TShwEventPause pause;
       
   324     iCShwMusicControl->NotifyL(&pause);
       
   325 	EUNIT_ASSERT(iMusicOn == EFalse);
       
   326 
       
   327 	}   
       
   328 
       
   329 TInt T_CShwMusicControl::StopPlaying(TAny* aMusicControl)
       
   330     {
       
   331     T_CShwMusicControl* self = reinterpret_cast<T_CShwMusicControl*>(aMusicControl);
       
   332 
       
   333 	if(self->iPlayWait.IsStarted())
       
   334 		{
       
   335 		self->iPlayWait.AsyncStop();
       
   336 		}
       
   337 
       
   338     return KErrNone;
       
   339     }    
       
   340 
       
   341 void T_CShwMusicControl::T_ErrorInFileL()
       
   342     {
       
   343     // need to reset state as the setup is empty
       
   344     gMusicOnCalled = EFalse;
       
   345     // file that does not exist
       
   346     _LIT( KErrorFileName, "C:\\juubaduuba.mp123" );
       
   347 	// create 
       
   348     iCShwMusicControl = CShwMusicControl::NewL( *this, KErrorFileName );
       
   349 	// set the event queue
       
   350 	iCShwMusicControl->SetEventQueue( this );
       
   351 	// test that object exists
       
   352 	EUNIT_ASSERT_DESC(iCShwMusicControl, "object created");
       
   353 	
       
   354 	// test that error was called
       
   355 	// test that no error
       
   356 	EUNIT_ASSERT_EQUALS_DESC( 
       
   357 	    KErrNotFound, iErrorCode, "track should not exist");
       
   358 
       
   359     // test that start is a no-op
       
   360     TShwEventStart start;
       
   361     iCShwMusicControl->NotifyL( &start );
       
   362 	
       
   363 	// test that music is off
       
   364     EUNIT_ASSERT_DESC( gMusicOnCalled == EFalse, "music on was not called" );
       
   365 
       
   366 	// delete music control
       
   367     delete iCShwMusicControl;
       
   368     iCShwMusicControl = NULL;
       
   369     // EUnit checks for memory leaks
       
   370 	}   
       
   371 
       
   372 void T_CShwMusicControl::T_LeaveInObserverL()
       
   373     {
       
   374     // make observer leave
       
   375     iLeaveInObserver = KErrCorrupt;
       
   376 
       
   377     // send start event
       
   378     TShwEventStart start;
       
   379     iCShwMusicControl->NotifyL( &start );
       
   380     
       
   381 	// test that music is off
       
   382 	EUNIT_ASSERT_DESC( iMusicOn == EFalse, "music is off");
       
   383     EUNIT_ASSERT_DESC( gMusicOnCalled == ETrue, "music on was called (and it did leave)" );
       
   384     EUNIT_ASSERT_DESC( gMusicOffCalled == ETrue, "music off was called" );
       
   385     // reset flags
       
   386     gMusicOnCalled = EFalse;
       
   387     gMusicOffCalled = EFalse;
       
   388 
       
   389     // make observer not leave
       
   390     iLeaveInObserver = KErrNone;
       
   391 
       
   392     // resend start event
       
   393     iCShwMusicControl->NotifyL( &start );
       
   394 	// test that music is on
       
   395 	EUNIT_ASSERT_DESC( iMusicOn == ETrue, "music is on");
       
   396     EUNIT_ASSERT_DESC( gMusicOnCalled == ETrue, "music on was called" );
       
   397     EUNIT_ASSERT_DESC( gMusicOffCalled == EFalse, "music off was not called" );
       
   398     // reset flags
       
   399     gMusicOnCalled = EFalse;
       
   400     gMusicOffCalled = EFalse;
       
   401 
       
   402     // send pause
       
   403     TShwEventPause pause;
       
   404     iCShwMusicControl->NotifyL( &pause );
       
   405 	EUNIT_ASSERT_DESC( iMusicOn == EFalse, "music is off" );
       
   406     EUNIT_ASSERT_DESC( gMusicOnCalled == EFalse, "music on was not called" );
       
   407     EUNIT_ASSERT_DESC( gMusicOffCalled == ETrue, "music off was called" );
       
   408     // reset flags
       
   409     gMusicOnCalled = EFalse;
       
   410     gMusicOffCalled = EFalse;
       
   411 
       
   412     // make observer leave
       
   413     iLeaveInObserver = KErrCorrupt;
       
   414 
       
   415     // send resume
       
   416     TShwEventResume resume;
       
   417     iCShwMusicControl->NotifyL( &resume );
       
   418 	EUNIT_ASSERT_DESC( iMusicOn == EFalse, "music is off" );
       
   419     EUNIT_ASSERT_DESC( gMusicOnCalled == ETrue, "music on was called (and it did leave)" );
       
   420     EUNIT_ASSERT_DESC( gMusicOffCalled == ETrue, "music off was called" );
       
   421     // reset flags
       
   422     gMusicOnCalled = EFalse;
       
   423     gMusicOffCalled = EFalse;
       
   424 
       
   425     // send volume up
       
   426     TShwEventVolumeUp vol;
       
   427     iCShwMusicControl->NotifyL( &vol );
       
   428     EUNIT_ASSERT_DESC( gMusicOnCalled == EFalse, "music on was not called" );
       
   429     EUNIT_ASSERT_DESC( gMusicOffCalled == EFalse, "music off was not called" );
       
   430     // reset flags
       
   431     gMusicOnCalled = EFalse;
       
   432     gMusicOffCalled = EFalse;
       
   433 
       
   434     // make observer not leave
       
   435     iLeaveInObserver = KErrNone;
       
   436 
       
   437     // send resume
       
   438     iCShwMusicControl->NotifyL( &resume );
       
   439 	EUNIT_ASSERT_DESC( iMusicOn == ETrue, "music is on" );
       
   440     EUNIT_ASSERT_DESC( gMusicOnCalled == ETrue, "music on was called" );
       
   441     EUNIT_ASSERT_DESC( gMusicOffCalled == EFalse, "music off was not called" );
       
   442 	}
       
   443 
       
   444 //  TEST TABLE
       
   445 EUNIT_BEGIN_TEST_TABLE(
       
   446     T_CShwMusicControl,
       
   447     "Test suite for CShwMusicControl",
       
   448     "UNIT" )
       
   449 
       
   450 /*
       
   451  Commented out as MPX crashes with Kern-exec 0
       
   452 EUNIT_ALLOC_TEST(
       
   453     "Constructor test",
       
   454     "CShwMusicControl",
       
   455     "NewL",
       
   456     "FUNCTIONALITY",
       
   457     Empty, TestConstructL, Teardown )
       
   458 */
       
   459 // these cant be decorated as the EUnit
       
   460 // scheduler does not implement Error method
       
   461 EUNIT_NOT_DECORATED_TEST(
       
   462     "NotifyL - test 0",
       
   463     "CShwMusicControl",
       
   464     "NotifyL",
       
   465     "FUNCTIONALITY",
       
   466     SetupL, T_NotifyLL, Teardown)
       
   467 
       
   468 EUNIT_NOT_DECORATED_TEST(
       
   469     "NotifyL - test 1",
       
   470     "CShwMusicControl",
       
   471     "NotifyL",
       
   472     "FUNCTIONALITY",
       
   473     SetupL, T_NotifyL1L, Teardown)
       
   474 
       
   475 EUNIT_NOT_DECORATED_TEST(
       
   476     "NotifyL - test 2",
       
   477     "CShwMusicControl",
       
   478     "NotifyL",
       
   479     "FUNCTIONALITY",
       
   480     SetupL, T_NotifyL2L, Teardown)
       
   481 
       
   482 EUNIT_NOT_DECORATED_TEST(
       
   483     "NotifyL - test 3",
       
   484     "CShwMusicControl",
       
   485     "NotifyL",
       
   486     "FUNCTIONALITY",
       
   487     SetupL, T_NotifyL3L, Teardown)
       
   488 
       
   489 EUNIT_NOT_DECORATED_TEST(
       
   490     "NotifyL - test 4",
       
   491     "CShwMusicControl",
       
   492     "NotifyL",
       
   493     "FUNCTIONALITY",
       
   494     SetupL, T_NotifyL4L, Teardown)
       
   495 
       
   496 EUNIT_NOT_DECORATED_TEST(
       
   497     "NotifyL - test 5",
       
   498     "CShwMusicControl",
       
   499     "NotifyL",
       
   500     "FUNCTIONALITY",
       
   501     SetupL, T_NotifyL5L, Teardown)
       
   502 
       
   503 EUNIT_NOT_DECORATED_TEST(
       
   504     "Test error in file",
       
   505     "CShwMusicControl",
       
   506     "ConstructL, NotifyL",
       
   507     "FUNCTIONALITY",
       
   508     Empty, T_ErrorInFileL, Teardown)
       
   509 
       
   510 EUNIT_NOT_DECORATED_TEST(
       
   511     "Test leave in observer",
       
   512     "CShwMusicControl",
       
   513     "NotifyL",
       
   514     "FUNCTIONALITY",
       
   515     SetupL, T_LeaveInObserverL, Teardown )
       
   516 
       
   517 /*
       
   518 commented out as no added value for the time beeing
       
   519 EUNIT_NOT_DECORATED_TEST(
       
   520     "Play for multitple seconds",
       
   521     "CShwMusicControl",
       
   522     "NotifyL - test 6",
       
   523     "FUNCTIONALITY",
       
   524     SetupL, T_ExtendedPlayL, Teardown)
       
   525 */
       
   526     
       
   527     
       
   528 EUNIT_END_TEST_TABLE
       
   529 
       
   530 //  END OF FILE