messagingfw/msgtest/integration/email/src/testframeutils.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 1999-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 "testframeutils.h"
       
    17 
       
    18 _LIT(KCantOpenFile, "Could not open file: %S");
       
    19 _LIT(KSectionNotFound, "Could not find section: %S");
       
    20 _LIT(KMiscFileError, "File error");
       
    21 
       
    22 _LIT(KInfoTestFinished, "[%4d] Test finished: %S");
       
    23 _LIT(KInfoTestStarted, "[%4d] Starting test: %S");
       
    24 _LIT(KInfoTestError, "[%4d] Test failed in section: %S, command %S, reason %d");
       
    25 
       
    26 
       
    27 
       
    28 
       
    29 
       
    30 //
       
    31 //
       
    32 // TTestDebugInfo
       
    33 //
       
    34 
       
    35 TInt TTestDebugInfo::LineNumber()
       
    36 	{
       
    37 	return iLineNumber;
       
    38 	}
       
    39 
       
    40 HBufC* TTestDebugInfo::TestStartStringLC()
       
    41 	{
       
    42 	TBuf<256> startString;
       
    43 	HBufC* sectionName = TestHarnessNameLC();
       
    44 	startString.Format(KInfoTestStarted, iLineNumber, sectionName);
       
    45 	CleanupStack::PopAndDestroy(sectionName);
       
    46 	HBufC* startStringHBufC = HBufC::NewLC(startString.Length());
       
    47 	(startStringHBufC->Des()).Copy(startString);
       
    48 	return startStringHBufC;
       
    49 	}
       
    50 
       
    51 HBufC* TTestDebugInfo::TestCompleteStringLC()
       
    52 	{
       
    53 	TBuf<256> startString;
       
    54 	HBufC* sectionName = TestHarnessNameLC();
       
    55 	startString.Format(KInfoTestFinished, iLineNumber, sectionName);
       
    56 	CleanupStack::PopAndDestroy(sectionName);
       
    57 	HBufC* startStringHBufC = HBufC::NewLC(startString.Length());
       
    58 	(startStringHBufC->Des()).Copy(startString);
       
    59 	return startStringHBufC;
       
    60 	}
       
    61 
       
    62 HBufC* TTestDebugInfo::TestFailedStringLC(TInt aReason)
       
    63 	{
       
    64 	TBuf<256> failedString;
       
    65 	HBufC* sectionName = SectionNameLC();
       
    66 	HBufC* commandName = CommandNameLC();
       
    67 	failedString.Format(KInfoTestError, iLineNumber, sectionName, commandName, aReason);
       
    68 	CleanupStack::PopAndDestroy(2, sectionName);
       
    69 	HBufC* failedStringHBufC = HBufC::NewLC(failedString.Length());
       
    70 	(failedStringHBufC->Des()).Copy(failedString);
       
    71 	return failedStringHBufC;
       
    72 	}
       
    73 
       
    74 void TTestDebugInfo::SetTestHarnessName(TInt aTestHarnessPosition)
       
    75 	{
       
    76 	iTestHarnessPosition = aTestHarnessPosition;
       
    77 	}
       
    78 
       
    79 HBufC* TTestDebugInfo::SectionNameLC()
       
    80 	{
       
    81 	TBuf8<128> sectionName;
       
    82 	iScriptFile->GetLineL(sectionName, iSectionPosition);
       
    83 	HBufC* sectionNameHBufC = HBufC::NewLC(sectionName.Length());
       
    84 	sectionNameHBufC->Des().Copy(sectionName);
       
    85 	return sectionNameHBufC;
       
    86 	}
       
    87 
       
    88 HBufC* TTestDebugInfo::TestHarnessNameLC()
       
    89 	{
       
    90 	TBuf8<128> sectionName;
       
    91 	iScriptFile->GetLineL(sectionName, iTestHarnessPosition);
       
    92 	HBufC* sectionNameHBufC = HBufC::NewLC(sectionName.Length());
       
    93 	sectionNameHBufC->Des().Copy(sectionName);
       
    94 	return sectionNameHBufC;
       
    95 	}
       
    96 
       
    97 HBufC* TTestDebugInfo::CommandNameLC()
       
    98 	{
       
    99 	TBuf8<128> commandName;
       
   100 	iScriptFile->GetLineL(commandName, iCommandPosition);
       
   101 	HBufC* commandNameHBufC = HBufC::NewLC(commandName.Length());
       
   102 	commandNameHBufC->Des().Copy(commandName);
       
   103 	return commandNameHBufC;
       
   104 	}
       
   105 
       
   106 TTestDebugInfo::TTestDebugInfo(CTestScript& aTestScript, TInt aSectionPosition, TInt aCommandPosition, TInt aLineNumber) : iScriptFile(&aTestScript), iSectionPosition(aSectionPosition), iCommandPosition(aCommandPosition), iLineNumber(aLineNumber)
       
   107 	{
       
   108 	}	
       
   109 
       
   110 TTestDebugInfo::TTestDebugInfo(CTestScript& aTestScript, TInt aSectionPosition, TInt aCommandPosition) : iScriptFile(&aTestScript), iSectionPosition(aSectionPosition), iCommandPosition(aCommandPosition), iLineNumber(0)
       
   111 	{
       
   112 	}
       
   113 
       
   114 TTestDebugInfo::TTestDebugInfo() :  iScriptFile(0), iSectionPosition(0), iCommandPosition(0), iLineNumber(0)
       
   115 	{
       
   116 	}
       
   117 
       
   118 
       
   119 //
       
   120 //
       
   121 // CTestScript
       
   122 //
       
   123 
       
   124 CTestScript::~CTestScript()
       
   125 	{
       
   126 	delete iSectionPositions;
       
   127 	iFile.Close();
       
   128 	}
       
   129 
       
   130 CTestScript* CTestScript::NewL(RFs& aFs)
       
   131 	{
       
   132 	CTestScript* self = new (ELeave) CTestScript(aFs);
       
   133 	CleanupStack::PushL(self);
       
   134 	self->ConstructL();
       
   135 	CleanupStack::Pop(self);
       
   136 	return self;
       
   137 	}
       
   138 
       
   139 void CTestScript::ConstructL()
       
   140 	{
       
   141 	iSectionPositions = new (ELeave) CArrayFixFlat<TSectionPosition>(10);
       
   142 	}
       
   143 
       
   144 TBool CTestScript::LoadFileL(const TDesC& aFileName)
       
   145 	{
       
   146 	iFile.Close();
       
   147 	TBool loaded = EFalse;
       
   148 
       
   149 	iSectionPositions->ResizeL(0);
       
   150 	iEndOfFile = EFalse;
       
   151 
       
   152 	TTestScriptString line;
       
   153 
       
   154 	TInt err = iFile.Open(iFs, aFileName, EFileShareAny);
       
   155 
       
   156 	if (err == KErrNone)
       
   157 	// Scan the file for file sections, eg. [section1]
       
   158 		{
       
   159 		TInt lineNumber = 0;
       
   160 		TSectionPosition section;
       
   161 		section.iSectionPosition = 0;
       
   162 		while (GetLineL(line))
       
   163 			{
       
   164 			lineNumber++;
       
   165 			if (IsSection(line))
       
   166 			// If the line is the start of a section then add it to the list.
       
   167 				{
       
   168 				// Strip the '[' and ']' characters
       
   169 				line = line.Mid(1, line.Size() - 2);
       
   170 
       
   171 				// Store the section name and the position
       
   172 				section.iSectionName = line;
       
   173 				section.iFilePosition = 0;
       
   174 				section.iLineNumber = lineNumber;
       
   175 				err = iFile.Seek(ESeekCurrent, section.iFilePosition);
       
   176 				iSectionPositions->AppendL(section);
       
   177 				}
       
   178 			section.iSectionPosition = 0;
       
   179 			err = iFile.Seek(ESeekCurrent, section.iSectionPosition);
       
   180 			}
       
   181 
       
   182 		loaded = ETrue;
       
   183 		}
       
   184 	else
       
   185 		{
       
   186 		iLastError.Format(KCantOpenFile, aFileName);
       
   187 		}
       
   188 
       
   189 	return loaded;
       
   190 	}
       
   191 
       
   192 CTestSection* CTestScript::GetSectionL(const TDesC& aSectionName)
       
   193 	{
       
   194 	CTestSection* newSection = 0;
       
   195 
       
   196 	TSectionPosition section;
       
   197 	TTestScriptString command;
       
   198 	TBool sectionFound = EFalse;
       
   199 
       
   200 	// Look for the section name in the array
       
   201 	TInt sectionIndex = iSectionPositions->Count();
       
   202 	TBuf<128> tempSectionNameU;
       
   203 	while ((sectionIndex--) && (!sectionFound))
       
   204 		{
       
   205 		tempSectionNameU.Copy(((*iSectionPositions)[sectionIndex]).iSectionName);
       
   206 		if (tempSectionNameU == aSectionName)
       
   207 			{
       
   208 			section = (*iSectionPositions)[sectionIndex];
       
   209 			sectionFound = ETrue;
       
   210 			}
       
   211 		}
       
   212 
       
   213 	if (!sectionFound)
       
   214 		{
       
   215 		iLastError.Format(KSectionNotFound, aSectionName);
       
   216 		}
       
   217 	else
       
   218 		{
       
   219 		newSection = CTestSection::NewL(section.iSectionPosition, tempSectionNameU, section.iLineNumber);
       
   220 		CleanupStack::PushL(newSection);
       
   221 		TInt err = iFile.Seek(ESeekStart, section.iFilePosition);
       
   222 		if (err == KErrNone)
       
   223 			{
       
   224 			TInt commandLineNumber = section.iLineNumber;
       
   225 			TInt commandPosition = 0;
       
   226 			iFile.Seek(ESeekCurrent, commandPosition);
       
   227 			while (GetNextCommandInSectionL(command, commandLineNumber))
       
   228 				{
       
   229 				newSection->AddCommandL(command, commandPosition, commandLineNumber);
       
   230 				commandPosition = 0;
       
   231 				iFile.Seek(ESeekCurrent, commandPosition);
       
   232 				}
       
   233 			}
       
   234 		else
       
   235 			{
       
   236 			iLastError = KMiscFileError;
       
   237 			}
       
   238 		CleanupStack::Pop(newSection);
       
   239 		}
       
   240 
       
   241 	return newSection;
       
   242 	}
       
   243 
       
   244 CTestScript::CTestScript(RFs& aFs) : iFs(aFs)
       
   245 	{
       
   246 	}
       
   247 
       
   248 TBool CTestScript::GetLineL(TTestScriptString& aLine)
       
   249 	{
       
   250 	TBool noError = EFalse;
       
   251 
       
   252 	TInt startPosition = 0;
       
   253 	// Save the current position
       
   254 	TInt err = iFile.Seek(ESeekCurrent, startPosition);
       
   255 
       
   256 	if (err == KErrNone)
       
   257 		{
       
   258 		// Read a block of data
       
   259 		TTestScriptString dataFromFile;
       
   260 		err = iFile.Read(dataFromFile);
       
   261 
       
   262 		if (err != KErrNone)
       
   263 			{
       
   264 			iLastError.Copy(KMiscFileError);
       
   265 			}
       
   266 		else
       
   267 			{
       
   268 			// Look for the cr/lf pair or the end of the line
       
   269 			TInt bytesOnLine = 0;
       
   270 			TBool finished = EFalse;
       
   271 			while (!finished)
       
   272 				{
       
   273 				if (bytesOnLine == dataFromFile.Size())
       
   274 					{
       
   275 					finished = ETrue;
       
   276 					}
       
   277 				else if (dataFromFile[bytesOnLine] == 0x0d)
       
   278 					{
       
   279 					finished = ETrue;
       
   280 					}
       
   281 				else
       
   282 					{
       
   283 					bytesOnLine++;
       
   284 					}
       
   285 				}
       
   286 
       
   287 			TInt startOfNextLine = startPosition + bytesOnLine + 1;
       
   288 			if (bytesOnLine+1 < dataFromFile.Size())
       
   289 				{
       
   290 				if (dataFromFile[bytesOnLine+1] == 0x0a)
       
   291 					{
       
   292 					startOfNextLine++;
       
   293 					}
       
   294 				}
       
   295 
       
   296 			// Seek to after the cr/lf pair
       
   297 			iFile.Seek(ESeekStart, startOfNextLine);
       
   298 
       
   299 			// Set the line
       
   300 			aLine.Copy(dataFromFile.Left(bytesOnLine));
       
   301 			
       
   302 			// Get rid of any trailing white-space
       
   303 			TInt whiteSpaceIndex = aLine.Size();
       
   304 			TBool noMoreWhiteSpace = EFalse;
       
   305 			while ((whiteSpaceIndex--) && (!noMoreWhiteSpace))
       
   306 				{
       
   307 				if ((aLine[whiteSpaceIndex] == ' ')
       
   308 					|| (aLine[whiteSpaceIndex] == '\t'))
       
   309 					{
       
   310 					aLine.SetLength(whiteSpaceIndex);
       
   311 					}
       
   312 				else
       
   313 					{
       
   314 					noMoreWhiteSpace = ETrue;
       
   315 					}
       
   316 				}
       
   317 
       
   318 			if (dataFromFile.Size() != 0)
       
   319 				{
       
   320 				noError = ETrue;
       
   321 				}
       
   322 			}
       
   323 		}
       
   324 
       
   325 	return noError;
       
   326 	}
       
   327 
       
   328 TBool CTestScript::IsSection(const TDesC8& aLine) const
       
   329 	{
       
   330 	TBool isSection = EFalse;
       
   331 
       
   332 	if (aLine.Size() > 2)
       
   333 		{
       
   334 		if (aLine[0] == '[' && (aLine[aLine.Size() - 1] == ']'))
       
   335 			{
       
   336 			isSection = ETrue;
       
   337 			}
       
   338 		}
       
   339 
       
   340 	return isSection;
       
   341 	}
       
   342 
       
   343 TBool CTestScript::IsSectionEnd(const TDesC8& aLine) const
       
   344 	{
       
   345 	TBool isSectionEnd = EFalse;
       
   346 
       
   347 	if (aLine.Length() == 1 && aLine[0] == '.')
       
   348 		{
       
   349 		isSectionEnd = ETrue;
       
   350 		}
       
   351 
       
   352 	return isSectionEnd;
       
   353 	}
       
   354 
       
   355 
       
   356 // MU 20-11-00 New command to determine if the aLine is a comment.
       
   357 TBool CTestScript::IsComment(const TDesC8& aLine) const
       
   358 	{
       
   359 	TBool isComment = EFalse;
       
   360 
       
   361 	if (aLine.Size() >= 2)
       
   362 		{
       
   363 		if (aLine[0] == '/' && aLine[1] == '/')
       
   364 			{
       
   365 			isComment = ETrue;
       
   366 			}
       
   367 		}
       
   368 
       
   369 	return isComment;
       
   370 	}
       
   371 
       
   372 
       
   373 // MU 20-11-00 Changed this function to get the next "valid" command in section.
       
   374 //              This now allows the user to have comments within sections.
       
   375 //              Note:  the first blank line in a section denotes the end of a section.
       
   376 TBool CTestScript::GetNextCommandInSectionL(TTestScriptString& aCommand, TInt& aLineNumber)
       
   377 	{
       
   378 	TBool commandFound;
       
   379 
       
   380 	do 	{
       
   381 			GetLineL(aCommand);
       
   382 			aLineNumber++;
       
   383 		}
       
   384 	while (aCommand.Size() > 0 && IsComment(aCommand));
       
   385 
       
   386 	if(aCommand.Size() > 0 && !IsSection(aCommand) && !IsSectionEnd(aCommand))
       
   387 		commandFound = ETrue;
       
   388 	else
       
   389 		commandFound = EFalse;
       
   390 	
       
   391 	return commandFound;
       
   392 	}
       
   393 
       
   394 TBool CTestScript::GetLineL(TTestScriptString& aLine, TInt aPos)
       
   395 	{
       
   396 	TBool lineFound = EFalse;
       
   397 	TInt err = iFile.Seek(ESeekStart, aPos);
       
   398 	if (err == KErrNone)
       
   399 		{
       
   400 		lineFound = GetLineL(aLine);
       
   401 		}
       
   402 
       
   403 	return lineFound;
       
   404 	}
       
   405 
       
   406 
       
   407 
       
   408 //
       
   409 //
       
   410 // CTestSection
       
   411 //
       
   412 CTestSection* CTestSection::NewL(TInt aSectionPosition, const TDesC& aSectionName, TInt aLineNumber)
       
   413 	{
       
   414 	CTestSection* self = new (ELeave) CTestSection(aSectionPosition, aLineNumber);
       
   415 	CleanupStack::PushL(self);
       
   416 	self->ConstructL();
       
   417 	self->iSectionName = aSectionName;
       
   418 	CleanupStack::Pop(self);
       
   419 	return self;
       
   420 	}
       
   421 
       
   422 
       
   423 // Returns current command line number if found
       
   424 TInt CTestSection::GetCurrentCommand(TDes& aCurrentCommand) const
       
   425 	{
       
   426 	TInt currentCommand = 0;
       
   427 
       
   428 	if (iCommandIndex < iCommandList->Count())
       
   429 		{
       
   430 		aCurrentCommand.Copy((*iCommandList)[iCommandIndex].iCommand);
       
   431 		currentCommand = (*iCommandList)[iCommandIndex].iLineNumber;
       
   432 		}
       
   433 	
       
   434 	return currentCommand;
       
   435 	}
       
   436 
       
   437 TBool CTestSection::NextCommand()
       
   438 	{
       
   439 	TBool nextCommandFound = EFalse;
       
   440 
       
   441 	if ((iCommandIndex + 1) < iCommandList->Count())
       
   442 		{
       
   443 		nextCommandFound = ETrue;
       
   444 		}
       
   445 
       
   446 	iCommandIndex++;
       
   447 
       
   448 	return nextCommandFound;
       
   449 	}
       
   450 
       
   451 void CTestSection::AddCommandL(const CTestScript::TTestScriptString& aCommand, TInt aCommandPosition, TInt aLineNumber)
       
   452 	{
       
   453 	TCommandInfo command;
       
   454 	command.iCommand = aCommand;
       
   455 	command.iCommandPosition = aCommandPosition;
       
   456 	command.iLineNumber = aLineNumber;
       
   457 	iCommandList->AppendL(command);
       
   458 	}
       
   459 
       
   460 CTestSection::CTestSection(TInt aSectionPosition, TInt aLineNumber) : iSectionPosition(aSectionPosition), iLineNumber(aLineNumber)
       
   461 	{
       
   462 	}
       
   463 
       
   464 
       
   465 void CTestSection::ConstructL()
       
   466 	{
       
   467 	iCommandList = new (ELeave) CArrayFixFlat<TCommandInfo>(10);
       
   468 	}
       
   469 
       
   470 CTestSection::~CTestSection()
       
   471 	{
       
   472 	delete iCommandList;
       
   473 	}
       
   474 
       
   475 TInt CTestSection::SectionPosition() const
       
   476 	{
       
   477 	return iSectionPosition;
       
   478 	}
       
   479 
       
   480 TInt CTestSection::CurrentCommandPosition() const
       
   481 	{
       
   482 	return (*iCommandList)[iCommandIndex].iCommandPosition;
       
   483 	}
       
   484 
       
   485 const TDesC& CTestSection::SectionName() const
       
   486 	{
       
   487 	return iSectionName;
       
   488 	}