pimappsupport/vcardandvcal/tsrc/TDefaultCharset.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "TDefaultCharset.h"
       
    17 
       
    18 // System includes
       
    19 #include <versit.h>
       
    20 #include <f32file.h>
       
    21 #include <e32test.h>
       
    22 
       
    23 // Literal constants
       
    24 _LIT(KTestName, "TDefaultCharSet");
       
    25 
       
    26 _LIT(KTestInputFile, "Z:\\TestFiles\\DefaultCharSetInput.vcf");
       
    27 _LIT(KTestOutputFile, "C:\\DefaultCharSetOutput.vcf");
       
    28 _LIT8(KShiftJis, "Shift_JIS");
       
    29 
       
    30 // Globals
       
    31 static RFs					TheFsSession;
       
    32 static RTest				TheTest(KTestName);
       
    33 
       
    34 static void FileCleanup(TAny * aFileMan)
       
    35 	{
       
    36 	CFileMan* FileMan = (CFileMan*)aFileMan;
       
    37 	FileMan->Delete(_L("C:\\DefaultCharSetOutput.vcf"));
       
    38 	}
       
    39 
       
    40 static void DoTestsL()
       
    41 	{
       
    42 	CFileMan* FileMan = CFileMan::NewL(TheFsSession);
       
    43 	CleanupStack::PushL(FileMan);
       
    44 	CleanupStack::PushL(TCleanupItem(FileCleanup, FileMan));
       
    45 	CVersitCardTest* test = CVersitCardTest::NewLC();
       
    46 	test->StartTestsL();
       
    47 	CleanupStack::PopAndDestroy(3, FileMan); // filecleanup, test
       
    48 	}
       
    49 
       
    50 /**
       
    51 @SYMTestCaseID PIM-TDEFAULTCHARSET-0001
       
    52 */
       
    53 GLDEF_C TInt E32Main()
       
    54 	{
       
    55 	
       
    56 	TTime startTime;
       
    57 	startTime.UniversalTime();
       
    58 	
       
    59 	__UHEAP_MARK;
       
    60 	CTrapCleanup* TheCleanup = CTrapCleanup::New();
       
    61 	if	(!TheCleanup)
       
    62 		return KErrNoMemory;
       
    63 
       
    64 	if	(TheFsSession.Connect() < KErrNone)
       
    65 		return KErrGeneral;
       
    66 
       
    67 	TheTest.Start(_L("@SYMTestCaseID PIM-TDEFAULTCHARSET-0001 TDefaultCharSet"));
       
    68 	TRAPD(error, DoTestsL());
       
    69 	TheTest(error == KErrNone);
       
    70 	TheTest.End();
       
    71 	TheTest.Close();
       
    72 	TheFsSession.Close();
       
    73 
       
    74 	delete TheCleanup;
       
    75 	__UHEAP_MARKEND;
       
    76 	
       
    77 	TTime finishTime;
       
    78 	finishTime.UniversalTime();
       
    79 	TReal elapsed = (TReal)finishTime.MicroSecondsFrom(startTime).Int64();
       
    80 	TheTest.Printf(_L("Elapsed time: %.4f\n"), elapsed/1000000);
       
    81 	
       
    82 	return KErrNone;
       
    83 	}
       
    84 
       
    85 //
       
    86 // ------> CVersitCardTest (source)
       
    87 //
       
    88 
       
    89 //********************************************************************************************
       
    90 CVersitCardTest* CVersitCardTest::NewLC()
       
    91 	{
       
    92 	CVersitCardTest* self = new(ELeave) CVersitCardTest;
       
    93 	CleanupStack::PushL(self);
       
    94 	return self;
       
    95 	}
       
    96 
       
    97 //********************************************************************************************
       
    98 void CVersitCardTest::StartTestsL()
       
    99 	{
       
   100 	/*	
       
   101 	This test is created to ensure that the new TVersitParserFlags enumeration 
       
   102 	EUseDefaultCharSetForAllProperties works correctly and it gives the expected behaviour. 
       
   103 	
       
   104 	1.	Create parser and set default language from US-ASCII->SHIFT-JIS
       
   105 	2.	Set EUseDefaultCharSetForAllProperties flag
       
   106 	3.	Internalize a card with default SHIFT-JIS fields with no explicit charsets
       
   107 	4.	Externalise the same contact (should put shift-jis charset explicitly into vcf)
       
   108 	5.	Check to ensure that the exported vcf contains explict charset of SHIFT-JIS
       
   109 		NOTE:: cannot check with internalize due to stripping of charset in AnalysesEncodingCharset()
       
   110 	
       
   111 	*/
       
   112 	
       
   113 	
       
   114 	TheTest.Start(_L("Set up Parser"));
       
   115 	
       
   116 	CParserVCard* cardParser = CParserVCard::NewL();
       
   117 	CleanupStack::PushL(cardParser);
       
   118 		
       
   119 	cardParser->SetDefaultCharSet(Versit::EShiftJISCharSet);					//1
       
   120 	cardParser->SetFlags(CVersitParser::EUseDefaultCharSetForAllProperties);	//2
       
   121 	
       
   122 	TheTest.Next(_L("Start Internalization of Card"));
       
   123 	TRAPD(err, InternalizeCardL(KTestInputFile, *cardParser));					//3
       
   124 	TheTest(err==KErrNone);
       
   125 	
       
   126 	TheTest.Next(_L("Externalize the Card"));
       
   127 	TRAP(err, ExternalizeCardL(KTestOutputFile, *cardParser));					//4
       
   128 	TheTest(err==KErrNone);
       
   129 	
       
   130 	
       
   131 	TheTest.Next(_L("Check that resulting card is correct"));
       
   132 	TRAP(err, TheTest(CheckResultL(KTestOutputFile)));							//5
       
   133 	TheTest(err==KErrNone);
       
   134 	
       
   135 	TheTest.End();	
       
   136 	
       
   137 	CleanupStack::PopAndDestroy(cardParser);	
       
   138 	}
       
   139 //********************************************************************************************
       
   140 
       
   141 void CVersitCardTest::InternalizeCardL(const TDesC& aFile, CParserVCard& aParser) const
       
   142 //
       
   143 //	Internalize the specified file
       
   144 //
       
   145 	{
       
   146 	TInt pos = 0;
       
   147 	RFile file;
       
   148 	User::LeaveIfError(file.Open(TheFsSession, aFile, EFileRead));
       
   149 	CleanupClosePushL(file);
       
   150 
       
   151 	// Read from the file
       
   152 	static_cast<CVersitParser&>(aParser).InternalizeL(file, pos);
       
   153 
       
   154 	CleanupStack::PopAndDestroy(); // file
       
   155 	}
       
   156 //********************************************************************************************
       
   157 
       
   158 void CVersitCardTest::ExternalizeCardL(const TDesC& aFile, CParserVCard& aParser) const
       
   159 //
       
   160 //	Externalize the specified file
       
   161 //
       
   162 	{
       
   163 	RFile file;
       
   164 	User::LeaveIfError(file.Replace(TheFsSession, aFile, EFileWrite));
       
   165 	CleanupClosePushL(file);
       
   166 
       
   167 	// Write from the file
       
   168 	static_cast<CVersitParser&>(aParser).ExternalizeL(file);
       
   169 
       
   170 	CleanupStack::PopAndDestroy(); // file
       
   171 	}
       
   172 //********************************************************************************************
       
   173 
       
   174 TBool CVersitCardTest::CheckResultL(const TDesC& aFile) const
       
   175 	{
       
   176 	TBool result=EFalse;
       
   177 	RFs fs;
       
   178 	User::LeaveIfError(fs.Connect());
       
   179 	CleanupClosePushL(fs);
       
   180 	
       
   181 	RFile myFile;
       
   182 	CleanupClosePushL(myFile);
       
   183 
       
   184 	TInt err = myFile.Open(fs, aFile, EFileRead|EFileShareReadersOnly);
       
   185 	if (err != KErrNone)
       
   186 		User::Leave(err);	
       
   187 		
       
   188 	TBuf8<256> buffer;
       
   189 	myFile.Read(buffer, 256);
       
   190 	
       
   191 	//	check to ensure that shift-jis is explicitly set.
       
   192 	if(KErrNotFound != buffer.Find(KShiftJis))
       
   193 		result=ETrue;
       
   194 	else
       
   195 		result=EFalse;
       
   196 
       
   197 	CleanupStack::PopAndDestroy(2);	//	myFile, fs
       
   198 	return result;
       
   199 	}