phonebookengines/contactsmodel/tsrc/t_cnt_backup.cpp
changeset 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2004-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 <e32test.h>
       
    17 #include <f32file.h>
       
    18 #include <s32file.h>
       
    19 #include <cntdb.h>
       
    20 #include <cntitem.h>
       
    21 #include <cntfield.h>
       
    22 #include <cntfldst.h>
       
    23 #include <coreappstest/testserver.h>
       
    24 
       
    25 #include <connect/sbeclient.h>
       
    26 
       
    27 #include "T_UTILS.H"
       
    28 #include "t_utils2.h"
       
    29 #include "t_cnt_backup.h"
       
    30 
       
    31 
       
    32 // cleanup version of test macro
       
    33 LOCAL_C void TestCondition(TBool aCondition, TInt aLineNumber);
       
    34 #define TEST_CONDITION(x) TestCondition(x, __LINE__)
       
    35 
       
    36 
       
    37 // test databases to create, for use by Contacts test clients
       
    38 _LIT(KBackupDatabaseFile1,"c:BackupTest1.cdb");
       
    39 _LIT(KBackupDatabaseFile2,"c:BackupTest2.cdb");
       
    40 _LIT(KBackupDatabaseFile3,"c:BackupTest3.cdb"); 
       
    41 
       
    42 
       
    43 // real location of Contacts db files
       
    44 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
    45 _LIT(KBackupDatabaseFileFullPath1,"c:\\private\\10003A73\\BackupTest1.cdb");
       
    46 _LIT(KBackupDatabaseFileFullPath2,"c:\\private\\10003A73\\BackupTest2.cdb");
       
    47 _LIT(KBackupDatabaseFileFullPath3,"c:\\private\\10003A73\\BackupTest3.cdb"); 
       
    48 _LIT(KBackupDestinationDatabasePath1,"c:\\t_cnt_backup\\BackupTest1.cdb");
       
    49 _LIT(KBackupDestinationDatabasePath2,"c:\\t_cnt_backup\\BackupTest2.cdb");
       
    50 _LIT(KBackupDestinationDatabasePath3,"c:\\t_cnt_backup\\BackupTest3.cdb"); 
       
    51 _LIT(KContactsBackupRegistrationDefaultDbFileFullPath, "c:\\private\\10003A73\\backup_registrationcontacts_cdb.xml");
       
    52 #else
       
    53 _LIT(KBackupDatabaseFileFullPath1,"c:\\private\\10003A73\\SQLite__BackupTest1.cdb");
       
    54 _LIT(KBackupDatabaseFileFullPath2,"c:\\private\\10003A73\\SQLite__BackupTest2.cdb");
       
    55 _LIT(KBackupDatabaseFileFullPath3,"c:\\private\\10003A73\\SQLite__BackupTest3.cdb"); 
       
    56 _LIT(KBackupDestinationDatabasePath1,"c:\\t_cnt_backup\\SQLite__BackupTest1.cdb");
       
    57 _LIT(KBackupDestinationDatabasePath2,"c:\\t_cnt_backup\\SQLite__BackupTest2.cdb");
       
    58 _LIT(KBackupDestinationDatabasePath3,"c:\\t_cnt_backup\\SQLite__BackupTest3.cdb"); 
       
    59 _LIT(KContactsBackupRegistrationDefaultDbFileFullPath, "c:\\private\\10003A73\\backup_registrationSQLite__contacts_cdb.xml");
       
    60 #endif
       
    61 
       
    62 
       
    63 // Contact model backup files
       
    64 _LIT(KContactsBackupRegistrationIniFileFullPath, "c:\\private\\10003A73\\backup_registration.xml");
       
    65 _LIT(KContactsIniBackupFileFullPath, "c:\\private\\10003A73\\Cntmodel.ini");
       
    66 _LIT(KBackupDestinationDirectory, "c:\\t_cnt_backup\\");
       
    67 _LIT(KBackupDestinationContactsIniPath,"c:\\t_cnt_backup\\Cntmodel.ini");
       
    68 _LIT(KRestoreDamagedDatabasePath,"z:\\t_cnt_backup_Corrupted_100065FF.cdb");
       
    69 
       
    70 // delays between Backup/Restoretest steps, to allow notifications to propagate to all components
       
    71 static const TInt KWaitForBackupNotification(15000000);
       
    72 static const TInt KWaitForClientToSettle(2000000);
       
    73 static const TInt KWaitForSystemToSettle(20000000);
       
    74 
       
    75 
       
    76 // Test databases have 10 contacts created, various contact Ids are used for different purposes
       
    77 static const TInt KNumContactsInDatabase= 10;
       
    78 static const TContactItemId KOpenContactId1 = 1;
       
    79 static const TContactItemId KOpenContactId2 = 2;
       
    80 static const TContactItemId KOpenContactId3 = 3;
       
    81 static const TContactItemId KSpeedDialContactId = 4;
       
    82 static const TContactItemId KReadContactId = 5;
       
    83 static const TInt KSpeedDialPositionOne = 1;
       
    84 
       
    85 
       
    86 _LIT(KAsyncFindMatch,"a");
       
    87 
       
    88 
       
    89 // global data for unit test
       
    90 _LIT(KTestName, "t_cnt_backup");
       
    91 static RTest test(KTestName);
       
    92 static RFs TheFsSession;
       
    93 
       
    94 
       
    95 /*
       
    96  Class CBackupTestConductor drives the whole set of tests
       
    97  1. Check the Backup Registration file is present
       
    98  2. Check the Secure Backup Engine has processed it as we expect
       
    99  3. ... various Backup / Restore scenarios for contacts clients
       
   100  */
       
   101 
       
   102 CBackupTestConductor* CBackupTestConductor::NewL()
       
   103 	{
       
   104 	CBackupTestConductor* self = new (ELeave) CBackupTestConductor();
       
   105 	CleanupStack::PushL(self);
       
   106 	self->ConstructL();
       
   107 	CleanupStack::Pop(self);
       
   108 	return self;
       
   109 	}
       
   110 
       
   111 CBackupTestConductor::~CBackupTestConductor()
       
   112 	{
       
   113 	// ensure test databases are deleted
       
   114 	CBackupTestConductor::DeleteDatabases();
       
   115 
       
   116 	// cleanup backup files
       
   117 	TheFsSession.Delete(KBackupDestinationContactsIniPath);
       
   118 	TheFsSession.Delete(KBackupDestinationDatabasePath1);
       
   119 	TheFsSession.Delete(KBackupDestinationDatabasePath2);
       
   120 	TheFsSession.Delete(KBackupDestinationDatabasePath3);
       
   121 	TheFsSession.RmDir(KBackupDestinationDirectory);
       
   122 	}
       
   123 
       
   124 
       
   125 CBackupTestConductor::CBackupTestConductor()
       
   126 	{
       
   127 	}
       
   128 
       
   129 
       
   130 void CBackupTestConductor::ConstructL()
       
   131 	{
       
   132 	const TInt err = TheFsSession.MkDirAll(KBackupDestinationDirectory);
       
   133 	if ((err != KErrNone) && (err != KErrAlreadyExists))
       
   134 		{
       
   135 		test.Printf(_L("Unable to create the directory %S\n"), &KBackupDestinationDirectory);
       
   136 		User::Leave(err);
       
   137 		}
       
   138 	}
       
   139 
       
   140 
       
   141 void CBackupTestConductor::RunTestsL()
       
   142 	{
       
   143 	// need databases for B&R testing
       
   144 	CreateDatabasesL();
       
   145 	// 1.x Check Backup Registration files
       
   146 	test.Start(_L("Backup registration files exist"));
       
   147 	TEST_CONDITION(CheckXMLBackupRegistrationExistsL());
       
   148 	test.End();
       
   149 
       
   150 	
       
   151 	// framework that runs a Backup & Restore test cycle
       
   152 	CBackupTestFramework* backupFramework = CBackupTestFramework::NewLC();
       
   153 	CleanupStack::Check(backupFramework);
       
   154 
       
   155 	// 2. ... run all Backup / Restore test situations
       
   156 	backupFramework->DoBackupRestoreTestsL();
       
   157 
       
   158 	// delete framework
       
   159 	CleanupStack::PopAndDestroy(backupFramework);
       
   160 	}
       
   161 
       
   162 
       
   163 // demonstrates that database is populated correctly
       
   164 #ifdef _DEBUG
       
   165 void CBackupTestConductor::ShowFieldsL(CContactDatabase* aDb)
       
   166 #else
       
   167 void CBackupTestConductor::ShowFieldsL(CContactDatabase* /*aDb*/)
       
   168 #endif
       
   169 	{
       
   170 #if _DEBUG
       
   171 	CContactItem* contact = aDb->ReadContactLC(iFirstContactId);
       
   172 	
       
   173 	CContactItemFieldSet& fields = contact->CardFields();
       
   174 	const TInt fieldCount = fields.Count();
       
   175 
       
   176 	for (TInt i = 0; i < fieldCount; i++)
       
   177 		{
       
   178 		CContactItemField &field = fields[i];
       
   179 		if (field.StorageType() == KStorageTypeText)
       
   180 			{
       
   181 			TPtrC fieldText = field.TextStorage()->Text();
       
   182 			TPtrC labelText = field.Label();
       
   183 		    test.Printf(_L("--Field(%d) %S: %S \n\r"), i, &labelText, &fieldText);
       
   184 			}
       
   185 		}
       
   186 	CleanupStack::PopAndDestroy(contact);	
       
   187 #endif
       
   188 	}
       
   189 
       
   190 
       
   191 /*
       
   192  Populate the database - add TEN contacts, based on the default template,
       
   193  setting FOUR fields. (Given name, Family name, phone number + Company name.)
       
   194  Set the Company Name field to be the same as the name of the database.
       
   195  
       
   196  @param aDb the CContactDatabase instance to add contacts to.
       
   197  @param aFileName Database file name to put in the Company name field
       
   198  */
       
   199 void CBackupTestConductor::PopulateDatabaseL(CContactDatabase* aDb,const TDesC& aFileName)
       
   200 	{
       
   201 	test.Printf(_L("Populating database %S\n"), &aFileName);
       
   202 	TInt index = 0;
       
   203 	TBool allDone = EFalse;
       
   204 
       
   205 	const TContactItemId templateId = aDb->TemplateId();
       
   206 	CContactItem* templateCard = aDb->ReadContactLC(templateId);
       
   207 
       
   208 	do
       
   209 	{
       
   210 		CContactCard* card = CContactCard::NewLC(templateCard);
       
   211 
       
   212 		CContactItemFieldSet& cardFields = card->CardFields();
       
   213 
       
   214 		TInt posLastName = cardFields.Find(KUidContactFieldGivenName);
       
   215 		TInt posFirstName = cardFields.Find(KUidContactFieldFamilyName);
       
   216 
       
   217 
       
   218 		switch (index)
       
   219 			{ // (famous writers on computer topics)
       
   220 			case 0:
       
   221 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test1"));
       
   222 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name1"));
       
   223 				break;
       
   224 
       
   225 			case 1:
       
   226 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test2"));
       
   227 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name2"));
       
   228 				break;
       
   229 
       
   230 			case 2:
       
   231 				cardFields[posLastName].TextStorage()->SetTextL(_L("Thompson10"));
       
   232 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name3"));
       
   233 				break;
       
   234 
       
   235 			case 3:
       
   236 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test3"));
       
   237 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name4"));
       
   238 				break;
       
   239 
       
   240 			case 4:
       
   241 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test4"));
       
   242 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name5"));
       
   243 				break;
       
   244 
       
   245 			case 5:
       
   246 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test5"));
       
   247 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name6"));
       
   248 				break;
       
   249 
       
   250 			case 6:
       
   251 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test6"));
       
   252 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name7"));
       
   253 				break;
       
   254 
       
   255 			case 7:
       
   256 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test7"));
       
   257 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name8"));
       
   258 				break;
       
   259 
       
   260 			case 8:
       
   261 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test8"));
       
   262 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name9"));
       
   263 				break;
       
   264 
       
   265 			case 9:
       
   266 				cardFields[posLastName].TextStorage()->SetTextL(_L("Test9"));
       
   267 				cardFields[posFirstName].TextStorage()->SetTextL(_L("Name10"));
       
   268 				// last contact
       
   269 				allDone = ETrue;
       
   270 				break;
       
   271 			}
       
   272 
       
   273 
       
   274 		// Set the telephone number
       
   275 		TInt posNumber = cardFields.Find(KUidContactFieldPhoneNumber);
       
   276 		if (posNumber != KErrNotFound)
       
   277 			{
       
   278 			cardFields[posNumber].SetMapping(KUidContactFieldVCardMapTEL);
       
   279 			cardFields[posNumber].TextStorage()->SetTextL(_L("1234"));
       
   280 			}
       
   281 
       
   282 
       
   283 		// set database file name as company name
       
   284 		TInt posCompanyName = cardFields.Find(KUidContactFieldCompanyName);
       
   285 		if (posCompanyName != KErrNotFound)
       
   286 			{
       
   287 			cardFields[posCompanyName].TextStorage()->SetTextL(aFileName);
       
   288 			}
       
   289 
       
   290 
       
   291 		iLastContactId = aDb->AddNewContactL(*card);
       
   292 		if (index == 0)
       
   293 			{
       
   294 			iFirstContactId = iLastContactId;
       
   295 			}
       
   296 
       
   297 		CleanupStack::PopAndDestroy(card);
       
   298 		++index;
       
   299 		
       
   300 		test.Printf(_L("."));
       
   301 		}
       
   302 		while (!allDone);
       
   303 
       
   304 	CleanupStack::PopAndDestroy(templateCard);
       
   305 
       
   306 	ShowFieldsL(aDb);
       
   307 	}
       
   308 
       
   309 // create THREE empty contacts databases then populate each
       
   310 void CBackupTestConductor::CreateDatabasesL()
       
   311 	{
       
   312 	// Create & close databases
       
   313 	CContactDatabase* db;
       
   314 	// Need to create the default data base if it does not exist to enusre
       
   315 	// prescence of backup registration file.
       
   316 	if (!CContactDatabase::DefaultContactDatabaseExistsL())
       
   317 		{
       
   318 		db = CContactDatabase::CreateL();
       
   319 		delete db;		
       
   320 		}
       
   321 		
       
   322 	test.Printf(_L("Creating database %S\n"), &KBackupDatabaseFile1);
       
   323 	db = CContactDatabase::ReplaceL(KBackupDatabaseFile1);
       
   324 	PopulateDatabaseL(db, KBackupDatabaseFile1);
       
   325 	delete db;
       
   326 	
       
   327 	test.Printf(_L("Creating database %S\n"), &KBackupDatabaseFile2);
       
   328 	db = CContactDatabase::ReplaceL(KBackupDatabaseFile2);
       
   329 	PopulateDatabaseL(db, KBackupDatabaseFile2);
       
   330 	delete db;
       
   331 	
       
   332 	test.Printf(_L("Creating database %S\n"), &KBackupDatabaseFile3);
       
   333 	db = CContactDatabase::ReplaceL(KBackupDatabaseFile3);
       
   334 	PopulateDatabaseL(db, KBackupDatabaseFile3);
       
   335 	delete db;
       
   336 	}
       
   337 
       
   338 
       
   339 void CBackupTestConductor::DeleteDatabases()
       
   340 	{
       
   341 	// Delete databases
       
   342 	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KBackupDatabaseFile1));
       
   343 	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KBackupDatabaseFile2));
       
   344 	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KBackupDatabaseFile3));
       
   345 	}
       
   346 
       
   347 
       
   348 /*
       
   349  Check that the Contact Model's Backup Registration files are present and > 0 bytes length
       
   350  */
       
   351 TBool CBackupTestConductor::CheckXMLBackupRegistrationExistsL()
       
   352 	{
       
   353 	// Check the Contacts private area for the expected registration files
       
   354 	TEntry entry;
       
   355 
       
   356 	// See if initialisation registration file is in the directory on drive Z:
       
   357 	TInt error = TheFsSession.Entry(KContactsBackupRegistrationIniFileFullPath, entry);
       
   358 
       
   359 	if (error == KErrNotFound)
       
   360 		{
       
   361 		return EFalse;
       
   362 		}
       
   363 		
       
   364 	User::LeaveIfError(error);
       
   365 
       
   366 	if(entry.iSize == 0)
       
   367 		{
       
   368 		// file is present but is empty
       
   369 		User::Leave(KErrCorrupt);
       
   370 		}
       
   371 
       
   372 	// See if default database registration file is in the directory on drive Z:
       
   373 	error = TheFsSession.Entry(KContactsBackupRegistrationDefaultDbFileFullPath, entry);
       
   374 
       
   375 	if (error == KErrNotFound)
       
   376 		{
       
   377 		return EFalse;
       
   378 		}
       
   379 		
       
   380 	User::LeaveIfError(error);
       
   381 
       
   382 	if(entry.iSize == 0)
       
   383 		{
       
   384 		// file is present but is empty
       
   385 		User::Leave(KErrCorrupt);
       
   386 		}
       
   387 
       
   388 	// Found the registration files
       
   389 	return ETrue;
       
   390 	}
       
   391 			
       
   392 
       
   393 //
       
   394 
       
   395 /*
       
   396  Framework for simulating Contact Model clients
       
   397  
       
   398  @param aDatabaseName database file name to open
       
   399  @param aObserver Observer in Backup / Restore test framework to count Backup and Restore notifications
       
   400  @param aContactViewObserver Observer in Backup / Restore test framework to count View Ready and Unavailable notifications
       
   401  */
       
   402 CContactsClient* CContactsClient::NewLC(const TDesC& aDatabaseName, MBackupRestoreObserver* aObserver, MContactViewObserver& aContactViewObserver, TBool aCloseForBackup)
       
   403 	{
       
   404 	CContactsClient* self = new (ELeave) CContactsClient(aObserver, aContactViewObserver, aCloseForBackup);
       
   405 	CleanupStack::PushL(self);
       
   406 	self->ConstructL(aDatabaseName);
       
   407 	return self;
       
   408 	}
       
   409 	
       
   410 	
       
   411 CContactsClient::CContactsClient(MBackupRestoreObserver* aObserver, MContactViewObserver& aContactViewObserver, TBool aCloseForBackup)
       
   412 	: CActive(EPriorityStandard), iObserver(aObserver), iContactViewObserver(aContactViewObserver), iDatabaseIsOpen(EFalse), iCloseForBackup(aCloseForBackup), iOpenContacts(4)
       
   413 	{
       
   414 	CActiveScheduler::Add(this);
       
   415 	}
       
   416 
       
   417 CContactsClient::~CContactsClient()
       
   418 	{
       
   419 	Cancel();
       
   420 	CloseViews();
       
   421 	CloseDatabase();
       
   422 	iOpenContacts.ResetAndDestroy();
       
   423 
       
   424 	delete iDbName;
       
   425 	}
       
   426 
       
   427 
       
   428 void CContactsClient::StartActiveClose()
       
   429 	{
       
   430 	// start active object to close contact/view/database
       
   431 	// required as these can't be closed directly from the event handlers
       
   432 	if (!IsActive() && iDatabaseIsOpen)
       
   433 		{
       
   434 		TRequestStatus* pS = &iStatus;
       
   435 		User::RequestComplete(pS,0);
       
   436 		SetActive();
       
   437 		}
       
   438 	}
       
   439 	
       
   440 void CContactsClient::DoCancel()
       
   441 	{
       
   442 	}
       
   443 	
       
   444 
       
   445 void CContactsClient::RunL()
       
   446 	{
       
   447 	if (iStatus.Int() != KErrCancel)
       
   448 		{
       
   449 		CloseDatabase();
       
   450 		}
       
   451 	}
       
   452 	
       
   453 void CContactsClient::CloseDatabase()
       
   454 	{
       
   455 	CloseContacts();
       
   456 	CloseViews();
       
   457 	iDatabaseIsOpen = EFalse;
       
   458 
       
   459 	if (iIdleFinder)
       
   460 		{
       
   461 		delete iIdleFinder;
       
   462 		iIdleFinder = NULL;
       
   463 		}
       
   464 
       
   465 	if (iItemFieldDef)
       
   466 		{
       
   467 		delete iItemFieldDef;
       
   468 		iItemFieldDef = NULL;
       
   469 		}
       
   470 
       
   471 	if (iActiveCompress)
       
   472 		{
       
   473 		delete iActiveCompress;
       
   474 		iActiveCompress = NULL;
       
   475 		}
       
   476 
       
   477 	if (iActiveRecover)
       
   478 		{
       
   479 		delete iActiveRecover;
       
   480 		iActiveRecover = NULL;
       
   481 		}
       
   482 
       
   483 	delete iNotifier;
       
   484 	iNotifier = NULL;
       
   485 	delete iDb;
       
   486 	iDb = NULL;
       
   487 	}
       
   488 
       
   489 
       
   490 void CContactsClient::ConstructL(const TDesC& aDatabaseName)
       
   491 	{
       
   492 	// remember the database name
       
   493 	iDbName = aDatabaseName.AllocL();
       
   494 	// open it
       
   495 	OpenDatabaseL();
       
   496 	}
       
   497 
       
   498 
       
   499 void CContactsClient::OpenDatabaseL()
       
   500 	{
       
   501 	// can only open once
       
   502 	if (iDatabaseIsOpen)
       
   503 		{
       
   504 		User::Leave(KErrInUse);
       
   505 		}
       
   506 
       
   507 	test.Printf(_L("Open database %S\n"), iDbName);
       
   508 	iDb = CContactDatabase::OpenL(iDbName->Des());
       
   509 	iDatabaseIsOpen = ETrue;
       
   510 	iNotifier = CContactChangeNotifier::NewL(*iDb,this);
       
   511 	}
       
   512 
       
   513 
       
   514 const TDesC& CContactsClient::DatabaseName()
       
   515 	{
       
   516 	return *iDbName;
       
   517 	}
       
   518 
       
   519 
       
   520 TBool CContactsClient::IsOpen()
       
   521 	{
       
   522 	return iDatabaseIsOpen;
       
   523 	}
       
   524 
       
   525 TBool CContactsClient::IsCloseForBackup()
       
   526 	{
       
   527 	return iCloseForBackup;
       
   528 	}	
       
   529 
       
   530 void CContactsClient::OpenRemoteViewL()
       
   531 	{
       
   532 	if (iRemoteView != NULL)
       
   533 		{
       
   534 		User::Leave(KErrInUse);
       
   535 		}
       
   536 		
       
   537 	RContactViewSortOrder viewSortOrder;
       
   538 	
       
   539 	viewSortOrder.AppendL(KUidContactFieldFamilyName);
       
   540 	viewSortOrder.AppendL(KUidContactFieldGivenName);
       
   541 	viewSortOrder.AppendL(KUidContactFieldCompanyName);
       
   542 
       
   543 	iRemoteView = CContactRemoteView::NewL(iContactViewObserver, *iDb, viewSortOrder, EContactsOnly);
       
   544 
       
   545 	viewSortOrder.Close();
       
   546 	}
       
   547 
       
   548 
       
   549 void CContactsClient::OpenLocalViewL()
       
   550 	{
       
   551 	if (iLocalView != NULL)
       
   552 		{
       
   553 		User::Leave(KErrInUse);
       
   554 		}
       
   555 		
       
   556 	RContactViewSortOrder viewSortOrder;
       
   557 	
       
   558 	viewSortOrder.AppendL(KUidContactFieldFamilyName);
       
   559 	viewSortOrder.AppendL(KUidContactFieldGivenName);
       
   560 	viewSortOrder.AppendL(KUidContactFieldCompanyName);
       
   561 
       
   562 	iLocalView = CContactLocalView::NewL(iContactViewObserver, *iDb, viewSortOrder, EContactsOnly);
       
   563 
       
   564 	viewSortOrder.Close();
       
   565 	}
       
   566 
       
   567 
       
   568 void CContactsClient::CloseViews()
       
   569 	{
       
   570 	if (iLocalView)
       
   571 		{
       
   572 		iLocalView->Close(iContactViewObserver);
       
   573 		iLocalView = NULL;
       
   574 		}
       
   575 
       
   576 	if (iRemoteView)
       
   577 		{
       
   578 		iRemoteView->Close(iContactViewObserver);
       
   579 		iRemoteView = NULL;
       
   580 		}
       
   581 	}
       
   582 
       
   583 
       
   584 void CContactsClient::FindPhoneNumberL()
       
   585 	{
       
   586 	_LIT(KPhoneNumber, "1234");
       
   587 	const TInt KNumMatchDigit = 4;
       
   588 	CContactIdArray* array = iDb->MatchPhoneNumberL(KPhoneNumber, KNumMatchDigit);
       
   589 	CleanupStack::PushL(array);
       
   590 	TEST_CONDITION(array->Count() == 10);
       
   591 	CleanupStack::PopAndDestroy(array);
       
   592 	}
       
   593 
       
   594 
       
   595 // try to open a contact for edit
       
   596 void CContactsClient::OpenContactL(TContactItemId aContactId)
       
   597 	{
       
   598 	CContactItem* contact = iDb->OpenContactL(aContactId);
       
   599 	iOpenContacts.AppendL(contact);
       
   600 	}
       
   601 
       
   602 
       
   603 // try to commit an open contact
       
   604 void CContactsClient::CommitContactL(TContactItemId aContactId)
       
   605 	{
       
   606 	const TInt numOpenContacts = iOpenContacts.Count();
       
   607 	
       
   608 	for (TInt j = 0; j < numOpenContacts; ++j)
       
   609 		{
       
   610 		CContactItem* tempContact = iOpenContacts[j];
       
   611 		if (tempContact->Id() == aContactId)
       
   612 			{
       
   613 			iDb->CommitContactL(*tempContact);
       
   614 			iOpenContacts.Remove(j);
       
   615 			delete tempContact;
       
   616 			return;
       
   617 			}
       
   618 		}
       
   619 	// contact isn't in open list
       
   620 	TEST_CONDITION(EFalse);
       
   621 	}
       
   622 
       
   623 
       
   624 // close any open contacts
       
   625 void CContactsClient::CloseContacts()
       
   626 	{
       
   627 	while (iOpenContacts.Count())
       
   628 		{
       
   629 		CContactItem* tempContact = iOpenContacts[0];
       
   630 		TRAP_IGNORE(iDb->CloseContactL(tempContact->Id()));
       
   631 		iOpenContacts.Remove(0);
       
   632 		delete tempContact;
       
   633 		}
       
   634 	}
       
   635 
       
   636 // try to Read the template Contact from database
       
   637 // and also, check that one record exists with the Company Name field set 
       
   638 // to the clients database name 
       
   639 void CContactsClient::ReadContactL()
       
   640 	{
       
   641 	CContactItem* contact = iDb->ReadContactLC(KReadContactId);
       
   642 	CleanupStack::PopAndDestroy(contact);
       
   643 
       
   644 	// find records with the Company Name field set
       
   645 	CContactItemFieldDef* fieldDef = new(ELeave) CContactItemFieldDef;
       
   646 	CleanupStack::PushL(fieldDef);
       
   647 	TFieldType fieldtype = KUidContactFieldCompanyName;
       
   648 	fieldDef->AppendL(fieldtype);
       
   649 	CContactIdArray* matchList = iDb->FindLC(iDbName->Des(),fieldDef);
       
   650   
       
   651 	// all added contacts should have this field initialised
       
   652 	TInt matchCount = matchList->Count();
       
   653     TEST_CONDITION(matchCount == KNumContactsInDatabase);	
       
   654 
       
   655     CleanupStack::PopAndDestroy(2, fieldDef);
       
   656 	}
       
   657 
       
   658 
       
   659 void CContactsClient::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
       
   660 	{
       
   661 	/** Contacts DB about to be backed up  */
       
   662 	if (aEvent.iType == EContactDbObserverEventBackupBeginning)
       
   663 		{
       
   664 		iRestore = EFalse;
       
   665 		iObserver->ContactsBackupRestoreObserver(*this, iRestore, ETrue);
       
   666 		}
       
   667 	/** Contacts DB about to be restored */
       
   668 	else if (aEvent.iType == EContactDbObserverEventRestoreBeginning)
       
   669 		{
       
   670 		iRestore = ETrue;
       
   671 		iObserver->ContactsBackupRestoreObserver(*this, iRestore, ETrue);
       
   672 		}
       
   673 	/** The backup/restore operation has completed. */
       
   674 	else if (aEvent.iType == EContactDbObserverEventBackupRestoreCompleted)
       
   675 		{
       
   676 		iObserver->ContactsBackupRestoreObserver(*this, iRestore, EFalse);
       
   677 		}
       
   678 	/** The restore operation has completed but the database could not be opened. */
       
   679 	else if (aEvent.iType == EContactDbObserverEventRestoreBadDatabase)
       
   680 		{
       
   681 		iObserver->ContactsBackupRestoreObserver(*this, iRestore, EFalse);
       
   682 		}
       
   683 	}
       
   684 
       
   685 
       
   686 void CContactsClient::StartAsyncFindL()
       
   687 	{
       
   688 	if (!iItemFieldDef)
       
   689 		{
       
   690 		iItemFieldDef = new(ELeave) CContactItemFieldDef();
       
   691 		iItemFieldDef->AppendL(KUidContactFieldGivenName);
       
   692 		iItemFieldDef->AppendL(KUidContactFieldFamilyName);
       
   693 		iItemFieldDef->AppendL(KUidContactFieldCompanyName);
       
   694 		iItemFieldDef->AppendL(KUidContactFieldPhoneNumber);
       
   695 		iItemFieldDef->AppendL(KUidContactFieldFax);
       
   696 		iItemFieldDef->AppendL(KUidContactFieldEMail);
       
   697 		iItemFieldDef->AppendL(KUidContactFieldUrl);
       
   698 		iItemFieldDef->AppendL(KUidContactFieldAddress);
       
   699 		iItemFieldDef->AppendL(KUidContactFieldLocality);
       
   700 		iItemFieldDef->AppendL(KUidContactFieldRegion);
       
   701 		iItemFieldDef->AppendL(KUidContactFieldPostcode);
       
   702 		iItemFieldDef->AppendL(KUidContactFieldCountry);
       
   703 		iItemFieldDef->AppendL(KUidContactFieldNote);
       
   704 		}
       
   705 
       
   706 	iIdleFinder = iDb->FindAsyncL(KAsyncFindMatch, iItemFieldDef, this);
       
   707 	}
       
   708 
       
   709 
       
   710 void CContactsClient::StartActiveCompactL()
       
   711 	{
       
   712 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   713 	if (! iActiveCompress)
       
   714 		{
       
   715 		iActiveCompress = iDb->CreateCompressorLC();
       
   716 		CleanupStack::Pop();
       
   717 		}
       
   718 	iActiveCompress->SetObserver(this);
       
   719 #endif
       
   720 	}
       
   721 
       
   722 
       
   723 void CContactsClient::StartActiveRecoverL()
       
   724 	{
       
   725 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   726 	if (! iActiveRecover)
       
   727 		{
       
   728 		iActiveRecover = iDb->CreateRecoverLC();
       
   729 		CleanupStack::Pop();
       
   730 		}
       
   731 	iActiveRecover->SetObserver(this);
       
   732 #endif
       
   733 	}
       
   734 
       
   735 
       
   736 void CContactsClient::StartIdleSorterL(TRequestStatus& aStatus)
       
   737 	{
       
   738 	CArrayFix<CContactDatabase::TSortPref>* sortOrder = new(ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(3);
       
   739 	CleanupStack::PushL(sortOrder);
       
   740 
       
   741 	// prepare a sort order
       
   742 	sortOrder->AppendL(CContactDatabase::TSortPref(KUidContactFieldGivenName));
       
   743 	sortOrder->AppendL(CContactDatabase::TSortPref(KUidContactFieldFamilyName));
       
   744 	sortOrder->AppendL(CContactDatabase::TSortPref(KUidContactFieldCompanyName));
       
   745 
       
   746 	// starts Contacts Model Idle Sorter
       
   747 	iDb->SortAsyncL(sortOrder, aStatus);
       
   748 
       
   749 
       
   750 	// sortOrder ownership was passed to the Contact Model
       
   751 	CleanupStack::Pop(sortOrder);
       
   752 	}
       
   753 
       
   754 
       
   755 void CContactsClient::CompleteAsyncFindL()
       
   756 	{
       
   757 	// nothing to do Async Find completes in its own time
       
   758 	}
       
   759 
       
   760 
       
   761 void CContactsClient::CompleteActiveCompactL()
       
   762 	{
       
   763 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   764 	TInt step;
       
   765 	while(iActiveCompress->Step())
       
   766 		{
       
   767 		step = iActiveCompress->StepsTogo();
       
   768 		TEST_CONDITION(step > 0);
       
   769 		}
       
   770 	step = iActiveCompress->StepsTogo();
       
   771 	TEST_CONDITION(step == 0);
       
   772 	delete iActiveCompress;
       
   773 	iActiveCompress = NULL;
       
   774 #endif
       
   775 	}
       
   776 
       
   777 
       
   778 void CContactsClient::CompleteActiveRecoverL()
       
   779 	{
       
   780 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   781 	TInt step;
       
   782 	while(iActiveRecover->Step())
       
   783 		{
       
   784 		step = iActiveRecover->StepsTogo();
       
   785 		TEST_CONDITION(step > 0);
       
   786 		}
       
   787 	step = iActiveRecover->StepsTogo();
       
   788 	TEST_CONDITION(step == 0);
       
   789 	delete iActiveRecover;
       
   790 	iActiveRecover = NULL;
       
   791 #endif
       
   792 	}
       
   793 
       
   794 
       
   795 // class MContactUiCompactObserver
       
   796 void CContactsClient::Step(TInt /*aStep*/)
       
   797 	{
       
   798 	}
       
   799 
       
   800 
       
   801 void CContactsClient::HandleError(TInt /*aError*/)
       
   802 	{
       
   803 	}
       
   804 
       
   805 
       
   806 // class MIdleFindObserver
       
   807 void CContactsClient::IdleFindCallback()
       
   808 	{
       
   809 	}
       
   810 
       
   811 
       
   812 // Speed Dial Check, Remove and Set - for testing cntmodel.ini is Backed up and Restored correctly
       
   813 void CContactsClient::CheckSpeedDialSetL(TContactItemId aContactId, TInt aSpeedDialPosition)
       
   814 	{
       
   815 	HBufC* buf = HBufC::NewLC(100);	
       
   816 	TPtr fieldContents = buf->Des();
       
   817 	
       
   818 	TContactItemId id = iDb->GetSpeedDialFieldL(aSpeedDialPosition, fieldContents);
       
   819 	CleanupStack::PopAndDestroy(buf);
       
   820 
       
   821 	TEST_CONDITION(id == aContactId);
       
   822 	}
       
   823 
       
   824 
       
   825 void CContactsClient::RemoveSpeedDialL(TContactItemId aContactId, TInt aSpeedDialPosition)
       
   826 	{
       
   827 	iDb->RemoveSpeedDialFieldL(aContactId, aSpeedDialPosition);
       
   828 	}
       
   829 
       
   830 
       
   831 void CContactsClient::SetSpeedDialL(TContactItemId aContactId, TInt aSpeedDialPosition)
       
   832 	{
       
   833 	CContactItem* speedDialItem = iDb->OpenContactLX(aContactId);
       
   834 	CleanupStack::PushL(speedDialItem);
       
   835 	CContactItemFieldSet& cardFields = speedDialItem->CardFields();
       
   836 
       
   837 	TInt posNumber = cardFields.Find(KUidContactFieldPhoneNumber);
       
   838 
       
   839 	iDb->SetFieldAsSpeedDialL(*speedDialItem, posNumber, aSpeedDialPosition);
       
   840 	CleanupStack::PopAndDestroy(speedDialItem);
       
   841 	CleanupStack::Pop(); // Lock Record
       
   842 	}
       
   843 
       
   844 
       
   845 //
       
   846 // Backup and Restore Test Framework class
       
   847 //
       
   848 
       
   849 CBackupHelper* CBackupHelper::NewL(RPIMTestServer& aTestServer)
       
   850 	{
       
   851     CBackupHelper* self = new(ELeave) CBackupHelper(aTestServer);
       
   852 	CleanupStack::PushL(self);
       
   853 	self->ConstructL();
       
   854 	CleanupStack::Pop(self);
       
   855 	return self;
       
   856 	}
       
   857 	
       
   858 	
       
   859 CBackupHelper::CBackupHelper(RPIMTestServer& aTestServer) : 
       
   860     iTestServ (aTestServer)
       
   861 	{
       
   862 	}
       
   863 	
       
   864 	
       
   865 void CBackupHelper::ConstructL()
       
   866 	{
       
   867 	/* Initialise the drive list to empty and then get drive list data from
       
   868 	 File Server.
       
   869 	 Required before Backup and Restore testing
       
   870 	 */
       
   871 	iDriveList.FillZ();
       
   872 	User::LeaveIfError(TheFsSession.DriveList(iDriveList));
       
   873 
       
   874 	iBackupClient = conn::CSBEClient::NewL();
       
   875 	}
       
   876 
       
   877 
       
   878 CBackupHelper::~CBackupHelper()
       
   879 	{
       
   880 	delete iBackupClient;
       
   881 	}
       
   882 	
       
   883 	
       
   884 void CBackupHelper::StartBackupL(conn::TBURPartType aBURPartType, conn::TBackupIncType aBackupIncType)
       
   885 	{
       
   886 	/* Use the Secure Backup Engine API to "start" a backup.  Ultimate effect
       
   887 	 is to cause CContactsClient::HandleDatabaseEventL() to be called.
       
   888 	 This happens indirectly through CLockSrvServer::BackupBeginningL()/
       
   889 	 BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
   890 	 These methods are implementations for MBackupRestoreNotificatioObserver
       
   891 	 which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
   892 	 This P&S property is changed by calling the Secure Backup Engine API
       
   893 	 below.
       
   894 	 */	
       
   895 	iBackupClient->SetBURModeL(iDriveList, aBURPartType, aBackupIncType);
       
   896 	}
       
   897 
       
   898 
       
   899 void CBackupHelper::EndBackupL()
       
   900 	{
       
   901 	/* Use the Secure Backup Engine API to "end" a backup.  Ultimate effect
       
   902 	 is to cause CContactsClient::HandleDatabaseEventL() to be called.
       
   903 	 This happens indirectly through CLockSrvServer::BackupBeginningL()/
       
   904 	 BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
   905 	 These methods are implementations for MBackupRestoreNotificatioObserver
       
   906 	 which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
   907 	 This P&S property is changed by calling the Secure Backup Engine API
       
   908 	 below.
       
   909 	 */
       
   910 	iBackupClient->SetBURModeL(iDriveList, conn::EBURNormal, conn::ENoBackup);
       
   911 	}
       
   912 
       
   913 
       
   914 void CBackupHelper::StartRestoreL()
       
   915 	{
       
   916 	/* Use the Secure Backup Engine API to "start" a restore.  Ultimate effect
       
   917 	 is to cause CContactsClient::HandleDatabaseEventL() to be called.
       
   918 	 This happens indirectly through CLockSrvServer::BackupBeginningL()/
       
   919 	 BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
   920 	 These methods are implementations for MBackupRestoreNotificatioObserver
       
   921 	 which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
   922 	 This P&S property is changed by calling the Secure Backup Engine API
       
   923 	 below.
       
   924 	 */
       
   925 	iBackupClient->SetBURModeL(iDriveList, conn::EBURRestoreFull, conn::EBackupBase);
       
   926 	}
       
   927 
       
   928 
       
   929 void CBackupHelper::EndRestoreL()
       
   930 	{
       
   931 	/* Use the Secure Backup Engine API to "end" a restore.  Ultimate effect
       
   932 	 is to cause CContactsClient::HandleDatabaseEventL() to be called.
       
   933 	 This happens indirectly through CLockSrvServer::BackupBeginningL()/
       
   934 	 BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
   935 	 These methods are implementations for MBackupRestoreNotificatioObserver
       
   936 	 which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
   937 	 This P&S property is changed by calling the Secure Backup Engine API
       
   938 	 below.
       
   939 	 */
       
   940 	iBackupClient->SetBURModeL(iDriveList, conn::EBURNormal, conn::ENoBackup);
       
   941 	}
       
   942 
       
   943 
       
   944 
       
   945 
       
   946 //
       
   947 // Simulate Backup of .ini file. Indicating if the operation was a success.
       
   948 //
       
   949 TBool CBackupHelper::SimulateIniBackup()
       
   950 	{
       
   951     // Backup CntModel.ini file.
       
   952     TRAPD(error, iTestServ.CopyFileL(KContactsIniBackupFileFullPath,
       
   953         KBackupDestinationContactsIniPath))
       
   954 	
       
   955 	if (error != KErrNone)
       
   956 		{
       
   957 		test.Printf(_L("Copy failed with error %i"), error);
       
   958 		return EFalse;
       
   959 		}
       
   960 	return ETrue;
       
   961 	}
       
   962 
       
   963 
       
   964 //
       
   965 // Simulate Backup of database(s). Indicating if the operation was a success.
       
   966 //
       
   967 TBool CBackupHelper::SimulateDbBackup()
       
   968 	{
       
   969     // backup databases
       
   970     TRAPD(error, iTestServ.CopyFileL(KBackupDatabaseFileFullPath1, KBackupDestinationDatabasePath1))
       
   971 
       
   972 	if (error == KErrNone)
       
   973 		{
       
   974         TRAP(error, iTestServ.CopyFileL(KBackupDatabaseFileFullPath2, KBackupDestinationDatabasePath2))
       
   975 		}
       
   976 
       
   977 	if (error == KErrNone)
       
   978 		{
       
   979         TRAP(error, iTestServ.CopyFileL(KBackupDatabaseFileFullPath3, KBackupDestinationDatabasePath3))
       
   980 		}
       
   981 
       
   982 	if (error != KErrNone)
       
   983 		{
       
   984 		test.Printf(_L("Copy failed with error %i"), error);
       
   985 		return EFalse;
       
   986 		}
       
   987 
       
   988 	return ETrue;
       
   989 	}
       
   990 
       
   991 
       
   992 //
       
   993 // Simulate restore of .ini file. Indicating if the operation was a success.
       
   994 //
       
   995 TBool CBackupHelper::SimulateIniRestore()
       
   996 	{
       
   997 	// Restore CntModel.ini file.
       
   998     TRAPD(error, iTestServ.CopyFileL(KBackupDestinationContactsIniPath, KContactsIniBackupFileFullPath))
       
   999 
       
  1000 	if (error != KErrNone)
       
  1001 		{
       
  1002 		test.Printf(_L("Copy failed with error %i"), error);
       
  1003 		return EFalse;
       
  1004 		}
       
  1005 	return ETrue;
       
  1006 	}
       
  1007 
       
  1008 
       
  1009 //
       
  1010 // Simulate restore of database(s). Indicating if the operation was a success.
       
  1011 //
       
  1012 TBool CBackupHelper::SimulateDbRestore()
       
  1013 	{
       
  1014 	// restore databases
       
  1015     TRAPD(error, iTestServ.CopyFileL(KBackupDestinationDatabasePath1, KBackupDatabaseFileFullPath1))
       
  1016 
       
  1017     if (error == KErrNone)
       
  1018 		{
       
  1019         TRAP(error, iTestServ.CopyFileL(KBackupDestinationDatabasePath2, KBackupDatabaseFileFullPath2))
       
  1020 		}
       
  1021 
       
  1022 	if (error == KErrNone)
       
  1023 		{
       
  1024         TRAP(error, iTestServ.CopyFileL(KBackupDestinationDatabasePath3, KBackupDatabaseFileFullPath3))
       
  1025 		}
       
  1026 
       
  1027 	if (error != KErrNone)
       
  1028 		{
       
  1029 		test.Printf(_L("Copy failed with error %i"), error);
       
  1030 		return EFalse;
       
  1031 		}
       
  1032 
       
  1033 	return ETrue;
       
  1034 	}
       
  1035 
       
  1036 //
       
  1037 // Simulate restoring a corrupted database.
       
  1038 //
       
  1039 TBool CBackupHelper::SimulateDamagedDbRestore()
       
  1040     {
       
  1041     // Restore first one from a damaged and the rest from a good copy.		
       
  1042     TRAPD(error, iTestServ.CopyFileL(KRestoreDamagedDatabasePath, KBackupDatabaseFileFullPath1))	
       
  1043 
       
  1044     if (error == KErrNone)
       
  1045         {
       
  1046         TRAP(error, iTestServ.CopyFileL(KBackupDestinationDatabasePath2, KBackupDatabaseFileFullPath2))
       
  1047         }
       
  1048 
       
  1049     if (error == KErrNone)
       
  1050         {
       
  1051         TRAP(error, iTestServ.CopyFileL(KBackupDestinationDatabasePath3, KBackupDatabaseFileFullPath3))
       
  1052         }
       
  1053 	
       
  1054     if (error != KErrNone)
       
  1055         {
       
  1056         test.Printf(_L("Copy failed with error %i"), error);
       
  1057         return EFalse;
       
  1058         }
       
  1059 
       
  1060     return ETrue;
       
  1061     }
       
  1062 
       
  1063 //
       
  1064 // Backup and Restore Test Framework class
       
  1065 //
       
  1066 static const CBackupTestFramework::TBackupRestoreTestCase BackupTestCases[] =
       
  1067 	{
       
  1068 		/* iTestTitle, iNumDatabases, iClientsPerDatabase, iActivity, iCloseForBackup, iExpectedBackupSuccess */
       
  1069 
       
  1070 		/*
       
  1071 		 Test client activities that DO NOT BLOCK Backup / Restore
       
  1072 		 Backup and Restore should always succeed, tests are in pairs:
       
  1073 		 1. does not respond to Backup
       
  1074 		 2. closes all resources when Backup or Restore begin
       
  1075 		*/
       
  1076 
       
  1077 		// Single client per database - Backup and Restore should always succeed
       
  1078 		{
       
  1079 		(TText*) L"Clients that keep CContactDatabase Open",
       
  1080 		3, 1, CBackupTestFramework::EIdleClient, ETrue, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1081 		},
       
  1082 		{
       
  1083 		(TText*) L"Clients that close CContactDatabase",
       
  1084 		3, 1, CBackupTestFramework::EIdleClient, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1085     	},
       
  1086 
       
  1087         {
       
  1088 		(TText*) L"Clients that keep CContactDatabase Open; Restore fails",
       
  1089 		3, 1, CBackupTestFramework::ERestoreCorruptDbTest, ETrue, CBackupTestFramework::EAllOpenForBackup , conn::EBackupBase
       
  1090 		},
       
  1091 
       
  1092 		// clients open a contact Local View - Backup and Restore should always succeed
       
  1093 		{
       
  1094 		(TText*) L"Clients that keep a Local View Open",
       
  1095 		1, 1, CBackupTestFramework::EOpenLocalView, ETrue, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1096 		},
       
  1097 		{
       
  1098 		(TText*) L"Clients that close Local View and database",
       
  1099 		1, 1, CBackupTestFramework::EOpenLocalView, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1100 		},
       
  1101 
       
  1102 		// clients open a contact Remote View - Backup and Restore should always succeed
       
  1103 		{
       
  1104 		(TText*) L"Clients that keep a Remote View Open",
       
  1105 		1, 1, CBackupTestFramework::EOpenRemoteView, ETrue, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1106 		},
       
  1107 		{
       
  1108 		(TText*) L"Clients that close Remote View and database",
       
  1109 		1, 1, CBackupTestFramework::EOpenRemoteView, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1110 		},
       
  1111 
       
  1112 		// Multiple clients per database - Backup and Restore should always succeed
       
  1113 		{
       
  1114 		(TText*) L"Multiple clients per database, all keep CContactDatabase Open",
       
  1115 		3, 3, CBackupTestFramework::EIdleClient, ETrue, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1116 		},
       
  1117 		{
       
  1118 		(TText*) L"Multiple clients per database, all close CContactDatabase",
       
  1119 		3, 3, CBackupTestFramework::EIdleClient, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1120 		},
       
  1121 		
       
  1122 				
       
  1123 		{
       
  1124 		(TText*) L"Two clients per database, one close and one keep CContactDatabase open",
       
  1125 		2, 2, CBackupTestFramework::EIdleClient, ETrue, CBackupTestFramework::EHalfCloseForBackup, conn::EBackupBase
       
  1126 		},
       
  1127 		{
       
  1128 		(TText*) L"Two clients per database, one closes its contact item and one keeps its contact item open",
       
  1129 		1, 2, CBackupTestFramework::EMultipleOpenOneContactForEdit, EFalse, CBackupTestFramework::EHalfCloseForBackup, conn::EBackupBase
       
  1130         },
       
  1131 
       
  1132 		/*
       
  1133 		 Test client activities that should BLOCK Backup / Restore
       
  1134 		 Tests are in pairs:
       
  1135 		 1. does not respond to Backup or Restore - (tests that Backup & Restore fail)
       
  1136 		 2. closes all resources when Backup or Restore begin - (tests that Backup & Restore succeed)
       
  1137 		*/
       
  1138 
       
  1139 		// Client opens contact for edit - Backup and Restore succeed when resources are freed
       
  1140 		{
       
  1141 		(TText*) L"Client opens contact for edit, resources kept open",
       
  1142 		1, 1, CBackupTestFramework::EOpenOneContactForEdit, EFalse, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1143 		},
       
  1144 		{
       
  1145 		(TText*) L"Client opens contact for edit, resources released",
       
  1146 		1, 1, CBackupTestFramework::EOpenOneContactForEdit, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1147 		},
       
  1148 
       
  1149 		// Open two contacts for edit - Backup and Restore succeed when resources are freed
       
  1150 		{
       
  1151 		(TText*) L"Open two contacts for edit, resources kept open",
       
  1152 		1, 1, CBackupTestFramework::EOpenTwoContactsForEdit, EFalse, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1153 		},
       
  1154 		{
       
  1155 		(TText*) L"Open two contacts for edit, resources released",
       
  1156 		1, 1, CBackupTestFramework::EOpenTwoContactsForEdit, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1157 		},
       
  1158 
       
  1159 
       
  1160 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
  1161 		// Active Compact - Backup and Restore succeed when resources are free
       
  1162 		{
       
  1163 		(TText*) L"Active Compact, resources kept open",
       
  1164 		1, 1, CBackupTestFramework::EContactsActiveCompact, EFalse, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1165 		},
       
  1166 		{
       
  1167 		(TText*) L"Active Compact, resources released",
       
  1168 		1, 1, CBackupTestFramework::EContactsActiveCompact, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1169 		},
       
  1170 
       
  1171 		// Active Recover - Backup and Restore succeed when resources are free
       
  1172 		{
       
  1173 		(TText*) L"Active Recover, resources kept open",
       
  1174 		1, 1, CBackupTestFramework::EContactsActiveRecover, EFalse, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1175 		},
       
  1176 		{
       
  1177 		(TText*) L"Active Recover, resources released",
       
  1178 		1, 1, CBackupTestFramework::EContactsActiveRecover, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1179 		},
       
  1180 #endif
       
  1181 
       
  1182 		// Async Find - Backup and Restore succeed when resources are freed
       
  1183 		{
       
  1184 		(TText*) L"Async Find - Blocks Backup / Restore until it completes",
       
  1185 		1, 1, CBackupTestFramework::EContactsAsyncFind, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1186 		},
       
  1187 
       
  1188 		// Contacts Idle Sorter - Backup and Restore succeed when it completes and resources are freed
       
  1189 		{
       
  1190 		(TText*) L"Contacts Idle Sorter - Blocks Backup / Restore until it completes",
       
  1191 		1, 1, CBackupTestFramework::EContactsIdleSorter, ETrue, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1192 		},
       
  1193 
       
  1194 		// Contacts client updates SpeedDial and checks this is Backed-up and Restored
       
  1195 		{
       
  1196 		(TText*) L"Contacts test for .Ini file Backup and Restore (clients don't close)",
       
  1197 		1, 1, CBackupTestFramework::EContactsIniFileTest, ETrue, CBackupTestFramework::EAllOpenForBackup, conn::EBackupBase
       
  1198 		},
       
  1199 		// Open Remote View and open a contact - Backup and Restore succeed when it completes and resources are freed
       
  1200  		{
       
  1201 		(TText*) L"Multiple clients that create a remote view and open a contact(clients close)",
       
  1202 		1, 2, CBackupTestFramework::EOpenRemoteViewOneContactForEdit, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1203 		},
       
  1204 		{
       
  1205 		(TText*) L"Contacts test for .Ini file Backup and Restore(clients close",
       
  1206 		1, 1, CBackupTestFramework::EContactsIniFileTest, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupBase
       
  1207 		},
       
  1208 
       
  1209 		//
       
  1210 		// Incremental Backup and Restore. Should always succeed.
       
  1211 		{
       
  1212 		(TText*) L"INCREMENTAL: Clients that keep CContactDatabase Open",
       
  1213 		3, 1, CBackupTestFramework::EIdleClient, ETrue, CBackupTestFramework::EAllOpenForBackup, conn::EBackupIncrement
       
  1214 		},
       
  1215 		{
       
  1216 		(TText*) L"INCREMENTAL: Client opens contact for edit, resources released",
       
  1217 		1, 1, CBackupTestFramework::EOpenOneContactForEdit, ETrue, CBackupTestFramework::EAllCloseForBackup, conn::EBackupIncrement
       
  1218  		}
       
  1219 	};
       
  1220 
       
  1221 
       
  1222 CBackupTestFramework* CBackupTestFramework::NewLC()
       
  1223 	{
       
  1224 	CBackupTestFramework* self = new(ELeave) CBackupTestFramework();
       
  1225 	CleanupStack::PushL(self);
       
  1226 	self->ConstructL();
       
  1227 	return self;
       
  1228 	}
       
  1229 	
       
  1230 	
       
  1231 CBackupTestFramework::CBackupTestFramework() : CTimer(CActive::EPriorityStandard)
       
  1232 	{
       
  1233 	CActiveScheduler::Add(this);
       
  1234 	}
       
  1235 	
       
  1236 	
       
  1237 void CBackupTestFramework::ConstructL()
       
  1238 	{
       
  1239 	CTimer::ConstructL();
       
  1240 	
       
  1241     // Test server used for copying files around.    
       
  1242     User::LeaveIfError(iTestServ.Connect());
       
  1243     iBackupHelper = CBackupHelper::NewL(iTestServ);
       
  1244 	}
       
  1245 
       
  1246 
       
  1247 CBackupTestFramework::~CBackupTestFramework()
       
  1248 	{
       
  1249 	// cleanup from current test case
       
  1250 	CTimer::Cancel();
       
  1251 
       
  1252     iTestServ.Close();
       
  1253 
       
  1254 	delete iBackupHelper;
       
  1255 	iContactClientSession.ResetAndDestroy();
       
  1256 	}
       
  1257 	
       
  1258 	
       
  1259 void CBackupTestFramework::DoBackupRestoreTestsL()
       
  1260 	{
       
  1261 	const TInt testCount = sizeof(BackupTestCases) / sizeof(CBackupTestFramework::TBackupRestoreTestCase);
       
  1262 
       
  1263 	test.Printf(_L("Iterate through all %i Backup and Restore test cases"), testCount);
       
  1264 
       
  1265 	for (TInt testIndex = 0; testIndex < testCount; ++testIndex)
       
  1266 		{
       
  1267 		iTestCase = &BackupTestCases[testIndex];
       
  1268 		BackupRestoreTestL();
       
  1269 		}
       
  1270 	}
       
  1271 
       
  1272 
       
  1273 
       
  1274 // Method to setup the Use Case under test
       
  1275 // returns ETrue if TrequestStatus was used
       
  1276 TBool CBackupTestFramework::PrepareBackupClientTestCaseL(TRequestStatus& aStatus)
       
  1277 	{
       
  1278 	TBool asyncRequest = EFalse;
       
  1279 
       
  1280 	// for every database we are required to open
       
  1281 	for (TInt dbCount = 1; (dbCount <= iTestCase->iNumDatabases) && (!asyncRequest); ++dbCount)
       
  1282 		{
       
  1283 		switch (dbCount)
       
  1284 			{
       
  1285 			case 1:
       
  1286 				asyncRequest = PrepareTestCaseL(KBackupDatabaseFile1(), aStatus);
       
  1287 				break; 
       
  1288 
       
  1289 			case 2:
       
  1290 				asyncRequest = PrepareTestCaseL(KBackupDatabaseFile2(), aStatus);
       
  1291 				break;
       
  1292 
       
  1293 			case 3:
       
  1294 				asyncRequest = PrepareTestCaseL(KBackupDatabaseFile3(), aStatus);
       
  1295 				break;
       
  1296 
       
  1297 			default: // something is wrong with test code
       
  1298 				User::Invariant();
       
  1299 			}
       
  1300 		}
       
  1301 
       
  1302 	return asyncRequest;
       
  1303 	}
       
  1304 	
       
  1305 
       
  1306 // Method to implement the Use Case under test
       
  1307 TBool CBackupTestFramework::PrepareTestCaseL(const TDesC& aDatabaseName, TRequestStatus& aStatus)
       
  1308 	{
       
  1309 	TBool asyncRequest = EFalse;
       
  1310 
       
  1311 	// at least one, possibly more, clients per database
       
  1312 	for (TInt clClient = 1; clClient <= iTestCase->iClientsPerDatabase; ++clClient)
       
  1313 		{
       
  1314 		// open database
       
  1315 				TBool isCloseForBackup = ETrue;
       
  1316 
       
  1317 		switch(iTestCase->iCloseForBackup)
       
  1318 			{
       
  1319 			case EAllOpenForBackup:
       
  1320 				isCloseForBackup = EFalse;
       
  1321 				break;
       
  1322 			case EHalfCloseForBackup:
       
  1323 				if(clClient > iTestCase->iClientsPerDatabase/2)
       
  1324 					{
       
  1325 					isCloseForBackup = EFalse;
       
  1326 					}
       
  1327 				break;
       
  1328 			case EAllCloseForBackup:
       
  1329 			default:
       
  1330 				break;
       
  1331 			}
       
  1332 
       
  1333 		CContactsClient* db = NewDatabaseClientL(aDatabaseName, isCloseForBackup);
       
  1334 
       
  1335 
       
  1336 		// specific test case behaviour
       
  1337 		switch (iTestCase->iActivity)
       
  1338 			{
       
  1339 			// activities that do not block backup / restore
       
  1340 			case EIdleClient:
       
  1341 			case ERestoreCorruptDbTest:
       
  1342 				// demonstrate database is open, read from it
       
  1343 				test.Next(_L("Read a contact from database"));
       
  1344 				db->ReadContactL();
       
  1345 				break;
       
  1346 
       
  1347 			case EOpenLocalView:
       
  1348 				test.Next(_L("open contact Local View"));
       
  1349 				db->OpenLocalViewL();
       
  1350 				break;
       
  1351 
       
  1352 			case EOpenRemoteView:
       
  1353 				test.Next(_L("open contact Remote View"));
       
  1354 				db->OpenRemoteViewL();
       
  1355 				break;
       
  1356 
       
  1357 			// activities that can block backup / restore
       
  1358 			case EOpenOneContactForEdit:
       
  1359 				test.Next(_L("Open a contact from database"));
       
  1360 				db->OpenContactL(KOpenContactId1);
       
  1361 				break;
       
  1362 
       
  1363 			case EOpenTwoContactsForEdit:
       
  1364 				test.Next(_L("Open 2 contacts from database"));
       
  1365 				db->OpenContactL(KOpenContactId1);
       
  1366 				db->OpenContactL(KOpenContactId2);
       
  1367 				break;
       
  1368 
       
  1369 			case EContactsAsyncFind:
       
  1370 				test.Next(_L("Start Async Find on database"));
       
  1371 				db->StartAsyncFindL();
       
  1372 				break;
       
  1373 
       
  1374 			case EContactsActiveCompact:
       
  1375 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
  1376 				test.Next(_L("Start Active Compact on database"));
       
  1377 				db->StartActiveCompactL();
       
  1378 #endif
       
  1379 				break;
       
  1380 
       
  1381 			case EContactsActiveRecover:
       
  1382 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
  1383 				test.Next(_L("Start Active Recover on database"));
       
  1384 				db->StartActiveRecoverL();
       
  1385 #endif
       
  1386 				break;
       
  1387 
       
  1388 			case EContactsIdleSorter:
       
  1389 				test.Next(_L("Start Contacts Idle Sorter"));
       
  1390 				db->StartIdleSorterL(aStatus);
       
  1391 				asyncRequest = ETrue;
       
  1392 				break;
       
  1393 
       
  1394 			case EContactsIniFileTest:
       
  1395 				// demonstrate database is open, read from it
       
  1396 				test.Next(_L("Read a contact from database"));
       
  1397 				db->ReadContactL();
       
  1398 				break;
       
  1399 				
       
  1400 						case EOpenRemoteViewOneContactForEdit:
       
  1401 			    test.Next(_L("Open a contact from database & open contact Remote View"));
       
  1402 			    if(clClient == 1) //There are two clients for this DB, first opens a view, second edits a contact
       
  1403 			        {
       
  1404 			    	db->OpenRemoteViewL();
       
  1405 			        }
       
  1406 			    else
       
  1407 			        {
       
  1408 			    	db->OpenContactL(KOpenContactId1);
       
  1409 			        }
       
  1410 			    break;
       
  1411 			case EMultipleOpenOneContactForEdit:
       
  1412 			    test.Next(_L("Multiple clients, one closes contact item one leaves open"));
       
  1413 			    if (isCloseForBackup) //ETrue
       
  1414 			        {
       
  1415 			    	db->OpenContactL(KOpenContactId1);
       
  1416 			        }  
       
  1417 				else //EFalse
       
  1418 				    {
       
  1419 					db->OpenContactL(KOpenContactId2);
       
  1420 				    }
       
  1421 				    
       
  1422 				break;
       
  1423 
       
  1424 			}
       
  1425 		}
       
  1426 
       
  1427 	return asyncRequest;
       
  1428 	}
       
  1429 
       
  1430 
       
  1431 
       
  1432 // Reconnect disconnected client after Backup/Restore
       
  1433 void CBackupTestFramework::ReconnectBackupClientTestCaseL()
       
  1434 	{
       
  1435 	// check that databases can be re-opened and are still useable
       
  1436 	const TInt numDatabases = iContactClientSession.Count();
       
  1437 	for (TInt i = 0; i < numDatabases; i++)
       
  1438 		{
       
  1439 		if (!iContactClientSession[i]->IsOpen())
       
  1440 			{
       
  1441 			test.Printf(_L("reopen database client # %i after backup/restore\n\r"), i + 1);
       
  1442 			iContactClientSession[i]->OpenDatabaseL();
       
  1443 			}
       
  1444 			
       
  1445 		// read from db
       
  1446 		test.Next(_L("Read a contact through database client"));
       
  1447 		iContactClientSession[i]->ReadContactL();
       
  1448 		}
       
  1449 	}
       
  1450 
       
  1451 
       
  1452 // test action if client didn't close database
       
  1453 void CBackupTestFramework::CheckConnectionAfterBackupForTestCaseL()
       
  1454 	{
       
  1455 	const TInt numDatabases = iContactClientSession.Count();
       
  1456 
       
  1457 	// specific test case behaviour
       
  1458 	switch (iTestCase->iActivity)
       
  1459 		{
       
  1460 		// activities that do not block backup / restore
       
  1461 		case EIdleClient:
       
  1462 		case EOpenLocalView:
       
  1463 		case EOpenRemoteView:
       
  1464 		case EContactsIniFileTest:
       
  1465 			for (TInt i = 0; i < numDatabases; i++)
       
  1466 				{
       
  1467 				// read from each db
       
  1468 				test.Next(_L("Read a contact through database client"));
       
  1469 				iContactClientSession[i]->ReadContactL();
       
  1470 				}
       
  1471 			break;
       
  1472 
       
  1473 		case ERestoreCorruptDbTest:
       
  1474 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
  1475 			for (TInt i = 0; i < numDatabases; i++)
       
  1476 				{
       
  1477 				// read from each db
       
  1478 				test.Next(_L("Read a contact through database client"));
       
  1479 				TRAPD(err, iContactClientSession[i]->ReadContactL());
       
  1480 				
       
  1481 				// The restore is complete. The corrupted db file was damaged
       
  1482                 // but should have been recovered after the restore completed. 
       
  1483                 // However, the contact doesn't exist in the db. If the 
       
  1484                 // corrupted db file was totally unrecognizable then the recover
       
  1485                 // wouldn't have been attempted and the db would be closed. This
       
  1486                 // would then return KErrNotReady.
       
  1487 				if((iTestStep == EPostRestore) &&
       
  1488 				    (iContactClientSession[i]->DatabaseName()==KBackupDatabaseFile1))
       
  1489 					{
       
  1490 					TEST_CONDITION(err==KErrNotFound);
       
  1491 					}
       
  1492 				else //others should not Leave, so propagate Leave if it occurs.
       
  1493 					{
       
  1494 					if(err != KErrNone)
       
  1495 						{
       
  1496 						User::Leave(err);
       
  1497 						}
       
  1498 					}
       
  1499 				}
       
  1500 #endif
       
  1501 			break;
       
  1502 
       
  1503 		// activities that can block backup / restore
       
  1504 		case EOpenOneContactForEdit:
       
  1505 			for (TInt i = 0; i < numDatabases; i++)
       
  1506 				{
       
  1507 				test.Next(_L("Commit contact to database"));
       
  1508 				iContactClientSession[i]->CommitContactL(KOpenContactId1);
       
  1509 				}
       
  1510 			break;
       
  1511 
       
  1512 		case EOpenTwoContactsForEdit:
       
  1513 			for (TInt i = 0; i < numDatabases; i++)
       
  1514 				{
       
  1515 				test.Next(_L("Commit 2 contacts to database"));
       
  1516 				iContactClientSession[i]->CommitContactL(KOpenContactId1);
       
  1517 				iContactClientSession[i]->CommitContactL(KOpenContactId2);
       
  1518 				}
       
  1519 			break;
       
  1520 
       
  1521 		case EContactsAsyncFind:
       
  1522 			for (TInt i = 0; i < numDatabases; i++)
       
  1523 				{
       
  1524 				test.Next(_L("Complete Async Find on database"));
       
  1525 				iContactClientSession[i]->CompleteAsyncFindL();
       
  1526 				}
       
  1527 			break;
       
  1528 
       
  1529 		case EContactsActiveCompact:
       
  1530 			for (TInt i = 0; i < numDatabases; i++)
       
  1531 				{
       
  1532 				test.Next(_L("Complete Active Compact on database"));
       
  1533 				iContactClientSession[i]->CompleteActiveCompactL();
       
  1534 				}
       
  1535 			break;
       
  1536 
       
  1537 		case EContactsActiveRecover:
       
  1538 			for (TInt i = 0; i < numDatabases; i++)
       
  1539 				{
       
  1540 				test.Next(_L("Complete Active Recover on database"));
       
  1541 				iContactClientSession[i]->CompleteActiveRecoverL();
       
  1542 				}
       
  1543 			break;
       
  1544 
       
  1545 		case EContactsIdleSorter:
       
  1546 			// nothing to do for this Use Case
       
  1547 			break;
       
  1548 		}
       
  1549 	}
       
  1550 
       
  1551 
       
  1552 
       
  1553 // support methods for testing .Ini file Backup and Restore
       
  1554 
       
  1555 // any test case action required immediately before Backup start notifiction
       
  1556 void CBackupTestFramework::PreBackupNotificationActionsL()
       
  1557 	{
       
  1558 	const TInt numDatabases = iContactClientSession.Count();
       
  1559 
       
  1560 	// specific test case behaviour
       
  1561 	switch (iTestCase->iActivity)
       
  1562 		{
       
  1563 		case EIdleClient:
       
  1564         case ERestoreCorruptDbTest:
       
  1565 		case EOpenLocalView:
       
  1566 		case EOpenRemoteView:
       
  1567 		case EOpenOneContactForEdit:
       
  1568 		case EOpenTwoContactsForEdit:
       
  1569 		case EContactsAsyncFind:
       
  1570 		case EContactsActiveCompact:
       
  1571 		case EContactsActiveRecover:
       
  1572 		case EContactsIdleSorter:
       
  1573 			// nothing to do for this Use Case
       
  1574 			break;
       
  1575 
       
  1576 		case EContactsIniFileTest:
       
  1577 			// make change to Contacts .ini file that should be Backed-up
       
  1578 			for (TInt i = 0; i < numDatabases; i++)
       
  1579 				{
       
  1580 				// read from each db
       
  1581 				test.Next(_L("Set Speed Dial 1 for database"));
       
  1582 				iContactClientSession[i]->SetSpeedDialL(KSpeedDialContactId, KSpeedDialPositionOne);
       
  1583 				}
       
  1584 			break;
       
  1585 		}
       
  1586 	}
       
  1587 
       
  1588 
       
  1589 // any test case action required after Backup completion
       
  1590 void CBackupTestFramework::PostBackupActionsL()
       
  1591 	{
       
  1592 	const TInt numDatabases = iContactClientSession.Count();
       
  1593 
       
  1594 	// specific test case behaviour
       
  1595 	switch (iTestCase->iActivity)
       
  1596 		{
       
  1597 		case EIdleClient:
       
  1598         case ERestoreCorruptDbTest:
       
  1599 		case EOpenLocalView:
       
  1600 		case EOpenRemoteView:
       
  1601 		case EOpenOneContactForEdit:
       
  1602 		case EOpenTwoContactsForEdit:
       
  1603 		case EContactsAsyncFind:
       
  1604 		case EContactsActiveCompact:
       
  1605 		case EContactsActiveRecover:
       
  1606 		case EContactsIdleSorter:
       
  1607 		case EOpenRemoteViewOneContactForEdit:
       
  1608 		case EMultipleOpenOneContactForEdit:
       
  1609 
       
  1610 			// nothing to do for this Use Case
       
  1611 			break;
       
  1612 
       
  1613 		case EContactsIniFileTest:
       
  1614 			// check Contacts .ini is correct
       
  1615 			for (TInt i = 0; i < numDatabases; i++)
       
  1616 				{
       
  1617 				// read from each db
       
  1618 				test.Next(_L("Check Speed Dial 1 still set for database"));
       
  1619 				iContactClientSession[i]->CheckSpeedDialSetL(KSpeedDialContactId, KSpeedDialPositionOne);
       
  1620 				}
       
  1621 			break;
       
  1622 		}
       
  1623 	}
       
  1624 
       
  1625 
       
  1626 // any test case action required immediately before Restore start notifiction
       
  1627 void CBackupTestFramework::PreRestoreNotificationActionsL()
       
  1628 	{
       
  1629 	const TInt numDatabases = iContactClientSession.Count();
       
  1630 
       
  1631 	// specific test case behaviour
       
  1632 	switch (iTestCase->iActivity)
       
  1633 		{
       
  1634 		case EIdleClient:
       
  1635         case ERestoreCorruptDbTest:
       
  1636 		case EOpenLocalView:
       
  1637 		case EOpenRemoteView:
       
  1638 		case EOpenOneContactForEdit:
       
  1639 		case EOpenTwoContactsForEdit:
       
  1640 		case EContactsAsyncFind:
       
  1641 		case EContactsActiveCompact:
       
  1642 		case EContactsActiveRecover:
       
  1643 		case EContactsIdleSorter:
       
  1644 		case EOpenRemoteViewOneContactForEdit:
       
  1645 		case EMultipleOpenOneContactForEdit:
       
  1646 
       
  1647 			// nothing to do for this Use Case
       
  1648 			break;
       
  1649 
       
  1650 		case EContactsIniFileTest:
       
  1651 			// make change to Contacts .ini file before Restore
       
  1652 			for (TInt i = 0; i < numDatabases; i++)
       
  1653 				{
       
  1654 				// read from each db
       
  1655 				test.Next(_L("Remove Speed Dial 1 from database"));
       
  1656 				iContactClientSession[i]->RemoveSpeedDialL(KSpeedDialContactId, KSpeedDialPositionOne);
       
  1657 				}
       
  1658 			break;
       
  1659 		}
       
  1660 	}
       
  1661 
       
  1662 // any test case action required after Restore completion
       
  1663 void CBackupTestFramework::PostRestoreActionsL()
       
  1664 	{
       
  1665 	const TInt numDatabases = iContactClientSession.Count();
       
  1666 
       
  1667 	// specific test case behaviour
       
  1668 	switch (iTestCase->iActivity)
       
  1669 		{
       
  1670 		case EIdleClient:
       
  1671         case ERestoreCorruptDbTest:
       
  1672 		case EOpenLocalView:
       
  1673 		case EOpenRemoteView:
       
  1674 		case EOpenOneContactForEdit:
       
  1675 		case EOpenTwoContactsForEdit:
       
  1676 		case EContactsAsyncFind:
       
  1677 		case EContactsActiveCompact:
       
  1678 		case EContactsActiveRecover:
       
  1679 		case EContactsIdleSorter:
       
  1680 		case EOpenRemoteViewOneContactForEdit:
       
  1681 		case EMultipleOpenOneContactForEdit:
       
  1682 
       
  1683 			// nothing to do for this Use Case
       
  1684 			break;
       
  1685 
       
  1686 		case EContactsIniFileTest:
       
  1687 			// check Restored Contacts .ini file is the same as was Backed-up
       
  1688 			for (TInt i = 0; i < numDatabases; i++)
       
  1689 				{
       
  1690 				// read from each db
       
  1691 				test.Next(_L("Check Restored Speed Dial 1 from database"));
       
  1692 				iContactClientSession[i]->CheckSpeedDialSetL(KSpeedDialContactId, KSpeedDialPositionOne);
       
  1693 				}
       
  1694 			break;
       
  1695 		}
       
  1696 	}
       
  1697 
       
  1698 
       
  1699 
       
  1700 
       
  1701 // Backup and Restore test with chosen Use Case
       
  1702 void CBackupTestFramework::BackupRestoreTestL()
       
  1703 	{
       
  1704 	// need to do type conversion in 2 steps
       
  1705 	TPtrC title(iTestCase->iTestTitle);
       
  1706 	test.Next(title);
       
  1707 
       
  1708 
       
  1709 	// start backup & restore tests in Active Scheduler
       
  1710 	iTestStep = EPreBackup;
       
  1711 	After(0);
       
  1712 	CActiveScheduler::Start();
       
  1713 
       
  1714 	// ensure databases closed & release resources
       
  1715 	iContactClientSession.ResetAndDestroy();
       
  1716 
       
  1717 	// any error from active scheduler?
       
  1718 	User::LeaveIfError(iTestError);
       
  1719 	}
       
  1720 
       
  1721 
       
  1722 
       
  1723 void CBackupTestFramework::RunL()
       
  1724 	{
       
  1725 	TBool waitForAsyncRequest = EFalse;
       
  1726 	
       
  1727 	if (iStatus.Int() != KErrCancel)
       
  1728 		{
       
  1729 		switch (iTestStep)
       
  1730 			{
       
  1731 		// Backup test
       
  1732 		case EPreBackup:
       
  1733 			test.Start(_L("Backup test"));
       
  1734 
       
  1735 			test.Start(_L("Prepare Backup test case, open database(s) etc..."));
       
  1736 			waitForAsyncRequest = PrepareBackupClientTestCaseL(iStatus);
       
  1737 
       
  1738 			// reset count number of Backup notifications
       
  1739 			iClientNotifications = 0;
       
  1740 			iViewUnavailableCount = 0;
       
  1741 			break;
       
  1742 				
       
  1743 		case ENotifyBackupStart:
       
  1744 			// any test case action required immediately before Backup start notifiction
       
  1745 			PreBackupNotificationActionsL();
       
  1746 
       
  1747 			if(conn::EBackupIncrement == iTestCase->iBackupIncType)
       
  1748 				{
       
  1749 				test.Next(_L("notify start of incremental backup"));
       
  1750 				iBackupHelper->StartBackupL(conn::EBURBackupPartial, conn::EBackupIncrement);
       
  1751 				}
       
  1752 			else
       
  1753 				{
       
  1754 				test.Next(_L("notify start of backup"));
       
  1755 				iBackupHelper->StartBackupL(conn::EBURBackupFull, conn::EBackupBase);
       
  1756 				}
       
  1757 
       
  1758 			break;
       
  1759 
       
  1760 		case ECheckBackupStartNotifications:
       
  1761 			if(conn::EBackupIncrement == iTestCase->iBackupIncType)
       
  1762 				{
       
  1763 				test.Next(_L("check number of incremental backup start notifications"));					
       
  1764 				}
       
  1765 			else
       
  1766 				{
       
  1767 				test.Next(_L("check number of backup start notifications"));	
       
  1768 				}
       
  1769 			TEST_CONDITION(iContactClientSession.Count() == iClientNotifications);
       
  1770 
       
  1771 			// test case has views?
       
  1772 			if ((iTestCase->iActivity == EOpenLocalView) || (iTestCase->iActivity == EOpenRemoteView))
       
  1773 				{
       
  1774 				if (iTestCase->iCloseForBackup)
       
  1775 					{
       
  1776 					// Contacts clients close for Backup or Restore
       
  1777 					test.Next(_L("check not too many Contact View unavailable notifications"));
       
  1778 					TEST_CONDITION((iTestCase->iNumDatabases * iTestCase->iClientsPerDatabase) >= iViewUnavailableCount);
       
  1779 					}
       
  1780 				else
       
  1781 					{
       
  1782 					// Contacts clients open for Backup or Restore
       
  1783 					test.Next(_L("check the number of Contact View unavailable notifications"));
       
  1784 					TEST_CONDITION((iTestCase->iNumDatabases * iTestCase->iClientsPerDatabase) == iViewUnavailableCount);
       
  1785 					}
       
  1786 				}
       
  1787 			break;
       
  1788 
       
  1789 		case EDoBackup:
       
  1790 			if(conn::EBackupIncrement == iTestCase->iBackupIncType)
       
  1791 				{
       
  1792 				test.Next(_L("attempt incremental backup of database(s)"));					
       
  1793 				}
       
  1794 			else
       
  1795 				{
       
  1796 				test.Next(_L("attempt backup of database(s)"));		
       
  1797 				}
       
  1798 
       
  1799 			// Clients have not closed for Backup?
       
  1800 			if (iTestCase->iCloseForBackup != EAllCloseForBackup)
       
  1801 				{
       
  1802 				const TInt numDatabases = iContactClientSession.Count();
       
  1803 
       
  1804 				// find open database sessions
       
  1805 				test.Next(_L("Check that new Read and Open.(Write) operations cannot start"));
       
  1806 				for (TInt i = 0; i < numDatabases; i++)
       
  1807 					{
       
  1808 					if (! iContactClientSession[i]->IsCloseForBackup())
       
  1809 					    {
       
  1810 					    test.Next(_L("check that database read is locked"));
       
  1811 					    TRAPD(error, iContactClientSession[i]->ReadContactL());
       
  1812 					    TEST_CONDITION(KErrLocked == error);
       
  1813 
       
  1814 					    test.Next(_L("check that database write is locked"));
       
  1815 					    TRAP(error, iContactClientSession[i]->OpenContactL(KOpenContactId3));
       
  1816 					    TEST_CONDITION(KErrLocked == error);
       
  1817 
       
  1818 					    test.Next(_L("check that phone number lookup (database read) is locked"));
       
  1819 					    TRAP(error, iContactClientSession[i]->FindPhoneNumberL());
       
  1820 					    TEST_CONDITION(KErrLocked == error);
       
  1821 					    }
       
  1822 					}
       
  1823 				}
       
  1824 
       
  1825 			if(iTestCase->iActivity == EContactsIniFileTest)
       
  1826 				{
       
  1827 			    test.Next(_L("checking that backup of cntmodel.ini file succeeds"));
       
  1828 		 	    TEST_CONDITION(iBackupHelper->SimulateIniBackup());
       
  1829 				}
       
  1830 
       
  1831 			if (iTestCase->iExpectedBackupSuccess)
       
  1832 				{
       
  1833 				test.Next(_L("checking that backup of databases succeeds"));
       
  1834 				TEST_CONDITION(iBackupHelper->SimulateDbBackup());
       
  1835 				}
       
  1836 			else
       
  1837 				{
       
  1838 				test.Next(_L("checking that backup of databases fails"));
       
  1839 				TEST_CONDITION(! iBackupHelper->SimulateDbBackup());
       
  1840 				}
       
  1841 
       
  1842 			// reset count for end of backup notifications		
       
  1843 			iClientNotifications = 0;
       
  1844 			iViewReadyCount = 0;
       
  1845 			break;
       
  1846 			
       
  1847 		case ENotifyBackupEnd:
       
  1848 			test.Next(_L("notify end of backup"));
       
  1849 			iBackupHelper->EndBackupL();
       
  1850 			break;
       
  1851 
       
  1852 		case ECheckBackupEndNotifications:
       
  1853 			// if database connections are kept open
       
  1854 			if (iTestCase->iCloseForBackup == EAllOpenForBackup)
       
  1855 				{
       
  1856 				test.Next(_L("check number of backup end notifications"));
       
  1857 				TEST_CONDITION(iContactClientSession.Count() == iClientNotifications);
       
  1858 
       
  1859 				// test case kept views open?
       
  1860 				if ((iTestCase->iActivity == EOpenLocalView) || (iTestCase->iActivity == EOpenRemoteView))
       
  1861 					{
       
  1862 					test.Next(_L("check the number of Contact View ready notifications"));
       
  1863 					TEST_CONDITION((iTestCase->iNumDatabases * iTestCase->iClientsPerDatabase) == iViewReadyCount);
       
  1864 					}
       
  1865 				}
       
  1866 			break;
       
  1867 
       
  1868 		case EPostBackup:
       
  1869 			test.Next(_L("post backup verify database(s) useable"));
       
  1870 			if (iTestCase->iCloseForBackup != EAllOpenForBackup)
       
  1871 				{
       
  1872 				ReconnectBackupClientTestCaseL();
       
  1873 				}
       
  1874 			else
       
  1875 				{
       
  1876 				CheckConnectionAfterBackupForTestCaseL();
       
  1877 				}
       
  1878 
       
  1879 			// any test case action required after Backup completion
       
  1880 			PostBackupActionsL();
       
  1881 			break;
       
  1882 			
       
  1883 		case EFinishedBackup:
       
  1884 			test.Next(_L("Cleanup all client resources for test case"));
       
  1885 			// cleanup after backup test
       
  1886 			iContactClientSession.ResetAndDestroy();
       
  1887 			test.End();
       
  1888 			break;
       
  1889 
       
  1890 		// Successful Restore test
       
  1891 		case EPreRestore:
       
  1892 			test.Next(_L("Restore test"));
       
  1893 
       
  1894 			test.Start(_L("pre-restore access database(s)"));
       
  1895 			waitForAsyncRequest = PrepareBackupClientTestCaseL(iStatus);
       
  1896 
       
  1897 			// reset count of Restore notifications
       
  1898 			iClientNotifications = 0;
       
  1899 			iViewUnavailableCount = 0;
       
  1900 			break;
       
  1901 			
       
  1902 		case ENotifyRestoreStart:
       
  1903 			// any test case action required immediately before Restore start notifiction
       
  1904 			PreRestoreNotificationActionsL();
       
  1905 
       
  1906 			test.Next(_L("notify start of restore"));
       
  1907 			iBackupHelper->StartRestoreL();
       
  1908 			break;
       
  1909 
       
  1910 		case ECheckRestoreStartNotifications:
       
  1911 			test.Next(_L("check number of restore start notifications"));
       
  1912 			TEST_CONDITION(iContactClientSession.Count() == iClientNotifications);
       
  1913 
       
  1914 			// test case has views?
       
  1915 			if ((iTestCase->iActivity == EOpenLocalView) || (iTestCase->iActivity == EOpenRemoteView))
       
  1916 				{
       
  1917 				if (iTestCase->iCloseForBackup == EAllOpenForBackup)
       
  1918 					{
       
  1919 					// Contacts clients open for Backup or Restore
       
  1920 					test.Next(_L("check the number of Contact View unavailable notifications"));
       
  1921 					TEST_CONDITION((iTestCase->iNumDatabases * iTestCase->iClientsPerDatabase) == iViewUnavailableCount);
       
  1922 					}
       
  1923 				else
       
  1924 					{
       
  1925 					// Contacts clients close for Backup or Restore
       
  1926 					test.Next(_L("check not too many Contact View unavailable notifications"));
       
  1927 					TEST_CONDITION((iTestCase->iNumDatabases * iTestCase->iClientsPerDatabase) >= iViewUnavailableCount);
       
  1928 					}
       
  1929 				}
       
  1930 			break;
       
  1931 
       
  1932 		case EDoRestore:
       
  1933 			test.Next(_L("attempt restore of database(s)"));
       
  1934 
       
  1935 			// Clients have not closed for Backup?
       
  1936 				if (iTestCase->iCloseForBackup == EAllOpenForBackup)
       
  1937 
       
  1938 				{
       
  1939 				const TInt numDatabases = iContactClientSession.Count();
       
  1940 
       
  1941 				// find all open database sessions
       
  1942 				for (TInt i = 0; i < numDatabases; i++)
       
  1943 					{
       
  1944 					test.Next(_L("check that database read is Locked"));
       
  1945 					TRAPD(error, iContactClientSession[i]->ReadContactL());
       
  1946 					TEST_CONDITION(KErrLocked == error);
       
  1947 
       
  1948 					test.Next(_L("check that database write is locked"));
       
  1949 					TRAP(error, iContactClientSession[i]->OpenContactL(KOpenContactId3));
       
  1950 					TEST_CONDITION(KErrLocked == error);
       
  1951 
       
  1952 				    test.Next(_L("check that phone number lookup (database read) is locked"));
       
  1953 				    TRAP(error, iContactClientSession[i]->FindPhoneNumberL());
       
  1954 				    TEST_CONDITION(KErrLocked == error);
       
  1955 					}
       
  1956 				}
       
  1957  			if(iTestCase->iActivity == EContactsIniFileTest)
       
  1958 				{
       
  1959 			    test.Next(_L("checking that restore of ini file succeeds"));
       
  1960 			    TEST_CONDITION(iBackupHelper->SimulateIniRestore());
       
  1961 				}
       
  1962 
       
  1963 			if (iTestCase->iExpectedBackupSuccess)
       
  1964 				{
       
  1965 				if (iTestCase->iActivity == ERestoreCorruptDbTest)
       
  1966 					{
       
  1967 					test.Next(_L("checking that restore of databases fails on the corrupted one"));
       
  1968 					TEST_CONDITION(iBackupHelper->SimulateDamagedDbRestore());
       
  1969 					}
       
  1970 				else
       
  1971 					{
       
  1972     				test.Next(_L("checking that restore of databases succeeds"));
       
  1973     				TEST_CONDITION(iBackupHelper->SimulateDbRestore());
       
  1974                     }
       
  1975 				}
       
  1976 			else
       
  1977 				{
       
  1978 				test.Next(_L("checking that restore of databases fails"));
       
  1979 				TEST_CONDITION(! iBackupHelper->SimulateDbRestore());
       
  1980 				}
       
  1981 
       
  1982 			// reset count for end of Restore notifications
       
  1983 			iClientNotifications = 0;
       
  1984 			iViewReadyCount = 0;
       
  1985 			break;
       
  1986 			
       
  1987 		case ENotifyRestoreEnd:
       
  1988 			test.Next(_L("notify end of restore"));
       
  1989 			iBackupHelper->EndRestoreL();
       
  1990 			break;
       
  1991 
       
  1992 		case ECheckRestoreEndNotifications:
       
  1993 			// if database connections are kept open
       
  1994 			if (iTestCase->iCloseForBackup == EAllOpenForBackup)
       
  1995 				{
       
  1996 				test.Next(_L("check number of restore end notifications"));
       
  1997 				TEST_CONDITION(iContactClientSession.Count() == iClientNotifications);
       
  1998 
       
  1999 				// test case kept views open?
       
  2000 				if ((iTestCase->iActivity == EOpenLocalView) || (iTestCase->iActivity == EOpenRemoteView))
       
  2001 					{
       
  2002 					test.Next(_L("check the number of Contact View ready notifications"));
       
  2003 					TEST_CONDITION((iTestCase->iNumDatabases * iTestCase->iClientsPerDatabase) == iViewReadyCount);
       
  2004 					}
       
  2005 				}
       
  2006 			break;
       
  2007 
       
  2008 		case EPostRestore:
       
  2009 			test.Next(_L("post-restore verify database(s) useable"));
       
  2010 			if (iTestCase->iCloseForBackup)
       
  2011 				{
       
  2012 				ReconnectBackupClientTestCaseL();
       
  2013 				}
       
  2014 			else
       
  2015 				{
       
  2016 				CheckConnectionAfterBackupForTestCaseL();
       
  2017 				}
       
  2018 
       
  2019 			// any test case action required after Restore completion
       
  2020 			PostRestoreActionsL();
       
  2021 			break;
       
  2022 
       
  2023 		case EFinishedRestore:
       
  2024 			test.Next(_L("Cleanup all client resources for test case"));
       
  2025 			// cleanup after backup test
       
  2026 			iContactClientSession.ResetAndDestroy();
       
  2027 			// end x.y.z restore test
       
  2028 			test.End();
       
  2029 			// end x.y test case
       
  2030 			test.End();
       
  2031 
       
  2032             // overwrite corrupted database, other tests don't use it
       
  2033             if(iTestCase->iActivity == ERestoreCorruptDbTest)
       
  2034                 {			
       
  2035                 // Copy a non-corrupted db over the corrupted one for other tests to use.
       
  2036                 iBackupHelper->SimulateDbRestore();
       
  2037                 }
       
  2038 
       
  2039 			// All Done
       
  2040 			CActiveScheduler::Stop();
       
  2041 			return;
       
  2042 			}
       
  2043 
       
  2044 
       
  2045 		// no pause required between steps?
       
  2046 		if (waitForAsyncRequest)
       
  2047 			// waits for an asynchronous request to complete
       
  2048 			{
       
  2049 			SetActive();
       
  2050 			}
       
  2051 		else if ((iTestStep == EPreBackup) || (iTestStep == EPreRestore))
       
  2052 			{
       
  2053 			// start of Backp or Restore test cycle - allow contacts client to settle (e.g. contacts view to sort)
       
  2054 			test.Printf(_L("Wait %d ms to allow contacts client to settle (e.g. contacts view to sort)\n"), KWaitForClientToSettle);
       
  2055 			After(KWaitForClientToSettle);
       
  2056 			}
       
  2057 		else if ((iTestStep == EPostBackup) || (iTestStep == EPostRestore))
       
  2058 			{
       
  2059 			// end of Backup or Restore test cycle - allow all system components to settle
       
  2060 			test.Printf(_L("Wait %d ms to allow all system components to settle\n"), KWaitForSystemToSettle);
       
  2061 			After(KWaitForSystemToSettle);
       
  2062 			}
       
  2063 		else if ((iTestStep == ENotifyBackupStart) || (iTestStep == ENotifyBackupEnd) || (iTestStep == ENotifyRestoreStart) || (iTestStep == ENotifyRestoreEnd))
       
  2064 			{
       
  2065 			// allow notification to propagate
       
  2066 			test.Printf(_L("Wait %d ms to allow notification to propagate\n"), KWaitForBackupNotification);
       
  2067 			After(KWaitForBackupNotification);
       
  2068 			}
       
  2069 		else
       
  2070 			{
       
  2071 			// minimum wait required
       
  2072 			After(1);
       
  2073 			}
       
  2074 			
       
  2075 		// next step
       
  2076 		iTestStep = (TBackupRestoreSteps) (iTestStep + 1);
       
  2077 		}
       
  2078 	}
       
  2079 
       
  2080 
       
  2081 TInt CBackupTestFramework::RunError(TInt aError)
       
  2082 	{
       
  2083 	// RunL is verbose and prints details of each step
       
  2084 	// so just print error info
       
  2085 	test.Printf(_L("Backup and Restore test error %d (at step %d)\n\r"), aError, iTestStep);
       
  2086 	
       
  2087 	// and state info
       
  2088 	const TInt clientCount = iContactClientSession.Count();
       
  2089 	for (TInt j = 0; j < clientCount; j++)
       
  2090 		{
       
  2091 		test.Printf(_L("client %i, IsOpen = %i, name \"%S\"\n\r"), 
       
  2092 			j, iContactClientSession[j]->IsOpen(), &iContactClientSession[j]->DatabaseName());
       
  2093 		}
       
  2094 	
       
  2095 	// propagate error
       
  2096 	iTestError = aError;
       
  2097 	
       
  2098 	CActiveScheduler::Stop();
       
  2099 	return KErrNone;
       
  2100 	}
       
  2101 
       
  2102 
       
  2103 //
       
  2104 
       
  2105 CContactsClient* CBackupTestFramework::NewDatabaseClientL(const TDesC& aDatabaseName, TBool aCloseForBackup)
       
  2106 	{
       
  2107 	test.Printf(_L("open database \"%S\"\n\r"), &aDatabaseName);
       
  2108 
       
  2109 	// open a Contacts Model client for test case
       
  2110 	CContactsClient* newClient = CContactsClient::NewLC(aDatabaseName, this, *this, aCloseForBackup);
       
  2111 	CleanupStack::Check(newClient);
       
  2112 
       
  2113 	// remember for later cleanup
       
  2114 	iContactClientSession.AppendL(newClient);
       
  2115 	CleanupStack::Pop(newClient);
       
  2116 	return newClient;
       
  2117 	}
       
  2118 
       
  2119 
       
  2120 void CBackupTestFramework::ContactsBackupRestoreObserver(CContactsClient& aContactClient, const TBool aRestore, const TBool aStart)
       
  2121 	{
       
  2122 	iClientNotifications ++;
       
  2123 	
       
  2124 	// test case should respond to Backup / Restore notification ?
       
  2125 	if (aStart && aContactClient.IsCloseForBackup())
       
  2126 		{
       
  2127 		test.Next(aRestore ? _L("Restore start notification - closing client") : _L("Backup start notification - closing client"));
       
  2128 		aContactClient.CloseViews();
       
  2129 		aContactClient.StartActiveClose();	
       
  2130 		}
       
  2131 	}
       
  2132 
       
  2133 
       
  2134 
       
  2135 /*
       
  2136  Contact Views become EUnavailable when Backup or Restore start,
       
  2137  if the View is kept open it becomes EReady again afterwards.
       
  2138  */
       
  2139 void CBackupTestFramework::HandleContactViewEvent(const CContactViewBase& /*aView*/,const TContactViewEvent& aEvent)
       
  2140 	{
       
  2141 	if (aEvent.iEventType == TContactViewEvent::EUnavailable)
       
  2142 		{
       
  2143 		iViewUnavailableCount++;
       
  2144 		// reset Ready count for end of Backup or Restore
       
  2145 		iViewReadyCount = 0;
       
  2146 		}
       
  2147 	else if (aEvent.iEventType == TContactViewEvent::EReady)
       
  2148 		{
       
  2149 		iViewReadyCount++;
       
  2150 		}
       
  2151 	}
       
  2152 
       
  2153 
       
  2154 
       
  2155 
       
  2156 //
       
  2157 
       
  2158 
       
  2159 LOCAL_C void TestCondition(TBool aCondition, TInt aLineNumber)
       
  2160 	{
       
  2161 	// if the test is about to fail, cleanup files first
       
  2162 	if (!aCondition)
       
  2163 		{
       
  2164 		CBackupTestConductor::DeleteDatabases();
       
  2165 		}
       
  2166 	test.operator()(aCondition, aLineNumber);
       
  2167 	}
       
  2168 
       
  2169 
       
  2170 LOCAL_C void DoMainL(CActiveScheduler* scheduler)
       
  2171     {
       
  2172 	CActiveScheduler::Install(scheduler);
       
  2173 
       
  2174 	CBackupTestConductor* tester = CBackupTestConductor::NewL();
       
  2175 	CleanupStack::PushL(tester);
       
  2176 
       
  2177  	tester->RunTestsL();
       
  2178 	CleanupStack::PopAndDestroy(tester); 
       
  2179 	}
       
  2180 /**
       
  2181 
       
  2182 @SYMTestCaseID     PIM-T-CNT-BACKUP-0001
       
  2183 
       
  2184 */
       
  2185 
       
  2186 GLDEF_C TInt E32Main()
       
  2187 	{
       
  2188 	__UHEAP_MARK;
       
  2189 	test.Title();
       
  2190 	test.Start(_L("SYMTestCaseID:PIM-T-CNT-BACKUP-0001 Contacts Backup and Restore"));
       
  2191 	CActiveScheduler* scheduler = new CActiveScheduler;
       
  2192 	test(scheduler != NULL);
       
  2193 	CTrapCleanup* theCleanup = CTrapCleanup::New();
       
  2194 	User::LeaveIfError(TheFsSession.Connect());
       
  2195 
       
  2196 	test.Printf(_L("SECURE_DATA mode\r\n"));
       
  2197 	User::After(KWaitForSystemToSettle);
       
  2198 
       
  2199 	TRAPD(ret,DoMainL(scheduler));
       
  2200 	if (ret != KErrNone)
       
  2201 		{
       
  2202 		test.Printf(_L("Something in the test left with error code %d\n"), ret);
       
  2203 		}
       
  2204 	TEST_CONDITION(ret == KErrNone);
       
  2205 
       
  2206 	TheFsSession.Close();
       
  2207 	delete theCleanup;	
       
  2208 	delete scheduler;
       
  2209 	test.End();
       
  2210 	test.Close();
       
  2211 	__UHEAP_MARKEND;
       
  2212 	return(KErrNone);
       
  2213     }