phonebookengines_old/contactsmodel/tsrc/t_iccentry.cpp
branchGCC_SURGE
changeset 41 d11de32a5e6f
parent 40 b46a585f6909
equal deleted inserted replaced
38:11319788d38f 41:d11de32a5e6f
       
     1 // Copyright (c) 2001-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 <cntdb.h>
       
    18 #include <cntitem.h>
       
    19 #include <cntfield.h>
       
    20 #include <cntfldst.h>
       
    21 #include <phbksync.h>
       
    22 
       
    23 #include "T_UTILS.H"
       
    24 #include "./TestSyncPlugIn/cnttestsynchroniser.h"
       
    25 
       
    26 // include templates for CleanupResetAndDestroyPushL(T)
       
    27 #include "cntviewprivate.h"
       
    28 
       
    29 
       
    30 /** 
       
    31  * @file
       
    32  * Test harness for CContactICCEntry and MContactSynchroniser interface.
       
    33  * todo Find out why telephone field won't match template correctly 
       
    34  *       (Maybe the ICC slot field needs to be added to the "golden" template)
       
    35  *
       
    36  */
       
    37 
       
    38 _LIT(KDatabaseFileName,"C:T_ICCENTRY");
       
    39 
       
    40 _LIT(KTestName,"Basic tests for CContactICCEntry");
       
    41 _LIT(KGivenName,"Given");
       
    42 _LIT(KGivenEditName,"EditedName");
       
    43 _LIT(KTelephoneNum,"020 79460111");
       
    44 _LIT(KTelephoneNumModified,"020 79460222");
       
    45 _LIT(KTemplateName,"SIM Card Contacts"); 
       
    46 
       
    47 _LIT(KPluginName,"phone book synchronizer Implementation");
       
    48 _LIT(KTestPluginName,"Test phone book synchronizer Implementation");
       
    49 
       
    50 CCntTest* CntTest=NULL;
       
    51 LOCAL_D RTest test(KTestName);
       
    52 
       
    53 LOCAL_D CContactTestSynchroniser* synchroniser;
       
    54 LOCAL_D CContactSyncChecker* syncChecker;
       
    55 
       
    56 LOCAL_C TInt findWordSplitterL(TAny *aParams)
       
    57 	{
       
    58 	SFindInTextDefWordParser *parser=(SFindInTextDefWordParser *)aParams;
       
    59 	parser->iWordArray->AppendL(*parser->iSearchString);
       
    60 	return(KErrNone);
       
    61 	}
       
    62 
       
    63 CContactIdArray* TestFindL(CContactDatabase& aDatabase, const TDesC& aText, const CContactItemFieldDef *aFieldDef, RTest& aTest)
       
    64 	{
       
    65 	CContactIdArray* retval = NULL;
       
    66 	
       
    67 	retval = aDatabase.FindLC(aText, aFieldDef);
       
    68 	aTest(retval == NULL);
       
    69 	CleanupStack::Pop( retval );
       
    70 	
       
    71 	return retval;
       
    72 	}
       
    73 	
       
    74 CContactIdArray* TestFindInTextDefL(CContactDatabase& aDatabase, const MDesCArray& aFindWords,CContactTextDef* aTextDef, const TCallBack &aWordParserCallback, RTest& aTest)
       
    75 	{
       
    76 	CContactIdArray* retval = NULL;
       
    77 	retval = aDatabase.FindInTextDefLC(aFindWords, aTextDef, aWordParserCallback);
       
    78 	aTest(retval==NULL);
       
    79 	CleanupStack::Pop( retval );
       
    80 	return retval;
       
    81 	}
       
    82 		
       
    83 /** 
       
    84  * Test Finding ICC Contacts when the ICC is locked.
       
    85  * @param aDb Contact database
       
    86  */
       
    87 
       
    88 void TestFindingL(CContactDatabase& aDb)
       
    89 	{
       
    90 	test.Next(_L("Test FindLC"));
       
    91 
       
    92 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrLocked);
       
    93 	syncChecker->SetValidateResponseL(MContactSynchroniser::ESearch,KErrLocked);
       
    94 	
       
    95 	CContactItemFieldDef* fieldDef=new(ELeave) CContactItemFieldDef;
       
    96 	CleanupStack::PushL(fieldDef);
       
    97 	fieldDef->AppendL(KUidContactFieldFamilyName); 
       
    98 	
       
    99 	
       
   100 	TRAPD(err,TestFindL(aDb, KGivenEditName, fieldDef, test) );
       
   101 	test(err==KErrLocked);
       
   102 	CleanupStack::PopAndDestroy(fieldDef);
       
   103 
       
   104 	CContactItemFieldDef* fieldDef2=new(ELeave) CContactItemFieldDef;
       
   105 	CleanupStack::PushL(fieldDef2);
       
   106 	fieldDef2->AppendL(KUidContactFieldPhoneNumber); 
       
   107 	TRAP(err, TestFindL(aDb, KTelephoneNum, fieldDef2, test) );
       
   108 	test(err==KErrLocked);
       
   109 	CleanupStack::PopAndDestroy(fieldDef2);
       
   110 
       
   111 	test.Next(_L("Test FindInTextDefL"));
       
   112 
       
   113 	TCallBack callBack(findWordSplitterL);
       
   114 	CContactTextDef* textDef=CContactTextDef::NewLC();
       
   115 	textDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
       
   116 	CDesCArray* desArray = new(ELeave) CDesCArrayFlat(5);
       
   117 	desArray->AppendL(_L("e"));
       
   118 	
       
   119 	CContactIdArray* arr = NULL;
       
   120 	TRAP(err,arr = TestFindInTextDefL(aDb,*desArray,textDef, callBack, test) );
       
   121 	
       
   122 //	test(err==KErrLocked);
       
   123 	desArray->Delete(0);
       
   124 	delete desArray;
       
   125 //FIX
       
   126 	delete arr;	
       
   127 	CleanupStack::PopAndDestroy(textDef); 
       
   128 
       
   129 	test.Next(_L("Test FindInTextDefL2"));
       
   130 
       
   131 	TCallBack callBack2(findWordSplitterL);
       
   132 	CContactTextDef* textDef2=CContactTextDef::NewLC();
       
   133 	textDef2->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
       
   134 	textDef2->AppendL(TContactTextDefItem(KUidContactFieldUrl));
       
   135 	CDesCArray* desArray2 = new(ELeave) CDesCArrayFlat(5);
       
   136 	desArray2->AppendL(_L("e"));
       
   137 	
       
   138 	TRAP(err,arr = TestFindInTextDefL(aDb,*desArray2,textDef2, callBack2, test) );
       
   139 //	test(err==KErrLocked);
       
   140 	desArray2->Delete(0);
       
   141 	delete desArray2;
       
   142 //FIX
       
   143 	delete arr;	
       
   144 	CleanupStack::PopAndDestroy(textDef2);
       
   145 	}
       
   146 
       
   147 
       
   148 /** 
       
   149  * Test ICC contacts can be matched using MatchPhoneNumberL API. The 
       
   150  * synchroniser is not called to validate the IDs at for this method because 
       
   151  * phone number resolution needs to be as quick as possible. 
       
   152  * Instead, validation is done when the client attempts to read the items.
       
   153  * @param aDb Contact database
       
   154  */
       
   155  
       
   156 
       
   157 void TestPhoneMatchingL(CContactDatabase& aDb)
       
   158 	{
       
   159 	test.Next(_L("Test phone match"));
       
   160 
       
   161 	CContactIdArray* array = aDb.MatchPhoneNumberL(KTelephoneNum,KMaxPhoneMatchLength);	
       
   162 	test(array->Count() == 4);
       
   163 	delete array;
       
   164 
       
   165 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   166 	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrNone);
       
   167 	syncChecker->SetValidateWriteResponseL(KErrNone);
       
   168 	aDb.SetDbViewContactType(KUidContactICCEntry);
       
   169 	TContactItemId id = (*aDb.SortedItemsL())[0];
       
   170 	CContactItem* item = aDb.OpenContactLX(id);
       
   171 	CleanupStack::PushL(item);
       
   172 	CContactItemFieldSet& fieldset = item->CardFields();
       
   173 	const TInt pos = fieldset.Find(KUidContactFieldPhoneNumber);	
       
   174 	CContactItemField& field = fieldset[pos];
       
   175 	CContactTextField* textfield = field.TextStorage();
       
   176 	textfield->SetTextL(KTelephoneNumModified);
       
   177 	aDb.CommitContactL(*item);
       
   178 	CleanupStack::PopAndDestroy(2); //item, lock record
       
   179 
       
   180 	CContactIdArray* array2 = aDb.MatchPhoneNumberL(KTelephoneNumModified,KMaxPhoneMatchLength);	
       
   181 	test(array2->Count() == 1);
       
   182 	delete array2;
       
   183 	}
       
   184 
       
   185 
       
   186 /** 
       
   187  * Test database iteration using TContactIter.
       
   188  * @param aDb Contact database
       
   189  */
       
   190  
       
   191 
       
   192 void TestDatabaseIterationL(CContactDatabase& aDb)
       
   193 	{
       
   194 	test.Next(_L("Test database iteration"));
       
   195 
       
   196 	aDb.SetDbViewContactType(KUidContactICCEntry);
       
   197 	TContactIter iterator(aDb);
       
   198 	TContactItemId id = iterator.FirstL();
       
   199 	while (id!=KNullContactId)
       
   200 		{
       
   201 		CContactItem* item = aDb.ReadContactLC(id);
       
   202 		test(item->Type() == KUidContactICCEntry);
       
   203 		CleanupStack::PopAndDestroy(item);
       
   204 		id = iterator.NextL();
       
   205 		}
       
   206 	}
       
   207 
       
   208 
       
   209 
       
   210 CContactItem* ReadItemL( CContactDatabase& aDatabase, TContactItemId aId )
       
   211 	{
       
   212 	CContactItem* retval = NULL;
       
   213 	retval = aDatabase.ReadMinimalContactLC( aId );
       
   214 	CleanupStack::Pop( retval );
       
   215 	return retval;
       
   216 	}
       
   217 
       
   218 /** 
       
   219  * Verify that the plug-in implementation is called when the 
       
   220  * ReadMinimalContactL API is used. 
       
   221  * 
       
   222  * Tests methods:
       
   223  * IMPORT_C CContactItem* ReadMinimalContactL(TContactItemId aContactId);
       
   224  * IMPORT_C CContactItem* ReadMinimalContactLC(TContactItemId aContactId);
       
   225  * 
       
   226  * @param aDb Contact database
       
   227  * @param aId Contact item ID to read
       
   228  */
       
   229 void TestReadMinimalContactL(CContactDatabase& aDb, TContactItemId aId)
       
   230 	{
       
   231 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   232 	syncChecker->ResetMethodCallCountsL();
       
   233 	test(syncChecker->ValidateMethodCallCountL() == 0);	
       
   234 	CContactItem* item = aDb.ReadMinimalContactLC(aId);
       
   235 	test(syncChecker->ValidateMethodCallCountL() == 1);	
       
   236 	CleanupStack::PopAndDestroy(item);
       
   237 
       
   238 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
       
   239 	syncChecker->ResetMethodCallCountsL();
       
   240 	test(syncChecker->ValidateMethodCallCountL() == 0);
       
   241 	item=NULL;
       
   242 	//TRAPD(err, item = aDb.ReadMinimalContactLC(aId));
       
   243 	TRAPD( err, item = ReadItemL( aDb, aId ) );
       
   244 	delete item;
       
   245 	test(err==KErrAccessDenied);
       
   246 	test(syncChecker->ValidateMethodCallCountL() == 1);	
       
   247 	}
       
   248 
       
   249 /** 
       
   250  * Verify that the plug-in implementation is called when the 
       
   251  * ReadContactL API is used. 
       
   252  * 
       
   253  * Tests code called by methods:
       
   254  * IMPORT_C CContactItem* ReadContactL(TContactItemId aContactId);
       
   255  * IMPORT_C CArrayPtr<CContactItem>* ReadContactAndAgentL(TContactItemId aContactId);
       
   256  * IMPORT_C CContactItem* ReadContactL(TContactItemId aContactId,const CContactItemViewDef& aViewDef);
       
   257  * IMPORT_C CContactItem* ReadContactLC(TContactItemId aContactId);
       
   258  * IMPORT_C CContactItem* ReadContactLC(TContactItemId aContactId,const CContactItemViewDef& aViewDef);
       
   259  * 
       
   260  * @param aDb Contact database
       
   261  * @param aId Contact item ID to read
       
   262  */
       
   263 void TestReadContactL(CContactDatabase& aDb, TContactItemId aId)
       
   264 	{
       
   265 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   266 	syncChecker->ResetMethodCallCountsL();
       
   267 	test(syncChecker->ValidateMethodCallCountL() == 0);	
       
   268 	CContactItem* item = aDb.ReadContactLC(aId);
       
   269 	test(syncChecker->ValidateMethodCallCountL() == 1);	
       
   270 	CleanupStack::PopAndDestroy(item);
       
   271 
       
   272 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
       
   273 	syncChecker->ResetMethodCallCountsL();
       
   274 	test(syncChecker->ValidateMethodCallCountL() == 0);
       
   275 	item=NULL;
       
   276 	TRAPD(err, item = aDb.ReadContactL(aId));
       
   277 	delete item;
       
   278 	test(err==KErrAccessDenied);
       
   279 	test(syncChecker->ValidateMethodCallCountL() == 1);	
       
   280 	}
       
   281 
       
   282 /** 
       
   283  * Verify that read validation is done for ReadContactTextDefL
       
   284  * 
       
   285  * @param aDb Contact database
       
   286  * @param aId Contact item ID to read
       
   287  */
       
   288 void TestReadTextDefinitionL(CContactDatabase& aDb, TContactItemId aId)
       
   289 	{
       
   290 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   291 	syncChecker->ResetMethodCallCountsL();
       
   292 	test(syncChecker->ValidateMethodCallCountL() == 0);
       
   293 	TBuf<64> textDef;
       
   294 	aDb.ReadContactTextDefL(aId,textDef);
       
   295 	test(syncChecker->ValidateMethodCallCountL() == 1);	
       
   296 
       
   297 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
       
   298 	syncChecker->ResetMethodCallCountsL();
       
   299 	test(syncChecker->ValidateMethodCallCountL() == 0);
       
   300 	TRAPD(err, aDb.ReadContactTextDefL(aId,textDef));
       
   301 	test(err==KErrAccessDenied);
       
   302 	test(syncChecker->ValidateMethodCallCountL() == 1);	
       
   303 	}
       
   304 
       
   305 /** 
       
   306  * Create example ICC Entry 
       
   307  * @param aDb Contact database
       
   308  */
       
   309 TContactItemId CreateTestICCEntryL(CContactDatabase& aDb)
       
   310 	{
       
   311 	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
       
   312 	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
       
   313 	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
       
   314 	CleanupStack::PopAndDestroy(iccTemplate);
       
   315 	CleanupStack::PushL(entry);
       
   316 	SetNameL(*entry,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,KGivenName,EFalse);
       
   317 	SetNameL(*entry,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,KTelephoneNum,EFalse);
       
   318 
       
   319 	//Add to database
       
   320 	syncChecker->SetValidateWriteResponseL(KErrNone);
       
   321 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   322 	syncChecker->SetDeleteContactResponseL(KErrNone);
       
   323 
       
   324 	TContactItemId id = aDb.AddNewContactL(*entry); 
       
   325 
       
   326 	CleanupStack::PopAndDestroy(entry);	
       
   327 	test(id!=KNullContactId);
       
   328 	return id;
       
   329 	}
       
   330 
       
   331 /**
       
   332  * Verify Read validation is being done for reading items.
       
   333  * @param aDb Contact database
       
   334  */
       
   335 
       
   336 void TestReadL(CContactDatabase& aDb)
       
   337 	{
       
   338 	TContactItemId id = CreateTestICCEntryL(aDb);
       
   339 	test.Next(_L("TestReadMinimalContactL"));
       
   340 
       
   341 	TestReadMinimalContactL(aDb,id);
       
   342 	test.Next(_L("TestReadContactL"));
       
   343 
       
   344 	TestReadContactL(aDb,id);
       
   345 	test.Next(_L("TestReadTextDefinitionL"));
       
   346 
       
   347 	TestReadTextDefinitionL(aDb,id);
       
   348 	}
       
   349 
       
   350 /**
       
   351  * Verify that edit validation is being done for ICC entries
       
   352  * and write validation is done when Commit is called.
       
   353  * @param aDb Contact database
       
   354  * @param aId Item Id to edit
       
   355  */
       
   356 void TestSuccessfulEditL(CContactDatabase& aDb, TContactItemId aId)
       
   357 	{
       
   358 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   359 	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrNone);
       
   360 	syncChecker->SetValidateWriteResponseL(KErrNone);
       
   361 	syncChecker->ResetMethodCallCountsL();
       
   362 	test(syncChecker->ValidateMethodCallCountL() == 0);
       
   363 	CContactItem* item = aDb.OpenContactLX(aId);
       
   364 	CleanupStack::PushL(item);
       
   365 	test(syncChecker->ValidateMethodCallCountL() == 2);	
       
   366 
       
   367 	CContactItemFieldSet& fieldset = item->CardFields();
       
   368 	const TInt pos = fieldset.Find(KUidContactFieldFamilyName);	
       
   369 	CContactItemField& field = fieldset[pos];
       
   370 	CContactTextField* textfield = field.TextStorage();
       
   371 	textfield->SetTextL(KGivenEditName);
       
   372 
       
   373 	aDb.CommitContactL(*item);
       
   374 	test(syncChecker->ValidateMethodCallCountL() == 4);	
       
   375 	CleanupStack::PopAndDestroy(2); //item, lock record
       
   376 	}
       
   377 
       
   378 /**
       
   379  * Unsuccessful edit. Fail the edit validation request
       
   380  * @param aDb Contact database
       
   381  * @param aId Item Id to edit
       
   382  */
       
   383 void FailEditValidationL(CContactDatabase& aDb, TContactItemId aId)
       
   384 	{
       
   385 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   386 	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrAccessDenied);
       
   387 	syncChecker->ResetMethodCallCountsL();
       
   388 	test(syncChecker->ValidateMethodCallCountL() == 0);
       
   389 	TRAPD(err, aDb.OpenContactL(aId));
       
   390 	test(syncChecker->ValidateMethodCallCountL() == 2);	
       
   391 	test(err==KErrAccessDenied);
       
   392 	}
       
   393 
       
   394 /**
       
   395  * Unsuccessful edit. Fail the write validation request
       
   396  * @param aDb Contact database
       
   397  * @param aId Item Id to edit
       
   398  */
       
   399 void FailWriteValidationL(CContactDatabase& aDb, TContactItemId aId)
       
   400 	{
       
   401 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   402 	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrNone);
       
   403 	syncChecker->SetValidateWriteResponseL(KErrAccessDenied);
       
   404 	syncChecker->ResetMethodCallCountsL();
       
   405 
       
   406 	CContactItem* item = aDb.OpenContactLX(aId);
       
   407 	CleanupStack::PushL(item);
       
   408 	CContactItemFieldSet& fieldset = item->CardFields();
       
   409 	const TInt pos = fieldset.Find(KUidContactFieldFamilyName);	
       
   410 	CContactItemField& field = fieldset[pos];
       
   411 	CContactTextField* textfield = field.TextStorage();
       
   412 	textfield->SetTextL(KGivenEditName);
       
   413 
       
   414 	TRAPD(err, aDb.CommitContactL(*item));
       
   415 	test(syncChecker->ValidateMethodCallCountL() == 4);	
       
   416 	test(err==KErrAccessDenied);
       
   417 
       
   418 	CleanupStack::PopAndDestroy(2); //item, lock record
       
   419 	}
       
   420 
       
   421 /**
       
   422  * Verify that edit validation is being done for ICC entries
       
   423  * and write validation is done when Commit is called.
       
   424  * @param aDb Contact database
       
   425  */
       
   426 
       
   427 void TestEditL(CContactDatabase& aDb)
       
   428 	{
       
   429 	TContactItemId id = CreateTestICCEntryL(aDb);
       
   430 	test.Next(_L("Test successful edit"));
       
   431 
       
   432 	TestSuccessfulEditL(aDb,id);
       
   433 	test.Next(_L("Test unsuccessful edit - fail edit"));
       
   434 
       
   435 	FailEditValidationL(aDb,id);
       
   436 	test.Next(_L("Test unsuccessful edit - fail write"));
       
   437 
       
   438 	FailWriteValidationL(aDb,id);
       
   439 	}
       
   440 
       
   441 /** 
       
   442  * Test an unsuccessful delete
       
   443  * @param aDb Contact database
       
   444  */
       
   445 void TestUnsuccessfulDeleteL(CContactDatabase& aDb)
       
   446 	{
       
   447 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   448 	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrNone);
       
   449 	TContactItemId id = CreateTestICCEntryL(aDb);
       
   450 	// Creating the contact sets the delete error to KErrNone, so must do this afterwards
       
   451 	syncChecker->SetDeleteContactResponseL(KErrCorrupt);
       
   452 	syncChecker->ResetMethodCallCountsL();
       
   453 	TRAPD(err, aDb.DeleteContactL(id));
       
   454 	test(syncChecker->ValidateMethodCallCountL() == 2);
       
   455 	test(err==KErrCorrupt);
       
   456 	//Read contact to check it wasn't deleted anyway
       
   457 	CContactItem* item = NULL;
       
   458 	TRAP(err,item = aDb.ReadContactL(id));
       
   459 	test(err==KErrNone);
       
   460 	delete item;
       
   461 	}
       
   462 
       
   463 
       
   464 /** 
       
   465  * Verify that delete validation is being done for ICC entries.
       
   466  * @param aDb Contact database
       
   467  */
       
   468 
       
   469 void TestDeleteL(CContactDatabase& aDb)
       
   470 	{
       
   471 	test.Next(_L("Test successful delete"));
       
   472 
       
   473 	TContactItemId id = CreateTestICCEntryL(aDb);
       
   474 	syncChecker->ResetMethodCallCountsL();
       
   475 	aDb.DeleteContactL(id);
       
   476 	test(syncChecker->ValidateMethodCallCountL() == 2);
       
   477 	test.Next(_L("Test unsuccessful delete"));
       
   478 
       
   479 	TestUnsuccessfulDeleteL(aDb);
       
   480 	}
       
   481 
       
   482 
       
   483 /** 
       
   484  * Create template card for ICC contact items 
       
   485  * @param aDb Contact database
       
   486  */
       
   487 TContactItemId CreateICCTemplateL(CContactDatabase& aDb)
       
   488 	{
       
   489 	CContactItem* temp = aDb.CreateContactCardTemplateLC(KTemplateName);
       
   490 	TContactItemId templateId = temp->Id();
       
   491 	CleanupStack::PopAndDestroy(temp);
       
   492 	temp=NULL;
       
   493 
       
   494 	//Remove all the unnecessary fields
       
   495 	temp = aDb.OpenContactLX(templateId);
       
   496 	CleanupStack::PushL(temp);	
       
   497 	const TInt fieldCount = temp->CardFields().Count();
       
   498 	for(TInt i=fieldCount-1;i>=0;i--)
       
   499 		temp->RemoveField(i);
       
   500 		
       
   501 	CContactItemField* name = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldFamilyName);
       
   502 	name->SetMapping(KUidContactFieldVCardMapUnusedN);
       
   503 	temp->AddFieldL(*name);
       
   504 	CleanupStack::Pop(name);
       
   505 
       
   506 	CContactItemField* number = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber);
       
   507 	number->SetMapping(KUidContactFieldVCardMapTEL);
       
   508 	temp->AddFieldL(*number);
       
   509 	CleanupStack::Pop(number);
       
   510 				
       
   511 	CContactItemField* slotnum = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldICCSlot);
       
   512 	temp->AddFieldL(*slotnum);
       
   513 	CleanupStack::Pop(slotnum);
       
   514 
       
   515 	CContactItemField* phonebook = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldICCPhonebook);
       
   516 	temp->AddFieldL(*phonebook);
       
   517 	CleanupStack::Pop(phonebook);
       
   518 
       
   519 	aDb.CommitContactL(*temp);
       
   520 	CleanupStack::PopAndDestroy(2);	// temp, close template	
       
   521 	
       
   522 	return templateId;
       
   523 	}
       
   524 
       
   525 /**
       
   526  * Test SetDbViewContactType can be used for KUidContactICCEntry items.
       
   527  * @param aDb Contact database
       
   528  */
       
   529 void TestDatabaseViewL(CContactDatabase& aDb)
       
   530 	{
       
   531 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   532 	syncChecker->SetValidateWriteResponseL(KErrNone);
       
   533 	CntTest->CloseDatabase();
       
   534 	CntTest->DeleteDatabaseL();
       
   535 	// re-initialise the TLS
       
   536 	syncChecker->EnableVerboseLoggingL();
       
   537 
       
   538 	CntTest->CreateDatabaseL();	
       
   539 	CntTest->OpenDatabaseL();
       
   540 	delete synchroniser;
       
   541 	synchroniser = CContactTestSynchroniser::NewL();
       
   542 	// test with GSM ADN phonebook only
       
   543 	//test(KErrNone == syncChecker->SetGsmPhonebook());
       
   544 
       
   545 	// Start from clean
       
   546 	CContactItem* card = CContactCard::NewLC();
       
   547 	aDb.AddNewContactL(*card);
       
   548 	aDb.CreateContactGroupLC();
       
   549 	aDb.CreateOwnCardLC();
       
   550 	TContactItemId id=CreateICCTemplateL(aDb);
       
   551 	test(KErrNone == syncChecker->UpdatePhonebookEntryL(KUidIccGlobalAdnPhonebook, id, KNullContactId));
       
   552 	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
       
   553 	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
       
   554 	CContactICCEntry* iccentry = CContactICCEntry::NewL(*iccTemplate);
       
   555 	CleanupStack::PopAndDestroy(iccTemplate);
       
   556 	CleanupStack::PushL(iccentry);
       
   557 	aDb.AddNewContactL(*iccentry);
       
   558 	TContactItemId iccId = iccentry->Id();
       
   559 	CleanupStack::PopAndDestroy(4); //card,group,owncard,iccentry
       
   560 	
       
   561 	test(aDb.SortedItemsL()->Count() == 5); //default view includes ALL contact types
       
   562 	CntTest->CloseDatabase();
       
   563 	delete synchroniser;
       
   564 	synchroniser = NULL;
       
   565 	CntTest->OpenDatabaseL();
       
   566 	aDb.SetDbViewContactType(KUidContactICCEntry);
       
   567 	test(aDb.SortedItemsL()->Count() == 1);
       
   568 	test((*aDb.SortedItemsL())[0]==iccId);
       
   569 	}
       
   570 
       
   571 /** 
       
   572  * Create phonebook group 
       
   573  * @param aDb Contact database
       
   574  */
       
   575 TContactItemId CreatePhonebookGroupL(CContactDatabase& aDb)
       
   576 	{
       
   577 	_LIT(KPhbkGroup,"ADN Phonebook"); 
       
   578 	CContactItem* group = aDb.CreateContactGroupLC(KPhbkGroup);
       
   579 	TContactItemId groupId = group->Id();
       
   580 	CleanupStack::PopAndDestroy(group);	
       
   581 	return groupId;
       
   582 	}
       
   583 
       
   584 /** 
       
   585  * Verify that the template was correctly added 
       
   586  * @param aTemplate CContactCardTemplate template item
       
   587  */
       
   588 void CheckTemplateL(CContactItem& aTemplate)
       
   589 	{
       
   590 	test(aTemplate.Type() == KUidContactCardTemplate);
       
   591 	test(aTemplate.TemplateRefId() == KNullContactId);
       
   592 	test(static_cast<CContactCardTemplate&>(aTemplate).GetTemplateLabelL() == KTemplateName);
       
   593 	CContactItemFieldSet& fieldset = aTemplate.CardFields();
       
   594 	TInt pos = fieldset.Find(KUidContactFieldTemplateLabel);
       
   595 	test(pos!=KErrNotFound);
       
   596 	TInt count = fieldset.Count();
       
   597 	test(count==5); //label, name, number, ICC slot, phonebook
       
   598 	}
       
   599 
       
   600 
       
   601 /** 
       
   602  * Check that the field of type aFieldType has the same content as 
       
   603  * aExpectedContent
       
   604  * @param aFieldset Contact item fieldset
       
   605  * @param aFieldType Field type to test
       
   606  * @param aExpectedContent Expected data
       
   607  */
       
   608 void CheckFieldContentL(CContactItemFieldSet& aFieldset, TFieldType aFieldType, const TDesC& aExpectedContent)
       
   609 	{
       
   610 	TInt pos = aFieldset.Find(aFieldType);
       
   611 	CContactItemField& field = aFieldset[pos];
       
   612 	CContactTextField* textField = field.TextStorage();
       
   613 	test(textField != NULL);
       
   614 	test(textField->Text() == aExpectedContent);
       
   615 	}
       
   616 
       
   617 /** 
       
   618  * Test creation of CContactICCEntry items.
       
   619  * 
       
   620  * Set-up appropriate template and group ready...
       
   621  * @param aDb Contact database
       
   622  */
       
   623 
       
   624 /**
       
   625 
       
   626 @SYMTestCaseID     PIM-T-ICCENTRY-0001
       
   627 
       
   628 */
       
   629 
       
   630 void TestCreationL(CContactDatabase& aDb)
       
   631 	{
       
   632 	_LIT(KTestCreation,"@SYMTESTCaseID:PIM-T-ICCENTRY-0001 Create CContactICCEntry item");
       
   633 	test.Start(KTestCreation);
       
   634 
       
   635 	//System Template
       
   636 	TContactItemId systemTemplateId = aDb.TemplateId();
       
   637 	CContactItem* systemTemplate = aDb.ReadContactLC(systemTemplateId);
       
   638 	CContactICCEntry* entry = CContactICCEntry::NewL(*systemTemplate);
       
   639 	CleanupStack::PopAndDestroy(systemTemplate);
       
   640 	CleanupStack::PushL(entry);
       
   641 	
       
   642 	//Test CContactICCEntry items can be identified from normal contact cards
       
   643 	test(entry->Type() == KUidContactICCEntry);
       
   644 	test(entry->TemplateRefId() == systemTemplateId);
       
   645 	CleanupStack::PopAndDestroy(entry);
       
   646 
       
   647 	//Create user defined template for ICC contacts
       
   648 	test.Next(_L("CContactDatabase::ICCTemplateL()"));
       
   649 
       
   650 	TContactItemId templateId = CreateICCTemplateL(aDb);
       
   651 	test(KErrNone == syncChecker->UpdatePhonebookEntryL(KUidIccGlobalAdnPhonebook, templateId, KNullContactId));
       
   652 
       
   653 	//Check that contacts model calls the plug-in when asked for the
       
   654 	//template ID.
       
   655 	TInt callCount = syncChecker->ValidateMethodCallCountL();
       
   656 	test(callCount==0);
       
   657 	TContactItemId templateIdFromCntmodel;
       
   658 	templateIdFromCntmodel = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
       
   659 
       
   660 	test(syncChecker->ValidateMethodCallCountL() == 1);
       
   661 	test(templateId == templateIdFromCntmodel);
       
   662 	
       
   663 	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
       
   664 	CheckTemplateL(*iccTemplate);
       
   665 	//create ICC contact
       
   666 	CContactICCEntry* iccentry = CContactICCEntry::NewL(*iccTemplate);
       
   667 	CleanupStack::PopAndDestroy(iccTemplate);
       
   668 	CleanupStack::PushL(iccentry);
       
   669 	test(iccentry->Type() == KUidContactICCEntry);
       
   670 	test(iccentry->TemplateRefId() == templateId);
       
   671 	CleanupStack::PopAndDestroy(iccentry);
       
   672 	
       
   673 	test.End();
       
   674 	}
       
   675 
       
   676 
       
   677 
       
   678 /** 
       
   679  * Test successful addition to database.
       
   680  * @param aDb Contact database
       
   681  */
       
   682 void TestSuccessfulAddL(CContactDatabase& aDb)
       
   683 	{
       
   684 	//Create group 
       
   685 	TContactItemId groupId = CreatePhonebookGroupL(aDb);
       
   686 	test(KErrNone == syncChecker->UpdatePhonebookGroupIdL(KUidIccGlobalAdnPhonebook, groupId));
       
   687 	//Create item and add fields
       
   688 	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
       
   689 	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
       
   690 	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
       
   691 	CleanupStack::PopAndDestroy(iccTemplate);
       
   692 	CleanupStack::PushL(entry);
       
   693 	SetNameL(*entry,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,KGivenName,EFalse);
       
   694 	SetNameL(*entry,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,KTelephoneNum,EFalse);
       
   695 
       
   696 	//Add to database
       
   697 	syncChecker->ResetMethodCallCountsL();
       
   698 	syncChecker->SetValidateWriteResponseL(KErrNone);
       
   699 	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
       
   700 
       
   701 	test(syncChecker->ValidateMethodCallCountL() == 0);
       
   702 	TContactItemId id = aDb.AddNewContactL(*entry); 
       
   703 	CleanupStack::PopAndDestroy(entry);	
       
   704 	test(syncChecker->ValidateMethodCallCountL() == 3);
       
   705 	test(id!=KNullContactId);
       
   706 
       
   707 	CContactICCEntry* fetchedItem = static_cast<CContactICCEntry*>(aDb.ReadContactL(id));
       
   708 	CleanupStack::PushL(fetchedItem);
       
   709 	//Check group membership	
       
   710 	const CContactIdArray* owned = fetchedItem->GroupsJoined();
       
   711 	test(owned!=NULL && owned->Count() == 1);
       
   712 	test((*owned)[0]==groupId);
       
   713 	
       
   714 	//Check number of fields and content
       
   715 	
       
   716 	CContactItemFieldSet& fieldset = fetchedItem->CardFields();
       
   717 	TInt pos = fieldset.Find(KUidContactFieldTemplateLabel);
       
   718 	test(pos==KErrNotFound);
       
   719 	
       
   720 //	test(fieldset.Count() == 3); 
       
   721 	
       
   722 /*	for (TInt i=0; i<fieldset.Count(); i++)
       
   723 		{
       
   724 		CContactItemField& field = fieldset[i];
       
   725 		TInt count = field.ContentType().FieldTypeCount();
       
   726 		for (TInt j=0; j<count; j++)
       
   727 			{
       
   728 			TFieldType ft= field.ContentType().FieldType(j);
       
   729 			}
       
   730 
       
   731 
       
   732 		TUid mapping = field.ContentType().Mapping();
       
   733 		}
       
   734 */	
       
   735 	CheckFieldContentL(fieldset,KUidContactFieldFamilyName,KGivenName);
       
   736 //	CheckFieldContentL(fieldset,KUidContactFieldPhoneNumber,KTelephoneNum);
       
   737 
       
   738 	CleanupStack::PopAndDestroy(fetchedItem);
       
   739 	}
       
   740 
       
   741 /** 
       
   742  * Test an unsuccessful addition to the database
       
   743  * @param aDb Contact database
       
   744  */
       
   745 void TestUnsuccessfulAddL(CContactDatabase& aDb)
       
   746 	{
       
   747 	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
       
   748 	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
       
   749 	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
       
   750 	CleanupStack::PopAndDestroy(iccTemplate);
       
   751 	CleanupStack::PushL(entry);
       
   752 	SetNameL(*entry,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,KGivenName,EFalse);
       
   753 	SetNameL(*entry,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,KTelephoneNum,EFalse);
       
   754 
       
   755 	syncChecker->ResetMethodCallCountsL();
       
   756 	syncChecker->SetValidateWriteResponseL(KErrAccessDenied);
       
   757 	TRAPD(error, aDb.AddNewContactL(*entry)); 	
       
   758 	test(error==KErrAccessDenied);
       
   759 	test(syncChecker->ValidateMethodCallCountL() == 1);
       
   760 	CleanupStack::PopAndDestroy(entry);
       
   761 	}
       
   762 
       
   763 /** 
       
   764  * Test addition to database without adding to a group
       
   765  * @param aDb Contact database
       
   766  */
       
   767 void TestAddingWithoutGroupL(CContactDatabase& aDb)
       
   768 	{
       
   769 	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
       
   770 	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
       
   771 	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
       
   772 	CleanupStack::PopAndDestroy(iccTemplate);
       
   773 	CleanupStack::PushL(entry);
       
   774 	SetNameL(*entry,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,KGivenName,EFalse);
       
   775 	SetNameL(*entry,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,KTelephoneNum,EFalse);
       
   776 
       
   777 	syncChecker->ResetMethodCallCountsL();
       
   778 	syncChecker->SetValidateWriteResponseL(KErrNone);
       
   779 	TInt oldGroupId = syncChecker->GroupIdL(KUidIccGlobalAdnPhonebook);
       
   780 	test(KErrNone == syncChecker->UpdatePhonebookGroupIdL(KUidIccGlobalAdnPhonebook, KNullContactId));
       
   781 	TContactItemId id = aDb.AddNewContactL(*entry); 	
       
   782 	test(syncChecker->ValidateMethodCallCountL() == 3);
       
   783 	CleanupStack::PopAndDestroy(entry);
       
   784 
       
   785 	CContactICCEntry* fetchedItem = static_cast<CContactICCEntry*>(aDb.ReadContactL(id));
       
   786 	CleanupStack::PushL(fetchedItem);
       
   787 	//Check group membership	
       
   788 	const CContactIdArray* owned = fetchedItem->GroupsJoined();
       
   789 	test(owned==NULL || owned->Count() == 0);
       
   790 	CleanupStack::PopAndDestroy(fetchedItem);
       
   791 		
       
   792 	test(KErrNone == syncChecker->UpdatePhonebookGroupIdL(KUidIccGlobalAdnPhonebook, oldGroupId));
       
   793 	}
       
   794 
       
   795 /** 
       
   796  * Verify that adding contact cards doesn't call the synchroniser interface
       
   797  * @param aDb Contact database
       
   798  */
       
   799 void TestAddingContactCardL(CContactDatabase& aDb)
       
   800 	{
       
   801 	CContactCard* card = CContactCard::NewLC();
       
   802 	syncChecker->ResetMethodCallCountsL();
       
   803 	aDb.AddNewContactL(*card);
       
   804 	test(syncChecker->ValidateMethodCallCountL() == 0);
       
   805 	CleanupStack::PopAndDestroy(card);
       
   806 	}
       
   807 
       
   808 /** 
       
   809  * Test addition of CContactICCEntry items.
       
   810  * @param aDb Contact database
       
   811  */
       
   812 void TestAddL(CContactDatabase& aDb)
       
   813 	{
       
   814 	TestCreationL(aDb);
       
   815 	TestSuccessfulAddL(aDb);
       
   816 	TestUnsuccessfulAddL(aDb);
       
   817 	TestAddingWithoutGroupL(aDb);
       
   818 	TestAddingContactCardL(aDb);
       
   819 	}
       
   820 
       
   821 /** 
       
   822  * Test PhonebookGroupIdL method.
       
   823  * @param aDb Contact database
       
   824  */
       
   825 void TestPhonebookGroupIdL(CContactDatabase& aDb)
       
   826 	{
       
   827 	synchroniser = CContactTestSynchroniser::NewL();
       
   828 	const TContactItemId expectedGroupId(KNullContactId);
       
   829 	test(KErrNone == syncChecker->UpdatePhonebookGroupIdL(KUidIccGlobalAdnPhonebook, expectedGroupId));
       
   830 	TContactItemId id = aDb.PhonebookGroupIdL();
       
   831 	test(id == expectedGroupId);
       
   832 	}
       
   833 
       
   834 
       
   835 void CheckForPhbkSyncPluginL()
       
   836 	{
       
   837 	test.Next(_L("Check for PhbkSync test plug-in"));
       
   838 
       
   839 
       
   840 	RImplInfoPtrArray	implInfoArray;
       
   841 	CleanupResetAndDestroyPushL(implInfoArray);
       
   842 	REComSession::ListImplementationsL(KUidEcomCntPhBkSyncInterface, implInfoArray);
       
   843 	//Find implementations of KUidEcomCntPhBkSyncInterface
       
   844 	TInt availCount = implInfoArray.Count(); 
       
   845 	TInt count;
       
   846 	for(count = 0; count < availCount; count++)
       
   847 		{
       
   848 		//const TUid firstImplementationFound = implInfoArray[count]->ImplementationUid();
       
   849 		CImplementationInformation *info = implInfoArray[count];
       
   850 		test.Printf(_L("\n"));
       
   851 		test.Printf(_L("PhbkSync plugin #%i, Implementation UID 0x%08X version %i\n"),
       
   852 			count + 1, info->ImplementationUid(), info->Version());
       
   853 		test.Printf(_L("Plugin name = \"%S\"\n"), &(info->DisplayName()));
       
   854 		}
       
   855 
       
   856 	// is telephony's plug-in in the list?
       
   857 	for(count = 0; count < availCount; count++)
       
   858 		{
       
   859 		//const TUid firstImplementationFound = implInfoArray[count]->ImplementationUid();
       
   860 		CImplementationInformation *info = implInfoArray[count];
       
   861 		if(info->DisplayName() == KTestPluginName)
       
   862 			{
       
   863 			test.Printf(_L("\n"));
       
   864 			test.Printf(_L("This test has now loaded the test plugin"));
       
   865 			test.Printf(_L("\n"));
       
   866 			availCount = 1;
       
   867 			break;
       
   868 			}		
       
   869 		
       
   870 		if(info->DisplayName() == KPluginName)
       
   871 			{
       
   872 			test.Printf(_L("\n"));
       
   873 			test.Printf(_L("This test only works with Contacts the test plugin and not the original phonebooksync plugin."));
       
   874 			test.Printf(_L("Depending on the build to removed the plugin in different ways:"));
       
   875 			test.Printf(_L("hardware - delete the line \"ECOM_PLUGIN(phbksyncplugin.dll,1020428C.rsc)\" from phbksync.iby"));
       
   876 			test.Printf(_L("winscw - delete phbksyncplugin.dll from %epocroot%/epoc32/release/winscw/udeb or similarly named directory"));
       
   877 			test.Printf(_L("\n"));
       
   878 			test(0);  // stop
       
   879 			break;
       
   880 			}
       
   881 		}
       
   882 
       
   883 	// only continue test if there is exactly one plug-in present
       
   884 	test(availCount == 1);	
       
   885 	CleanupStack::PopAndDestroy(&implInfoArray);
       
   886 	}
       
   887 
       
   888 /** Lists tests to execute */
       
   889 
       
   890 void DoTestsL()
       
   891     {
       
   892 	test.Start(_L("Basic tests for CContactICCEntry"));
       
   893 
       
   894 
       
   895 	CheckForPhbkSyncPluginL();
       
   896 
       
   897 	CntTest->CreateDatabaseL();	
       
   898 	CntTest->OpenDatabaseL();
       
   899 
       
   900 	syncChecker = CContactSyncChecker::NewL();
       
   901 	synchroniser = CContactTestSynchroniser::NewL();
       
   902 	syncChecker->ResetL();
       
   903 	syncChecker->EnableVerboseLoggingL();
       
   904 	// test with GSM ADN phonebook only
       
   905 	test(KErrNone == syncChecker->SetGsmPhonebookL());
       
   906 	
       
   907 	CContactDatabase& db= *CntTest->Db();
       
   908 	TestAddL(db);
       
   909 	TestReadL(db);
       
   910 	TestEditL(db);
       
   911 	TestPhoneMatchingL(db);
       
   912 	TestFindingL(db);
       
   913 	TestDeleteL(db);
       
   914 	TestDatabaseIterationL(db);
       
   915 	TestDatabaseViewL(db);
       
   916 	TestPhonebookGroupIdL(db);
       
   917 	
       
   918 	CntTest->CloseDatabase();
       
   919 	CntTest->DeleteDatabaseL();
       
   920 
       
   921 	delete synchroniser;
       
   922 	synchroniser = NULL;
       
   923 	delete syncChecker;
       
   924 	syncChecker = NULL;
       
   925     }
       
   926 
       
   927 /** Standard E32Main method */
       
   928 GLDEF_C TInt E32Main()
       
   929 	{
       
   930 	__UHEAP_MARK;
       
   931     CntTest=new(ELeave) CCntTest;
       
   932 	CntTest->ConstructL(test,KDatabaseFileName);
       
   933     TRAPD(err,DoTestsL());
       
   934 	if (err)
       
   935 		{
       
   936 		// delete globals
       
   937 		delete synchroniser;
       
   938 		synchroniser = NULL;
       
   939 		delete syncChecker;
       
   940 		syncChecker = NULL;
       
   941 		}
       
   942 	CntTest->EndTestLib(err);
       
   943 	__UHEAP_MARKEND;
       
   944 	return KErrNone;
       
   945     }