pimappsupport/vcardandvcal/tsrc/tvgen.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1999-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 // Versit tests
       
    15 // 
       
    16 //
       
    17 
       
    18 // System includes
       
    19 #include <bautils.h>
       
    20 #include <e32test.h>
       
    21 #include <s32file.h>
       
    22 #include <e32math.h>
       
    23 #include <versit.h>
       
    24 #include <vprop.h>
       
    25 #include <vuid.h>
       
    26 #include <vcard.h>
       
    27 #include <vcal.h>
       
    28 
       
    29 // User includes
       
    30 #include "thelpers.h"
       
    31 
       
    32 //
       
    33 // STRUCTS
       
    34 //
       
    35 struct TPropertyDef
       
    36 	{
       
    37 	HBufC8*				iPropertyName;
       
    38 	TInt				iNumberOfValuesExpected;		// only used by array items
       
    39 	TUint32				iType;
       
    40 	};
       
    41 
       
    42 
       
    43 struct TPropertyParameterDef
       
    44 	{
       
    45 	HBufC8*				iParameterName;
       
    46 	HBufC8*				iParameterValue;
       
    47 	};
       
    48 
       
    49 
       
    50 //
       
    51 // GLOBAL DATA
       
    52 //
       
    53 LOCAL_D RTest									gTest(_L("TVERSIT2"));
       
    54 LOCAL_D CTrapCleanup*							gCleanup;
       
    55 LOCAL_D RFs										gFsSession;
       
    56 
       
    57 LOCAL_D CArrayFixFlat<TPropertyParameterDef>*	gParameterDefinitions;
       
    58 LOCAL_D CArrayFixFlat<TPropertyDef>*			gPropertyDefinitions;
       
    59 
       
    60 LOCAL_D CArrayFixFlat<TDateTime>*				gDateTimeArray;
       
    61 LOCAL_D CDesCArrayFlat*							gTextArray;
       
    62 
       
    63 LOCAL_D TInt64									gSeed;
       
    64 
       
    65 
       
    66 //
       
    67 // vCard files & directories needed for this test code
       
    68 //
       
    69 
       
    70 // Source vCards that are randomly generated by this code - they are written in this directory
       
    71 // for reference (this is the output exactly as it was generated)
       
    72 _LIT(KDirRandomlyGeneratedOut,			"C:\\vCards\\RandomlyGenerated\\Original\\");
       
    73 
       
    74 // This is the the vCard ouput that has been read in from the directory above and then
       
    75 // written back out to file without any modifications. They should theoretically be identical
       
    76 // (except any CHARSET parameters are removed, as this means that the original and newly created
       
    77 // vCard may potentially be encoded in different ways making comparison difficult).
       
    78 _LIT(KDirRandomlyGeneratedIn,			"C:\\vCards\\RandomlyGenerated\\ReadInAndThenWrittenOut\\");
       
    79 
       
    80 // This is the name of the vCard generated to be unique in the directory.
       
    81 _LIT(KNameGeneratedVCardName,			"vCard_for_test_%d");
       
    82 _LIT(KNameGeneratedVCardExtension,		".vcf");
       
    83 _LIT(KNameDeleteFileSpec,				"*.vcf");
       
    84 
       
    85 // This is the name of the log file generated for errors 
       
    86 _LIT(KNameErrorLogfile,"c:\\tvgen.log");
       
    87 
       
    88 //
       
    89 // Other contants
       
    90 //
       
    91 _LIT(KFailureDescription,		"Failed on %S tag\n");
       
    92 _LIT(KTestIndicator,			"Test [%d] about to commence\n");
       
    93 _LIT(KTestIndicatorSeparator,	"=================================\n\n");
       
    94 const TInt KMinNumberOfTestsToPerform = 100;
       
    95 const TInt KMaxNumberOfTestsToPerform = 100;
       
    96 
       
    97 
       
    98 //****************************************************************************************
       
    99 LOCAL_C void CreateArraysL()
       
   100 	{
       
   101 	gParameterDefinitions	= new(ELeave) CArrayFixFlat<TPropertyParameterDef>(10);
       
   102 	gPropertyDefinitions	= new(ELeave) CArrayFixFlat<TPropertyDef>(10);
       
   103 	gTextArray				= new(ELeave) CDesCArrayFlat(10);
       
   104 	gDateTimeArray			= new(ELeave) CArrayFixFlat<TDateTime>(10);
       
   105 	}
       
   106 
       
   107 
       
   108 //****************************************************************************************
       
   109 LOCAL_C void DestroyArraysL()
       
   110 	{
       
   111 	TInt count = gParameterDefinitions->Count();
       
   112 	for(TInt i=0; i<count; i++)
       
   113 		{
       
   114 		TPropertyParameterDef& def = gParameterDefinitions->At(i);
       
   115 		delete def.iParameterName;	def.iParameterName = NULL;
       
   116 		delete def.iParameterValue;	def.iParameterValue= NULL;
       
   117 		}
       
   118 
       
   119 	count = gPropertyDefinitions->Count();
       
   120 	for(TInt j=0; j<count; j++)
       
   121 		{
       
   122 		TPropertyDef& def = gPropertyDefinitions->At(j);
       
   123 		delete def.iPropertyName;	def.iPropertyName = NULL;
       
   124 		}
       
   125 
       
   126 	delete gPropertyDefinitions;
       
   127 	delete gParameterDefinitions;
       
   128 	delete gTextArray;
       
   129 	delete gDateTimeArray;
       
   130 	}
       
   131 
       
   132 
       
   133 //****************************************************************************************
       
   134 LOCAL_C void CreatePropertyParamL(const TDesC8& aParamName, const TDesC8& aParamValue)
       
   135 	{
       
   136 	TInt amountToPop = 1;
       
   137 	TPropertyParameterDef def;
       
   138 	def.iParameterName			= aParamName.AllocL();
       
   139 	CleanupStack::PushL(def.iParameterName);
       
   140 
       
   141 	if	(aParamValue.Length())
       
   142 		{
       
   143 		def.iParameterValue		= aParamValue.AllocL();
       
   144 		CleanupStack::PushL(def.iParameterValue);
       
   145 		amountToPop = 2;
       
   146 		}
       
   147 	else
       
   148 		def.iParameterValue = NULL;
       
   149 
       
   150 	gParameterDefinitions->AppendL(def);
       
   151 	
       
   152 	CleanupStack::Pop(amountToPop);
       
   153 	}
       
   154 
       
   155 
       
   156 //****************************************************************************************
       
   157 LOCAL_C void CreatePropertyDefinitionL(const TDesC8& aName, TUint32 aExpectedType, TInt aValueCount)
       
   158 	{
       
   159 	TPropertyDef def;
       
   160 	def.iNumberOfValuesExpected	= aValueCount;
       
   161 	def.iType					= aExpectedType;
       
   162 	def.iPropertyName			= aName.AllocL();
       
   163 	CleanupStack::PushL(def.iPropertyName);
       
   164 	gPropertyDefinitions->AppendL(def);
       
   165 	CleanupStack::Pop();
       
   166 	}
       
   167 
       
   168 
       
   169 //****************************************************************************************
       
   170 LOCAL_C TBool CheckWhetherPropertyAlreadyExistsL(CVersitParser* aParser, const TDesC8& aPropertyName)
       
   171 	{
       
   172 	CArrayPtr<CParserProperty>* propertyArray = aParser->ArrayOfProperties(EFalse); // don't take ownership
       
   173 	if	(!propertyArray)
       
   174 		return EFalse;
       
   175 	
       
   176 	const TInt count = propertyArray->Count();
       
   177 	for(TInt i=0; i<count; i++)
       
   178 		{
       
   179 		if	(propertyArray->At(i)->Name() == aPropertyName)
       
   180 			return ETrue;
       
   181 		}
       
   182 	return EFalse;
       
   183 	}
       
   184 
       
   185 
       
   186 //****************************************************************************************
       
   187 LOCAL_C void CreatePropertyValuesL()
       
   188 	{
       
   189 	// Simple text items
       
   190 	gTextArray->AppendL(_L("symbian.foundation@exp.example.test"));
       
   191 	gTextArray->AppendL(_L("me@here.com"));
       
   192 	gTextArray->AppendL(_L("this@there.com"));
       
   193 	gTextArray->AppendL(_L("1 Example Road Example Town ZZ99 EXP"));
       
   194 	gTextArray->AppendL(_L("Somewhere Road Example Town ZZ99 EXP"));
       
   195 	gTextArray->AppendL(_L("wibble"));
       
   196 	gTextArray->AppendL(_L("@@me@@here@@.com"));
       
   197 	gTextArray->AppendL(_L("01632 575 24211 10"));
       
   198 	gTextArray->AppendL(_L("5757847=====d98==y"));
       
   199 	gTextArray->AppendL(_L("@@me@@here@@.com"));
       
   200 	gTextArray->AppendL(_L("01632 575==24211"));
       
   201 	gTextArray->AppendL(_L("557895789æ?øö!¦µØ"));
       
   202 	gTextArray->AppendL(_L("item1"));
       
   203 	gTextArray->AppendL(_L("item2"));
       
   204 	gTextArray->AppendL(_L("item3"));
       
   205 	gTextArray->AppendL(_L("item4"));
       
   206 	gTextArray->AppendL(_L("item5"));
       
   207 	gTextArray->AppendL(_L("Some house"));
       
   208 	gTextArray->AppendL(_L("called some name"));
       
   209 	gTextArray->AppendL(_L("at some address"));
       
   210 	gTextArray->AppendL(_L("in this place"));
       
   211 	gTextArray->AppendL(_L("UK"));
       
   212 	gTextArray->AppendL(_L("testing multi"));
       
   213 	gTextArray->AppendL(_L("lines of text"));
       
   214 	gTextArray->AppendL(_L("addresses"));
       
   215 	gTextArray->AppendL(_L("with post;code"));
       
   216 	gTextArray->AppendL(_L("Firstname"));
       
   217 	gTextArray->AppendL(_L("Lastname"));
       
   218 	gTextArray->AppendL(_L("namefirst"));
       
   219 	gTextArray->AppendL(_L("namelast"));
       
   220 	gTextArray->AppendL(_L("246256265"));
       
   221 	gTextArray->AppendL(_L("/3/3/3/3"));
       
   222 	gTextArray->AppendL(_L("+=+=+==="));
       
   223 	gTextArray->AppendL(_L("+1+1=244=]1/2234"));
       
   224 
       
   225 	// Date items
       
   226 	TDateTime time(1999, EOctober, 15, 12, 53, 0, 0);
       
   227 	gDateTimeArray->AppendL(time);
       
   228 	time.SetYear(2000); time.SetMonth(EJanuary); time.SetDay(2);
       
   229 	gDateTimeArray->AppendL(time);
       
   230 	time.SetYear(1); time.SetMonth(EAugust); time.SetDay(10);
       
   231 	gDateTimeArray->AppendL(time);
       
   232 	time.SetYear(9999); time.SetMonth(EDecember); time.SetDay(20);
       
   233 	gDateTimeArray->AppendL(time);
       
   234 
       
   235 	// Date Time
       
   236 	TDateTime time2(1999, EOctober, 15, 12, 53, 0, 0);
       
   237 	gDateTimeArray->AppendL(time2);
       
   238 	time2.SetYear(2000); time2.SetHour(18); time2.SetMonth(EJanuary);
       
   239 	gDateTimeArray->AppendL(time2);
       
   240 	time2.SetYear(1); time2.SetHour(0); time2.SetMonth(EJanuary); time2.SetMinute(1);
       
   241 	gDateTimeArray->AppendL(time2);
       
   242 	time2.SetYear(9999); time2.SetHour(23); time2.SetMinute(59); time2.SetSecond(59);
       
   243 	gDateTimeArray->AppendL(time2);
       
   244 	}
       
   245 
       
   246 
       
   247 //****************************************************************************************
       
   248 LOCAL_C void CreatePropertyParametersL()
       
   249 	{
       
   250 	// Misc params
       
   251 	CreatePropertyParamL(_L8("WORK"), _L8(""));
       
   252 	CreatePropertyParamL(_L8("HOME"), _L8(""));
       
   253 	CreatePropertyParamL(_L8("INTERNET"), _L8(""));
       
   254 	CreatePropertyParamL(_L8("DEFAULT"), _L8(""));
       
   255 	CreatePropertyParamL(_L8("FAX"), _L8(""));
       
   256 	CreatePropertyParamL(_L8("CELL"), _L8(""));
       
   257 	CreatePropertyParamL(_L8("VOICE"), _L8(""));
       
   258 	CreatePropertyParamL(_L8("MOBILE"), _L8(""));
       
   259 	CreatePropertyParamL(_L8("PARCEL"), _L8(""));
       
   260 	CreatePropertyParamL(_L8("AVI"), _L8(""));
       
   261 	CreatePropertyParamL(_L8("MPEG"), _L8(""));
       
   262 	CreatePropertyParamL(_L8("GIF"), _L8(""));
       
   263 
       
   264 	/*
       
   265 	CreatePropertyParamL(_L("ENCODING"), _L("8-BIT"));
       
   266 	CreatePropertyParamL(_L("ENCODING"), _L("BASE64"));
       
   267 	CreatePropertyParamL(_L("ENCODING"), _L("QUOTED-PRINTABLE"));
       
   268 	CreatePropertyParamL(_L("CHARSET"), _L("UTF-8"));
       
   269 	CreatePropertyParamL(_L("CHARSET"), _L("UTF-7"));
       
   270 	CreatePropertyParamL(_L("CHARSET"), _L("ISO-8859-1"));
       
   271 	CreatePropertyParamL(_L("CHARSET"), _L("ISO-8859-2"));
       
   272 	CreatePropertyParamL(_L("CHARSET"), _L("ISO-8859-5"));
       
   273 	CreatePropertyParamL(_L("CHARSET"), _L("ISO-8859-9"));
       
   274 	*/
       
   275 
       
   276 	CreatePropertyParamL(_L8("CHARSET"), _L8("ASCII"));
       
   277 	CreatePropertyParamL(_L8("LANGUAGE"), _L8("en"));
       
   278 	CreatePropertyParamL(_L8("LANGUAGE"), _L8("fr-CA"));
       
   279 
       
   280 	// Epoc extended labels
       
   281 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Misc"));
       
   282 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Wibble"));
       
   283 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Wobble"));
       
   284 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("This"));
       
   285 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("That"));
       
   286 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Work address"));
       
   287 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Home address"));
       
   288 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Moble phone"));
       
   289 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Epoc"));
       
   290 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Symbian"));
       
   291 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Psion"));
       
   292 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("Palm"));
       
   293 	CreatePropertyParamL(_L8("X-EPOCCNTMODELFIELDLABEL"), _L8("App Engines"));
       
   294 	}
       
   295 
       
   296 
       
   297 //****************************************************************************************
       
   298 LOCAL_C void CreatePropertyDefinitionsL()
       
   299 	{	
       
   300 	CreatePropertyDefinitionL(_L8("FN"),			KVersitPropertyHBufCUid,		1);
       
   301 	CreatePropertyDefinitionL(_L8("N"),			KVersitPropertyCDesCArrayUid,	2);
       
   302 	CreatePropertyDefinitionL(_L8("ADR"),		KVersitPropertyCDesCArrayUid,	5);
       
   303 	CreatePropertyDefinitionL(_L8("TITLE"),		KVersitPropertyHBufCUid,		1);
       
   304 	CreatePropertyDefinitionL(_L8("TEL"),		KVersitPropertyHBufCUid,		1);
       
   305 	CreatePropertyDefinitionL(_L8("EMAIL"),		KVersitPropertyHBufCUid,		1);
       
   306 	CreatePropertyDefinitionL(_L8("ROLE"),		KVersitPropertyHBufCUid,		1);
       
   307 	CreatePropertyDefinitionL(_L8("LABEL"),		KVersitPropertyHBufCUid,		1);
       
   308 	CreatePropertyDefinitionL(_L8("URL"),		KVersitPropertyHBufCUid,		1);
       
   309 	CreatePropertyDefinitionL(_L8("ORG"),		KVersitPropertyCDesCArrayUid,	1);
       
   310 	CreatePropertyDefinitionL(_L8("BDAY"),		KVersitPropertyDateUid,			1);
       
   311 	CreatePropertyDefinitionL(_L8("NOTE"),		KVersitPropertyHBufCUid,		1);
       
   312 	CreatePropertyDefinitionL(_L8("VERSION"),	KVersitPropertyHBufCUid,		1);
       
   313 	CreatePropertyDefinitionL(_L8("REV"),		KVersitPropertyDateTimeUid,		1);
       
   314 	}
       
   315 
       
   316 
       
   317 //****************************************************************************************
       
   318 LOCAL_C void CreateUserDefinedPropertyDefinitionsL()
       
   319 	{	
       
   320 	CreatePropertyDefinitionL(_L8("X-WVID"),			KVersitPropertyHBufCUid,		1);
       
   321 	CreatePropertyDefinitionL(_L8("X-MYPROP"),			KVersitPropertyHBufCUid,		1);	
       
   322 	}
       
   323 
       
   324 //****************************************************************************************
       
   325 LOCAL_C CParserPropertyValue* GetRandomPropertyValueL(TUint32 aType, TInt aNumberOfValues /*for array parameters*/)
       
   326 	{
       
   327 	TInt randomIndex = KErrNotFound;
       
   328 	switch (aType)
       
   329 		{
       
   330 		case KVersitPropertyHBufCUid:
       
   331 			{
       
   332 			randomIndex = VTestHelpers::MakeRandomNumber(0, gTextArray->Count()-1, gSeed);
       
   333 			return VTestHelpers::CreateTextPropertyValueL((*gTextArray)[randomIndex]);
       
   334 			}
       
   335 
       
   336 		case KVersitPropertyDateTimeUid:
       
   337 			{
       
   338 			randomIndex = VTestHelpers::MakeRandomNumber(0, gDateTimeArray->Count()-1, gSeed);
       
   339 			return VTestHelpers::CreateDateTimePropertyValueL((*gDateTimeArray)[randomIndex]);
       
   340 			}
       
   341 
       
   342 		case KVersitPropertyDateUid:
       
   343 			{
       
   344 			randomIndex = VTestHelpers::MakeRandomNumber(0, gDateTimeArray->Count()-1, gSeed);
       
   345 			return VTestHelpers::CreateDatePropertyValueL((*gDateTimeArray)[randomIndex]);
       
   346 			}
       
   347 
       
   348 		case KVersitPropertyCDesCArrayUid:
       
   349 			{
       
   350 			CDesCArray* array = NULL;
       
   351 			if (aNumberOfValues>0)
       
   352 				array = new(ELeave) CDesCArrayFlat(5);
       
   353 			TInt length=0;
       
   354 			TBool addSpace;
       
   355 			for(TInt i=0; i<aNumberOfValues; i++)
       
   356 				{
       
   357 				randomIndex = VTestHelpers::MakeRandomNumber(0, gTextArray->Count()-1, gSeed);
       
   358 				length+=(*gTextArray)[randomIndex].Length();
       
   359 				addSpace=(length>15);
       
   360 				if (addSpace)
       
   361 					length=0;
       
   362 				VTestHelpers::CreateDescriptorArrayL((*gTextArray)[randomIndex], array, addSpace);
       
   363 				}
       
   364 			CParserPropertyValueCDesCArray* propertyValue = VTestHelpers::CreateArrayPropertyValueL(array);
       
   365 			return propertyValue;
       
   366 			}
       
   367 
       
   368 		default:
       
   369 			User::Panic(_L("TVGen"), 0);
       
   370 			return NULL; // never reached
       
   371 		}
       
   372 	}
       
   373 
       
   374 
       
   375 //****************************************************************************************
       
   376 LOCAL_C CVersitParser* CreateVCardObjectLC()
       
   377 	{
       
   378 	// Crete vCard object
       
   379 
       
   380 	CVersitParser* parser = CParserVCard::NewL();
       
   381 	CleanupStack::PushL(parser);
       
   382 
       
   383 	const TInt KRandomNumberOfProperties = VTestHelpers::MakeRandomNumber(4, gPropertyDefinitions->Count()-1, gSeed);
       
   384 	const TInt maxLineLength=KMaxExternalizedTokenLength/3+1;
       
   385 	const TInt maxLineLength2=KMaxExternalizedTokenLength/2;
       
   386 	TInt lineLength=0;		//To stop a warning
       
   387 
       
   388 	for(TInt i=0; i<KRandomNumberOfProperties; i++)
       
   389 		{
       
   390 		// First of all pick a random property name from the property name array...
       
   391 		TInt randomNameIndex = -1;
       
   392 		TBool propertyIsValid = EFalse;
       
   393 		for(TInt j=0; j<gPropertyDefinitions->Count(); j++)
       
   394 			{
       
   395 			randomNameIndex = VTestHelpers::MakeRandomNumber(0, gPropertyDefinitions->Count()-1, gSeed); 
       
   396 			TPtrC8 name(*gPropertyDefinitions->At(randomNameIndex).iPropertyName);
       
   397 			if	(!CheckWhetherPropertyAlreadyExistsL(parser, name))
       
   398 				{
       
   399 				propertyIsValid = ETrue;
       
   400 				lineLength=name.Length()+1;
       
   401 				break;
       
   402 				}
       
   403 			}
       
   404 
       
   405 		if	(!propertyIsValid)
       
   406 			return parser; // no more unique properties to be added
       
   407 
       
   408 		// We've now established that "name" is unique in the current list of properties, so the 
       
   409 		// next step is to populate this property with random parameter data...
       
   410 		TInt randomNumberOfParametersToAdd = VTestHelpers::MakeRandomNumber(0, gParameterDefinitions->Count()-1, gSeed);
       
   411 		CArrayPtr<CParserParam>* arrayOfParams = NULL;
       
   412 		if	(randomNumberOfParametersToAdd)
       
   413 			{
       
   414 			arrayOfParams = new(ELeave) CArrayPtrFlat<CParserParam>(randomNumberOfParametersToAdd);
       
   415 			CleanupStack::PushL(arrayOfParams);
       
   416 			}
       
   417 		TInt oldLineLength;
       
   418 		for(TInt k=0; k<randomNumberOfParametersToAdd && lineLength<maxLineLength; k++)
       
   419 			{
       
   420 			// Get a random property
       
   421 			TPropertyParameterDef def = gParameterDefinitions->At(VTestHelpers::MakeRandomNumber(0, gParameterDefinitions->Count()-1, gSeed));
       
   422 			
       
   423 			// Check whether this property has already been added
       
   424 			if	(VTestHelpers::DoesParameterAlreadyExist(arrayOfParams, *def.iParameterName))
       
   425 				continue; // get another param as this one is already been added to the array
       
   426 
       
   427 			//CParserParam* param = CParserParam::NewL(*def.iParameterName,def.iParameterValue);
       
   428 			TPtr8 value(NULL,0);
       
   429 			oldLineLength=lineLength;
       
   430 			lineLength+=def.iParameterName->Length()+1;
       
   431 			if (def.iParameterValue)
       
   432 				{
       
   433 				value.Set(def.iParameterValue->Des());
       
   434 				lineLength+=def.iParameterValue->Length()+1;
       
   435 				}
       
   436 			if (lineLength>maxLineLength2)
       
   437 				{
       
   438 				lineLength=oldLineLength;
       
   439 				continue;
       
   440 				}
       
   441 			CParserParam* param = CParserParam::NewL(*def.iParameterName,value);
       
   442 			CleanupStack::PushL(param);
       
   443 			arrayOfParams->AppendL(param);
       
   444 			CleanupStack::Pop();
       
   445 			}
       
   446 
       
   447 
       
   448 		// Now generate a suitable property name & value pair for this property
       
   449 		TPropertyDef def			= gPropertyDefinitions->At(randomNameIndex);
       
   450 		CParserPropertyValue* value	= GetRandomPropertyValueL(def.iType, def.iNumberOfValuesExpected);
       
   451 		CParserProperty* property	= CParserProperty::NewL(*value, *def.iPropertyName, arrayOfParams);
       
   452 		parser->AddPropertyL(property); // will push on the cleanupstack and then pop off if no problem... odd, but thats the way it was written!
       
   453 
       
   454 		if	(randomNumberOfParametersToAdd)
       
   455 			CleanupStack::Pop(); // arrayOfParams
       
   456 		}
       
   457 
       
   458 	return parser;
       
   459 	}
       
   460 
       
   461 
       
   462 void DoWriteVCardToFileL(CVersitParser* aParserObject, const TDesC& aFileName)
       
   463 	{
       
   464 	RFile outputFile;
       
   465 	User::LeaveIfError(outputFile.Replace(gFsSession, aFileName, EFileShareExclusive | EFileStream | EFileWrite));
       
   466 	aParserObject->ExternalizeL(outputFile);
       
   467 	outputFile.Close();
       
   468 	}
       
   469 
       
   470 void WriteVCardToFile(CVersitParser* aParserObject, TInt aTestNumber, TDes& aFileName)
       
   471 	{
       
   472 	const TUint KAppendChar = '_';
       
   473 
       
   474 	// Generate the first bit of a unique filename
       
   475 	TInt repetitions = 0;
       
   476 	TFileName out;
       
   477 	do
       
   478 		{
       
   479 		aFileName.Format(KNameGeneratedVCardName, aTestNumber);
       
   480 		aFileName.AppendFill(KAppendChar, repetitions++);
       
   481 		aFileName += KNameGeneratedVCardExtension;
       
   482 		out += KDirRandomlyGeneratedOut;
       
   483 		out += aFileName;
       
   484 		} while(BaflUtils::FileExists(gFsSession, out));
       
   485 
       
   486 	DoWriteVCardToFileL(aParserObject, out);
       
   487 
       
   488 	// At this point we need to remove all ocurrances of the CHARSET tag as this messes
       
   489 	// up the comparison when the text is written to file and then streamed in again later.
       
   490 
       
   491 	CArrayPtr<CParserProperty>* properties = aParserObject->ArrayOfProperties(EFalse);
       
   492 	TInt propCount = properties->Count();
       
   493 	for(TInt i=0; i<propCount; i++)
       
   494 		{
       
   495 		STATIC_CAST(CParserPropertyWithoutCharsetTags*, properties->At(i))->RemoveAllCharsetTagsFromParametersL();
       
   496 		}
       
   497 
       
   498 	DoWriteVCardToFileL(aParserObject, _L("C:\\Test.vcf"));
       
   499 	}
       
   500 
       
   501 
       
   502 CVersitParser* ReadVCardFromFileLC(const TDesC& aFileName)
       
   503 	{
       
   504 	// Read in the written out file and then write it out straight away so that a comparison
       
   505 	// may be made manually if required.
       
   506 	// Then read in the 
       
   507 	TFileName file(KDirRandomlyGeneratedOut);
       
   508 	file += aFileName;
       
   509 
       
   510 	TInt bytesThroughFile = 0;
       
   511 	RFile inputFile;
       
   512 	User::LeaveIfError(inputFile.Open(gFsSession, file, EFileShareReadersOnly | EFileStream | EFileRead));
       
   513 
       
   514 	CVersitParser* readParser = CParserVCard::NewL();
       
   515 	CleanupStack::PushL(readParser);
       
   516 	readParser->InternalizeL(inputFile, bytesThroughFile);
       
   517 	inputFile.Close();
       
   518 
       
   519 	file  = KDirRandomlyGeneratedIn;
       
   520 	file += aFileName;
       
   521 	DoWriteVCardToFileL(readParser, file);
       
   522 
       
   523 	return readParser;
       
   524 	}
       
   525 
       
   526 
       
   527 TBool CreateVCardL(TInt aTestNumber, TFileText& aLogFile)
       
   528 	{
       
   529 	// Create a fully populated vCard.
       
   530 	CVersitParser* parser = CreateVCardObjectLC();
       
   531 
       
   532 	// Write the vCard to a file whilst obtaining the filename of the file written to
       
   533 	TFileName file;
       
   534 	WriteVCardToFile(parser, aTestNumber, file);
       
   535 
       
   536 	// Read the vCard from a file
       
   537 	CVersitParser* readParser = ReadVCardFromFileLC(file);
       
   538 
       
   539 	// Get a list of properties read from the file and from the artifically created vCard
       
   540 	CArrayPtr<CParserProperty>*	readProperties	= readParser->ArrayOfProperties(EFalse);
       
   541 	CArrayPtr<CParserProperty>*	properties		= parser->ArrayOfProperties(EFalse);
       
   542 
       
   543 	// Test that the property count is the same
       
   544 	TInt count = readProperties->Count();
       
   545 	gTest(count == properties->Count());
       
   546 
       
   547 	// Compare the properties one by one
       
   548 	TBool passedTest = ETrue;
       
   549 	for(TInt i=0; i<count; i++)
       
   550 		{
       
   551 		CComparitorParserProperty* property = STATIC_CAST(CComparitorParserProperty*, properties->At(i));
       
   552 		CComparitorParserProperty* readProperty = STATIC_CAST(CComparitorParserProperty*, readProperties->At(i));
       
   553 		if (!property->IsEqualL(readProperty))
       
   554 			{
       
   555 			passedTest=EFalse;
       
   556 			TPtrC8 propertyName = property->Name();
       
   557 			gTest.Printf(KFailureDescription, &propertyName);
       
   558 			// write to log file
       
   559 			TBuf<128> error;
       
   560 			error.Format(_L("Test %d failed on property %S\r"),aTestNumber,&propertyName);
       
   561 			aLogFile.Write(error);
       
   562 			}
       
   563 		}
       
   564 	
       
   565 	if (passedTest)
       
   566 		{
       
   567 		TBuf<32> success;
       
   568 		success.Format(_L("Test %d passed\r"),aTestNumber);
       
   569 		aLogFile.Write(success);
       
   570 		}
       
   571 	
       
   572 	CleanupStack::PopAndDestroy(2); // parser, readParser
       
   573 	return passedTest;
       
   574 	}
       
   575 
       
   576 
       
   577 void MakeDirectoriesL()
       
   578 	{
       
   579 	gFsSession.MkDirAll(KDirRandomlyGeneratedIn);
       
   580 	gFsSession.MkDirAll(KDirRandomlyGeneratedOut);
       
   581 	}
       
   582 
       
   583 
       
   584 void DeleteAllPreviousTestsL()
       
   585 	{
       
   586 	TFileName fileSpecToDelete;
       
   587 	
       
   588 	CFileMan* fileMan = CFileMan::NewL(gFsSession);
       
   589 	CleanupStack::PushL(fileMan);
       
   590 
       
   591 	// Delete all the vCards in the Original directory...
       
   592 	fileSpecToDelete  = KDirRandomlyGeneratedOut;
       
   593 	fileSpecToDelete += KNameDeleteFileSpec;
       
   594 	fileMan->Delete(fileSpecToDelete);
       
   595 
       
   596 	// Delete all the vCards in the ReadInWrittenOut directory...
       
   597 	fileSpecToDelete  = KDirRandomlyGeneratedIn;
       
   598 	fileSpecToDelete += KNameDeleteFileSpec;
       
   599 	fileMan->Delete(fileSpecToDelete);
       
   600 
       
   601 	CleanupStack::PopAndDestroy(); // fileMan
       
   602 	}
       
   603 
       
   604 void FileCleanup(TAny * aFileMan)
       
   605 	{
       
   606 	gFsSession.Delete(_L("C:\\Test.vcf"));
       
   607 	CFileMan* FileMan = (CFileMan*)aFileMan;
       
   608 	FileMan->RmDir(_L("C:\\vCards\\"));
       
   609 	}
       
   610 
       
   611 void PrepareForTestsL()
       
   612 	{
       
   613 	MakeDirectoriesL();
       
   614 	DeleteAllPreviousTestsL();
       
   615 	}
       
   616 
       
   617 void doMainL()
       
   618 	{
       
   619 	CFileMan* FileMan = CFileMan::NewL(gFsSession);
       
   620 	CleanupStack::PushL(FileMan);
       
   621 	CleanupStack::PushL(TCleanupItem(FileCleanup, FileMan));
       
   622 
       
   623 	TTime time;
       
   624 	time.UniversalTime();
       
   625 	gSeed = time.Int64();
       
   626 #if defined(__WINS__)
       
   627 	//gSeed=MAKE_TINT64(0x00e07096,0xb1f742e0);
       
   628 	TInt64 sSeed=gSeed;
       
   629 #endif
       
   630 
       
   631 	RFile file;
       
   632 	TInt err = file.Replace(gFsSession, KNameErrorLogfile(), EFileWrite+EFileShareAny+EFileStreamText);
       
   633 	User::LeaveIfError(err);
       
   634 	CleanupClosePushL(file);
       
   635 	TFileText logFile;
       
   636 	logFile.Set(file);
       
   637 
       
   638 	/*TBuf<1> u;
       
   639 	u.Append(0xfffe);
       
   640 	logFile.Write(u);*/
       
   641 
       
   642 	CreateArraysL();
       
   643 	CreatePropertyValuesL();
       
   644 	CreatePropertyParametersL();
       
   645 	CreatePropertyDefinitionsL();
       
   646 
       
   647 	const TInt KNumberOfTests = VTestHelpers::MakeRandomNumber(KMinNumberOfTestsToPerform, KMaxNumberOfTestsToPerform, gSeed);
       
   648 	TInt firstFail=0;
       
   649 	for(TInt ii=1; ii<KNumberOfTests; ++ii)
       
   650 		{
       
   651 		gTest.Console()->ClearScreen();
       
   652 		gTest.Printf(KTestIndicator,ii);
       
   653 		gTest.Printf(KTestIndicatorSeparator);
       
   654 		if (!CreateVCardL(ii,logFile) && firstFail==0)
       
   655 			firstFail=ii;
       
   656 		}
       
   657 	if (firstFail>0)
       
   658 		{
       
   659 		_LIT(KFail,"First test to fail = %d\n");
       
   660 		gTest.Printf(KFail,firstFail);
       
   661 		}
       
   662 
       
   663 	DestroyArraysL();
       
   664 	
       
   665 	PrepareForTestsL();
       
   666 	_LIT(KFail,"\r\n Starting Test for creating user properties\n");
       
   667 	gTest.Printf(KFail);
       
   668 
       
   669 	CreateArraysL();
       
   670 	CreatePropertyValuesL();
       
   671 	CreatePropertyParametersL();
       
   672 	CreateUserDefinedPropertyDefinitionsL();
       
   673 
       
   674 	const TInt KNumberOfTests1 = VTestHelpers::MakeRandomNumber(KMinNumberOfTestsToPerform, KMaxNumberOfTestsToPerform, gSeed);
       
   675 	firstFail=0;
       
   676 	for(TInt jj=1; jj<KNumberOfTests1; ++jj)
       
   677 		{
       
   678 		gTest.Console()->ClearScreen();
       
   679 		gTest.Printf(KTestIndicator,jj);
       
   680 		gTest.Printf(KTestIndicatorSeparator);
       
   681 		if (!CreateVCardL(jj,logFile) && firstFail==0)
       
   682 			firstFail=jj;
       
   683 		}
       
   684 	if (firstFail>0)
       
   685 		{
       
   686 		_LIT(KFail,"First test to fail = %d\n");
       
   687 		gTest.Printf(KFail,firstFail);
       
   688 		}
       
   689 
       
   690 	DestroyArraysL();
       
   691 	
       
   692 	CleanupStack::Pop(); //file.Close()
       
   693 	CleanupStack::PopAndDestroy(2, FileMan);
       
   694 	}
       
   695 
       
   696 /**
       
   697 @SYMTestCaseID PIM-TVGEN-0001
       
   698 */	
       
   699 GLDEF_C TInt E32Main()
       
   700 	{	
       
   701 	
       
   702 	TTime startTime;
       
   703 	startTime.UniversalTime();
       
   704 	
       
   705 	__UHEAP_MARK;
       
   706 	gTest.Start(_L("@SYMTestCaseID PIM-TVGEN-0001 TVGEN"));
       
   707 	gTest.Title();
       
   708 	gCleanup = CTrapCleanup::New();
       
   709 	gFsSession.Connect();
       
   710 
       
   711 	TInt error = KErrNone;
       
   712 
       
   713 	TRAP(error, PrepareForTestsL());	
       
   714 	TRAP(error, doMainL());	
       
   715 	gTest(error==KErrNone);
       
   716 	delete gCleanup;	
       
   717 
       
   718 	gFsSession.Close();
       
   719 	gTest.End();
       
   720 	gTest.Close();
       
   721 	__UHEAP_MARKEND;
       
   722 	
       
   723 	TTime finishTime;
       
   724 	finishTime.UniversalTime();
       
   725 	TReal elapsed = (TReal)finishTime.MicroSecondsFrom(startTime).Int64();
       
   726 	gTest.Printf(_L("Elapsed time: %.4f\n"), elapsed/1000000);
       
   727 	
       
   728 	return(KErrNone);
       
   729 	}
       
   730