localisation/uiftestfw/src/appfwk_tmsteststep.cpp
branchSymbian3
changeset 57 b8d18c84f71c
equal deleted inserted replaced
56:aa99f2208aad 57:b8d18c84f71c
       
     1 /*
       
     2 * Copyright (c) 2008 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20 @file
       
    21 @test
       
    22 @internalComponent
       
    23 */
       
    24 
       
    25 #include <bautils.h>
       
    26 #include <test/appfwk_tmsteststep.h>
       
    27 #include "f32file.h"
       
    28 #include <e32std.h>
       
    29 
       
    30 
       
    31 _LIT(KLogLocation,"c:\\logs\\testexecute\\TMS_Log.txt");
       
    32 _LIT8(KLogPass," PASS");
       
    33 _LIT8(KLogFail," FAIL");
       
    34 _LIT8(KRet, "\n");
       
    35 _LIT8 (KBlankSpace," ");
       
    36 
       
    37 // Special notes about using this class
       
    38 //
       
    39 // In any derived CTxxxxStep class after the __CONSTRUCT_STEP__(xxxx) macro (generally at the end of the file)
       
    40 // if the methods void CTxxxxxStep::TestSetupL() and void CTxxxxStep::TestClose() do not exist
       
    41 // they should be added as follows :-
       
    42 //
       
    43 //	void CTxxxxStep::TestSetupL()
       
    44 //		{
       
    45 //		MultipleResultsForSameID(ETrue); // true or false set on test requirements
       
    46 //		}
       
    47 //
       
    48 //	void CTxxxxStep::TestClose()
       
    49 //		{
       
    50 //		iTestStepNames.Close();
       
    51 //		}
       
    52 //
       
    53 // if the methods do already exist add the MultipleResultsForSameID and iTestStepNames.Close lines to them respectivly
       
    54 //
       
    55 
       
    56 TInt CTmsTestStep::ReadNextLineL( RFile &aFile, TDes8 &aLine )
       
    57 // read a cr/lf limiited line from the file,  assumes file is a valid file
       
    58 // and that aLine is of sufficient length to hold the data
       
    59 	{
       
    60 	aLine.Zero();
       
    61 	TBuf8<1> chr;
       
    62 	for (;;)
       
    63 		{
       
    64 		aFile.Read(chr);
       
    65 		if ( chr.Length() == 0 )
       
    66 			{
       
    67 			break;
       
    68 			}
       
    69 		if (chr.CompareF(KRet) == 0)
       
    70 			{
       
    71 			// got a line, exctract newline as well
       
    72 			aFile.Read(chr);
       
    73 			break;
       
    74 			}
       
    75 		else
       
    76 			{
       
    77 			aLine.Append(chr);
       
    78 			}
       
    79 		}
       
    80 
       
    81 	return aLine.Length();
       
    82 	}
       
    83 
       
    84 void CTmsTestStep::CleanUpAndWriteResults()
       
    85 	{
       
    86 	RFs fs;
       
    87 	fs.Connect();
       
    88 	CleanupClosePushL(fs);
       
    89 
       
    90 	iTestStepPositions.Close();
       
    91 	if (iTestStepNames != NULL)
       
    92 		{
       
    93 		iTestStepNames->Reset();
       
    94 		delete iTestStepNames;
       
    95 		iTestStepNames = NULL;
       
    96 
       
    97 		if (BaflUtils::FolderExists(fs, KLogLocation))
       
    98 		   	{
       
    99 			RFile file;
       
   100 			if (BaflUtils::FileExists( fs, KLogLocation ))
       
   101 				{
       
   102 				// open the temporary tms log
       
   103 				TInt err = file.Open( fs, KLogLocation, EFileRead|EFileShareAny);
       
   104 			    if(err == KErrInUse)
       
   105 			    	{
       
   106 			       	CleanupStack::PopAndDestroy();
       
   107 			       	return;
       
   108 				   	}
       
   109 			    if (err == KErrNone)
       
   110 			    	{
       
   111 			    	CleanupClosePushL(file);
       
   112 			    	TBuf8<256> line;
       
   113 			    	TBuf<250> testID;
       
   114 			    	TBuf<6> testResult;
       
   115 			    	// get a line from the temporary tms log
       
   116 			    	while (CTmsTestStep::ReadNextLineL(file,line))
       
   117 			    		{
       
   118 			    		TInt blankPos = line.Find(KBlankSpace);
       
   119 
       
   120 			    		// get the ID from the line
       
   121 			    		testID.Copy(line.Left(blankPos));
       
   122 
       
   123 			    		//get the pass or fail result from the line
       
   124 			    		testResult.Copy(line.Mid(blankPos+1,4));
       
   125 
       
   126 			    		// print into the standard tef log the id and the result in the correct format
       
   127 			    		// so that they are correctly parsed into the TMS csv file
       
   128 			    		INFO_PRINTF2(_L("START_TESTCASE %S"),&testID);
       
   129 			    		INFO_PRINTF2(_L("Line = 1 Command = START_TESTCASE %S"),&testID);
       
   130 			    		INFO_PRINTF2(_L("END_TESTCASE %S"),&testID);
       
   131 			    		INFO_PRINTF3(_L("Line = 1 Command = END_TESTCASE %S ***TestCaseResult = %S"),&testID,&testResult);
       
   132 			    		}
       
   133 			    	CleanupStack::PopAndDestroy();
       
   134 			    	}
       
   135 			    // remove the temporary tms log
       
   136 			    fs.Delete(KLogLocation);
       
   137 			    }
       
   138 			}
       
   139 		}
       
   140 	CleanupStack::PopAndDestroy();
       
   141 	}
       
   142 
       
   143 EXPORT_C CTmsTestStep::CTmsTestStep() : iStepPassFail(ETrue), iShowMultipleResults(EFalse)
       
   144 	{
       
   145 	iCurrentTestStepName.Copy(KUnknownSYMTestCaseIDName);
       
   146 	iTestStepNames=new CDesCArrayFlat(1);
       
   147 	RFs fs;
       
   148 	fs.Connect();
       
   149 	CleanupClosePushL(fs);
       
   150 	if (BaflUtils::FolderExists(fs, KLogLocation))
       
   151 		   {
       
   152 		   if (BaflUtils::FileExists( fs, KLogLocation ))
       
   153 			   {
       
   154 			   // remove the temporary tms log
       
   155 			   fs.Delete(KLogLocation);
       
   156 			   }
       
   157 		   }
       
   158 	CleanupStack::PopAndDestroy();
       
   159 	}
       
   160 
       
   161 EXPORT_C void CTmsTestStep::CloseTMSGraphicsStep()
       
   162 	{
       
   163 	CTmsTestStep::CleanUpAndWriteResults();
       
   164 
       
   165 	}
       
   166 
       
   167 EXPORT_C CTmsTestStep::~CTmsTestStep()
       
   168 	{
       
   169 	CTmsTestStep::CleanUpAndWriteResults();
       
   170 	}
       
   171 
       
   172 // writes out the results of a test that has just been executed to the log file(s)
       
   173 // the first time a test result is written it is also added to the iTestStepNames array.
       
   174 // This array is then checked on subsequent tests to see if the test has already been written to log
       
   175 // if it has and multiple test results are not wanted further logging of this test is omitted.
       
   176 
       
   177 
       
   178 void CTmsTestStep::DebugLogL(const TDesC8& aTestCaseID, const TDesC8& aResult, TInt aPos = -1)
       
   179 	{
       
   180     TInt pos = 0;
       
   181 	RFs fs;
       
   182 	fs.Connect();
       
   183 	CleanupClosePushL(fs);
       
   184 
       
   185 	if (BaflUtils::FolderExists(fs, KLogLocation))
       
   186 	   {
       
   187 		RFile file;
       
   188 		if (!BaflUtils::FileExists( fs, KLogLocation ))
       
   189 			{
       
   190 		    User::LeaveIfError( file.Create( fs, KLogLocation, EFileWrite|EFileShareAny));
       
   191 			}
       
   192 		else
       
   193 			{
       
   194 		    TInt err = file.Open( fs, KLogLocation, EFileWrite|EFileShareAny);
       
   195 		    if(err == KErrInUse)
       
   196 			   {
       
   197 		       CleanupStack::PopAndDestroy();
       
   198 			   return;
       
   199 			   }
       
   200 		    if (aPos == -1)
       
   201 		    	file.Seek(ESeekEnd, pos);
       
   202 		    else
       
   203 		    	file.Seek(ESeekCurrent, aPos);
       
   204 	        }
       
   205 			file.Write(aTestCaseID);
       
   206 			file.Write(aResult);
       
   207 			file.Write(_L8("\r\n "));
       
   208 			file.Flush();
       
   209 			file.Close();
       
   210 	   }
       
   211 		CleanupStack::PopAndDestroy();
       
   212 	}
       
   213 
       
   214 TInt CTmsTestStep::DebugLogPosL()
       
   215 	{
       
   216     TInt pos = 0;
       
   217     TInt pos1 = 0;
       
   218 	RFs fs;
       
   219 	fs.Connect();
       
   220 	CleanupClosePushL(fs);
       
   221 
       
   222 	if (BaflUtils::FolderExists(fs, KLogLocation))
       
   223 	   {
       
   224 		RFile file;
       
   225 		if (!BaflUtils::FileExists( fs, KLogLocation ))
       
   226 			{
       
   227 		    User::LeaveIfError( file.Create( fs, KLogLocation, EFileWrite|EFileShareAny));
       
   228 			}
       
   229 		else
       
   230 			{
       
   231 		    TInt err = file.Open( fs, KLogLocation, EFileWrite|EFileShareAny);
       
   232 		    if(err == KErrInUse)
       
   233 			   {
       
   234 		       CleanupStack::PopAndDestroy();
       
   235 			   return KErrGeneral;
       
   236 			   }
       
   237 		    file.Seek(ESeekEnd, pos);
       
   238 		    file.Seek(ESeekCurrent, pos1);
       
   239 		    file.Close();
       
   240 	        }
       
   241 
       
   242 
       
   243 	   }
       
   244 	   CleanupStack::PopAndDestroy();
       
   245 	   return pos1;
       
   246 	}
       
   247 
       
   248 EXPORT_C void CTmsTestStep::RecordTestResultL()
       
   249 	{
       
   250 	TBuf<64> unknownTMSID(KUnknownSYMTestCaseIDName);
       
   251 	TBuf8<64> unknownID8;
       
   252 	unknownID8.Copy(unknownTMSID);
       
   253 
       
   254 	TBuf<64> undefinedTMSID(KUndefinedSYMTestCaseIDName);
       
   255 	TBuf8<64> undefinedID8;
       
   256 	undefinedID8.Copy(undefinedTMSID);
       
   257 
       
   258 	TBuf<64> notaTMSID(KNotATestSYMTestCaseIDName);
       
   259 	TBuf8<64> notaID8;
       
   260 	notaID8.Copy(notaTMSID);
       
   261 
       
   262 	TBuf<128> currentTSName;
       
   263 	currentTSName.Copy(iCurrentTestStepName);
       
   264 
       
   265 	if (iCurrentTestStepName.Compare(unknownID8) == 0) 	// unknown tests logged to warning file
       
   266 		{
       
   267 		INFO_PRINTF1(_L("ID NOT SET"));
       
   268 		}
       
   269 	else if (iCurrentTestStepName.Compare(undefinedID8) == 0)	// undefined tests logged to warning file
       
   270 		{
       
   271 		INFO_PRINTF1(_L("ID UNDEFINED"));
       
   272 		}
       
   273 	else if (iCurrentTestStepName.Compare(notaID8) == 0)
       
   274 		{
       
   275 		}
       
   276 	else if(iShowMultipleResults)	// multiple results for same test should be logged
       
   277 		{
       
   278 		if( iStepPassFail == EFalse)
       
   279 			{
       
   280 			INFO_PRINTF2(_L("%S FAILED"), &currentTSName );
       
   281 			DebugLogL(iCurrentTestStepName, KLogFail);
       
   282 			}
       
   283 		else
       
   284 			{
       
   285 			INFO_PRINTF2(_L("%S PASSED"), &currentTSName );
       
   286 			DebugLogL(iCurrentTestStepName, KLogPass);
       
   287 			}
       
   288 		}
       
   289 	else if (!iShowMultipleResults)
       
   290 		{
       
   291 		TInt existsInArray;
       
   292 		if (iTestStepNames == NULL)
       
   293 			iTestStepNames=new CDesCArrayFlat(1);
       
   294 
       
   295 		if( iTestStepNames->Find(currentTSName,existsInArray) == 0)
       
   296 			{
       
   297 			if( iStepPassFail == EFalse)
       
   298 				{
       
   299 					INFO_PRINTF2(_L("%S FAILED"), &currentTSName );
       
   300 					DebugLogL(iCurrentTestStepName, KLogFail, iTestStepPositions[existsInArray] );
       
   301 				}
       
   302 			}
       
   303 		else
       
   304 			{
       
   305 			iTestStepPositions.AppendL(DebugLogPosL());
       
   306 			iTestStepNames->AppendL(currentTSName);
       
   307 
       
   308 			if( iStepPassFail == EFalse)
       
   309 				{
       
   310 					INFO_PRINTF2(_L("%S FAILED"), &currentTSName );
       
   311 					DebugLogL(iCurrentTestStepName, KLogFail );
       
   312 				}
       
   313 				else
       
   314 				{
       
   315 					INFO_PRINTF2(_L("%S PASSED"), &currentTSName );
       
   316 					DebugLogL(iCurrentTestStepName, KLogPass);
       
   317 				}
       
   318 			}
       
   319 		}
       
   320 	}
       
   321 
       
   322 // sets the name and pass/fail state of the individual test that is about to be executed
       
   323 EXPORT_C void CTmsTestStep::SetTestStepID(const TDesC& aStepName)
       
   324 	{
       
   325 	iCurrentTestStepName.Copy(aStepName);
       
   326 	iStepPassFail = ETrue;
       
   327 	}
       
   328 
       
   329 EXPORT_C void CTmsTestStep::SetOverallTestStepID(const TDesC& aStepName)
       
   330 	{
       
   331 	iCurrentTestStepName.Copy(aStepName);
       
   332 	iStepPassFail = ETrue;
       
   333 	if (!TestStepResult() == EPass)
       
   334 				TEST(EFalse);
       
   335 	}
       
   336 
       
   337 // needed for the overide of CTGraphicsBase::testBooleanTrue to allow us to set
       
   338 // whether an individual test has passed or failed
       
   339 EXPORT_C void CTmsTestStep::MQCTest(TBool aCondition, const TText8* aFile, TInt aLine)
       
   340 	{
       
   341 	if(!aCondition)
       
   342 		{
       
   343 		iStepPassFail = EFalse;
       
   344 		}
       
   345 	testBooleanTrue( aCondition, aFile, aLine, ETrue);
       
   346 	}
       
   347 
       
   348 // needed for the overide of CTGraphicsBase::testBooleanTrue to allow us to set
       
   349 // whether an individual test has passed or failed
       
   350 EXPORT_C void CTmsTestStep::MQCTestL(TBool aCondition, const TText8* aFile, TInt aLine)
       
   351 	{
       
   352 	if(!aCondition)
       
   353 		{
       
   354 		iStepPassFail = EFalse;
       
   355 		}
       
   356 	testBooleanTrueL( aCondition, aFile, aLine, ETrue);
       
   357 	}
       
   358 
       
   359 // sets whether tests with the same @SYMTestCaseID xxxxxx name are printed multiple times in the log files
       
   360 EXPORT_C void CTmsTestStep::MultipleResultsForSameID(TBool aShowMultipleResults)
       
   361 	{
       
   362 	iShowMultipleResults = aShowMultipleResults;
       
   363 	}