messagingfw/msgtest/testutils/base/src/MsvTestUtilsBase.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2000-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 <logcli.h>
       
    17 #include <logview.h>
       
    18 #include <logwrap.h>
       
    19 #include <txtetext.h>
       
    20 #include <msventry.h>
       
    21 #include <msvapi.h>
       
    22 #include <msvids.h>
       
    23 #include <barsc.h>
       
    24 
       
    25 #include "msvtestutilsbase.h"
       
    26 
       
    27 #include <cntdb.h>
       
    28 #include <cntitem.h>
       
    29 #include <cntfldst.h>
       
    30 #include <bautils.h>
       
    31 #include <e32property.h>
       
    32 #include "msvtestutils.h"
       
    33 #include <simtsy.h>
       
    34 
       
    35 _LIT(KMsvTestFileInputBase,"MsgTest");
       
    36 _LIT(KMsvTestFileOutputBase,"MsgLogs");
       
    37 
       
    38 // this is used if we can't find MsgLogs anywhere
       
    39 _LIT(KMsvTestFileDefaultOutputBase,"\\MsgLogs\\");
       
    40 _LIT(KMsvPathSep,"\\");
       
    41 #define KMaxLogLineLength 256
       
    42 
       
    43 
       
    44 //
       
    45 //
       
    46 // CTestActive
       
    47 //
       
    48 //
       
    49 
       
    50 EXPORT_C CTestActive::CTestActive()
       
    51 : CActive(0)
       
    52 	{
       
    53 	CActiveScheduler::Add(this);
       
    54 	}
       
    55 
       
    56 EXPORT_C CTestActive::~CTestActive()
       
    57 	{
       
    58 	Cancel();
       
    59 	}
       
    60 
       
    61 EXPORT_C void CTestActive::DoCancel()
       
    62 	{
       
    63 	TRequestStatus* s=&iStatus;
       
    64 	User::RequestComplete(s, KErrNone);
       
    65 	}
       
    66 
       
    67 EXPORT_C void CTestActive::StartL()
       
    68 	{
       
    69 	SetActive();
       
    70 	}
       
    71 
       
    72 EXPORT_C void CTestActive::RunL() 
       
    73 	{
       
    74 	CActiveScheduler::Stop();
       
    75 	}
       
    76 
       
    77 
       
    78 //
       
    79 //
       
    80 // CTestTimer
       
    81 //
       
    82 //
       
    83 
       
    84 EXPORT_C CTestTimer::CTestTimer()
       
    85 : CTimer(EPriorityLow)
       
    86 	{}
       
    87 
       
    88 EXPORT_C void CTestTimer::RunL()
       
    89 	{
       
    90 	if (!iReport)
       
    91 		{
       
    92 		CActiveScheduler::Stop();
       
    93 		}
       
    94 	else
       
    95 		{
       
    96 		User::RequestComplete(iReport, iStatus.Int());
       
    97 		iReport = NULL;
       
    98 		}
       
    99 	}
       
   100 
       
   101 EXPORT_C CTestTimer* CTestTimer::NewL()
       
   102 	{
       
   103 	CTestTimer* self = new(ELeave) CTestTimer();
       
   104 	CleanupStack::PushL(self);
       
   105 	self->ConstructL(); // CTimer
       
   106 	CActiveScheduler::Add(self);
       
   107 	CleanupStack::Pop();
       
   108 	return self;
       
   109 	}
       
   110 
       
   111 EXPORT_C void CTestTimer::AfterReq(TTimeIntervalMicroSeconds32 aSec, TRequestStatus& aStatus)
       
   112 	{
       
   113 	iReport = &aStatus;
       
   114 	aStatus = KRequestPending;
       
   115 	After(aSec);
       
   116 	}
       
   117 
       
   118 
       
   119 //
       
   120 //
       
   121 // CTestUtils
       
   122 //
       
   123 //
       
   124 
       
   125 EXPORT_C CTestUtils::CTestUtils(RTest& aRTest)
       
   126 : iRTest(aRTest), iLogToConsole(ETrue), iLogToFile(ETrue)
       
   127 	{
       
   128 	TBuf<256> command;
       
   129 		User::CommandLine(command);
       
   130 
       
   131 	command.Trim();
       
   132 	SetRunAuto(command.FindF(KTestShowMenu) == KErrNotFound);
       
   133 
       
   134 	if (!RunAuto())
       
   135 		iRTest.Printf(_L("Not Run Auto. Command line: %S\n"), &command);
       
   136 	}
       
   137 
       
   138 EXPORT_C CTestUtils::~CTestUtils()
       
   139 	{
       
   140 	ResetMenu();
       
   141 	delete iMenu;
       
   142 
       
   143 	CloseMainLog();
       
   144 	iFs.Close();
       
   145 	}
       
   146 
       
   147 EXPORT_C void CTestUtils::ConstructL()
       
   148 	{
       
   149 	User::LeaveIfError(iFs.Connect());
       
   150 	OpenMainLogL();
       
   151 	WriteToLog(KTestHeader);
       
   152 	iMenu = new (ELeave) CArrayPtrFlat<HBufC>(10);
       
   153 	}
       
   154 
       
   155 TBool CTestUtils::CheckLogStatus(const TBool aEventViewRet, const TRequestStatus& aStatus)
       
   156 	{
       
   157 	TBool ret = ETrue;
       
   158 	TBufC<256> txtBuf;
       
   159 	TPtr txt = txtBuf.Des();
       
   160 
       
   161 	if (!aEventViewRet)
       
   162 		{
       
   163 		txt.Zero();
       
   164 		txt.Append(_L("No more events in the LogView!"));
       
   165 		AppendEol(txt, EFalse);
       
   166 		Printf(txt);
       
   167 		ret = EFalse;
       
   168 		}
       
   169 	else if (aStatus.Int() != KErrNone)
       
   170 		{
       
   171 		Printf(_L("\nError: CLogViewEvent async request completed with error %d.\n"), aStatus.Int());
       
   172 		ret = EFalse;
       
   173 		}
       
   174 
       
   175 	return ret;
       
   176 	}
       
   177 
       
   178 EXPORT_C void CTestUtils::DoBuf(TDes8& buf, const TDesC& label, const TDesC& data)
       
   179 	{
       
   180 	buf.Zero();
       
   181 	buf.Copy(label); 
       
   182 	buf.Append(data);
       
   183 	buf.Append(_L("\r\n"));
       
   184 	}
       
   185 
       
   186 EXPORT_C void CTestUtils::DoBuf(TDes8& buf, const TDesC& label, const TDesC8& data)
       
   187 	{
       
   188 	buf.Zero();
       
   189 	buf.Copy(label); 
       
   190 	buf.Append(data);
       
   191 	buf.Append(_L("\r\n"));
       
   192 	}
       
   193 
       
   194 
       
   195 EXPORT_C void CTestUtils::WriteToFileL(RFile& aFile, TDesC8& aInputLine)
       
   196 	{
       
   197 	User::LeaveIfError(aFile.Write(aInputLine));
       
   198 	}
       
   199 
       
   200 EXPORT_C void CTestUtils::WriteToFileL(RFile& aFile, const TDesC& aInputLine)
       
   201 	{
       
   202 	HBufC8* pOutputLine = HBufC8::NewLC(aInputLine.Length()*2);
       
   203 	TPtr8 pOutput = pOutputLine->Des();
       
   204 	pOutput.Zero();
       
   205 
       
   206 	TUint lowChar;
       
   207 	TUint highChar;
       
   208 	TInt i=0;
       
   209 	
       
   210 	while(i<aInputLine.Length())
       
   211 		{
       
   212 		lowChar=((TUint)aInputLine[i])&0x00FF;
       
   213 		pOutput.Append(lowChar);
       
   214 		highChar=((TUint)aInputLine[i++])&0xFF00;
       
   215 		pOutput.Append(highChar);
       
   216 		}
       
   217 
       
   218 	aFile.Write(pOutput);
       
   219 	CleanupStack::PopAndDestroy(); // pBodyText
       
   220 	}
       
   221 
       
   222 EXPORT_C void CTestUtils::RemoveRichtextFormating(TDes& aSourceLine)
       
   223 	{
       
   224 	TUint8* ptr = (TUint8*)aSourceLine.Ptr();
       
   225 	TUint8* start = ptr;
       
   226 
       
   227 	TInt totalLength = aSourceLine.Length()*2;
       
   228 	do {
       
   229 		if(*ptr==CEditableText::EParagraphDelimiter || *ptr==CEditableText::ELineBreak || *ptr==CEditableText::EPageBreak)
       
   230 			*ptr=0x0A;
       
   231 		} while((++ptr-start)<totalLength);
       
   232 	}
       
   233 
       
   234 EXPORT_C TInt CTestUtils::DisplayFile(const TDesC& aFileName)
       
   235 	{
       
   236 	RFile file;
       
   237 
       
   238 	TInt err = file.Open(iFs, aFileName, EFileShareAny | EFileRead | EFileStreamText);
       
   239 
       
   240 	if (err != KErrNone)
       
   241 		return err;
       
   242 
       
   243 	TBuf8<256> text8;
       
   244 	TBuf<512> text16;
       
   245 
       
   246 	while (file.Read(text8) == KErrNone && text8.Length())
       
   247 		{
       
   248 		text16.Copy(text8);
       
   249 		Printf(text16);
       
   250 		}
       
   251 
       
   252 	file.Close();
       
   253 
       
   254 	return KErrNone;
       
   255 	}
       
   256 
       
   257 EXPORT_C TInt CTestUtils::DisplayMenu(const TDesC& aTitle, TInt aMaxAttempts, TBool aAllowExit, TBool aClearScreenBefore, TBool aClearScreenAfter)
       
   258 	{
       
   259 	TBool logToFile = SetLogToFile(EFalse);
       
   260 	TInt ret = DoDisplayMenu(aTitle, aMaxAttempts, aAllowExit, aClearScreenBefore, aClearScreenAfter);
       
   261 	SetLogToFile(logToFile);
       
   262 	return ret;
       
   263 	}
       
   264 
       
   265 TInt CTestUtils::DoDisplayMenu(const TDesC& aTitle, TInt& aMaxAttempts, TBool aAllowExit, TBool aClearScreenBefore, TBool aClearScreenAfter)
       
   266 	{
       
   267 	if (aMaxAttempts < 1)
       
   268 		{
       
   269 		ClearScreen(aClearScreenAfter);
       
   270 		return 0;
       
   271 		}
       
   272 
       
   273 	aMaxAttempts--;
       
   274 
       
   275 	TInt count = MenuCount();
       
   276 
       
   277 	__ASSERT_ALWAYS(count > 0 && count < 10, Panic(KErrArgument));
       
   278 	
       
   279 	ClearScreen(aClearScreenBefore);
       
   280 	
       
   281 	TBuf<256> buf = aTitle;
       
   282 	AppendEol(buf, EFalse);
       
   283 	AppendEol(buf, EFalse);
       
   284 
       
   285 	Printf(buf);
       
   286 
       
   287 
       
   288 	for (TInt curOption = 0; curOption < count; curOption++)
       
   289 		{
       
   290 		buf.Zero();
       
   291 		buf.Append(_L("%d : "));
       
   292 		buf.Append(*(iMenu->At(curOption)));
       
   293 		AppendEol(buf, EFalse);
       
   294 		Printf(buf, curOption + 1); 
       
   295 		}
       
   296 
       
   297 	if (aAllowExit)
       
   298 		{
       
   299 		Printf(_L("\nX : Exit\n"));
       
   300 		}
       
   301 
       
   302 	Printf(_L("\nEnter your selection from 1 to %d\n"), count);
       
   303 
       
   304 	TInt result = iRTest.Getch();
       
   305 
       
   306 	if (aAllowExit && (result == 'x' || result == 'X'))
       
   307 		{
       
   308 		ClearScreen(aClearScreenAfter);
       
   309 		return 0;
       
   310 		}
       
   311 	else
       
   312 		{
       
   313 		result = result - '0';
       
   314 
       
   315 		if (result > 0 && result <= count)
       
   316 			{
       
   317 			ClearScreen(aClearScreenAfter);
       
   318 			return result;
       
   319 			}
       
   320 		else
       
   321 			{
       
   322 			return DoDisplayMenu(aTitle, aMaxAttempts, aAllowExit, aClearScreenBefore, aClearScreenAfter);
       
   323 			}
       
   324 		}
       
   325 	}
       
   326 
       
   327 EXPORT_C void CTestUtils::Printf(TRefByValue<const TDesC> aFmt,...)
       
   328 	{
       
   329 	// Print to the log file.
       
   330 	TTestOverflow overflow(*this);
       
   331 	VA_LIST list;
       
   332 	VA_START(list, aFmt);
       
   333 	TBuf<0x100> aBuf;
       
   334 	aBuf.AppendFormatList(aFmt, list, &overflow);
       
   335 
       
   336 	if (iLogToConsole)
       
   337 		{
       
   338 		iRTest.Printf(aBuf);
       
   339 		}
       
   340 
       
   341 	if (iLogToFile)
       
   342 		{
       
   343 		WriteComment(aBuf);
       
   344 		}
       
   345 	}
       
   346 
       
   347 
       
   348 
       
   349 EXPORT_C void CTestUtils::AppendText(const TDesC& aToken, TDes& aTxt, TInt aMaxTokenLength)
       
   350 	{
       
   351 	TInt totalLen = aTxt.Length() + aMaxTokenLength;
       
   352 
       
   353 	aTxt.Append(aToken);
       
   354 	aTxt.TrimRight();
       
   355 
       
   356 	if (aTxt.Length() < totalLen)
       
   357 		{
       
   358 		aTxt.AppendFill(' ', totalLen - aTxt.Length());
       
   359 		}
       
   360 
       
   361 	aTxt.SetLength(totalLen);
       
   362 	}
       
   363 
       
   364 EXPORT_C void CTestUtils::ClearLogL(const CLogFilter& aFilter)
       
   365 	{
       
   366 	CLogClient* client = CLogClient::NewL(iFs);
       
   367 	CleanupStack::PushL(client);
       
   368 
       
   369 	CLogViewEvent* eventView = CLogViewEvent::NewL(*client);
       
   370 	CleanupStack::PushL(eventView);
       
   371 
       
   372 	CTestActive* testAct = new (ELeave) CTestActive();
       
   373 	CleanupStack::PushL(testAct);
       
   374 
       
   375 	CLogClient* logCli = CLogClient::NewL(iFs);
       
   376 	CleanupStack::PushL(logCli);
       
   377 
       
   378 	TBool found = eventView->SetFilterL(aFilter, testAct->iStatus);
       
   379 
       
   380 	if (found)
       
   381 		{
       
   382 		testAct->StartL();
       
   383 		CActiveScheduler::Start();	
       
   384 		}
       
   385 
       
   386 	if (!CheckLogStatus(found, testAct->iStatus))
       
   387 		{
       
   388 		CleanupStack::PopAndDestroy(4);
       
   389 		return;
       
   390 		}
       
   391 
       
   392 	found = eventView->FirstL(testAct->iStatus);
       
   393 
       
   394 	if (found)
       
   395 		{
       
   396 		testAct->StartL();
       
   397 		CActiveScheduler::Start();	
       
   398 		}
       
   399 
       
   400 	if (!CheckLogStatus(found, testAct->iStatus))
       
   401 		{
       
   402 		CleanupStack::PopAndDestroy(4);
       
   403 		return;
       
   404 		}
       
   405 
       
   406 	const CLogEvent& event = eventView->Event();
       
   407 
       
   408 	Printf(_L("Clearing System Log\n"));
       
   409 
       
   410 	do
       
   411 		{
       
   412 		iRTest.Printf(_L("."));
       
   413 		logCli->DeleteEvent(event.Id(), testAct->iStatus);
       
   414 		testAct->StartL();
       
   415 		CActiveScheduler::Start();	
       
   416 
       
   417 		found = eventView->FirstL(testAct->iStatus);
       
   418 
       
   419 		if (found)
       
   420 			{
       
   421 			testAct->StartL();
       
   422 			CActiveScheduler::Start();	
       
   423 			}
       
   424 
       
   425 		found = CheckLogStatus(found, testAct->iStatus);
       
   426 		}
       
   427 	while (found);
       
   428 
       
   429 	CleanupStack::PopAndDestroy(4);
       
   430 	}
       
   431 
       
   432 EXPORT_C void CTestUtils::DisplayLogL(const CLogFilter& aFilter, TBool aForward)
       
   433 	{
       
   434 	TBuf<256> header;
       
   435 	TBuf<256> txt;
       
   436 	TBuf<64> temp;
       
   437 
       
   438 	CLogClient* client = CLogClient::NewL(iFs);
       
   439 	CleanupStack::PushL(client);
       
   440 
       
   441 	CLogViewEvent* eventView = CLogViewEvent::NewL(*client);
       
   442 	CleanupStack::PushL(eventView);
       
   443 
       
   444 	CTestActive* testAct = new (ELeave) CTestActive();
       
   445 	CleanupStack::PushL(testAct);
       
   446 
       
   447 	TBool found = eventView->SetFilterL(aFilter, testAct->iStatus);
       
   448 
       
   449 	if (found)
       
   450 		{
       
   451 		testAct->StartL();
       
   452 		CActiveScheduler::Start();	
       
   453 		}
       
   454 
       
   455 	if (!CheckLogStatus(found, testAct->iStatus))
       
   456 		{
       
   457 		CleanupStack::PopAndDestroy(3);
       
   458 		return;
       
   459 		}
       
   460 
       
   461 	TInt totalCount = eventView->CountL();
       
   462 
       
   463 	txt.Zero();
       
   464 	txt.Append(_L("Total Log Events: "));
       
   465 	temp.Num(totalCount);
       
   466 	txt.Append(temp);
       
   467 	AppendEol(txt, EFalse);
       
   468 	Printf(txt);
       
   469 
       
   470 	const TInt numLen		= 3;
       
   471 	const TInt timeLen		= 11;
       
   472 	const TInt dateLen		= 10;
       
   473 	const TInt statusLen	= 7;
       
   474 	const TInt subjLen		= 20;
       
   475 	const TInt dirLen		= 3;
       
   476 	const TInt typeLen		= 10;
       
   477 
       
   478 	header.Zero();
       
   479 	AppendText(_L("Num"), header, numLen);
       
   480 	header.Append('|');
       
   481 	AppendText(_L("Date"), header, dateLen);
       
   482 	header.Append('|');
       
   483 	AppendText(_L("Time"), header, timeLen);
       
   484 	header.Append('|');
       
   485 	AppendText(_L("Status"), header, statusLen);
       
   486 	header.Append('|');
       
   487 	AppendText(_L("Subject"), header, subjLen);
       
   488 	header.Append('|');
       
   489 	AppendText(_L("Dir"), header, dirLen);
       
   490 	header.Append('|');
       
   491 	AppendText(_L("Type"), header, typeLen);
       
   492 
       
   493 	AppendEol(header, EFalse);
       
   494 	Printf(header);
       
   495 
       
   496 	found = aForward ? eventView->FirstL(testAct->iStatus) : eventView->LastL(testAct->iStatus);
       
   497 
       
   498 	if (found)
       
   499 		{
       
   500 		testAct->StartL();
       
   501 		CActiveScheduler::Start();	
       
   502 		}
       
   503 
       
   504 	if (!CheckLogStatus(found, testAct->iStatus))
       
   505 		{
       
   506 		CleanupStack::PopAndDestroy(3);
       
   507 		return;
       
   508 		}
       
   509 
       
   510 	TInt counter = 0;
       
   511 	do
       
   512 		{
       
   513 		const CLogEvent& event = eventView->Event();
       
   514 		counter++;
       
   515 		if (!RunAuto() && (counter % 6 == 0))
       
   516 			{
       
   517 			iRTest.Printf(_L("\nPress any key for more...\n"));
       
   518 			iRTest.Getch();
       
   519 			Printf(header);
       
   520 			}
       
   521 
       
   522 		txt.Zero();
       
   523 
       
   524 		if (aForward)
       
   525 			{
       
   526 			temp.Num(totalCount - counter + 1);
       
   527 			}
       
   528 		else
       
   529 			{
       
   530 			temp.Num(counter);
       
   531 			}
       
   532 
       
   533 		AppendText(temp, txt, numLen);
       
   534 		txt.Append('|');
       
   535 
       
   536 		event.Time().FormatL(temp, _L("%D%M%Y%/0%1%/1%2%/2%3%/3"));
       
   537 		AppendText(temp, txt, dateLen);
       
   538 		txt.Append('|');
       
   539 
       
   540 	    event.Time().FormatL(temp,_L("%-B%:0%J%:1%T%:2%S%:3%+B"));
       
   541 		AppendText(temp, txt, timeLen);
       
   542 		txt.Append('|');
       
   543 
       
   544 		AppendText(event.Status(), txt, statusLen);
       
   545 		txt.Append('|');
       
   546 		AppendText(event.Subject(), txt, subjLen);
       
   547 		txt.Append('|');
       
   548 		AppendText(event.Direction(), txt, dirLen);
       
   549 		txt.Append('|');
       
   550 
       
   551 		GetEventType(event.EventType(), temp);
       
   552 		AppendText(temp, txt, typeLen);
       
   553 
       
   554 		AppendEol(txt, EFalse);
       
   555 		Printf(txt);
       
   556 
       
   557 		DisplayLogEventData(event);
       
   558 
       
   559 		found = aForward ? eventView->NextL(testAct->iStatus) : eventView->PreviousL(testAct->iStatus);
       
   560 
       
   561 		if (found)
       
   562 			{
       
   563 			testAct->StartL();
       
   564 			CActiveScheduler::Start();	
       
   565 			}
       
   566 
       
   567 		if (!CheckLogStatus(found, testAct->iStatus))
       
   568 			{
       
   569 			CleanupStack::PopAndDestroy(3);
       
   570 			return;
       
   571 			}
       
   572 		}
       
   573 	while (found);
       
   574 
       
   575 	CleanupStack::PopAndDestroy(3);
       
   576 	}
       
   577 
       
   578 void CTestUtils::GetEventType(TUid aEventType, TDes& rOutput)
       
   579 	{
       
   580 	switch (aEventType.iUid)
       
   581 		{
       
   582 		case KLogCallEventType:
       
   583 			rOutput = _L("Call");
       
   584 			break;
       
   585 		case KLogDataEventType:
       
   586 			rOutput = _L("Data");
       
   587 			break;
       
   588 		case KLogFaxEventType:
       
   589 			rOutput = _L("Fax");
       
   590 			break;
       
   591 		case KLogShortMessageEventType:
       
   592 			rOutput = _L("SMS");
       
   593 			break;
       
   594 		case KLogMailEventType:
       
   595 			rOutput = _L("Mail");
       
   596 			break;
       
   597 		case KLogTaskSchedulerEventType:
       
   598 			rOutput = _L("SchSvr");
       
   599 			break;
       
   600 		default:
       
   601 			rOutput.Num((TInt) aEventType.iUid);
       
   602 		}
       
   603 	}
       
   604 
       
   605 EXPORT_C void CTestUtils::CreateAllTestDirectories()
       
   606 	{
       
   607 	TChar driveChar=RFs::GetSystemDriveChar();
       
   608  	TBuf<2> systemDrive;
       
   609  	systemDrive.Append(driveChar);
       
   610  	systemDrive.Append(KDriveDelimiter);
       
   611 	
       
   612 	iFs.SetSessionPath(systemDrive);
       
   613 
       
   614 	if (!iFs.MkDir(KLogsDir))
       
   615 		{
       
   616 		Printf(_L("Created c:\\logs\\ directory\n"));
       
   617 		}
       
   618 	}
       
   619 
       
   620 EXPORT_C TContactItemId CTestUtils::CreateContactL(const TDesC& aGivenName, const TDesC& aFamilyName, const TDesC& aPhoneNumber, const TDesC& aEmailAddress, TBool aReplaceContactsDb)
       
   621 	{
       
   622 	CContactDatabase* db = NULL;
       
   623 
       
   624 	if (aReplaceContactsDb)
       
   625 		{
       
   626 		db = CContactDatabase::ReplaceL();
       
   627 		}
       
   628 	else
       
   629 		{
       
   630 		TRAPD(err, db = CContactDatabase::OpenL());
       
   631 		
       
   632 		if (err == KErrNotFound)
       
   633 			{
       
   634 			TRAP(err, db = CContactDatabase::CreateL());
       
   635 			}
       
   636 
       
   637 		User::LeaveIfError(err);
       
   638 		}
       
   639 
       
   640 	CleanupStack::PushL(db);
       
   641 
       
   642 	CContactItem* item=CContactCard::NewLC();
       
   643 
       
   644 	if (aFamilyName.Length())
       
   645 		SetNameL(*item, KUidContactFieldFamilyName, aFamilyName, ETrue);
       
   646 
       
   647 	if (aGivenName.Length())
       
   648 		SetNameL(*item, KUidContactFieldGivenName, aGivenName, ETrue);
       
   649 
       
   650 	if (aPhoneNumber.Length())
       
   651 		SetNameL(*item, KUidContactFieldPhoneNumber, aPhoneNumber, ETrue);
       
   652 
       
   653 	if (aEmailAddress.Length())
       
   654 		SetNameL(*item, KUidContactFieldEMail, aEmailAddress, ETrue);
       
   655 
       
   656 	CContactItemFieldSet& fieldSet= item->CardFields();
       
   657 
       
   658 	TContactItemId id = KNullContactId;
       
   659 	
       
   660 	if (fieldSet.Count())
       
   661 		id = db->AddNewContactL(*item);
       
   662 	else
       
   663 		User::Leave(KErrNotSupported);
       
   664 
       
   665 	CleanupStack::PopAndDestroy(2); // item, db
       
   666 	return id;
       
   667 	}
       
   668 
       
   669 EXPORT_C void CTestUtils::SetNameL(CContactItem& aItem,TUid aType,const TDesC& aName, TBool aAddField)
       
   670 //
       
   671 // Set the contents of a text field, creating the field if required
       
   672 //
       
   673 	{
       
   674 	CContactItemFieldSet& fieldSet=aItem.CardFields();
       
   675 	const TInt pos=fieldSet.Find(aType);
       
   676 	if (!aAddField && pos!=KErrNotFound)
       
   677 		fieldSet[pos].TextStorage()->SetTextL(aName);
       
   678 	else
       
   679 		{
       
   680 		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,aType);
       
   681    		field->SetMapping(KUidContactFieldVCardMapUnusedN);
       
   682 		field->TextStorage()->SetTextL(aName);
       
   683 		aItem.AddFieldL(*field);
       
   684 		CleanupStack::Pop(); // field
       
   685 		}
       
   686 	}
       
   687 
       
   688 EXPORT_C void CTestUtils::DeleteContactsFileL()
       
   689 	{
       
   690 	CContactDatabase::DeleteDefaultFileL();
       
   691 	}
       
   692 
       
   693 EXPORT_C HBufC8* CTestUtils::ReadFileLC(const TDesC& aFile)
       
   694 	{
       
   695 	//Returns a HBufC8 with the contents of aFile
       
   696 	RFile file;
       
   697 	User::LeaveIfError(file.Open(iFs, aFile, EFileShareAny | EFileRead));
       
   698 	CleanupClosePushL(file);
       
   699 
       
   700 	TInt size = 0;
       
   701 	TInt err = file.Size(size);
       
   702 
       
   703 	if (err)
       
   704 		{
       
   705 		Printf(_L("ReadFileLC(%S) leaving with %d. Unable to size file\n"), &aFile, err);
       
   706 		User::Leave(err);
       
   707 		}
       
   708 
       
   709 	HBufC8* contents = HBufC8::NewLC(size + 4);
       
   710 	TPtr8 contentsPtr(contents->Des());
       
   711 	err = file.Read(0, contentsPtr);
       
   712 
       
   713 	if (err)
       
   714 		{
       
   715 		Printf(_L("ReadFileLC(%S) leaving with %d. Unable to read file\n"), &aFile, err);
       
   716 		User::Leave(err);
       
   717 		}
       
   718 
       
   719 	CleanupStack::Pop(); //contents
       
   720 	CleanupStack::PopAndDestroy(); //file
       
   721 	CleanupStack::PushL(contents);
       
   722 
       
   723 	return contents;
       
   724 	}
       
   725 
       
   726 const TInt KNumberOfHex = 16;
       
   727 
       
   728 EXPORT_C void CTestUtils::PrintHex(const TDesC& aDes)
       
   729 	{
       
   730 	TBuf<256> temp;
       
   731 	TInt pos = 0;
       
   732 	const TInt len = aDes.Length();
       
   733 
       
   734 	while (pos < len)
       
   735 		{
       
   736 		temp.Zero();
       
   737 		TPtrC ptr = aDes.Mid(pos, Min(KNumberOfHex, len - pos));
       
   738 		const TInt ptrLen = ptr.Length();
       
   739 
       
   740 		for (TInt i = 0; i < ptrLen; i++)
       
   741 			{
       
   742 			temp.AppendFormat(_L("%2.2x "), ptr[i]);
       
   743 			}
       
   744 
       
   745 		if (ptrLen < KNumberOfHex)
       
   746 			{
       
   747 			temp.AppendFill(' ', (KNumberOfHex - ptrLen) * 3);
       
   748 			}
       
   749 
       
   750 		temp.Append(ptr);
       
   751 		Printf(temp);
       
   752 
       
   753 		pos += KNumberOfHex;
       
   754 		}
       
   755 	}
       
   756 
       
   757 EXPORT_C void CTestUtils::PrintHex(const TDesC8& aDes)
       
   758 	{
       
   759 	TBuf<256> temp;
       
   760 	TInt pos = 0;
       
   761 	const TInt len = aDes.Length();
       
   762 
       
   763 	while (pos < len)
       
   764 		{
       
   765 		temp.Zero();
       
   766 		TPtrC8 ptr = aDes.Mid(pos, Min(KNumberOfHex, len - pos));
       
   767 		const TInt ptrLen = ptr.Length();
       
   768 
       
   769 		for (TInt i = 0; i < ptrLen; i++)
       
   770 			{
       
   771 			temp.AppendFormat(_L("%2.2x "), ptr[i]);
       
   772 			}
       
   773 
       
   774 		if (ptrLen < KNumberOfHex)
       
   775 			{
       
   776 			temp.AppendFill(' ', (KNumberOfHex - ptrLen) * 3);
       
   777 			}
       
   778 
       
   779 		TBuf<KNumberOfHex> buf16;
       
   780 		buf16.Copy(ptr);
       
   781 		temp.Append(buf16);
       
   782 		Printf(temp);
       
   783 
       
   784 		pos += KNumberOfHex;
       
   785 		}
       
   786 	}
       
   787 
       
   788 EXPORT_C TInt CTestUtils::ResolveFile(const TDesC& aComponent, const TDesC& aFileName, TParse& aParseOut)
       
   789 	{
       
   790 	TInt err ;
       
   791 	TFileName* savedPath = new TFileName;
       
   792 	TFileName* fileName = new TFileName;
       
   793 	if ((savedPath == NULL) || (fileName == NULL))
       
   794 		{
       
   795 		err=KErrNoMemory;
       
   796 		}
       
   797 	fileName->Append(KMsvPathSep);
       
   798 	fileName->Append(KMsvTestFileInputBase);
       
   799 	fileName->Append(KMsvPathSep);
       
   800 	fileName->Append(aComponent);
       
   801 	fileName->Append(KMsvPathSep);
       
   802 	fileName->Append(aFileName);
       
   803 	
       
   804 	// file finder will look in the session drive first, then Y->A,Z
       
   805 	// so set session drive to Y (save old and restore it afterwards)
       
   806 	iFs.SessionPath(*savedPath);
       
   807 	_LIT(KTopDrive,"Y:\\");
       
   808 	iFs.SetSessionPath(KTopDrive);
       
   809     TFindFile file_finder(iFs);
       
   810     err = file_finder.FindByDir(*fileName,KNullDesC);
       
   811 	if(err==KErrNone)
       
   812 		aParseOut.Set(file_finder.File(),NULL,NULL);
       
   813 	iFs.SetSessionPath(*savedPath);
       
   814 	delete savedPath;
       
   815 	delete fileName;
       
   816 	return(err);
       
   817 	}
       
   818 
       
   819 
       
   820 EXPORT_C TInt CTestUtils::ResolveLogFile(const TDesC& aFileName, TParse& aParseOut)
       
   821 	{
       
   822 	TFileName* savedPath = new TFileName;
       
   823 	TFileName* fileName = new TFileName;
       
   824 	if ((savedPath == NULL) || (fileName == NULL))
       
   825 		return KErrNoMemory;
       
   826 
       
   827 	fileName->Append(KMsvPathSep);
       
   828 	fileName->Append(KMsvTestFileOutputBase);
       
   829 	fileName->Append(KMsvPathSep);
       
   830 	
       
   831 	// file finder will look in the session drive first, then Y->A,Z
       
   832 	// so set session drive to Y (save old and restore it afterwards)
       
   833 	iFs.SessionPath(*savedPath);
       
   834 	_LIT(KTopDrive,"Y:\\");
       
   835 	iFs.SetSessionPath(KTopDrive);
       
   836     TFindFile file_finder(iFs);
       
   837     TInt err = file_finder.FindByDir(*fileName,KNullDesC);
       
   838 
       
   839 	if(err==KErrNone)
       
   840 		{
       
   841 		fileName->Copy(file_finder.File());
       
   842 		AppendTestName(*fileName);
       
   843 		fileName->Append(KMsvPathSep);
       
   844 		fileName->Append(aFileName);
       
   845 		iFs.MkDirAll(*fileName);
       
   846 		aParseOut.Set(*fileName,NULL,NULL);
       
   847 		}
       
   848 	iFs.SetSessionPath(*savedPath);
       
   849 	delete savedPath;
       
   850 	delete fileName;
       
   851 	return(err);
       
   852 	}
       
   853 
       
   854 EXPORT_C void CTestUtils::ResetMenu()
       
   855 	{
       
   856 	iMenu->ResetAndDestroy();
       
   857 	}
       
   858 
       
   859 EXPORT_C void CTestUtils::AppendToMenuL(const TDesC& aOption)
       
   860 	{
       
   861 	HBufC* buf = HBufC::NewLC(256);
       
   862 	TPtr ptr = buf->Des();
       
   863 	ptr.Copy(aOption);
       
   864 	iMenu->AppendL(buf);
       
   865 	CleanupStack::Pop();
       
   866 	}
       
   867 
       
   868 EXPORT_C TInt CTestUtils::MenuCount() const
       
   869 	{
       
   870 	return iMenu->Count();
       
   871 	}
       
   872 
       
   873 EXPORT_C void CTestUtils::ClearScreen(TBool aClearScreen)
       
   874 	{
       
   875 	if (aClearScreen)
       
   876 		{
       
   877 		iRTest.Console()->ClearScreen();
       
   878 		iRTest.Console()->SetPos(0,0);
       
   879 		}
       
   880 	}
       
   881 
       
   882 
       
   883 EXPORT_C TBool CTestUtils::SetLogToConsole(TBool aLogToConsole)
       
   884 	{
       
   885 	TBool save = iLogToConsole;
       
   886 	iLogToConsole = aLogToConsole;
       
   887 	return save;
       
   888 	}
       
   889 
       
   890 EXPORT_C TBool CTestUtils::SetLogToFile(TBool aLogToFile)
       
   891 	{
       
   892 	TBool save = iLogToFile;
       
   893 	iLogToFile = aLogToFile;
       
   894 	return save;
       
   895 	}
       
   896 
       
   897 
       
   898 
       
   899 void CTestUtils::AppendEol(TDes& aTxt, TBool aToFile)
       
   900 	{
       
   901 	aToFile ? aTxt.Append(_L("\r\n")) : aTxt.Append(_L("\n"));
       
   902 	}
       
   903 
       
   904 EXPORT_C TBool CTestUtils::RunAuto() const
       
   905 	{
       
   906 	return iRunAuto;
       
   907 	}
       
   908 
       
   909 EXPORT_C void CTestUtils::SetRunAuto(TBool aRunAuto)
       
   910 	{
       
   911 	iRunAuto = aRunAuto;
       
   912 	}
       
   913 
       
   914 
       
   915 // appends .<PLATFORM>.<VARIANT>.log to aFileName;
       
   916 void CTestUtils::AppendVariantName(TDes& aFileName)
       
   917 	{
       
   918 #if (defined(__THUMB__) || defined(__MARM_THUMB__))
       
   919 	aFileName.Append(_L(".THUMB."));
       
   920 #endif
       
   921 #if (defined(__ARMI__) || defined(__MARM_ARMI__))
       
   922 	aFileName.Append(_L(".ARMI."));
       
   923 #endif
       
   924 #if (defined(__ARM4__) || defined(__MISA__) || defined(__MARM_ARM4__))
       
   925 	aFileName.Append(_L(".ARM4."));
       
   926 #endif
       
   927 #if defined(__WINSCW__)
       
   928 	aFileName.Append(_L(".WINSCW."));
       
   929 #else 
       
   930 #if defined(__WINS__)
       
   931 	aFileName.Append(_L(".WINS."));
       
   932 #endif
       
   933 #endif
       
   934 
       
   935 #if defined(_DEBUG)
       
   936 	aFileName.Append(_L("DEB."));
       
   937 #else
       
   938 	aFileName.Append(_L("REL."));
       
   939 #endif
       
   940 	aFileName.Append(_L("LOG"));
       
   941 	}
       
   942 
       
   943 // appends the test harness name to aFileName
       
   944 void CTestUtils::AppendTestName(TDes &aFileName)
       
   945 	{
       
   946 	TInt start = RProcess().FileName().LocateReverse('\\');
       
   947 	TInt end = RProcess().FileName().LocateReverse('.');
       
   948 
       
   949 	if (end == KErrNotFound)
       
   950 		end = RProcess().FileName().Length();
       
   951 
       
   952 	aFileName.Append(RProcess().FileName().Mid(start + 1, end - start - 1));
       
   953 	}	
       
   954 
       
   955 
       
   956 
       
   957 TInt CTestUtils::OpenMainLogL()
       
   958 	{
       
   959 	_LIT(KDisplayLogFile,"Log File %S\n");
       
   960 	TParse loglocation;
       
   961 	TFileName logfile;
       
   962 	TInt err=ResolveLogFile(KNullDesC, loglocation);
       
   963 	if(err!=KErrNone) 
       
   964 		{
       
   965 		TChar driveChar=RFs::GetSystemDriveChar();
       
   966  		TBuf<2> systemDrive;
       
   967  		systemDrive.Append(driveChar);
       
   968  		systemDrive.Append(KDriveDelimiter);
       
   969  		TPath pathName(systemDrive) ;
       
   970 		pathName.Append(KMsvTestFileDefaultOutputBase);		
       
   971 		iFs.MkDirAll(pathName);
       
   972 		err=ResolveLogFile(KNullDesC, loglocation);
       
   973 		}
       
   974 	User::LeaveIfError(err);
       
   975 	logfile.Copy(loglocation.FullName());
       
   976 	logfile.Delete(logfile.Length()-1,1);
       
   977 	AppendVariantName(logfile);
       
   978 	iRTest.Printf(KDisplayLogFile, &logfile);
       
   979 	iFs.MkDirAll(logfile);
       
   980 
       
   981 	iLogBuf=HBufC::NewL(KMaxLogLineLength);
       
   982 	iLogBuf8=HBufC8::NewL(KMaxLogLineLength);
       
   983 	return(iFile.Replace(iFs,logfile,EFileWrite|EFileShareAny));
       
   984 	}
       
   985 
       
   986 TInt CTestUtils::AppendMainLogL()
       
   987 	{
       
   988 	_LIT(KDisplayLogFile,"Log File %S\n");
       
   989 	TParse loglocation;
       
   990 	TFileName logfile;
       
   991 	TInt err=ResolveLogFile(KNullDesC, loglocation);
       
   992 	if(err!=KErrNone) 
       
   993 		{
       
   994 		iFs.MkDirAll(KMsvTestFileDefaultOutputBase);
       
   995 		err=ResolveLogFile(KNullDesC, loglocation);
       
   996 		}
       
   997 	User::LeaveIfError(err);
       
   998 	logfile.Copy(loglocation.FullName());
       
   999 	logfile.Delete(logfile.Length()-1,1);
       
  1000 	AppendVariantName(logfile);
       
  1001 	iRTest.Printf(KDisplayLogFile, &logfile);
       
  1002 	iFs.MkDirAll(logfile);
       
  1003 
       
  1004 	iLogBuf=HBufC::NewL(KMaxLogLineLength);
       
  1005 	iLogBuf8=HBufC8::NewL(KMaxLogLineLength);
       
  1006 	TInt pos=0;
       
  1007 	TInt ret=KErrNone;
       
  1008 	ret=iFile.Open(iFs,logfile,EFileWrite|EFileShareAny);
       
  1009 	if (ret==KErrNotFound)
       
  1010 		ret=iFile.Create(iFs,logfile,EFileWrite|EFileShareAny);
       
  1011 	if (ret==KErrNone)
       
  1012 		return(iFile.Seek(ESeekEnd,pos));
       
  1013 	else
       
  1014 		return(ret);
       
  1015 	}
       
  1016 
       
  1017 void CTestUtils::CloseMainLog()
       
  1018 	{
       
  1019 	delete iLogBuf;
       
  1020 	iLogBuf=0;
       
  1021 	delete iLogBuf8;
       
  1022 	iLogBuf8=0;
       
  1023 
       
  1024 	iFile.Close();
       
  1025 	}
       
  1026 
       
  1027 
       
  1028 EXPORT_C void CTestUtils::TestStart(TInt aTest, const TDesC& aTestDescription)
       
  1029 	{
       
  1030 	WriteToLog(KTestStartingWithDesc, aTest, &aTestDescription);
       
  1031 	}
       
  1032 
       
  1033 EXPORT_C void CTestUtils::TestStart(TInt aTest)
       
  1034 	{
       
  1035 	WriteToLog(KTestStarting, aTest);
       
  1036 	}
       
  1037 
       
  1038 
       
  1039 
       
  1040 EXPORT_C void CTestUtils::TestFinish(TInt aTest, TInt aError)
       
  1041 	{
       
  1042 	aError ?
       
  1043 		WriteToLog(KTestFailed, aTest, aError)
       
  1044 		: WriteToLog(KTestPassed, aTest);
       
  1045 	}
       
  1046 
       
  1047 
       
  1048 EXPORT_C void CTestUtils::TestHarnessCompleted()
       
  1049 	{
       
  1050 	WriteToLog(KTestHarnessCompleted);
       
  1051 	}
       
  1052 
       
  1053 EXPORT_C void CTestUtils::TestHarnessFailed(TInt aError)
       
  1054 	{	
       
  1055 	WriteToLog(KTestHarnessFailed, aError);
       
  1056 	}
       
  1057 
       
  1058 EXPORT_C void CTestUtils::WriteComment(const TDesC& aComment)
       
  1059 	{
       
  1060 	/* Prepends KTestCommentPrepend to aComment, then writes it to aFlogger */
       
  1061 
       
  1062 	TBuf<0x100> buf;
       
  1063 	buf.Copy(aComment);
       
  1064 	buf.Trim();
       
  1065 	buf.Insert(0,KTestCommentPrepend);
       
  1066 	WriteToLog(buf);
       
  1067 	}
       
  1068 
       
  1069 void CTestUtils::WriteToLog(TRefByValue<const TDesC> aFmt,...)
       
  1070 	{
       
  1071 	_LIT(KDateFormatString, "%D%M%*Y%1%/1%2%/2%3 %H%:1%T%:2%S ");
       
  1072 	_LIT(Kcr,"\r\n");
       
  1073 	iLogBuf->Des().Zero();
       
  1074 	TTime date;
       
  1075 	date.HomeTime();
       
  1076 	TBuf<18> dateString;
       
  1077 	TRAPD(error, date.FormatL(dateString,(KDateFormatString)));
       
  1078 	iLogBuf->Des().Copy(dateString);
       
  1079 	
       
  1080 	VA_LIST list;
       
  1081 	VA_START(list,aFmt);
       
  1082 
       
  1083 	iLogBuf->Des().AppendFormatList(aFmt,list);
       
  1084 	iLogBuf->Des().Append(Kcr);
       
  1085 	iLogBuf8->Des().Copy(*iLogBuf);
       
  1086 	iFile.Write(*iLogBuf8);
       
  1087 	}
       
  1088 
       
  1089 EXPORT_C void CTestUtils::OpenResourceFileL(const TDesC& aResFileName, RResourceFile& aResFile)
       
  1090 	{
       
  1091 	TFileName fileName;
       
  1092     Dll::FileName(fileName);
       
  1093     TParse parse;
       
  1094     parse.Set(aResFileName, &fileName, 0);
       
  1095 	fileName=parse.FullName();
       
  1096 	BaflUtils::NearestLanguageFile(iFs, fileName);
       
  1097 	aResFile.OpenL(iFs, fileName);
       
  1098 	}
       
  1099 
       
  1100 EXPORT_C void CTestUtils::ReadResourceStringL(RResourceFile& aResFile, TInt aId, TDes& rString)
       
  1101 	{
       
  1102 	HBufC8* buf = aResFile.AllocReadLC(aId);
       
  1103 	TResourceReader reader;
       
  1104 	reader.SetBuffer(buf);
       
  1105 	rString.Copy(reader.ReadTPtrC());
       
  1106 	CleanupStack::PopAndDestroy(buf);
       
  1107 	}
       
  1108 
       
  1109 EXPORT_C void CTestUtils::ReadResourceStringL(const TDesC& aResFileName, TInt aId, TDes& rString)
       
  1110 	{
       
  1111 	RResourceFile resFile;
       
  1112 	OpenResourceFileL(aResFileName, resFile);
       
  1113 	CleanupClosePushL(resFile);
       
  1114 	ReadResourceStringL(resFile, aId, rString);
       
  1115 	CleanupStack::PopAndDestroy(); //resFile
       
  1116 	}
       
  1117 
       
  1118 EXPORT_C void CTestUtils::SetSimTsyTestNumberL(TInt aTestNumber)
       
  1119 	{
       
  1120 	TInt testState;
       
  1121 	if(KErrNone != RProperty::Get(KUidSystemCategory, KUidSASimTsyTestNumberValue, testState))
       
  1122 		{
       
  1123 		User::LeaveIfError(RProperty::Define(KUidSystemCategory, KUidSASimTsyTestNumberValue, RProperty::EInt));
       
  1124 		}
       
  1125 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KUidSASimTsyTestNumberValue, aTestNumber));
       
  1126 	}
       
  1127 
       
  1128 EXPORT_C void CTestUtils::CopyToSimTsyConfigFileL(RFs& aFs, const TDesC& aFilename)
       
  1129 	{
       
  1130 	_LIT(KFileName,"config.txt");
       
  1131 	TChar driveChar=RFs::GetSystemDriveChar();
       
  1132  	TBuf<2> systemDrive;
       
  1133  	systemDrive.Append(driveChar);
       
  1134  	systemDrive.Append(KDriveDelimiter);	
       
  1135 	aFs.SetSessionPath(systemDrive);
       
  1136 	aFs.Delete(KFileName);					//< Ignore Error
       
  1137 
       
  1138 	CFileMan* fileMan = CFileMan::NewL(aFs);
       
  1139 	CleanupStack::PushL(fileMan);
       
  1140 
       
  1141 	User::LeaveIfError(fileMan->Copy(aFilename, KFileName));
       
  1142 
       
  1143 	CleanupStack::PopAndDestroy(fileMan);
       
  1144 	}
       
  1145 
       
  1146 EXPORT_C TInt CTestUtils::SeekLogFile(TSeek aMode,TInt& aPos)
       
  1147 	{
       
  1148 	return (iFile.Seek(aMode,aPos));
       
  1149 	}
       
  1150 
       
  1151 
       
  1152 void CTestUtils::ConstructKeepLogFileL()
       
  1153 	{
       
  1154 	User::LeaveIfError(iFs.Connect());
       
  1155 	AppendMainLogL();
       
  1156 	iMenu = new (ELeave) CArrayPtrFlat<HBufC>(10);
       
  1157 	}