phonebookengines_old/contactsmodel/tsrc/t_database_killer.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2002-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 <e32std.h>
       
    17 #include <e32test.h>
       
    18 #include <cntdb.h>
       
    19 #include <cntitem.h>
       
    20 #include <cntfldst.h>
       
    21 #include "t_utils2.h"
       
    22 #include "t_database_killer.h"
       
    23 
       
    24 
       
    25 // Test Macro
       
    26 LOCAL_D RTest test(_L("T_DATABASE_KILLER"));
       
    27 
       
    28 _LIT(KTestName,"@SYMTESTCaseID:PIM-T-DATABASE-KILLER-0001 t_database_killer");
       
    29 _LIT(KLogFileName,"t_database_killer.log");
       
    30 
       
    31 _LIT(KDbFileName,"c:t_database_killer.cdb");
       
    32 
       
    33 const TInt KNumSmsContacts=5;
       
    34 const TInt KNumWorkEmailContacts=4;
       
    35 const TInt KNumHomeEmailContacts=4;
       
    36 const TInt KNumLandlineContacts=10;
       
    37 const TInt KNumFaxContacts=4;
       
    38 
       
    39 CTestConductor* CTestConductor::NewL(TBool aKillFourTimes)
       
    40 	{
       
    41 	CTestConductor* self=new(ELeave) CTestConductor(aKillFourTimes);
       
    42 	CleanupStack::PushL(self);
       
    43 	self->ConstructL();
       
    44 	self->RunTestsL();
       
    45 	CleanupStack::Pop();
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 CTestConductor::~CTestConductor()
       
    50 	{
       
    51 	delete iLog;
       
    52 	delete iDb;
       
    53 	delete iTempFiles;
       
    54 	iFs.Close();
       
    55 	}
       
    56 
       
    57 CTestConductor::CTestConductor(TBool aKillFourTimes)
       
    58 	{
       
    59 	iKillFourTimes = aKillFourTimes;
       
    60 	}
       
    61 
       
    62 void CTestConductor::ConstructL()
       
    63 	{
       
    64 	User::LeaveIfError(iFs.Connect());
       
    65 	iTempFiles=CTestRegister::NewLC();
       
    66 	CleanupStack::Pop(iTempFiles);
       
    67 	iTempFiles->RegisterL(KDbFileName, EFileTypeCnt);
       
    68 	iLog=CLog::NewL(test,KLogFileName);
       
    69 	iDb=CContactDatabase::ReplaceL(KDbFileName);
       
    70 	}
       
    71 
       
    72 
       
    73 void CTestConductor::RunTestsL()
       
    74 	{
       
    75 	CDatabaseTester* tester=CDatabaseTester::NewL(*iLog,*iDb,this, iKillFourTimes);
       
    76 	CleanupStack::PushL(tester);
       
    77 	CActiveScheduler::Start();
       
    78 	CleanupStack::Pop(tester);
       
    79 	// error from CDatabaseTester ?
       
    80 	User::LeaveIfError(iTestError);
       
    81 	}
       
    82 
       
    83 void CTestConductor::SetTestError(TInt aTestError)
       
    84 	{
       
    85 	iTestError = aTestError;
       
    86 	}
       
    87 
       
    88 //
       
    89 // CDatabaseTester.
       
    90 //
       
    91 
       
    92 CDatabaseTester* CDatabaseTester::NewL(CLog& aLog,CContactDatabase& aDb,CTestConductor* aTestConductor, TBool aKillFourTimes)
       
    93 	{
       
    94 	CDatabaseTester* self=new(ELeave) CDatabaseTester(aLog,aDb,aTestConductor, aKillFourTimes);
       
    95 	CleanupStack::PushL(self);
       
    96 	self->ConstructL();
       
    97 	CleanupStack::Pop();
       
    98 	return self;
       
    99 	}
       
   100 
       
   101 CDatabaseTester::~CDatabaseTester()
       
   102 	{
       
   103 	delete iRandomGenerator;
       
   104 
       
   105 	DeleteProcess();
       
   106 
       
   107 	}
       
   108 
       
   109 CDatabaseTester::CDatabaseTester(CLog& aLog,CContactDatabase& aDb,CTestConductor* aTestConductor, TBool aKillFourTimes)
       
   110 	: CActive(EPriorityStandard),iLog(aLog),iDb(aDb),iCurrentTest(EInitialisingTests),iTestConductor(aTestConductor)
       
   111 	{
       
   112 	CActiveScheduler::Add(this);
       
   113 	iTotalContacts = 0;
       
   114 	iKillFourTimes = aKillFourTimes;
       
   115 	}
       
   116 
       
   117 void CDatabaseTester::ConstructL()
       
   118 	{
       
   119 	iRandomGenerator=CRandomContactGenerator::NewL();
       
   120 	iRandomGenerator->SetDbL(iDb);
       
   121 	NextTest();
       
   122 	}
       
   123 
       
   124 void CDatabaseTester::RunL()
       
   125 	{
       
   126 	CContactIdArray* arrayOfContacts = NULL;
       
   127 	TInt num = 0;
       
   128 	TInt counter = 0;
       
   129 	TInt halfOfAllContacts = 0;
       
   130 	switch (iCurrentTest)
       
   131 		{
       
   132 		case EAddContacts:
       
   133 			iLog.LogLine(_L("=== Add Contacts to DB"));
       
   134 			AddContactsL();
       
   135 			NextTest();
       
   136 			break;
       
   137 
       
   138 		case EKillThreadFirstTime:
       
   139 			iLog.LogLine(_L("=== Kill Server 1st Time"));
       
   140 			arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   141 			num = arrayOfContacts->Count();
       
   142 			test(num == iTotalContacts);
       
   143 			FindServerAndKillItL(2000);
       
   144 			NextTest();
       
   145 			break;
       
   146 
       
   147 		case ERemoveContacts:
       
   148 			iLog.LogLine(_L("==== Remove Some Contacts"));
       
   149 			arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   150 			num = arrayOfContacts->Count();
       
   151 			test(num == iTotalContacts);
       
   152 
       
   153 			halfOfAllContacts = num / 2;
       
   154 			for ( counter = 0; counter < halfOfAllContacts; counter++)
       
   155 				{
       
   156 				iDb.DeleteContactL((*arrayOfContacts)[counter]);
       
   157 				iTotalContacts--;
       
   158 				}
       
   159 			CleanupStack::PopAndDestroy(arrayOfContacts);
       
   160 			arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   161 			num = arrayOfContacts->Count();
       
   162 			test(num == iTotalContacts);
       
   163 			NextTest();
       
   164 			break;
       
   165 
       
   166 		case EKillThreadSecondTime:
       
   167 			iLog.LogLine(_L("=== Kill Server 2nd Time"));
       
   168 			arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   169 			num = arrayOfContacts->Count();
       
   170 			test(num == iTotalContacts);
       
   171 			FindServerAndKillItL(2000);
       
   172 			NextTest();
       
   173 			break;
       
   174 
       
   175 		case EAddMoreContacts:
       
   176 			iLog.LogLine(_L("=== Add More Contacts to DB"));
       
   177 			AddContactsL();
       
   178 			NextTest();
       
   179 			break;
       
   180 
       
   181 		case EKillThreadThirdTime:
       
   182 			iLog.LogLine(_L("=== Kill Server 3rd Time"));
       
   183 			arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   184 			num = arrayOfContacts->Count();
       
   185 			test(num == iTotalContacts);
       
   186 			FindServerAndKillItL(2000);
       
   187 			NextTest();
       
   188 			break;
       
   189 
       
   190 		case ERemoveMoreContacts:
       
   191 			iLog.LogLine(_L("==== Remove Some More Contacts"));
       
   192 			arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   193 			num = arrayOfContacts->Count();
       
   194 			test(num == iTotalContacts);
       
   195 
       
   196 			halfOfAllContacts = num / 2;
       
   197 			for ( counter = 0; counter < halfOfAllContacts; counter++)
       
   198 				{
       
   199 				iDb.DeleteContactL((*arrayOfContacts)[counter]);
       
   200 				iTotalContacts--;
       
   201 				}
       
   202 			CleanupStack::PopAndDestroy(arrayOfContacts);
       
   203 			arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   204 			num = arrayOfContacts->Count();
       
   205 			test(num == iTotalContacts);
       
   206 			NextTest();
       
   207 			break;
       
   208 
       
   209 		case EKillThreadForthTime:
       
   210 			if (iKillFourTimes)
       
   211 				{
       
   212 				iLog.LogLine(_L("=== Kill Server 4th Time"));
       
   213 				arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   214 				num = arrayOfContacts->Count();
       
   215 				test(num == iTotalContacts);
       
   216 				FindServerAndKillItL(2000);
       
   217 				}
       
   218 			else
       
   219 				{
       
   220 				iLog.LogLine(_L("=== Skipping : Kill Server 4th Time"));
       
   221 				}
       
   222 			NextTest();
       
   223 			break;
       
   224 
       
   225 		case ENumTests:
       
   226 			iLog.LogLine(_L("==== Filter View Finished"));
       
   227 			arrayOfContacts = iDb.FindLC(_L(""), NULL);
       
   228 			num = arrayOfContacts->Count();
       
   229 			test(num == iTotalContacts);
       
   230 			CActiveScheduler::Stop();
       
   231 			delete this;
       
   232 			break;
       
   233 
       
   234 		default:
       
   235 			ASSERT(EFalse);
       
   236 			break;
       
   237 		}
       
   238 
       
   239 	if (arrayOfContacts != NULL)
       
   240 		{
       
   241 		CleanupStack::PopAndDestroy(arrayOfContacts);
       
   242 		}
       
   243 	}
       
   244 
       
   245 
       
   246 TInt CDatabaseTester::RunError(TInt aError)
       
   247 	{
       
   248 	// propagate error back to CTestConductor
       
   249 	iTestConductor->SetTestError(aError);
       
   250 
       
   251 	// some diagnostic info on where the test failed
       
   252 	switch (iCurrentTest)
       
   253 		{
       
   254 		case EAddContacts: test.Printf(_L("Failed at test step AddContacts (%i) error %i"), iCurrentTest, aError); break;
       
   255 		case EKillThreadFirstTime: test.Printf(_L("Failed at test step KillThreadFirstTime (%i) error %i"), iCurrentTest, aError); break;
       
   256 		case ERemoveContacts: test.Printf(_L("Failed at test step RemoveContacts (%i) error %i"), iCurrentTest, aError); break;
       
   257 		case EKillThreadSecondTime: test.Printf(_L("Failed at test step KillThreadSecondTime (%i) error %i"), iCurrentTest, aError); break;
       
   258 		case EAddMoreContacts: test.Printf(_L("Failed at test step AddMoreContacts (%i) error %i"), iCurrentTest, aError); break;
       
   259 		case EKillThreadThirdTime: test.Printf(_L("Failed at test step KillThreadThirdTime (%i) error %i"), iCurrentTest, aError); break;
       
   260 		case ERemoveMoreContacts: test.Printf(_L("Failed at test step RemoveMoreContacts (%i) error %i"), iCurrentTest, aError); break;
       
   261 		case EKillThreadForthTime: test.Printf(_L("Failed at test step KillThreadForthTime (%i) error %i"), iCurrentTest, aError); break;
       
   262 
       
   263 		case ENumTests: test.Printf(_L("Failed at test step NumTests (%i) error %i"), iCurrentTest, aError); break;
       
   264 
       
   265 		default: test.Printf(_L("Failed at test step %i error %i"), iCurrentTest, aError); break;
       
   266 		}
       
   267 
       
   268 	// stop test
       
   269 	CActiveScheduler::Stop();
       
   270 
       
   271 	return KErrNone;
       
   272 	}
       
   273 
       
   274 
       
   275 void CDatabaseTester::NextTest()
       
   276 	{
       
   277 	iCurrentTest = static_cast<TTest>( static_cast<TInt>(iCurrentTest) + 1 );
       
   278 	TRequestStatus *pS=&iStatus;
       
   279 	User::RequestComplete(pS,KErrNone);
       
   280 	SetActive();
       
   281 	}
       
   282 
       
   283 
       
   284 
       
   285 void CDatabaseTester::DoCancel()
       
   286 	{
       
   287 	}
       
   288 
       
   289 void CDatabaseTester::FindServerAndKillItL(TInt aDelay)
       
   290 	{
       
   291 	if (aDelay > 0) User::After(aDelay);
       
   292 	SetupProcessL();
       
   293 	OpenAndKillProcess();
       
   294 	WaitForServerToClose();
       
   295 	}
       
   296 
       
   297 void CDatabaseTester::AddContactsL()
       
   298 	{
       
   299 	test.Printf(_L("Adding "));
       
   300 	TInt ii;
       
   301 	for (ii=0;ii<KNumSmsContacts;ii++)
       
   302 		{
       
   303 		TInt bit=0;
       
   304 		bit |= CContactDatabase::ESmsable;
       
   305 		iRandomGenerator->AddTypicalContactForFilterL(bit);
       
   306 		iTotalContacts++;
       
   307 		}
       
   308 	for (ii=0;ii<KNumWorkEmailContacts;ii++)
       
   309 		{
       
   310 		TInt bit=0;
       
   311 		bit |= CContactDatabase::EMailable;
       
   312 		iRandomGenerator->AddTypicalContactForFilterL(bit);
       
   313 		iTotalContacts++;
       
   314 		}
       
   315 	for (ii=0;ii<KNumHomeEmailContacts;ii++)
       
   316 		{
       
   317 		TInt bit=0;
       
   318 		bit |= CContactDatabase::EMailable;
       
   319 		bit |= CContactDatabase::EHome;
       
   320 		iRandomGenerator->AddTypicalContactForFilterL(bit);
       
   321 		iTotalContacts++;
       
   322 		}
       
   323 	for (ii=0;ii<KNumLandlineContacts;ii++)
       
   324 		{
       
   325 		TInt bit=0;
       
   326 		bit |= CContactDatabase::ELandLine;
       
   327 		iRandomGenerator->AddTypicalContactForFilterL(bit);
       
   328 		iTotalContacts++;
       
   329 		}
       
   330 	for (ii=0;ii<KNumFaxContacts;ii++)
       
   331 		{
       
   332 		TInt bit=0;
       
   333 		bit |= CContactDatabase::EFaxable;
       
   334 		iRandomGenerator->AddTypicalContactForFilterL(bit);
       
   335 		iTotalContacts++;
       
   336 		}
       
   337 	}
       
   338 
       
   339 void CDatabaseTester::OpenAndKillProcess()
       
   340 	{
       
   341 	iLog.LogLine(_L("==== Open and Kill Process"));
       
   342 	/*TInt retVal = */iProcessOrThread.iProcess.iExecution->Open(iProcessOrThreadFullName);
       
   343 	iProcessOrThread.iProcess.iExecution->Kill(KErrNone);
       
   344 	}
       
   345 
       
   346 void CDatabaseTester::OpenAndKillThread()
       
   347 	{
       
   348 	iLog.LogLine(_L("==== Open and Kill Thread"));
       
   349 	iProcessOrThread.iThread.iExecution->Open(iProcessOrThreadFullName);
       
   350 	iProcessOrThread.iThread.iExecution->Kill(KErrNone);
       
   351 	}
       
   352 
       
   353 void CDatabaseTester::WaitForServerToClose()
       
   354 	{
       
   355 	const TInt KRetryPeriod(100000);
       
   356 	const TInt KMaxRetries(50);
       
   357 	
       
   358 	TInt attempts = 0;
       
   359 	do
       
   360 		{
       
   361 		// Attempt to find the server process
       
   362 		TRAPD(err, SetupProcessL());
       
   363 		if (err == KErrNotFound)
       
   364 			{
       
   365 			// Server not found - it has closed
       
   366 			return;
       
   367 			}
       
   368 		
       
   369 		
       
   370 		// Not found, wait then try again
       
   371 		User::After(KRetryPeriod);
       
   372 		
       
   373 		_LIT(KRetryReportFormat, "Close server attempt %d failed\n");
       
   374 		test.Printf(KRetryReportFormat, attempts + 1);
       
   375 		++attempts;
       
   376 		
       
   377 		} while (attempts < KMaxRetries);
       
   378 	
       
   379 	// Failed to close contacts server
       
   380 	_LIT(KServerCloseFailed, "Warning: Unable to verify contacts server has closed\n");
       
   381 	test.Printf(KServerCloseFailed);
       
   382 	}
       
   383 
       
   384 void CDatabaseTester::SetupThreadL()
       
   385 	{
       
   386 	DeleteThread();
       
   387 	iProcessOrThread.iThread.iFind = new (ELeave) TFindThread(_L("CntLockServer*"));
       
   388 	iProcessOrThread.iThread.iExecution = new (ELeave) RThread();
       
   389 	iProcessOrThread.iThread.iFind->Next(iProcessOrThreadFullName);
       
   390 	}
       
   391 
       
   392 void CDatabaseTester::SetupProcessL()
       
   393 	{
       
   394 	DeleteProcess();
       
   395 	iProcessOrThread.iProcess.iFind = new (ELeave) TFindProcess(_L("CNTSRV*"));
       
   396 	iProcessOrThread.iProcess.iExecution = new (ELeave) RProcess();
       
   397 	User::LeaveIfError(iProcessOrThread.iProcess.iFind->Next(iProcessOrThreadFullName));
       
   398 	}
       
   399 
       
   400 void CDatabaseTester::DeleteThread()
       
   401 	{
       
   402 	delete iProcessOrThread.iThread.iFind;
       
   403 	if (iProcessOrThread.iThread.iExecution != NULL) iProcessOrThread.iThread.iExecution->Close();
       
   404 	delete iProcessOrThread.iThread.iExecution;
       
   405 
       
   406 	iProcessOrThread.iThread.iFind = NULL;
       
   407 	iProcessOrThread.iThread.iExecution = NULL;
       
   408 	}
       
   409 
       
   410 void CDatabaseTester::DeleteProcess()
       
   411 	{
       
   412 	delete iProcessOrThread.iProcess.iFind;
       
   413 	if (iProcessOrThread.iProcess.iExecution != NULL) iProcessOrThread.iProcess.iExecution->Close();
       
   414 	delete iProcessOrThread.iProcess.iExecution;
       
   415 
       
   416 	iProcessOrThread.iProcess.iFind = NULL;
       
   417 	iProcessOrThread.iProcess.iExecution = NULL;
       
   418 	}
       
   419 
       
   420 void CDatabaseTester::AddContactL(TInt aBitwiseFilterType)
       
   421 	{
       
   422 	test.Printf(_L("Adding Contact"));
       
   423 	iRandomGenerator->AddTypicalContactForFilterL(aBitwiseFilterType);
       
   424 	iTotalContacts++;
       
   425 	}
       
   426 
       
   427 //
       
   428 // Main.
       
   429 //
       
   430 
       
   431 /**
       
   432 
       
   433 @SYMTestCaseID     PIM-T-DATABASE-KILLER-0001
       
   434 
       
   435 */
       
   436 
       
   437 GLDEF_C TInt E32Main()
       
   438 	{
       
   439 	TInt error = KErrNone;
       
   440 	__UHEAP_MARK;
       
   441 	CActiveScheduler* scheduler=new CActiveScheduler;
       
   442 	if (scheduler)
       
   443 		{
       
   444 		CActiveScheduler::Install(scheduler);
       
   445 		CTrapCleanup* cleanup=CTrapCleanup::New();
       
   446 		if (cleanup)
       
   447 			{
       
   448 			CTestConductor* testConductor=NULL;
       
   449 			// NEWL parameters :
       
   450 			//  EFalse - kill the server 3 times
       
   451 			//  ETrue  - kill the server 4 times
       
   452 			//
       
   453 			// - The client should keep track of the number of times the server is restarted.
       
   454 			// - restarting the server more than 3 times should result in the client being paniced.
       
   455 			// - this has been tested, and the ability to test for this has been included for completeness.
       
   456 			test.Start(KTestName);
       
   457 
       
   458 			TRAP(error,testConductor=CTestConductor::NewL(EFalse));
       
   459 			RDebug::Print(_L("T_database_killer test completed \n"));
       
   460 			test(error == KErrNone);
       
   461 			test.End();
       
   462 			delete testConductor;
       
   463 			test.Close();
       
   464 			delete cleanup;
       
   465 			}
       
   466 		delete scheduler;
       
   467 		}
       
   468 	__UHEAP_MARKEND;
       
   469 	return KErrNone;
       
   470     }