imagingtestenv/imagingtestfw/Source/TestFrameworkClient/TestUtils.cpp
changeset 0 5752a19fdefe
equal deleted inserted replaced
-1:000000000000 0:5752a19fdefe
       
     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 // This contains file utilities which can be called from Test Framework scripts
       
    15 // 
       
    16 //
       
    17 
       
    18 // EPOC includes
       
    19 #include <e32base.h>
       
    20 #include <f32file.h> 
       
    21 
       
    22 // Test system includes
       
    23 #include <testframework.h>
       
    24 
       
    25 // do not export if Unit Testing
       
    26 #if defined (__TSU_TESTFRAMEWORK__)
       
    27 #undef EXPORT_C
       
    28 #define EXPORT_C
       
    29 #endif
       
    30 
       
    31 
       
    32 /**
       
    33  *
       
    34  * Initialise the test utilities.
       
    35  *
       
    36  * @param	"CLog* aLogSystem"
       
    37  *			The logger the test utilities are to use.
       
    38  *
       
    39  * @xxxx 
       
    40  *
       
    41  */
       
    42 EXPORT_C CTestUtils* CTestUtils::NewL(CLog* aLogSystem)
       
    43 	{
       
    44 	CTestUtils* self = new(ELeave) CTestUtils;
       
    45 	self->Construct(aLogSystem);
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 /**
       
    50  *
       
    51  * Initialise the test utilities (second phase).
       
    52  *
       
    53  * @param	"CLog* aLogSystem"
       
    54  *			The logger the test utilities are to use.
       
    55  *
       
    56  * @xxxx
       
    57  *
       
    58  */
       
    59 void CTestUtils::Construct(CLog* aLogSystem)
       
    60 	{
       
    61 	iLogSystem = aLogSystem;
       
    62 	}
       
    63 
       
    64 /**
       
    65  *
       
    66  * Run the test utilities inside a trap harness.
       
    67  *
       
    68  * @param	"const TDesC& aText"
       
    69  *			Test string giving the utility to run.
       
    70  *
       
    71  * @xxxx 
       
    72  *
       
    73  */
       
    74 EXPORT_C void CTestUtils::RunUtils(const TDesC& aText)
       
    75 	{
       
    76 	TRAPD(r, RunUtilsL(aText));
       
    77 
       
    78 	if (r != KErrNone)
       
    79 		{
       
    80 		WARN_PRINTF3(_L("Warning: Test Utils : %S left, error %d"), &aText, r);
       
    81 		}
       
    82 	}
       
    83 
       
    84 /**
       
    85  *
       
    86  * Run the test utilities inside a trap harness.
       
    87  *
       
    88  * @param	"const TDesC& aText"
       
    89  *			Test string giving the utility to run.
       
    90  *
       
    91  * @xxxx
       
    92  *
       
    93  */
       
    94 void CTestUtils::RunUtilsL(const TDesC& aText)
       
    95 	{
       
    96 	// use Tlex to decode the cmd line
       
    97 	TLex lex(aText);
       
    98 
       
    99 	// step over the keyword
       
   100 	lex.NextToken();
       
   101 
       
   102 	// get util required
       
   103 	TPtrC token;
       
   104 	token.Set(lex.NextToken());
       
   105 
       
   106 	if (token.FindF( _L("ChangeDir")) == 0)
       
   107 		{
       
   108 		User::Panic(_L("ChangeDir is not supported on EKA2"), KErrNotSupported);
       
   109 		}
       
   110 	else if (token.FindF( _L("CopyFile")) == 0)
       
   111 		{
       
   112 		// get the parameter
       
   113 		TPtrC file1 = lex.NextToken();
       
   114 		TPtrC file2 = lex.NextToken();
       
   115 
       
   116 		CopyFileL(file1, file2);
       
   117 		}
       
   118 	else if (token.FindF( _L("CopyAndInvertFile")) == 0)
       
   119 		{
       
   120 		// get the parameter
       
   121 		TPtrC file1 = lex.NextToken();
       
   122 		TPtrC file2 = lex.NextToken();
       
   123 
       
   124 		CopyAndInvertFileL(file1, file2);
       
   125 		}	
       
   126 	else if (token.FindF( _L("MkDir")) == 0)
       
   127 		{
       
   128 		// get the parameter
       
   129 		token.Set(lex.NextToken());
       
   130 
       
   131 		MakeDirL(token);
       
   132 		}
       
   133 	else if (token.FindF( _L("Delete")) == 0)
       
   134 		{
       
   135 		// get the parameter
       
   136 		token.Set(lex.NextToken());
       
   137 
       
   138 		DeleteFileL(token);
       
   139 		}
       
   140 	else if (token.FindF( _L("MakeReadWrite")) == 0)
       
   141 		{
       
   142 		// get the parameter
       
   143 		token.Set(lex.NextToken());
       
   144 
       
   145 		MakeReadWriteL(token);
       
   146 		}
       
   147 	else
       
   148 		{
       
   149 		ERR_PRINTF2(_L("Failed to decode RUN_UTILS command : %S"), &aText);
       
   150 		User::Leave(KErrNotFound);
       
   151 		}
       
   152 	}
       
   153 
       
   154 
       
   155 /**
       
   156  *
       
   157  * Test utility : make new directory
       
   158  *
       
   159  * @param	"const TDesC& aDirname"
       
   160  *			New directory path.
       
   161  *
       
   162  * @xxxx
       
   163  *
       
   164  */
       
   165 void CTestUtils::MakeDirL(const TDesC& aDirname) 
       
   166 	{
       
   167 	// parse the filenames
       
   168 	_LIT(KDefault,"C:\\"); 
       
   169 	TParse fullFileName;
       
   170 	TInt returnCode = fullFileName.Set(aDirname, &KDefault, NULL);
       
   171 	if (returnCode != KErrNone)
       
   172 		{
       
   173 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   174 		ERR_PRINTF3(_L("Failed to decode full path name %S - %S"), 
       
   175 			&fullFileName.FullName(), 
       
   176 			&errortxt);
       
   177 		User::Leave(returnCode);
       
   178 		}
       
   179 
       
   180 	// connect to file server
       
   181 	returnCode = iFs.Connect();
       
   182 	if (returnCode != KErrNone)
       
   183 		{
       
   184 		ERR_PRINTF1(_L("Error connecting to file server"));	
       
   185 		User::Leave(returnCode);
       
   186 		}
       
   187 
       
   188 	// now create the new directory
       
   189 	returnCode = iFs.MkDirAll(fullFileName.DriveAndPath()); 
       
   190 	if ((returnCode != KErrNone) && (returnCode != KErrAlreadyExists))
       
   191 		{		
       
   192 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   193 
       
   194 		ERR_PRINTF3(_L("Error %S making directory %S"), 
       
   195 			&errortxt, 
       
   196 			&fullFileName.FullName());
       
   197 		iFs.Close();
       
   198 		User::Leave(returnCode);
       
   199 		}
       
   200 	if(returnCode == KErrNone)
       
   201 		{
       
   202 		// display full (including path) file name
       
   203 		INFO_PRINTF2( _L("Made directory %S"), &fullFileName.FullName());	
       
   204 		}
       
   205 	else
       
   206 		{
       
   207 		INFO_PRINTF2( _L("directory already exists %S"), &fullFileName.FullName());		
       
   208 		}
       
   209 	iFs.Close();
       
   210 	}
       
   211 
       
   212 
       
   213 /**
       
   214  *
       
   215  * Test utility : copy a file
       
   216  *
       
   217  * @param	"const TDesC& aOld"
       
   218  *			Source file.
       
   219  *
       
   220  * @param	"const TDesC& aNew"
       
   221  *			Destination file.
       
   222  *
       
   223  * @xxxx
       
   224  *
       
   225  */
       
   226 void CTestUtils::CopyFileL (const TDesC& aOld,const TDesC& aNew) 
       
   227 	{
       
   228 	// connect to file server
       
   229 	TInt returnCode = iFs.Connect();
       
   230 	if (returnCode != KErrNone)
       
   231 		{
       
   232 		ERR_PRINTF1(_L("Error connecting to file server"));	
       
   233 		User::Leave(returnCode);
       
   234 		}
       
   235 
       
   236 	// create a file manager
       
   237 	CFileMan* fileMan = CFileMan::NewL(iFs);
       
   238 	CleanupStack::PushL(fileMan);
       
   239 
       
   240 	// parse the filenames
       
   241 	TParse parseSource;
       
   242 	returnCode = parseSource.Set(aOld, NULL, NULL);
       
   243 	if (returnCode != KErrNone)
       
   244 		{
       
   245 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   246 		ERR_PRINTF3( _L("Failed to parse %S - %S"), 
       
   247 			&aOld, 
       
   248 			&errortxt);
       
   249 		iFs.Close();
       
   250 		User::Leave(returnCode);
       
   251 		}
       
   252  
       
   253 	// parse the filenames
       
   254 	TParse parseTarget;
       
   255 	returnCode = parseTarget.Set(aNew, NULL, NULL);
       
   256 	if (returnCode != KErrNone)
       
   257 		{
       
   258 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   259 		ERR_PRINTF3(_L("Failed to parse %S - %S"), 
       
   260 			&aNew, 
       
   261 			&errortxt);
       
   262 		iFs.Close();
       
   263 		User::Leave(returnCode);
       
   264 		}
       
   265 
       
   266 	// do the copy
       
   267 	returnCode = fileMan->Copy(parseSource.FullName(), 
       
   268 		parseTarget.FullName(), 
       
   269 		CFileMan::EOverWrite | CFileMan::ERecurse);
       
   270 
       
   271 	if (returnCode != KErrNone)
       
   272 		{
       
   273 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   274 
       
   275 		ERR_PRINTF4(_L("Failed to copy %S to %S - %S"), 
       
   276 			&parseSource.FullName(), 
       
   277 			&parseTarget.FullName(),
       
   278 			&errortxt);
       
   279 		iFs.Close();
       
   280 		User::Leave(returnCode);
       
   281 		}
       
   282 
       
   283 	INFO_PRINTF3(_L("Copied file from %S to %S"), 
       
   284 					&parseSource.FullName(), 
       
   285 					&parseTarget.FullName());
       
   286 
       
   287 	iFs.Close();
       
   288 	CleanupStack::PopAndDestroy(fileMan);
       
   289 	}
       
   290 
       
   291 
       
   292 /**
       
   293  *
       
   294  * Test utility : invert a file and copy it to a tartget file
       
   295  *
       
   296  * @param	"const TDesC& aOld"
       
   297  *			Source file.
       
   298  *
       
   299  * @param	"const TDesC& aNew"
       
   300  *			Destination file.
       
   301  *          Will contain the inverted file
       
   302  *          
       
   303  *
       
   304  * @xxxx
       
   305  *
       
   306  */
       
   307 void CTestUtils::CopyAndInvertFileL (const TDesC& aOld,const TDesC& aNew) 
       
   308 	{
       
   309     
       
   310     TRAPD(error, DoCopyAndInvertL(aOld, aNew));  
       
   311 
       
   312     if (error != KErrNone)
       
   313 		{
       
   314    		TPtrC errortxt = CLog::EpocErrorToText(error);
       
   315 		ERR_PRINTF2(_L("Failed to copy Files - %S"), &errortxt);
       
   316 		User::Leave(error);
       
   317 		}	
       
   318     	
       
   319 	}
       
   320 	
       
   321 
       
   322 	
       
   323 	
       
   324 /**
       
   325  *
       
   326  * Test utility : Do the invert and copy of a source file
       
   327  *				  into a target file. 
       
   328  *
       
   329  * @param	"const TDesC& aOld"
       
   330  *			Source file.
       
   331  *
       
   332  * @param	"const TDesC& aNew"
       
   333  *			Destination file.
       
   334  *          Will contain the inverted file
       
   335  *          
       
   336  *
       
   337  * @xxxx
       
   338  *
       
   339  */
       
   340 void CTestUtils::DoCopyAndInvertL (const TDesC& aOld,const TDesC& aNew)
       
   341 	{
       
   342 	// connect to file server
       
   343 	TInt returnCode = iFs.Connect();
       
   344 	if (returnCode != KErrNone)
       
   345 		{
       
   346 		ERR_PRINTF1(_L("Error connecting to file server"));	
       
   347 		User::Leave(returnCode);
       
   348 		}
       
   349     CleanupClosePushL(iFs);	
       
   350 		
       
   351 	// parse the filenames
       
   352 	TParse parseSource;
       
   353 	User::LeaveIfError(parseSource.Set(aOld, NULL, NULL));
       
   354 
       
   355 	// parse the filenames
       
   356 	TParse parseTarget;
       
   357 	User::LeaveIfError(parseTarget.Set(aNew, NULL, NULL));
       
   358 	
       
   359 	RFile sourceFile;
       
   360 	
       
   361 	// open the source file for reading
       
   362 	User::LeaveIfError(sourceFile.Open(iFs, parseSource.FullName(), EFileRead));
       
   363 	CleanupClosePushL(sourceFile);
       
   364 	
       
   365 	RFile targetFile;
       
   366 	
       
   367 	// open the target file for writing
       
   368 	User::LeaveIfError(targetFile.Replace(iFs, parseTarget.FullName(), EFileWrite));
       
   369 	CleanupClosePushL(targetFile);
       
   370 	
       
   371 	const TInt KSizeOfInvertBuf = 256;
       
   372 	
       
   373 	// Buffers used for read,invert and write to the target file
       
   374 	TBuf8<KSizeOfInvertBuf> invertBuffer;
       
   375 	TInt8 byteBuf;//A temp byte size buffer
       
   376 	
       
   377 	//Invert the source file and copy it into the target file 
       
   378 	do 
       
   379 		{
       
   380 		User::LeaveIfError(sourceFile.Read(invertBuffer));
       
   381 		
       
   382 		for (TInt index=0; index<invertBuffer.Length(); index++)
       
   383 			 {
       
   384 			 byteBuf = invertBuffer[index];
       
   385 			 byteBuf ^= 0xFF; //inverting the buf bits
       
   386 			 invertBuffer[index] = byteBuf;
       
   387 			 }
       
   388 		User::LeaveIfError(targetFile.Write(invertBuffer));
       
   389 	 	}
       
   390    while (invertBuffer.Length() != 0);
       
   391    
       
   392 
       
   393     // Flush the data to the target file
       
   394 	User::LeaveIfError(targetFile.Flush());
       
   395 	
       
   396     // display full (including path) file name
       
   397 	INFO_PRINTF3(_L("Copied and Inverted successfully file %S to file %S"), 
       
   398 		&parseSource.FullName(),
       
   399 		&parseTarget.FullName());
       
   400 	
       
   401 	//closing the files
       
   402 	CleanupStack::PopAndDestroy(3, &iFs);
       
   403 	
       
   404 	}	
       
   405 
       
   406 
       
   407 
       
   408 
       
   409 
       
   410 /**
       
   411  *
       
   412  * Test utility : delete a file
       
   413  *
       
   414  * @param	"const TDesC& aFile"
       
   415  *			File to be deleted.
       
   416  *
       
   417  * @xxxx
       
   418  *
       
   419  */
       
   420 void CTestUtils::DeleteFileL (const TDesC& aFile) 
       
   421 	{
       
   422 	// parse the filenames
       
   423 	_LIT(KDefault,"C:\\xxxxx.xxx"); 
       
   424 	TParse fullFileName;
       
   425 	TInt returnCode = fullFileName.Set(aFile, &KDefault, NULL);
       
   426 	if (returnCode != KErrNone)
       
   427 		{
       
   428 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   429 		ERR_PRINTF3(_L("Failed to decode full path name %S - %S"), 
       
   430 			&fullFileName.FullName(), 
       
   431 			&errortxt);
       
   432 		User::Leave(returnCode);
       
   433 		}
       
   434 
       
   435 	// connect to file server
       
   436 	returnCode = iFs.Connect();
       
   437 	if (returnCode != KErrNone)
       
   438 		{
       
   439 		ERR_PRINTF1(_L("Error connecting to file server"));	
       
   440 		User::Leave(returnCode);
       
   441 		}
       
   442 
       
   443 	// now do the delete
       
   444 	returnCode = iFs.Delete(fullFileName.FullName()); 
       
   445 	if (returnCode != KErrNone)
       
   446 		{		
       
   447 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   448 		ERR_PRINTF3(_L("Error %S deleting %S"), 
       
   449 			&errortxt, 
       
   450 			&fullFileName.FullName());
       
   451 		iFs.Close();
       
   452 		User::Leave(returnCode);
       
   453 		}
       
   454 
       
   455 	// display full (including path) file name
       
   456 	INFO_PRINTF2(_L("Deleted %S"), &fullFileName.FullName());	
       
   457 	iFs.Close();
       
   458 	}
       
   459 
       
   460 
       
   461 /**
       
   462  *
       
   463  * Test utility : make a file read-write (clear the read-only attribute)
       
   464  *
       
   465  * @param	"const TDesC& aFile"
       
   466  *			File to be made read-write.
       
   467  *
       
   468  * @xxxx
       
   469  *
       
   470  */
       
   471 void CTestUtils::MakeReadWriteL (const TDesC& aFile) 
       
   472 	{
       
   473 	// parse the filenames
       
   474 	_LIT(KDefault,"C:\\xxxxx.xxx"); 
       
   475 	TParse fullFileName;
       
   476 	TInt returnCode = fullFileName.Set(aFile, &KDefault, NULL);
       
   477 	if (returnCode != KErrNone)
       
   478 		{
       
   479 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   480 		ERR_PRINTF3(_L("Failed to decode full path name %S - %S"), 
       
   481 			&fullFileName.FullName(), 
       
   482 			&errortxt);
       
   483 		User::Leave(returnCode);
       
   484 		}
       
   485 
       
   486 	// connect to file server
       
   487 	returnCode = iFs.Connect();
       
   488 	if (returnCode != KErrNone)
       
   489 		{
       
   490 		ERR_PRINTF1(_L("Error connecting to file server"));	
       
   491 		User::Leave(returnCode);
       
   492 		}
       
   493 
       
   494 	// now do the delete
       
   495 	returnCode = iFs.SetAtt(fullFileName.FullName(), 0, KEntryAttReadOnly); 
       
   496 
       
   497 	// check for errors
       
   498 	if (returnCode != KErrNone )
       
   499 		{		
       
   500 		TPtrC errortxt = CLog::EpocErrorToText(returnCode);
       
   501 
       
   502 		ERR_PRINTF3(_L("Error %S making %S read-write"), 
       
   503 			&errortxt, 
       
   504 			&fullFileName.FullName());
       
   505 		iFs.Close();
       
   506 		User::Leave(returnCode);
       
   507 		}
       
   508 
       
   509 	// display full (including path) file name
       
   510 	INFO_PRINTF2(_L("Made %S read-write"), &fullFileName.FullName());	
       
   511 	iFs.Close();
       
   512 	}
       
   513 
       
   514 /**
       
   515  *
       
   516  * General logging function for test utils.
       
   517  *
       
   518  * @param	"TRefByValue<const TDesC16> aFmt"
       
   519  *			Printf-style format.
       
   520  *
       
   521  * @param	"..."
       
   522  *			Variable print parameters
       
   523  *
       
   524  * @xxxx 
       
   525  *
       
   526  */
       
   527 void CTestUtils::Log(TRefByValue<const TDesC16> aFmt, ...)
       
   528 	{
       
   529 
       
   530 	VA_LIST aList;
       
   531 	VA_START(aList, aFmt);
       
   532 
       
   533 	if(iLogSystem) 
       
   534 		iLogSystem->Log(aFmt, aList);
       
   535 
       
   536 	VA_END(aList);
       
   537 	}
       
   538 
       
   539 /**
       
   540  *
       
   541  * General logging function for test utils, with severity.
       
   542  *
       
   543  * @param	"TInt aSeverity"
       
   544  *			Severity level required to log
       
   545  *
       
   546  * @param	"TRefByValue<const TDesC16> aFmt"
       
   547  *			Printf-style format.
       
   548  *
       
   549  * @param	"..."
       
   550  *			Variable print parameters
       
   551  *
       
   552  * @xxxx 
       
   553  *
       
   554  */
       
   555 void CTestUtils::Log(TInt /* aSeverity */, TRefByValue<const TDesC16> aFmt, ...)
       
   556 	{
       
   557 	VA_LIST aList;
       
   558 	VA_START(aList, aFmt);
       
   559 
       
   560 	// NB : Test Utils log regardless of severity (as they are tied to no suite)
       
   561 	if(iLogSystem) 
       
   562 		iLogSystem->Log(aFmt, aList);
       
   563 
       
   564 	VA_END(aList);
       
   565 	}
       
   566 
       
   567 /**
       
   568  *
       
   569  * Traceable logging function for test utils.
       
   570  *
       
   571  * @param	"const TText8* aFile"
       
   572  *			Source code file name
       
   573  *
       
   574  * @param	"TInt aLine"
       
   575  *			Source code line
       
   576  *
       
   577  * @param	"TInt aSeverity"
       
   578  *			Severity level required to log
       
   579  *
       
   580  * @param	"TRefByValue<const TDesC16> aFmt"
       
   581  *			Printf-style format.
       
   582  *
       
   583  * @param	"..."
       
   584  *			Variable print parameters
       
   585  *
       
   586  * @xxxx 
       
   587  *
       
   588  */
       
   589 void CTestUtils::LogExtra(const TText8* aFile, TInt aLine, TInt aSeverity,
       
   590 		TRefByValue<const TDesC16> aFmt,...)
       
   591 	{
       
   592 	VA_LIST aList;
       
   593 	VA_START(aList, aFmt);
       
   594 
       
   595 	// NB : Test Utils log regardless of severity (as they are tied to no suite)
       
   596 	if(iLogSystem)
       
   597 		iLogSystem->LogExtra(aFile, aLine, aSeverity, aFmt, aList);
       
   598 
       
   599 	VA_END(aList);
       
   600 	}
       
   601 
       
   602