phonebookengines_old/contactsmodel/tsrc/T_FIELD.CPP
branchRCL_3
changeset 19 5b6f26637ad3
equal deleted inserted replaced
18:d4f567ce2e7c 19:5b6f26637ad3
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <f32file.h>
       
    18 #include <s32file.h>
       
    19 #include <s32mem.h>
       
    20 #include <cntdb.h>
       
    21 #include <cntitem.h>
       
    22 #include <cntfield.h>
       
    23 #include <cntfldst.h>
       
    24 #include "T_UTILS.H"
       
    25 #include "cnttestsynchroniser.h"
       
    26 
       
    27 CCntTest* CntTest=NULL;
       
    28 LOCAL_D RTest test(_L("T_FIELD"));
       
    29 
       
    30 const TPtrC KDatabaseFileName=_L("C:T_FIELD");
       
    31 
       
    32 #define KTestLabelName _L("Label %d")
       
    33 #define KTestNewLabelName _L("New Label %d")
       
    34 
       
    35 LOCAL_C void TestIsFullL()
       
    36 	{
       
    37 	CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldCompanyName);
       
    38 	test(field->TextStorage()->IsFull()==EFalse);
       
    39 	field->TextStorage()->SetTextL(_L("Should be full now"));
       
    40 	test(field->TextStorage()->IsFull());
       
    41 	CleanupStack::PopAndDestroy();	// field
       
    42 //
       
    43 	field=CContactItemField::NewLC(KStorageTypeDateTime,KUidContactFieldBirthday);
       
    44 	test(field->DateTimeStorage()->IsFull()==EFalse);
       
    45 	field->DateTimeStorage()->SetTime(TTime(123));
       
    46 	test(field->DateTimeStorage()->IsFull());
       
    47 	CleanupStack::PopAndDestroy();	// field
       
    48 	}
       
    49 
       
    50 void AddThingFieldL(CContactCard* aCard, const TDesC8 *aThingy)
       
    51 	{
       
    52 	if (aThingy)
       
    53 		{
       
    54 		CContactItemField* field=CContactItemField::NewLC(KStorageTypeStore);
       
    55 		field->StoreStorage()->SetThingL(*aThingy);
       
    56 		aCard->AddFieldL(*field);
       
    57 		CleanupStack::Pop(); // field
       
    58 		test((*field->StoreStorage()->Thing())==(*aThingy));
       
    59 		}
       
    60 	}
       
    61 
       
    62 void TestThingyStorageL(const TDesC8 *aThingy1,const TDesC8 *aThingy2,const TDesC8 *aThingy3)
       
    63 	{
       
    64 	CContactCard* card=CContactCard::NewLC();
       
    65 	AddThingFieldL(card,aThingy1);
       
    66 	AddThingFieldL(card,aThingy2);
       
    67 	AddThingFieldL(card,aThingy3);
       
    68 // then add the new contact to the database
       
    69 	TContactItemId id=CntTest->Db()->AddNewContactL(*card);
       
    70 	CleanupStack::PopAndDestroy(); // card
       
    71 //
       
    72 	card=STATIC_CAST(CContactCard*,CntTest->Db()->OpenContactL(id));
       
    73 	CleanupStack::PushL(card);
       
    74 	CContactItemFieldSet& newFields = card->CardFields();
       
    75 	HBufC8* testThing=newFields[0].StoreStorage()->Thing();
       
    76 	test(*testThing==*aThingy1);
       
    77 	if (aThingy2)
       
    78 		{
       
    79 		testThing=newFields[1].StoreStorage()->Thing();
       
    80 		test(*testThing==*aThingy2);
       
    81 		if (aThingy3)
       
    82 			{
       
    83 			testThing=newFields[2].StoreStorage()->Thing();
       
    84 			test(*testThing==*aThingy3);
       
    85 			}
       
    86 		}
       
    87 	CleanupStack::PopAndDestroy(); // card
       
    88 	CntTest->Db()->CloseContactL(id);
       
    89 	}
       
    90 
       
    91 HBufC8 *CreateThingyLC(TInt aSize)
       
    92 	{
       
    93 	HBufC8 *thingy=NULL;
       
    94 	if (aSize>0)
       
    95 		{
       
    96 		thingy=HBufC8::NewL(aSize);
       
    97 		TUint8 *ptr=(TUint8 *)thingy->Ptr();
       
    98 		for(TInt loop=0;loop<aSize;loop++)
       
    99 			*ptr++=(TUint8)(loop%255);
       
   100 		thingy->Des().SetLength(aSize);
       
   101 		}
       
   102 	CleanupStack::PushL(thingy);
       
   103 	return(thingy);
       
   104 	}
       
   105 
       
   106 void TestThingyStorageBufL(TInt aSize1,TInt aSize2,TInt aSize3)
       
   107 	{
       
   108 	HBufC8 *thingy1=CreateThingyLC(aSize1);
       
   109 	HBufC8 *thingy2=CreateThingyLC(aSize2);
       
   110 	HBufC8 *thingy3=CreateThingyLC(aSize3);
       
   111 	TestThingyStorageL(thingy1,thingy2,thingy3);
       
   112 	CleanupStack::PopAndDestroy(3);	// thingy1,2,3
       
   113 	}
       
   114 
       
   115 LOCAL_C void TestThingyFieldsL()
       
   116 	{
       
   117 // Test binary storage
       
   118 	test.Next(_L("binary storage"));
       
   119 
       
   120 	_LIT8(thingyText1,"Thingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy textThingy ");
       
   121 	_LIT8(thingyText2,"x");
       
   122 	_LIT8(thingyText3,"q1");
       
   123 	TestThingyStorageL(&thingyText1,NULL,NULL);
       
   124 	TestThingyStorageL(&thingyText2,&thingyText3,NULL);
       
   125 	TestThingyStorageL(&thingyText2,&thingyText1,&thingyText3);
       
   126 	for(TInt sizeLoop=5;sizeLoop<100;sizeLoop+=2)
       
   127 		for(TInt sizeLoop2=0;sizeLoop2<5;sizeLoop2++)
       
   128 			TestThingyStorageBufL(sizeLoop*sizeLoop+sizeLoop2,sizeLoop2+sizeLoop,sizeLoop2*sizeLoop2+sizeLoop);
       
   129 	}
       
   130 
       
   131 
       
   132 LOCAL_C void TestFieldSetL()
       
   133 //
       
   134 // test CContactItemFieldSet
       
   135 //
       
   136 	{
       
   137 	test.Next(_L("field set"));
       
   138 
       
   139 	CContactItemFieldSet* fieldSet=CContactItemFieldSet::NewLC();
       
   140 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldCompanyName,KUidContactFieldVCardMapORG);
       
   141 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL);
       
   142 	test(fieldSet->Count()==2);
       
   143 	
       
   144 	TInt index=fieldSet->Find(KUidContactFieldCompanyName);
       
   145 	test((*fieldSet)[index].ContentType().ContainsFieldType(KUidContactFieldCompanyName));
       
   146 
       
   147 	index=fieldSet->Find(KUidContactFieldPhoneNumber);
       
   148 	test((*fieldSet)[index].ContentType().ContainsFieldType(KUidContactFieldPhoneNumber));
       
   149 
       
   150 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL);
       
   151 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldCompanyName,KUidContactFieldVCardMapORG);
       
   152 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL);
       
   153 	AddFieldL(fieldSet,KStorageTypeDateTime,KUidContactFieldBirthday,KUidContactFieldVCardMapBDAY);
       
   154 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL);
       
   155 	index=fieldSet->Find(KUidContactFieldBirthday);
       
   156 	test(index!=KErrNotFound);
       
   157 	index=fieldSet->Find(KUidContactFieldCompanyName);
       
   158 	test(index!=KErrNotFound);
       
   159 //
       
   160 	index=fieldSet->FindNext(KUidContactFieldCompanyName,index+1);
       
   161 	test(index!=KErrNotFound);
       
   162 	
       
   163 	index=fieldSet->FindNext(KUidContactFieldCompanyName,index+1);
       
   164 	test(index==KErrNotFound);
       
   165 	index=fieldSet->Find(KUidContactFieldPhoneNumber);
       
   166 	index=fieldSet->FindNext(KUidContactFieldPhoneNumber,index+1);
       
   167 	test(index!=KErrNotFound);
       
   168 	
       
   169 	index=fieldSet->FindNext(KUidContactFieldPhoneNumber,index+1);
       
   170 	test(index!=KErrNotFound);
       
   171 	index=fieldSet->FindNext(KUidContactFieldPhoneNumber,index+1);
       
   172 	test(index!=KErrNotFound);
       
   173 	index=fieldSet->FindNext(KUidContactFieldPhoneNumber,index+1);
       
   174 	test(index==KErrNotFound);
       
   175 
       
   176 	test(fieldSet->Count()==7);
       
   177 	index=fieldSet->Find(KUidContactFieldCompanyName);
       
   178 	index=fieldSet->FindNext(KUidContactFieldCompanyName,index+1);
       
   179 	fieldSet->Remove(index);
       
   180 	test(fieldSet->Count()==6);
       
   181 	
       
   182 	index=fieldSet->Find(KUidContactFieldCompanyName);
       
   183 	test(fieldSet->FindNext(KUidContactFieldCompanyName,index+1)==KErrNotFound);
       
   184 	
       
   185 	fieldSet->Reset();
       
   186 	test(fieldSet->Count()==0);
       
   187 
       
   188 	CleanupStack::PopAndDestroy(); // fieldSet
       
   189 
       
   190 	fieldSet=CContactItemFieldSet::NewL();
       
   191 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldCompanyName,KUidContactFieldVCardMapUnusedN);
       
   192 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL);
       
   193 	test(fieldSet->Count()==2);
       
   194 	delete fieldSet;
       
   195 	}
       
   196 
       
   197 LOCAL_C void StoreRestoreTestL()
       
   198 	{
       
   199 	CStreamStore* blobstore=CBufStore::NewLC(3);
       
   200 	CStreamStore* textstore=CBufStore::NewLC(3);
       
   201 	CStreamStore* store=CBufStore::NewLC(3);
       
   202 	RStoreWriteStream stream;
       
   203 	TStreamId textId=stream.CreateLC(*textstore);
       
   204 //
       
   205 	CContactItemFieldSet* fieldSet=CContactItemFieldSet::NewLC();
       
   206 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldCompanyName,KUidContactFieldVCardMapORG);
       
   207 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL);
       
   208 	AddFieldL(fieldSet,KStorageTypeDateTime,KUidContactFieldBirthday,KUidContactFieldVCardMapBDAY);
       
   209 //
       
   210 	TStreamId streamId=fieldSet->StoreL(*store,stream,*blobstore);
       
   211 	CleanupStack::PopAndDestroy(); // fieldSet
       
   212 //
       
   213 	CContactItemFieldSet* fieldSet2=CContactItemFieldSet::NewLC();
       
   214 	CContactItemViewDef *viewDef=CContactItemViewDef::NewLC(CContactItemViewDef::EMaskFields,CContactItemViewDef::EMaskHiddenFields );
       
   215 	RStoreReadStream rstream;
       
   216 	rstream.OpenLC(*textstore,textId);
       
   217 	fieldSet2->RestoreL(*store,streamId,blobstore,*viewDef,rstream);
       
   218 	test(fieldSet2->Count()==3);
       
   219 	CntTest->TestField((*fieldSet2)[0],KStorageTypeText,KUidContactFieldCompanyName,KUidContactFieldVCardMapORG);
       
   220 	CntTest->TestField((*fieldSet2)[1],KStorageTypeText,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL);
       
   221 	CntTest->TestField((*fieldSet2)[2],KStorageTypeDateTime,KUidContactFieldBirthday,KUidContactFieldVCardMapBDAY);
       
   222 	CleanupStack::PopAndDestroy(7); // viewDef,fieldSet2 stream store textstore rstream,blobstore
       
   223 //zzz more to do here
       
   224 	}
       
   225 LOCAL_C void CommonIdArrayTestsL(CContactIdArray& aIds,TInt aId)
       
   226 	{
       
   227 	TInt index=aIds.Find(aId);
       
   228 	test(index!=KErrNotFound);
       
   229 	for (TInt ii=0;ii<5;ii++)
       
   230 		{
       
   231 		aIds.MoveL(index,ii);
       
   232 		test(aIds.Count()==5);
       
   233 		const TInt newPos=aIds.Find(aId);
       
   234 		test(newPos==ii);
       
   235 		aIds.MoveL(newPos,index);
       
   236 		test(aIds.Count()==5);
       
   237 		test(aIds.Find(aId)==index);
       
   238 		}
       
   239 	aIds.Remove(index);
       
   240 	test(aIds.Count()==4);
       
   241 	aIds.InsertL(index,aId);
       
   242 	test(aIds.Count()==5);
       
   243 	test(aIds.Find(aId)==index);
       
   244 	test(aIds[index]==aId);
       
   245 	}
       
   246 
       
   247 
       
   248 LOCAL_C void TestIdArrayL()
       
   249 	{
       
   250 	test.Next(_L("id array"));
       
   251 
       
   252 	CContactIdArray* ids=CContactIdArray::NewLC();
       
   253 	test(ids->Count()==0);
       
   254 	ids->AddL(3);
       
   255 	ids->AddL(78);
       
   256 	ids->AddL(1);
       
   257 	ids->AddL(9);
       
   258 	ids->AddL(20);
       
   259 	test(ids->Count()==5);
       
   260 	
       
   261 	CommonIdArrayTestsL(*ids,3);
       
   262 	CommonIdArrayTestsL(*ids,78);
       
   263 	CommonIdArrayTestsL(*ids,1);
       
   264 	CommonIdArrayTestsL(*ids,9);
       
   265 	CommonIdArrayTestsL(*ids,20);
       
   266 	
       
   267 	ids->Reset();
       
   268 	test(ids->Count()==0);
       
   269 	ids->AddL(1);
       
   270 	test(ids->Count()==1);
       
   271 	CleanupStack::PopAndDestroy(); // ids
       
   272 
       
   273 	ids=CContactIdArray::NewL();
       
   274 	test(ids->Count()==0);
       
   275 	ids->AddL(4);
       
   276 	test(ids->Count()==1);
       
   277 	delete ids;
       
   278 	}
       
   279 
       
   280 
       
   281 LOCAL_C void MainTestsL()
       
   282 //
       
   283 // Create a database in a store and add some contact cards
       
   284 //
       
   285 	{
       
   286 	CContactDatabase *db=CntTest->CreateDatabaseL();
       
   287 	TestFieldSetL();
       
   288 	TestThingyFieldsL();
       
   289 	TestIdArrayL();
       
   290 	CntTest->DeleteAllTemplateFieldsL();
       
   291 	
       
   292 	test.Next(_L("Labels"));
       
   293 
       
   294 	CContactCard* card=CContactCard::NewLC();
       
   295 	CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldGivenName);
       
   296 	field->SetLabelL(_L("Test label"));
       
   297 	field->TextStorage()->SetTextL(_L("x"));
       
   298 	card->AddFieldL(*field);
       
   299 	CleanupStack::Pop(); // field
       
   300 	TContactItemId id=db->AddNewContactL(*card);
       
   301 	CleanupStack::PopAndDestroy(); // card
       
   302 	card=STATIC_CAST(CContactCard*,db->OpenContactL(id));
       
   303 	CleanupStack::PushL(card);
       
   304 		{
       
   305 	CContactItemFieldSet& fieldSet3=card->CardFields();
       
   306 	test(fieldSet3.Count()==1);
       
   307 	test(fieldSet3[0].Label()==_L("Test label"));
       
   308 	fieldSet3[0].SetLabelL(_L("New test label"));
       
   309 		}
       
   310 	db->CommitContactL(*card);
       
   311 
       
   312 	CleanupStack::PopAndDestroy(); // card
       
   313 	card=STATIC_CAST(CContactCard*,db->ReadContactL(id));
       
   314 		{
       
   315 	CContactItemFieldSet& fieldSet4=card->CardFields();
       
   316 	test(fieldSet4.Count()==1);
       
   317 	test(fieldSet4[0].Label()==_L("New test label"));
       
   318 		}
       
   319 	delete card;
       
   320 	
       
   321 	test.Next(_L("Attributes"));
       
   322 
       
   323 	card=CContactCard::NewLC();
       
   324 	field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldGivenName);
       
   325 	field->SetHidden(ETrue);
       
   326 	field->SetReadOnly(ETrue);
       
   327 	field->SetSynchronize(EFalse);
       
   328 	field->TextStorage()->SetTextL(_L("x"));
       
   329 	card->AddFieldL(*field);
       
   330 	CleanupStack::Pop(); // field
       
   331 	id=db->AddNewContactL(*card);
       
   332 	CleanupStack::PopAndDestroy(); // card
       
   333 //
       
   334 	card=STATIC_CAST(CContactCard*,db->ReadContactLC(id));
       
   335 	CContactItemFieldSet& fieldSet5=card->CardFields();
       
   336 	test(fieldSet5.Count()==0); // ignore hidden fields by default
       
   337 	CleanupStack::PopAndDestroy(); // card
       
   338 //
       
   339 	card=STATIC_CAST(CContactCard*,db->OpenContactL(id));
       
   340 	CleanupStack::PushL(card);
       
   341 	CContactItemFieldSet& fieldSet5b=card->CardFields();
       
   342 	test(fieldSet5b.Count()==1); // ignore hidden fields by default
       
   343 	db->CloseContactL(id);
       
   344 //
       
   345 	CContactItemViewDef* viewDef=CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EIncludeHiddenFields);
       
   346     viewDef->AddL(KUidContactFieldMatchAll);
       
   347 	CContactViewDef* def=CContactViewDef::NewLC(viewDef);
       
   348     CleanupStack::Pop(); // viewDef
       
   349     db->SetViewDefinitionL(def);
       
   350     CleanupStack::Pop(); // def
       
   351 	CleanupStack::PopAndDestroy(); // card
       
   352 	card=STATIC_CAST(CContactCard*,db->OpenContactL(id));
       
   353 	CleanupStack::PushL(card);
       
   354 		{
       
   355 	CContactItemFieldSet& fieldSet6=card->CardFields();
       
   356 	test(fieldSet6.Count()==1); // now includes hidden fields
       
   357 	field=&fieldSet6[0];
       
   358 	test(field->IsHidden());
       
   359 	test(field->IsReadOnly());
       
   360 	test(!field->DoSynchronize());
       
   361 	field->SetHidden(EFalse);
       
   362 	field->SetReadOnly(EFalse);
       
   363 	field->SetSynchronize(ETrue);
       
   364 	field->SetDisabled(ETrue);
       
   365 		}
       
   366 		{
       
   367 	CContactItemFieldSet& fieldSet7=card->CardFields();
       
   368 	test(fieldSet7.Count()==1); 
       
   369 	db->CommitContactL(*card);
       
   370 
       
   371 	CleanupStack::PopAndDestroy(); // card
       
   372 	card=STATIC_CAST(CContactCard*,db->ReadContactL(id));
       
   373 		}
       
   374 		{
       
   375 	CContactItemFieldSet& fieldSet8=card->CardFields();
       
   376 	test(fieldSet8.Count()==1);
       
   377 	field=&fieldSet8[0];
       
   378 	test(!field->IsHidden());
       
   379 	test(!field->IsReadOnly());
       
   380 	test(field->DoSynchronize());
       
   381 	test(field->IsDisabled());
       
   382 	delete card;
       
   383 		}
       
   384 
       
   385 	test.Next(_L("Multiple cards"));
       
   386 
       
   387 	card=CContactCard::NewLC();
       
   388 	field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldGivenName);
       
   389 	field->TextStorage()->SetTextL(_L("x"));
       
   390 	card->AddFieldL(*field);
       
   391 	CleanupStack::Pop(); // field
       
   392 	field=&(card->CardFields())[0];
       
   393 	TContactItemId ids[5];
       
   394 	TInt ii=0;
       
   395 	for (ii=0;ii<5;ii++)
       
   396 		{
       
   397 		TBuf<16> label;
       
   398 		label.Format(KTestLabelName,ii);
       
   399 		field->SetLabelL(label);
       
   400 		field->SetHidden(ETrue);
       
   401 		field->SetReadOnly(EFalse);
       
   402 		field->SetSynchronize(EFalse);
       
   403 		field->SetDisabled(ii&1);
       
   404 		ids[ii]=db->AddNewContactL(*card);
       
   405 		}
       
   406 	CleanupStack::PopAndDestroy(); // card
       
   407 	for (ii=0;ii<5;ii++)
       
   408 		{
       
   409 		card=STATIC_CAST(CContactCard*,db->OpenContactL(ids[ii]));
       
   410 		CleanupStack::PushL(card);
       
   411 		test(card->CardFields().Count()==1);
       
   412 		field=&(card->CardFields())[0];
       
   413 		TBuf<16> label;
       
   414 		label.Format(KTestLabelName,ii);
       
   415 		test(field->Label()==label);
       
   416 		test(field->IsHidden());
       
   417 		test(!field->IsReadOnly());
       
   418 		test(!field->DoSynchronize());
       
   419 		TBool disabled=field->IsDisabled();
       
   420 		test(ii&1?disabled:!disabled);
       
   421 		// edit card
       
   422 		label.Format(KTestNewLabelName,ii);
       
   423 		field->SetLabelL(label);
       
   424 		field->SetHidden(EFalse);
       
   425 		field->SetReadOnly(ETrue);
       
   426 		field->SetSynchronize(ETrue);
       
   427 		TBool uaf=EFalse;
       
   428 		if (ii%1)
       
   429 			uaf=ETrue;
       
   430 		field->SetUserAddedField(uaf);
       
   431 		db->CommitContactL(*card);
       
   432 
       
   433 		CleanupStack::PopAndDestroy(); // card
       
   434 		}
       
   435 	for (ii=0;ii<5;ii++)
       
   436 		{
       
   437 		card=STATIC_CAST(CContactCard*,db->ReadContactL(ids[ii]));
       
   438 		test(card->CardFields().Count()==1);
       
   439 		field=&(card->CardFields())[0];
       
   440 		TBuf<16> label;
       
   441 		label.Format(KTestNewLabelName,ii);
       
   442 		test(field->Label()==label);
       
   443 		test(!field->IsHidden());
       
   444 		test(field->IsReadOnly());
       
   445 		test(field->DoSynchronize());
       
   446 		TBool uaf=field->UserAddedField();
       
   447 		test(ii%1?uaf:!uaf);
       
   448 		delete card;
       
   449 		}
       
   450 
       
   451 	test.Next(_L("Multiple fields"));
       
   452 
       
   453 	card=CContactCard::NewLC();
       
   454 	for (ii=0;ii<10;ii++)
       
   455 		{
       
   456 		field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldGivenName);
       
   457 		TBuf<16> label;
       
   458 		label.Format(KTestLabelName,ii);
       
   459 		field->SetLabelL(label);
       
   460 		field->SetHidden(ETrue);
       
   461 		field->SetReadOnly(EFalse);
       
   462 		field->SetSynchronize(ETrue);
       
   463 		field->TextStorage()->SetTextL(_L("x"));
       
   464 		card->AddFieldL(*field);
       
   465 		CleanupStack::Pop(); // field
       
   466 		}
       
   467 	id=db->AddNewContactL(*card);
       
   468 	CleanupStack::PopAndDestroy(); // card
       
   469 	card=STATIC_CAST(CContactCard*,db->OpenContactL(id));
       
   470 	CleanupStack::PushL(card);
       
   471 	for (ii=0;ii<10;ii++)
       
   472 		{
       
   473 		field=&(card->CardFields())[ii];
       
   474 		TBuf<16> label;
       
   475 		label.Format(KTestLabelName,ii);
       
   476 		test(field->Label()==label);
       
   477 		test(field->IsHidden());
       
   478 		test(!field->IsReadOnly());
       
   479 		test(field->DoSynchronize());
       
   480 		// edit fields
       
   481 		label.Format(KTestNewLabelName,ii);
       
   482 		field->SetLabelL(label);
       
   483 		field->SetHidden(EFalse);
       
   484 		field->SetReadOnly(EFalse);
       
   485 		field->SetSynchronize(EFalse);
       
   486 		}
       
   487 	db->CommitContactL(*card);
       
   488 	CleanupStack::PopAndDestroy(); // card
       
   489 	card=STATIC_CAST(CContactCard*,db->ReadContactL(id));
       
   490 	for (ii=0;ii<10;ii++)
       
   491 		{
       
   492 		field=&(card->CardFields())[ii];
       
   493 		TBuf<16> label;
       
   494 		label.Format(KTestNewLabelName,ii);
       
   495 		test(field->Label()==label);
       
   496 		test(!field->IsHidden());
       
   497 		test(!field->IsReadOnly());
       
   498 		test(!field->DoSynchronize());
       
   499 		}
       
   500 	delete card;
       
   501 	CntTest->CloseDatabase();
       
   502 	}
       
   503 
       
   504 void FieldIdTestsL()
       
   505 	{
       
   506 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
   507 	CContactCard* card=CContactCard::NewLC();
       
   508 	card->AddFieldL(*CContactItemField::NewLC(KStorageTypeText,KUidContactFieldCompanyName));
       
   509 	CleanupStack::Pop(); // New field
       
   510 	card->AddFieldL(*CContactItemField::NewLC(KStorageTypeText,KUidContactFieldGivenName));
       
   511 	CleanupStack::Pop(); // New field
       
   512 	card->AddFieldL(*CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber));
       
   513 	CleanupStack::Pop(); // New field
       
   514 //
       
   515 	TContactItemId id=db->AddNewContactL(*card);
       
   516 	CleanupStack::PopAndDestroy(); // card
       
   517 	card=NULL;
       
   518 //
       
   519 	card=STATIC_CAST(CContactCard*,db->ReadContactL(id));
       
   520 	CleanupStack::PushL(card);
       
   521 	CContactItemFieldSet& fieldSet2=card->CardFields();
       
   522 	for(TInt loop1=0;loop1<fieldSet2.Count();loop1++)
       
   523 		for(TInt loop2=0;loop2<fieldSet2.Count();loop2++)
       
   524 			if (loop1!=loop2)
       
   525 				test(fieldSet2[loop1].Id()!=fieldSet2[loop2].Id());
       
   526 	CleanupStack::PopAndDestroy(); // card
       
   527 	CntTest->CloseDatabase();
       
   528 	}
       
   529 
       
   530 const TUid KExtraUid1={0x1};
       
   531 const TUid KExtraUid2={0x2};
       
   532 const TUid KExtraUid3={0x12345678};
       
   533 
       
   534 void AdditionalUidTests1L()
       
   535 	{
       
   536 	CContactItemFieldSet* fieldSet=CContactItemFieldSet::NewLC();
       
   537 //
       
   538 	CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldCompanyName);
       
   539 	field->AddFieldTypeL(KExtraUid1);
       
   540 	field->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   541 	field->AddFieldTypeL(KExtraUid2);
       
   542 	field->AddFieldTypeL(KUidContactFieldAdditionalName);
       
   543 	field->AddFieldTypeL(KUidContactFieldAddress);
       
   544 	field->AddFieldTypeL(KExtraUid3);
       
   545 	fieldSet->AddL(*field);
       
   546 	CleanupStack::Pop(); // field
       
   547 //
       
   548 	test((*fieldSet)[0].ContentType().ContainsFieldType(KUidContactFieldCompanyName));
       
   549 	test((*fieldSet)[0].ContentType().ContainsFieldType(KUidContactFieldPhoneNumber));
       
   550 	test((*fieldSet)[0].ContentType().ContainsFieldType(KUidContactFieldAdditionalName));
       
   551 	test((*fieldSet)[0].ContentType().ContainsFieldType(KUidContactFieldAddress));
       
   552 	test((*fieldSet)[0].ContentType().ContainsFieldType(KExtraUid1));
       
   553 	test((*fieldSet)[0].ContentType().ContainsFieldType(KExtraUid2));
       
   554 	test((*fieldSet)[0].ContentType().ContainsFieldType(KExtraUid3));
       
   555 	CleanupStack::PopAndDestroy(); // fieldSet
       
   556 	}
       
   557 
       
   558 void AdditionalUidTests2L()
       
   559 	{
       
   560 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
   561 	CContactCard* card=CContactCard::NewLC();
       
   562 	CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KExtraUid1);
       
   563 	field->AddFieldTypeL(KUidContactFieldPhoneNumber);
       
   564 	field->AddFieldTypeL(KExtraUid2);
       
   565 	field->AddFieldTypeL(KUidContactFieldAdditionalName);
       
   566 	field->AddFieldTypeL(KExtraUid3);
       
   567 	field->AddFieldTypeL(KUidContactFieldAddress);
       
   568 	field->TextStorage()->SetTextL(_L("x"));
       
   569 	card->AddFieldL(*field);
       
   570 	CleanupStack::Pop(); // field
       
   571 //
       
   572 	TContactItemId id=db->AddNewContactL(*card);
       
   573 	CleanupStack::PopAndDestroy(); // card
       
   574 	card=NULL;
       
   575 //
       
   576 	card=STATIC_CAST(CContactCard*,db->ReadContactL(id));
       
   577 	CleanupStack::PushL(card);
       
   578 	CContactItemFieldSet& fieldSet2=card->CardFields();
       
   579 	test(fieldSet2[0].ContentType().ContainsFieldType(KExtraUid1));
       
   580 	test(fieldSet2[0].ContentType().ContainsFieldType(KExtraUid2));
       
   581 	test(fieldSet2[0].ContentType().ContainsFieldType(KExtraUid3));
       
   582 	test(fieldSet2[0].ContentType().ContainsFieldType(KUidContactFieldPhoneNumber));
       
   583 	test(fieldSet2[0].ContentType().ContainsFieldType(KUidContactFieldAdditionalName));
       
   584 	test(fieldSet2[0].ContentType().ContainsFieldType(KUidContactFieldAddress));
       
   585 
       
   586 	CleanupStack::PopAndDestroy(); // card
       
   587 //
       
   588 	CntTest->CloseDatabase();
       
   589 	}
       
   590 
       
   591 /** write fields to the card and read them back again */
       
   592 
       
   593 void AdditionalUidTests3L()
       
   594 	{
       
   595 	const TInt KNumberOfFields=6;
       
   596 	const TInt KFirstPronunciationField=3;
       
   597 
       
   598 	_LIT(KCoName,"Symbian Ltd.");
       
   599 	_LIT(K1stName,"Name1");
       
   600 	_LIT(KLastName,"Test1");
       
   601 	_LIT(KFormatCo,"%S\n");
       
   602 	_LIT(KFormat1st,"%S ");
       
   603 	_LIT(KFormatLast,"%S from ");
       
   604 	_LIT(KFormatPronunciation,"<%S>");
       
   605 	_LIT(KCoRead,"Sim'bee'an");
       
   606 	_LIT(K1stRead,"Jon");
       
   607 	_LIT(KLastRead,"Dough");
       
   608 
       
   609 	// do this so we can just loop rather than repeat code many times
       
   610 	const void* names[KNumberOfFields] = { &K1stName, &KLastName, &KCoName, &K1stRead, &KLastRead,&KCoRead};
       
   611 	const void* formats[] = {&KFormat1st, &KFormatLast, &KFormatCo};
       
   612 	TInt fieldIds[KNumberOfFields] = {KUidContactFieldGivenNameValue, KUidContactFieldFamilyNameValue, KUidContactFieldCompanyNameValue, 
       
   613 		KUidContactFieldGivenNamePronunciationValue, KUidContactFieldFamilyNamePronunciationValue, KUidContactFieldCompanyNamePronunciationValue};
       
   614 	test.Next(_L("Pronunciation fields"));
       
   615 
       
   616 
       
   617 	
       
   618 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
   619 	CContactCard* card=CContactCard::NewLC();
       
   620 	CContactItemField* field;
       
   621 
       
   622 	TInt i=0;	
       
   623 	for(i=0;i<KNumberOfFields;i++)
       
   624 		{	// write each field to the card as a text type
       
   625 		field=CContactItemField::NewLC(KStorageTypeText,TUid::Uid(fieldIds[i]));
       
   626 		field->TextStorage()->SetTextL(* ((const TDesC*) names[i]));
       
   627 		card->AddFieldL(*field);
       
   628 		CleanupStack::Pop(field); 
       
   629 		}
       
   630 	
       
   631 	TContactItemId id=db->AddNewContactL(*card);
       
   632 	CleanupStack::PopAndDestroy(card);
       
   633 	card=NULL;
       
   634  
       
   635 	card=STATIC_CAST(CContactCard*,db->ReadContactL(id));
       
   636 	CleanupStack::PushL(card);
       
   637 	CContactItemFieldSet& fieldSet=card->CardFields();
       
   638 
       
   639 	// alternate printing pronunciation then name
       
   640 	for(i=0;i<KNumberOfFields;i++)
       
   641 		{
       
   642 		const TInt delta = (i%2) ? 0: KFirstPronunciationField;
       
   643 		const TInt index = fieldSet.Find( TUid::Uid(fieldIds[i/2+delta]));
       
   644 		test(index!=KErrNotFound);		// field should be there
       
   645 		TPtrC text(fieldSet[index].TextStorage()->Text());
       
   646 		test(text.Compare(* ((const TDesC*) names[i/2+delta]))==0); // content should be what we expect
       
   647 		TPtrC format(KFormatPronunciation);
       
   648 		if(i%2) format.Set(* ((const TDesC*) formats[i/2]));
       
   649 		test.Printf(format ,&text);	// print this just to make sure
       
   650 		}
       
   651 	CleanupStack::PopAndDestroy(card); 
       
   652 	CntTest->CloseDatabase();
       
   653 	}
       
   654 
       
   655 
       
   656 void AdditionalUidTestsL()
       
   657 	{
       
   658 	AdditionalUidTests1L();
       
   659 	AdditionalUidTests2L();
       
   660 	AdditionalUidTests3L();
       
   661 	}
       
   662 
       
   663 /**
       
   664 
       
   665 @SYMTestCaseID     PIM-T-FIELD-0001
       
   666 
       
   667 */
       
   668 
       
   669 void DoTestsL()
       
   670     {
       
   671     CContactSyncChecker::ResetL();
       
   672 
       
   673 	test.Start(_L("@SYMTESTCaseID:PIM-T-FIELD-0001 Test fields"));
       
   674 
       
   675     CContactSyncChecker::ResetL();
       
   676 
       
   677 	TRAPD(ret,MainTestsL());
       
   678 	test.Printf(_L("MainTestsL() returned with %d\n"), ret);
       
   679 	test(ret==KErrNone);
       
   680 //
       
   681 	test.Next(_L("Is Full"));
       
   682 
       
   683 	TRAP(ret,TestIsFullL());
       
   684 	test(ret==KErrNone);
       
   685 //
       
   686 	test.Next(_L("Field Ids"));
       
   687 
       
   688 	TRAP(ret,FieldIdTestsL());
       
   689 	test(ret==KErrNone);
       
   690 //
       
   691 	test.Next(_L("Store restore test"));
       
   692 
       
   693 	TRAP(ret,StoreRestoreTestL());
       
   694 	test(ret==KErrNone);
       
   695 //
       
   696 	test.Next(_L("Additional Uids"));
       
   697 
       
   698 	TRAP(ret,AdditionalUidTestsL());
       
   699 	test(ret==KErrNone);
       
   700 //
       
   701 	test.Next(_L("Delete database"));
       
   702 
       
   703 	TRAP(ret,CntTest->DeleteDatabaseL());
       
   704 	test(ret==KErrNone);
       
   705     }
       
   706 
       
   707 GLDEF_C TInt E32Main()
       
   708 	{
       
   709     CntTest=new(ELeave) CCntTest;
       
   710 	CntTest->ConstructL(test,KDatabaseFileName);
       
   711     TRAPD(err,DoTestsL());
       
   712 	CntTest->EndTestLib(err);
       
   713 	return KErrNone;
       
   714     }