pimappservices/calendar/tsrc/caltestlib.cpp
changeset 0 f979ecb2b13e
child 12 38571fd2a704
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1997-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 "caltestlib.h"
       
    17 
       
    18 #include <calentryview.h>
       
    19 #include <calinstance.h>
       
    20 #include <calinstanceview.h>
       
    21 #include <caldataexchange.h>
       
    22 #include <caldataformat.h>
       
    23 #include <calsession.h>
       
    24 #include <calprogresscallback.h>
       
    25 #include <e32test.h>
       
    26 #include <consolealarmalertservermain.h>
       
    27 #include <e32math.h>
       
    28 #include <caliterator.h>
       
    29 #include <s32file.h>
       
    30 #include <ecom/ecom.h>
       
    31 #include <caldataformat.h>
       
    32 #include <calinstance.h>
       
    33 #include <s32mem.h>
       
    34 #include <e32std.h>
       
    35 
       
    36 #ifdef __WINS__
       
    37 const TUid KServerUid2 = {0x1000008D};
       
    38 const TUid KServerUid3 = {0x101F502A};
       
    39 _LIT(KConsoleAlarmAlertServerImg, "ConsoleAlarmAlertServer");	
       
    40 #endif // __WINS__
       
    41 
       
    42 _LIT(KCalTestLibRefCountChunkName, "CalTestLibRefCount");
       
    43 
       
    44 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
    45  * Constructor/Destructor
       
    46  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    47 
       
    48 EXPORT_C CCalTestLibrary* CCalTestLibrary::NewLC(TBool aDelFileFlag)
       
    49 	{
       
    50 	CCalTestLibrary* self = new (ELeave) CCalTestLibrary(aDelFileFlag);
       
    51 	CleanupStack::PushL(self);
       
    52 	self->BaseConstructL();
       
    53 	return (self);
       
    54 	}
       
    55 
       
    56 
       
    57 EXPORT_C CCalTestLibrary* CCalTestLibrary::NewL(TBool aDelFileFlag)
       
    58 	{
       
    59 	CCalTestLibrary* self = NewLC(aDelFileFlag);
       
    60 	CleanupStack::Pop(self);
       
    61 	return (self);
       
    62 	}
       
    63 
       
    64 
       
    65 // protected
       
    66 
       
    67 EXPORT_C CCalTestLibrary::CCalTestLibrary(TBool aDelFileFlag) : iDelFileFlag(aDelFileFlag)
       
    68 	{
       
    69 	TTime now;
       
    70 	now.UniversalTime();
       
    71 	iSeed = now.Int64();
       
    72 	}
       
    73 
       
    74 
       
    75 // protected
       
    76 
       
    77 EXPORT_C void CCalTestLibrary::BaseConstructL()
       
    78 	{
       
    79 	#if __WINS__
       
    80 		StartAlarmServerL();
       
    81 	#endif
       
    82 	User::LeaveIfError(iFs.Connect());
       
    83 	
       
    84 	if (iChunkMutex.OpenGlobal(KCalTestLibMutex) != KErrNone)
       
    85 		{
       
    86 		User::LeaveIfError(iChunkMutex.CreateGlobal(KCalTestLibMutex)) ;
       
    87 		}
       
    88 	TInt createChunkError = iChunk.CreateGlobal(KCalTestLibRefCountChunkName, sizeof(TInt), sizeof(TInt));
       
    89 	iChunk.OpenGlobal(KCalTestLibRefCountChunkName, EFalse);
       
    90 	iRefCount = reinterpret_cast<TInt*>(iChunk.Base());
       
    91 	TBool refExists = ETrue;
       
    92 	if (createChunkError != KErrAlreadyExists)
       
    93 		{
       
    94 		refExists = EFalse;	
       
    95 		}
       
    96 	OpenCalendarSessionL(refExists);
       
    97 	iFilesToDel = new(ELeave) CDesCArrayFlat(5);
       
    98 	User::LeaveIfError(iPIMTestServer.Connect());
       
    99 	SetTimeZoneL(KDefaultTimeZone);
       
   100 	
       
   101 	iTestRegister = CTestRegister::NewL();
       
   102 	User::LeaveIfError(iAlarmServer.Connect());
       
   103 	}
       
   104 
       
   105 void CCalTestLibrary::OpenCalendarSessionL(TBool aRefCountExists)
       
   106 	{
       
   107 	iChunkMutex.Wait();
       
   108 	iSession = CCalSession::NewL();
       
   109 	if (aRefCountExists)
       
   110 		{
       
   111 		++*iRefCount;
       
   112 		}
       
   113 	else
       
   114 		{
       
   115 		*iRefCount = 1;
       
   116 		}
       
   117 	iChunkMutex.Signal();
       
   118 	}
       
   119 
       
   120 void CCalTestLibrary::CloseCalendarSession(TBool aWaitForAgendaToClose)
       
   121 	{
       
   122 	iChunkMutex.Wait();
       
   123 	delete iSession;
       
   124 	iSession = NULL;	
       
   125 	if (iRefCount && (--*iRefCount <= 0) && aWaitForAgendaToClose)
       
   126 		{
       
   127 		WaitForAgendaServerClose();
       
   128 		}
       
   129 	iChunkMutex.Signal();
       
   130 	}
       
   131 	
       
   132 EXPORT_C void CCalTestLibrary::WaitForAgendaServerClose()
       
   133 	{
       
   134 	// This is the last time we are closing the cal test library	
       
   135 	// So wait for the agenda server process to finish
       
   136 	
       
   137 	// Make sure any ecom sessions are destroyed before waiting for the agenda server to close
       
   138 	REComSession::FinalClose();
       
   139 	
       
   140 	// the name of the agenda server process includes its uid like this [10003a5b]
       
   141 	TFindProcess findProcess(_L("*[10003a5b]*"));
       
   142 	TFullName fullName;
       
   143 	TInt findProcessResult = findProcess.Next(fullName);
       
   144 	
       
   145 	if (findProcessResult == KErrNone)
       
   146 		{
       
   147 		// find the latest agenda server process
       
   148 		while (findProcessResult == KErrNone)
       
   149 			{
       
   150 			findProcessResult = findProcess.Next(fullName);
       
   151 			}
       
   152 	
       
   153 		// the agenda server process is running so wait 
       
   154 		RProcess process;
       
   155 		if (process.Open(fullName) == KErrNone)
       
   156 			{
       
   157 			RTimer timeoutTimer;
       
   158 			timeoutTimer.CreateLocal();
       
   159 			TRequestStatus timeoutTimerStatus;
       
   160 			timeoutTimer.After(timeoutTimerStatus, 10000000); // 10 seconds
       
   161 			TRequestStatus processStatus;
       
   162 			process.Logon(processStatus); // ask for a callback when the process ends
       
   163 					
       
   164 			User::WaitForRequest(processStatus, timeoutTimerStatus);
       
   165 					
       
   166 			if (timeoutTimerStatus.Int() == KRequestPending)
       
   167 				{
       
   168 				// the process ended so cancel the timeout timer
       
   169 				timeoutTimer.Cancel();
       
   170 				User::WaitForRequest(timeoutTimerStatus);
       
   171 				}
       
   172 			else if (processStatus.Int() == KRequestPending)
       
   173 				{
       
   174 				process.LogonCancel(processStatus);
       
   175 				User::WaitForRequest(processStatus);
       
   176 				}
       
   177 			
       
   178 			process.Close();
       
   179 			}
       
   180 		}
       
   181 	}
       
   182 	
       
   183 EXPORT_C void CCalTestLibrary::CloseAgendaServer()
       
   184 	{
       
   185 	delete iDataExchange;
       
   186 	iDataExchange = NULL;
       
   187 
       
   188 	delete iIter;
       
   189 	iIter = NULL;;
       
   190 
       
   191 	delete iInstanceView;
       
   192 	iInstanceView = NULL;;
       
   193 
       
   194 	delete iEntryView;
       
   195 	iEntryView = NULL;;
       
   196 
       
   197 	CloseCalendarSession();
       
   198 	}
       
   199 	
       
   200 EXPORT_C void CCalTestLibrary::ConnectAgendaServerL()
       
   201 	{
       
   202 	if (!iSession)
       
   203 		{
       
   204 		OpenCalendarSessionL();		
       
   205 		}
       
   206 	}
       
   207 	
       
   208 EXPORT_C CCalTestLibrary::~CCalTestLibrary()
       
   209 	{
       
   210 	// Delete created files
       
   211 	if(iDelFileFlag && iFilesToDel)
       
   212 		{
       
   213 		for(TInt i = 0; i < iFilesToDel->Count(); ++i)
       
   214 			{
       
   215 			TRAP_IGNORE(iSession->DeleteCalFileL((*iFilesToDel)[i]));
       
   216 			}
       
   217 		// Empty the file list:
       
   218 		iFilesToDel->Reset();
       
   219 		}
       
   220 	delete iFilesToDel;
       
   221 	delete iDataExchange;
       
   222 	iPIMTestServer.Close();
       
   223 	
       
   224 	delete iIter;
       
   225 	delete iInstanceView;
       
   226 	delete iEntryView;
       
   227 	delete iTestRegister;
       
   228 	iFs.Close();
       
   229 
       
   230 	if (iChunkMutex.IsHeld())
       
   231 		{
       
   232 		iChunkMutex.Signal();
       
   233 		}
       
   234 		
       
   235 	CloseCalendarSession();
       
   236 
       
   237 	iChunkMutex.Close();
       
   238 	
       
   239 	iChunk.Close();
       
   240 	iAlarmServer.Close();
       
   241 	}
       
   242 	
       
   243 EXPORT_C CTestRegister& CCalTestLibrary::TestRegister()
       
   244 	{
       
   245 	return *iTestRegister;
       
   246 	}
       
   247 
       
   248 #if __WINS__
       
   249 EXPORT_C void CCalTestLibrary::StartAlarmServerL()
       
   250 	{
       
   251 	TInt findProcessResult(KErrNone);
       
   252 	
       
   253 	// the name of the console alarm alert server process includes its uid like this [101F502A]
       
   254 	_LIT(KAlarmServerUIDMatch, "*[101F502A]*");
       
   255 	TFindProcess findProcess(KAlarmServerUIDMatch);
       
   256 	TFullName fullName;
       
   257 	findProcessResult = findProcess.Next(fullName);
       
   258 	if (findProcessResult != KErrNone)
       
   259 		{
       
   260 		// The console alarm alert server process is not running. Start it
       
   261 		const TUidType serverUid(KNullUid, KServerUid2, KServerUid3);
       
   262 		
       
   263 		RProcess alarmAlertServer;
       
   264 
       
   265 		User::LeaveIfError(alarmAlertServer.Create(KConsoleAlarmAlertServerImg, KNullDesC, serverUid));
       
   266 		
       
   267 		TRequestStatus stat;
       
   268 		alarmAlertServer.Rendezvous(stat);
       
   269 		if (stat != KRequestPending)
       
   270 			{
       
   271 			alarmAlertServer.Kill(0);
       
   272 			}
       
   273 		else
       
   274 			{
       
   275 			alarmAlertServer.Resume();	// logon OK - start the server
       
   276 			}
       
   277 		alarmAlertServer.Close();
       
   278 		User::WaitForRequest(stat);
       
   279 		TInt result = stat.Int();
       
   280 		if(result != KErrNone && result != KErrAlreadyExists)
       
   281 			{
       
   282 			User::LeaveIfError(result);	
       
   283 			}
       
   284 		}
       
   285 	}
       
   286 #endif // __WINS__
       
   287 
       
   288 
       
   289 
       
   290 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   291  * Implementation of MCalProgressCallBack interface
       
   292  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   293 
       
   294 void CCalTestLibrary::Progress(TInt /*aPercentageCompleted*/)
       
   295 	{
       
   296 	}
       
   297 
       
   298 
       
   299 void CCalTestLibrary::Completed(TInt aError)
       
   300 	{
       
   301 	CActiveScheduler::Stop();
       
   302 	iCompletedError = aError;
       
   303 	}
       
   304 
       
   305 
       
   306 TBool CCalTestLibrary::NotifyProgress()
       
   307 	{
       
   308 	return (EFalse);
       
   309 	}
       
   310 
       
   311 
       
   312 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   313  * File handling
       
   314  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   315 
       
   316 EXPORT_C RFs& CCalTestLibrary::FileSession()
       
   317 	{
       
   318 	return (iFs);
       
   319 	}
       
   320 
       
   321 EXPORT_C void CCalTestLibrary::RegisterCalFileL(const TDesC& aFileName)
       
   322 	{
       
   323 	TInt pos=-1;
       
   324 	
       
   325 	if(iDelFileFlag)
       
   326 		{
       
   327 		if(iFilesToDel->Find(aFileName,pos)!=0) 
       
   328 			{
       
   329 			iFilesToDel->AppendL(aFileName);
       
   330 			}
       
   331 		}
       
   332 	}
       
   333 
       
   334 EXPORT_C void CCalTestLibrary::CreateFileL(const TDesC& aFileName)
       
   335 	{
       
   336 	iSession->CreateCalFileL(aFileName);
       
   337 	TInt pos=-1;
       
   338 	if(iDelFileFlag)
       
   339 		{
       
   340 		if(iFilesToDel->Find(aFileName, pos)!=0)	
       
   341 			{
       
   342 			iFilesToDel->AppendL(aFileName);
       
   343 			}
       
   344 		}
       
   345 	}
       
   346 
       
   347 
       
   348 EXPORT_C void CCalTestLibrary::ReplaceFileL(const TDesC& aFileName)
       
   349 	{
       
   350 	TRAPD(err, CreateFileL(aFileName));
       
   351 	
       
   352 	if (err == KErrAlreadyExists)
       
   353 		{
       
   354 		DeleteFileL(aFileName);
       
   355 		
       
   356 		CreateFileL(aFileName);
       
   357 		}
       
   358 	else
       
   359 		{
       
   360 		User::LeaveIfError(err);
       
   361 		}
       
   362 	}
       
   363 
       
   364 EXPORT_C void CCalTestLibrary::OpenFileL(const TDesC& aFileName, TBool aDelFileFlag) 
       
   365 	{
       
   366 	delete iDataExchange;
       
   367 	iDataExchange = NULL;
       
   368 
       
   369 	delete iIter;
       
   370 	iIter = NULL;	
       
   371 	
       
   372 	delete iEntryView;
       
   373 	iEntryView = NULL;
       
   374 	
       
   375 	delete iInstanceView;
       
   376 	iInstanceView = NULL;
       
   377 	
       
   378 	CloseCalendarSession(EFalse);
       
   379 	OpenCalendarSessionL();
       
   380 
       
   381 	iSession->OpenL(aFileName);
       
   382 	TInt pos=-1;
       
   383 	if( iDelFileFlag && aDelFileFlag )
       
   384 		{
       
   385 		if( iFilesToDel->Find(aFileName, pos)!=0 )
       
   386 			{
       
   387 			iFilesToDel->AppendL(aFileName);
       
   388 			}
       
   389 		}
       
   390 	}
       
   391 
       
   392 
       
   393 EXPORT_C void CCalTestLibrary::DeleteFileL(const TDesC& aFileName, TBool aIsCalFile)
       
   394 	{
       
   395 	if (!iSession) // some tests call CloseAgendaServer
       
   396 		{
       
   397 		OpenCalendarSessionL();	
       
   398 		}
       
   399 	
       
   400 	if (aIsCalFile)
       
   401 		{
       
   402 		iSession->DeleteCalFileL(aFileName);
       
   403 		TInt indx=-1;
       
   404 		if(iDelFileFlag)
       
   405 			{
       
   406 			if(iFilesToDel->Find(aFileName, indx)==0)
       
   407 				{
       
   408 				iFilesToDel->Delete(indx,1);
       
   409 				}
       
   410 			}
       
   411 		}
       
   412 		else
       
   413 			{
       
   414 			iPIMTestServer.DeleteFileL(aFileName);
       
   415 			}
       
   416 	}
       
   417 
       
   418 
       
   419 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   420  * Session and Views handling
       
   421  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   422 
       
   423 EXPORT_C CCalSession& CCalTestLibrary::GetSession()
       
   424 	{
       
   425 	// the pointyer is never NULL because the object is created in NewL...
       
   426 	return (*iSession);
       
   427 	}
       
   428 
       
   429 
       
   430 EXPORT_C CCalEntryView& CCalTestLibrary::AsynCGetEntryViewL()
       
   431 	{
       
   432 	//The caller should expect the completion in their RunL
       
   433 	if (!iEntryView)
       
   434 		{
       
   435 		iEntryView = CCalEntryView::NewL(*iSession, *this);
       
   436 		}
       
   437 
       
   438 	return (*iEntryView);
       
   439 	}
       
   440 
       
   441 
       
   442 EXPORT_C CCalInstanceView& CCalTestLibrary::AsynCGetInstanceViewL()
       
   443 	{
       
   444 	if (!iInstanceView)
       
   445 		{
       
   446 		iInstanceView = CCalInstanceView::NewL(*iSession, *this);
       
   447 		}
       
   448 
       
   449 	return (*iInstanceView);
       
   450 	}
       
   451 
       
   452 
       
   453 EXPORT_C CCalInstanceView& CCalTestLibrary::SynCGetInstanceViewL()
       
   454 	{
       
   455 	if (!iInstanceView)
       
   456 		{
       
   457 		iInstanceView = CCalInstanceView::NewL(*iSession);
       
   458 		}
       
   459 
       
   460 	return (*iInstanceView);
       
   461 	}
       
   462 
       
   463 
       
   464 EXPORT_C CCalEntryView& CCalTestLibrary::SynCGetEntryViewL()
       
   465 	{
       
   466 	if (!iEntryView)
       
   467 		{
       
   468 		iEntryView = CCalEntryView::NewL(*iSession);
       
   469 		}
       
   470 		
       
   471 	return (*iEntryView);
       
   472 	}
       
   473 
       
   474 EXPORT_C void CCalTestLibrary::DeleteViews()
       
   475 	{
       
   476 	delete iInstanceView;
       
   477 	iInstanceView = NULL;
       
   478 	delete iEntryView;
       
   479 	iEntryView = NULL;
       
   480 	delete iIter;
       
   481 	iIter = NULL;
       
   482 	delete iDataExchange;
       
   483 	iDataExchange = NULL;
       
   484 	}
       
   485 
       
   486 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   487  * Entry handling
       
   488  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   489 
       
   490 EXPORT_C CCalEntry* CCalTestLibrary::CreateCalEntryL(CCalEntry::TType aType, HBufC8*& aUid)
       
   491 	{
       
   492 	HBufC8* uid = NULL;
       
   493 	if (aUid)
       
   494 		{
       
   495 		uid = aUid;
       
   496 		}
       
   497 	else
       
   498 		{
       
   499 		uid = HBufC8::NewLC(255);
       
   500 		TPtr8 uidP = uid->Des();
       
   501 		RandomText8(uidP);
       
   502 		}
       
   503 
       
   504 	CCalEntry* entry = CCalEntry::NewL(aType, uid, CCalEntry::EMethodAdd, 0);
       
   505 	if (!aUid)
       
   506 		{
       
   507 		CleanupStack::Pop(uid);
       
   508 		}
       
   509 	aUid = uid;
       
   510 	
       
   511 	return (entry);
       
   512 	}
       
   513 
       
   514 EXPORT_C void CCalTestLibrary::SetEntryStartAndEndTimeL(CCalEntry* aEntry, const TTime& aStartTime, const TTime& aEndTime, TInt aYear)
       
   515 	{
       
   516 	TTime today;
       
   517 	today.HomeTime();
       
   518 	TInt64 seed  = today.Int64();
       
   519 
       
   520 	TInt date    = Math::Rand(seed);
       
   521 	TInt year    = aYear;
       
   522 	TInt month   = date % 12;
       
   523 	TInt day     = date % 28;
       
   524 	TInt hours   = date % 24;
       
   525 	TInt minutes = date % 60;
       
   526 
       
   527 	TTime startRanTime(TDateTime(year, (TMonth)month, day, hours, minutes, 0, 0));
       
   528 	TTime startTime;
       
   529 	TTime endTime;
       
   530 	switch (aEntry->EntryTypeL())
       
   531 		{
       
   532 		case CCalEntry::EAppt:
       
   533 		case CCalEntry::ETodo:
       
   534 			if (aStartTime == Time::NullTTime() && aEndTime == Time::NullTTime())
       
   535 				{
       
   536 				startTime = startRanTime.Int64();
       
   537 				endTime   = startRanTime + TTimeIntervalHours(2);
       
   538 				}
       
   539 			else
       
   540 				{
       
   541 				startTime = aStartTime.Int64();
       
   542 				endTime   = aEndTime.Int64();
       
   543 				}
       
   544 			break;
       
   545 			
       
   546 		case CCalEntry::EReminder:
       
   547 		case CCalEntry::EEvent:
       
   548 		case CCalEntry::EAnniv:
       
   549 			if (aStartTime == Time::NullTTime())
       
   550 				{
       
   551 				startTime = startRanTime.Int64();
       
   552 				}
       
   553 			else
       
   554 				{
       
   555 				startTime = aStartTime.Int64();
       
   556 				}
       
   557 			endTime = startTime;
       
   558 			break;
       
   559 			
       
   560 		default:
       
   561 			User::Invariant();
       
   562 			break;
       
   563 		}
       
   564 				
       
   565 	TCalTime startCalTime;
       
   566 	startCalTime.SetTimeUtcL(startTime);
       
   567 	TCalTime endCalTime;
       
   568 	endCalTime.SetTimeUtcL(endTime);
       
   569 	aEntry->SetStartAndEndTimeL(startCalTime, endCalTime);	
       
   570 	}
       
   571 
       
   572 
       
   573 EXPORT_C void CCalTestLibrary::RandomText(TDes& aText, TBool aPossibleNulDes)
       
   574 	{
       
   575 	aText.Zero();
       
   576 	
       
   577 	if (aText.MaxLength() <= 8)
       
   578 		{
       
   579 		User::Panic(_L("RandomText - descriptor too short"), 0);
       
   580 		}
       
   581 		
       
   582 	if (aPossibleNulDes)
       
   583 		{
       
   584 		if (Math::Rand(iSeed) % 2)
       
   585 			{
       
   586 			aText=KNullDesC;
       
   587 			}
       
   588 		}
       
   589 	else
       
   590 		{
       
   591 		TInt numChar = 0;
       
   592 		do	{
       
   593 			numChar = Math::Rand(iSeed) % (aText.MaxLength() - 1);
       
   594 			}
       
   595 		while (numChar < 8);
       
   596 		
       
   597 		for (TInt i = 0;i < numChar; ++i)
       
   598 			{
       
   599 			TInt c = Math::Rand(iSeed) % 6;
       
   600 			c = (c == 0) ? ' ' : ( (Math::Rand(iSeed) % 26) + ((c == 2) ? 'A' : 'a') );
       
   601 			aText.Append(c);
       
   602 			}
       
   603 		}
       
   604 	}
       
   605 
       
   606 
       
   607 EXPORT_C void CCalTestLibrary::RandomText8(TDes8& aText, TBool aPossibleNulDes)
       
   608 	{
       
   609 	aText.Zero();
       
   610 		
       
   611 	if (aPossibleNulDes)
       
   612 		{
       
   613 		if (Math::Rand(iSeed) % 2)
       
   614 			{
       
   615 			aText = KNullDesC8;
       
   616 			}
       
   617 		}
       
   618 	else
       
   619 		{
       
   620 		// The number of characters in the guid should be
       
   621 		// more than 8 (to ensure the guids are not too small)
       
   622 		// less than 255 (this is the maximum length of a guid)
       
   623 		// less than MaxLength of the descriptor passed in		
       
   624 		TInt numChar = 0;
       
   625 		do	{
       
   626 			numChar = Math::Rand(iSeed) % (aText.MaxLength() - 1);
       
   627 			}
       
   628 		while( numChar > 255 || numChar < 8 );
       
   629 		
       
   630 		for (TInt i = 0; i < numChar; ++i)
       
   631 			{
       
   632 			TInt c = Math::Rand(iSeed) % 6;
       
   633 			c = Math::Rand(iSeed) % 26 + ((c == 2)? 'A' : 'a');
       
   634 			aText.Append(c);
       
   635 			}
       
   636 		}
       
   637 	}
       
   638 
       
   639 
       
   640 EXPORT_C void CCalTestLibrary::CleanDatabaseL()
       
   641 	{
       
   642 	CDesC8ArrayFlat* guidArray = new (ELeave) CDesC8ArrayFlat(1);
       
   643 	CleanupStack::PushL(guidArray);
       
   644 
       
   645 	CCalIter& iter = GetIterL();
       
   646 	
       
   647 	for (TPtrC8 Uid(iter.FirstL()); Uid != KNullDesC8(); Uid.Set(iter.NextL()))
       
   648 		{
       
   649 		guidArray->AppendL(Uid);
       
   650 		}
       
   651 
       
   652 	if (guidArray->Count() > 0)
       
   653 		{
       
   654   		SynCGetEntryViewL().DeleteL(*guidArray);
       
   655 		}
       
   656   	
       
   657 	CleanupStack::PopAndDestroy(guidArray);
       
   658 	}
       
   659 
       
   660 
       
   661 EXPORT_C CCalIter& CCalTestLibrary::GetIterL()
       
   662 	{
       
   663 	if (!iIter)
       
   664 		{
       
   665 		iIter = CCalIter::NewL(*iSession);
       
   666 		}
       
   667 		
       
   668 	return (*iIter);		
       
   669 	}
       
   670 
       
   671 
       
   672 EXPORT_C void CCalTestLibrary::StoreEntryL(const CCalEntry& aEntry)
       
   673 	{
       
   674 	RPointerArray<CCalEntry> entriesToAdd;
       
   675 	CleanupClosePushL(entriesToAdd);
       
   676 	entriesToAdd.AppendL(&aEntry);
       
   677 	TInt success = 0;
       
   678 	SynCGetEntryViewL().StoreL(entriesToAdd, success);
       
   679 	CleanupStack::PopAndDestroy(&entriesToAdd); // entriesToAdd.Close()
       
   680 	}
       
   681 
       
   682 EXPORT_C void CCalTestLibrary::FetchAllInstancesL(RPointerArray<CCalInstance>& aInstances)
       
   683 	{
       
   684 	CalCommon::TCalViewFilter filter = CalCommon::EIncludeAll;
       
   685 	TCalTime minTime;
       
   686 	minTime.SetTimeUtcL(TCalTime::MinTime());
       
   687 	TCalTime maxTime;
       
   688 	maxTime.SetTimeUtcL(TCalTime::MaxTime());
       
   689 	CalCommon::TCalTimeRange timeRange(minTime, maxTime);
       
   690 	
       
   691 	SynCGetInstanceViewL().FindInstanceL(aInstances, filter, timeRange);
       
   692 	}
       
   693 
       
   694 EXPORT_C void CCalTestLibrary::OutputInstanceDataL(const RPointerArray<CCalInstance>& aInstances)
       
   695 	{
       
   696 	for (TInt i = 0; i < aInstances.Count(); i++)
       
   697 		{
       
   698 		TDateTime dt = aInstances[i]->Time().TimeLocalL().DateTime();
       
   699 		TCalLocalUid luid = aInstances[i]->Entry().LocalUidL();
       
   700 		RDebug::Print(_L("Instance %d has local uid %d and occurs at %2d:%2d:%2d on %2d/%2d/%4d"), 
       
   701 			i, luid, dt.Hour(), dt.Minute(), dt.Second(), dt.Day()+1, dt.Month()+1, dt.Year());
       
   702 		}
       
   703 	}
       
   704 
       
   705 EXPORT_C RPIMTestServer& CCalTestLibrary::PIMTestServer()
       
   706     {
       
   707     return iPIMTestServer;
       
   708     }
       
   709 
       
   710 EXPORT_C void CCalTestLibrary::CreateTestFileL(const TDesC8& aVCalData, const TDesC& aFileName)
       
   711 	{
       
   712 	RFile file;
       
   713 	
       
   714 	iTestRegister->CreateTempFileLC(file, aFileName); 												     
       
   715 	User::LeaveIfError(file.Write(aVCalData));
       
   716 	CleanupStack::PopAndDestroy(&file); //file.Close()
       
   717 	}
       
   718 	
       
   719 EXPORT_C CCalDataExchange& CCalTestLibrary::DataExchangeL()
       
   720 	{
       
   721 	if (iDataExchange == NULL)
       
   722 		{
       
   723 		iDataExchange = CCalDataExchange::NewL(*iSession);		
       
   724 		}
       
   725 		
       
   726 	return *iDataExchange;
       
   727 	}
       
   728 
       
   729 EXPORT_C void CCalTestLibrary::ImportL(const TDesC& aImportVCalendarFile, RPointerArray<CCalEntry>& aEntriesImported)
       
   730 	{	
       
   731 	RFile infile;
       
   732     iTestRegister->OpenTempFileLC(infile, aImportVCalendarFile);
       
   733 	RFileReadStream readStream(infile);
       
   734 	CleanupClosePushL(readStream);
       
   735 	
       
   736 	DataExchangeL().ImportL(KUidVCalendar, readStream, aEntriesImported);
       
   737 
       
   738 	CleanupStack::PopAndDestroy(&readStream); // readStream.Close()
       
   739 	CleanupStack::PopAndDestroy(&infile); // infile.Close()
       
   740 	}
       
   741 	
       
   742 EXPORT_C void CCalTestLibrary::ImportL(const TDesC8& aVCalendar, RPointerArray<CCalEntry>& aEntriesImported)
       
   743 	{
       
   744 	RDesReadStream readStream(aVCalendar);
       
   745 	CleanupClosePushL(readStream);
       
   746 	
       
   747 	DataExchangeL().ImportL(KUidVCalendar, readStream, aEntriesImported);
       
   748 
       
   749 	CleanupStack::PopAndDestroy(&readStream);
       
   750 	}
       
   751 	
       
   752 EXPORT_C void CCalTestLibrary::ExportL(const TDesC& aExportVCalendarFile, RPointerArray<CCalEntry>& aEntriesExported)
       
   753 	{
       
   754 	RFile outFile;
       
   755     iTestRegister->CreateTempFileLC(outFile, aExportVCalendarFile);
       
   756 	RFileWriteStream writeStream(outFile);
       
   757 	CleanupClosePushL(writeStream);
       
   758 	
       
   759 	DataExchangeL().ExportL(KUidVCalendar, writeStream, aEntriesExported);
       
   760 
       
   761 	writeStream.CommitL();
       
   762 
       
   763 	CleanupStack::PopAndDestroy(&writeStream); // writeStream.Close()
       
   764 	CleanupStack::PopAndDestroy(&outFile); // outFile.Close()
       
   765 	}
       
   766 
       
   767 EXPORT_C void CCalTestLibrary::SetTimeZoneL(const TDesC8& aTimeZoneName)
       
   768 	{
       
   769 	CSetTimezone* Timezone = CSetTimezone::NewL(aTimeZoneName, &iPIMTestServer);
       
   770 	CleanupStack::PushL(Timezone);
       
   771 	Timezone->Start();
       
   772 	CActiveScheduler::Start();
       
   773 	CleanupStack::PopAndDestroy(Timezone);
       
   774 	}
       
   775 	
       
   776 
       
   777 
       
   778 EXPORT_C void CCalTestLibrary::__dbgClearTzClientCacheL(TBool aRestartCaching)
       
   779 	{
       
   780 	iSession->__dbgClearTzClientCacheL(aRestartCaching);	
       
   781 	}
       
   782 
       
   783 /*
       
   784   WaitForAlarmEvent
       
   785   Waits for specific alarm's event.
       
   786   @param aAlarmId - id of alarm we are waiting for notification. If KNullAlarmId then wait for notification from any alarm
       
   787   @param aExpectedEvent - event type we are waiting for
       
   788   @aExpectedState - expected state of given alarm. Used only if aExpectedEvent is EAlarmChangeEventState.
       
   789   @param aTimeout - timeout in microseconds (applied in parallel with every call to WaitForRequest)
       
   790   @return ETrue if alarm request was completed with expected event (and, in case of EAlarmChangeEventState, alarm is in correct state)
       
   791           EFalse if timeout was reached.
       
   792  */
       
   793 EXPORT_C TBool CCalTestLibrary::WaitForAlarmEvent(TAlarmId aAlarmId, TAlarmChangeEvent aExpectedEvent, TAlarmState aExpectedState, TInt aTimeout)
       
   794 	{
       
   795 	TRequestStatus status;
       
   796 	TRequestStatus statusTimeout;
       
   797 	RTimer timer;
       
   798 	CleanupClosePushL(timer);
       
   799 	User::LeaveIfError(timer.CreateLocal());
       
   800 	TAlarmId returnedAlarmId;
       
   801 	FOREVER
       
   802 		{
       
   803 		iAlarmServer.NotifyChange(status,returnedAlarmId);
       
   804 		timer.After(statusTimeout,TTimeIntervalMicroSeconds32(aTimeout));
       
   805 		User::WaitForRequest(status,statusTimeout); // waits for change of status
       
   806 		if(statusTimeout != KRequestPending)
       
   807 			{
       
   808 			if(statusTimeout == KErrNone)
       
   809 				{
       
   810 				RDebug::Print(_L("Timeout reached\n"));
       
   811 				}
       
   812 			else
       
   813 				{
       
   814 				RDebug::Print(_L("\tTimeout error %d \n"),statusTimeout.Int());
       
   815 				}
       
   816 			iAlarmServer.NotifyChangeCancel();
       
   817 			User::WaitForRequest(status);
       
   818 			CleanupStack::PopAndDestroy(&timer);
       
   819 			return EFalse;
       
   820 			}
       
   821 		else
       
   822 			{
       
   823 			timer.Cancel();
       
   824 			User::WaitForRequest(statusTimeout);
       
   825 			if(statusTimeout == KErrCancel)
       
   826 				{
       
   827 				if((aAlarmId == KNullAlarmId || returnedAlarmId == aAlarmId) && status.Int() == aExpectedEvent) 
       
   828 					{
       
   829 					if(aExpectedEvent == EAlarmChangeEventState)
       
   830 						{
       
   831 						TASShdAlarm alarm;
       
   832 						TInt err = iAlarmServer.GetAlarmDetails(returnedAlarmId,alarm);
       
   833 						if(err == KErrNone || err == KErrNotFound)
       
   834 							{
       
   835 							if(err == KErrNotFound || alarm.State() != aExpectedState)
       
   836 								{
       
   837 								continue;
       
   838 								}
       
   839 							}
       
   840 						else
       
   841 							{
       
   842 							User::Leave(err);
       
   843 							}
       
   844 						}
       
   845 					CleanupStack::PopAndDestroy(&timer);
       
   846 					return ETrue;
       
   847 					}
       
   848 				}
       
   849 			}
       
   850 		}
       
   851     }
       
   852 
       
   853 // A utility class to set the timezone.
       
   854 CSetTimezone::CSetTimezone() : CActive(EPriorityLow)
       
   855 	{
       
   856 	// For some as yet undetermined reason setting the priority of this object 
       
   857 	// to a value other than EPriorityLow results in this class failing to set
       
   858 	// the timezone correctly. Do not change this value.
       
   859 	CActiveScheduler::Add(this);
       
   860 	}
       
   861 	
       
   862 CSetTimezone::~CSetTimezone()
       
   863 	{
       
   864 	Cancel();
       
   865 	if(iZoneId)	
       
   866 		{
       
   867 			delete iZoneId;
       
   868 		}
       
   869 	}
       
   870 
       
   871 CSetTimezone* CSetTimezone::NewL(const TDesC8& aZoneId, RPIMTestServer* aPIMTestServer)
       
   872 	{
       
   873 	CSetTimezone* self = new (ELeave) CSetTimezone;
       
   874 	CleanupStack::PushL(self);
       
   875 	self->ConstructL(aZoneId, aPIMTestServer);
       
   876 	CleanupStack::Pop(self);
       
   877 	return self;
       
   878 	}
       
   879 
       
   880 void CSetTimezone::ConstructL(const TDesC8& aZoneId, RPIMTestServer* aPIMTestServer)
       
   881 	{
       
   882 	iPIMTestServer = aPIMTestServer;
       
   883 	iZoneId = aZoneId.AllocL();
       
   884 	iStep = 0;
       
   885 	}
       
   886 
       
   887 void CSetTimezone::Start()
       
   888 	{
       
   889 	TRequestStatus* status = &iStatus;
       
   890 	User::RequestComplete(status, 0);
       
   891 	SetActive();
       
   892 	}
       
   893 	
       
   894 void CSetTimezone::RunL()
       
   895 	{
       
   896 	TBool complete = EFalse;
       
   897 	switch (iStep)
       
   898 		{
       
   899 		case 0:
       
   900 			iPIMTestServer->SetTimeZoneL(iZoneId->Des());
       
   901 			break;
       
   902 			
       
   903 		default:
       
   904 			complete = ETrue;
       
   905 			break;
       
   906 		}
       
   907 		
       
   908 	if (complete)
       
   909 		{
       
   910 		CActiveScheduler::Stop();
       
   911 		}
       
   912 	else
       
   913 		{
       
   914 		iStep++;	
       
   915 		Start();
       
   916 		}
       
   917 	}
       
   918 	
       
   919 void CSetTimezone::DoCancel()
       
   920 	{
       
   921 	}
       
   922 	
       
   923 EXPORT_C TPerformanceTimer::TPerformanceTimer(RTest& aTest)
       
   924 	:iTest(aTest)
       
   925 	{
       
   926 	}
       
   927 
       
   928 EXPORT_C void TPerformanceTimer::Start()
       
   929 	{
       
   930 	iStartTickCount = User::NTickCount();	
       
   931 	}
       
   932 
       
   933 EXPORT_C void TPerformanceTimer::Stop()
       
   934 	{
       
   935 	iEndTickCount = User::NTickCount();
       
   936 	}
       
   937 	
       
   938 EXPORT_C void TPerformanceTimer::PrintOut() const
       
   939 	{
       
   940 	iTest.Printf(_L("Elapsed time: %d microseconds\n"), ElapsedTime().Int());		
       
   941 	}
       
   942 
       
   943 EXPORT_C TTimeIntervalMicroSeconds32 TPerformanceTimer::ElapsedTime() const
       
   944 	{
       
   945 	TInt tickPeriod;
       
   946 	HAL::Get(HAL::ENanoTickPeriod, tickPeriod);
       
   947 	TUint durationInTicks = iEndTickCount - iStartTickCount;
       
   948 	return tickPeriod * durationInTicks;	
       
   949 	}
       
   950 
       
   951