phonebookengines_old/contactsmodel/tsrc/T_VExportType.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2006-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 // This tests the versit export when the TYPE parameter name is missing.
       
    15 // E.g PHOTO;GIF;BASE64: is present in the vcf file instead of PHOTO;TYPE=GIF;BASE64:
       
    16 // which the vcard specifications states is acceptable
       
    17 // 
       
    18 //
       
    19 
       
    20 #include <e32test.h>
       
    21 #include <e32debug.h>
       
    22 #include <f32file.h>
       
    23 #include <s32file.h>
       
    24 #include "T_UTILS.H"
       
    25 
       
    26 LOCAL_D CCntTest* CntTest=NULL;
       
    27 LOCAL_D const TPtrC KDatabaseFileName=_L("C:VTypeDb.cdb");
       
    28 LOCAL_D RTest test(_L("T_VExportType"));
       
    29 LOCAL_D RFs fsSession;
       
    30 
       
    31 _LIT8(KTypePattern, "TYPE=GIF");
       
    32 
       
    33 
       
    34 _LIT(KVImportFileName, "Z:\\t_cntvcard\\T_VExportTypeWithGif.vcf");
       
    35 _LIT(KVExportFileName, "C:\\vTypeExport.vcf");
       
    36 _LIT(KVReExportedFileName, "C:\\vTypeRe-export.vcf");
       
    37 
       
    38 
       
    39 
       
    40 void DeleteVcardFilesL()
       
    41     {
       
    42     User::LeaveIfError(fsSession.Connect());
       
    43 	CleanupClosePushL(fsSession);
       
    44 	
       
    45 	fsSession.Delete(KVExportFileName);
       
    46 	fsSession.Delete(KVReExportedFileName);
       
    47 	
       
    48 	fsSession.Close();
       
    49     CleanupStack::PopAndDestroy(&fsSession);
       
    50     }
       
    51 
       
    52 void EmptyDatabase()
       
    53 	{
       
    54 	TContactItemId theid;
       
    55 	TContactIter iter(*CntTest->Db());
       
    56 	theid=iter.FirstL();
       
    57 	while(theid!=KNullContactId )
       
    58 		{
       
    59 		CntTest->Db()->DeleteContactL(theid);
       
    60 		theid=iter.NextL();
       
    61 		}
       
    62 	}
       
    63 	
       
    64 void VersitImportContactL(const TDesC& aImportFileName)
       
    65 {
       
    66     RFileReadStream readStream;
       
    67 		
       
    68 	//Import aImportFileName
       
    69 	User::LeaveIfError(readStream.Open(fsSession, aImportFileName, EFileRead));
       
    70 	CleanupClosePushL(readStream);
       
    71 	
       
    72 	TBool success=EFalse;
       
    73 	CArrayPtr<CContactItem>* contactItems=CntTest->Db()->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl),
       
    74 	                                                                     readStream, success,
       
    75 	                                                                     CContactDatabase::EImportSingleContact+CContactDatabase::ETTFormat);
       
    76 	                                                                     
       
    77 	                                                                     
       
    78     //Should be the only contact in the database at this point
       
    79 	TInt count = CntTest->Db()->CountL();
       
    80 	test(count == 1);
       
    81 	
       
    82 	//Store the id of the new contact - we need this later
       
    83 	TContactItemId contactId = (*contactItems)[0]->Id();
       
    84 	CntTest->Db()->SetCurrentItem(contactId);
       
    85 	
       
    86 	readStream.Close();                                                                
       
    87 	CleanupStack::PopAndDestroy(); //readStream.Close()
       
    88     contactItems->ResetAndDestroy();
       
    89     delete contactItems;
       
    90 	
       
    91 }
       
    92 
       
    93 void VersitExportContactL(const TDesC& aExportFileName)
       
    94 {
       
    95     RFileWriteStream writeStream;
       
    96     User::LeaveIfError(writeStream.Replace(fsSession, aExportFileName, EFileWrite));
       
    97     CleanupClosePushL(writeStream);
       
    98     
       
    99     CContactIdArray* idArray = CContactIdArray::NewLC();
       
   100     TInt count = CntTest->Db()->CountL();
       
   101 	test(count == 1);
       
   102     TContactItemId contactId = CntTest->Db()->GetCurrentItem();
       
   103     
       
   104     idArray->InsertL(0, contactId);
       
   105     
       
   106     CntTest->Db()->ExportSelectedContactsL(TUid::Uid(KUidVCardConvDefaultImpl), *idArray, writeStream,0);
       
   107     writeStream.Close();
       
   108     
       
   109 	CleanupStack::PopAndDestroy(2); //idArray, writeStream
       
   110 	                                                                     
       
   111     	
       
   112 }
       
   113 
       
   114 TBool VersitCheckPatternL(const TDesC& aExportFileName, const TDesC8& aExportedPattern)
       
   115 {
       
   116 	RFile fileHandle;
       
   117 	TBool patternFound = EFalse;
       
   118 	fileHandle.Open(fsSession, aExportFileName, EFileRead|EFileStreamText);
       
   119 	CleanupClosePushL(fileHandle);
       
   120 	
       
   121 //	TFileText exportedfile; //Bug in TFileText so not used at the moment
       
   122 //	exportedfile.Set(fileHandle);
       
   123 //	exportedfile.Seek(ESeekStart);
       
   124 	TBuf8<256> line;
       
   125 	
       
   126 	while(fileHandle.Read(line) == KErrNone && line.Length() != 0)
       
   127 	{
       
   128 		if (line.Find(aExportedPattern) != KErrNotFound)
       
   129 		{
       
   130 		    patternFound = ETrue;
       
   131 		    break;	
       
   132 		}	
       
   133 	}
       
   134 	
       
   135 	CleanupStack::PopAndDestroy(&fileHandle);
       
   136 	return patternFound;
       
   137 }
       
   138 
       
   139 
       
   140 
       
   141 void TestImportExportTypeL(const TDesC& aImportFileName,const TDesC& aExportFileName, const TDesC8& aExportedPattern)
       
   142     {
       
   143     VersitImportContactL(aImportFileName);
       
   144     
       
   145     VersitExportContactL(aExportFileName);
       
   146     
       
   147     test(VersitCheckPatternL(aExportFileName, aExportedPattern));
       
   148     }
       
   149 
       
   150 /**
       
   151 
       
   152 @SYMTestCaseID     PIM-T-VEXPORTTYPE-0001
       
   153 
       
   154 */
       
   155 
       
   156 void TestTypeL()
       
   157     {
       
   158 	test.Start(_L("@SYMTESTCaseID:PIM-T-VEXPORTTYPE-0001 Test Versit export of Type line"));
       
   159 
       
   160 	
       
   161 	User::LeaveIfError(fsSession.Connect());
       
   162 	CleanupClosePushL(fsSession);
       
   163 	
       
   164 	CntTest->CreateDatabaseL();
       
   165 	
       
   166 	test.Next(_L("PHOTO;GIF;BASE64:"));
       
   167 
       
   168 	
       
   169 	TestImportExportTypeL(KVImportFileName, KVExportFileName, KTypePattern);
       
   170 	
       
   171 	EmptyDatabase();
       
   172 	
       
   173 	test.Next(_L("PHOTO;TYPE=GIF;BASE64:"));
       
   174 
       
   175 	
       
   176 	TestImportExportTypeL(KVExportFileName, KVReExportedFileName, KTypePattern);
       
   177 	
       
   178 	EmptyDatabase();
       
   179 	
       
   180 	fsSession.Close();
       
   181     CleanupStack::PopAndDestroy(&fsSession);
       
   182 	
       
   183 	CntTest->CloseDatabase();
       
   184 	
       
   185 	CntTest->DeleteDatabaseL();
       
   186     }
       
   187 
       
   188 GLDEF_C TInt E32Main()
       
   189     {
       
   190 	__UHEAP_MARK;
       
   191 	CntTest = new CCntTest;
       
   192 	if (!CntTest)
       
   193 		{
       
   194 		return KErrNoMemory;
       
   195 		}
       
   196 
       
   197 	TRAPD(err, CntTest->ConstructL(test,KDatabaseFileName) );
       
   198 	test(err == KErrNone);
       
   199 		
       
   200 	TRAP(err, TestTypeL() );
       
   201 	test(err == KErrNone);
       
   202 	
       
   203 	TRAP_IGNORE(DeleteVcardFilesL()); //Tidy up the exported files
       
   204 	
       
   205 	CntTest->EndTestLib(err);
       
   206 	
       
   207 	__UHEAP_MARKEND;
       
   208 	return KErrNone;
       
   209     }