os/mm/mmlibs/mmfw/tsrc/mmfunittest/AFMT/TSU_MMF_AFMT.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2002-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 // EPOC includes
    17 #include <e32base.h>
    18 
    19 #include <mmf/server/mmffile.h>						// for TMMFFileParams and CMMFFile
    20 #include <mmf/server/mmfformat.h>						// For CMMFFormatDecode
    21 #include <mmf/server/mmfdes.h>							// for TMMFDescriptorParams and CMMFDescriptor
    22 #include <mmf/server/mmfdatasource.h> 
    23 #include <mmf/plugin/mmfformatimplementationuids.hrh>	// for KMmfUidFormatWAVRead UIDs
    24 
    25 
    26 // Test system includes
    27 #include "TSU_MMF_AFMT.h"
    28 #include "TSU_MMF_AFMTSuite.h"
    29 
    30 
    31 /** 
    32  * This function creates a datasource object based on either a descriptor
    33  * or a file.
    34  * @param aFile - If 'ETrue', datasource if file based, else it's descriptor based.
    35  * @param aFilename - If filebased, this is the file name passed to the DataSource
    36  *					  If descriptor based, the data is read from the file.
    37  * @param aOffset - The offset from the start of the file to read (for Descriptor
    38  *				     based DataSources only)
    39  */
    40 void CTestStep_MMF_AFMT::CreateDataSource(const TBool& aFile, const TDesC& aFilename, 
    41 										  const TUint& aOffset)
    42 	{
    43 	if (aFile)
    44 		{ // File based DataSource (making a CMMFFile object)		
    45 		TMMFFileParams fileParams ;
    46 		fileParams.iPath = TFileName(aFilename) ;	
    47 		TMMFFileConfig fileConfig( fileParams ) ;	
    48 
    49 		// Create the iDataSource object.
    50 		iDataSource = STATIC_CAST(CMMFFile*, MDataSource::NewSourceL( KUidMmfFileSource, 
    51 												*STATIC_CAST(TDesC8*,&fileConfig)));
    52 		}
    53 	else 
    54 		{ // Descriptor based DataSource
    55 		// Reads first 4k buffer from file + puts in iData.
    56 		ReadFileToDescriptorL(aFilename, aOffset, KFormatDefaultFrameSize);
    57 
    58 		// Create parameters for the DataSource
    59 		RThread thread;
    60 		TMMFDescriptorParams descParams;
    61 		descParams.iDes  = &iData;			
    62 		descParams.iDesThreadId = thread.Id(); // This thread
    63 		TMMFDescriptorConfig descConfig(descParams);
    64 
    65 		// Create the MDataSource object.
    66 		iDataSource = STATIC_CAST(CMMFDescriptor*, MDataSource::NewSourceL( 
    67 											KUidMmfDescriptorSource, 
    68 											*STATIC_CAST(TDesC8*,&descConfig) 
    69 											));
    70 		}
    71 	}
    72 
    73 
    74 
    75 
    76 /** 
    77  * This function creates a data sink object from a CMmfFile object.
    78  * @param aFile - If 'ETrue', datasource if file based, else it's descriptor based [NOTE 
    79  * use of CMmfDescriptor based sinks is currently not supported]
    80  * @param aFilename - If filebased, this is the file name passed to the DataSource
    81  *					  If descriptor based, the data is read from the file.
    82  * @param aOffset - The offset from the start of the file to read (for Descriptor
    83  *				     based DataSources only)
    84  */
    85 void CTestStep_MMF_AFMT::CreateDataSink(const TBool& aFile, const TDesC& aFilename, 
    86 										  const TUint& /*aOffset*/)
    87 	{
    88 	TInt err = KErrNone;
    89 
    90 	if (aFile)
    91 		{ // File based DataSink (making a CMMFFile object)		
    92 		TMMFFileParams fileParams ;
    93 		fileParams.iPath = TFileName(aFilename) ;	
    94 		TMMFFileConfig fileConfig( fileParams ) ;	
    95 
    96 		TRAP(err, iDataSink = STATIC_CAST(CMMFFile*, MDataSink::NewSinkL( KUidMmfFileSink, 
    97 												*STATIC_CAST(TDesC8*,&fileConfig))));
    98 		}
    99 	else 
   100 		{ // Descriptor based DataSink		
   101 		// Not sure how to code this bit up yet, so leave with Not Supported.
   102 //		User::Leave(KErrNotSupported);	
   103 			// Reads first 4k buffer from file + puts in iData.
   104 //		ReadFileToDescriptorL(aFilename, aOffset, KFormatDefaultFrameSize);
   105 		TRAP(err,
   106 			iDescHBuf = HBufC8::NewL(0x1000);	
   107 			TPtr8 dataBuf = iDescHBuf->Des();	
   108 
   109 			iData.Set(dataBuf);
   110 
   111 			// Create parameters for the DataSource
   112 			RThread thread;
   113 			TMMFDescriptorParams descParams;
   114 			descParams.iDes  = &iData;			
   115 			descParams.iDesThreadId = thread.Id(); // This thread
   116 			TMMFDescriptorConfig descConfig(descParams);
   117 
   118 			// Create the MDataSource object.
   119 			iDataSink = STATIC_CAST(CMMFDescriptor*, MDataSink::NewSinkL( 
   120 												KUidMmfDescriptorSink, 
   121 												*STATIC_CAST(TDesC8*,&descConfig) 
   122 												));
   123 			);
   124 		}
   125 
   126 	// Check for errors
   127 	if (err != KErrNone)
   128 		{
   129 		ERR_PRINTF2(_L("CreateDataSink failed with error code %d"), err);
   130 		}
   131 	}
   132 
   133 
   134 
   135 
   136 /** 
   137  * This function creates a Format object (type of which is specified by 'aUid') and
   138  * a data source or data sink to go with the object.  This function should be used
   139  * as a quick way for setting up objects for testing.
   140  * @param aIsFile -  ETrue if we are using CMmfFile or EFalse if using 
   141  * CMmfDescriptor as the DataSource. NOTE: CMmfDescriptor support has not
   142  * yet been added to this test harness for SINKs.  Generally it's best to just use
   143  * CMmfFile based data sources rather than descriptors.
   144  * @param aFilename - The file which acts as the data sink.  For a CMmfFile
   145  * based object, this is the file that data gets written to.
   146  * @param aUid - The UID of the format object to be created.
   147  */
   148 void CTestStep_MMF_AFMT::CreateObjectL(TBool aIsFile, const TDesC& aFilename, TInt aUid)
   149 	{
   150 	// Use the UID to work out whether we want a Source/Read or Sink/Write pair
   151 	switch (aUid)
   152 		{
   153 		case KMmfUidFormatAUWrite:
   154 		case KMmfUidFormatRAWWrite:
   155 		case KMmfUidFormatWAVWrite:
   156 			// Create the CMMFWavFormatWrite object.
   157 			CreateEncodeObjectL(aIsFile, aFilename, aUid); 
   158 			iDecode = EFalse;
   159 			break;
   160 
   161 		case KMmfUidFormatAURead:
   162 		case KMmfUidFormatRAWRead:
   163 		case KMmfUidFormatWAVRead:
   164 			// Create the CMMFWavFormatRead object.
   165 			CreateDecodeObjectL(aIsFile, aFilename, aUid); 
   166 			iDecode = ETrue;
   167 			break;
   168 		default:
   169 			INFO_PRINTF1(_L("Leaving from Setup as UID not supported"));
   170 			User::Leave(KErrNotSupported);
   171 		}
   172 	}
   173 
   174 
   175 
   176 /** 
   177  * This function is used as a quick way to setup objects for testing.
   178  * @param aFilename - The file which acts as the data sink.  For a CMmfFile
   179  * based object, this is the file that data gets read/written.
   180  * @param aUid - The UID of the format object to be created.
   181  * @return TVerdict - Will always return EPass unless it leaves.
   182  */
   183 TVerdict CTestStep_MMF_AFMT::SetupL(const TDesC& aFilename, TInt aUid)
   184 	{
   185 	CreateObjectL(ETrue, aFilename, aUid);
   186 	return EPass;
   187 	}
   188 
   189 
   190 
   191 /**
   192  * This function deletes all of the pointer member variables which may have been 
   193  * initialised.
   194  */
   195 void CTestStep_MMF_AFMT::Cleanup()
   196 	{	
   197 	if (iFormatDec)
   198 		{
   199 		iFormatDec->SourceThreadLogoff();
   200 		}
   201 	if (iFormatEnc)
   202 		{
   203 		iFormatEnc->SourceThreadLogoff();
   204 		}
   205 
   206 
   207 	// Tidyup
   208 	delete iDataSource;		iDataSource = NULL;
   209 	delete iFormatDec;		iFormatDec = NULL;
   210 	delete iFormatEnc;		iFormatEnc = NULL;
   211 	delete iDataSink;		iDataSink = NULL;
   212 	delete iDescHBuf;		iDescHBuf = NULL;
   213 	}
   214 
   215 
   216 
   217 
   218 
   219 /**
   220  * This function reads from a source file into the member variable heap buffer, 'iDescHBuf'.
   221  * This can be used to compare the contents of a file with a buffer for example.
   222  * @param aFilename - The file to read from.
   223  * @param aPosition - Where to start reading from in the file.
   224  * @param aSize - How much of the file to read (in bytes) though if you pass '-1', it will
   225  * try and read the whole file from 'aPosition' onwards.
   226  */
   227 void CTestStep_MMF_AFMT::ReadFileToDescriptorL(const TDesC& aFilename, 
   228 											   TInt aPosition, 
   229 											   TInt aSize)
   230 	{	
   231 	RFs rfs;
   232 	rfs.Connect();
   233 	RFile file;
   234 
   235 	// Try to open the file.
   236 	User::LeaveIfError( file.Open(rfs,aFilename,EFileRead|EFileShareAny) );
   237 
   238 	// Create the databuffer in which to store the data.
   239 	TInt fileSize = 0;
   240 	file.Size(fileSize);
   241 	if (aSize == -1)  // if -1, use whole file size
   242 		iDescHBuf = HBufC8::NewL(fileSize-aPosition);	
   243 	else 
   244 		iDescHBuf = HBufC8::NewL(aSize);	
   245 	TPtr8 dataBuf = iDescHBuf->Des();	
   246 
   247 	// Check we are not trying to read past the end of the file...
   248 	if (fileSize< (aPosition+aSize) )
   249 		User::Leave(KErrEof);	
   250 
   251 	// Seek to the place we want to start reading from
   252 	TInt offset = aPosition;
   253 	file.Seek(ESeekStart, offset);
   254 
   255 	// Read the data from the file to the data buffer
   256 	
   257 	if(aSize == -1)
   258 		{
   259 		User::LeaveIfError(file.Read(dataBuf,fileSize-aPosition));
   260 		}
   261 	else
   262 		{ 
   263 		User::LeaveIfError(file.Read(dataBuf, aSize));	
   264 		}
   265 	
   266 	iData.Set(dataBuf);
   267 	
   268 	file.Close();
   269 	rfs.Close();	
   270 	}
   271 
   272 
   273 
   274 
   275 
   276 /**
   277  * This function can be used to make sure that a specified file is not present
   278  * before a set of tests are run.
   279  * @param aFilename - The name of the file to delete/check is not present.
   280  */
   281 void CTestStep_MMF_AFMT::DeleteFileL(const TDesC& aFilename)
   282 	{
   283 	RFs fs;	
   284 	User::LeaveIfError( fs.Connect() );
   285 	TInt ret = fs.Delete(aFilename);
   286 	if (ret != KErrNone && ret != KErrNotFound)
   287 		User::Leave(ret);
   288 	}
   289 
   290 
   291 
   292 
   293 /** 
   294  * This function creates a datasource object based on either a descriptor
   295  * or a file.
   296  * @param aIsFile -  ETrue if we are using CMmfFile or EFalse if using 
   297  * CMmfDescriptor as the DataSource
   298  * @param aFilename - The file which acts as the data source.  For a CMmfFile
   299  * based object, this IS the file.  For a CMmfDescriptor based object, this
   300  * file contains the data that is read into the descriptor for test purposes.
   301  * @param aUid - The UID of the format object to be created.
   302  */
   303 void CTestStep_MMF_AFMT::CreateDecodeObjectL(TBool aIsFile, const TDesC& aFilename, TInt aUid)
   304 	{	
   305 	// Create the datasource (stored in iDataSource)
   306 	CreateDataSource(aIsFile, aFilename);
   307 
   308 	iUID = TUid::Uid(aUid);
   309 
   310 	// Make the NewL call.
   311 	iFormatDec = CMMFFormatDecode::NewL( iUID, iDataSource);
   312 
   313 	User::LeaveIfError(iFormatDec->SourceThreadLogon(*this));
   314 	iFormatDec->SourcePrimeL();
   315 	}
   316 
   317 /** 
   318  * This function creates a datasink object based on either a descriptor
   319  * or a file.
   320  * @param aIsFile -  ETrue if we are using CMmfFile or EFalse if using 
   321  * CMmfDescriptor as the DataSource. NOTE: CMmfDescriptor support has not
   322  * yet been added to this test harness for SINKs.
   323  * @param aFilename - The file which acts as the data sink.  For a CMmfFile
   324  * based object, this IS the file.  
   325  * @param aUid - The UID of the format object to be created.
   326  */
   327 void CTestStep_MMF_AFMT::CreateEncodeObjectL(TBool aIsFile, const TDesC& aFilename, TInt aUid)
   328 	{	
   329 	// Create the data sink (stored in iDataSink)
   330 	CreateDataSink(aIsFile, aFilename);
   331 
   332 	iUID = TUid::Uid(aUid);
   333 
   334 	// Make the NewL call.
   335 	iFormatEnc = CMMFFormatEncode::NewL( iUID, iDataSink);
   336 
   337 	User::LeaveIfError(iFormatEnc->SourceThreadLogon(*this));
   338 	}
   339 
   340 /**
   341  *
   342  * Destructor for this class.
   343  *
   344  */
   345 CTestStep_MMF_AFMT::~CTestStep_MMF_AFMT()
   346 	{
   347 	Cleanup();
   348 	}
   349 
   350 /**
   351  *
   352  * Close
   353  *
   354  */
   355 void CTestStep_MMF_AFMT::Close()
   356 	{
   357 	Cleanup(); 
   358 	}
   359