phonebookengines_old/contactsmodel/tsrc/T_VERS.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 <s32mem.h>
       
    20 #include <cntdb.h>
       
    21 #include <cntitem.h>
       
    22 #include <cntfldst.h>
       
    23 #include <e32def.h>
       
    24 #include "T_VERS.H"
       
    25 #include "T_UTILS.H"
       
    26 #include <coreappstest/testserver.h>
       
    27 
       
    28 enum ImpExpTestFlags
       
    29 	{
       
    30 	EImpExpTestMultipleEntries=0x1,
       
    31 	EImpExpTestModifyEntries=0x2,
       
    32 	EImpExpTestExportExtraEntry=0x4,
       
    33 	};
       
    34 
       
    35 CCntTest* CntTest=NULL;
       
    36 LOCAL_D RTest test(_L("T_VERS"));
       
    37 LOCAL_D CContactIdArray* TheIds;
       
    38 
       
    39 LOCAL_D CArrayPtr<CContactItem>* TheItems;
       
    40 LOCAL_D CArrayPtr<CContactItem>* TheItems2;
       
    41 LOCAL_D CArrayPtr<CContactItem>* TheItems3;
       
    42 LOCAL_D CArrayPtr<CContactItem>* TheItems4;
       
    43 
       
    44 const TPtrC KDatabaseFileName=_L("C:T_VERS");
       
    45 
       
    46 _LIT(KVCardFileToImport,"c:\\t_vers.vcf");
       
    47 
       
    48 //
       
    49 // CVersitTest
       
    50 //
       
    51 
       
    52 CVersitTest::CVersitTest()
       
    53 	{}
       
    54 
       
    55 CVersitTest::~CVersitTest()
       
    56 	{
       
    57 	delete iParser;
       
    58 	delete iStore;
       
    59 	}
       
    60 
       
    61 void CVersitTest::StreamInL()
       
    62 //Convert the iVersit into stream format, save it as "VersitIn"
       
    63 //and internalize it as a CVersitParser
       
    64 	{
       
    65 	CFileStore* store = CDirectFileStore::ReplaceLC(CntTest->Fs(),_L("c:\\Versit2"),EFileWrite);
       
    66 	store->SetTypeL(KDirectFileStoreLayoutUid);
       
    67 	RStoreWriteStream outstream;
       
    68 	TStreamId id = outstream.CreateLC(*store);
       
    69 	TInt length=iVersit.Length();
       
    70 	for (TInt ii=0; ii<length; ii++)
       
    71 		outstream.WriteInt8L(iVersit[ii]);
       
    72 	outstream.CommitL();
       
    73 	CleanupStack::PopAndDestroy();
       
    74 	store->SetRootL(id);
       
    75 	store->CommitL();
       
    76 	CleanupStack::PopAndDestroy();
       
    77 	store = CDirectFileStore::OpenLC(CntTest->Fs(),_L("c:\\Versit2"),EFileRead); //retrieve stream
       
    78 	RStoreReadStream instream;
       
    79 	instream.OpenLC(*store,store->Root());
       
    80 	iParser->InternalizeL(instream);
       
    81 	CleanupStack::PopAndDestroy(2); //store + stream
       
    82 	}
       
    83 
       
    84 void CVersitTest::StreamLCC(RReadStream& aStream, TInt aMode, TInt aContactId)
       
    85 	{
       
    86 	switch(aMode)
       
    87 		{
       
    88 		case 1:
       
    89 			GetSampleVersitL();
       
    90 			break;
       
    91 		case 2:
       
    92 			GetSampleVersit2L(aContactId);
       
    93 			break;
       
    94 		case 3:
       
    95 			GetSampleVersit3L();
       
    96 			break;
       
    97 		}
       
    98 	TFileName versitIn;
       
    99 	versitIn.Format(_L("c:\\VersitIn%d"),aMode);
       
   100 	CFileStore* store = CDirectFileStore::ReplaceLC(CntTest->Fs(),versitIn,EFileWrite);
       
   101 	store->SetTypeL(KDirectFileStoreLayoutUid);
       
   102 	RStoreWriteStream outstream;
       
   103 	TStreamId id = outstream.CreateLC(*store);
       
   104 	TInt length=iVersit.Length();
       
   105 	for (TInt ii=0; ii<length; ii++)
       
   106 		outstream.WriteInt8L(iVersit[ii]);
       
   107 	outstream.CommitL();
       
   108 	CleanupStack::PopAndDestroy();
       
   109 	store->SetRootL(id);
       
   110 	store->CommitL();
       
   111  	CleanupStack::PopAndDestroy();	// store
       
   112 	store = CDirectFileStore::OpenLC(CntTest->Fs(),versitIn,EFileRead); //retrieve stream
       
   113 	RStoreReadStream instream;
       
   114 	instream.OpenLC(*store,store->Root());
       
   115 	CleanupStack::Pop();	// instream
       
   116 	aStream=RReadStream(instream);
       
   117 	}
       
   118 
       
   119 void CVersitTest::StreamOutL()
       
   120 	{
       
   121 	}
       
   122 
       
   123 void CVersitTest::SaveFileVersitL()
       
   124 // Save iParser into VersitFile	
       
   125 	{
       
   126 	if (iParser)
       
   127 		{
       
   128 		User::LeaveIfError(CntTest->Fs().Connect());
       
   129 		RFile file;
       
   130 		if (file.Replace(CntTest->Fs(),_L("C:\\VersitFile"),EFileWrite)==KErrNone)
       
   131 			iParser->ExternalizeL(file);
       
   132 		}
       
   133 	}
       
   134 
       
   135 //
       
   136 // CVCardTest
       
   137 //
       
   138 
       
   139 void CVCardTest::LoadFileVersitL()
       
   140 // Read VCard.vcf into iVersit	
       
   141 	{
       
   142 	iParser=CParserVCard::NewL();
       
   143 	User::LeaveIfError(CntTest->Fs().Connect());
       
   144 	RFile file;
       
   145 	if (file.Open(CntTest->Fs(),_L("C:\\VCard.vcf"),EFileRead)==KErrNone)
       
   146 		{		
       
   147 		TInt start=0;
       
   148 		iParser->InternalizeL(file,start);
       
   149 		}
       
   150 	}
       
   151 	
       
   152 LOCAL_C void DisplayField(TInt aFieldNumber, const CContentType &aContentType, const TDesC &aDes)
       
   153 	{
       
   154 	TBuf<128> out;
       
   155 	out.Format(_L("Field %d mapped to %x {"),aFieldNumber,aContentType.Mapping());
       
   156 	for(TInt loop=0;loop<aContentType.FieldTypeCount();loop++)
       
   157 		{
       
   158 		if (loop>0)
       
   159 			out.Append(TChar(','));
       
   160 		out.AppendFormat(_L("%x"),aContentType.FieldType(loop));
       
   161 		}
       
   162 	out.AppendFormat(_L("} is %S store type"),&aDes);
       
   163 	test.Printf(out);
       
   164 	}
       
   165 
       
   166 LOCAL_C void DisplayDatabase()
       
   167 	{
       
   168 	TContactItemId theid;
       
   169 	CContactItem* contactItem=NULL;
       
   170 	TContactIter iter(*CntTest->Db());
       
   171 	theid=iter.FirstL();
       
   172 	while(theid!=KNullContactId )
       
   173 		{
       
   174 		test.Printf(_L("\n========================ROW ID %d"),theid);
       
   175 		contactItem=CntTest->Db()->ReadContactLC(theid);
       
   176 		CContactItemFieldSet& fieldSet=contactItem->CardFields();
       
   177 		TInt fieldcount=fieldSet.Count();
       
   178 		test.Printf(_L(" %d fields"),fieldcount);
       
   179 		for (TInt ii=0;ii<fieldcount;ii++)
       
   180 			{
       
   181 	 		const CContentType &content=fieldSet[ii].ContentType();
       
   182 			test.Printf(_L("\nLabel ["));
       
   183 			test.Printf(fieldSet[ii].Label());
       
   184 			test.Printf(_L("]\n"));
       
   185 			switch(fieldSet[ii].StorageType())
       
   186 				{
       
   187 			case KStorageTypeText: 
       
   188 				{
       
   189 				DisplayField(ii, content, _L("Text"));
       
   190 				test.Printf(_L("\n"));
       
   191 				TPtrC fieldText=fieldSet[ii].TextStorage()->Text();
       
   192 //				test.Printf(fieldText);
       
   193 				test.Printf(_L("\tLength = %d"),fieldText.Length());
       
   194 				}
       
   195 				break;
       
   196 			case KStorageTypeStore:
       
   197 				DisplayField(ii, content, _L("Store"));
       
   198 				break;
       
   199 			case KStorageTypeDateTime:
       
   200 				DisplayField(ii, content, _L("DateTime"));
       
   201 				break;
       
   202 			case KStorageTypeContactItemId:
       
   203 				DisplayField(ii, content, _L("ContactItemId"));
       
   204 				break;
       
   205 			default:
       
   206 				DisplayField(ii, content, _L("unknown"));
       
   207 				break;
       
   208 				}
       
   209 //			test.Getch();
       
   210 			}
       
   211 	 	theid=iter.NextL();
       
   212 		CleanupStack::PopAndDestroy(); // contactItem
       
   213 		}
       
   214 	test.Printf(_L("\nEnd of the database\n"));
       
   215 	}
       
   216 	
       
   217 LOCAL_C void AddLocalLabels()
       
   218 	{
       
   219 	TContactItemId theid;
       
   220 	TContactIter iter(*CntTest->Db());
       
   221 	theid=iter.FirstL();
       
   222 	while(theid!=KNullContactId )
       
   223 		{
       
   224 		CContactItem* contactItem=CntTest->Db()->OpenContactL(theid);
       
   225 		CleanupStack::PushL(contactItem);
       
   226 		CContactItemFieldSet& fieldSet=contactItem->CardFields();
       
   227 		TInt fieldcount=fieldSet.Count();
       
   228 		for (TInt ii=0;ii<fieldcount;ii=ii+2)
       
   229 			{
       
   230 	 		TPtrC labelName(_L("aTestLabelName"));
       
   231 			fieldSet[ii].SetLabelL(labelName);
       
   232 			}
       
   233 	 	theid=iter.NextL();
       
   234 		CntTest->Db()->CommitContactL(*contactItem);
       
   235 		CleanupStack::PopAndDestroy();	// contactitem
       
   236 		}				
       
   237 	}
       
   238 
       
   239 LOCAL_C void DecAccessCount()
       
   240 	{
       
   241 	TContactItemId theid;
       
   242 	TContactIter iter(*CntTest->Db());
       
   243 	theid=iter.FirstL();
       
   244 	while(theid!=KNullContactId )
       
   245 		{
       
   246 		CContactItem* contactItem=CntTest->Db()->OpenContactL(theid);
       
   247 		CleanupStack::PushL(contactItem);
       
   248 		contactItem->DecAccessCount();
       
   249 		CntTest->Db()->CommitContactL(*contactItem);
       
   250 		theid=iter.NextL();
       
   251 		CleanupStack::PopAndDestroy();	// contactitem
       
   252 		}				
       
   253 	}
       
   254 
       
   255 /*
       
   256 LOCAL_C void RemoveBlankFields(CContactItem &aItem)
       
   257 	{
       
   258 	CContactItemFieldSet& fieldSet=aItem.CardFields();
       
   259 	for(TInt loop1=0;loop1<fieldSet.Count();)
       
   260 		{
       
   261 		if (fieldSet[loop1].Storage()->IsFull())
       
   262 			loop1++;
       
   263 		else
       
   264 			fieldSet.Remove(loop1);
       
   265 		}
       
   266 	}
       
   267 
       
   268 LOCAL_C void TestExportCard(CContactItem &aItem, const CContactItem &aOriginal)
       
   269 	{
       
   270 	CContactItemFieldSet& fieldSet=aItem.CardFields();
       
   271 	CContactCard *origCopy=CContactCard::NewLC(&aOriginal);
       
   272 //
       
   273 	RemoveBlankFields(*origCopy);
       
   274 	RemoveBlankFields(aItem);
       
   275 	CContactItemFieldSet& origFieldSet=origCopy->CardFields();
       
   276 //
       
   277 	test(fieldSet.Count()==origFieldSet.Count());
       
   278 	for(TInt loop=0;loop<fieldSet.Count();loop++)
       
   279 		{
       
   280 		CContactItemField& field=fieldSet[loop];
       
   281 		CContactItemField& origField=origFieldSet[loop];
       
   282 		switch(field.StorageType())
       
   283 			{
       
   284 			case KStorageTypeText:
       
   285 				{
       
   286 				TPtrC fieldTxt(field.TextStorage()->Text());
       
   287 				TPtrC origFieldTxt(origField.TextStorage()->Text());
       
   288 				test(fieldTxt==origFieldTxt);
       
   289 				}
       
   290 				break;
       
   291 			case KStorageTypeDateTime:
       
   292 				{
       
   293 				const TDateTime dt1(field.DateTimeStorage()->Time().DateTime());
       
   294 				const TDateTime dt2(origField.DateTimeStorage()->Time().DateTime());
       
   295 				test(dt1.Year()==dt2.Year());
       
   296 				test(dt1.Month()==dt2.Month());
       
   297 				test(dt1.Day()==dt2.Day());
       
   298 				}				
       
   299 				break;
       
   300 			case KStorageTypeStore:
       
   301 			case KStorageTypeContactItemId:
       
   302 				break;
       
   303 			}
       
   304 		CntTest->CompareFields(field,origField);
       
   305 		}
       
   306 	CleanupStack::PopAndDestroy();	// origCopy
       
   307 	}
       
   308 
       
   309 LOCAL_C void SetTestField(CContactItemFieldSet &aFieldSet,TInt aIndex, TBool aIsModifiedVersion)
       
   310 	{
       
   311 	CContactItemField& field=aFieldSet[aIndex];
       
   312 	switch(field.StorageType())
       
   313 		{
       
   314 		case KStorageTypeText:
       
   315 			{
       
   316 			TBuf<16> buf;
       
   317 			if (aIsModifiedVersion)
       
   318 				buf.Format(_L("TxtMod[%d]"),aIndex);
       
   319 			else
       
   320 				buf.Format(_L("Txt[%d]"),aIndex);
       
   321 			field.TextStorage()->SetTextL(buf);
       
   322 			}
       
   323 			break;
       
   324 		case KStorageTypeDateTime:
       
   325 			if (aIsModifiedVersion)
       
   326 				field.DateTimeStorage()->SetTime(TTime(aIndex*1000000));
       
   327 			else
       
   328 				field.DateTimeStorage()->SetTime(TTime(aIndex*2000000));
       
   329 			break;
       
   330 		case KStorageTypeStore:
       
   331 		case KStorageTypeContactItemId:
       
   332 			break;
       
   333 		}
       
   334 	}
       
   335 
       
   336 LOCAL_C void DuplicateTestField(CContactItemFieldSet &aFieldSet,TInt aIndex)
       
   337 	{
       
   338 	CContactItemField *field=CContactItemField::NewLC(aFieldSet[aIndex]);
       
   339 	aFieldSet.InsertL(aIndex+1,*field);
       
   340 	CleanupStack::Pop();	// field
       
   341 	}
       
   342 
       
   343 LOCAL_C void SetTestFields(CContactItem &aContact, TInt aTestNum, TUint aTestFlags, TBool aIsModifiedVersion)
       
   344 	{
       
   345 	if (aTestFlags&EImpExpTestMultipleEntries)
       
   346 		{
       
   347 		for(TInt loop=aTestNum;loop<aContact.CardFields().Count();loop+=(aTestNum+1))
       
   348 			SetTestField(aContact.CardFields(),loop,aIsModifiedVersion);
       
   349 		}
       
   350 	else
       
   351 		SetTestField(aContact.CardFields(),aTestNum,aIsModifiedVersion);
       
   352 	}
       
   353 
       
   354 LOCAL_C CContactItem* ReadContactIncHiddenFieldsLC(CContactDatabase* aDb, TContactItemId aContactId)
       
   355 	{
       
   356 	CContactItemViewDef *viewDef=CContactItemViewDef::NewLC(CContactItemViewDef::EMaskFields,CContactItemViewDef::EIncludeHiddenFields);
       
   357 	CContactItem *contact=aDb->ReadContactL(aContactId,*viewDef);
       
   358 	CleanupStack::PopAndDestroy();	// viewDef
       
   359 	CleanupStack::PushL(contact);
       
   360 	return(contact);
       
   361 	}
       
   362 
       
   363 LOCAL_C void doTestExportImportContactL(CContactItem *aTemplateItem, TInt aTestNum, TUint aTestFlags)
       
   364 	{
       
   365 	CContactItem *originalContact=CContactCard::NewLC(aTemplateItem);
       
   366 	SetTestFields(*originalContact,aTestNum,aTestFlags,EFalse);
       
   367 	TContactItemId addId=CntTest->Db()->AddNewContactL(*originalContact);
       
   368 	CContactIdArray *ids=CContactIdArray::NewLC();
       
   369 	ids->AddL(addId);
       
   370 	const TInt KMaxFormats=8;
       
   371 	for(TInt format=0;format<KMaxFormats;format++)
       
   372 		{
       
   373 		TUint formatFlags=0;
       
   374 		if (format&0x1)
       
   375 			formatFlags|=CContactDatabase::EIncludeX;
       
   376 		if (format&0x2)
       
   377 			formatFlags|=CContactDatabase::ETTFormat;
       
   378 		if (format&0x4)
       
   379 			{
       
   380 			if (aTestFlags&EImpExpTestModifyEntries)
       
   381 				continue;
       
   382 			formatFlags|=CContactDatabase::EExcludeUid;
       
   383 			}
       
   384 		else if (!(formatFlags&CContactDatabase::ETTFormat))
       
   385 			continue;
       
   386 		CVCardTestStore* store=CntTest->ExportContactsLC(ids,formatFlags);
       
   387 //
       
   388 		if (aTestFlags&EImpExpTestModifyEntries)
       
   389 			{
       
   390 			CContactItem* modifyItem=CntTest->Db()->OpenContactLX(originalContact->Id());
       
   391 			CleanupStack::PushL(modifyItem);
       
   392 			SetTestFields(*modifyItem,aTestNum,aTestFlags,ETrue);
       
   393 			CntTest->Db()->CommitContactL(*modifyItem);
       
   394 			CleanupStack::PopAndDestroy(2);	// modifyItem,modifyItem->Close()
       
   395 			}
       
   396 		if (aTestFlags&EImpExpTestExportExtraEntry)
       
   397 			{
       
   398 			CContactItem* modifyItem=CntTest->Db()->OpenContactLX(originalContact->Id());
       
   399 			CleanupStack::PushL(modifyItem);
       
   400 			SetTestFields(*modifyItem,(aTestNum+5)%10,0,EFalse);
       
   401 			SetTestFields(*modifyItem,(aTestNum+1)%10,0,EFalse);
       
   402 			CntTest->Db()->CommitContactL(*modifyItem);
       
   403 			CleanupStack::PopAndDestroy(2);	// modifyItem,modifyItem->Close()
       
   404 			}
       
   405 //
       
   406 		CArrayPtr<CContactItem>* items=CntTest->ImportContactsLC(store,formatFlags);
       
   407 		test(items->Count()==1);
       
   408 		if (!(formatFlags&CContactDatabase::EExcludeUid))
       
   409 			TestExportCard(*(*items)[0],*originalContact);
       
   410 		TContactItemId addedId=(*items)[0]->Id();
       
   411 		CleanupStack::PopAndDestroy();	// items->ResetAndDestroy()
       
   412 		CContactItem *reloadedItem=ReadContactIncHiddenFieldsLC(CntTest->Db(),addedId);
       
   413 		TestExportCard(*reloadedItem,*originalContact);
       
   414 		CleanupStack::PopAndDestroy();	// reloadedItem
       
   415 		CleanupStack::PopAndDestroy();	// store
       
   416 		}
       
   417 	CleanupStack::PopAndDestroy();	// ids
       
   418 	CleanupStack::PopAndDestroy();	// originalContact
       
   419 //
       
   420 	CContactIdArray *deleteIds=CContactIdArray::NewLC(CntTest->Db()->SortedItemsL());
       
   421 	CntTest->Db()->DeleteContactsL(*deleteIds);
       
   422 	CleanupStack::PopAndDestroy();	// deleteIds
       
   423 //
       
   424 	if (CntTest->Db()->CompressRequired())
       
   425 		CntTest->Db()->CompressL();
       
   426 	test.Printf(_L("."));	// Just to show some life
       
   427 	}
       
   428 */
       
   429 
       
   430 
       
   431 /**
       
   432 LOCAL_C void TestExportImportContactL()
       
   433 	{
       
   434 	test.Next(_L("Export/Import test"));
       
   435 
       
   436 	CContactDatabase *db=CntTest->CreateDatabaseL();
       
   437 	CContactItem *templateItem=ReadContactIncHiddenFieldsLC(db,db->TemplateId());
       
   438 	TInt templateFieldCount=templateItem->CardFields().Count();
       
   439 //	Create an contact with all template entries doubled up
       
   440 	CContactItem *templateItem2=ReadContactIncHiddenFieldsLC(db,db->TemplateId());
       
   441 const TInt KNonDuplicateFields=5;	// Names can not be duplicated
       
   442 	for(TInt dblLoop=KNonDuplicateFields;dblLoop<templateFieldCount;dblLoop++)
       
   443 		DuplicateTestField(templateItem2->CardFields(),dblLoop*2-KNonDuplicateFields);
       
   444 //	Create an contact with just the fourth template entry doubled up
       
   445 	CContactItem *templateItem3=ReadContactIncHiddenFieldsLC(db,db->TemplateId());
       
   446 	DuplicateTestField(templateItem3->CardFields(),7);
       
   447 // Test all the fields 1 at a time
       
   448 	for(TInt loop1=0;loop1<templateFieldCount;loop1++)
       
   449 		{
       
   450 		doTestExportImportContactL(templateItem,loop1,0);
       
   451 		doTestExportImportContactL(templateItem2,loop1,0);
       
   452 		doTestExportImportContactL(templateItem3,loop1,0);
       
   453 //		doTestExportImportContactL(templateItem,loop1,EImpExpTestExportExtraEntry);
       
   454 //		doTestExportImportContactL(templateItem,loop1,EImpExpTestModifyEntries);
       
   455 //		doTestExportImportContactL(templateItem2,loop1,EImpExpTestModifyEntries);
       
   456 //		doTestExportImportContactL(templateItem3,loop1,EImpExpTestModifyEntries);
       
   457 		}
       
   458 // Test various combinations of fields being set
       
   459 // (Mode 0 will test a card with all fields set)
       
   460 	doTestExportImportContactL(templateItem2,0,EImpExpTestMultipleEntries|EImpExpTestModifyEntries);
       
   461 	for(TInt testNum=0;testNum<templateFieldCount;testNum++)
       
   462 		{
       
   463 		doTestExportImportContactL(templateItem,testNum,EImpExpTestMultipleEntries);
       
   464 		doTestExportImportContactL(templateItem2,testNum,EImpExpTestMultipleEntries);
       
   465 		doTestExportImportContactL(templateItem3,testNum,EImpExpTestMultipleEntries);
       
   466 		doTestExportImportContactL(templateItem,testNum,EImpExpTestMultipleEntries|EImpExpTestModifyEntries);
       
   467 //		doTestExportImportContactL(templateItem2,testNum,EImpExpTestMultipleEntries|EImpExpTestModifyEntries);
       
   468 //		doTestExportImportContactL(templateItem3,testNum,EImpExpTestMultipleEntries|EImpExpTestModifyEntries);
       
   469 //		doTestExportImportContactL(templateItem,testNum,EImpExpTestMultipleEntries|EImpExpTestExportExtraEntry);
       
   470 		}
       
   471 //
       
   472 	CleanupStack::PopAndDestroy(3);	// templateItem,templateItem2,templateItem3
       
   473 	CntTest->CloseDatabase();
       
   474 	}
       
   475 */
       
   476 
       
   477 // modified since vCards which don't exist in the database are now added rather than leaving
       
   478 // this is necessary so that vcards of contacts with deleted uids are added.
       
   479 LOCAL_C void SynchWithNonExistentRecordL()
       
   480 	{
       
   481 	CntTest->CreateDatabaseL();
       
   482 	TContactItemId addId1=AddContactL(CntTest->Db(),KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,_L("Add1"));
       
   483 //
       
   484 	CVCardTestStore* store=CntTest->ExportContactLC(addId1,CContactDatabase::ETTFormat);
       
   485 	CntTest->DeleteContact(addId1);
       
   486 	CArrayPtr<CContactItem>* items=NULL;
       
   487 	TRAPD(err,	items=CntTest->ImportContactsL(store,CContactDatabase::ETTFormat));
       
   488 	test(err==KErrNone);
       
   489 	items->ResetAndDestroy();
       
   490 	delete items;
       
   491 //
       
   492 	CleanupStack::PopAndDestroy();	// store
       
   493 	CntTest->CloseDatabase();
       
   494 	}
       
   495 
       
   496 /*
       
   497 LOCAL_C void MergeBlankVCardL(TContactItemId aId)
       
   498 	{
       
   499 	CVCardTest *vtest=new(ELeave) CVCardTest;
       
   500 	CleanupStack::PushL(vtest);
       
   501 	RReadStream stream;
       
   502 	vtest->StreamLCC(stream,2,aId);
       
   503 	stream.PushL();
       
   504 	TBool success;
       
   505 	CArrayPtr<CContactItem>* items=CntTest->Db()->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl),stream,success,0);
       
   506 	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,items));
       
   507 	test(success);
       
   508 	CleanupStack::PopAndDestroy(4);	// stream,store,vtest,items
       
   509 	}
       
   510 */
       
   511 
       
   512 // This test doesn't do anything useful anymore - since vCalendar import to 
       
   513 // change deleted contacts creates new contacts rather than updating them.
       
   514 //
       
   515 
       
   516 #if defined(_DEBUG)
       
   517 LOCAL_C void BlankImportL()
       
   518 	{
       
   519 	CntTest->CreateDatabaseL();
       
   520 	CntTest->Db()->OverrideMachineUniqueId(0); //testcode assumes that HAL would return 0 
       
   521 	TContactItemId addId1=AddContactL(CntTest->Db(),KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,_L("Add1"));
       
   522 	TContactItemId addId2=AddContactL(CntTest->Db(),KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,_L("Add2"));
       
   523 	CntTest->AdjustContactAccessCountL(addId1,1);
       
   524 	CntTest->AdjustContactAccessCountL(addId2,1);
       
   525 //
       
   526 	CntTest->DeleteContact(addId1);
       
   527 	CntTest->CheckDeletedContact(addId1);
       
   528 //	MergeBlankVCardL(addId1);
       
   529 	CntTest->CheckDeletedContact(addId1);
       
   530 	CntTest->AdjustContactAccessCountL(addId1,-1);
       
   531 	CntTest->CheckContactDoesNotExist(addId1);
       
   532 //
       
   533 //	MergeBlankVCardL(addId2);
       
   534 	CntTest->DeleteContact(addId2);
       
   535 	CntTest->CheckDeletedContact(addId2);
       
   536 	CntTest->AdjustContactAccessCountL(addId2,-1);
       
   537 	CntTest->CheckContactDoesNotExist(addId2);
       
   538 //
       
   539 	CntTest->CloseDatabase();
       
   540 	}
       
   541 
       
   542 LOCAL_C void ExportImportDifferentMachineIds()
       
   543 	{
       
   544 	CntTest->CreateDatabaseL();
       
   545 	CntTest->Db()->OverrideMachineUniqueId(0x1);
       
   546 	TContactItemId addId1=AddContactL(CntTest->Db(),KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,_L("Add1"));
       
   547 	HBufC *uid1=CntTest->ContactUidLC(addId1,0x1);
       
   548 	CVCardTestStore* store=CntTest->ExportContactLC(addId1,CContactDatabase::ETTFormat);
       
   549 	TInt oldCount=CntTest->Db()->CountL();
       
   550 //
       
   551 	CArrayPtr<CContactItem>* items=CntTest->ImportContactsLC(store,CContactDatabase::ETTFormat);
       
   552 	test(items->Count()==1);
       
   553 	CleanupStack::PopAndDestroy(2);	// store,items
       
   554 	test(oldCount==CntTest->Db()->CountL());
       
   555 //
       
   556 	store=CntTest->ExportContactLC(addId1,CContactDatabase::ETTFormat);
       
   557 	CntTest->Db()->OverrideMachineUniqueId(0x2);
       
   558 	items=CntTest->ImportContactsLC(store,CContactDatabase::ETTFormat);
       
   559 	test(items->Count()==1);
       
   560 	TContactItemId importId=(*items)[0]->Id();
       
   561 	test((oldCount+1)==CntTest->Db()->CountL());
       
   562 	HBufC *uid2=CntTest->ContactUidLC(importId,0x2);
       
   563 	test(*uid1==*uid2);
       
   564 	CleanupStack::PopAndDestroy(3);	// store,items,uid2
       
   565 //
       
   566 	store=CntTest->ExportContactLC(addId1,CContactDatabase::ETTFormat);
       
   567 	items=CntTest->ImportContactsLC(store,CContactDatabase::ETTFormat);
       
   568 	test(items->Count()==1);
       
   569 	CleanupStack::PopAndDestroy(2);	// store,items
       
   570 	test((oldCount+1)==CntTest->Db()->CountL());
       
   571 //
       
   572 	store=CntTest->ExportContactLC(importId,CContactDatabase::ETTFormat);
       
   573 	items=CntTest->ImportContactsLC(store,CContactDatabase::ETTFormat);
       
   574 	test(items->Count()==1);
       
   575 	CleanupStack::PopAndDestroy(2);	// store,items
       
   576 	test((oldCount+1)==CntTest->Db()->CountL());
       
   577 //
       
   578 	CleanupStack::PopAndDestroy();	// uid1
       
   579 	CntTest->CloseDatabase();
       
   580 	}
       
   581 
       
   582 #endif //(_DEBUG)
       
   583 
       
   584 LOCAL_C void DoTests1L()
       
   585 	{
       
   586 	// create test database
       
   587 	TheIds=CContactIdArray::NewLC();
       
   588 	CVersitTest* vtest=NULL;
       
   589 	vtest=new(ELeave)CVCardTest;
       
   590 	CleanupStack::PushL(vtest);
       
   591 	CVersitTest* vtest2=NULL;
       
   592 	vtest2=new(ELeave)CVCardTest;
       
   593 	CleanupStack::PushL(vtest2);
       
   594 	//Import
       
   595 	test.Next(_L("Importing vcard"));
       
   596 
       
   597 
       
   598 	CContactDatabase *db=CntTest->CreateDatabaseL();
       
   599 	test.Next(_L(""));
       
   600 
       
   601 //	TInt64 machineUid = db->MachineId();
       
   602 	
       
   603 	
       
   604 	TBool success;
       
   605 	TUid vcardmode;
       
   606 	vcardmode.iUid=KUidVCardConvDefaultImpl;
       
   607 	RReadStream stream;
       
   608 	vtest->StreamLCC(stream,1,-1);
       
   609 	stream.PushL();
       
   610 	TheItems=db->ImportContactsL(vcardmode,stream,success,CContactDatabase::EIncludeX|CContactDatabase::EImportSingleContact|CContactDatabase::EIncreaseAccessCount);
       
   611 	test(success);
       
   612 	test(db->CountL()==2);  // the first contact has an agent, that makes it 2
       
   613 	TheItems2=db->ImportContactsL(vcardmode,stream,success,CContactDatabase::EIncludeX|CContactDatabase::EIncreaseAccessCount);
       
   614 	test(success);
       
   615 	
       
   616 	AddLocalLabels();
       
   617 	CleanupStack::PopAndDestroy(2);	// stream,store
       
   618 	test(db->CountL()==3);
       
   619 	DisplayDatabase();
       
   620 	//Export
       
   621 	test.Next(_L("Exporting vcard"));
       
   622 
       
   623 	TStreamId id;
       
   624 	CFileStore* store ;
       
   625 	RStoreWriteStream outstream;
       
   626 	TUid uid;
       
   627 	/*= CDirectFileStore::ReplaceLC(CntTest->Fs(),_L("Versitout"),EFileWrite);
       
   628 	store->SetTypeL(KDirectFileStoreLayoutUid);
       
   629 	RStoreWriteStream outstream;
       
   630 	TStreamId id = outstream.CreateLC(*store);
       
   631 	//TUid uid;
       
   632 	uid.iUid=KUidVCardConvDefaultImpl;
       
   633 	TInt itemCount=TheItems->Count();
       
   634 	for (TInt ii=0;ii<itemCount;ii++)
       
   635 		TheIds->AddL((*TheItems)[ii]->Id());
       
   636 	db->ExportSelectedContactsL(uid,*TheIds,outstream,CContactDatabase::EDefault);
       
   637 	outstream.CommitL();
       
   638 	store->SetRootL(id);
       
   639 	store->CommitL();  	
       
   640 	CleanupStack::PopAndDestroy(2); // store+ oustream*/
       
   641 	//Update 2 (delete record 0)
       
   642 	test.Next(_L("Updating vcard 2"));
       
   643 
       
   644 	RReadStream stream2;
       
   645 	vtest2->StreamLCC(stream2,2,1);
       
   646 	stream2.PushL();
       
   647 	TheItems3=db->ImportContactsL(vcardmode,stream2,success,CContactDatabase::EIncreaseAccessCount);
       
   648 	test(success);
       
   649 	CleanupStack::PopAndDestroy(2);	// stream,store
       
   650 	test(db->CountL()==2);
       
   651 //
       
   652 	CArrayFix<CContactDatabase::TSortPref>* sortOrder=new(ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(1);
       
   653 	CleanupStack::PushL(sortOrder);
       
   654 	sortOrder->AppendL(CContactDatabase::TSortPref(KUidContactFieldFamilyName,CContactDatabase::TSortPref::EAsc));
       
   655 	db->SortL(sortOrder);
       
   656 	CleanupStack::Pop();	// sortOrder
       
   657 	sortOrder=new(ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(1);
       
   658 	CleanupStack::PushL(sortOrder);
       
   659 	db->SortL(sortOrder);
       
   660 	CleanupStack::Pop();	// sortOrder
       
   661 //
       
   662 	DecAccessCount();
       
   663 	test(db->CountL()==2);
       
   664 	//Export again
       
   665 	test.Next(_L("Exporting vcard"));
       
   666 
       
   667 	TheIds->Reset();
       
   668 	TheIds->AddL(2);// zzz shouldn't be hardwired value
       
   669 	store = CDirectFileStore::ReplaceLC(CntTest->Fs(),_L("c:\\Versitoutb"),EFileWrite);
       
   670 	store->SetTypeL(KDirectFileStoreLayoutUid);
       
   671 	id = outstream.CreateLC(*store);
       
   672 	uid.iUid=KUidVCardConvDefaultImpl;
       
   673 	db->ExportSelectedContactsL(uid,*TheIds,outstream,CContactDatabase::EIncludeX);
       
   674 	outstream.CommitL();
       
   675 	store->SetRootL(id);
       
   676 	store->CommitL();  	
       
   677 	CleanupStack::PopAndDestroy(2); // store+ oustream
       
   678 
       
   679 	if (TheItems)
       
   680 		TheItems->ResetAndDestroy();
       
   681 	delete TheItems;
       
   682 	if (TheItems2)
       
   683 		TheItems2->ResetAndDestroy();
       
   684 	delete TheItems2;
       
   685 	if (TheItems3)
       
   686 		TheItems3->ResetAndDestroy();
       
   687 	delete TheItems3;
       
   688 	
       
   689 	CleanupStack::PopAndDestroy(2); // vtest, vtest2
       
   690 	CleanupStack::PopAndDestroy(); //TheIds
       
   691 //
       
   692 	CVCardTest *vtest3=new(ELeave) CVCardTest;
       
   693 	CleanupStack::PushL(vtest3);
       
   694 	test.Next(_L("Multiple card import"));
       
   695 
       
   696 	RReadStream stream3;
       
   697 	vtest3->StreamLCC(stream3,3,-1);
       
   698 	stream3.PushL();
       
   699 	TInt oldCount=db->CountL();
       
   700 	TheItems4=db->ImportContactsL(vcardmode,stream3,success,CContactDatabase::EIncreaseAccessCount);
       
   701 	test(success);
       
   702 	CleanupStack::PopAndDestroy(2);	// stream3,store
       
   703 	test(TheItems4->Count()==2);
       
   704 	test(db->CountL()==(oldCount+TheItems4->Count()));
       
   705 	CleanupStack::PopAndDestroy(); // vtest3
       
   706 //
       
   707 	TheItems4->ResetAndDestroy();
       
   708 	delete TheItems4;
       
   709 	CntTest->CloseDatabase();
       
   710 	}
       
   711 
       
   712 LOCAL_C void TestIncludeXL()
       
   713 	{
       
   714 	CContactDatabase *db=CntTest->CreateDatabaseL();
       
   715 	CntTest->DeleteAllTemplateFieldsL();
       
   716 	CContactCard *item=CContactCard::NewLC();
       
   717 	SetNameL(*item,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,_L("FamilyName"),ETrue);
       
   718 	SetNameL(*item,KUidContactFieldNote,KUidContactFieldVCardMapNOTE,_L("Note"),ETrue);
       
   719 	SetNameL(*item,KUidContactFieldJobTitle,KUidContactFieldVCardMapTITLE,_L("Job Title"),ETrue);
       
   720 	SetNameL(*item,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,_L("Tel"),ETrue);
       
   721 	CContactItemFieldSet& fieldSet=item->CardFields();
       
   722 	fieldSet[3].SetHidden(ETrue);	// zzz if set to 0 fails, names don't support hidden flag
       
   723 	fieldSet[1].SetReadOnly(ETrue);
       
   724 	fieldSet[2].SetHidden(ETrue);
       
   725 	fieldSet[2].SetReadOnly(ETrue);
       
   726 	fieldSet[0].SetLabelL(_L("Field 0 Label"));
       
   727 	fieldSet[1].SetLabelL(_L("Field 1 Label"));
       
   728 	fieldSet[2].SetLabelL(_L("Field 2 Label"));
       
   729 	fieldSet[3].SetLabelL(_L("Field 3 Label"));
       
   730 	TContactItemId addId1=db->AddNewContactL(*item);
       
   731 //
       
   732 	CVCardTestStore* store=CntTest->ExportContactLC(addId1,CContactDatabase::EIncludeX|CContactDatabase::EExcludeUid,KVCardStoreTypeFile,9);
       
   733 	
       
   734 	CArrayPtr<CContactItem>* importedItems=CntTest->ImportContactsLC(store,CContactDatabase::EIncludeX|CContactDatabase::EExcludeUid);
       
   735 	test(CompareItemFields((*importedItems)[0],item));
       
   736 	CleanupStack::PopAndDestroy(2);	// importedItems,store
       
   737 //
       
   738 	CleanupStack::PopAndDestroy();	// item
       
   739 //
       
   740 	CntTest->CloseDatabase();
       
   741 	}
       
   742 
       
   743 void createTestVCardL(RFs& aFs)
       
   744 	{
       
   745 	RFile file;
       
   746 	TInt err=file.Replace(aFs, KVCardFileToImport, EFileWrite+EFileShareAny+EFileStreamText);
       
   747 	User::LeaveIfError(err);
       
   748 	TPtrC8 content((const TText8*)	"BEGIN:VCARD\r\n"
       
   749 									"VERSION:2.1\r\n"
       
   750 									"REV:20020520T134824Z\r\n"
       
   751 									"N:smith;john;;;\r\n"
       
   752 									"TEL;HOME;VOICE:020 8450 1537\r\n"
       
   753 									"END:VCARD\r\n");
       
   754 	file.Write(content);
       
   755 	file.Close();
       
   756 	}
       
   757 
       
   758 TContactItemId ImportVCalL(CContactDatabase* aDb,RFs& aFs,TBool aUseNullTemplateId = EFalse)
       
   759 	{
       
   760 	RFileReadStream vcard;
       
   761 	User::LeaveIfError(vcard.Open(aFs, KVCardFileToImport, EFileRead));
       
   762 	CleanupClosePushL(vcard);
       
   763 
       
   764 	TBool success=EFalse;
       
   765 	TInt options=0;
       
   766 	options |= CContactDatabase::EImportSingleContact;
       
   767 	if (aUseNullTemplateId)
       
   768 		{
       
   769 		options |= CContactDatabase::ENullTemplateId;
       
   770 		}
       
   771 	CArrayPtr<CContactItem>* contactItems=aDb->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, options);
       
   772 	CleanupStack::PopAndDestroy(&vcard);
       
   773 	TContactItemId id=(*contactItems)[0]->Id();
       
   774 	contactItems->ResetAndDestroy();
       
   775 	delete contactItems;
       
   776 	return id;
       
   777 	}
       
   778 
       
   779 #if defined(_DEBUG)
       
   780 LOCAL_C void TestImportWithNullTemplateIdL()
       
   781 	{
       
   782 	CntTest->CreateDatabaseL();
       
   783 //
       
   784 	// Get the number of fields in the golden template
       
   785 	CContactItem* item = CntTest->Db()->ReadContactLC(KGoldenTemplateId);
       
   786 	TInt goldenTemplateCount = item->CardFields().Count();
       
   787 	CleanupStack::PopAndDestroy(item);
       
   788 //
       
   789 	// Test fields imported using golden template
       
   790 	createTestVCardL(CntTest->Fs());
       
   791 	TContactItemId id = ImportVCalL(CntTest->Db(),CntTest->Fs());
       
   792 	item = CntTest->Db()->ReadContactLC(id);
       
   793 	test(item->CardFields().Count() == goldenTemplateCount);
       
   794 	CleanupStack::PopAndDestroy(item);
       
   795 //
       
   796 	// Test fields imported using null template
       
   797 	createTestVCardL(CntTest->Fs());
       
   798 	id = ImportVCalL(CntTest->Db(),CntTest->Fs(),ETrue);
       
   799 	item = CntTest->Db()->ReadContactLC(id);
       
   800 	test(item->CardFields().Count() == 3); // number of fields in vCard
       
   801 	CleanupStack::PopAndDestroy(item);
       
   802 //
       
   803 	CntTest->Fs().Delete(KVCardFileToImport);
       
   804 	CntTest->CloseDatabase();
       
   805 	}
       
   806 
       
   807 LOCAL_C void TestsAccessCountL()
       
   808 	{
       
   809 	CntTest->OpenDatabaseL();
       
   810 	TContactItemId addId1=AddContactL(CntTest->Db(),KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,_L("Add1"));
       
   811 	CContactIdArray *ids=CContactIdArray::NewLC();
       
   812 	ids->AddL(addId1);
       
   813 //
       
   814 	CntTest->TestAccessCount(addId1,0);
       
   815 	CVCardTestStore* store1=CntTest->ExportContactsLC(ids,CContactDatabase::EIncreaseAccessCount);
       
   816 	CntTest->TestAccessCount(addId1,1);
       
   817 	CntTest->ImportContactsLC(store1,CContactDatabase::EIncreaseAccessCount);
       
   818 	CleanupStack::PopAndDestroy(2);	// items->ResetAndDestroy(),store1
       
   819 	CntTest->TestAccessCount(addId1,2);
       
   820 //
       
   821 	CVCardTestStore* store2=CntTest->ExportContactsLC(ids,CContactDatabase::EDecreaseAccessCount);
       
   822 	CntTest->TestAccessCount(addId1,1);
       
   823 	CntTest->ImportContactsLC(store2,CContactDatabase::EDecreaseAccessCount);
       
   824 	CleanupStack::PopAndDestroy(2);	// items->ResetAndDestroy(),store2
       
   825 	CntTest->TestAccessCount(addId1,0);
       
   826 	CleanupStack::PopAndDestroy();	// ids
       
   827 //
       
   828 	TContactItemId addId2=AddContactL(CntTest->Db(),KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,_L("Add1"));
       
   829 	ids=CContactIdArray::NewLC();
       
   830 	ids->AddL(addId2);
       
   831 //
       
   832 	CntTest->TestAccessCount(addId2,0);
       
   833 	CntTest->ExportContactsLC(ids,CContactDatabase::EIncreaseAccessCount);
       
   834 	CleanupStack::PopAndDestroy(2);	// ids,store from ExportContactsLC
       
   835 	CntTest->TestAccessCount(addId2,1);
       
   836 //
       
   837 	CVCardTest *vtest=new(ELeave) CVCardTest;
       
   838 	CleanupStack::PushL(vtest);
       
   839 	RReadStream stream;
       
   840 	vtest->StreamLCC(stream,2,addId2);
       
   841 	stream.PushL();
       
   842 	TBool success;
       
   843 	CntTest->Db()->OverrideMachineUniqueId(0); //testcode assumes that HAL would return 0 
       
   844 	CArrayPtr<CContactItem>* items=CntTest->Db()->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl),stream,success,CContactDatabase::EDecreaseAccessCount);
       
   845 	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,items));
       
   846 	CntTest->CheckContactDoesNotExist(addId2);
       
   847 	CleanupStack::PopAndDestroy(4);	// stream,store,vtest,items
       
   848 //
       
   849 	CntTest->CloseDatabase();
       
   850 	}
       
   851 
       
   852 #endif //(_DEBUG)
       
   853 
       
   854 /**
       
   855 
       
   856 @SYMTestCaseID     PIM-T-VERS-0001
       
   857 
       
   858 */
       
   859 
       
   860 LOCAL_C void DoTestsL()
       
   861 	{
       
   862 	test.Start(_L("@SYMTESTCaseID:PIM-T-VERS-0001 Preparing tests"));
       
   863 
       
   864 
       
   865 	CTestRegister * TempFiles = CTestRegister::NewLC();
       
   866 	TempFiles->RegisterL(KDatabaseFileName, EFileTypeCnt);
       
   867 	TempFiles->RegisterL(KVCardFileToImport);
       
   868 	TempFiles->RegisterL(_L("c:\\versit2"));
       
   869 	TempFiles->RegisterL(_L("c:\\versitin1"));
       
   870 	TempFiles->RegisterL(_L("c:\\versitin2"));
       
   871 	TempFiles->RegisterL(_L("c:\\versitin3"));
       
   872 	TempFiles->RegisterL(_L("c:\\versitfile"));
       
   873 	TempFiles->RegisterL(_L("c:\\vcard.vcf"));
       
   874 	TempFiles->RegisterL(_L("c:\\versitoutb"));
       
   875 
       
   876 	DoTests1L();
       
   877 #if defined(_DEBUG)
       
   878 	TestsAccessCountL();
       
   879 	ExportImportDifferentMachineIds();
       
   880 #endif
       
   881 	SynchWithNonExistentRecordL();
       
   882 #if defined(_DEBUG)
       
   883 	BlankImportL();
       
   884 #endif
       
   885 	TestIncludeXL();
       
   886 #if defined(_DEBUG)
       
   887 	TestImportWithNullTemplateIdL();
       
   888 #endif
       
   889 //	TestExportImportContactL();
       
   890 
       
   891 	CleanupStack::PopAndDestroy(TempFiles);
       
   892 	}
       
   893 
       
   894 GLDEF_C TInt E32Main()
       
   895 	{
       
   896     CntTest=new(ELeave) CCntTest;
       
   897 	CntTest->ConstructL(test,KDatabaseFileName);
       
   898     TRAPD(err,DoTestsL());
       
   899 	CntTest->EndTestLib(err);
       
   900 	return KErrNone;
       
   901     }