mmlibs/mmfw/tsrc/mmfunittest/MidiClnt/TestStepMidiDRM.cpp
changeset 0 b8ed18f6c07b
equal deleted inserted replaced
-1:000000000000 0:b8ed18f6c07b
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <testframework.h>
       
    17 
       
    18 #include "TSU_MMFMIDICLNT.h"
       
    19 #include "TS_MMFMIDICLNTsuite.h"
       
    20 
       
    21 #include "TestMidiClientUtility.h"
       
    22 
       
    23 _LIT(KDrmSectName, "SectionDRM");
       
    24 _LIT(KDrmFileKey, "MidiDrmContent");
       
    25 
       
    26 const TInt K1Second		=1000000;
       
    27 const TInt K5Seconds	= 5 * K1Second;
       
    28 const TInt K30Seconds	=30 * K1Second;
       
    29 
       
    30 class CCallbackTimer : public CTimer
       
    31    	{
       
    32 public:
       
    33    	static CCallbackTimer* NewL();
       
    34    	~CCallbackTimer();
       
    35    	void SetCb(TCallBack aCallback)
       
    36    		{
       
    37    		iCallback = aCallback;
       
    38    		}
       
    39 	void After(TTimeIntervalMicroSeconds32 aCancelDelay)
       
    40 		{
       
    41 		CTimer::After(aCancelDelay);
       
    42 		}
       
    43 	void After(TTimeIntervalMicroSeconds32 aCancelDelay, TCallBack aCallback);
       
    44 
       
    45 private:
       
    46    CCallbackTimer();
       
    47    // from CActive
       
    48     void RunL();
       
    49 
       
    50 private:
       
    51     TCallBack iCallback; 
       
    52     };
       
    53     
       
    54 CCallbackTimer* CCallbackTimer::NewL()
       
    55     {
       
    56 	CCallbackTimer* self=new (ELeave) CCallbackTimer();
       
    57    	CleanupStack::PushL(self);
       
    58    	self->ConstructL();
       
    59    	CleanupStack::Pop();
       
    60     return self;
       
    61     }
       
    62     
       
    63 CCallbackTimer::CCallbackTimer():CTimer(EPriorityHigh)
       
    64 	{
       
    65 	CActiveScheduler::Add(this);
       
    66 	}
       
    67 
       
    68 CCallbackTimer::~CCallbackTimer()
       
    69     {
       
    70     Cancel();
       
    71     }
       
    72 
       
    73 
       
    74 void CCallbackTimer::After(TTimeIntervalMicroSeconds32 aCancelDelay, TCallBack aCallback)
       
    75     {
       
    76    	iCallback = aCallback;   
       
    77    	CTimer::After(aCancelDelay);
       
    78     }
       
    79 
       
    80 /**
       
    81 *
       
    82 * RunL
       
    83 *
       
    84 **/
       
    85 void CCallbackTimer::RunL()
       
    86     {
       
    87     iCallback.CallBack();
       
    88     }
       
    89 
       
    90 
       
    91 
       
    92 CTestStepDrm::CTestStepDrm(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams):
       
    93 								CTestMmfMidiClntStep(aTestName, aTestType),
       
    94 								iDrmParams(aDrmParams)
       
    95 	{
       
    96 	}
       
    97 
       
    98 TVerdict CTestStepDrm::DoTestStepPreambleL()
       
    99 	{
       
   100 	if (EPass==CTestMmfMidiClntStep::DoTestStepPreambleL())
       
   101 		{
       
   102 		iTimer = CCallbackTimer::NewL();
       
   103 		iTimer->SetCb(TCallBack(TimerGate, this));
       
   104 		User::LeaveIfError( iFs.Connect() );
       
   105 		return EPass;
       
   106 		}
       
   107 	return EFail;
       
   108 	}
       
   109 
       
   110 TVerdict CTestStepDrm::DoTestStepPostambleL()
       
   111 	{
       
   112 	delete iTimer;
       
   113 	iTimer = NULL;
       
   114 	
       
   115 	iFile.Close();
       
   116 	iFs.Close();
       
   117 	return CTestMmfMidiClntStep::DoTestStepPostambleL();
       
   118 	}
       
   119 
       
   120 TVerdict CTestStepDrm::DoTestStepL()
       
   121 	{
       
   122 	if (iDrmParams.iTestMode != TDrmTestParams::EAlloc)
       
   123 		{
       
   124 		return CTestMmfMidiClntStep::DoTestStepL();
       
   125 		}
       
   126 	TVerdict result=EPass;
       
   127 	TInt failCount = 1;
       
   128 	TBool completed = EFalse;
       
   129 	CMidiClientUtility* midiClnt = NULL;
       
   130 	TInt err;
       
   131 	while(!completed)	
       
   132 		{
       
   133 		iError = KErrNone;
       
   134 		__UHEAP_SETFAIL(RHeap::EFailNext ,failCount);
       
   135 		__MM_HEAP_MARK;
       
   136 
       
   137 		TRAP( err, 
       
   138 				midiClnt = CMidiClientUtility::NewL(*this);
       
   139 				result=DoTestL(midiClnt);
       
   140 			);
       
   141 		iTimer->Cancel();
       
   142 		
       
   143  		completed = EFalse;
       
   144 		if (err == KErrNone)
       
   145 			{
       
   146 			TAny *testAlloc = User::Alloc(1); // when this fails, we passed through all allocs within test
       
   147 			if (testAlloc==NULL)
       
   148 				{
       
   149 				completed = ETrue;
       
   150 				}
       
   151 			else
       
   152 				{
       
   153 				completed = ETrue;
       
   154 				User::Free(testAlloc);
       
   155 				}
       
   156 			}
       
   157 		else if (err != KErrNoMemory) // bad error code
       
   158 			{
       
   159 			completed 	= ETrue;
       
   160 			result		= EFail;
       
   161 			}
       
   162 		else
       
   163 			{
       
   164 			++failCount;
       
   165 			}
       
   166 			
       
   167 		delete midiClnt;
       
   168 		midiClnt = NULL;
       
   169 					
       
   170 		__UHEAP_RESET;
       
   171 		__MM_HEAP_MARKEND;
       
   172 		}
       
   173 	return result;
       
   174 	}
       
   175 
       
   176 /*static*/
       
   177 TInt CTestStepDrm::TimerGate(TAny* aPtr)
       
   178 	{
       
   179 	reinterpret_cast<CTestStepDrm*>(aPtr)->OnTimer();
       
   180 	return KErrNone;
       
   181 	}
       
   182 	
       
   183 void CTestStepDrm::OnTimer()
       
   184 	{
       
   185 	iTimer->Cancel();
       
   186 	CActiveScheduler::Stop();
       
   187 	iError = KErrTimedOut;	
       
   188 	}
       
   189 
       
   190 TVerdict CTestStepDrm::OpenFileL(CMidiClientUtility* aMidi)
       
   191 	{
       
   192 	TPtrC fileName;
       
   193 	if (iDrmParams.iFileNameKey==NULL)
       
   194 		{
       
   195 		if(!GetStringFromConfig(KDrmSectName,KDrmFileKey,fileName))
       
   196 			{
       
   197 			return EInconclusive;
       
   198 			}		
       
   199 		}
       
   200 	else
       
   201 		{
       
   202 		if(!GetStringFromConfig(KDrmSectName,TPtrC(iDrmParams.iFileNameKey), fileName))
       
   203 			{
       
   204 			return EInconclusive;
       
   205 			}				
       
   206 		}
       
   207 	if (iDrmParams.iSourceType==TDrmTestParams::EFileName)
       
   208 		{
       
   209 		TPtrC uniqueId(ContentAccess::KDefaultContentObject);
       
   210 		if (iDrmParams.iUniqueId)
       
   211 			{
       
   212 			uniqueId.Set(TPtrC(iDrmParams.iUniqueId));
       
   213 			}
       
   214 		if (iDrmParams.iUniqueId==NULL && iDrmParams.iIntent==TDrmTestParams::KNoIntent)
       
   215 			{
       
   216 			aMidi->OpenFile(TMMFileSource(fileName));
       
   217 			}
       
   218 		else if (iDrmParams.iIntent==TDrmTestParams::KNoIntent)
       
   219 			{
       
   220 			aMidi->OpenFile(TMMFileSource(fileName, uniqueId));
       
   221 			}
       
   222 		else
       
   223 			{
       
   224 			aMidi->OpenFile(TMMFileSource(fileName, uniqueId, iDrmParams.iIntent));
       
   225 			}		
       
   226 		}
       
   227 	else
       
   228 		{
       
   229 		iFile.Close();
       
   230 		User::LeaveIfError( iFs.ShareProtected() );
       
   231 		User::LeaveIfError( iFile.Open(iFs, fileName, EFileRead | EFileShareExclusive) );
       
   232 		
       
   233 		TPtrC uniqueId(ContentAccess::KDefaultContentObject);
       
   234 		if (iDrmParams.iUniqueId)
       
   235 			{
       
   236 			uniqueId.Set(TPtrC(iDrmParams.iUniqueId));
       
   237 			}
       
   238 		if (iDrmParams.iUniqueId==NULL && iDrmParams.iIntent==TDrmTestParams::KNoIntent)
       
   239 			{
       
   240 			aMidi->OpenFile(TMMFileHandleSource(iFile));
       
   241 			}
       
   242 		else if (iDrmParams.iIntent==TDrmTestParams::KNoIntent)
       
   243 			{
       
   244 			aMidi->OpenFile(TMMFileHandleSource(iFile, uniqueId));
       
   245 			}
       
   246 		else
       
   247 			{
       
   248 			aMidi->OpenFile(TMMFileHandleSource(iFile, uniqueId, iDrmParams.iIntent));
       
   249 			}				
       
   250 		}
       
   251 
       
   252 	return EPass;	
       
   253 	}
       
   254 
       
   255 
       
   256 /*static*/
       
   257 CTestDrmNegative* CTestDrmNegative::NewL(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams)
       
   258 	{
       
   259 	return new (ELeave) CTestDrmNegative(aTestName, aTestType, aDrmParams);
       
   260 	}
       
   261 
       
   262 CTestDrmNegative::CTestDrmNegative(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams):
       
   263 						CTestStepDrm(aTestName, aTestType, aDrmParams)
       
   264 	{
       
   265 	}
       
   266 
       
   267 void CTestDrmNegative::MmcuoStateChanged(TMidiState /*aOldState*/,TMidiState /*aNewState*/, const TTimeIntervalMicroSeconds& /*aTime*/,TInt aError)
       
   268 	{
       
   269 	iError = aError;
       
   270 	CActiveScheduler::Stop();
       
   271 	}
       
   272 
       
   273 TVerdict CTestDrmNegative::DoTestL(CMidiClientUtility* aMidi)
       
   274 	{
       
   275 	iTimer->After( TTimeIntervalMicroSeconds32(K5Seconds) );
       
   276 	TVerdict result=OpenFileL(aMidi);
       
   277 	if (result!=EPass)
       
   278 		{
       
   279 		iTimer->Cancel();
       
   280 		return result; 
       
   281 		}
       
   282 	CActiveScheduler::Start();
       
   283 
       
   284 	if (iError!=iDrmParams.iExpectedError)
       
   285 		{
       
   286 		result=EFail;
       
   287 		ERR_PRINTF3(_L("Open() gave error %d (expected %d)"),iError, iDrmParams.iExpectedError);
       
   288 		}
       
   289 	iTimer->Cancel();
       
   290 	return result;
       
   291 	}
       
   292 
       
   293 
       
   294 /*static*/
       
   295 CTestDrmPlay* CTestDrmPlay::NewL(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams)
       
   296 	{
       
   297 	return new (ELeave) CTestDrmPlay(aTestName, aTestType, aDrmParams);
       
   298 	}
       
   299 
       
   300 CTestDrmPlay::CTestDrmPlay(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams):
       
   301 						CTestStepDrm(aTestName, aTestType, aDrmParams)
       
   302 	{
       
   303 	}
       
   304 
       
   305 void CTestDrmPlay::MmcuoStateChanged(TMidiState /*aOldState*/,TMidiState aNewState, const TTimeIntervalMicroSeconds& /*aTime*/,TInt aError)
       
   306 	{
       
   307 	iError 		= aError;
       
   308 	iLatestState= aNewState;
       
   309 	CActiveScheduler::Stop();
       
   310 	}
       
   311 
       
   312 TVerdict CTestDrmPlay::PlayL(CMidiClientUtility* aMidi, TTimeIntervalMicroSeconds32 aStopAfter)
       
   313 	{
       
   314 	iTimer->Cancel();
       
   315 	iTimer->After(aStopAfter);
       
   316 	
       
   317 	TVerdict result=OpenFileL(aMidi);
       
   318 	if (result!=EPass)
       
   319 		{
       
   320 		iTimer->Cancel();
       
   321 		return result; 
       
   322 		}
       
   323 	CActiveScheduler::Start();
       
   324 	User::LeaveIfError(iError);
       
   325 	
       
   326 	aMidi->Play();
       
   327 	CActiveScheduler::Start();
       
   328 	User::LeaveIfError(iError);
       
   329 	if (iLatestState != EMidiStateOpenPlaying)
       
   330 		{
       
   331 		iTimer->Cancel();
       
   332 		ERR_PRINTF3(_L("Play() got state %d (expected %d)"),iLatestState, EMidiStateOpenPlaying);	
       
   333 		return EFail;
       
   334 		}
       
   335 	
       
   336 	CActiveScheduler::Start();
       
   337 	
       
   338 	iTimer->Cancel();
       
   339 	if (iError==KErrNoMemory && iDrmParams.iTestMode==TDrmTestParams::EAlloc)
       
   340 		{
       
   341 		User::Leave(iError);
       
   342 		}
       
   343 		
       
   344 	return result;
       
   345 	}
       
   346 
       
   347 TVerdict CTestDrmPlay::DoTestL(CMidiClientUtility* aMidi)
       
   348 	{
       
   349 	TVerdict res=EPass;
       
   350 	TInt i=iDrmParams.iNumPlayLoops > 0 ? iDrmParams.iNumPlayLoops : 1;
       
   351 	for (; i && res==EPass && iError==KErrNone; --i)
       
   352 		{
       
   353 		TRAP( iError,
       
   354 				res=PlayL(aMidi, TTimeIntervalMicroSeconds32(K30Seconds));
       
   355 			);
       
   356 		}
       
   357 	if (iError==KErrNoMemory && iDrmParams.iTestMode==TDrmTestParams::EAlloc)
       
   358 		{
       
   359 		User::Leave(iError);
       
   360 		}		
       
   361 	if (res==EPass)
       
   362 		{
       
   363 		res=(i==0 && iDrmParams.iExpectedError==iError)? EPass : EFail;
       
   364 		}
       
   365 		
       
   366 	if (iError!=iDrmParams.iExpectedError)
       
   367 		{
       
   368 		ERR_PRINTF4(_L("Play() gave error %d (expected %d) loops left=%d"),iError, iDrmParams.iExpectedError, i);
       
   369 		}		
       
   370 	return res;		
       
   371 	}
       
   372 
       
   373 /*
       
   374 ***** Play() -> Stop()
       
   375 */
       
   376 CTestDrmPlayStop* CTestDrmPlayStop::NewL(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams)
       
   377 	{
       
   378 	return new (ELeave) CTestDrmPlayStop(aTestName, aTestType, aDrmParams);
       
   379 	}
       
   380 
       
   381 
       
   382 CTestDrmPlayStop::CTestDrmPlayStop(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams):
       
   383 					CTestDrmPlay(aTestName, aTestType, aDrmParams)
       
   384 	{
       
   385 	}
       
   386 	
       
   387 void CTestDrmPlayStop::OnTimer()
       
   388 	{
       
   389 	if (iLatestState != EMidiStateOpenPlaying || iStopping)
       
   390 		{
       
   391 		iError = KErrGeneral;
       
   392 		CActiveScheduler::Stop();
       
   393 		ERR_PRINTF3(_L("CTestDrmPlayStop::OnTimer() got err %d stopping %d"),iError, iStopping);	
       
   394 		}
       
   395 	else
       
   396 		{
       
   397 		iMidi->Stop(TTimeIntervalMicroSeconds(100000));
       
   398 		iStopping=ETrue;
       
   399 		iTimer->After(K5Seconds);
       
   400 		}
       
   401 	}
       
   402 	
       
   403 TVerdict CTestDrmPlayStop::DoTestL(CMidiClientUtility* aMidi)
       
   404 	{
       
   405 	iMidi = aMidi;
       
   406 	TVerdict res=PlayL(aMidi, TTimeIntervalMicroSeconds32(K5Seconds));
       
   407 	iTimer->Cancel();
       
   408 	if (res!=EPass)
       
   409 		{
       
   410 		return res;
       
   411 		}
       
   412 	return iError==iDrmParams.iExpectedError ? EPass : EFail;
       
   413 	}
       
   414 
       
   415 /*
       
   416 ***** Play() -> Stop()
       
   417 */
       
   418 
       
   419 /*static*/
       
   420 CTestDrmPlayWithUI* CTestDrmPlayWithUI::NewL(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams)
       
   421 	{
       
   422 	return new (ELeave) CTestDrmPlayWithUI(aTestName, aTestType, aDrmParams);
       
   423 	}
       
   424 
       
   425 CTestDrmPlayWithUI::CTestDrmPlayWithUI(const TDesC& aTestName, const TTestStepType aTestType, const TDrmTestParams& aDrmParams):
       
   426 						CTestStepDrm(aTestName, aTestType, aDrmParams)
       
   427 	{
       
   428 	}
       
   429 
       
   430 void CTestDrmPlayWithUI::MmcuoStateChanged(TMidiState /*aOldState*/,TMidiState aNewState, const TTimeIntervalMicroSeconds& /*aTime*/,TInt aError)
       
   431 	{
       
   432 	iError 		= aError;
       
   433 	iLatestState= aNewState;
       
   434 	CActiveScheduler::Stop();
       
   435 	}
       
   436 
       
   437 TVerdict CTestDrmPlayWithUI::PlayL(CMidiClientUtility* aMidi, TTimeIntervalMicroSeconds32 aStopAfter)
       
   438 	{
       
   439 	iTimer->Cancel();
       
   440 	iTimer->After(aStopAfter);
       
   441 	
       
   442 	TVerdict result=OpenFileL(aMidi);
       
   443 	if (result!=EPass)
       
   444 		{
       
   445 		iTimer->Cancel();
       
   446 		return result; 
       
   447 		}
       
   448 	CActiveScheduler::Start();
       
   449 	User::LeaveIfError(iError);
       
   450 	
       
   451 	MMMFDRMCustomCommand* cmd=aMidi->GetDRMCustomCommand();
       
   452 	if (cmd==NULL)
       
   453 		{
       
   454 		ERR_PRINTF1(_L("GetDRMCustomCommand() returned NULL"));
       
   455 		return EFail;		
       
   456 		}
       
   457 	iError=cmd->SetAgentProperty(ContentAccess::EAgentPropertyAgentUI, ETrue);
       
   458 	// The test agent should return KErrCANotSupported...
       
   459 	if (iError != KErrCANotSupported)
       
   460 		{
       
   461 		ERR_PRINTF2(_L("SetAgentProperty() Error=%d"),iError);
       
   462 		return EFail;		
       
   463 		}
       
   464 	else
       
   465 		{
       
   466 		// ..it has, so flag that there's no error!
       
   467 		iError = KErrNone;
       
   468 		}
       
   469 	User::LeaveIfError(iError);
       
   470 	
       
   471 	aMidi->Play();
       
   472 	CActiveScheduler::Start();
       
   473 	User::LeaveIfError(iError);
       
   474 	
       
   475 	if (iLatestState != EMidiStateOpenPlaying)
       
   476 		{
       
   477 		iTimer->Cancel();
       
   478 		ERR_PRINTF3(_L("Play() got state %d (expected %d)"),iLatestState, EMidiStateOpenPlaying);	
       
   479 		return EFail;
       
   480 		}	
       
   481 	
       
   482 	CActiveScheduler::Start();
       
   483 	
       
   484 	iTimer->Cancel();
       
   485 	if (iError==KErrNoMemory && iDrmParams.iTestMode==TDrmTestParams::EAlloc)
       
   486 		{
       
   487 		User::Leave(iError);
       
   488 		}
       
   489 		
       
   490 	return EPass;
       
   491 	}
       
   492 
       
   493 TVerdict CTestDrmPlayWithUI::DoTestL(CMidiClientUtility* aMidi)
       
   494 	{
       
   495 	TVerdict res=EPass;
       
   496 	TInt i=iDrmParams.iNumPlayLoops > 0 ? iDrmParams.iNumPlayLoops : 1;
       
   497 	for (; i && res==EPass; --i)
       
   498 		{
       
   499 		res=PlayL(aMidi, TTimeIntervalMicroSeconds32(K30Seconds));
       
   500 		}
       
   501 	if (res==EFail)
       
   502 		{
       
   503 		return res;
       
   504 		}
       
   505 	if (iError!=iDrmParams.iExpectedError)
       
   506 		{
       
   507 		res=EFail;
       
   508 		ERR_PRINTF3(_L("Play() gave error %d (expected %d)"),iError, iDrmParams.iExpectedError);
       
   509 		}				
       
   510 	return res;
       
   511 	}
       
   512