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