phonebookengines_old/contactsmodel/tsrc/T_ITEM.CPP
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     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 <cntdb.h>
       
    20 #include <cntitem.h>
       
    21 #include <cntfield.h>
       
    22 #include <cntfldst.h>
       
    23 #include "T_UTILS.H"
       
    24 
       
    25 CCntTest* CntTest=NULL;
       
    26 LOCAL_D RTest test(_L("T_ITEM"));
       
    27 
       
    28 const TPtrC KDatabaseFileName=_L("C:T_ITEM");
       
    29 
       
    30 #define KCardGivenName _L("Given Name #%d")
       
    31 #define KCardFamilyName _L("Family Name #%d")
       
    32 #define KCardFieldText _L("Card id %d, text field #%d")
       
    33 #define KCardFieldEditedText _L("UPDATED Card id %d, text field #%d")
       
    34 
       
    35 const TInt KNumTestContacts=20;
       
    36 
       
    37 /** Create a database in a store and add KNumTestContacts contact cards  */
       
    38 LOCAL_C void CreateDatabaseL()
       
    39 	{
       
    40 	CContactDatabase *db=CntTest->CreateDatabaseL();
       
    41 	CntTest->DeleteAllTemplateFieldsL();
       
    42 	test.Next(_L("Add basic cards"));
       
    43 	CContactCard* card=CContactCard::NewL();
       
    44 	CleanupStack::PushL(card);
       
    45 	for (TInt ii=0;ii<KNumTestContacts;ii++)
       
    46 		db->AddNewContactL(*card);
       
    47 	CleanupStack::PopAndDestroy(); // card
       
    48 	CntTest->CloseDatabase();
       
    49 	}
       
    50 	
       
    51 LOCAL_C void EditCardsL()
       
    52 	{
       
    53 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
    54 	// add fields to cards
       
    55 	test.Next(_L("Add text fields to cards"));
       
    56 	CContactIdArray* idList=CContactIdArray::NewLC(db->SortedItemsL());
       
    57 	TInt ii=0;
       
    58 	for (;ii<KNumTestContacts;ii++)
       
    59 		{
       
    60 		const TInt index=(*idList)[ii];
       
    61 		CContactItem* item=db->OpenContactL(index);
       
    62 		CleanupStack::PushL(item);
       
    63 		const TInt fieldCount=ii%9;
       
    64 		for (TInt jj=0;jj<fieldCount;jj++)
       
    65 			{
       
    66 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldDefinedText);
       
    67 			TBuf<32> text;
       
    68 			text.Format(KCardFieldText,index,jj);
       
    69 			field->TextStorage()->SetTextL(text);
       
    70 			item->AddFieldL(*field);
       
    71 			CleanupStack::Pop(); // field
       
    72 			}
       
    73 		db->CommitContactL(*item);
       
    74 		CleanupStack::PopAndDestroy(); // item
       
    75 		}
       
    76 	// test field values
       
    77 	test.Next(_L("Check field contents"));
       
    78 	for (ii=0;ii<KNumTestContacts;ii++)
       
    79 		{
       
    80 		const TInt index=(*idList)[ii];
       
    81 		CContactItem* item=db->ReadContactLC(index);
       
    82 		CContactItemFieldSet& fields=item->CardFields();
       
    83 		const TInt fieldCount=ii%9;
       
    84 		for (TInt jj=0;jj<fieldCount;jj++)
       
    85 			{
       
    86 			TBuf<32> text;
       
    87 			text.Format(KCardFieldText,index,jj);
       
    88 			TPtrC fieldText=fields[jj].TextStorage()->Text();
       
    89 			test(text==fieldText);
       
    90 			}
       
    91 		CleanupStack::PopAndDestroy(); // item
       
    92 		}
       
    93 	// test field values have been persisted, then edit them
       
    94 	CleanupStack::PopAndDestroy(); // idlist
       
    95 	CntTest->CloseDatabase();
       
    96 //
       
    97 	test.Next(_L("Edit text fields"));
       
    98 	db=CntTest->OpenDatabaseL();
       
    99 	idList=CContactIdArray::NewLC(db->SortedItemsL());
       
   100 	for (ii=0;ii<KNumTestContacts;ii++)
       
   101 		{
       
   102 		const TInt index=(*idList)[ii];
       
   103 		CContactItem* item=db->ReadContactLC(index);
       
   104 		CContactItemFieldSet& fields=item->CardFields();
       
   105 		const TInt fieldCount=fields.Count();
       
   106 		test(fieldCount==ii%9);
       
   107 		for (TInt jj=0;jj<fieldCount;jj++)
       
   108 			{
       
   109 			TBuf<32> text;
       
   110 			text.Format(KCardFieldText,index,jj);
       
   111 			TPtrC fieldText=fields[jj].TextStorage()->Text();
       
   112 			test(text==fieldText);
       
   113 			}
       
   114 		CleanupStack::PopAndDestroy(); // item
       
   115 		}
       
   116 	for (ii=0;ii<10;ii++)
       
   117 		{
       
   118 		const TInt index=(*idList)[ii];
       
   119 		CContactItem* item=db->OpenContactL(index);
       
   120 		CleanupStack::PushL(item);
       
   121 		CContactItemFieldSet& fields=item->CardFields();
       
   122 		const TInt fieldCount=fields.Count();
       
   123 		for (TInt jj=0;jj<fieldCount;jj++)
       
   124 			{
       
   125 			CContactItemField& field=fields[jj];
       
   126 			TBuf<64> text;
       
   127 			text.Format(KCardFieldEditedText,index,jj);
       
   128 			field.TextStorage()->SetTextL(text);
       
   129 			}
       
   130 		db->CommitContactL(*item);
       
   131 		CleanupStack::PopAndDestroy(); // item
       
   132 		}
       
   133 	for (ii=0;ii<10;ii++)
       
   134 		{
       
   135 		const TInt index=(*idList)[ii];
       
   136 		CContactItem* item=db->ReadContactLC(index);
       
   137 		CContactItemFieldSet& fields=item->CardFields();
       
   138 		const TInt fieldCount=fields.Count();
       
   139 		for (TInt jj=0;jj<fieldCount;jj++)
       
   140 			{
       
   141 			CContactItemField& field=fields[jj];
       
   142 			TBuf<64> text;
       
   143 			text.Format(KCardFieldEditedText,index,jj);
       
   144 			TPtrC fieldText=field.TextStorage()->Text();
       
   145 			test(text==fieldText);
       
   146 			}
       
   147 		CleanupStack::PopAndDestroy(); // item
       
   148 		}
       
   149 	CleanupStack::PopAndDestroy(); // idlist
       
   150 	CntTest->CloseDatabase();
       
   151 	}
       
   152 
       
   153 
       
   154 /** Test that the text of the field with index aCurrentField contains the content: "Card id 123, text field #n",
       
   155 	where n is aCurrentField
       
   156 @panic USER 84 if the content is not what was expected
       
   157 */
       
   158   LOCAL_C void TestField(CContactItemFieldSet& aFields, TInt aCurrentField, TInt aOriginalField)
       
   159 	{
       
   160 	TBuf<32> text;
       
   161 	text.Format(KCardFieldText,123,aOriginalField);
       
   162 	TPtrC fieldText=aFields[aCurrentField].TextStorage()->Text();
       
   163 	test(text==fieldText);
       
   164 	}
       
   165 
       
   166 LOCAL_C void TestFields(CContactItemFieldSet& aFields, TInt aField1,TInt aField2,TInt aField3,TInt aField4)
       
   167 	{
       
   168 	TestField(aFields,0,aField1);
       
   169 	TestField(aFields,1,aField2);
       
   170 	TestField(aFields,2,aField3);
       
   171 	TestField(aFields,3,aField4);
       
   172 	}
       
   173 
       
   174 LOCAL_C void MoveFields()
       
   175 	{
       
   176 	test.Next(_L("Field moving"));
       
   177 //
       
   178 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
   179 //
       
   180 	CContactCard* card=CContactCard::NewL();
       
   181 	CleanupStack::PushL(card);
       
   182 	TContactItemId testId=db->AddNewContactL(*card);
       
   183 	CleanupStack::PopAndDestroy(); // card
       
   184 //
       
   185 	CContactItem* item=db->OpenContactL(testId);
       
   186 	CleanupStack::PushL(item);
       
   187 	for (TInt loop=0;loop<4;loop++)
       
   188 		{
       
   189 		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText);
       
   190 		TBuf<32> text;
       
   191 		text.Format(KCardFieldText,123,loop);
       
   192 		field->TextStorage()->SetTextL(text);
       
   193 		item->AddFieldL(*field);
       
   194 		CleanupStack::Pop(); // field
       
   195 		}
       
   196 	db->CommitContactL(*item);
       
   197 //
       
   198 	CContactItemFieldSet& fields=item->CardFields();
       
   199 	fields.Move(0,1);
       
   200 	TestFields(fields,1,0,2,3);
       
   201 	fields.Move(0,3);
       
   202 	TestFields(fields,0,2,3,1);
       
   203 	fields.Move(0,2);
       
   204 	TestFields(fields,2,3,0,1);
       
   205 	fields.Move(1,3);
       
   206 	TestFields(fields,2,0,1,3);
       
   207 	fields.Move(2,3);
       
   208 	TestFields(fields,2,0,3,1);
       
   209 	fields.Move(1,2);
       
   210 	TestFields(fields,2,3,0,1);
       
   211 //
       
   212 	CleanupStack::PopAndDestroy(); // item
       
   213 	CntTest->CloseDatabase();
       
   214 	}
       
   215 
       
   216 LOCAL_C void FieldSetTests()
       
   217 	{
       
   218 	CntTest->OpenDatabaseL();
       
   219 //
       
   220 	CContactCard* card=CContactCard::NewL();
       
   221 	CleanupStack::PushL(card);
       
   222 //
       
   223 	AddFieldL(card,KStorageTypeText,KUidContactFieldNone,KUidContactFieldVCardMapUnusedN);
       
   224 //
       
   225 	CContactItemFieldSet *fieldSet=CContactItemFieldSet::NewLC();
       
   226 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL);
       
   227 	AddFieldL(fieldSet,KStorageTypeText,KUidContactFieldSuffixName,KUidContactFieldVCardMapUnusedN);
       
   228 	(*fieldSet)[1].SetDisabled(ETrue);
       
   229 //
       
   230 	card->UpdateFieldSet(fieldSet);
       
   231 	CleanupStack::Pop(); // fieldSet
       
   232 //
       
   233 	CContactItemFieldSet &cardFieldSet=card->CardFields();
       
   234 	test(cardFieldSet[0].ContentType().ContainsFieldType(KUidContactFieldPhoneNumber));
       
   235 	test(cardFieldSet[1].ContentType().ContainsFieldType(KUidContactFieldSuffixName));
       
   236 	test(cardFieldSet[1].IsDisabled());
       
   237 //
       
   238 	CContactItemField *field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldAddress);
       
   239 	card->InsertFieldL(*field,1);
       
   240 	CleanupStack::Pop(); // field
       
   241 	test(cardFieldSet[1].ContentType().ContainsFieldType(KUidContactFieldAddress));
       
   242 	test(cardFieldSet[1].IsDisabled()==EFalse);
       
   243 //
       
   244 	card->RemoveField(1);
       
   245 	test(cardFieldSet[1].ContentType().ContainsFieldType(KUidContactFieldSuffixName));
       
   246 //
       
   247 	card->RemoveField(0);
       
   248 	test(cardFieldSet[0].ContentType().ContainsFieldType(KUidContactFieldSuffixName));
       
   249 //
       
   250 	field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldEMail);
       
   251 	card->InsertFieldL(*field,0);
       
   252 	CleanupStack::Pop(); // field
       
   253 	test(cardFieldSet[0].ContentType().ContainsFieldType(KUidContactFieldEMail));
       
   254 //
       
   255 	CleanupStack::PopAndDestroy(); // card
       
   256 	CntTest->CloseDatabase();
       
   257 	}
       
   258 
       
   259 
       
   260 /** Delete the surname field of the contact indicated by the index. Will Leave if the contact has no surname 
       
   261 @leave	KErrNotFound	contact has no surname
       
   262 @param		aDb		The contact database
       
   263 @param		aIndex	The index of the contact whose surname will be removed
       
   264 */
       
   265 LOCAL_C void TestDeleteNameL(CContactDatabase& aDb, TInt aIndex) 
       
   266 	{
       
   267 	CContactItem* item=aDb.OpenContactL(aIndex);
       
   268 	CleanupStack::PushL(item);
       
   269 	CContactItemFieldSet& fields=item->CardFields();
       
   270 	TInt i = fields.Find(KUidContactFieldFamilyName);
       
   271 	User::LeaveIfError(i);
       
   272 	fields[i].TextStorage()->SetTextL(KNullDesC);
       
   273 	aDb.CommitContactL(*item);
       
   274 	CleanupStack::PopAndDestroy(item);
       
   275 	}
       
   276 
       
   277 
       
   278 /** Add first and last name fields to the contact in the format "Given Name #n" and "Family Name #n",
       
   279 	where n is the supplied index.
       
   280 @param		aItem	The contact database
       
   281 @param		aIndex	The number attached to the first and last names
       
   282 */
       
   283 LOCAL_C void AddNamesL(CContactItem& aItem, TInt aIndex)
       
   284 	{
       
   285 	CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldFamilyName);
       
   286 	TBuf<32> text;
       
   287 	text.Format(KCardFamilyName,aIndex);
       
   288 	field->TextStorage()->SetTextL(text);
       
   289 	aItem.AddFieldL(*field);
       
   290 	CleanupStack::Pop(field); 
       
   291 	field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldGivenName);
       
   292 	text.Format(KCardGivenName,aIndex);
       
   293 	field->TextStorage()->SetTextL(text);
       
   294 	aItem.AddFieldL(*field);
       
   295 	CleanupStack::Pop(field); 
       
   296 	}
       
   297 
       
   298 
       
   299 
       
   300 LOCAL_C void DeleteNamesL()
       
   301 	{
       
   302 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
   303 	// add fields to cards
       
   304 	test.Next(_L("Add names to cards"));
       
   305 	CContactIdArray* idList=CContactIdArray::NewLC(db->SortedItemsL());
       
   306 	TInt ii=0;
       
   307 	for (;ii<KNumTestContacts;ii++)
       
   308 		{
       
   309 		const TInt index=(*idList)[ii];
       
   310 		CContactItem* item=db->OpenContactL(index);
       
   311 		CleanupStack::PushL(item);
       
   312 		const TInt fieldCount=ii%9;
       
   313 		for (TInt jj=0;jj<fieldCount;jj++)
       
   314 			{
       
   315 			CContactItemField* field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldDefinedText);
       
   316 			TBuf<32> text;
       
   317 			text.Format(KCardFieldText,index,jj);
       
   318 			field->TextStorage()->SetTextL(text);
       
   319 			item->AddFieldL(*field);
       
   320 			CleanupStack::Pop(field); // 
       
   321 			}
       
   322 		AddNamesL(*item, ii);
       
   323 		db->CommitContactL(*item);
       
   324 		CleanupStack::PopAndDestroy(); // item
       
   325 		}
       
   326 	
       
   327 	// test field values
       
   328 	// test field values have been persisted, then edit them
       
   329 	CleanupStack::PopAndDestroy(); // idlist
       
   330 	CntTest->CloseDatabase();
       
   331 
       
   332 	db=CntTest->OpenDatabaseL();
       
   333 	idList=CContactIdArray::NewLC(db->SortedItemsL());
       
   334 	test.Next(_L("Delete last names"));
       
   335 	for (ii=0;ii<KNumTestContacts;ii++)
       
   336 		{
       
   337 		TestDeleteNameL(*db, (*idList)[ii] );
       
   338 		}
       
   339 
       
   340 	test.Next(_L("Check field contents"));
       
   341 	for (ii=0;ii<KNumTestContacts;ii++)
       
   342 		{
       
   343 		const TInt index=(*idList)[ii];
       
   344 		CContactItem* item=db->ReadContactLC(index);
       
   345 		CContactItemFieldSet& fields=item->CardFields();
       
   346 		TInt i = fields.Find(KUidContactFieldFamilyName);
       
   347 		test(i==KErrNotFound);
       
   348 		CleanupStack::PopAndDestroy(item); 
       
   349 		}
       
   350 	CleanupStack::PopAndDestroy(idList); 
       
   351 	CntTest->CloseDatabase();
       
   352 	}
       
   353 
       
   354 
       
   355 /** Test CContactItem::Guid method. */
       
   356 LOCAL_C void TestGuidL()
       
   357 	{
       
   358 	CContactCard* card=CContactCard::NewLC();
       
   359 	TPtrC ptr = card->Guid();
       
   360 	test(ptr==KNullDesC);
       
   361 	CntTest->OpenDatabaseL();
       
   362 	CContactDatabase& db = *(CntTest->Db());
       
   363 	TContactItemId id = db.AddNewContactL(*card);
       
   364 	CleanupStack::PopAndDestroy(card);
       
   365 	CContactItem* card2 = db.OpenContactL(id);
       
   366 	CleanupStack::PushL(card2);
       
   367 	TPtrC guid = card2->Guid();
       
   368 	test(guid.Length()!=0);
       
   369 	db.CloseContactL(id);
       
   370 	CleanupStack::PopAndDestroy(card2);
       
   371 	CntTest->CloseDatabase();
       
   372 	}
       
   373 
       
   374 /**
       
   375 
       
   376 @SYMTestCaseID     PIM-T-ITEM-0001
       
   377 
       
   378 */
       
   379 
       
   380 void DoTestsL()
       
   381     {
       
   382 	test.Start(_L("@SYMTestCaseID:PIM-T-ITEM-0001 Create new database"));
       
   383 	CreateDatabaseL();
       
   384 	TRAPD(ret,EditCardsL());
       
   385 	test(ret==KErrNone);
       
   386 	TRAP(ret,MoveFields());
       
   387 	test(ret==KErrNone);
       
   388 	TRAP(ret,FieldSetTests());
       
   389 	test(ret==KErrNone);
       
   390 	test.Next(_L("Test CContactItem::Guid method"));
       
   391 	TestGuidL();
       
   392 	TRAP(ret,DeleteNamesL());
       
   393 	test(ret==KErrNone);
       
   394 	test.Next(_L("Delete database"));
       
   395 	CntTest->CloseDatabase();
       
   396 	CntTest->DeleteDatabaseL();
       
   397     }
       
   398 
       
   399 GLDEF_C TInt E32Main()
       
   400 	{
       
   401 	__UHEAP_MARK;
       
   402     CntTest=new(ELeave) CCntTest;
       
   403 	CntTest->ConstructL(test,KDatabaseFileName);
       
   404     TRAPD(err,DoTestsL());
       
   405 	CntTest->EndTestLib(err);
       
   406 	__UHEAP_MARKEND;
       
   407 	return KErrNone;
       
   408     }