phonebookengines/contactsmodel/tsrc/Integration/CntPerfTest/src/ContactViewTest.cpp
changeset 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2006-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 /**
       
    17  @file
       
    18  @publishedAll
       
    19  @released
       
    20 */
       
    21 
       
    22 #include <test/testexecutelog.h>
       
    23 #include <cntfldst.h>
       
    24 #include <e32panic.h>
       
    25 #include <e32cmn.h>
       
    26 #include "ClientServer.h"
       
    27 #include "ContactViewTest.h"
       
    28 #include "ViewObserver.h"
       
    29 
       
    30 _LIT(KRun1,"EmptyAll");
       
    31 _LIT(KRun2,"FullContacts");
       
    32 _LIT(KRun3,"FullView");
       
    33 _LIT(KRun4,"FullAll");
       
    34 _LIT(KRun5,"EmptyFieldset");
       
    35 _LIT(KRun6,"EmptyFieldsetView");
       
    36 _LIT(KRun7,"MissingFields");
       
    37 _LIT(KRun8,"Performance");
       
    38 _LIT(KRun9,"CompressRecover");
       
    39 _LIT(KRun10,"ConcurrentCNR");
       
    40 
       
    41 _LIT(KTest1,"Testing Empty contacts and empty view");
       
    42 _LIT(KTest2,"Testing Full contacts and empty view");
       
    43 _LIT(KTest3,"Testing Empty contacts and Full view");
       
    44 _LIT(KTest4,"Testing Full contacts and Full view");
       
    45 _LIT(KTest5,"Testing Contacts with empty fieldsets and Full view");
       
    46 _LIT(KTest6,"Testing Contacts with empty fieldsets and empty view");
       
    47 _LIT(KTest7,"Testing Contacts with 1 or more empty fields and empty view");
       
    48 _LIT(KTest8,"***Testing Empty Contacts Views performance***");
       
    49 _LIT(KTest81,"***Testing Medium Contacts Views performance***");
       
    50 _LIT(KTest82,"***Testing Full Contacts Views performance***");
       
    51 _LIT(KTest9,"Compress and recovery test");
       
    52 _LIT(KTest10,"Concurrent Compress and recovery test");
       
    53 
       
    54 static const TInt KManyPerfIters = 1;
       
    55 _LIT(KUfield,"field");
       
    56 _LIT(KMissingFields, "MissingFields");
       
    57 _LIT(KPLView, "PLView");
       
    58 _LIT(KRecover, "Recover_" );
       
    59 
       
    60 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
    61 _LIT(KContactsDB,"c:\\private\\10003A73\\Contacts.cdb");
       
    62 #else
       
    63 _LIT(KContactsDB,"C:\\private\\10003A73\\SQLite__Contacts.cdb");
       
    64 #endif
       
    65 
       
    66 _LIT(KPersistenceDB,"c:\\private\\10273EFB\\Contacts.cdb");
       
    67 
       
    68 #define ALLPRINT( x )	INFO_PRINTF1( x );\
       
    69 						RDebug::Print( x );
       
    70 
       
    71 #define ALLPRINT2( x, y )	INFO_PRINTF2( x, y );\
       
    72 							RDebug::Print( x, y );
       
    73 
       
    74 
       
    75 #ifdef _WINS
       
    76 #define KMaxTimeAllowed	500 //seconds, minimum performance
       
    77 #else
       
    78 #define KMaxTimeAllowed	10000 //hardware is slower
       
    79 #endif
       
    80 
       
    81 _LIT(KStars,"***************");
       
    82 _LIT(KStartRecover,"StartRecover");
       
    83 _LIT(KEndRecover,"EndRecover");
       
    84 _LIT(KStartCompress,"StartCompress");
       
    85 _LIT(KEndCompress,"EndCompress");
       
    86 
       
    87 //Number of contacts
       
    88 CContactViewTest::CContactViewTest(CCntPerfServer &aParent):CCntBaseStep(10,aParent),
       
    89 															iFullness(EBlank),
       
    90 															iPerformanceContacts( EFew )
       
    91 	{
       
    92 	// Call base class method to set up the human readable name for logging
       
    93 	SetTestStepName(SharedConstants::KContactViewTest);
       
    94 	}
       
    95 
       
    96 void CContactViewTest::PreTestL()
       
    97 	{
       
    98 	iSemaphore.CreateLocal( 0 );
       
    99 	iSemaphore2.CreateLocal( 0 );
       
   100 	const TDesC &KSection = ConfigSection();
       
   101 	iPLView = ( KSection.Find( KPLView ) == 0 ); // section has to start with plview
       
   102 	iRecoverFlag = ( KSection.Find( KRecover ) == 0 );
       
   103 	}
       
   104 
       
   105 TVerdict CContactViewTest::doTestStepPostambleL()
       
   106 	{
       
   107 	iSemaphore.Close();
       
   108 	EndRecoverL();
       
   109 	EndCompressL();
       
   110 	return CCntBaseStep::doTestStepPostambleL();
       
   111 	}
       
   112 
       
   113 void CContactViewTest::CleanupL()
       
   114 	{
       
   115 	if( iObserver )
       
   116 		{
       
   117 		iObserver->CleanView();
       
   118 		CLEAR( iObserver );
       
   119 		}
       
   120 
       
   121     CLEAR( iContactsDatabase );
       
   122 	CLEAR( iContactsDatabase2 );
       
   123 	if( iPLView )
       
   124 		{
       
   125 		CLEAR(iPersistence);
       
   126 		if( iPimServ )
       
   127 			{
       
   128 			iPimServ->DeleteFileL(KPersistenceDB);
       
   129 			iPimServ->Close();
       
   130 			CLEAR(iPimServ);
       
   131 			}
       
   132 		}
       
   133 	if( iSortOrder )
       
   134 		{
       
   135 		iSortOrder->Close();
       
   136 		CLEAR(iSortOrder);
       
   137 		}
       
   138 	CCntBaseStep::Cleanup();
       
   139 	}
       
   140 
       
   141 void CContactViewTest::InitializeL()
       
   142 	{
       
   143 	CCntBaseStep::InitializeL();
       
   144 	if( iPLView )
       
   145 		{
       
   146 		iPimServ = new(ELeave) RPIMTestServer();
       
   147 		User::LeaveIfError(iPimServ->Connect());
       
   148 		User::LeaveIfError( iParent->Fs().Connect() );
       
   149 		}
       
   150 	OpenPersistanceL();//opens iContactsDatabase2
       
   151 	iSortOrder = new(ELeave) RContactViewSortOrder();
       
   152 	iObserver = CViewObserver::NewLC( *this, &CContactViewTest::AllEmptyTestL );// first test to run
       
   153 	CleanupStack::Pop( iObserver );
       
   154 	this->Logger().HtmlLogger().ShareAuto();
       
   155 	iObserver->Activate();
       
   156 	}
       
   157 
       
   158 TVerdict CContactViewTest::doTestStepL()
       
   159 	{
       
   160 
       
   161 	void (CContactViewTest::*mainFunc)() = NULL;
       
   162 
       
   163 	const TDesC &run = ConfigSection();
       
   164 
       
   165 	if( run.Find( KRun1 ) > KErrNotFound )
       
   166 		{
       
   167 		mainFunc = &CContactViewTest::AllEmptyTestL;
       
   168 		}
       
   169 	else if( run.Find( KRun2 ) > KErrNotFound )
       
   170 		{
       
   171 		mainFunc = &CContactViewTest::ViewEmptyTestL;
       
   172 		}
       
   173 	else if( run.Find( KRun3 ) > KErrNotFound )
       
   174 		{
       
   175 		mainFunc = &CContactViewTest::ContactEmptyTestL;
       
   176 		}
       
   177 	else if( run.Find( KRun4 ) > KErrNotFound )
       
   178 		{
       
   179 		mainFunc = &CContactViewTest::AllFullTestL;
       
   180 		}
       
   181 	else if( run.Find( KRun5 ) > KErrNotFound )
       
   182 		{
       
   183 		mainFunc = &CContactViewTest::EmptyFieldsetTestL;
       
   184 		}
       
   185 	else if( run.Find( KRun6 ) > KErrNotFound )
       
   186 		{
       
   187 		mainFunc = &CContactViewTest::EmptyFieldsetSortTestL;
       
   188 		}
       
   189 	else if( run.Find( KRun7 ) > KErrNotFound )
       
   190 		{
       
   191 		mainFunc = &CContactViewTest::MissingFieldTestL;
       
   192 		}
       
   193 	else if( run.Find( KRun8 ) > KErrNotFound )
       
   194 		{
       
   195 		mainFunc = &CContactViewTest::PerformanceTestL;
       
   196 		}
       
   197 	else if( run.Find( KRun9 ) > KErrNotFound )
       
   198 		{
       
   199 		mainFunc = &CContactViewTest::RecoverCompressTestL; // this test does not apply to the SQLite version of contacts
       
   200 		}
       
   201 	else if( run.Find( KRun10 ) > KErrNotFound )
       
   202 		{
       
   203 		mainFunc = &CContactViewTest::ConcurrentRecoverCompressTestL; // this test does not apply to the SQLite version of contacts
       
   204 		}
       
   205 	else
       
   206 		{
       
   207 		MissngTestPanic();
       
   208 		}
       
   209 
       
   210 	__UHEAP_MARK;
       
   211 
       
   212 	iRemote = EFalse;
       
   213 	ALLPRINT(KStars);
       
   214 	if( iPLView )
       
   215 		{
       
   216 		_LIT(KPersistance,"Initialising LocalView on Persistence Layers tests");
       
   217 		ALLPRINT(KPersistance);
       
   218 		}
       
   219 	else
       
   220 		{
       
   221 		_LIT(KLocalViewCS,"Initialising LocalView on Client/Server tests");
       
   222 		ALLPRINT(KLocalViewCS);
       
   223 		}
       
   224 	ALLPRINT(KStars);
       
   225 	InitializeL();
       
   226 	iObserver->iMainFunc = mainFunc;
       
   227 	CActiveScheduler::Start();
       
   228 
       
   229 	if( !iPLView )
       
   230 		{
       
   231 		ALLPRINT(KStars);
       
   232 		_LIT(KRemoteViewCS,"Initialising RemoteView on Client/Server tests");
       
   233 		ALLPRINT(KRemoteViewCS);
       
   234 		ALLPRINT(KStars);
       
   235 		iRemote = ETrue;
       
   236         // Ensure that the recover is deleted before another one is created.
       
   237         EndRecoverL();
       
   238 		ResetDatabaseL(10);
       
   239 		iObserver->iMainFunc = mainFunc;// test to run
       
   240 		iObserver->iDoFunc = NULL;
       
   241 		iObserver->CleanView();
       
   242 		iObserver->Activate();
       
   243 		CActiveScheduler::Start();
       
   244 		}
       
   245 
       
   246 	if( !iPLView )
       
   247 		{
       
   248 		ALLPRINT(KStars);
       
   249 		_LIT(KRemoteViewCS,"Initialising RemoteView on Client/Server tests");
       
   250 		ALLPRINT(KRemoteViewCS);
       
   251 		ALLPRINT(KStars);
       
   252 		iRemote = ETrue;
       
   253 		ResetDatabaseL(10);
       
   254 		iObserver->iMainFunc = mainFunc;// test to run
       
   255 		iObserver->iDoFunc = NULL;
       
   256 		iObserver->CleanView();
       
   257 		iObserver->Activate();
       
   258 		CActiveScheduler::Start();
       
   259 		}
       
   260 
       
   261 		CleanupL();
       
   262 		__UHEAP_MARKEND;
       
   263 
       
   264 	return TestStepResult();
       
   265 	}
       
   266 
       
   267 /**
       
   268 create local view with empty sortorder,
       
   269 database is contains ten empty contacts
       
   270 */
       
   271 void CContactViewTest::AllEmptyTestL()
       
   272 	{
       
   273 	ALLPRINT(KTest1);
       
   274 	ResetSortL();
       
   275 	iObserver->SetView( CreateViewL() );
       
   276 	iObserver->iMainFunc = NULL;
       
   277 	iObserver->iDoFunc = &CContactViewTest::doAllEmptyTestL;
       
   278 	}
       
   279 
       
   280 /**
       
   281 check that local view contains 10 contacts, each containing 1 empty field
       
   282 */
       
   283 void CContactViewTest::doAllEmptyTestL(const CContactViewBase &aLView)
       
   284 	{
       
   285 	ClosePersistanceL(ETrue);
       
   286 	TInt length = aLView.CountL();
       
   287 	TInt manyfields = 0;
       
   288 	TInt i;
       
   289 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
   290 
       
   291 	for(i = 0; i < length; ++i)
       
   292 		{
       
   293 		manyfields = aLView.ContactAtL(i).FieldCount();
       
   294 		INTCOMPARE( manyfields , ==,  1,  i, 0 );
       
   295 		SINGLECOMPARE(  CheckViewContact( aLView.ContactAtL(i), KNullDesC ),  i ,  0 );
       
   296 		}
       
   297 	iObserver->iMainFunc = NULL;
       
   298 	iObserver->iDoFunc = NULL;
       
   299 	iObserver->CleanView();
       
   300 	}
       
   301 
       
   302 /**
       
   303 create local view with empty sortorder,
       
   304 database contains 10 contacts with all fields set to preset values
       
   305 */
       
   306 void CContactViewTest::ViewEmptyTestL()
       
   307 	{
       
   308 	ALLPRINT(KTest2);
       
   309 	ClosePersistanceL(EFalse);
       
   310 	ResetDatabaseL();
       
   311 	for(TInt i = 0; i < iContacts; ++i)
       
   312 		{
       
   313 		const TContactItemId cid = iIterate->NextL();
       
   314 		OpenL(cid, *iViewAll);
       
   315 		SetContactL();
       
   316 		CommitL();
       
   317 		}
       
   318 	ResetSortL();
       
   319 	OpenPersistanceL();
       
   320 	iObserver->SetView( CreateViewL() );
       
   321 	iObserver->iMainFunc = NULL;
       
   322 	iObserver->iDoFunc = &CContactViewTest::doViewEmptyTestL;
       
   323 	}
       
   324 
       
   325 /**
       
   326 check that local view contains 10 contacts, each containing 1 empty field
       
   327 */
       
   328 void CContactViewTest::doViewEmptyTestL(const CContactViewBase &aLView)
       
   329 	{
       
   330 	TInt length = aLView.CountL();
       
   331 	TInt manyfields = 0;
       
   332 	TInt i;
       
   333 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
   334 	iIterate->Reset();
       
   335 	for(i = 0; i < length; ++i)
       
   336 		{
       
   337 		const CViewContact &vcontact = aLView.ContactAtL(i);
       
   338 		manyfields = vcontact.FieldCount();
       
   339 		INTCOMPARE( manyfields , ==,  1,  i, 0 );
       
   340 		SINGLECOMPARE(  CheckViewContact( vcontact, GetFieldL(1) ),  i,  0 );
       
   341 		}
       
   342 	iObserver->iMainFunc = NULL;
       
   343 	iObserver->iDoFunc = NULL;
       
   344 	iObserver->CleanView();
       
   345 	}
       
   346 
       
   347 /**
       
   348 create local view with sortorder containing all (existing) uids,
       
   349 database contains 10 empty contacts
       
   350 */
       
   351 void CContactViewTest::ContactEmptyTestL()
       
   352 	{
       
   353 	ALLPRINT(KTest3);
       
   354 	ClosePersistanceL(EFalse);
       
   355 	ResetDatabaseL();
       
   356 	ResetSortL();
       
   357 
       
   358 	TInt length = iExistingUidsArray->Count();
       
   359 	TInt i = 0;
       
   360 	for( ; i < length ; ++i)
       
   361 		{
       
   362 		iSortOrder->AppendL( TUid::Uid( (*iExistingUidsArray)[i] ) );
       
   363 		}
       
   364 	OpenPersistanceL();
       
   365 	iObserver->SetView( CreateViewL() );
       
   366 	iObserver->iMainFunc = NULL;
       
   367 	iObserver->iDoFunc = &CContactViewTest::doContactEmptyTestL;
       
   368 	}
       
   369 
       
   370 void CContactViewTest::doContactEmptyTestL(const CContactViewBase &aLView)
       
   371 	{
       
   372 	TInt length = aLView.CountL();
       
   373 	TInt arrayLength = iExistingUidsArray->Count();
       
   374 	TInt manyfields = 0;
       
   375 	TInt i;
       
   376 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
   377 
       
   378 	for(i = 0; i < length; ++i)
       
   379 		{
       
   380 		const CViewContact &vcontact = aLView.ContactAtL(i);
       
   381 		manyfields = vcontact.FieldCount();
       
   382 		INTCOMPARE( manyfields , ==,  arrayLength,  i, 0 );
       
   383 		SINGLECOMPARE(  CheckViewContact( vcontact, KNullDesC ),  i ,  0 );
       
   384 		}
       
   385 	iObserver->iMainFunc = NULL;
       
   386 	iObserver->iDoFunc = NULL;
       
   387 	iObserver->CleanView();
       
   388 	}
       
   389 
       
   390 void CContactViewTest::ResetDatabaseL(const TInt aMany)
       
   391 	{
       
   392 	iIterate->Reset();
       
   393 	for(TInt i = 0; i < iContacts; ++i)
       
   394 		{
       
   395 		const TContactItemId cid = iIterate->NextL();
       
   396 		iIterate->RemoveL(cid);
       
   397 		}
       
   398 	if( aMany > 0 )
       
   399 		{
       
   400 		iContacts = aMany;
       
   401 		}
       
   402 	CLEAR( iContactsDatabase );
       
   403 	CLEAR( iContactsDatabase2 );
       
   404 	iContactsDatabase = CContactDatabase::ReplaceL();
       
   405 
       
   406 	iContactsDatabase2 = CContactDatabase::OpenL( CContactDatabase::EMultiThread );
       
   407 	AddEmptyContactsL(iContacts);
       
   408 	iIterate->Reset();
       
   409 	}
       
   410 
       
   411 TBool CContactViewTest::CheckViewContact(const CViewContact &aVContact, const TDesC &aVal)
       
   412 	{
       
   413 	TBool SingleCheckResult = EFalse;
       
   414 	TBool OverallCheckResult = ETrue;
       
   415 	TInt length = aVContact.FieldCount();
       
   416 	TInt i = 0;
       
   417 	for(; i < length; ++i)
       
   418 		{
       
   419 		SingleCheckResult = STRINGCOMPARE( aVContact.Field(i), ==, aVal, i, 0);
       
   420 		OverallCheckResult = OverallCheckResult && SingleCheckResult;
       
   421 		}
       
   422 	return ( OverallCheckResult && length > 0 );
       
   423 	}
       
   424 
       
   425 /**
       
   426 create local view with sortorder containing all (existing) uids,
       
   427 database contains 10 contacts with all fields set to preset values
       
   428 */
       
   429 void CContactViewTest::AllFullTestL()
       
   430 	{
       
   431 	ALLPRINT(KTest4);
       
   432 	ClosePersistanceL(EFalse);
       
   433 	ResetDatabaseL();
       
   434 	TInt i;
       
   435 	for(i = 0; i < iContacts; ++i)
       
   436 		{
       
   437 		const TContactItemId cid = iIterate->NextL();
       
   438 		OpenL(cid, *iViewAll);
       
   439 		SetContactL();
       
   440 		CommitL();
       
   441 		}
       
   442 
       
   443 
       
   444 	TInt length = iExistingUidsArray->Count();
       
   445 	ResetSortL();
       
   446 	for(i = 0 ; i < length ; ++i)
       
   447 		{
       
   448 		iSortOrder->AppendL( TUid::Uid( (*iExistingUidsArray)[i] ) );
       
   449 		}
       
   450 
       
   451 	OpenPersistanceL();
       
   452 	iObserver->SetView( CreateViewL() );
       
   453 	iObserver->iMainFunc = NULL;
       
   454 	iObserver->iDoFunc = &CContactViewTest::doAllFullTestL;
       
   455 	}
       
   456 
       
   457 /**
       
   458 check that local view contains 10 contacts, each containing as many fields
       
   459 as there are uids (114), all field that have a corresponding uid should be
       
   460 non-empty
       
   461 */
       
   462 void CContactViewTest::doAllFullTestL(const CContactViewBase &aLView)
       
   463 	{
       
   464 	ClosePersistanceL(ETrue);
       
   465 	TInt length = aLView.CountL();
       
   466 	TInt arrayLength = iExistingUidsArray->Count();
       
   467 	TInt manyfields = 0;
       
   468 	TInt i;
       
   469 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
   470 
       
   471 	for(i = 0; i < length; ++i)
       
   472 		{
       
   473 		const CViewContact &vcontact = aLView.ContactAtL(i);
       
   474 		manyfields = vcontact.FieldCount();
       
   475 		INTCOMPARE( manyfields , ==,  arrayLength,  i, 0 );
       
   476 		SINGLECOMPARE(  CheckViewContactL( vcontact ),  i ,  0 );
       
   477 		}
       
   478 	iObserver->iMainFunc = NULL;
       
   479 	iObserver->iDoFunc = NULL;
       
   480 	iObserver->CleanView();
       
   481 	}
       
   482 
       
   483 
       
   484 void CContactViewTest::SetContactL()
       
   485 	{
       
   486 	TInt length = iFields->Count();
       
   487 	TInt i;
       
   488 	for( i = 0; i < length; ++i)
       
   489 		{
       
   490 		SetFieldL(*iFields, i, (*iFields)[i].Label());
       
   491 		}
       
   492 	}
       
   493 
       
   494 /**
       
   495 Check that contents of all fields in view contact equal corresponding fields in contact item
       
   496 */
       
   497 TBool CContactViewTest::CheckViewContactL(const CViewContact &aVContact)
       
   498 	{
       
   499 	TBool OverallCheckResult = EFalse;
       
   500 
       
   501 	//retrieve contact item id of view contact
       
   502 	TContactItemId cid = aVContact.Id();
       
   503 
       
   504 	//open contact item
       
   505 	ReadL(cid, *iViewAll);
       
   506 	TInt length = aVContact.FieldCount();
       
   507 	TInt i = 0;
       
   508 	for(; i < length; ++i)
       
   509 		{
       
   510 		OverallCheckResult = EFalse;
       
   511 		//find first field in contact item that equals uid (of view contact field) stored in sort order
       
   512 		TInt pos = iFields->Find((*iSortOrder)[i]);
       
   513 		TDesC *ContactField = NULL;
       
   514 
       
   515 		if(pos < 0)
       
   516 			{
       
   517 			ContactField = const_cast<TDesC *>(&KNullDesC);
       
   518 			}
       
   519 		else
       
   520 			{
       
   521 			//converts contents of field - matching the view contact uid - to a string
       
   522 			ContactField = FieldStringLC( (*iFields)[pos] );
       
   523 			}
       
   524 		//check that conents of contact item field equal conents of corresponing view contact field
       
   525 		OverallCheckResult = STRINGCOMPARE( aVContact.Field(i), ==, *ContactField, i, 0 );
       
   526 
       
   527 		if(pos > -1)
       
   528 			{
       
   529 			CleanupStack::PopAndDestroy( ContactField );
       
   530 			}
       
   531 
       
   532 		//if view contact field doesnt equal contact item field, fail test
       
   533 		if( !OverallCheckResult )
       
   534 			{
       
   535 			break;//test failed
       
   536 			}
       
   537 		}
       
   538 	Close();
       
   539 
       
   540 	return OverallCheckResult;
       
   541 	}
       
   542 /**
       
   543 cleanup sort order, create new empty sort order
       
   544 */
       
   545 void CContactViewTest::ResetSortL()
       
   546 	{
       
   547 	iSortOrder->Close();
       
   548 	CLEAR(iSortOrder);
       
   549 	iSortOrder = new(ELeave) RContactViewSortOrder();
       
   550 	}
       
   551 
       
   552 /**
       
   553 converts contents of a contact item field to a string
       
   554 */
       
   555 HBufC *CContactViewTest::FieldStringLC(const CContactItemField &aField)
       
   556 	{
       
   557 	HBufC *buf = NULL;
       
   558 	if( aField.IsTemplateLabelField() )
       
   559 		{
       
   560 		buf = HBufC::NewLC(0);
       
   561 		return buf;
       
   562 		}
       
   563 	switch(aField.StorageType())
       
   564 		{
       
   565 		case KStorageTypeText:
       
   566 			{
       
   567 			buf = HBufC::NewLC(aField.Label().Length()+2);
       
   568 			TPtr ptr = buf->Des();
       
   569 			ptr.Append( aField.Label() );
       
   570 			}
       
   571 		break;
       
   572 		case KStorageTypeContactItemId:
       
   573 			{
       
   574 			buf = HBufC::NewLC(20);//assume cid < 20 digits
       
   575 			TContactItemId cid = aField.AgentStorage()->Value();
       
   576 			TPtr ptr = buf->Des();
       
   577 			ptr.AppendNum(cid);
       
   578 			}
       
   579 		break;
       
   580 		case KStorageTypeStore:
       
   581 		case KStorageTypeDateTime:
       
   582 			{
       
   583 			buf = HBufC::NewLC(0);//views dont care about date/time or binary fields
       
   584 			}
       
   585 		break;
       
   586 		default:
       
   587 			{
       
   588 			_LIT(KCCVTest,"CCVTest");
       
   589 			User::Panic(KCCVTest , EInvariantFalse );
       
   590 			}
       
   591 		break;
       
   592 		}
       
   593 	return buf;
       
   594 
       
   595 	}
       
   596 
       
   597 /**
       
   598 create local view with sortorder containing all (existing) uids,
       
   599 database contains 10 contacts with a fieldset containing no fields.
       
   600 */
       
   601 void CContactViewTest::EmptyFieldsetTestL()
       
   602 	{
       
   603 	ALLPRINT(KTest5);
       
   604 	ClosePersistanceL(EFalse);
       
   605 	ResetDatabaseL();
       
   606 	TInt i;
       
   607 	for(i = 0; i < iContacts; ++i)
       
   608 		{
       
   609 		const TContactItemId cid = iIterate->NextL();
       
   610 		OpenL(cid, *iViewAll);
       
   611 		iFields->Reset();
       
   612 		CommitL();
       
   613 		}
       
   614 
       
   615 
       
   616 	TInt length = iExistingUidsArray->Count();
       
   617 	ResetSortL();
       
   618 	for(i = 0 ; i < length ; ++i)
       
   619 		{
       
   620 		iSortOrder->AppendL( TUid::Uid( (*iExistingUidsArray)[i] ) );
       
   621 		}
       
   622 
       
   623 	OpenPersistanceL();
       
   624 	iObserver->SetView( CreateViewL() );
       
   625 	iObserver->iMainFunc = NULL;
       
   626 	iObserver->iDoFunc = &CContactViewTest::doEmptyFieldsetTestL;
       
   627 	}
       
   628 
       
   629 /**
       
   630 check that local view contains 10 contacts, each containing as many fields
       
   631 as there are uids (114), all field should be empty. create new view with
       
   632 empty sortorder
       
   633 */
       
   634 void CContactViewTest::doEmptyFieldsetTestL(const CContactViewBase &aLView)
       
   635 	{
       
   636 	TInt length = aLView.CountL();
       
   637 	TInt arrayLength = iExistingUidsArray->Count();
       
   638 	TInt manyfields = 0;
       
   639 	TInt i;
       
   640 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
   641 
       
   642 	for(i = 0; i < length; ++i)
       
   643 		{
       
   644 		const CViewContact &vcontact = aLView.ContactAtL(i);
       
   645 		manyfields = vcontact.FieldCount();
       
   646 		INTCOMPARE( manyfields , ==,  arrayLength,  i, 0 );
       
   647 		SINGLECOMPARE(  CheckViewContact( aLView.ContactAtL(i), KNullDesC ),  i,  0 );
       
   648 		}
       
   649 	iObserver->iMainFunc = NULL;
       
   650 	iObserver->iDoFunc = NULL;
       
   651 	iObserver->CleanView();
       
   652 	}
       
   653 
       
   654 /**
       
   655 Delete all fields from all contacts
       
   656 */
       
   657 void CContactViewTest::EmptyFieldsetSortTestL()
       
   658 	{
       
   659 	ALLPRINT(KTest6);
       
   660 	ClosePersistanceL(EFalse);
       
   661 	ResetDatabaseL();
       
   662 	TInt i;
       
   663 	for(i = 0; i < iContacts; ++i)
       
   664 		{
       
   665 		const TContactItemId cid = iIterate->NextL();
       
   666 		OpenL(cid, *iViewAll);
       
   667 		//deletes all fields
       
   668 		iFields->Reset();
       
   669 		CommitL();
       
   670 		}
       
   671 
       
   672 	ResetSortL();
       
   673 	OpenPersistanceL();
       
   674 	iObserver->SetView( CreateViewL() );
       
   675 	iObserver->iMainFunc = NULL;
       
   676 	iObserver->iDoFunc = &CContactViewTest::doEmptyFieldsetSortTestL;
       
   677 	}
       
   678 
       
   679 /**
       
   680 Check that view contains 1 field for each contact and that field is null
       
   681 */
       
   682 void CContactViewTest::doEmptyFieldsetSortTestL(const CContactViewBase &aLView)
       
   683 	{
       
   684 	TInt length = aLView.CountL();
       
   685 	TInt manyfields = 0;
       
   686 	TInt i;
       
   687 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
   688 
       
   689 	for(i = 0; i < length; ++i)
       
   690 		{
       
   691 		const CViewContact &vcontact = aLView.ContactAtL(i);
       
   692 		manyfields = vcontact.FieldCount();
       
   693 		INTCOMPARE( manyfields , ==,  1,  i, 0 );
       
   694 		SINGLECOMPARE(  CheckViewContact( aLView.ContactAtL(i), KNullDesC ),  i ,  0 );
       
   695 		}
       
   696 	iObserver->iMainFunc = NULL;
       
   697 	iObserver->iDoFunc = NULL;
       
   698 	iObserver->CleanView();
       
   699 	}
       
   700 
       
   701 /**
       
   702 Set all fields of all contacts to contain their own label string
       
   703 use empty sort order
       
   704 */
       
   705 void CContactViewTest::MissingFieldTestL()
       
   706 	{
       
   707 	ALLPRINT(KTest7);
       
   708 	ClosePersistanceL(EFalse);
       
   709 	ResetDatabaseL();
       
   710 	for(TInt i = 0; i < iContacts; ++i)
       
   711 		{
       
   712 		const TContactItemId cid = iIterate->NextL();
       
   713 		OpenL(cid, *iViewAll);
       
   714 		SetContactL();
       
   715 		CommitL();
       
   716 		}
       
   717 	ResetSortL();
       
   718 	OpenPersistanceL();
       
   719 	iObserver->SetView( CreateViewL() );
       
   720 	iObserver->iMainFunc = NULL;
       
   721 	iObserver->iDoFunc = &CContactViewTest::doMissingFieldTestL;
       
   722 	}
       
   723 
       
   724 /**
       
   725 check that a field is generated for each view contact and that field matches list of missing of fields
       
   726 remove generated field from contact item and repopulate contact view repeat until view contact field is null
       
   727 */
       
   728 void CContactViewTest::doMissingFieldTestL(const CContactViewBase &aLView)
       
   729 	{
       
   730 	ClosePersistanceL(ETrue);
       
   731 	TBool end = ETrue;
       
   732 	TInt length = aLView.CountL();
       
   733 	TInt manyfields = 0;
       
   734 	TInt i;
       
   735 	//iterate through ini file, that stores list of expected field in order
       
   736 	const TPtrC mfield = GetNextFieldL();
       
   737 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
   738 	iIterate->Reset();
       
   739 
       
   740 	for(i = 0; i < length; ++i)
       
   741 		{
       
   742 		const TInt loc = aLView.FindL(iIterate->NextL());
       
   743 		const CViewContact &vcontact = aLView.ContactAtL( loc );
       
   744 		manyfields = vcontact.FieldCount();
       
   745 		const TPtrC vfield = vcontact.Field(0);
       
   746 		INTCOMPARE(  manyfields , ==,  1,  i, 0 );
       
   747 		
       
   748 		//compare view contact field to field stored in ini file
       
   749 		STRINGCOMPARE( vfield, ==, mfield, i, 0 );
       
   750 		//if view contact field is null, end test
       
   751 		end = end && (vfield == KNullDesC);
       
   752 		//find view contact field in contact item and set to null, if field not found, end test
       
   753 		end = end || !FindAndClearFieldL( vcontact.Id(), vfield );
       
   754 		}
       
   755 
       
   756 
       
   757 	if( end )
       
   758 		{
       
   759 		iObserver->iMainFunc = NULL;
       
   760 		iObserver->iDoFunc = NULL;
       
   761 		iObserver->CleanView();
       
   762 		}
       
   763 	else
       
   764 		{
       
   765 		//if test not complete, reset view and repopulate
       
   766 		OpenPersistanceL();
       
   767 		iObserver->CleanView();
       
   768 		iObserver->SetView( CreateViewL() );
       
   769 		}
       
   770 	}
       
   771 
       
   772 /**
       
   773 populate database with full, medium or empty contacts
       
   774 */
       
   775 void CContactViewTest::PerformanceTestL()
       
   776 	{
       
   777 	ClosePersistanceL( EFalse );
       
   778 	ResetDatabaseL(iPerformanceContacts);
       
   779 
       
   780 	//populate contacts
       
   781 	if( EMedium == iFullness )
       
   782 		{
       
   783 		if( EFew == iPerformanceContacts )
       
   784 			{
       
   785 			ALLPRINT(KTest81);
       
   786 			}
       
   787 		TInt i = 0;
       
   788 		for(; i < iContacts; ++i)
       
   789 			{
       
   790 			const TContactItemId cid = iIterate->NextL();
       
   791 			OpenL(cid, *iViewAll);
       
   792 			SetMediumL();
       
   793 			CommitL();
       
   794 			}
       
   795 		}
       
   796 	else if( EFull == iFullness )
       
   797 		{
       
   798 		if( EFew == iPerformanceContacts )
       
   799 			{
       
   800 			ALLPRINT(KTest82);
       
   801 			}
       
   802 		TInt i = 0;
       
   803 		for(; i < iContacts; ++i)
       
   804 			{
       
   805 			const TContactItemId cid = iIterate->NextL();
       
   806 			OpenL(cid, *iViewAll);
       
   807 			SetFullL();
       
   808 			CommitL();
       
   809 			}
       
   810 		}
       
   811 	else
       
   812 		{
       
   813 		if( EFew == iPerformanceContacts )
       
   814 			{
       
   815 			ALLPRINT(KTest8);
       
   816 			}
       
   817 		}
       
   818 
       
   819 	/**
       
   820 	Print the number of contacts used for this test
       
   821 	*/
       
   822 	switch( iPerformanceContacts )
       
   823 		{
       
   824 		case EFew:
       
   825 			{
       
   826 			_LIT(KFewContacts,"Few contacts");
       
   827 			ALLPRINT( KFewContacts );
       
   828 			iPerformanceContacts = EMid;
       
   829 			break;
       
   830 			}
       
   831 		case EMid:
       
   832 			{
       
   833 			_LIT(KMidContacts,"Mid contacts");
       
   834 			ALLPRINT( KMidContacts );
       
   835 			iPerformanceContacts = EMany;
       
   836 			break;
       
   837 			}
       
   838 		case EMany:
       
   839 			{
       
   840 			_LIT(KManyContacts,"Many contacts");
       
   841 			ALLPRINT( KManyContacts );
       
   842 			iPerformanceContacts = EMax;
       
   843 			break;
       
   844 			}
       
   845 		case EMax:
       
   846 			{
       
   847 			_LIT(KMaxContacts,"Max contacts");
       
   848 			ALLPRINT( KMaxContacts );
       
   849 			iPerformanceContacts = EDone;
       
   850 			break;
       
   851 			}
       
   852 		default:
       
   853 			{
       
   854 			_LIT(KInvalidContacts,"bad performance contacts");
       
   855 			User::Panic(KInvalidContacts, 333 );
       
   856 			break;
       
   857 			}
       
   858 		}
       
   859 
       
   860 	_LIT(KStartCompact,"Start Compact");
       
   861 	ALLPRINT( KStartCompact );
       
   862 	iContactsDatabase->CompactL();//to make sure that the server is not busy doing automatic compaction
       
   863 	_LIT(KEndCompact,"End Compact");
       
   864 	ALLPRINT( KEndCompact );
       
   865 
       
   866 
       
   867 	//Create sortorder with typical uids
       
   868 	ResetSortL();
       
   869 	iSortOrder->AppendL(KUidContactFieldGivenName);
       
   870 	iSortOrder->AppendL(KUidContactFieldAdditionalName);
       
   871 	iSortOrder->AppendL(KUidContactFieldFamilyName);
       
   872 	iSortOrder->AppendL(KUidContactFieldPhoneNumber);
       
   873 
       
   874 	OpenPersistanceL();
       
   875 	_LIT(KStartView,"Start view creation");
       
   876 	ALLPRINT( KStartView );
       
   877 	//note time view is created
       
   878 	iStartC.UniversalTime();
       
   879 	iObserver->SetView( CreateViewL() );
       
   880 	//note time vie creat is completed
       
   881 	iStartP.UniversalTime();
       
   882 	_LIT(KEndView,"End view creation, wait for view to be populated");
       
   883 	ALLPRINT( KEndView );
       
   884 	iObserver->iMainFunc = NULL;
       
   885 	iObserver->iDoFunc = &CContactViewTest::doPerformanceTestL;
       
   886 	}
       
   887 
       
   888 /**
       
   889 Calculate/Print time taken to create view
       
   890 Calculate/Print time take to populate view
       
   891 */
       
   892 void CContactViewTest::doPerformanceTestL(const CContactViewBase &/*aLView*/)
       
   893 	{
       
   894 	static TInt iters = KManyPerfIters;
       
   895 	static TInt64 creation = 0;
       
   896 	static TInt64 population = 0;
       
   897 
       
   898 	_LIT(KIteration,"Iteration: %d");
       
   899 	ALLPRINT2(KIteration, iters);
       
   900 
       
   901 	//note time when view has been populated
       
   902 	iFinishP.UniversalTime();
       
   903 	//time taken to pupulate view
       
   904 	population += ( iFinishP.MicroSecondsFrom( iStartP ).Int64() );
       
   905 	//time taken to create view
       
   906 	creation += ( iStartP.MicroSecondsFrom( iStartC ).Int64() );
       
   907 
       
   908 	//if average of many iterations of view population is being measured, wait for last iteration before
       
   909 	//calculating average tiem taken to creat/populate a view
       
   910 	if(--iters <= 0)
       
   911 		{
       
   912 		iters = KManyPerfIters;
       
   913 
       
   914 		TReal creatSec = 0;
       
   915 		TReal popSec = 0;
       
   916 
       
   917 		creatSec =static_cast<TReal>(creation)/(1000000*iters);
       
   918 		popSec = static_cast<TReal>(population)/(1000000*iters);
       
   919 
       
   920 		_LIT(KCreationTime,"Creation Time: %f seconds");
       
   921 		ALLPRINT2(KCreationTime, creatSec);
       
   922 
       
   923 		_LIT(KPopulationTime,"Population time: %f seconds");
       
   924 		ALLPRINT2(KPopulationTime, popSec);
       
   925 		creation = 0;
       
   926 		population = 0;
       
   927 
       
   928 		if( EFull == iFullness )
       
   929 			{
       
   930 			iters = KManyPerfIters;
       
   931 			}
       
   932 
       
   933 		iObserver->iDoFunc = NULL;
       
   934 		iObserver->CleanView();
       
   935 
       
   936 		//time taken to create and populate view should not exceed defined limit
       
   937 		INTCOMPARE( (creatSec + popSec), <, KMaxTimeAllowed, 0, 0 );
       
   938 
       
   939 		//if empty view, change to medium view performance test
       
   940 		if( EBlank == iFullness )
       
   941 			{
       
   942 			if( EDone == iPerformanceContacts )
       
   943 				{
       
   944 				iPerformanceContacts = EFew;
       
   945 				iFullness = EMedium;
       
   946 				}
       
   947 			iObserver->iMainFunc = &CContactViewTest::PerformanceTestL;
       
   948 			iObserver->Activate();
       
   949 			}
       
   950 
       
   951 		//if medium view, change to full view performance test
       
   952 		else if( EMedium == iFullness )
       
   953 			{
       
   954 			if( EDone == iPerformanceContacts )
       
   955 				{
       
   956 				iPerformanceContacts = EFew;
       
   957 				iFullness = EFull;
       
   958 				}
       
   959 			iObserver->iMainFunc = &CContactViewTest::PerformanceTestL;
       
   960 			iObserver->Activate();
       
   961 
       
   962 			}
       
   963 		//if full view
       
   964 		else
       
   965 			{
       
   966 			//if all contact permuations done, end test
       
   967 			if( EDone == iPerformanceContacts )
       
   968 				{
       
   969 				iPerformanceContacts = EFew;
       
   970 				iFullness = EBlank;
       
   971 				iObserver->iMainFunc = NULL;
       
   972 				iObserver->CleanView();
       
   973 				}
       
   974 			else
       
   975 				{
       
   976 				//if other contact permuations availabe for full view, continue testing
       
   977 				iObserver->iMainFunc = &CContactViewTest::PerformanceTestL;
       
   978 				iObserver->Activate();
       
   979 				}
       
   980 			}
       
   981 
       
   982 		}
       
   983 	//if not final iteration
       
   984 	else
       
   985 		{
       
   986 		iObserver->iMainFunc = NULL;
       
   987 		iObserver->iDoFunc = &CContactViewTest::doPerformanceTestL;
       
   988 		//reset view
       
   989 		iObserver->CleanView();
       
   990 		iStartC.UniversalTime();
       
   991 		//recreate view
       
   992 		iObserver->SetView( CreateViewL() );
       
   993 		iStartP.UniversalTime();
       
   994 		}
       
   995 	}
       
   996 
       
   997 /**
       
   998 populate contact on first iteration
       
   999 reset view and start compress/recover
       
  1000 */
       
  1001 void CContactViewTest::RecoverCompressTestL()
       
  1002 	{
       
  1003 	ALLPRINT(KTest9);
       
  1004 	TInt i;
       
  1005 	if( !iIteration )
       
  1006 		{
       
  1007 		ResetDatabaseL();
       
  1008 
       
  1009 		for(i = 0; i < iContacts; ++i)
       
  1010 			{
       
  1011 			const TContactItemId cid = iIterate->NextL();
       
  1012 			OpenL(cid, *iViewAll);
       
  1013 			SetContactL();
       
  1014 			CommitL();
       
  1015 			}
       
  1016 		}
       
  1017 
       
  1018 	//sortorder contains all uids
       
  1019 	TInt length = iExistingUidsArray->Count();
       
  1020 	ResetSortL();
       
  1021 	for(i = 0 ; i < length ; ++i)
       
  1022 		{
       
  1023 		iSortOrder->AppendL( TUid::Uid( (*iExistingUidsArray)[i] ) );
       
  1024 		}
       
  1025 
       
  1026 	iObserver->SetView( CreateViewL() );
       
  1027 	iObserver->iMainFunc = NULL;
       
  1028 	iObserver->iDoFunc = &CContactViewTest::doRecoverCompressTestL;
       
  1029 
       
  1030 	//if first iteration start compress/recover
       
  1031 	if( !iIteration++)
       
  1032 		{
       
  1033 		if(iRecoverFlag)
       
  1034 			{
       
  1035 			StartRecoverL();
       
  1036 			}
       
  1037 		else
       
  1038 			{
       
  1039 			StartCompressL();
       
  1040 			}
       
  1041 		}
       
  1042 	}
       
  1043 
       
  1044 /**
       
  1045 if expected error generated, end compress or recover and regenerate view
       
  1046 otherwise check view is correctly populated
       
  1047 */
       
  1048 void CContactViewTest::doRecoverCompressTestL(const CContactViewBase &aLView)
       
  1049 	{
       
  1050 	//check expected generated error 
       
  1051 	if( ( ( KErrAccessDenied == iObserver->iEvent.iInt && !iRecoverFlag ) ||
       
  1052 		  ( KErrDisconnected == iObserver->iEvent.iInt && iRecoverFlag ) || iRemote)
       
  1053 		    && iIteration == 1)
       
  1054 		{
       
  1055 		//end recover / compress
       
  1056 		if(iRecoverFlag)
       
  1057 			{
       
  1058 			EndRecoverL();
       
  1059 			}
       
  1060 		else
       
  1061 			{
       
  1062 			EndCompressL();
       
  1063 			}
       
  1064 		//reset view
       
  1065 		iObserver->iMainFunc = &CContactViewTest::RecoverCompressTestL;
       
  1066 		iObserver->iDoFunc = NULL;
       
  1067 		iObserver->CleanView();
       
  1068 		iObserver->Activate();
       
  1069 		iObserver->iEvent = TContactViewEvent();
       
  1070 		return;
       
  1071 		}
       
  1072 
       
  1073 	//reset iteration
       
  1074 	iIteration = 0;
       
  1075 	TInt length = aLView.CountL();
       
  1076 	TInt arrayLength = iExistingUidsArray->Count();
       
  1077 	TInt manyfields = 0;
       
  1078 	TInt i;
       
  1079 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
  1080 
       
  1081 	//check view correctly populated
       
  1082 	for(i = 0; i < length; ++i)
       
  1083 		{
       
  1084 		const CViewContact &vcontact = aLView.ContactAtL(i);
       
  1085 		manyfields = vcontact.FieldCount();
       
  1086 		INTCOMPARE( manyfields , ==,  arrayLength,  i, 0 );
       
  1087 		SINGLECOMPARE(  CheckViewContactL( vcontact ),  i,  0 );
       
  1088 		}
       
  1089 	iObserver->iMainFunc = NULL;
       
  1090 	iObserver->iDoFunc = NULL;
       
  1091 	iObserver->CleanView();
       
  1092 	iObserver->iEvent = TContactViewEvent();
       
  1093 	}
       
  1094 
       
  1095 /**
       
  1096 start compress / recover before view is created in seperate thread
       
  1097 create view
       
  1098 end compress / recover in seperate thread
       
  1099 */
       
  1100 void CContactViewTest::ConcurrentRecoverCompressTestL()
       
  1101 	{
       
  1102 	ALLPRINT(KTest10);
       
  1103 	TInt i;
       
  1104 	if( !iIteration )
       
  1105 		{
       
  1106 		ResetDatabaseL();
       
  1107 
       
  1108 		for(i = 0; i < iContacts; ++i)
       
  1109 			{
       
  1110 			const TContactItemId cid = iIterate->NextL();
       
  1111 			OpenL(cid, *iViewAll);
       
  1112 			SetContactL();
       
  1113 			CommitL();
       
  1114 			}
       
  1115 		}
       
  1116 
       
  1117 	TInt length = iExistingUidsArray->Count();
       
  1118 	ResetSortL();
       
  1119 	for(i = 0 ; i < length ; ++i)
       
  1120 		{
       
  1121 		iSortOrder->AppendL( TUid::Uid( (*iExistingUidsArray)[i] ) );
       
  1122 		}
       
  1123 
       
  1124 	if (iThread.Handle() != RThread().Handle() )
       
  1125 		{
       
  1126 		iThread.Close();
       
  1127 		}
       
  1128 
       
  1129 	if( !iIteration )
       
  1130 		{
       
  1131 		iThread.Create( KTest10, CContactViewTest::ThreadRunL, KDefaultStackSize, NULL, this, EOwnerThread );
       
  1132 		iThread.Resume();
       
  1133 		iThread.SetPriority(EPriorityNormal);
       
  1134 		iSemaphore.Wait();//wait for recovery compress to be started
       
  1135 		}
       
  1136 
       
  1137 	TRAPD(err, iObserver->SetView(CreateViewL()));
       
  1138 	if( !iIteration )
       
  1139 		{
       
  1140 		iSemaphore2.Signal();// signal that view has been created
       
  1141 		}
       
  1142 	if( !iIteration )
       
  1143 		{
       
  1144 		iSemaphore.Wait();//wait for recovery/compress to be completed
       
  1145 		}
       
  1146 
       
  1147 	if (err == KErrNotReady)
       
  1148  		{
       
  1149 		_LIT(KCreateViewResult,"Create view leave with KErrNotReady(-18) in iIteration=%d");
       
  1150 		ALLPRINT2(KCreateViewResult, iIteration);
       
  1151 		TEST(iIteration == 0); // expect err=KErrNotReady for iIteration=0 in remote view test
       
  1152 
       
  1153 		iObserver->iMainFunc = &CContactViewTest::ConcurrentRecoverCompressTestL;
       
  1154 		iObserver->iDoFunc = NULL;
       
  1155 		iObserver->CleanView();
       
  1156 		iObserver->Activate();
       
  1157 		iObserver->iEvent = TContactViewEvent();
       
  1158 		}
       
  1159 	else
       
  1160 		{
       
  1161 		iObserver->iMainFunc = NULL;
       
  1162 		iObserver->iDoFunc = &CContactViewTest::doConcurrentRecoverCompressTestL;
       
  1163 		}
       
  1164 
       
  1165 	++iIteration;	
       
  1166 	}
       
  1167 
       
  1168 /**
       
  1169 on first iteration check that expected error is generated
       
  1170 on next iteration check that view is correctly populated
       
  1171 */
       
  1172 void CContactViewTest::doConcurrentRecoverCompressTestL(const CContactViewBase &aLView)
       
  1173 	{
       
  1174 	//check expected error generated
       
  1175 	if( ( ( KErrAccessDenied == iObserver->iEvent.iInt && !iRecoverFlag ) ||
       
  1176 		  ( KErrDisconnected == iObserver->iEvent.iInt && iRecoverFlag ) || iRemote)
       
  1177 		    && iIteration == 1)
       
  1178 		{
       
  1179 		//end recover / compress
       
  1180 		if(iRecoverFlag)
       
  1181 			{
       
  1182 			EndRecoverL();
       
  1183 			}
       
  1184 		else
       
  1185 			{
       
  1186 			EndCompressL();
       
  1187 			}
       
  1188 		//reset view
       
  1189 		iObserver->iMainFunc = &CContactViewTest::RecoverCompressTestL;
       
  1190 		iObserver->iDoFunc = NULL;
       
  1191 		iObserver->CleanView();
       
  1192 		iObserver->Activate();
       
  1193 		iObserver->iEvent = TContactViewEvent();
       
  1194 		return;
       
  1195 		}
       
  1196 		
       
  1197 	iIteration = 0;
       
  1198 	TInt length = aLView.CountL();
       
  1199 	TInt arrayLength = iExistingUidsArray->Count();
       
  1200 	TInt manyfields = 0;
       
  1201 	TInt i;
       
  1202 	INTCOMPARE( length , ==,  iContacts, 0, 0 );
       
  1203 
       
  1204 	for(i = 0; i < length; ++i)
       
  1205 		{
       
  1206 		const CViewContact &vcontact = aLView.ContactAtL(i);
       
  1207 		manyfields = vcontact.FieldCount();
       
  1208 		INTCOMPARE( manyfields , ==,  arrayLength,  i, 0 );
       
  1209 		SINGLECOMPARE(  CheckViewContactL( vcontact ),  i,  0 );
       
  1210 		}
       
  1211 	iObserver->iMainFunc = NULL;
       
  1212 	iObserver->iDoFunc = NULL;
       
  1213 	iObserver->CleanView();
       
  1214 	iObserver->iEvent = TContactViewEvent();
       
  1215 	}
       
  1216 
       
  1217 /**
       
  1218 New thread function, start recovery/compress before view creation and
       
  1219 completes recovery/compress after view creation
       
  1220 */
       
  1221 void CContactViewTest::RecoverAndCompressThreadTestL()
       
  1222 	{
       
  1223 	CContactDatabase *cntdb = CContactDatabase::OpenL();
       
  1224 	CleanupStack::PushL( cntdb );
       
  1225 
       
  1226 	if( iRecoverFlag )
       
  1227 		{
       
  1228 		// The views access the database directly and are oblivious of the state of the contacts server.
       
  1229 		// Therefore they will try and access a corrupt (damaged) database.  This leads to a DBMS panic
       
  1230 		// It is beleived that 9.1-9.4 the Contacts model has always exhibited this behavior (fixed in 9.5)
       
  1231 		#ifdef _DEBUG
       
  1232 			if( !cntdb->IsDamaged() && iDamageCounter == 0 )
       
  1233 				{
       
  1234 				cntdb->DamageDatabaseL(0x666);//will give us consistent behaviour on debug builds
       
  1235 				}
       
  1236 		#endif
       
  1237 		++iDamageCounter;
       
  1238 		CContactActiveRecover *recover = cntdb->CreateRecoverLC();
       
  1239 		recover->SetObserver( this );
       
  1240 		iSemaphore.Signal();//signal that recovery has started
       
  1241 		iSemaphore2.Wait();//wait for view to be created
       
  1242 
       
  1243 		ALLPRINT(KStartRecover);
       
  1244 		while(recover->Step())
       
  1245 			{
       
  1246 			User::After( 1000 );
       
  1247 			}
       
  1248 		CleanupStack::PopAndDestroy(recover);
       
  1249 		ALLPRINT(KEndRecover);
       
  1250 		}
       
  1251 	else
       
  1252 		{
       
  1253 		CContactActiveCompress 	*compress = cntdb->CreateCompressorLC();
       
  1254 		compress->SetObserver( this );
       
  1255 		iSemaphore.Signal();//signal that compress has started
       
  1256 		iSemaphore2.Wait();//wait for view to be created
       
  1257 		ALLPRINT(KStartCompress);
       
  1258 		while(compress->Step())
       
  1259 			{
       
  1260 			User::After( 1000 );
       
  1261 			}
       
  1262 		CleanupStack::PopAndDestroy(compress);
       
  1263 		ALLPRINT(KEndCompress);
       
  1264 		}
       
  1265 	iSemaphore.Signal();//signal that recovery compress is complete
       
  1266 	CleanupStack::PopAndDestroy( cntdb );
       
  1267 	}
       
  1268 
       
  1269 /**
       
  1270 Runs thread function, called from thread constructor
       
  1271 */
       
  1272 TInt CContactViewTest::ThreadRunL(TAny *aAny)
       
  1273 	{
       
  1274 	TInt ThreadError = KErrNone;
       
  1275 
       
  1276 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
  1277 	if(cleanup == NULL)
       
  1278 		{
       
  1279 		return KErrNoMemory;
       
  1280 		}
       
  1281 
       
  1282 	CActiveScheduler* sched=NULL;
       
  1283 	sched=new(ELeave) CActiveScheduler;
       
  1284 	CActiveScheduler::Install(sched);
       
  1285 
       
  1286 	CContactViewTest &test = *static_cast<CContactViewTest *>(aAny);
       
  1287 	TRAP(ThreadError, test.RecoverAndCompressThreadTestL()	);
       
  1288 	delete cleanup;
       
  1289 	delete sched;
       
  1290 	return ThreadError;
       
  1291 	}
       
  1292 
       
  1293 /**
       
  1294 Search for a field in contact item acid, that has the same label as aField
       
  1295 Sets the value of found field to null/empty
       
  1296 returns true if field found, false otherwise
       
  1297 */
       
  1298 TBool CContactViewTest::FindAndClearFieldL(const TContactItemId aCid, const TDesC &aField)
       
  1299 	{
       
  1300 	TBool ret = EFalse;
       
  1301 	OpenL(aCid,*iViewAll);
       
  1302 	TInt length = iFields->Count();
       
  1303 	TInt i;
       
  1304 	for(i = 0; i < length; ++i)
       
  1305 		{
       
  1306 		if( (*iFields)[i].Label() == aField && (*iFields)[i].Storage()->IsFull() )
       
  1307 			{
       
  1308 			SetFieldL(*iFields, i, KNullDesC);
       
  1309 			ret = ETrue;
       
  1310 			break;
       
  1311 			}
       
  1312 		}
       
  1313 	CommitL();
       
  1314 	return ret;
       
  1315 	}
       
  1316 
       
  1317 /**
       
  1318 Iterates through the missing fields list in the ini file,
       
  1319 retrieves the next field in the list
       
  1320 */
       
  1321 TPtrC CContactViewTest::GetNextFieldL()
       
  1322 	{
       
  1323 	static TInt iter = 0;
       
  1324 	++iter;
       
  1325 	TPtrC ret = GetFieldL(iter);
       
  1326 	if( ret == KNullDesC )
       
  1327 		{
       
  1328 		iter = 0;
       
  1329 		}
       
  1330 	return ret;
       
  1331 	}
       
  1332 
       
  1333 /**
       
  1334 Retrieves the field at aIndex of under missing fields section in ini file
       
  1335 */
       
  1336 TPtrC CContactViewTest::GetFieldL(const TInt aIndex)
       
  1337 	{
       
  1338 	TPtrC ret;
       
  1339 	TPtrC tfield = KUfield();
       
  1340 	HBufC *fval=HBufC::NewLC(tfield.Length()+2);//assume #fields < 100
       
  1341 
       
  1342 	TPtr fptr=fval->Des();
       
  1343 	fptr.Append( tfield );
       
  1344 	fptr.AppendNum( aIndex );
       
  1345 	GetStringFromConfig( KMissingFields, fptr, ret );
       
  1346 	CleanupStack::PopAndDestroy( fval );
       
  1347 	return ret;
       
  1348 	}
       
  1349 
       
  1350 /**
       
  1351 Creates a persistance layer local view, Client/server local view or remote view
       
  1352 depending on the type of testing being performed.
       
  1353 */
       
  1354 CContactViewBase* CContactViewTest::CreateViewL()
       
  1355 	{
       
  1356 	if( iPLView )
       
  1357 		{
       
  1358 		return CContactLocalView::NewL(	*iObserver, *iContactsDatabase,
       
  1359 										*iSortOrder, EContactsOnly, &iPersistence->FactoryL(), KNullDesC8 );
       
  1360 		}
       
  1361 	else
       
  1362 		{
       
  1363 		if(iRemote)
       
  1364 			{
       
  1365 			return CContactRemoteView::NewL(	*iObserver, *iContactsDatabase,
       
  1366 											*iSortOrder, EContactsOnly);
       
  1367 			}
       
  1368 		else
       
  1369 			{
       
  1370 			return CContactLocalView::NewL(	*iObserver, *iContactsDatabase,
       
  1371 										*iSortOrder, EContactsOnly);
       
  1372 			}
       
  1373 		}
       
  1374 	}
       
  1375 
       
  1376 /**
       
  1377 If performing persistance layer testing, cleanup the persistance layer instance
       
  1378 if aReOpen, also open sessions to contact server
       
  1379 */
       
  1380 void CContactViewTest::ClosePersistanceL(const TBool aReOpen)
       
  1381 	{
       
  1382 	if( iPLView )
       
  1383 		{
       
  1384 		if( iPersistence != NULL )
       
  1385 			{
       
  1386 			CLEAR( iPersistence );
       
  1387 			//wait for persistance layer to close, before opening server
       
  1388 			User::After( 200000 );
       
  1389 			if( aReOpen )
       
  1390 				{
       
  1391 				iContactsDatabase = CContactDatabase::OpenL();
       
  1392 
       
  1393 				iContactsDatabase2 = CContactDatabase::OpenL( CContactDatabase::EMultiThread );
       
  1394 				}
       
  1395 			}
       
  1396 		}
       
  1397 	}
       
  1398 
       
  1399 /**
       
  1400 If performing persistance layer testing, copy database from cntsrv private directory to test server
       
  1401 private directory using the pim test server. Then open copied db file using persistance layer.
       
  1402 */
       
  1403 void CContactViewTest::OpenPersistanceL()
       
  1404 	{
       
  1405 	if( iPLView )
       
  1406 		{
       
  1407 		TBuf<190> buf;
       
  1408 		iContactsDatabase->GetDefaultNameL(buf);
       
  1409 		CLEAR(iContactsDatabase);
       
  1410 		CLEAR(iContactsDatabase2);
       
  1411 		WaitForServerToExitL();
       
  1412 		iPimServ->CopyFileL(KContactsDB, KPersistenceDB);
       
  1413 		iPersistence = CPersistenceLayer::NewLC(iParent->Fs());
       
  1414 		CleanupStack::Pop(iPersistence);
       
  1415 
       
  1416 		#ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
  1417 		TRAPD( err, iPersistence->ContactsFileL().OpenL( buf ));
       
  1418 		#else
       
  1419 		TRAPD( err,
       
  1420 					while( iPersistence->ContactsFileL().OpenStepL( buf ) ) {}
       
  1421 		);
       
  1422 		#endif
       
  1423 
       
  1424 		User::LeaveIfError(err);
       
  1425 		}
       
  1426 	}
       
  1427 
       
  1428 /**
       
  1429 Set all fields in currently opened contact to a medium length string
       
  1430 */
       
  1431 void CContactViewTest::SetMediumL()
       
  1432 	{
       
  1433 
       
  1434 	for(TInt i = 0; i< 10; ++i)
       
  1435 		{
       
  1436 		SetFieldL(*iFields, i, TestConstants::KMediumString);
       
  1437 		}
       
  1438 	}
       
  1439 
       
  1440 /**
       
  1441 Set all fields in currently opened contact to a long length string
       
  1442 */
       
  1443 void CContactViewTest::SetFullL()
       
  1444 	{
       
  1445 	const TInt count = iFields->Count();
       
  1446 
       
  1447 	for(TInt i = 0; i< count; ++i)
       
  1448 		{
       
  1449 		SetFieldL(*iFields, i, TestConstants::KLongString);
       
  1450 		}
       
  1451 	}
       
  1452 /**
       
  1453 damage database and start active recovery
       
  1454 */
       
  1455 void CContactViewTest::StartRecoverL()
       
  1456 	{
       
  1457 	ALLPRINT(KStartRecover);
       
  1458 	#ifdef _DEBUG
       
  1459 	    #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
  1460 		iContactsDatabase2->DamageDatabaseL(0x666);//will give us consistent behaviour on debug builds
       
  1461 		#endif
       
  1462 	#endif
       
  1463 
       
  1464     User::After( 200000 );
       
  1465 
       
  1466 	iRecover = iContactsDatabase2->CreateRecoverLC();
       
  1467 	CleanupStack::Pop( iRecover );
       
  1468 
       
  1469 	iRecover->SetObserver( this );
       
  1470 
       
  1471 	while (iRecover->Step()) {}
       
  1472 
       
  1473 	}
       
  1474 
       
  1475 /**
       
  1476 cancel active recovery
       
  1477 */
       
  1478 void CContactViewTest::EndRecoverL()
       
  1479 	{
       
  1480 
       
  1481 	if( iRecover )
       
  1482 		{
       
  1483 		ALLPRINT(KEndRecover);
       
  1484 
       
  1485 		iRecover->Cancel();//deletes irecover
       
  1486 		iRecover = NULL;
       
  1487 		}
       
  1488 	}
       
  1489 
       
  1490 /**
       
  1491 start active compress
       
  1492 */
       
  1493 void CContactViewTest::StartCompressL()
       
  1494 	{
       
  1495 	ALLPRINT(KStartCompress);
       
  1496 	iCompress = iContactsDatabase2->CreateCompressorLC();
       
  1497 	iCompress->SetObserver( this );
       
  1498 	CleanupStack::Pop( iCompress );
       
  1499 
       
  1500 	}
       
  1501 
       
  1502 /**
       
  1503 cancels active compress
       
  1504 */
       
  1505 void CContactViewTest::EndCompressL()
       
  1506 	{
       
  1507 
       
  1508 	if( iCompress )
       
  1509 		{
       
  1510         ALLPRINT(KEndCompress);
       
  1511 
       
  1512 		iCompress->Cancel();//deletes iCompress
       
  1513 		iCompress = NULL;
       
  1514 		}
       
  1515 	}
       
  1516 
       
  1517 /**
       
  1518 Prints any errors encountered while performing active compress/recover
       
  1519 */
       
  1520 void CContactViewTest::HandleError(TInt aError)
       
  1521 	{
       
  1522 	iError = aError;
       
  1523 	_LIT(KCompressRecoverError,"Error in active compress/recover: %d");
       
  1524 	ALLPRINT2(KCompressRecoverError, iError);
       
  1525 	}
       
  1526