phonebookengines_old/contactsmodel/tsrc/t_cntvcard.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2002-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 // Unit tests for functionality delivered by the CNTVCARD.DLL library.
       
    15 // This test contains the tests originally in T_EXPDEL
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <f32file.h>
       
    21 #include <s32file.h>
       
    22 #include <s32mem.h>
       
    23 #include <cntdb.h>
       
    24 #include <cntvcard.h>
       
    25 #include <cntitem.h>
       
    26 #include <cntfldst.h>
       
    27 #include <e32def.h>
       
    28 #include "t_utils2.h"
       
    29 #include "T_rndutils.h"
       
    30 #include <coreappstest/testserver.h>
       
    31 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    32 #include "cntdb_internal.h"
       
    33 #endif
       
    34 
       
    35 _LIT(KTestName,"T_CNTVCARD");
       
    36 
       
    37 
       
    38 
       
    39 _LIT(KDatabaseFileName,"C:T_CNTVCARD");
       
    40 
       
    41 _LIT(KVcard1, "Z:\\t_cntvcard\\email1.vcf");
       
    42 _LIT(KVcard2, "Z:\\t_cntvcard\\email2.vcf");
       
    43 
       
    44 
       
    45 _LIT(KOutputFileName, "C:\\Test_Output.txt");
       
    46 _LIT(KExpDelAFileName, "c:\\expdela");
       
    47 _LIT(KTestVcardFile, "Z:\\cntvcard\\testvcard.vcf");
       
    48 
       
    49 _LIT(KImpTypeVcardName, "Z:\\t_cntvcard\\testTypeVcard.vcf");
       
    50 _LIT(KImpNoTypeVcardName, "Z:\\t_cntvcard\\testNoTypeVcard.vcf");
       
    51 
       
    52 _LIT(KTestVcardFile1, "Z:\\cntvcard\\testvcard1.vcf");
       
    53 //UpdateVCardL
       
    54 _LIT(KVCardUpdate1,"Z:\\System\\Programs\\CntmodelTest\\vcardupdate1.vcf");
       
    55 _LIT(KVCardUpdate2,"C:\\vcardupdate2.vcf");
       
    56 _LIT(KVCardUpdate3,"Z:\\System\\Programs\\CntmodelTest\\vcardupdate3.vcf");
       
    57 
       
    58 // digram data files
       
    59 _LIT(KFirstNameFileName,"z:\\cntvcard\\names.first.2gm");
       
    60 _LIT(KLastNameFileName,"z:\\cntvcard\\names.last.2gm");
       
    61 
       
    62 // vcard terminated by LF
       
    63 _LIT(KVCardFileLF,"z:\\t_cntvcard\\cntvcardLF.vcf");
       
    64 // vcard containing UID property
       
    65 _LIT(KVCardFileUID,"z:\\t_cntvcard\\cntvcardUID.vcf");
       
    66 
       
    67 _LIT8(KVCardBinaryKeyImportFileDes,"BEGIN:VCARD\r\n"
       
    68 							"VERSION:2.1\r\n"
       
    69 							"REV:20061120T111808Z\r\n"
       
    70 							"UID:0000000000000000-00e1005eee8a27d8-44\r\n"
       
    71 							"N:Name;;;;\r\n"
       
    72 							"TEL;HOME;VOICE;CELL:1111\r\n"
       
    73 							"KEY;ENCODING=BASE64:MIICajCCAdOgAwIBAgICBEUwDQYJKoZIhvcNAQEEBQA\r\n"
       
    74 							"wdzELMAkGA1UEBhMCVVMxLDAqBgNVBAoTI05ldHNjYXBlIENbW11bmljYX\r\n"
       
    75 							"Rpb25zIENvcnBvcmF0aW9uMRwwGgYDVQQLExNJbmZvcm1hdGlvbiBTeXN0\r\n"
       
    76 							"ZW1zMRwwGgYDVQQDExNyb290Y2EubmV0c2NhcGUuY29tMB4XDTk3MDYwNj\r\n"
       
    77 							"E5NDc1OVoXDTk3MTIwMzE5NDc1OVowgYkxCzAJBgNVBAYTAlVTMSYwJAYD\r\n"							 
       
    78 							"VQQKEx1OZXRzY2FwZSBDb21tdW5pY2F0aW9ucyBDb3JwLjEYMBYGA1UEAx\r\n"
       
    79 							"MPVGltb3RoeSBBIEhvd2VzMSEwHwYJKoZIhvcNAQkBFhJob3dlc0BuZXRz\r\n"
       
    80 							"Y2FwZS5jb20xFTATBgoJkiaJk/IsZAEBEwVob3dlczBcMA0GCSqGSIb3DQ\r\n"
       
    81 							"EBAQUAA0sAMEgCQQC0JZf6wkg8pLMXHHCUvMfL5H6zjSk4vTTXZpYyrdN2\r\n"
       
    82 							"dXcoX49LKiOmgeJSzoiFKHtLOIboyludF90CgqcxtwKnAgMBAAGjNjA0MB\r\n"
       
    83 							"EGCWCGSAGG+EIBAQQEAwIAoDAfBgNVHSMEGDAWgBT84FToB/GV3jr3mcau\r\n"
       
    84 							"+hUMbsQukjANBgkqhkiG9w0BAQQFAAOBgQBexv7o7mi3PLXadkmNP9LcIP\r\n"
       
    85 							"mx93HGp0Kgyx1jIVMyNgsemeAwBM+MSlhMfcpbTrONwNjZYW8vJDSoi//y\r\n"
       
    86 							"rZlVt9bJbs7MNYZVsyF1unsqaln4/vy6Uawfg8VUMk1U7jt8LYpo4YULU7\r\n"
       
    87 							"UZHPYVUaSgVttImOHZIKi4hlPXBOhcUQ==\r\n"
       
    88 							"\r\n"
       
    89 							"PHOTO;ENCODING=BASE64:\r\n"
       
    90 							"   ZmlsZTovLy9qcXB1YmxpYy5naWY=\r\n"
       
    91 							"\r\n"
       
    92 							"END:VCARD\r\n"
       
    93 							);
       
    94 							
       
    95 _LIT8(KVCardTextKeyImportFileDes,"BEGIN:VCARD\r\n"
       
    96 							"VERSION:2.1\r\n"
       
    97 							"REV:20061120T111808Z\r\n"
       
    98 							"UID:0000000000000000-00e1005eee8a27d8-44\r\n"
       
    99 							"N:Name;;;;\r\n"
       
   100 							"TEL;HOME;VOICE;CELL:1111\r\n"
       
   101 							"KEY:X509\r\n"
       
   102 							"PHOTO;ENCODING=BASE64:\r\n"
       
   103 							"   ZmlsZTovLy9qcXB1YmxpYy5naWY=\r\n"
       
   104 							"\r\n"
       
   105 							"END:VCARD\r\n"
       
   106 							);
       
   107 
       
   108 _LIT8(KMultiParam,"BEGIN:VCARD\r\n"
       
   109 				"VERSION:2.1\r\n"
       
   110 				"N:;Neo;;Mr.;\r\n"
       
   111 				"FN:Mr. TestName\r\n"
       
   112 				"TEL;TYPE=HOME;TYPE=VOICE;TYPE=CELL:123\r\n"
       
   113 				"END:VCARD\r\n"
       
   114 				);
       
   115 _LIT8(KTelExport, "TEL;HOME;VOICE;CELL:123\r\n");
       
   116 
       
   117 _LIT8(KVCardBeforeChange,"BEGIN:VCARD\r\n"
       
   118  						"VERSION:2.1\r\n"
       
   119  						"N:Jm;Jg;;;\r\n"
       
   120  						"TEL;HOME:11111\r\n"
       
   121  						"TEL;HOME:67890\r\n"
       
   122  						"TEL;CELL:12345\r\n"
       
   123  						"UID:4c2d2b19ef07adea-00e115956542fd28-1\r\n"
       
   124  						"END:VCARD\r\n"
       
   125  						);
       
   126  						
       
   127  _LIT8(KVCardAfterChange,"BEGIN:VCARD\r\n"
       
   128  						"VERSION:2.1\r\n"
       
   129  						"N:Jm;Jg;;;\r\n"
       
   130  						"TEL;HOME:11112\r\n"
       
   131  						"TEL;HOME:22222\r\n"
       
   132  						"TEL;HOME:22223\r\n"
       
   133  						"TEL;HOME:22224\r\n"
       
   134  						"UID:4c2d2b19ef07adea-00e115956542fd28-1\r\n"
       
   135  						"END:VCARD\r\n"
       
   136  						);				
       
   137  
       
   138  _LIT(KGivenName, "Jg");
       
   139  _LIT(KFamilyName, "Jm");
       
   140  _LIT(KTelHome1,"11111");
       
   141  _LIT(KTelHome2,"67890");
       
   142  _LIT(KTelCell,"12345");
       
   143  _LIT(KTelHome1Modified,"11112");
       
   144  _LIT(KTelHome2Modified,"22222");
       
   145  _LIT(KTelHome3Modified,"22223");
       
   146  _LIT(KTelHome4Modified,"22224");
       
   147 
       
   148 //vCard containing CompanyField
       
   149 _LIT(KVCardWithCompany, "z:\\t_cntvcard\\withcompany.vcf");
       
   150 //vCard without CompanyField
       
   151 _LIT(KVCardWithoutCompany, "z:\\t_cntvcard\\withoutcompany.vcf");
       
   152 
       
   153 _LIT(KVCardLargePhoto, "Z:\\t_cntvcard\\vcardwithlargephoto.vcf");
       
   154 
       
   155 
       
   156 CCntTest* CntTest=NULL;
       
   157 LOCAL_D RTest test(KTestName);
       
   158 
       
   159 LOCAL_C void TestCondition(TBool aCondition, TInt aLineNumber);
       
   160 #define TEST_CONDITION(x) TestCondition(x, __LINE__)
       
   161 
       
   162 const TInt KNumAccessCountContacts= 5;
       
   163 const TInt KMachineUniqueId=0;
       
   164 const TInt KNumberOfRandomContacts = 40;
       
   165 
       
   166 _LIT(KName,"Name");
       
   167 _LIT(KPhoneNumber,"1111");
       
   168 _LIT8(KModifiedNumberProperty,"TEL;HOME;CELL:11112222\r\n"); //no VOICE parameter!
       
   169 _LIT8(KModifiedNumberProperty2,"tel;work;cell;2:4444\r\ntel;home;Voice;CELL:\r\n"); //empty property to delete
       
   170 _LIT8(KModifiedNumberProperty3,"TEL;HOME;CELL:\r\nTEL;HOME;CELL:3333\r\n"); 
       
   171 
       
   172 _LIT(KVCardFile1,"c:\\cntvcard1.vcf");
       
   173 _LIT(KVCardFile2,"c:\\cntvcard2.vcf");
       
   174 _LIT(KVCardFile3,"c:\\cntvcard3.vcf");
       
   175 
       
   176 // data we expect to see in KTestVcardFile
       
   177 _LIT(KFirstName, "first");
       
   178 _LIT(KFirstNamePrn, "f-i-r-s-t");
       
   179 _LIT(KSurName, "last");
       
   180 _LIT(KSurNamePrn, "l-a-s-t");
       
   181 _LIT(KOrgName, "I work here");
       
   182 _LIT(KOrgNamePrn, "Eye w-o-r-k h-e-r-e");
       
   183 
       
   184 // labels we expect from the conacts resource file
       
   185 _LIT(KFirstNameLabel, "First name");
       
   186 _LIT(KFirstNamePrnLabel, "First name reading");
       
   187 _LIT(KSurNameLabel, "Last name");
       
   188 _LIT(KSurNamePrnLabel, "Last name reading");
       
   189 _LIT(KOrgNameLabel, "Company");
       
   190 _LIT(KOrgNamePrnLabel, "Company reading");
       
   191 
       
   192 _LIT(KVCardFile5, "c:\\cntvcard5.vcf");
       
   193 _LIT(KVCardFile6, "c:\\cntvcard6.vcf");
       
   194 _LIT8(KMasterVcard, "BEGIN:VCARD\r\n"
       
   195 					"VERSION:2.1\r\n"
       
   196 					"REV:20060821T131829Z\r\n"
       
   197 					"UID:f356da00bfd5320f-00e0f93a015e3ec0-18\r\n"
       
   198 					"N:Aaaa;Pekka;;;\r\n"
       
   199 					"END:VCARD\r\n"
       
   200 					"BEGIN:VCARD\r\n"
       
   201 					"VERSION:2.1\r\n"
       
   202 					"REV:20060821T131916Z\r\n"
       
   203 					"UID:f356da00bfd5320f-00e0f93a015e8128-19\r\n"
       
   204 					"N:Bbb;Pekka;;;\r\n"
       
   205 					"END:VCARD\r\n"
       
   206 					"BEGIN:VCARD\r\n"
       
   207 					"VERSION:2.1\r\n"
       
   208 					"REV:20060821T131829Z\r\n"
       
   209 					"UID:f356da00bfd5320f-00e0f93a015eb46d-20\r\n"
       
   210 					"N:Cccc;Pekka;;;\r\n"
       
   211 					"END:VCARD\r\n"
       
   212 );
       
   213 _LIT8(KModifierVcard,"BEGIN:VCARD\r\n"
       
   214 					"VERSION:2.1\r\n"
       
   215 					"REV:20060821T131829Z\r\n"
       
   216 					"UID:f356da00bfd5320f-00e0f93a015e3ec0-18\r\n"
       
   217 					"N:Aaaa;Pekka;;;\r\n"
       
   218 					"END:VCARD\r\n"
       
   219 					"BEGIN:VCARD\r\n"
       
   220 					"VERSION:2.1\r\n"
       
   221 					"REV:20060821T131916Z\r\n"
       
   222 					"UID:f356da00bfd5320f-00e0f93a015e8128-19\r\n"
       
   223 					"N:;Pekka;;;\r\n"
       
   224 					"END:VCARD\r\n"
       
   225 					"BEGIN:VCARD\r\n"
       
   226 					"VERSION:2.1\r\n"
       
   227 					"REV:20060821T131829Z\r\n"
       
   228 					"UID:f356da00bfd5320f-00e0f93a015eb46d-20\r\n"
       
   229 					"N:Cccc;Pekka;;;\r\n"
       
   230 					"END:VCARD\r\n"
       
   231 );
       
   232  
       
   233 _LIT8(KPartialVCard,"BEGIN:VCARD\r\n"
       
   234         "\r"
       
   235 );
       
   236 
       
   237 const TAny* GNames[]  = {&KFirstName, &KFirstNamePrn, &KSurName, &KSurNamePrn, &KOrgName, &KOrgNamePrn};
       
   238 const TAny* GLabels[]  = {&KFirstNameLabel, &KFirstNamePrnLabel, &KSurNameLabel, &KSurNamePrnLabel, &KOrgNameLabel, &KOrgNamePrnLabel};
       
   239 
       
   240 const TInt KNumberOfExtendedFieldNames=6;
       
   241 
       
   242 const TInt KExtendedFieldNames[KNumberOfExtendedFieldNames] = 
       
   243 	{
       
   244 	KUidContactFieldGivenNameValue, 
       
   245 	KUidContactFieldGivenNamePronunciationValue,
       
   246 	KUidContactFieldFamilyNameValue, 
       
   247 	KUidContactFieldFamilyNamePronunciationValue,
       
   248 	KUidContactFieldCompanyNameValue, 	
       
   249 	KUidContactFieldCompanyNamePronunciationValue, 
       
   250 	};
       
   251 
       
   252 		
       
   253 /** Tests for default VOICE property parameter handling (tests1-4), and the X-IRMC-x extentions for the SOUND property (tests 5-6) */
       
   254 class CDefaultVoiceParamTests : public CBase
       
   255 	{
       
   256 public :
       
   257 	enum THowToCheck {ECheckValue = 1, ECheckLabel = 2, EAppendValue = 4, EAppendLabel = 8};
       
   258 
       
   259 public:
       
   260 	static CDefaultVoiceParamTests* NewLC(CCntTest& aTestLibrary);
       
   261 	~CDefaultVoiceParamTests();
       
   262 	void RunTestsL();
       
   263 private:
       
   264 	CDefaultVoiceParamTests(CCntTest& aTestLibrary);
       
   265 	void ConstructL();
       
   266 	void TestOneL();
       
   267 	void TestTwoL();
       
   268 	void TestThreeL();
       
   269 	void TestFourL();
       
   270 	void TestFiveL();
       
   271 	void TestSixL();
       
   272 	void ImportContactsL(const TDesC& aFileName);
       
   273 	void ExportContactsL(const TDesC& aFileName, const CContactIdArray* aIds);
       
   274 	void CreateTestDataL(TContactItemId aId, const TDesC& aFileName, const TDesC8& aVCardData);
       
   275 	TInt CountPhoneNumberFieldsL(TContactItemId aId);
       
   276 	TContactItemId CreateItemL(const TDesC& aName, const TDesC& aNumber);
       
   277 	static void CheckCard(CContactCard* aCard,  CDesCArray* aFields = NULL, CDesCArray* aLabels =NULL, TUint aFlags = ECheckValue|ECheckLabel);
       
   278 	void CheckCardL(TContactItemId aId,  CDesCArray* aFields = NULL, CDesCArray* aLabels =NULL, TUint aFlags = ECheckValue|ECheckLabel) const;
       
   279 	
       
   280 private:
       
   281 	CCntTest& iTest;
       
   282 	CContactDatabase* iDb;
       
   283 	};
       
   284 
       
   285 CDefaultVoiceParamTests* CDefaultVoiceParamTests::NewLC(CCntTest& aTestLibrary)
       
   286 	{
       
   287 	CDefaultVoiceParamTests* self = new(ELeave) CDefaultVoiceParamTests(aTestLibrary);
       
   288 	CleanupStack::PushL(self);
       
   289 	self->ConstructL();
       
   290 	return self;
       
   291 	}
       
   292 
       
   293 CDefaultVoiceParamTests::~CDefaultVoiceParamTests()
       
   294 	{
       
   295 	}
       
   296 
       
   297 void CDefaultVoiceParamTests::RunTestsL()
       
   298 	{
       
   299 	TestFiveL(); //these two have to run 1st since tests 1-4 leave the db in an inconvenient state
       
   300 	TestSixL();
       
   301 	TestOneL();
       
   302 #if defined(_DEBUG)
       
   303 	//this test cannot be run in release mode due to UID mismatch.
       
   304 	TestTwoL();
       
   305 #endif
       
   306 	TestThreeL();
       
   307 	TestFourL();	
       
   308 	}
       
   309 
       
   310 CDefaultVoiceParamTests::CDefaultVoiceParamTests(CCntTest& aTestLibrary) : iTest(aTestLibrary)
       
   311 	{
       
   312 	}
       
   313 
       
   314 void CDefaultVoiceParamTests::ConstructL()
       
   315 	{
       
   316 	iDb=iTest.CreateDatabaseL();
       
   317 	iDb->OverrideMachineUniqueId(KMachineUniqueId); 
       
   318 	}
       
   319 
       
   320 /** Check all the fields specified by KExtendedFieldNames for correctness.
       
   321 	The specific behaviour on what is checked is regulated by aFlags. 
       
   322 	This also prints each field with its label.
       
   323 	
       
   324 	@param aFields 	If present and the ECheckValue bit of aFlags set, the nth field text value is compared against
       
   325 					the nth descriptor in aFields. 
       
   326 					If present and EAppendValue bit of aFlags set, each field's text value is appended to the end of aFields. 	
       
   327 						
       
   328 	@param aLabels 	If present and the ECheckLabel bit of aFlags set, the nth field's label is compared against
       
   329 					the nth descriptor in ECheckLabel. 
       
   330 					If present and EAppendLabel bit of aFlags set, each field's label is appended to the end of aLabels. 
       
   331 	@param aFlags	Bitfields indicating how aFields and aLabels are used. Note that some values from THowToCheck
       
   332 					 (like ECheckValue and EAppendValue) cannot be used at the same time.
       
   333  */
       
   334 void CDefaultVoiceParamTests::CheckCard(CContactCard* aCard,  CDesCArray* aFields, CDesCArray* aLabels, TUint aFlags)
       
   335 	{
       
   336 	CContactItemFieldSet& fieldSet=aCard->CardFields();
       
   337 	for(TInt i=0;i<KNumberOfExtendedFieldNames;i++)
       
   338 		{
       
   339 		TInt index = fieldSet.Find( TUid::Uid(KExtendedFieldNames[i]));
       
   340 		TEST_CONDITION(index != KErrNotFound);
       
   341       	TPtrC text;
       
   342 		if(aFields && (ECheckValue& aFlags))
       
   343 			{
       
   344 			TPtrC compare((*aFields)[i]);
       
   345 			TBool succeed(EFalse);
       
   346 			while(index != KErrNotFound)
       
   347                 {
       
   348     			text.Set(fieldSet[index].TextStorage()->Text());
       
   349     			succeed = (text.Compare(compare)==0);
       
   350     			if(succeed)
       
   351     			    {
       
   352     			    break;
       
   353     			    }
       
   354     			index = fieldSet.FindNext( TUid::Uid(KExtendedFieldNames[i]), index + 1);    
       
   355                 }
       
   356             TEST_CONDITION(succeed);    
       
   357 			}
       
   358 		else if (aFields && (aFlags & EAppendValue))
       
   359 			{
       
   360 			text.Set(fieldSet[index].TextStorage()->Text());
       
   361 			TRAP_IGNORE(aFields->AppendL(text));
       
   362 			}
       
   363 		
       
   364 		TPtrC label(fieldSet[index].Label());
       
   365 		if(aLabels && (ECheckLabel& aFlags))
       
   366 			{
       
   367 			TPtrC comparelabel((*aLabels)[i]);
       
   368 			TEST_CONDITION(label.Compare(comparelabel)==0);
       
   369 			}
       
   370 		else if (aLabels && (aFlags & EAppendLabel))
       
   371 			{
       
   372 			TRAP_IGNORE(aLabels->AppendL(label));
       
   373 			}
       
   374 		test.Printf(_L("%S: %S\n") ,&label, &text);	// print this just to make sure
       
   375 		}
       
   376 }
       
   377 
       
   378 void CDefaultVoiceParamTests::CheckCardL(TContactItemId aId,  CDesCArray* aFields, CDesCArray* aLabels, TUint aFlags) const
       
   379 	{
       
   380 	CContactCard* card = STATIC_CAST(CContactCard*, iDb->OpenContactL(aId));
       
   381 	CleanupStack::PushL(card);
       
   382 	CheckCard(card, aFields, aLabels, aFlags);
       
   383 	iDb->CloseContactL(aId);
       
   384 	CleanupStack::PopAndDestroy(card);
       
   385 	}
       
   386 
       
   387 /** 
       
   388 Default number is edited in PIM and synchronisation is started. 
       
   389 Default number is not edited in phone but new number is inserted
       
   390 
       
   391 1. Choose a phone number field of a phone contact as a default (For example Tel home: 1111)
       
   392 2. Start synchronisation
       
   393 3. Edit the contact's default number in PIM (For example Home: 1111 -> 11112222)
       
   394 4. Start synchronisation
       
   395 5. Default number wasn't amended in phone but new edited number detail (Tel Home 11112222) was created.
       
   396 */
       
   397 void CDefaultVoiceParamTests::TestOneL()
       
   398 	{
       
   399 	TContactItemId id = CreateItemL(KName(),KPhoneNumber());
       
   400 	
       
   401 	CContactIdArray* array = CContactIdArray::NewLC();
       
   402 	array->AddL(id);
       
   403 
       
   404 
       
   405 	ExportContactsL(KVCardFile1,array);
       
   406 	CreateTestDataL(id, KVCardFile2,KModifiedNumberProperty); 
       
   407 	ImportContactsL(KVCardFile2);
       
   408 	ExportContactsL(KVCardFile3,array); //for checking
       
   409 	CleanupStack::PopAndDestroy(array);
       
   410 
       
   411 	TEST_CONDITION(CountPhoneNumberFieldsL(id)==1); //no new field was created
       
   412 	}
       
   413 
       
   414 /**
       
   415 Case2
       
   416 Default number is deleted from PIM and new number is created in PIM. 
       
   417 After synchronisation default number is not deleted from PIM and new number is not created to phone.
       
   418 (If default number is deleted only and synch is started, default number is not deleted from phone)
       
   419 
       
   420 	1. Choose a phone number field of a phone contact as a default
       
   421 		(For example Tel home: 1111)
       
   422 	2. Start Synchronisation
       
   423 	3. Delete default number (Home:1111) in PIM and create a new number in PIM (Work2: 4444)
       
   424 	4. Start synchronisation
       
   425 	5. Default number wasn't deleted from phone and new number wasn't inserted to phone
       
   426 */
       
   427 void CDefaultVoiceParamTests::TestTwoL()
       
   428 	{
       
   429 	TContactItemId id = CreateItemL(KName,KPhoneNumber);
       
   430 
       
   431 	CContactIdArray* array = CContactIdArray::NewLC();
       
   432 	array->AddL(id);
       
   433 	ExportContactsL(KVCardFile1,array);
       
   434 	CreateTestDataL(id,KVCardFile2,KModifiedNumberProperty2); 
       
   435 	ImportContactsL(KVCardFile2);
       
   436 	ExportContactsL(KVCardFile3,array); //for checking
       
   437 	CleanupStack::PopAndDestroy(array);
       
   438 	TEST_CONDITION(CountPhoneNumberFieldsL(id)==1); 
       
   439 
       
   440 	CContactItem* item = iDb->ReadContactLC(id);
       
   441 	CContactItemFieldSet& fieldSet = item->CardFields();
       
   442 	TInt pos=fieldSet.Find(KUidContactFieldPhoneNumber);
       
   443 	while (pos!=KErrNotFound)
       
   444 		{
       
   445 		const CContactItemField& field = (fieldSet)[pos];
       
   446 		if (field.Storage()->IsFull())
       
   447 			{
       
   448 			CContactTextField* textfield = field.TextStorage();
       
   449 			_LIT(KExpectedNumber,"4444");
       
   450 			TEST_CONDITION(textfield->Text()==KExpectedNumber);
       
   451 			}
       
   452 		pos=fieldSet.FindNext(KUidContactFieldPhoneNumber,++pos);
       
   453 		}
       
   454 	CleanupStack::PopAndDestroy(item);
       
   455 	}
       
   456 
       
   457 
       
   458 /**
       
   459 Case3
       
   460 Default number is selected in phone, and same number is edited in PIM. 
       
   461 After synchronisation edited number is not syncronised at all
       
   462 	1. Choose a phone number field of a phone contact as a default
       
   463 		(For example Tel home: 1111)
       
   464 	2. Edit the same number (phone's default) for example
       
   465                          home 1111 to 11114444 in PIM
       
   466 	3. Start Synchronisation
       
   467 	4. Default number was not edited in phone. New number was inserted to phone 
       
   468 	but number was same as the default's number. So, edited number wasn't 
       
   469 	synchronised at all.
       
   470 
       
   471 */
       
   472 void CDefaultVoiceParamTests::TestThreeL()
       
   473 	{
       
   474 	//Not sure how to simulate. I believe this is case is duplicated by TestOneL()
       
   475 	}
       
   476 
       
   477 /*
       
   478 Case 4
       
   479 Default number is selected in phone, same number is deleted from PIM and new 
       
   480 number is created in PIM. 
       
   481 After synchronisation default number is not deleted from PIM but it is duplicated. 
       
   482 	1. Choose a phone number field of a phone contact as a default
       
   483 		(For example Tel home: 1111)
       
   484 	2. Delete the same number (phone's default) in PIM and create a new number in PIM 
       
   485 	3. Start Synchronisation
       
   486 	4. Default is not deleted from PIM. New number is inserted to phone but number is 
       
   487 	same as default's. Number you created in PIM, is copied correctly to phone.
       
   488 
       
   489 */
       
   490 void CDefaultVoiceParamTests::TestFourL()
       
   491 	{
       
   492 	TContactItemId id = CreateItemL(KName,KPhoneNumber);
       
   493 
       
   494 	CContactIdArray* array = CContactIdArray::NewLC();
       
   495 	array->AddL(id);
       
   496 	ExportContactsL(KVCardFile1,array);
       
   497 	CreateTestDataL(id, KVCardFile2,KModifiedNumberProperty3); 
       
   498 	ImportContactsL(KVCardFile2);
       
   499 	ExportContactsL(KVCardFile3,array); //for checking
       
   500 	CleanupStack::PopAndDestroy(array);
       
   501 	TEST_CONDITION(CountPhoneNumberFieldsL(id)==1); //no new field was created	
       
   502 	}
       
   503 
       
   504 
       
   505 /**This checks the contact indentity fields extracted from a known vCard (KTestVcardFile) to ensure
       
   506 	the contact is exactly what we expect. This tests both the field value and label in the generated contact.
       
   507 	*/
       
   508 void CDefaultVoiceParamTests::TestFiveL()
       
   509 	{
       
   510 	CDesCArray* value = new(ELeave) CDesCArrayFlat(KNumberOfExtendedFieldNames);
       
   511 	CleanupStack::PushL(value);
       
   512 	CDesCArray* label = new(ELeave) CDesCArrayFlat(KNumberOfExtendedFieldNames);	
       
   513 	CleanupStack::PushL(label);
       
   514 
       
   515 	for(TInt i=0;i<KNumberOfExtendedFieldNames;i++)
       
   516 		{
       
   517 		value->AppendL(*((const TDesC*) GNames[i]));
       
   518 		label->AppendL(*((const TDesC*) GLabels[i]));
       
   519 		}
       
   520 	ImportContactsL(KTestVcardFile);
       
   521 	const CContactIdArray& list = *iDb->SortedItemsL();
       
   522 	CheckCardL(list[0], value, label);
       
   523 	CleanupStack::PopAndDestroy(2,value); // + label
       
   524 	iDb->DeleteContactL(list[0]);
       
   525 	}
       
   526 
       
   527 /** This generates a whole bunch of conacts (KNumberOfRandomContacts) and exports them to a vCard file.
       
   528 	The contacts are then read back in. This tests the merge functionality of vCard parsing.
       
   529 	The retrieved cards' fields are checked against the original cards' fields to make sure they're the same */
       
   530 void CDefaultVoiceParamTests::TestSixL()
       
   531 	{
       
   532 	CDesCArray* fields = new(ELeave) CDesC16ArrayFlat(KNumberOfExtendedFieldNames*KNumberOfRandomContacts);
       
   533 	CleanupStack::PushL(fields);
       
   534 
       
   535 	CDesCArray* labels = new(ELeave) CDesC16ArrayFlat(KNumberOfExtendedFieldNames);	
       
   536 	CleanupStack::PushL(labels);
       
   537 	TInt  i;	
       
   538 	for( i=0;i<KNumberOfExtendedFieldNames;i++)
       
   539 		{
       
   540 		labels->AppendL(*((const TDesC*) GLabels[i]));
       
   541 		}
       
   542 	RArray<TInt> allIds;
       
   543 	CleanupClosePushL(allIds);
       
   544 			
       
   545 	TInt64 randseed = 0;
       
   546 	CWordDigrams* firstname = CWordDigrams::NewLC(KFirstNameFileName, randseed);
       
   547 	CWordDigrams* lastname = CWordDigrams::NewLC(KLastNameFileName, randseed);
       
   548 	CleanupStack::Pop(lastname);
       
   549 	CleanupStack::Pop(firstname);
       
   550 	CRandomContactGenerator* generator = CRandomContactGenerator::NewL(firstname,lastname );
       
   551 
       
   552 	CleanupStack::PushL(generator);
       
   553 	
       
   554 	generator->SetDbL(*iDb);
       
   555 	RArray<TInt> nameIds;
       
   556 	CleanupClosePushL(nameIds);
       
   557 	for( i = 0; i<KNumberOfExtendedFieldNames;i++)
       
   558 		{
       
   559 		nameIds.AppendL(KExtendedFieldNames[i]);
       
   560 		}
       
   561 
       
   562 
       
   563 	for ( i = 0;i<KNumberOfRandomContacts;i++)
       
   564 		{
       
   565 		TContactItemId id = generator->AddTypicalRandomContactWithNamesL(nameIds, ETrue);
       
   566 		allIds.AppendL(id);
       
   567 		CContactCard* card = STATIC_CAST(CContactCard*, iDb->OpenContactL(id));
       
   568 		CleanupStack::PushL(card);
       
   569 		CheckCard(card,fields, labels,ECheckLabel|EAppendValue );
       
   570 		iDb->CommitContactL(*card);	
       
   571 		CleanupStack::PopAndDestroy(card);
       
   572 	    }
       
   573 	    
       
   574 	CleanupStack::PopAndDestroy( 2, generator );	// + nameIds
       
   575 	
       
   576 	ExportContactsL(KVCardFile1, iDb->SortedItemsL());
       
   577 	ImportContactsL(KVCardFile1);
       
   578 
       
   579 	for (i =0; i<allIds.Count();i++)
       
   580 		{
       
   581 		CheckCardL(allIds[i], fields);
       
   582 		for(TInt j=0;j<KNumberOfExtendedFieldNames;j++)
       
   583 			{
       
   584 			fields->Delete(0);
       
   585 			}
       
   586 		}
       
   587 	CleanupStack::PopAndDestroy(3, fields );	// +label
       
   588 
       
   589 	}
       
   590 
       
   591 
       
   592 /** Import contacts from the vCard file specified by aFileName */
       
   593 void CDefaultVoiceParamTests::ImportContactsL(const TDesC& aFileName)
       
   594 	{
       
   595 	RFileReadStream vcard;
       
   596 	User::LeaveIfError(vcard.Open(iTest.Fs(), aFileName, EFileRead));
       
   597 	CleanupClosePushL(vcard);
       
   598 
       
   599 	TBool success=EFalse;
       
   600 	CArrayPtr<CContactItem>* contactItems=iDb->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, CContactDatabase::EImportSingleContact+CContactDatabase::ETTFormat);
       
   601 	CleanupStack::PopAndDestroy(&vcard);
       
   602 	contactItems->ResetAndDestroy();
       
   603 	delete contactItems;
       
   604 	}
       
   605 
       
   606 /** Export contact items specified by aIds to aFileName */
       
   607 void CDefaultVoiceParamTests::ExportContactsL(const TDesC& aFileName, const CContactIdArray* aIds)
       
   608 	{
       
   609 	RFile outfile;
       
   610 	outfile.Replace(iTest.Fs(),aFileName,EFileWrite);
       
   611 	CleanupClosePushL(outfile);
       
   612 	RFileWriteStream writeStream(outfile);
       
   613 	CleanupClosePushL(writeStream);
       
   614 		
       
   615 	TUid uid;
       
   616 	uid.iUid=KUidVCardConvDefaultImpl;
       
   617 	iDb->ExportSelectedContactsL(uid,*aIds,writeStream,CContactDatabase::EDefault);
       
   618 
       
   619 	writeStream.CommitL();
       
   620 	CleanupStack::PopAndDestroy(2); //writeStream.Close(), outfile.Close()
       
   621 	}
       
   622 
       
   623 
       
   624 /** Creates a vCard with the correct UID for the contact item specified by aId. */
       
   625 void CDefaultVoiceParamTests::CreateTestDataL(TContactItemId aId, const TDesC& aFileName, const TDesC8& aVCardData)
       
   626 	{
       
   627 	RFile file;
       
   628 	TInt err=file.Replace(iTest.Fs(), aFileName, EFileWrite+EFileShareAny+EFileStreamText);
       
   629 	User::LeaveIfError(err);
       
   630 	TPtrC8 header((const TText8*)	"BEGIN:VCARD\r\n"
       
   631 									"VERSION:2.1\r\n"
       
   632 									"REV:20020520T134824Z\r\n"
       
   633 									"N:Name;;;;\r\n");
       
   634 	TPtrC8 footer((const TText8*)	"END:VCARD\r\n");
       
   635 	TPtrC8 uidProperty((const TText8*)	"UID:");
       
   636 	TPtrC8 endProperty((const TText8*)	"\r\n");
       
   637 	file.Write(header);
       
   638 	file.Write(uidProperty);
       
   639 	HBufC* buffer = iTest.ContactUidLC(aId,KMachineUniqueId);
       
   640 	TBuf8<KMaxExternalizedTokenLength> des;
       
   641 	des.Copy(*buffer);
       
   642 	file.Write(des);
       
   643 	CleanupStack::PopAndDestroy(buffer);
       
   644 	file.Write(endProperty);
       
   645 	file.Write(aVCardData);
       
   646 	file.Write(footer);
       
   647 	file.Close();
       
   648 	}
       
   649 
       
   650 /** Count the number of non-empty phone number fields */
       
   651 TInt CDefaultVoiceParamTests::CountPhoneNumberFieldsL(TContactItemId aId)
       
   652 	{
       
   653 	CContactItem* item = iDb->ReadContactLC(aId);
       
   654 	CContactItemFieldSet& fieldSet = item->CardFields();
       
   655 	TInt numPhoneNumbers=0;
       
   656 	TInt pos=fieldSet.Find(KUidContactFieldPhoneNumber);
       
   657 	while (pos!=KErrNotFound)
       
   658 		{
       
   659 		const CContactItemField& field = (fieldSet)[pos];
       
   660 		if (field.Storage()->IsFull())
       
   661 			numPhoneNumbers++;
       
   662 		pos=fieldSet.FindNext(KUidContactFieldPhoneNumber,++pos);
       
   663 		}
       
   664 	CleanupStack::PopAndDestroy(item);
       
   665 	return numPhoneNumbers;
       
   666 	}
       
   667 
       
   668 /** Create a contact item */
       
   669 TContactItemId CDefaultVoiceParamTests::CreateItemL(const TDesC& aName, const TDesC& aNumber)
       
   670 	{
       
   671 	TContactItemId templateId = iDb->TemplateId();
       
   672 	CContactItem* templateCard = iDb->ReadContactLC(templateId);
       
   673 	CContactCard* card=CContactCard::NewL(templateCard); 
       
   674 	CleanupStack::PushL(card);
       
   675 	SetNameL(*card, KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN, aName, EFalse);
       
   676 	SetNameL(*card, KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL, aNumber, EFalse);
       
   677 	TContactItemId id = iDb->AddNewContactL(*card);
       
   678 	CleanupStack::PopAndDestroy(2,templateCard); 
       
   679 	return id;
       
   680 	}
       
   681 
       
   682 
       
   683 /** 
       
   684  * Regression testcode for defect EXT-599EF6 "Problems appear when contact's 
       
   685  * default number is deleted / edited in PIM - Lotus Organizer 6.0"
       
   686  * 
       
   687  * The basic problem is:
       
   688  * SymbianOS uses TEL;VOICE in a vCard to represent a phone number
       
   689  * TimeIS PC Connectivity code sends TEL;VOICE to PIM
       
   690  * PIM removes VOICE property parameter
       
   691  * TimeIS sends TEL back 
       
   692  * Contacts model does not match TEL;VOICE and TEL, so a duplicate number is added
       
   693  *
       
   694  */
       
   695 LOCAL_C void DefaultVoiceParamTestsL()
       
   696 	{
       
   697 	CDefaultVoiceParamTests* test = CDefaultVoiceParamTests::NewLC(*CntTest);
       
   698 	test->RunTestsL();
       
   699 	CleanupStack::PopAndDestroy(test);		
       
   700 	}
       
   701 
       
   702 /** Add KNumAccessCountContacts contacts **/
       
   703 LOCAL_C void AddNewContactsL()
       
   704 	{
       
   705 	_LIT(KNameFormat,"NAME #%d");
       
   706 	_LIT(KPhoneNumber,"123");
       
   707 	for (TInt ii=0;ii<KNumAccessCountContacts;ii++)
       
   708 		{
       
   709 		CContactCard* card=CContactCard::NewL();
       
   710 		CleanupStack::PushL(card);
       
   711 		TBuf<16> name;
       
   712 		name.Format(KNameFormat,ii);
       
   713 		SetNameL(*card, KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN, name, EFalse);
       
   714 		SetNameL(*card, KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL, KPhoneNumber, EFalse);
       
   715 		CntTest->Db()->AddNewContactL(*card);
       
   716 		CleanupStack::PopAndDestroy(card); 
       
   717 		}
       
   718 	}	
       
   719 
       
   720 
       
   721 LOCAL_C void EmptyDatabase()
       
   722 	{
       
   723 	TContactItemId theid;
       
   724 	TContactIter iter(*CntTest->Db());
       
   725 	theid=iter.FirstL();
       
   726 	while(theid!=KNullContactId )
       
   727 		{
       
   728 		CntTest->Db()->DeleteContactL(theid);
       
   729 		theid=iter.NextL();
       
   730 		}
       
   731 	}
       
   732 	
       
   733 
       
   734 enum TAccessCountFlags {EIncAccessCount,EDecAccessCount};
       
   735 
       
   736 LOCAL_C void UpdateAccessCountL(TAccessCountFlags aUpdateType)
       
   737 	{
       
   738 	TContactItemId theid;
       
   739 	TContactIter iter(*CntTest->Db());
       
   740 	theid=iter.FirstL();
       
   741 	while(theid!=KNullContactId )
       
   742 		{
       
   743 		CContactItem* contactItem=CntTest->Db()->OpenContactL(theid);
       
   744 		CleanupStack::PushL(contactItem);
       
   745 		if (contactItem->IsDeleted())
       
   746 			test.Next(_L("Synchronizer knows record 2 is deleted"));
       
   747 
       
   748 		if(aUpdateType==EIncAccessCount)
       
   749 			contactItem->IncAccessCount();
       
   750 		else
       
   751 			contactItem->DecAccessCount();
       
   752 		
       
   753 		theid=iter.NextL();
       
   754 		CntTest->Db()->CommitContactL(*contactItem);
       
   755 		CleanupStack::PopAndDestroy(contactItem);	
       
   756 		}
       
   757 	}
       
   758 
       
   759 /** Tests from T_EXPDEL */
       
   760 LOCAL_C void AccessCountTestsL()
       
   761 	{
       
   762 	CContactDatabase* db=CntTest->CreateDatabaseL();
       
   763 	AddNewContactsL();
       
   764 	TEST_CONDITION(db->CountL()==KNumAccessCountContacts);
       
   765 
       
   766 	test.Next(_L("Increase Access Count"));
       
   767 	UpdateAccessCountL(EIncAccessCount);
       
   768 	
       
   769 	test.Next(_L("Removing second contact"));
       
   770 	TContactIter iter(*db);
       
   771 	TContactItemId theid=iter.FirstL();
       
   772 	theid=iter.NextL();
       
   773 	db->DeleteContactL(theid);
       
   774 	TEST_CONDITION(db->CountL()==KNumAccessCountContacts-1);
       
   775 	
       
   776 #if defined(_DEBUG) //test case only valid in debug mode, see CContactDatabase::DeletedContactsLC for detail.
       
   777 	test.Next(_L("Checking DeletedContactsLC Deleted Count and Identity"));
       
   778 	CContactIdArray* delArray = db->DeletedContactsLC();
       
   779 	TEST_CONDITION(delArray->Count() == 1);  // as second contact is deleted but has access count of > 1
       
   780 	TEST_CONDITION((*delArray)[0] == theid); // test the DeletedContact Id is the second Id (that was deleted above)	
       
   781 	
       
   782 	CleanupStack::PopAndDestroy(delArray);
       
   783 #endif //defined(_DEBUG)
       
   784 	
       
   785 	test.Next(_L("Synchronizing"));
       
   786 	UpdateAccessCountL(EDecAccessCount);
       
   787 	TEST_CONDITION(db->CountL()==KNumAccessCountContacts-1);
       
   788 	test.Next(_L("Exporting"));
       
   789 	CContactIdArray* TheIds=CContactIdArray::NewLC();
       
   790 	theid=iter.FirstL();
       
   791 	while(theid!=KNullContactId )
       
   792 		{
       
   793 		TheIds->AddL(theid);
       
   794 		theid=iter.NextL();
       
   795 		}			
       
   796 	CFileStore* store = CDirectFileStore::ReplaceLC(CntTest->Fs(),KExpDelAFileName(),EFileWrite);
       
   797 	store->SetTypeL(KDirectFileStoreLayoutUid);
       
   798 	RStoreWriteStream outstream;
       
   799 	TStreamId id = outstream.CreateLC(*store);
       
   800 	TUid uid;
       
   801 	uid.iUid= KVersitEntityUidVCard; // KUidVCardConvDefaultImpl;
       
   802 	db->ExportSelectedContactsL(uid,*TheIds,outstream,CContactDatabase::EIncludeX);
       
   803 	outstream.CommitL();
       
   804 	store->SetRootL(id);
       
   805 	store->CommitL();  	
       
   806 	CleanupStack::PopAndDestroy(2); // store+ oustream
       
   807 	CleanupStack::PopAndDestroy(TheIds); 
       
   808 	test.Next(_L("Emptying database"));
       
   809 	EmptyDatabase();
       
   810 	}
       
   811 
       
   812 //
       
   813 /* Test Function Implementations                                             */
       
   814 //
       
   815 TContactItemId AddContactL (CContactDatabase& aDatabase)
       
   816 	{
       
   817 	TInt bit = 0;
       
   818 	TContactItemId retval;
       
   819 	CRandomContactGenerator* generator = NULL;
       
   820 
       
   821 	generator = CRandomContactGenerator::NewL();
       
   822 	CleanupStack::PushL(generator);
       
   823 
       
   824 	generator->SetDbL(aDatabase);
       
   825 
       
   826 	bit |= CContactDatabase::ESmsable;
       
   827 	retval = generator->AddTypicalContactForFilterL(bit);
       
   828 
       
   829 	CleanupStack::PopAndDestroy( generator );
       
   830 	
       
   831 	return retval;
       
   832 	}
       
   833 
       
   834 void ExportContactTestL()
       
   835 	{
       
   836 	TContactItemId itemId;
       
   837 	RFs fsSession;
       
   838 	RFileWriteStream fileStream;
       
   839 	CContactIdArray* idArray = NULL;
       
   840 	CContactItem* contactAdded = NULL;
       
   841 	TInt firstNameIndex = KErrNotFound;
       
   842 
       
   843 	idArray = CContactIdArray::NewL();
       
   844 
       
   845 	CleanupStack::PushL(idArray);
       
   846 
       
   847 	User::LeaveIfError(fsSession.Connect());
       
   848 	User::LeaveIfError(fileStream.Replace(fsSession, KOutputFileName, EFileWrite));
       
   849 
       
   850 
       
   851 	itemId = AddContactL(*CntTest->Db());
       
   852 	contactAdded = CntTest->Db()->OpenContactL(itemId);
       
   853 	CleanupStack::PushL(contactAdded);
       
   854 
       
   855 	CContactItemFieldSet& fieldSet = contactAdded->CardFields();
       
   856 	
       
   857 	firstNameIndex = fieldSet.Find(KUidContactFieldGivenName);
       
   858 	TEST_CONDITION(firstNameIndex != KErrNotFound);
       
   859 
       
   860 	CContactItemField& field = fieldSet[firstNameIndex];
       
   861 	field.SetLabelL(_L(" New Label "));
       
   862 
       
   863 	CntTest->Db()->CommitContactL(*contactAdded);
       
   864 	CleanupStack::PopAndDestroy(contactAdded);
       
   865 	contactAdded = NULL;
       
   866 
       
   867 
       
   868 	idArray->InsertL(0, itemId);
       
   869 
       
   870 	CntTest->Db()->ExportSelectedContactsL(TUid::Uid(KUidVCardConvDefaultImpl), *idArray, fileStream, 0 /*option*/);
       
   871 	CntTest->Db()->ExportSelectedContactsL(TUid::Uid(KUidVCardConvDefaultImpl), *idArray, fileStream, 1 /*option*/);
       
   872 
       
   873 	CleanupStack::PopAndDestroy(idArray);
       
   874 
       
   875 	fileStream.Close();
       
   876 	fsSession.Close();
       
   877 	}
       
   878 
       
   879 // Export a contact which has no name field,
       
   880 // and check that the exported file doesn't contain a name field:
       
   881 void ExportNamelessContactTestL()
       
   882 	{
       
   883 	TContactItemId itemId;
       
   884 	RFs fsSession;
       
   885 	RFileWriteStream fileStream;
       
   886 	RFileReadStream readStream;
       
   887 	CContactIdArray* idArray = NULL;
       
   888 	CContactItem* contactAdded = NULL;
       
   889 	TInt Index = KErrNotFound;
       
   890 
       
   891 	idArray = CContactIdArray::NewL();
       
   892 
       
   893 	CleanupStack::PushL(idArray);
       
   894 
       
   895 	User::LeaveIfError(fsSession.Connect());
       
   896 	User::LeaveIfError(fileStream.Replace(fsSession, KOutputFileName, EFileWrite));
       
   897 
       
   898 	// Create the usual random contact:
       
   899 	itemId = AddContactL(*CntTest->Db());
       
   900 	contactAdded = CntTest->Db()->OpenContactL(itemId);
       
   901 	CleanupStack::PushL(contactAdded);
       
   902 
       
   903 	// Find the name fields and remove them:
       
   904 	CContactItemFieldSet& fieldSet = contactAdded->CardFields();
       
   905 	Index = fieldSet.Find(KUidContactFieldGivenName);
       
   906 	if(Index != KErrNotFound)
       
   907 		fieldSet.Remove(Index);
       
   908 	Index = fieldSet.Find(KUidContactFieldFamilyName);
       
   909 	if(Index != KErrNotFound)
       
   910 		fieldSet.Remove(Index);
       
   911 	Index = fieldSet.Find(KUidContactFieldAdditionalName);
       
   912 	if(Index != KErrNotFound)
       
   913 		fieldSet.Remove(Index);
       
   914 	Index = fieldSet.Find(KUidContactFieldPrefixName);
       
   915 	if(Index != KErrNotFound)
       
   916 		fieldSet.Remove(Index);
       
   917 	Index = fieldSet.Find(KUidContactFieldSuffixName);
       
   918 	if(Index != KErrNotFound)
       
   919 		fieldSet.Remove(Index);
       
   920 
       
   921 	// Put the contact back:
       
   922 	CntTest->Db()->CommitContactL(*contactAdded);
       
   923 	CleanupStack::PopAndDestroy(contactAdded);
       
   924 	contactAdded = NULL;
       
   925 
       
   926 	// Export it:
       
   927 	idArray->InsertL(0, itemId);
       
   928 	CntTest->Db()->ExportSelectedContactsL(TUid::Uid(KUidVCardConvDefaultImpl), *idArray, fileStream, 0 /*option*/);
       
   929 	CleanupStack::PopAndDestroy(idArray);
       
   930 	fileStream.Close();
       
   931 
       
   932 	// Verify the output by reading it in and searching it long hand:
       
   933 	User::LeaveIfError(readStream.Open(fsSession, KOutputFileName, EFileRead));
       
   934 	HBufC8 * buf = HBufC8::NewLC(100);
       
   935 	TPtr8 ptr = buf->Des();
       
   936 	TRAPD(ret, readStream.ReadL(ptr));
       
   937 	TEST_CONDITION(ret == KErrNone || (ret == KErrEof && buf->Length() > 0));
       
   938 
       
   939 	// This is the important test - we should not find a name entry:
       
   940 	_LIT8(str, "\nN:");
       
   941 	TEST_CONDITION(buf->Find(str) == KErrNotFound);
       
   942 
       
   943 	// Clean up:
       
   944 	CleanupStack::PopAndDestroy(buf);
       
   945 
       
   946 	readStream.Close();
       
   947 	fsSession.Close();
       
   948 	}
       
   949 
       
   950 //=======================================================================
       
   951 //FindInFields
       
   952 //Finds a given value in the field set, assumes that the fields are text.
       
   953 //Used by CheckTypeSupport.
       
   954 //=======================================================================
       
   955 TBool FindInFields(CContactItemFieldSet& aFieldSet,TPtrC aExpectedValue)
       
   956 	{
       
   957 	for (TInt i = 0; i< aFieldSet.Count(); i++ )
       
   958 		{
       
   959 		CContactItemField& aFieldValue = aFieldSet[i];
       
   960 		TPtrC value = aFieldValue.TextStorage()->Text();
       
   961 		if (value == aExpectedValue)
       
   962 			{
       
   963 			return ETrue;
       
   964 			}
       
   965 		}
       
   966 		
       
   967 	// Value not found, return false.
       
   968 	return EFalse;
       
   969 	}
       
   970 
       
   971 //=======================================================================
       
   972 //FieldCheck
       
   973 //Compares the value of the CContactItemField at aIndex of aFieldSet
       
   974 //with the expected value.  Used by UpdateVCardTestL
       
   975 //=======================================================================
       
   976 TBool FieldCheck(CContactItemFieldSet& aFieldSet,TInt aIndex,TPtrC aExpectedValue)
       
   977 	{
       
   978 	CContactItemField& aPhoneNum = aFieldSet[aIndex];
       
   979 	TPtrC value = aPhoneNum.TextStorage()->Text();
       
   980 	return (value == aExpectedValue);
       
   981 	}
       
   982 //=======================================================================
       
   983 //UpdateVCardTestL()
       
   984 //Checks updating of a contact via a modified vcard.
       
   985 //1: Contact is imported from vcardupdate1.vcf
       
   986 //2: Contact is exported to vcardupdate2.vcf
       
   987 //3: Contact is updated from vcardupdate3.vcf
       
   988 //=======================================================================
       
   989 void UpdateVCardTestL()
       
   990 	{
       
   991 	test.Next(_L("Update VCard Test"));
       
   992 	//Check the database is empty before we start
       
   993 	EmptyDatabase();
       
   994 	TInt count = CntTest->Db()->CountL();
       
   995 	TEST_CONDITION(count == 0);
       
   996 	test.Next(_L("Database Empty"));
       
   997 	
       
   998 	RFs fsSession;
       
   999 	User::LeaveIfError(fsSession.Connect());
       
  1000 	
       
  1001 	RFileReadStream readStream;
       
  1002 	RFileWriteStream writeStream;
       
  1003 	CContactIdArray* idArray = NULL;
       
  1004 	
       
  1005 	idArray = CContactIdArray::NewL();
       
  1006 	CleanupStack::PushL(idArray);
       
  1007 		
       
  1008 	//Import vcardupdate1.vcf
       
  1009 	test.Next(_L("Importing contact from vcardupdate1.vcf"));
       
  1010 	User::LeaveIfError(readStream.Open(fsSession,KVCardUpdate1, EFileRead));
       
  1011 	CleanupClosePushL(readStream);
       
  1012 	TBool success=EFalse;
       
  1013 	CArrayPtr<CContactItem>* contactItems=CntTest->Db()->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), readStream, success, CContactDatabase::EImportSingleContact+CContactDatabase::ETTFormat);
       
  1014 	//Store the id of the new contact - we need this later
       
  1015 	TContactItemId contactId = (*contactItems)[0]->Id();
       
  1016 	idArray->InsertL(0, contactId);
       
  1017 	readStream.Close();
       
  1018 	//Should be the only contact in the database at this point
       
  1019 	count = CntTest->Db()->CountL();
       
  1020 	TEST_CONDITION(count == 1);
       
  1021 	test.Next(_L("Imported"));
       
  1022 	
       
  1023 	//Retrieve the new contact from the database and check the fields
       
  1024 	CContactItem* importedContact = CntTest->Db()->ReadContactLC(contactId);
       
  1025 	CContactItemFieldSet& importedContactFieldSet = importedContact->CardFields();
       
  1026 
       
  1027 	/*
       
  1028 	* For this particular test we are only interested in the following field indexes:
       
  1029 	* 5:	Mobile 1
       
  1030 	* 6:	Mobile 2
       
  1031 	* 29:	Work tel 1
       
  1032 	* 30:	Work tel 2
       
  1033 	* 31:	Work tel 3
       
  1034 	* 32:	Work tel 4
       
  1035 	* 33:	Work tel 5
       
  1036 	* You can get other field indexes by using:
       
  1037  	*
       
  1038 	 _LIT(KFormat,"%d:\t");
       
  1039 	 for (TInt i = 0; i < importedContactFieldSet.Count();i++)
       
  1040 		{
       
  1041 		CContactItemField& aPhoneNum = importedContactFieldSet[i];
       
  1042 		TPtrC label = aPhoneNum.Label();
       
  1043 		test.Printf(KFormat,i);
       
  1044 		test.Printf(label);
       
  1045 		}
       
  1046 	*/
       
  1047 	
       
  1048 	test.Next(_L("Check Mobile Number 1"));
       
  1049 	TEST_CONDITION(FieldCheck(importedContactFieldSet,5,_L("07905111")));
       
  1050 		
       
  1051 	test.Next(_L("Check Mobile Number 2"));
       
  1052 	TEST_CONDITION(FieldCheck(importedContactFieldSet,6,_L("07906222")));
       
  1053 
       
  1054 	test.Next(_L("Check Work Number 1"));
       
  1055 	TEST_CONDITION(FieldCheck(importedContactFieldSet,29,_L("1111111111")));
       
  1056 
       
  1057 	test.Next(_L("Check Work Number 2"));
       
  1058 	TEST_CONDITION(FieldCheck(importedContactFieldSet,30,_L("2222222222")));
       
  1059 
       
  1060 	test.Next(_L("Check Work Number 3"));
       
  1061 	TEST_CONDITION(FieldCheck(importedContactFieldSet,31,_L("3333333333")));
       
  1062 
       
  1063 	test.Next(_L("Check Work Number 4"));
       
  1064 	TEST_CONDITION(FieldCheck(importedContactFieldSet,32,_L("4444444444")));
       
  1065 
       
  1066 	test.Next(_L("Check Work Number 5"));
       
  1067 	TEST_CONDITION(FieldCheck(importedContactFieldSet,33,_L("5555555555")));
       
  1068 	
       
  1069 	CleanupStack::PopAndDestroy(importedContact);
       
  1070 
       
  1071 	//Export the contact to vcardupdate2.vcf
       
  1072 	test.Next(_L("Exporting contact to vcardupdate2.vcf"));
       
  1073 	User::LeaveIfError(writeStream.Replace(fsSession,KVCardUpdate2,EFileWrite));
       
  1074 	CleanupClosePushL(writeStream);
       
  1075 	CntTest->Db()->ExportSelectedContactsL(TUid::Uid(KUidVCardConvDefaultImpl), *idArray, writeStream,0);
       
  1076 	CleanupStack::PopAndDestroy(&writeStream);
       
  1077 
       
  1078 	//Import vcardupdate3.vcf - this should have the same UID as vcardupdate2.vcf
       
  1079 	test.Next(_L("Updating contact from vcardupdate3.vcf"));
       
  1080 	User::LeaveIfError(readStream.Open(fsSession,KVCardUpdate3, EFileRead));
       
  1081 	success=EFalse;
       
  1082 	contactItems->ResetAndDestroy();
       
  1083 	delete contactItems;
       
  1084 	contactItems = CntTest->Db()->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), readStream, success, CContactDatabase::EImportSingleContact+CContactDatabase::ETTFormat);
       
  1085 	
       
  1086 	//We are updating the previous contact so we should still only have
       
  1087 	//one database entry.  If count == 2 here check the uids of C:\\vcardupdate2.vcf and 
       
  1088 	//Z:\\System\\Programs\\CntmodelTest\\vcardupdate3.vcf - the last digits of vcardupdate3 should match vcardupdate2
       
  1089 	count = CntTest->Db()->CountL();
       
  1090 	//TEST_CONDITION(count == 1);
       
  1091 	test.Next(_L("Imported"));
       
  1092 
       
  1093 	//Retrieve the updated contact from the database and check the fields
       
  1094 	//Use the same contact id as before
       
  1095 	CContactItem* updatedContact = CntTest->Db()->ReadContactLC(contactId);
       
  1096 	CContactItemFieldSet& updatedContactFieldSet = updatedContact->CardFields();
       
  1097 
       
  1098 	test.Next(_L("Check Updated Mobile Number 1"));
       
  1099 	TEST_CONDITION(FieldCheck(updatedContactFieldSet,5,_L("7700900329")));
       
  1100 		
       
  1101 	test.Next(_L("Check Updated Mobile Number 2"));
       
  1102 	TEST_CONDITION(FieldCheck(updatedContactFieldSet,6,_L("07700900529")));
       
  1103 
       
  1104 	test.Next(_L("Check Updated Work Number 1"));
       
  1105 	TEST_CONDITION(FieldCheck(updatedContactFieldSet,29,_L("7700900999")));
       
  1106 
       
  1107 	test.Next(_L("Check Updated Work Number 2"));
       
  1108 	TEST_CONDITION(FieldCheck(updatedContactFieldSet,30,_L("7700900888")));
       
  1109 
       
  1110 	test.Next(_L("Check Updated Work Number 3"));
       
  1111 	TEST_CONDITION(FieldCheck(updatedContactFieldSet,31,_L("7700900777")));
       
  1112 
       
  1113 	test.Next(_L("Check Updated Work Number 4"));
       
  1114 	TEST_CONDITION(FieldCheck(updatedContactFieldSet,32,_L("7700900666")));
       
  1115 
       
  1116 	test.Next(_L("Check Updated Work Number 5"));
       
  1117 	TEST_CONDITION(FieldCheck(updatedContactFieldSet,33,_L("7700900555")));
       
  1118  
       
  1119 	//cleanup
       
  1120 	CleanupStack::PopAndDestroy(updatedContact);
       
  1121 	CleanupStack::PopAndDestroy(&readStream);
       
  1122 	CleanupStack::PopAndDestroy(idArray);
       
  1123 	fsSession.Close();
       
  1124 	contactItems->ResetAndDestroy();
       
  1125 	delete contactItems;
       
  1126 	}
       
  1127 
       
  1128 /*
       
  1129 The following is the test for trailing space to test DEF051853.
       
  1130 */
       
  1131 
       
  1132 const TUint KSpaceChar = 0x20;
       
  1133 
       
  1134 _LIT( KSearchText, "\x2029" );
       
  1135 _LIT( KIncommingVCard, "z:\\cntvcard\\address-with-trailing-white-space.vcf" );
       
  1136 _LIT( KOutgoingVCard, "c:\\address-with-trailing-white-space-out.vcf" );
       
  1137 
       
  1138 /** Import contacts from the vCard file specified by aFileName */
       
  1139 CArrayPtr<CContactItem>* ImportContactsL(const TDesC& aFileName, CContactDatabase& aDatabase);
       
  1140 void CheckForWhiteSpaceL( const TDesC& aFieldToCheck );
       
  1141 void ImportAndCheckForNoTrailSpaceStrippingL(const TDesC& aFileName, CContactIdArray& aArray);
       
  1142 void TrailingSpaceExportContactsL(const TDesC& aFileName, CContactIdArray* aIds);
       
  1143 void DoTrailingSpaceImportAndExportTestL();
       
  1144 
       
  1145 // Implementations
       
  1146 CArrayPtr<CContactItem>* ImportContactsL(const TDesC& aFileName, CContactDatabase& aDatabase)
       
  1147 	{
       
  1148 	CArrayPtr<CContactItem>* retval = NULL;
       
  1149 	RFileReadStream vcard;
       
  1150 	RFs fileSystem;
       
  1151 	User::LeaveIfError( fileSystem.Connect() );
       
  1152 	CleanupClosePushL( fileSystem );
       
  1153 	User::LeaveIfError(vcard.Open( fileSystem, aFileName, EFileRead));
       
  1154 	CleanupClosePushL(vcard);
       
  1155 
       
  1156 	TBool success=EFalse;
       
  1157 	retval = aDatabase.ImportContactsL(TUid::Uid(KVersitEntityUidVCard), vcard, success, CContactDatabase::EImportSingleContact+CContactDatabase::ETTFormat);
       
  1158 	CleanupStack::PopAndDestroy(&vcard);
       
  1159 	CleanupStack::PopAndDestroy(&fileSystem);
       
  1160 
       
  1161 	return retval;
       
  1162 	}
       
  1163 
       
  1164 void CheckForWhiteSpaceL( const TDesC& aFieldToCheck )
       
  1165 	{
       
  1166 	TInt trailingSpaceLocation = KErrNotFound;
       
  1167 	TUint thisShouldBeASpaceChar = 0;
       
  1168 
       
  1169 	trailingSpaceLocation = ( aFieldToCheck.Find(KSearchText) -1 );
       
  1170 
       
  1171 	thisShouldBeASpaceChar = aFieldToCheck[ trailingSpaceLocation ];
       
  1172 	test ( thisShouldBeASpaceChar == KSpaceChar );
       
  1173 
       
  1174 	}
       
  1175 
       
  1176 void ImportAndCheckForNoTrailSpaceStrippingL(const TDesC& aFileName, CContactIdArray& aArray)
       
  1177 	{
       
  1178 	CArrayPtr<CContactItem>* contactList = NULL;
       
  1179 	CContactItem* item = NULL;
       
  1180 	TInt addressIndex = KErrNotFound;
       
  1181 
       
  1182 	aArray.Reset();
       
  1183 
       
  1184 	contactList = ImportContactsL( aFileName, *CntTest->Db() );
       
  1185 	CleanupStack::PushL( contactList );
       
  1186 
       
  1187 	item = (*contactList)[0];
       
  1188 	aArray.AddL( item->Id() );
       
  1189 	CContactItemFieldSet& fieldSet = item->CardFields();
       
  1190 
       
  1191 	addressIndex = fieldSet.Find( KUidContactFieldAddress );
       
  1192 
       
  1193 	CContactItemField& addressField = fieldSet[ addressIndex ];
       
  1194 
       
  1195 
       
  1196 	TPtrC data = addressField.TextStorage()->Text();
       
  1197 
       
  1198 	CheckForWhiteSpaceL( data );
       
  1199 
       
  1200 	contactList->ResetAndDestroy();
       
  1201 	CleanupStack::PopAndDestroy( contactList );
       
  1202 
       
  1203 	}
       
  1204 
       
  1205 /** Export contact items specified by aIds to aFileName */
       
  1206 void TrailingSpaceExportContactsL(const TDesC& aFileName, CContactIdArray* aIds)
       
  1207 	{
       
  1208 	RFs fsSession;
       
  1209 	RFileWriteStream fileStream;
       
  1210 	
       
  1211 	User::LeaveIfError(fsSession.Connect());
       
  1212 	User::LeaveIfError(fileStream.Replace(fsSession, aFileName, EFileWrite));
       
  1213 
       
  1214 	CntTest->Db()->ExportSelectedContactsL(TUid::Uid(KVersitEntityUidVCard), *aIds, fileStream, 0 /*option*/);
       
  1215 
       
  1216 	fileStream.Close();
       
  1217 	fsSession.Close();
       
  1218 
       
  1219 	}
       
  1220 
       
  1221 
       
  1222 void DoTrailingSpaceImportAndExportTestL()
       
  1223 	{
       
  1224 	CContactIdArray* idArray = NULL;
       
  1225 
       
  1226 	idArray = CContactIdArray::NewL();
       
  1227 	CleanupStack::PushL( idArray );
       
  1228 
       
  1229 	ImportAndCheckForNoTrailSpaceStrippingL(KIncommingVCard, *idArray);
       
  1230 	TrailingSpaceExportContactsL( KOutgoingVCard, idArray );
       
  1231 
       
  1232 	ImportAndCheckForNoTrailSpaceStrippingL(KOutgoingVCard, *idArray);
       
  1233 	CleanupStack::PopAndDestroy( idArray );
       
  1234 	}
       
  1235 
       
  1236 
       
  1237 void ImportLargeVCardTestL()
       
  1238 	{
       
  1239  	test.Printf(_L("Importing large VCards"));
       
  1240  	
       
  1241 	EmptyDatabase();
       
  1242 			
       
  1243 	RFs fsSession;
       
  1244 	User::LeaveIfError(fsSession.Connect());
       
  1245 	
       
  1246 	RFileReadStream readStream;
       
  1247 		
       
  1248  	// Import vcardupdate1.vcf.
       
  1249  	test.Printf(_L("Importing contact from testvcard1.vcf"));
       
  1250 	User::LeaveIfError(readStream.Open(fsSession,KTestVcardFile1, EFileRead));
       
  1251  
       
  1252  	CleanupClosePushL(readStream);
       
  1253  
       
  1254  	TBool success=EFalse;
       
  1255  	CArrayPtr<CContactItem>* contactItems=CntTest->Db()->ImportContactsL(
       
  1256  		TUid::Uid(KUidVCardConvDefaultImpl), readStream, success, 
       
  1257  		CContactDatabase::EImportSingleContact+CContactDatabase::ETTFormat);
       
  1258  
       
  1259  	// If the above function does not leave or panic test has been successful.
       
  1260  	
       
  1261  	TEST_CONDITION(contactItems->Count() == 1);
       
  1262  	
       
  1263  	test.Printf(_L("Importing large VCards successful\n"));
       
  1264  
       
  1265  	contactItems->ResetAndDestroy();
       
  1266  	delete contactItems;
       
  1267  
       
  1268  	CleanupStack::PopAndDestroy(&readStream);	
       
  1269  
       
  1270  	fsSession.Close();			
       
  1271  	}
       
  1272  	
       
  1273 
       
  1274 //return the number of email fields within a contact
       
  1275 TInt ContactEmailCountL(TContactItemId aContactId)
       
  1276 	{
       
  1277 	
       
  1278 	CContactItem *item = CntTest->Db()->ReadContactLC(aContactId);
       
  1279 	TInt pos = 0;
       
  1280 	TInt many = 0;
       
  1281 	while (pos < item->CardFields().Count() )
       
  1282 		{
       
  1283 		pos = item->CardFields().FindNext(KUidContactFieldEMail, pos);
       
  1284 		if( pos == KErrNotFound )
       
  1285 			{
       
  1286 			break;
       
  1287 			}
       
  1288 		else
       
  1289 			{
       
  1290 			++many;
       
  1291 			++pos;
       
  1292 			}
       
  1293 		}
       
  1294 	
       
  1295 	CleanupStack::PopAndDestroy(item);
       
  1296 	return many;
       
  1297 	
       
  1298 	}
       
  1299 
       
  1300 //import a single contact item from vcard into db, return id of contact in db
       
  1301 TContactItemId ImportVCardL(const TDesC &aVcard, const TInt aOption)
       
  1302 	{
       
  1303 	RFs fsSession;
       
  1304 	User::LeaveIfError(fsSession.Connect());
       
  1305 	
       
  1306 	RFileReadStream readStream;
       
  1307 		
       
  1308 	//Import vcf
       
  1309 	User::LeaveIfError(readStream.Open(fsSession,aVcard, EFileRead));
       
  1310 	CleanupClosePushL(readStream);
       
  1311 	TBool success=EFalse;
       
  1312 	CArrayPtr<CContactItem>* contactItems = CntTest->Db()->ImportContactsL(
       
  1313 												TUid::Uid(KVersitEntityUidVCard), readStream, success, 
       
  1314 												aOption
       
  1315 											);
       
  1316 											
       
  1317 	//Store the id of the new contact - we need this later
       
  1318 	TContactItemId contactId = (*contactItems)[0]->Id();
       
  1319 	contactItems->ResetAndDestroy();
       
  1320 	delete contactItems;
       
  1321 	contactItems = NULL;
       
  1322 	//cleanup
       
  1323 	CleanupStack::PopAndDestroy(&readStream);
       
  1324 	fsSession.Close();
       
  1325 	return contactId;
       
  1326 	}
       
  1327 	
       
  1328 //returns the number of non empty fields in contact
       
  1329 TInt ContactFilledFields(TContactItemId aContactId)
       
  1330 	{
       
  1331 	CContactItem *item = CntTest->Db()->ReadMinimalContactLC(aContactId);
       
  1332 	TInt ret = item->CardFields().Count();
       
  1333 	CleanupStack::PopAndDestroy(item);
       
  1334 	return ret;
       
  1335 	}
       
  1336 
       
  1337 /**
       
  1338 @SYMTestCaseID PIM-T-CNTVCARD-DEF084708-0001
       
  1339 @SYMTestType UT
       
  1340 @SYMTestPriority High
       
  1341 @SYMDEF DEF084708
       
  1342 @SYMTestCaseDesc Vcard with full parameter description of TYPE=VALUE for properties 
       
  1343 ADDR or TEL should be correctly imported.
       
  1344 
       
  1345 @SYMTestActions
       
  1346 1) import vcard with home and tel properties with parameters of TYPE=HOME and TYPE=WORK
       
  1347 2) check that imported contact contains the correct home and work address 
       
  1348 
       
  1349 @SYMTestExpectedResults For the above tests:
       
  1350 On importing, the type=home and type=work fields should not be deleted.
       
  1351 
       
  1352 */
       
  1353 void CheckTypeSupport(const TDesC &inputVcard)
       
  1354 {
       
  1355 	test.Next(_L("@SYMTestCaseID:PIM-T-CNTVCARD-DEF084708-0001 Checking if the type=value parameter is handled correctly."));
       
  1356 	
       
  1357 	//Check the database is empty before we start
       
  1358 	EmptyDatabase();
       
  1359 	TInt count = CntTest->Db()->CountL();
       
  1360 	TEST_CONDITION(count == 0);
       
  1361 
       
  1362 	// First import the vcard file.
       
  1363 	RFs fsSession;
       
  1364 	RFileReadStream readStream;
       
  1365 	User::LeaveIfError(fsSession.Connect());
       
  1366 	test.Printf(_L("Importing contact "));
       
  1367 	User::LeaveIfError(readStream.Open(fsSession,inputVcard, EFileRead));
       
  1368 	CleanupClosePushL(readStream);
       
  1369 	TBool success=EFalse;
       
  1370 	CArrayPtr<CContactItem>* contactItems=CntTest->Db()->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), readStream, success, CContactDatabase::EImportSingleContact+CContactDatabase::ETTFormat);
       
  1371 	readStream.Close();
       
  1372 	
       
  1373 	//Should be the only contact in the database at this point
       
  1374 	count = CntTest->Db()->CountL();
       
  1375 	TEST_CONDITION(count == 1);
       
  1376 	test.Printf(_L("Imported the contact item"));
       
  1377 
       
  1378 	// Check that the home address and work address has been imported correctly into the card.
       
  1379 	test.Printf(_L("Find whether all the address fields are present."));
       
  1380 	TContactItemId contactId = (*contactItems)[0]->Id();
       
  1381 	CContactItem* importedContact = CntTest->Db()->ReadContactLC(contactId);
       
  1382 	CContactItemFieldSet& fieldSet = importedContact->CardFields();
       
  1383 	
       
  1384 	TEST_CONDITION(FindInFields(fieldSet, _L("W211")));
       
  1385 	TEST_CONDITION(FindInFields(fieldSet, _L("W212")));
       
  1386 	TEST_CONDITION(FindInFields(fieldSet, _L("W213")));
       
  1387 	TEST_CONDITION(FindInFields(fieldSet, _L("W214")));
       
  1388 	TEST_CONDITION(FindInFields(fieldSet, _L("W215")));
       
  1389 
       
  1390 	TEST_CONDITION(FindInFields(fieldSet, _L("H211")));
       
  1391 	TEST_CONDITION(FindInFields(fieldSet, _L("H212")));
       
  1392 	TEST_CONDITION(FindInFields(fieldSet, _L("H213")));
       
  1393 	TEST_CONDITION(FindInFields(fieldSet, _L("H214")));
       
  1394 	TEST_CONDITION(FindInFields(fieldSet, _L("H215")));
       
  1395 		
       
  1396 	// Check that the phone number has been correctly imported	
       
  1397 	TEST_CONDITION(FindInFields(fieldSet, _L("222222222222222222")));
       
  1398 	TEST_CONDITION(FindInFields(fieldSet, _L("333333333333333333")));
       
  1399 
       
  1400 	//cleanup
       
  1401 	CleanupStack::PopAndDestroy(importedContact);
       
  1402 	CleanupStack::PopAndDestroy(&readStream);
       
  1403 	fsSession.Close();
       
  1404 	contactItems->ResetAndDestroy();
       
  1405 	delete contactItems;
       
  1406 }
       
  1407 
       
  1408 /**
       
  1409 @SYMTestCaseID PIM-T-CNTVCARD-DEF081712-0001
       
  1410 @SYMTestType UT
       
  1411 @SYMTestPriority High
       
  1412 @SYMDEF DEF081712
       
  1413 @SYMTestCaseDesc Contact properties on device not deleted during synchronisation
       
  1414 
       
  1415 @SYMTestActions
       
  1416 1) import vcard with 3 emails
       
  1417 2) check that imported contact contains 3 email fields
       
  1418 3) update uid of imported contact to 8
       
  1419 4) import vcard with no email fields and uid 8
       
  1420 5) check that all email fields are deleted
       
  1421 6) check that no other fields have been deleted
       
  1422 
       
  1423 @SYMTestExpectedResults For the above tests:
       
  1424 It should be possible to delete multiple fields of the same type when a vcard is imported
       
  1425 */
       
  1426 
       
  1427 void VCardEmailTestL()
       
  1428 	{
       
  1429 	const TInt importOption =
       
  1430 		CContactDatabase::ELocalTime |
       
  1431 		CContactDatabase::EImportSingleContact | 
       
  1432 		CContactDatabase::ENullTemplateId;
       
  1433 
       
  1434 	test.Next(_L("@SYMTestCaseID:PIM-T-CNTVCARD-DEF081712-0001 VCard Email Test"));
       
  1435 	//start with fresh/clean database
       
  1436 	CntTest->CreateDatabaseL();
       
  1437 	TInt count = CntTest->Db()->CountL();
       
  1438 	TEST_CONDITION(count == 0);
       
  1439 	
       
  1440 	test.Next(_L("Importing initial contact from 1.vcf"));
       
  1441 	TContactItemId contactId = ImportVCardL( KVcard1, importOption );
       
  1442 	TInt initialcount = ContactFilledFields(contactId);
       
  1443 	
       
  1444 	count = CntTest->Db()->CountL();
       
  1445 	TEST_CONDITION(count == 1);
       
  1446 	
       
  1447 	CContactItem *item = CntTest->Db()->OpenContactLX(contactId);
       
  1448 	CleanupStack::PushL( item );
       
  1449 	
       
  1450 	_LIT(guid, "8");
       
  1451 	item->SetUidStringL( const_cast<TDesC &>( guid() ) );
       
  1452 	CntTest->Db()->CommitContactL( *item );
       
  1453 	CleanupStack::PopAndDestroy(item);
       
  1454 	CleanupStack::Pop();//lock
       
  1455 	item = NULL;
       
  1456 	
       
  1457 	test.Next(_L("check number of email fields"));
       
  1458  	TEST_CONDITION(ContactEmailCountL(contactId) == 3);
       
  1459  	
       
  1460  	test.Next(_L("reimport contact from 2.vcf"));
       
  1461 	contactId = ImportVCardL( KVcard2, importOption );
       
  1462 	count = CntTest->Db()->CountL();
       
  1463 	TEST_CONDITION(count == 1);
       
  1464 	
       
  1465 	TInt updatedcount = ContactFilledFields(contactId);
       
  1466 	
       
  1467 	test.Next(_L("check email fields are deleted"));
       
  1468  	TEST_CONDITION(ContactEmailCountL(contactId) == 0);
       
  1469  	
       
  1470  	test.Next(_L("check no other fields are cleared or deleted"));
       
  1471  	TEST_CONDITION( updatedcount == (initialcount - 3) );
       
  1472 	
       
  1473 	}
       
  1474 	
       
  1475 /**
       
  1476 @SYMTestCaseID PIM-T-CNTVCARD-DEF083613-0001
       
  1477 @SYMTestType UT
       
  1478 @SYMTestPriority High
       
  1479 @SYMDEF DEF083613
       
  1480 @SYMTestCaseDesc A VCard with lines terminated by LF(line-feed)s  only is not imported correctly.
       
  1481 
       
  1482 @SYMTestActions
       
  1483 1) import a vcard containing only line-feed line terminators.
       
  1484 2) Check that the import was successful.
       
  1485 
       
  1486 @SYMTestExpectedResults The vcard containing only LF line terminators is imported correctly.
       
  1487 */
       
  1488 
       
  1489 void VCardLineFeedOnlyTestL()
       
  1490 	{
       
  1491 	const TInt importOption =
       
  1492 		CContactDatabase::ELocalTime |
       
  1493 		CContactDatabase::EImportSingleContact | 
       
  1494 		CContactDatabase::ENullTemplateId;
       
  1495 
       
  1496 	TRAPD(err, ImportVCardL( KVCardFileLF, importOption ));
       
  1497 	TEST_CONDITION(err==0);
       
  1498 	}
       
  1499 /**
       
  1500 @SYMTestCaseID PIM-T-CNTVCARD-DEF085873-0001
       
  1501 @SYMTestType UT
       
  1502 @SYMTestPriority High
       
  1503 @SYMDEF DEF085873
       
  1504 @SYMTestCaseDesc Check that use of the CContactVCardConverter::EIgnoreUid flag
       
  1505 behaves correctly.
       
  1506 
       
  1507 @SYMTestActions
       
  1508 1) Empty database.
       
  1509 2) Import a vcard containing a UID without using the flag.
       
  1510 3) Check there is only one contact in the database.
       
  1511 4) Import the same vcard without using the flag.  The import should count as an
       
  1512 update of the existing contact item.
       
  1513 5) Check there is only one contact in the database.
       
  1514 6) Import the same vcard using the flag.  The import should count as an add of
       
  1515 a new item.
       
  1516 7) Check there are now 2 contacts in the database.
       
  1517 
       
  1518 @SYMTestExpectedResults The vcard is added to the database a second time when
       
  1519 the CContactVCardConverter::EIgnoreUid flag is used.
       
  1520 */
       
  1521 void VCardIgnoreUidTestL()
       
  1522 	{
       
  1523 	const TInt importOption1 = CContactDatabase::ELocalTime;
       
  1524 
       
  1525 	const TInt importOption2 =
       
  1526 		CContactDatabase::ELocalTime |
       
  1527 		CContactVCardConverter::EIgnoreUid;
       
  1528 
       
  1529 	test.Next(_L("@SYMTestCaseID:PIM-T-CNTVCARD-DEF085873-0001 Check that use of the CContactVCardConverter::EIgnoreUid flag behaves correctly."));
       
  1530 	
       
  1531 	// 1) Empty database.
       
  1532 	EmptyDatabase();
       
  1533 	TInt count = CntTest->Db()->CountL();
       
  1534 	TEST_CONDITION(count == 0);
       
  1535 
       
  1536 	// 2) Import a vcard containing a UID without using the flag.
       
  1537 	TRAPD(err, ImportVCardL( KVCardFileUID, importOption1 ));	
       
  1538 	TEST_CONDITION(err==0);
       
  1539 		
       
  1540 	// 3) Check there is only one contact in the database.
       
  1541 	count = CntTest->Db()->CountL();
       
  1542 	TEST_CONDITION(count == 1);
       
  1543 	
       
  1544 	// 4) Import the same vcard without using the flag.  The import should count
       
  1545 	// as an update of the existing contact item.
       
  1546 	TRAP(err, ImportVCardL( KVCardFileUID, importOption1 ));	
       
  1547 	TEST_CONDITION(err==0);
       
  1548 
       
  1549 	// 5) Check there is only one contact in the database.
       
  1550 	count = CntTest->Db()->CountL();
       
  1551 	TEST_CONDITION(count == 1);
       
  1552 
       
  1553 	// 6) Import the same vcard using the flag.  The import should count as an
       
  1554 	// add of a new item.
       
  1555 	TRAP(err, ImportVCardL( KVCardFileUID, importOption2 ));	
       
  1556 	TEST_CONDITION(err==0);
       
  1557 
       
  1558 	// 7) Check there are now 2 contacts in the database.
       
  1559 	count = CntTest->Db()->CountL();
       
  1560 	TEST_CONDITION(count == 2);
       
  1561 	} 
       
  1562 /*@SYMTestPriority High
       
  1563 @SYMDEF PDEF097999 
       
  1564 @SYMTestCaseDesc The identity table is not updated if we pass a null value for 
       
  1565 updation (as a result sorting goes wrong)
       
  1566 @SYMTestActions
       
  1567 1) Import a vcard containing firstname and lastname.
       
  1568 2) Import the same vcf file but without  having lastname for one entry
       
  1569 3) Check whether the old lastname is present for that entry.
       
  1570 
       
  1571 @SYMTestExpectedResults The identity table must be updated even if we give a null value for lastname.
       
  1572 so if we search for the old lastname it should not be there .
       
  1573 */
       
  1574 LOCAL_C void TestImportContactsL(const TDesC& aFileName1 , const TDesC& aFileName2 , const TDesC8& aVcfData1 , const TDesC8& aVcfData2)
       
  1575 	{
       
  1576 	test.Next(_L("checking  Updation of Identity table"));
       
  1577 	CContactDatabase *db=CntTest->CreateDatabaseL();	
       
  1578 
       
  1579 	RFs fileSystem;
       
  1580 	User::LeaveIfError( fileSystem.Connect() );
       
  1581 	CleanupClosePushL( fileSystem );
       
  1582 	//Creating input Vcf files
       
  1583 	RFile file;
       
  1584 	file.Replace(fileSystem, aFileName1, EFileWrite+EFileShareAny+EFileStreamText);
       
  1585 	file.Write(aVcfData1);
       
  1586 	file.Close();
       
  1587 	
       
  1588 	file.Replace(fileSystem, aFileName2, EFileWrite+EFileShareAny+EFileStreamText);
       
  1589 	file.Write(aVcfData2);
       
  1590 	file.Close();
       
  1591 		
       
  1592 	RFileReadStream vcard;
       
  1593 	User::LeaveIfError(vcard.Open(fileSystem, aFileName1, EFileRead));
       
  1594 	CleanupClosePushL(vcard);
       
  1595 	TBool success=EFalse;
       
  1596 	CArrayPtr<CContactItem>* contactItems=db->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, CContactDatabase::ETTFormat);
       
  1597 	CleanupStack::PopAndDestroy(&vcard);
       
  1598 	contactItems->ResetAndDestroy();
       
  1599 	delete contactItems;
       
  1600 	
       
  1601 	User::LeaveIfError(vcard.Open(fileSystem, aFileName2, EFileRead));
       
  1602 	CleanupClosePushL(vcard);
       
  1603 	success=EFalse;
       
  1604 	contactItems=db->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, CContactDatabase::ETTFormat);
       
  1605 	CleanupStack::PopAndDestroy(&vcard);
       
  1606 
       
  1607 	//Search for "Bbb" it must not be there
       
  1608     CContactItemFieldDef* fieldDef = new(ELeave) CContactItemFieldDef;
       
  1609 	TFieldType fieldtype = KUidContactFieldFamilyName;
       
  1610 	fieldDef->AppendL(fieldtype);
       
  1611 	CContactIdArray* matchList = db->FindLC(_L("Bbb"), fieldDef);
       
  1612 	TInt matchCount = matchList->Count();
       
  1613 	//Count  zero means the vcard has been updated properly
       
  1614 	TEST_CONDITION(matchCount==0);
       
  1615 	test.Printf	(_L("Sucessfully modified identity table"));
       
  1616 	CleanupStack::PopAndDestroy(matchList);
       
  1617 	contactItems->ResetAndDestroy();
       
  1618 	delete contactItems;
       
  1619 	delete fieldDef;
       
  1620 	CntTest->CloseDatabase();
       
  1621 	CleanupStack::PopAndDestroy(&fileSystem);
       
  1622 	}
       
  1623 
       
  1624  CArrayPtr<CContactItem>* ImportContactsL(const TDesC8& aImportFileInputDescriptor, CContactDatabase* aDatabase)
       
  1625  	{
       
  1626  	CArrayPtr<CContactItem>* retval = NULL;
       
  1627  	
       
  1628  	// read the input contents 
       
  1629  	RDesReadStream vCard(aImportFileInputDescriptor);
       
  1630  	CleanupClosePushL(vCard);
       
  1631  	
       
  1632 	//import
       
  1633  	TBool success=EFalse;
       
  1634 	retval = aDatabase->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vCard, success, CContactDatabase::EDefault);
       
  1635  
       
  1636  	CleanupStack::PopAndDestroy(&vCard);
       
  1637  	return retval;
       
  1638  	}
       
  1639 
       
  1640 void TestStorageType( CContactItem *aContactItem, TStorageType aExpectedStorageType)
       
  1641 	{
       
  1642 	//verify test
       
  1643  	TInt fieldIndex = aContactItem->CardFields().Find(KUidContactFieldVCardMapKEY);
       
  1644  	TStorageType  storageType = aContactItem->CardFields()[fieldIndex].StorageType();
       
  1645  	TEST_CONDITION(storageType == aExpectedStorageType);
       
  1646  	test.Next(_L("ImportExportVCardWithKeyL passed"));
       
  1647 	}
       
  1648 	
       
  1649  /**
       
  1650  @SYMTestCaseID PIM-T-CNTVCARD-DEF097565-0001
       
  1651  @SYMTestType UT
       
  1652  @SYMTestPriority Medium
       
  1653  @SYMDEF DEF097565
       
  1654  @SYMTestCaseDesc Importr the vcf file and then check whether the BInary Key is imported 
       
  1655  properly or not.
       
  1656 
       
  1657  @SYMTestActions
       
  1658  1) Create an empty database.
       
  1659  2) Import a vcard.
       
  1660  3) Check there is Key field imported is having storage type KStorageTypeStore.
       
  1661  
       
  1662  @SYMTestExpectedResults The vcard is containing the Binary Key value.
       
  1663  */	
       
  1664  void ImportExportVCardWithKeyL(const TDesC8& aImportFileInputDescriptor, TStorageType aExpectedStorageType)
       
  1665  	{
       
  1666  	test.Next(_L("@SYMTestCaseID:PIM-T-CNTVCARD-DEF097565-0001 ImportExportVCardWithKeyL Begins"));
       
  1667  
       
  1668  	CntTest->CreateDatabaseL();
       
  1669  	
       
  1670  	//import
       
  1671  	CArrayPtr <CContactItem>* contactItems = NULL;
       
  1672  	contactItems = ImportContactsL(aImportFileInputDescriptor, CntTest->Db());
       
  1673  
       
  1674  	//verify test
       
  1675  	TestStorageType((contactItems->At(0)), aExpectedStorageType);
       
  1676  
       
  1677  	// reclaim memory
       
  1678  	contactItems->ResetAndDestroy() ;
       
  1679  	delete contactItems;
       
  1680  	
       
  1681  	CntTest->CloseDatabase();
       
  1682    	}
       
  1683 /**
       
  1684 @SYMTestCaseID PIM-T-CNTVCARD-INC096705-0001
       
  1685 @SYMTestType UT
       
  1686 @SYMTestPriority Medium
       
  1687 @SYMDEF INC096705
       
  1688 @SYMTestCaseDesc Check that use of the CContactVCardConverter::EReplaceIfExists flag
       
  1689 behaves correctly.
       
  1690 
       
  1691 @SYMTestActions
       
  1692 1) Empty database.
       
  1693 2) Import a vcard containing a company field with EDefault flag.
       
  1694 3) Check of company field is imported
       
  1695 4) Import the same vcard without company field and using CContactVCardConverter::EReplaceIfExists option
       
  1696 
       
  1697 @SYMTestExpectedResults The vcard is replaced with the contact in the database when
       
  1698 the CContactVCardConverter::EReplaceIfExists flag is used.
       
  1699 */
       
  1700 void ImportVCardWithFlagReplaceIfExitstL()
       
  1701 {
       
  1702 	CContactDatabase *db=CntTest->CreateDatabaseL();
       
  1703 	test.Next(_L("@SYMTestCaseID:PIM-T-CNTVCARD-INC096705-0001 hecking  Replacing old contact with new one"));
       
  1704 	const TInt importOption1 = CContactDatabase::EDefault;
       
  1705 	const TInt importOption2 = CContactDatabase::EConverterReserved2;
       
  1706 	
       
  1707 	// 1) Empty database.
       
  1708 	EmptyDatabase();
       
  1709 	TInt count = db->CountL();
       
  1710 	TEST_CONDITION(count == 0);	
       
  1711 
       
  1712 	// 2) Import a vcard containing company filed with CContactDatabase::EDefault flag.
       
  1713 	// and ensure company field gets imported
       
  1714 	test.Next(_L("Importing Contact with Company field"));
       
  1715 	TContactItemId aContactId = ImportVCardL( KVCardWithCompany, importOption1 );		
       
  1716 	CContactItem *item = db->ReadContactLC(aContactId);
       
  1717 	CContactItemFieldSet& fieldSet = item->CardFields();	
       
  1718 	TInt companyIndex = fieldSet.Find( KUidContactFieldCompanyName );
       
  1719 	TEST_CONDITION(companyIndex  != KErrNotFound );	
       
  1720 	const CContactItemField& field = (fieldSet)[companyIndex];
       
  1721 	TEST_CONDITION(field.Storage()->IsFull() != 0);
       
  1722 	CContactTextField* textfield = field.TextStorage();
       
  1723 	_LIT(KExpectedNumber,"XYZ ltd");
       
  1724 	TEST_CONDITION(textfield->Text()==KExpectedNumber);				
       
  1725 	CleanupStack::PopAndDestroy(item);	
       
  1726 	
       
  1727 	//3)Import a vcard without containing company filed with CContactDatabase::EDefault flag.
       
  1728 	// and ensure company field gets merged
       
  1729 	test.Next(_L("Importing Same Contact and merging the old contact without company field"));
       
  1730 	TContactItemId aContactId1 = ImportVCardL( KVCardWithoutCompany, importOption1 );
       
  1731 	
       
  1732 	//4) Ensure that company field should be there
       
  1733 	CContactItem *item1 = db->ReadContactLC(aContactId1);
       
  1734 	CContactItemFieldSet& fieldSet1 = item1->CardFields();
       
  1735 	TInt companyIndex1 = fieldSet1.Find( KUidContactFieldCompanyName );
       
  1736 	TEST_CONDITION(companyIndex1  != KErrNotFound );			
       
  1737 	const CContactItemField& field1 = (fieldSet1)[companyIndex1];
       
  1738 	TEST_CONDITION(field1.Storage()->IsFull() != 0);
       
  1739 	CContactTextField* textfield1 = field1.TextStorage();
       
  1740 	_LIT(KExpectedNumber1,"XYZ ltd");
       
  1741 	TEST_CONDITION(textfield1->Text()==KExpectedNumber1);			
       
  1742 	
       
  1743 	CleanupStack::PopAndDestroy(item1);
       
  1744 	
       
  1745 	//5) Import the same vcard without company field and using CContactVCardConverter::EReplaceIfExists option
       
  1746 	test.Next(_L("Importing Same Contact and replacing with the old contact without company field"));
       
  1747 	TContactItemId aContactId2 = ImportVCardL( KVCardWithoutCompany, importOption2 );	
       
  1748 	
       
  1749 	//6) Ensure that company field should not be there	
       
  1750 	CContactItem *item2 = db->ReadContactLC(aContactId2);
       
  1751 	CContactItemFieldSet& fieldSet2 = item2->CardFields();
       
  1752 	TInt companyIndex2 = fieldSet2.Find( KUidContactFieldCompanyName );		
       
  1753 	const CContactItemField& field2 = (fieldSet2)[companyIndex2];	
       
  1754 	TEST_CONDITION(field2.Storage()->IsFull() == 0);
       
  1755 	CleanupStack::PopAndDestroy(item2);
       
  1756 }
       
  1757 
       
  1758  /**
       
  1759  @SYMTestCaseID PIM-T-CNTVCARD-INC110795-0001
       
  1760  @SYMTestType UT
       
  1761  @SYMTestPriority Medium
       
  1762  @SYMDEF INC110795
       
  1763  @SYMTestCaseDesc Check the use of import option with CContactVCardConverter::EReplaceIfExists flag
       
  1764  behaves correctly with duplicate fields.
       
  1765  
       
  1766  @SYMTestActions
       
  1767  1) Empty database.
       
  1768  2) Import a vcard containing 2 Home Tel fields with EDefault flag.
       
  1769  3) Check of all fields are imported
       
  1770  4) Import the same vcard with 4 modified Home Tel fields  and using CContactVCardConverter::EReplaceIfExists option
       
  1771  
       
  1772  @SYMTestExpectedResults The vcard is replaced with the contact in the database when
       
  1773  the CContactVCardConverter::EReplaceIfExists flag is used.
       
  1774  */
       
  1775  void ImportDuplicateFieldVCardWithReplaceIfExitstL()
       
  1776  {
       
  1777  
       
  1778  	test.Next(_L("@SYMTestCaseID:PIM-T-CNTVCARD-INC110795-0001 checking Replacing existing contact with new one (has duplicate fields) using EReplaceIfExists"));
       
  1779  	
       
  1780  	const TInt importOption1 = CContactDatabase::EDefault;
       
  1781  	const TInt importOption2 = CContactDatabase::EDefault|
       
  1782  								CContactDatabase::EImportSingleContact|
       
  1783  								CContactDatabase::ENullTemplateId |
       
  1784  								CContactVCardConverter::EReplaceIfExists;  
       
  1785  
       
  1786  								
       
  1787  	CContactDatabase *db=CntTest->CreateDatabaseL();
       
  1788  
       
  1789  	// 1) Empty database.
       
  1790  	EmptyDatabase();
       
  1791  	TInt count = db->CountL();
       
  1792  	test(count == 0);
       
  1793  	
       
  1794  	// 2) Import a vcard containing 2 Home Tel fields with CContactDatabase::EDefault flag.
       
  1795  	test.Next(_L("Importing Contact with 2 duplicate Home Tel fields"));
       
  1796  	TContactItemId contactId;
       
  1797  	
       
  1798  	RDesReadStream vcard(KVCardBeforeChange());
       
  1799  	CleanupClosePushL(vcard);
       
  1800  
       
  1801  	CArrayPtr<CContactItem>* contactItems;
       
  1802  	TBool success = EFalse;
       
  1803  	contactItems = db->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, importOption1);
       
  1804  	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,contactItems));
       
  1805  	contactId = (*contactItems)[0]->Id();
       
  1806  	CleanupStack::PopAndDestroy(contactItems);
       
  1807  	CleanupStack::PopAndDestroy(&vcard);
       
  1808  	
       
  1809  	CContactItem *item = db->ReadContactLC(contactId);
       
  1810  	CContactItemFieldSet& fieldSet = item->CardFields();	
       
  1811  
       
  1812  	// Ensure all fields get imported
       
  1813  	test(FindInFields(fieldSet, TPtrC16(KGivenName())));
       
  1814  	test(FindInFields(fieldSet, TPtrC16(KFamilyName())));
       
  1815  	test(FindInFields(fieldSet, TPtrC16(KTelHome1())));
       
  1816  	test(FindInFields(fieldSet, TPtrC16(KTelHome2())));
       
  1817  	test(FindInFields(fieldSet, TPtrC16(KTelCell())));
       
  1818  
       
  1819  	CleanupStack::PopAndDestroy(item);	
       
  1820  
       
  1821  
       
  1822  	//3) Import the same vcard with 4 modified Home tel fields and using 
       
  1823  	//CContactVCardConverter::EReplaceIfExists option
       
  1824  	test.Next(_L("Importing Same Contact and replacing the old contact with 4 modified Home Tel field using EReplaceIfExists"));
       
  1825  
       
  1826  	RDesReadStream vcard2(KVCardAfterChange());
       
  1827  	CleanupClosePushL(vcard2);
       
  1828  	
       
  1829  	success = EFalse;
       
  1830  	contactItems = db->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard2, success, importOption2);
       
  1831  	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,contactItems));
       
  1832  	contactId = (*contactItems)[0]->Id();
       
  1833  	CleanupStack::PopAndDestroy(contactItems);
       
  1834  	CleanupStack::PopAndDestroy(&vcard2);
       
  1835  	
       
  1836  	item = db->ReadContactLC(contactId);
       
  1837  
       
  1838  	CContactItemFieldSet& fieldSet2 = item->CardFields();
       
  1839  
       
  1840  	//4) Ensure that the all 4 Home TEL field have been modified 	
       
  1841  	test(FindInFields(fieldSet2, TPtrC16(KGivenName())));
       
  1842  	test(FindInFields(fieldSet2, TPtrC16(KFamilyName())));
       
  1843  	test(FindInFields(fieldSet2, TPtrC16(KTelHome1Modified())));
       
  1844  	test(FindInFields(fieldSet2, TPtrC16(KTelHome2Modified())));
       
  1845  	test(FindInFields(fieldSet2, TPtrC16(KTelHome3Modified())));
       
  1846  	test(FindInFields(fieldSet2, TPtrC16(KTelHome4Modified())));
       
  1847  	
       
  1848  	CleanupStack::PopAndDestroy(item);
       
  1849  	}
       
  1850    	
       
  1851 /**
       
  1852 Utility function to check if a specific line is present in a file
       
  1853 */	
       
  1854 TBool CheckExportFilesL(const TDesC8& aString, const TDesC16& aFile)
       
  1855 	{
       
  1856 	RFile fileHandle;
       
  1857 	TBool patternFound = EFalse;
       
  1858 	fileHandle.Open(CntTest->Fs(), aFile, EFileRead|EFileStreamText);
       
  1859 	CleanupClosePushL(fileHandle);
       
  1860 	TBuf8<256> line;
       
  1861 	
       
  1862 	while(fileHandle.Read(line) == KErrNone && line.Length() != 0)
       
  1863 		{
       
  1864 		if (line.Find(aString) != KErrNotFound)
       
  1865 			{
       
  1866 		    patternFound = ETrue;
       
  1867 		    break;	
       
  1868 			}	
       
  1869 		}
       
  1870 	
       
  1871 	CleanupStack::PopAndDestroy(&fileHandle);
       
  1872 	return patternFound;
       
  1873 	}
       
  1874 
       
  1875 /**
       
  1876 @SYMTestCaseID PIM-T-CNTVCARD-INC102410-0001
       
  1877 @SYMTestType UT
       
  1878 @SYMTestPriority Critical
       
  1879 @SYMTestCaseDesc Properties having multiple parameters should be imported properly.
       
  1880 
       
  1881 @SYMTestActions
       
  1882 1.Import vCard having property with multiple parameter.
       
  1883 2.Open imported contact and check if parameter values have been added as fields.
       
  1884 3.Export imported contact and check if the property has been exported properly.
       
  1885 
       
  1886 @SYMTestExpectedResults For the above tests:
       
  1887 1.vCard should be imported without losing data.
       
  1888 2.Necessary parameters should be added as fields in contact.
       
  1889 3.Exported file should contain all the parameters which were there while importing.
       
  1890 4.No leaves or panics should occur
       
  1891 */	
       
  1892 void TestMultipleParamsInVCardPropertyL()
       
  1893 	{
       
  1894 	test.Next(_L("@SYMTestCaseID:PIM-T-CNTVCARD-INC102410-0001 Test Import of property having multiple parameters"));
       
  1895 	CntTest->CreateDatabaseL();
       
  1896 	TContactItemId contactId;
       
  1897 	
       
  1898 	RDesReadStream vcard(KMultiParam());
       
  1899 	CleanupClosePushL(vcard);
       
  1900 
       
  1901 	CArrayPtr<CContactItem>* contactItems;
       
  1902 	TBool success = EFalse;
       
  1903 	contactItems = CntTest->Db()->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, CContactDatabase::ETTFormat);
       
  1904 	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,contactItems));
       
  1905 	contactId = (*contactItems)[0]->Id();
       
  1906 	CleanupStack::PopAndDestroy(contactItems);
       
  1907 	CleanupStack::PopAndDestroy(&vcard);
       
  1908 	
       
  1909 	CContactItem* item = CntTest->Db()->ReadContactLC(contactId);
       
  1910 	
       
  1911 	CContactItemFieldSet& fields = item->CardFields();
       
  1912 	TInt fieldPos = fields.FindNext(KUidContactFieldVCardMapTEL, KContactFieldSetSearchAll);
       
  1913 	const CContentType& content = fields[fieldPos].ContentType();
       
  1914 	
       
  1915 	TBool result = 0;
       
  1916 	result = content.ContainsFieldType(KUidContactFieldVCardMapCELL);
       
  1917 	TEST_CONDITION(result > 0);
       
  1918 	
       
  1919 	result = content.ContainsFieldType(KUidContactFieldVCardMapVOICE);
       
  1920 	TEST_CONDITION(result > 0);
       
  1921 	
       
  1922 	result = content.ContainsFieldType(KUidContactFieldVCardMapHOME);
       
  1923 	TEST_CONDITION(result > 0);
       
  1924 	  
       
  1925 	CleanupStack::PopAndDestroy(item);	//destroying contact item
       
  1926 	
       
  1927 	CContactIdArray* idArray = CContactIdArray::NewL();
       
  1928 	CleanupStack::PushL(idArray);
       
  1929 	idArray->AddL(contactId);
       
  1930 
       
  1931 	RFileWriteStream fileStream;
       
  1932 	User::LeaveIfError(fileStream.Replace(CntTest->Fs(), KOutputFileName, EFileWrite));
       
  1933 	CleanupClosePushL(fileStream);
       
  1934 
       
  1935 	CntTest->Db()->ExportSelectedContactsL(TUid::Uid(KUidVCardConvDefaultImpl), *idArray, fileStream,0);
       
  1936 	fileStream.CommitL();
       
  1937 	CleanupStack::PopAndDestroy(&fileStream);
       
  1938 	
       
  1939 	result = CheckExportFilesL(KTelExport(), KOutputFileName());
       
  1940 	TEST_CONDITION(result > 0);
       
  1941 	
       
  1942 	
       
  1943 	CleanupStack::PopAndDestroy();	//destroying id array
       
  1944 	}
       
  1945 	
       
  1946 /**
       
  1947 @SYMTestCaseID PIM-T-CNTVCARD-PDEF103252-0001
       
  1948 @SYMTestType UT
       
  1949 @SYMTestPriority High
       
  1950 @SYMTestCaseDesc Contact item having a large binary field should be exported properly without any leaves.
       
  1951 
       
  1952 @SYMTestActions
       
  1953 1.Import vCard having PHOTO propery with very large property value.
       
  1954 2.Export the imported contacts.
       
  1955 3.Import the exported contacts.
       
  1956 4.Compare the value of the PICTURE field present in the contact items resultin from two Import actions performed.
       
  1957 
       
  1958 @SYMTestExpectedResults For the above tests:
       
  1959 1.vCard should be Exported without losing data or causing any low memory leaves.
       
  1960 2.The Imported PHOTO data should be same in both cases.
       
  1961 .No leaves or panics should occur
       
  1962 */
       
  1963 void TestExportofContactHavingLargeBinaryFieldL()
       
  1964 	{
       
  1965 	test.Next(_L("@SYMTestCaseID:PIM-T-CNTVCARD-PDEF103252-0001 Test Export of contact having large binary field"));
       
  1966 	
       
  1967 	//	Empty the Database
       
  1968 	CntTest->CreateDatabaseL();
       
  1969 	CContactIdArray* idarray = CContactIdArray::NewL();
       
  1970 	CleanupStack::PushL(idarray);
       
  1971 	RFs fsSession;
       
  1972 	User::LeaveIfError(fsSession.Connect());
       
  1973 	RFileReadStream readStream;
       
  1974 
       
  1975  	//Import vcard with large PHOTO property
       
  1976  	test.Printf(_L("Importing contacts from VCard"));
       
  1977 	User::LeaveIfError(readStream.Open(fsSession, KVCardLargePhoto, EFileRead));
       
  1978  	CleanupClosePushL(readStream);
       
  1979  	TBool success = EFalse;
       
  1980  	CArrayPtr<CContactItem>* contactItems = CntTest->Db()->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl),
       
  1981  	 	 readStream, success, CContactDatabase::ETTFormat);
       
  1982  	 
       
  1983 	CleanupStack::PushL( TCleanupItem( CleanUpResetAndDestroy, contactItems ) );
       
  1984 
       
  1985 	TEST_CONDITION(contactItems->Count() == 1) ;
       
  1986 
       
  1987  	//adding imported contact Id's to idarray for export
       
  1988 	idarray->AddL((*contactItems)[0]->Id());
       
  1989 
       
  1990 	CleanupStack::PopAndDestroy(contactItems);
       
  1991  	CleanupStack::PopAndDestroy(&readStream);
       
  1992 
       
  1993 	test.Printf(_L("Exporting the Imported contacts, Testing it for both vCard2.1 and vCard3.0 export."));
       
  1994 	RFileWriteStream writeStream;
       
  1995 	TUid uid;
       
  1996 
       
  1997 	//vCard3.0
       
  1998 	User::LeaveIfError(writeStream.Replace(fsSession,KVCardFile2, EFileWrite));
       
  1999  	CleanupClosePushL(writeStream);
       
  2000 	uid.iUid = KUidPBAPVCardConvImpl;
       
  2001 	TInt64 filter = 0;
       
  2002 	filter |= 0x08;
       
  2003 	//This call should not leave with a Low memory Error
       
  2004 	CntTest->Db()->ExportSelectedContactsL(uid, *idarray, writeStream, CContactDatabase::EDefault, filter, NULL, EPBAPVCard30,ETrue);
       
  2005  	writeStream.CommitL();
       
  2006 	CleanupStack::PopAndDestroy(&writeStream);
       
  2007 	
       
  2008 	//vCard2.1
       
  2009 	User::LeaveIfError(writeStream.Replace(fsSession, KVCardFile3, EFileWrite));
       
  2010  	CleanupClosePushL(writeStream);
       
  2011 	uid.iUid = KUidVCardConvDefaultImpl;
       
  2012 	//This call should not leave with a Low memory Error
       
  2013 	CntTest->Db()->ExportSelectedContactsL(uid, *idarray, writeStream, CContactDatabase::EDefault);
       
  2014  	writeStream.CommitL();
       
  2015 	CleanupStack::PopAndDestroy(&writeStream);
       
  2016 	CleanupStack::PopAndDestroy(idarray);
       
  2017 	
       
  2018 	test.Printf(_L("Export of large binary property completed successfully"));		
       
  2019 
       
  2020 }
       
  2021 
       
  2022 /**
       
  2023 @SYMTestCaseID PIM-T-CNTVCARD-PDEF140328-0001
       
  2024 @SYMTestType UT
       
  2025 @SYMTestPriority High
       
  2026 @SYMTestCaseDesc Partial vCard's should be processed without any panics
       
  2027 
       
  2028 @SYMTestActions
       
  2029 1.Import a partial vCard.
       
  2030 
       
  2031 @SYMTestExpectedResults For the above tests:
       
  2032 1.There should be no panics while import is happening.
       
  2033 */
       
  2034  void TestImportingPartialOrEmptyVCardsL()
       
  2035 {
       
  2036     test.Next(_L("Test import of partial vCard"));
       
  2037     CArrayPtr<CContactItem>* contactItems = NULL;
       
  2038     RDesReadStream vcard(KPartialVCard());
       
  2039     CleanupClosePushL(vcard);
       
  2040     TInt success = EFalse;
       
  2041     CContactDatabase* db = CntTest->CreateDatabaseL();
       
  2042     contactItems = db->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, CContactDatabase::EImportSingleContact);
       
  2043     CleanupStack::PushL( TCleanupItem( CleanUpResetAndDestroy, contactItems ) );
       
  2044     CleanupStack::PopAndDestroy(contactItems);
       
  2045     CleanupStack::PopAndDestroy(&vcard);
       
  2046     test.Printf(_L("Import of partial vCard completed successfully"));
       
  2047 }
       
  2048 
       
  2049 
       
  2050 /**
       
  2051 
       
  2052 @SYMTestCaseID     PIM-T-CNTVCARD-0001
       
  2053 
       
  2054 */
       
  2055 
       
  2056 LOCAL_C void DoTestsL()
       
  2057 	{
       
  2058 	test.Start(_L("@SYMTestCaseID:PIM-T-CNTVCARD-0001 Preparing tests"));
       
  2059 	CTestRegister* TempFiles = CTestRegister::NewLC();
       
  2060 	TempFiles->RegisterL(KVCardFile1);
       
  2061 	TempFiles->RegisterL(KVCardFile2);
       
  2062 	TempFiles->RegisterL(KVCardFile3);
       
  2063 	TempFiles->RegisterL(KVCardUpdate2);
       
  2064 	TempFiles->RegisterL(KDatabaseFileName, EFileTypeCnt);
       
  2065 	TempFiles->RegisterL(KOutputFileName);
       
  2066 	TempFiles->RegisterL(KExpDelAFileName);
       
  2067 	TempFiles->RegisterL(KImpTypeVcardName);
       
  2068 	TempFiles->RegisterL(KImpNoTypeVcardName);	
       
  2069 	TempFiles->RegisterL(KVCardFile5);
       
  2070 	TempFiles->RegisterL(KVCardFile6);
       
  2071 
       
  2072 	//without the fix this test will cause User 21 panic in versit 
       
  2073 	TestImportingPartialOrEmptyVCardsL();
       
  2074 	VCardEmailTestL();
       
  2075 	AccessCountTestsL();			
       
  2076 	DefaultVoiceParamTestsL();
       
  2077 	ExportContactTestL();
       
  2078 	ExportNamelessContactTestL();
       
  2079 #if defined(_DEBUG)
       
  2080 	//this test cannot be run in release mode due to UID mismatch
       
  2081 	//UpdateVCardTestL();
       
  2082 #endif			
       
  2083 	
       
  2084  	ImportLargeVCardTestL() ;			
       
  2085 
       
  2086 	DoTrailingSpaceImportAndExportTestL() ;
       
  2087 	VCardLineFeedOnlyTestL();
       
  2088 	VCardIgnoreUidTestL();
       
  2089 	CheckTypeSupport(KImpTypeVcardName);
       
  2090 	CheckTypeSupport(KImpNoTypeVcardName);	
       
  2091 	TestImportContactsL(KVCardFile5, KVCardFile6, KMasterVcard, KModifierVcard);
       
  2092 	// ImportExportVCardWithBinaryKeyL called with binary KEY value
       
  2093 	ImportExportVCardWithKeyL(KVCardBinaryKeyImportFileDes, KStorageTypeStore);
       
  2094 	// ImportExportVCardWithBinaryKeyL called with text KEY value
       
  2095 	ImportExportVCardWithKeyL(KVCardTextKeyImportFileDes, KStorageTypeText);
       
  2096 	ImportVCardWithFlagReplaceIfExitstL();
       
  2097 	ImportDuplicateFieldVCardWithReplaceIfExitstL();
       
  2098 
       
  2099 	TestMultipleParamsInVCardPropertyL();
       
  2100 	TestExportofContactHavingLargeBinaryFieldL();
       
  2101 
       
  2102 	CntTest->CloseDatabase();
       
  2103 	CleanupStack::PopAndDestroy(TempFiles);
       
  2104 	}
       
  2105 
       
  2106 
       
  2107 LOCAL_C void CleanupFilesL()
       
  2108 	{
       
  2109     // delete the database file
       
  2110 	if (CContactDatabase::ContactDatabaseExistsL(KDatabaseFileName) )
       
  2111 		{
       
  2112 		CContactDatabase::DeleteDatabaseL(KDatabaseFileName);
       
  2113 		}
       
  2114 	}
       
  2115 
       
  2116 
       
  2117 LOCAL_C void TestCondition(TBool aCondition, TInt aLineNumber)
       
  2118 	{
       
  2119 	// if the test is about to fail, cleanup files first
       
  2120 	if (!aCondition)
       
  2121 		{
       
  2122 		TRAP_IGNORE(CleanupFilesL());
       
  2123 		}
       
  2124 	test.operator()(aCondition, aLineNumber);
       
  2125 	}
       
  2126 
       
  2127 
       
  2128 GLDEF_C TInt E32Main()
       
  2129 	{
       
  2130     __UHEAP_MARK; 
       
  2131     CntTest=new(ELeave) CCntTest();
       
  2132 	CntTest->ConstructL(test,KDatabaseFileName);
       
  2133     TRAPD(err,DoTestsL());
       
  2134     // ensure files are cleaned up even if DoTestsL() leaves
       
  2135     TRAP_IGNORE(CleanupFilesL() ); 
       
  2136     CntTest->EndTestLib(err);
       
  2137     __UHEAP_MARKEND;	
       
  2138 	return KErrNone;
       
  2139     }