pimappsupport/vcardandvcal/tsrc/Observ.CPP
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     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 //
       
    15 
       
    16 #include "Observ.H"
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <vobserv.h>
       
    20 #include <versit.h>
       
    21 #include <vcal.h>
       
    22 #include <vcard.h>
       
    23 
       
    24 //class CAgnRptDef;
       
    25 GLREF_D RTest TheTest;
       
    26 
       
    27 class CTestVersitObserver : public CBase, public MVersitObserver, public MVersitPlugIn
       
    28 	{
       
    29 public:
       
    30 	inline CTestVersitObserver(RFs& aFsSession) : iFs(aFsSession) {}
       
    31 	void Test1L();
       
    32 	void Test2L();
       
    33 	void Test3L();
       
    34 	void TestReccurrenceRulesL();
       
    35 public:
       
    36 //Pure virtual function from MVersitObserver
       
    37 	void VersionSet(CVersitParser* aParser,const TDesC16& aVersion);
       
    38 	void NewParser(CVersitParser* aParser);
       
    39 //Pure virtual function from MVersitPlugIn
       
    40 	TBool AddSpace();
       
    41 	TBool DeleteAllSpaces();
       
    42 	TBool NeedsBlankLine();
       
    43 	void RemoveEscaping(TPtr16& aText);
       
    44 	void AddEscaping(HBufC16*& aText);
       
    45 	TBool WrapLine(RWriteStream& aStream,TInt& aCurrentLineLength,const TPtr8& aText);
       
    46 	TBool EncodingType(Versit::TVersitEncoding& aEncoding,TBool aRequiresEncoding,Versit::TVersitEncoding aDefaultEncoding
       
    47 																						,TUid aPropertyUid,TUint aPropertyCharset);
       
    48 	const TDesC8& EncodingName(Versit::TVersitEncoding aEncoding);
       
    49 private:
       
    50 	enum TCountFlags
       
    51 		{
       
    52 		//Internalising
       
    53 		ECountVersionSet=0x00000001,
       
    54 		ECountNewParser=0x00000020,
       
    55 		ECountAddSpace=0x00000400,
       
    56 		ECountDeleteSpace=0x00008000,
       
    57 		ECountNeedsBlank=0x00100000,
       
    58 		ECountRemoveEscape=0x02000000,
       
    59 		//Externalising
       
    60 		ECountAddEscape=0x00000001,
       
    61 		ECountWrapLine=0x00000100,
       
    62 		ECountEncodeType=0x00010000,
       
    63 		ECountEncodeName=0x01000000,
       
    64 		};
       
    65 private:
       
    66 	RFs& iFs;
       
    67 	CVersitParser* iExpectedParser;
       
    68 	TBuf<16> iExpectedVersion;
       
    69 	TBuf<16> iExpectedFirstValueRE;
       
    70 	TBuf<16> iExpectedFirstValueAE;
       
    71 	TBuf8<16> iExpectedFirstValueWL;
       
    72 	TInt iExpectedFirstLineLength;
       
    73 	TInt iExpectedDefaultEncoding;
       
    74 	TUid iExpectedFirstUid;
       
    75 	TInt iExpectedEncodingEN;
       
    76 	TUint iCount;
       
    77 	};
       
    78 
       
    79 void CTestVersitObserver::VersionSet(CVersitParser* aParser,const TDesC16& aVersion)
       
    80 	{
       
    81 	if (iExpectedParser)
       
    82 		TheTest(iExpectedParser==aParser);
       
    83 	if (iExpectedVersion.Length()>0)
       
    84 		TheTest(iExpectedVersion==aVersion);
       
    85 	iCount+=ECountVersionSet;
       
    86 	}
       
    87 
       
    88 void CTestVersitObserver::NewParser(CVersitParser* aParser)
       
    89 	{
       
    90 	TheTest(iExpectedParser!=aParser);
       
    91 	aParser->SetObserver(this);
       
    92 	aParser->SetPlugIn(this);
       
    93 	iCount+=ECountNewParser;
       
    94 	}
       
    95 
       
    96 TBool CTestVersitObserver::AddSpace()
       
    97 	{
       
    98 	iCount+=ECountAddSpace;
       
    99 	return ETrue;
       
   100 	}
       
   101 
       
   102 TBool CTestVersitObserver::DeleteAllSpaces()
       
   103 	{
       
   104 	iCount+=ECountDeleteSpace;
       
   105 	return ETrue;
       
   106 	}
       
   107 
       
   108 TBool CTestVersitObserver::NeedsBlankLine()
       
   109 	{
       
   110 	iCount+=ECountNeedsBlank;
       
   111 	return ETrue;
       
   112 	}
       
   113 
       
   114 void CTestVersitObserver::RemoveEscaping(TPtr16& aText)
       
   115 	{
       
   116 	if (iExpectedFirstValueRE.Length()>0)
       
   117 		{
       
   118 		TheTest(iExpectedFirstValueRE==aText);
       
   119 		iExpectedFirstValueRE.Zero();
       
   120 		}
       
   121 	iCount+=ECountRemoveEscape;
       
   122 	}
       
   123 
       
   124 void CTestVersitObserver::AddEscaping(HBufC16*& aText)
       
   125 	{
       
   126 	if (iExpectedFirstValueAE.Length()>0)
       
   127 		{
       
   128 		TheTest(iExpectedFirstValueAE==*aText);
       
   129 		iExpectedFirstValueAE.Zero();
       
   130 		}
       
   131 	iCount+=ECountAddEscape;
       
   132 	}
       
   133 
       
   134 TBool CTestVersitObserver::WrapLine(RWriteStream& /*aStream*/,TInt& aCurrentLineLength,const TPtr8& aText)
       
   135 	{
       
   136 	if (iExpectedFirstValueWL.Length()>0)
       
   137 		{
       
   138 		TheTest(iExpectedFirstValueWL==aText);
       
   139 		iExpectedFirstValueWL.Zero();
       
   140 		}
       
   141 	if (iExpectedFirstLineLength>0)
       
   142 		{
       
   143 		TheTest(iExpectedFirstLineLength==aCurrentLineLength);
       
   144 		iExpectedFirstLineLength=0;
       
   145 		}
       
   146 	iCount+=ECountWrapLine;
       
   147 	return EFalse;
       
   148 	}
       
   149 
       
   150 TBool CTestVersitObserver::EncodingType(Versit::TVersitEncoding& /*aEncoding*/,TBool /*aRequiresEncoding*/
       
   151 												,Versit::TVersitEncoding aDefaultEncoding,TUid aPropertyUid,TUint /*aPropertyCharset*/)
       
   152 	{
       
   153 	TheTest(iExpectedDefaultEncoding==aDefaultEncoding);
       
   154 	if (iExpectedFirstUid.iUid!=0)
       
   155 		{
       
   156 		TheTest(iExpectedFirstUid==aPropertyUid);
       
   157 		iExpectedFirstUid.iUid=0;
       
   158 		}
       
   159 	iCount+=ECountEncodeType;
       
   160 	return EFalse;
       
   161 	}
       
   162 
       
   163 const TDesC8& CTestVersitObserver::EncodingName(Versit::TVersitEncoding aEncoding)
       
   164 	{
       
   165 	TheTest(iExpectedEncodingEN==aEncoding);
       
   166 	iCount+=ECountEncodeName;
       
   167 	return KNullDesC8;
       
   168 	}
       
   169 
       
   170 void CTestVersitObserver::Test1L()
       
   171 	{
       
   172 	RFile file;
       
   173 	_LIT(KFileI,"Z:\\TestFiles\\IVCal.VCS");
       
   174 	_LIT(KFileP,"C:\\TestFiles\\PVCal.VCS");
       
   175 	_LIT(KVersion,"1.0");
       
   176 	_LIT8(KVersion8,"1.0");
       
   177 	_LIT(KFirstValue,"VCALENDAR");
       
   178 	CParserVCal* parser=CParserVCal::NewL();
       
   179 	CleanupStack::PushL(parser);
       
   180 	parser->SetObserver(this);
       
   181 	parser->SetPlugIn(this);
       
   182 	TInt start=0;
       
   183 	iCount=0;
       
   184 	iExpectedParser=parser;
       
   185 	iExpectedVersion=KVersion;
       
   186 	iExpectedFirstValueRE=KFirstValue;
       
   187 	User::LeaveIfError(file.Open(iFs, KFileI, EFileRead));
       
   188 	CleanupClosePushL(file);
       
   189 	parser->CVersitParser::InternalizeL(file,start);
       
   190 	TheTest(iCount==ECountVersionSet+2*ECountNewParser+19*ECountRemoveEscape);
       
   191 	CleanupStack::Pop(&file);
       
   192 	file.Close();
       
   193 	iCount=0;
       
   194 	iExpectedDefaultEncoding=Versit::ENoEncoding;
       
   195 	iExpectedFirstValueAE=KVersion;
       
   196 	iExpectedFirstValueWL=KVersion8;
       
   197 	iExpectedFirstLineLength=8;
       
   198 	iExpectedFirstUid.iUid=KVersitPropertyHBufCUid;
       
   199 	User::LeaveIfError(file.Replace(iFs, KFileP, EFileWrite));
       
   200 	CleanupClosePushL(file);
       
   201 	parser->CVersitParser::ExternalizeL(file);
       
   202 	TheTest(iCount==2*ECountAddEscape+5*ECountWrapLine+13*ECountEncodeType);
       
   203 	CleanupStack::PopAndDestroy(2,parser);
       
   204 	}
       
   205 
       
   206 void CTestVersitObserver::Test2L()
       
   207 	{
       
   208 	RFile file;
       
   209 	_LIT(KFileI,"Z:\\TestFiles\\vCard.VCF");
       
   210 	_LIT(KFileP,"C:\\TestFiles\\PvCard.VCF");
       
   211 	_LIT(KVersion,"2.1");
       
   212 	_LIT8(KVersion8,"2.1");
       
   213 	_LIT(KFirstValue,"VCARD");
       
   214 	CParserVCard* parser=CParserVCard::NewL();
       
   215 	CleanupStack::PushL(parser);
       
   216 	parser->SetObserver(this);
       
   217 	parser->SetPlugIn(this);
       
   218 	TInt start=0;
       
   219 	iCount=0;
       
   220 	iExpectedParser=parser;
       
   221 	iExpectedVersion=KVersion;
       
   222 	iExpectedFirstValueRE=KFirstValue;
       
   223 	User::LeaveIfError(file.Open(iFs, KFileI, EFileRead));
       
   224 	CleanupClosePushL(file);
       
   225 	parser->CVersitParser::InternalizeL(file,start);
       
   226 	TheTest(iCount==ECountVersionSet+ECountAddSpace+5*ECountDeleteSpace+8*ECountRemoveEscape);
       
   227 	CleanupStack::Pop(&file);
       
   228 	file.Close();
       
   229 	iCount=0;
       
   230 	iExpectedDefaultEncoding=Versit::ENoEncoding;
       
   231 	iExpectedFirstValueAE=KVersion;
       
   232 	iExpectedFirstValueWL=KVersion8;
       
   233 	iExpectedFirstLineLength=8;
       
   234 	iExpectedFirstUid.iUid=KVersitPropertyHBufCUid;
       
   235 	User::LeaveIfError(file.Replace(iFs, KFileP, EFileWrite));
       
   236 	CleanupClosePushL(file);
       
   237 	parser->CVersitParser::ExternalizeL(file);
       
   238 	TheTest(iCount==5*ECountAddEscape+5*ECountWrapLine+6*ECountEncodeType);
       
   239 	CleanupStack::PopAndDestroy(2,parser);
       
   240 	}
       
   241 
       
   242 void CTestVersitObserver::Test3L()
       
   243 	{
       
   244 	RFile file;
       
   245 	_LIT(KFileI,"Z:\\TestFiles\\Base64.VCF");
       
   246 	_LIT(KFileP,"C:\\TestFiles\\PBase64.VCF");
       
   247 	_LIT(KFileP1,"C:\\TestFiles\\PBase64a.VCF");
       
   248 	_LIT(KVersion,"2.1");
       
   249 	_LIT8(KVersion8,"2.1");
       
   250 	_LIT(KFirstValue,"VCARD");
       
   251 	CParserVCard* parser=CParserVCard::NewL();
       
   252 	CleanupStack::PushL(parser);
       
   253 	parser->SetObserver(this);
       
   254 	parser->SetPlugIn(this);
       
   255 	TInt start=0;
       
   256 	iCount=0;
       
   257 	iExpectedParser=parser;
       
   258 	iExpectedVersion=KVersion;
       
   259 	iExpectedFirstValueRE=KFirstValue;
       
   260 	User::LeaveIfError(file.Open(iFs, KFileI, EFileRead));
       
   261 	CleanupClosePushL(file);
       
   262 	parser->CVersitParser::InternalizeL(file,start);
       
   263 	TheTest(iCount==ECountVersionSet+ECountNeedsBlank+5*ECountRemoveEscape);
       
   264 	CleanupStack::Pop(&file);
       
   265 	file.Close();
       
   266 	iCount=0;
       
   267 	iExpectedDefaultEncoding=Versit::ENoEncoding;
       
   268 	iExpectedFirstValueAE=KVersion;
       
   269 	iExpectedFirstValueWL=KVersion8;
       
   270 	iExpectedFirstLineLength=8;
       
   271 	iExpectedFirstUid.iUid=KVersitPropertyHBufCUid;
       
   272 	iExpectedEncodingEN=Versit::EQuotedPrintableEncoding;
       
   273 	User::LeaveIfError(file.Replace(iFs, KFileP, EFileWrite));
       
   274 	CleanupClosePushL(file);
       
   275 	parser->CVersitParser::ExternalizeL(file);
       
   276 	TheTest(iCount==3*ECountAddEscape+2*ECountWrapLine+3*ECountEncodeType+ECountEncodeName);
       
   277 	CleanupStack::Pop(&file);
       
   278 	file.Close();
       
   279 	iCount=0;
       
   280 	iExpectedDefaultEncoding=Versit::EBase64Encoding;
       
   281 	iExpectedFirstValueAE=KVersion;
       
   282 	iExpectedEncodingEN=Versit::EBase64Encoding;
       
   283 	User::LeaveIfError(file.Replace(iFs, KFileP1, EFileWrite));
       
   284 	CleanupClosePushL(file);
       
   285 	parser->SetDefaultEncoding(Versit::EBase64Encoding);
       
   286 	parser->CVersitParser::ExternalizeL(file);
       
   287 	TheTest(iCount==3*ECountAddEscape+3*ECountEncodeType+3*ECountEncodeName);
       
   288 	CleanupStack::PopAndDestroy(2,parser);
       
   289 	}
       
   290 
       
   291 //test for Reccurrence rule in VCal (VCS)
       
   292 void CTestVersitObserver::TestReccurrenceRulesL()
       
   293 	{
       
   294 	RFile file;
       
   295 	_LIT(KFileI,"Z:\\TestFiles\\IRDailyVCal.VCS");
       
   296 	CParserVCal* parser=CParserVCal::NewL();
       
   297 	CleanupStack::PushL(parser);
       
   298 	User::LeaveIfError(file.Open(iFs, KFileI, EFileRead));
       
   299 	CleanupClosePushL(file);
       
   300 	TInt start=0;
       
   301 	parser->CVersitParser::InternalizeL(file,start);
       
   302 	CleanupStack::Pop(&file);
       
   303 	file.Close();
       
   304 	CArrayPtr<CVersitParser>* entities= parser->ArrayOfEntities(EFalse);
       
   305 	CArrayPtr<CParserProperty>* properties = (*entities)[0]->PropertyL(KVersitTokenRRULE, TUid::Uid(KVCalPropertyRecurrenceUid), EFalse); // don't take ownership
       
   306 	CleanupStack::PushL(properties);
       
   307 	CParserPropertyValueRecurrence* propertyrecurval = (CParserPropertyValueRecurrence*)((*properties)[0]->Value());	
       
   308 	TheTest(2 == (propertyrecurval->Value())->iDuration);
       
   309 	CleanupStack::PopAndDestroy(properties);
       
   310 	CleanupStack::PopAndDestroy(parser);
       
   311 	}
       
   312 
       
   313 GLDEF_C void TestPlugInL(RFs& aFsSession)
       
   314 	{
       
   315 	_LIT(KTest,"PlugIn Test");
       
   316 	
       
   317 	TheTest.Start(KTest);
       
   318 	CTestVersitObserver* test=new(ELeave) CTestVersitObserver(aFsSession);
       
   319 	CleanupStack::PushL(test);
       
   320 	_LIT(KTest1,"Simple VCal Test");
       
   321 	
       
   322 	TheTest.Next(KTest1);
       
   323 	test->Test1L();
       
   324 	_LIT(KTest2,"VCard Test");
       
   325 	
       
   326 	TheTest.Next(KTest2);
       
   327 	test->Test2L();
       
   328 	_LIT(KTest3,"Base64 Test");
       
   329 	
       
   330 	TheTest.Next(KTest3);
       
   331 	test->Test3L();
       
   332 	//Testing reccurrence rules of VCal (.vcs)
       
   333 	_LIT(KTestRRule,"Testing Reccurrence Rules...");
       
   334 	
       
   335 	TheTest.Next(KTestRRule);
       
   336 	test->TestReccurrenceRulesL();
       
   337 	CleanupStack::PopAndDestroy(test);
       
   338 	TheTest.End();
       
   339 	}