pimappservices/calendar/tsrc/T_VCAL3.CPP
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1997-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 // Auto test VCal import functionality
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <s32file.h>
       
    20 #include <txtrich.h>
       
    21 #include <e32math.h>
       
    22 
       
    23 #include <caldataexchange.h>
       
    24 #include <caldataformat.h>
       
    25 #include <calentryview.h>
       
    26 #include <caliterator.h>
       
    27 #include <calentry.h>
       
    28 
       
    29 #include <coreappstest/testserver.h>
       
    30 
       
    31 #include "caltestlib.h"
       
    32 
       
    33 LOCAL_D RTest test(_L("T_VCAL3"));
       
    34 
       
    35 _LIT(KTestCalendarFile, "c:t_vcal3");
       
    36 _LIT(KTestVCalFile, "t_vcal3.vcs");
       
    37 
       
    38 const TInt numNames = 5;
       
    39 const TInt numParams = 2;
       
    40 const TInt numValues = 7;
       
    41 
       
    42 TBuf8<40> names[numNames];
       
    43 TBuf8<40> params[numParams];
       
    44 TBuf8<40> values[numValues];
       
    45 TBuf8<1> charstring;
       
    46 
       
    47 class CTestApp : public CBase
       
    48 	{
       
    49 public:
       
    50 	static CTestApp* NewL();
       
    51 	~CTestApp();
       
    52 	
       
    53 	void RunTestsL();
       
    54 	
       
    55 private:
       
    56 	void ConstructL();
       
    57 
       
    58 	void PrintValue(RFile& outfile, TInt64& seed);
       
    59 	void PrintParam(RFile& outfile, TInt64& seed);
       
    60 	void PrintName(RFile& outfile, TInt64& seed);
       
    61 
       
    62 private:
       
    63 	CCalTestLibrary* iCalTestLib;
       
    64 	CCalDataExchange* iCalDataExchange;
       
    65 
       
    66 	};
       
    67 	
       
    68 CTestApp* CTestApp::NewL()
       
    69 	{
       
    70 	CTestApp* self = new(ELeave) CTestApp;
       
    71 	CleanupStack::PushL(self);
       
    72 	self->ConstructL();
       
    73 	CleanupStack::Pop(self);
       
    74 	return self;
       
    75 	}
       
    76 	
       
    77 void CTestApp::ConstructL()
       
    78 	{
       
    79 	iCalTestLib = CCalTestLibrary::NewL();
       
    80 	iCalTestLib->ReplaceFileL(KTestCalendarFile);
       
    81 	iCalTestLib->OpenFileL(KTestCalendarFile);
       
    82 	iCalDataExchange = CCalDataExchange::NewL(iCalTestLib->GetSession());
       
    83 	}
       
    84 	
       
    85 CTestApp::~CTestApp()
       
    86 	{
       
    87 	TRAP_IGNORE(iCalTestLib->DeleteFileL(KTestCalendarFile, ETrue));
       
    88 	delete iCalDataExchange;
       
    89 	delete iCalTestLib;
       
    90 	}
       
    91 	
       
    92 void CTestApp::PrintValue(RFile& outfile, TInt64& seed)
       
    93 	{
       
    94 	TInt valueIndex = static_cast<TInt>(Math::FRand(seed) * numValues * 1.1);
       
    95 	if(valueIndex < numValues)
       
    96 		{
       
    97 		outfile.Write(values[valueIndex]);
       
    98 		}
       
    99 	else
       
   100 		{
       
   101 		if(static_cast<TInt>(Math::FRand(seed) + 0.5))
       
   102 			{
       
   103 			TInt repeats = static_cast<TInt>(Math::FRand(seed) * 50);
       
   104 			for(TInt p = 0; p < repeats; p++)
       
   105 				{
       
   106 				charstring.Fill(static_cast<TInt>(Math::FRand(seed) * 255), 1);
       
   107 				}
       
   108 			outfile.Write(charstring);
       
   109 			}
       
   110 		}
       
   111 	}
       
   112 
       
   113 void CTestApp::PrintParam(RFile& outfile, TInt64& seed)
       
   114 	{
       
   115 	TInt paramIndex = static_cast<TInt>(Math::FRand(seed) * numParams * 1.1);
       
   116 	if(paramIndex < numParams)
       
   117 		{
       
   118 		outfile.Write(_L8(";"));
       
   119 		outfile.Write(params[paramIndex]);
       
   120 		outfile.Write(_L8("="));
       
   121 		PrintValue(outfile, seed);
       
   122 		}
       
   123 	else
       
   124 		{
       
   125 		TInt repeats = static_cast<TInt>(Math::FRand(seed) * 50);
       
   126 		for(TInt p = 0; p < repeats; p++)
       
   127 			{
       
   128 			charstring.Fill(static_cast<TInt>(Math::FRand(seed) * 255), 1);
       
   129 			}
       
   130 		outfile.Write(charstring);
       
   131 		}
       
   132 	}
       
   133 
       
   134 void CTestApp::PrintName(RFile& outfile, TInt64& seed)
       
   135 	{
       
   136 	TInt nameIndex = static_cast<TInt>(Math::FRand(seed) * numNames * 1.1);
       
   137 	if(nameIndex < numNames)
       
   138 		{
       
   139 		outfile.Write(names[nameIndex]);
       
   140 
       
   141 		//write param
       
   142 		if(static_cast<TInt>(Math::FRand(seed) + 0.4))
       
   143 			PrintParam(outfile, seed);
       
   144 		outfile.Write(_L8(":"));
       
   145 
       
   146 		//write value
       
   147 		if(static_cast<TInt>(Math::FRand(seed) + 0.8))
       
   148 			PrintValue(outfile, seed);
       
   149 
       
   150 		if(static_cast<TInt>(Math::FRand(seed) + 0.8))
       
   151 			outfile.Write(_L8("\r\n"));
       
   152 		}
       
   153 	else
       
   154 		{
       
   155 		TInt repeats = static_cast<TInt>(Math::FRand(seed) * 10);
       
   156 		for(TInt p = 0; p < repeats; p++)
       
   157 			{
       
   158 			charstring.Fill(static_cast<TInt>(Math::FRand(seed) * 255), 1);
       
   159 			}
       
   160 		outfile.Write(charstring);
       
   161 		}				
       
   162 		if(static_cast<TInt>(Math::FRand(seed) + 0.8))
       
   163 			outfile.Write(_L8("\r\n"));
       
   164 	}
       
   165 
       
   166 void CTestApp::RunTestsL()
       
   167 	{	
       
   168 	TTime time;
       
   169 	time.UniversalTime();
       
   170 	TInt64 seed = time.Int64();
       
   171 
       
   172 	names[0] = _L8("BEGIN");
       
   173 	names[1] = _L8("END");
       
   174 	names[2] = _L8("\r\n");
       
   175 	names[3] = _L8("DTSTART:");
       
   176 	names[4] = _L8("DESCRIPTION:");
       
   177 	
       
   178 	params[0] = _L8("ENCODING");
       
   179 	params[1] = _L8("LANGUAGE");
       
   180 
       
   181 	values[0] = _L8("VCALENDAR");
       
   182 	values[1] = _L8("VEVENT");
       
   183 	values[2] = _L8("VTODO");
       
   184 	values[3] = _L8("BASE64");
       
   185 	values[4] = _L8("QUOTED-PRINTABLE");
       
   186 	values[5] = _L8("8-bit");
       
   187 	values[6] = _L8("fr-CA");
       
   188 
       
   189 	for(TInt i = 0; i < 100; i++)
       
   190 		{
       
   191 		iCalTestLib->CleanDatabaseL();
       
   192 		
       
   193 		// Create the random vcal file "vcal.vcs" full of "non-entries"
       
   194 		RFile outfile;
       
   195 		iCalTestLib->TestRegister().CreateTempFileLC(outfile, KTestVCalFile);
       
   196 		test.Printf(_L("Creating random vcal.vcs\r\n"));
       
   197 		
       
   198 		TInt repeats = static_cast<TInt>((Math::FRand(seed) * 10) + 2);
       
   199 		for(TInt j = 0; j < repeats; j++)
       
   200 			{
       
   201 			if(static_cast<TInt>(Math::FRand(seed) + 0.6))
       
   202 				{
       
   203 				outfile.Write(_L8("BEGIN:VCALENDAR"));
       
   204 				outfile.Write(_L8("\r\n"));
       
   205 				}
       
   206   			if(static_cast<TInt>(Math::FRand(seed) + 0.6))
       
   207 				{
       
   208 				outfile.Write(_L8("BEGIN:VEVENT"));
       
   209 				outfile.Write(_L8("\r\n"));
       
   210 				}
       
   211 
       
   212   			TInt entryrepeats = static_cast<TInt>(Math::FRand(seed) * 80);
       
   213 			for(TInt k = 0; k < entryrepeats; k++)
       
   214 				{
       
   215 				PrintName(outfile, seed);
       
   216 				}
       
   217 
       
   218   			if(static_cast<TInt>(Math::FRand(seed) + 0.6))
       
   219 				{
       
   220 				outfile.Write(_L8("END:VEVENT"));
       
   221 				outfile.Write(_L8("\r\n"));
       
   222 				}
       
   223   			if(static_cast<TInt>(Math::FRand(seed) + 0.6))
       
   224 				{
       
   225 				outfile.Write(_L8("END:VCALENDAR"));
       
   226 				outfile.Write(_L8("\r\n"));
       
   227 				}
       
   228 			}
       
   229 
       
   230 		CleanupStack::PopAndDestroy(&outfile);
       
   231 
       
   232 		test.Next(_L("Importing All entries"));
       
   233 
       
   234 
       
   235 		RFile infile;
       
   236 		iCalTestLib->TestRegister().OpenTempFileLC(infile, KTestVCalFile);
       
   237 
       
   238 		RFileReadStream readStream(infile);
       
   239 		CleanupClosePushL(readStream);
       
   240 
       
   241 		// Create array for new entries
       
   242 		RPointerArray<CCalEntry> entryArray;
       
   243 		CleanupResetAndDestroyPushL(entryArray);
       
   244 		
       
   245 		TRAPD(err, iCalDataExchange->ImportL(KUidVCalendar, readStream, entryArray));
       
   246 		
       
   247 		test(err == KErrNotFound || err == KErrNone); // test the import left appropriatly - not found if no matching BEGIN-END
       
   248 		
       
   249 		CleanupStack::PopAndDestroy(&entryArray);
       
   250 		CleanupStack::PopAndDestroy(&readStream);
       
   251 		CleanupStack::PopAndDestroy(&infile);
       
   252 		}
       
   253 		
       
   254 	iCalTestLib->FileSession().Delete(KTestVCalFile);
       
   255 	}
       
   256 	
       
   257 static void DoMainL()
       
   258 	{
       
   259 	CTestApp* testApp = CTestApp::NewL();
       
   260 	CleanupStack::PushL(testApp);
       
   261 	testApp->RunTestsL();
       
   262 	CleanupStack::PopAndDestroy(testApp);
       
   263 	}
       
   264 
       
   265 /**
       
   266 
       
   267 @SYMTestCaseID     PIM-T-VCAL3-0001
       
   268 
       
   269 */
       
   270 
       
   271 TInt E32Main()
       
   272 	{	
       
   273 	__UHEAP_MARK;
       
   274 	test.Start(_L("@SYMTESTCaseID:PIM-T-VCAL3-0001 T_VCAL3"));
       
   275 
       
   276 	test.Title();
       
   277 	CActiveScheduler* scheduler = new(ELeave)CActiveScheduler;
       
   278 	CActiveScheduler::Install(scheduler);
       
   279 	CTrapCleanup* theCleanup = CTrapCleanup::New();
       
   280 	TRAPD(ret,DoMainL());	
       
   281 	test(ret==KErrNone);
       
   282 	delete theCleanup;	
       
   283 	delete scheduler;
       
   284 	test.End();
       
   285 	test.Close();
       
   286 	__UHEAP_MARKEND;
       
   287 	return(KErrNone);
       
   288 	}
       
   289