pimappservices/calendar/tsrc/tcal_instance.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2005-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 "tcal_instance_testdata.h"
       
    17 #include "caltestlib.h"
       
    18 
       
    19 #include <calentry.h>
       
    20 #include <calentryview.h>
       
    21 #include <calinstance.h>
       
    22 #include <calinstanceview.h>
       
    23 #include <caliterator.h>
       
    24 #include <calprogresscallback.h>
       
    25 #include <calrrule.h>
       
    26 #include <calsession.h>
       
    27 #include <e32test.h>
       
    28 #include <tz.h>
       
    29 
       
    30 #include <calinstanceiterator.h>
       
    31 
       
    32 RTest test(_L("tcal_instance"));
       
    33 
       
    34 _LIT(KFormatDate, "%D%M%Y%/0%1%/1%2%/2%3%/3");
       
    35 _LIT(KFormatDateTime, "%D%M%Y%/0%1%/1%2%/2%3%/3 %H:%T:%S.%C");
       
    36 _LIT(KCalendarFile, "tcal_instance");
       
    37 
       
    38 class CInstanceTestManager : public CBase
       
    39 	{
       
    40 public:
       
    41 	enum TTestAPI
       
    42 		{
       
    43 		EFindInstance,
       
    44 		EIteratorNextInstance,
       
    45 		ETotalTestAPI
       
    46 		};
       
    47 public:
       
    48 	static CInstanceTestManager* NewLC();
       
    49 	~CInstanceTestManager();
       
    50 	
       
    51 	inline void CleanDatabaseL();
       
    52 	void TestPopulateAndCheckL(TInt aTestAPI);
       
    53 	void TestEditAndCheckL(TInt aTestAPI);	
       
    54 
       
    55 
       
    56 private:
       
    57 
       
    58 	// avoid any method but NewL instancing the class
       
    59 	CInstanceTestManager() {	}
       
    60 	// no copy constructor and assignment operator
       
    61 	CInstanceTestManager(CInstanceTestManager& );
       
    62 	CInstanceTestManager& operator = (const CInstanceTestManager& );
       
    63 
       
    64 	void ConstructL();
       
    65 
       
    66 	void ExtractFromDatabaseL(CTestDataStruct& aTestData, TInt aTestAPI);
       
    67 	void EditEntriesL();
       
    68 
       
    69 
       
    70 private: // member data
       
    71 	CCalTestLibrary*  iTestLibrary;
       
    72 	
       
    73 	CCalEntryView*    iEntryView;	// not owned
       
    74 	CCalInstanceView* iInstanceView;// not owned
       
    75 	};
       
    76 
       
    77 
       
    78 CInstanceTestManager* CInstanceTestManager::NewLC()
       
    79 	{
       
    80 	CInstanceTestManager* self = new (ELeave) CInstanceTestManager();
       
    81 	
       
    82 	CleanupStack::PushL(self);
       
    83 	self->ConstructL();
       
    84 	
       
    85 	return (self);
       
    86 	}
       
    87 	
       
    88 
       
    89 CInstanceTestManager::~CInstanceTestManager()
       
    90 	{
       
    91 	TRAP_IGNORE(iTestLibrary->DeleteFileL(KCalendarFile));
       
    92 	delete iTestLibrary;
       
    93 	}
       
    94 
       
    95 
       
    96 void CInstanceTestManager::ConstructL()
       
    97 	{
       
    98 	iTestLibrary = CCalTestLibrary::NewL();
       
    99 
       
   100 	iTestLibrary->ReplaceFileL(KCalendarFile());
       
   101 	iTestLibrary->OpenFileL(KCalendarFile());
       
   102 
       
   103 	iInstanceView = &iTestLibrary->SynCGetInstanceViewL();
       
   104 	iEntryView = &iTestLibrary->SynCGetEntryViewL();
       
   105 	}
       
   106 	
       
   107 	
       
   108 inline void CInstanceTestManager::CleanDatabaseL()
       
   109 	{
       
   110 	iTestLibrary->CleanDatabaseL();		
       
   111 	}
       
   112 
       
   113 
       
   114 void CInstanceTestManager::TestPopulateAndCheckL(TInt aTestAPI)
       
   115 	{
       
   116 	TInt testStart = 0; 
       
   117 	TInt testEnd = CTestData::ENumberOfTests;
       
   118 
       
   119 
       
   120 	for(TInt i = testStart; i < testEnd; ++i)
       
   121 		{
       
   122 		CTestDataStruct* testData = CTestData::GenerateTestDataLC((CTestData::TTestNumber)i, iInstanceView, iEntryView);
       
   123 
       
   124 		ExtractFromDatabaseL(*testData, aTestAPI);
       
   125 
       
   126 		CleanupStack::PopAndDestroy(testData);
       
   127 
       
   128 		CleanDatabaseL();
       
   129 		}		
       
   130 	}
       
   131 
       
   132 
       
   133 void CInstanceTestManager::TestEditAndCheckL(TInt aTestAPI)
       
   134 	{
       
   135 	TInt testStart = 0; 
       
   136 	TInt testEnd = CTestData::ENumberOfTests;
       
   137 	
       
   138 	
       
   139 	for(TInt j = testStart; j < testEnd; ++j)
       
   140 		{
       
   141 		CTestDataStruct* testData = CTestData::GenerateTestDataLC((CTestData::TTestNumber)j, iInstanceView, iEntryView);
       
   142 		EditEntriesL();
       
   143 		ExtractFromDatabaseL(*testData, aTestAPI);
       
   144 
       
   145 		CleanupStack::PopAndDestroy(testData);
       
   146 
       
   147 		CleanDatabaseL();
       
   148 		}			
       
   149 	}
       
   150 
       
   151 
       
   152 // Edit entries
       
   153 void CInstanceTestManager::EditEntriesL()
       
   154 	{
       
   155 	CCalIter& iter = iTestLibrary->GetIterL();
       
   156 	
       
   157 	for( TPtrC8 Uid(iter.FirstL()); Uid != KNullDesC8(); Uid.Set(iter.NextL()) )
       
   158 		{
       
   159 		RPointerArray<CCalEntry> entryList;
       
   160 		CleanupResetAndDestroyPushL(entryList);
       
   161 
       
   162 		iEntryView->FetchL(Uid, entryList);
       
   163 		// can't update child entries, so only the first entry in entryList can be used
       
   164 		while (entryList.Count() > 1)
       
   165 			{
       
   166 			delete entryList[1];
       
   167 			entryList.Remove(1);
       
   168 			}
       
   169 		
       
   170 		entryList[0]->SetSequenceNumberL( 0 );
       
   171 		// don't remove the original summary as it is used in text search tests 
       
   172 		TBuf<256> summary;
       
   173 		summary.Copy(entryList[0]->SummaryL());
       
   174 		summary.Append( _L("0123456789 qwertyuiopasdfghjklzxcvbnm") );
       
   175 		entryList[0]->SetSummaryL(summary);
       
   176 		
       
   177 		TInt updated = 0;
       
   178 
       
   179 		iEntryView->UpdateL(entryList, updated);
       
   180 
       
   181 		test( entryList.Count() == updated );		
       
   182 
       
   183 		CleanupStack::PopAndDestroy(&entryList);		
       
   184 		}
       
   185 	}
       
   186 
       
   187 
       
   188 // Call FindInstanceL and check the results
       
   189 void CInstanceTestManager::ExtractFromDatabaseL(CTestDataStruct& aTestData, TInt aTestAPI)
       
   190 	{
       
   191 	TCalTime startDate;
       
   192 	startDate.SetTimeLocalL(aTestData.iStartDate);
       
   193 	TCalTime endDate;
       
   194 	endDate.SetTimeLocalL(aTestData.iEndDate);
       
   195 
       
   196 	CalCommon::TCalTimeRange timeRange(startDate, endDate);
       
   197 
       
   198 	test.Printf(_L("TEST: %S\n"), &aTestData.iDescription);
       
   199 
       
   200 	RPointerArray<CCalInstance> instanceArray;
       
   201 	CleanupResetAndDestroyPushL(instanceArray);
       
   202 
       
   203 	switch (aTestAPI)
       
   204 		{
       
   205 		case EFindInstance:
       
   206 			{
       
   207 			test.Printf(_L("EFindInstance\n"));
       
   208 			
       
   209 			if (aTestData.iSearchString)
       
   210 				{
       
   211 				CCalInstanceView::TCalSearchParams searchParams(*aTestData.iSearchString, aTestData.iSearchBehaviour);
       
   212 				
       
   213 				iInstanceView->FindInstanceL(instanceArray, aTestData.iFilter, timeRange, searchParams);
       
   214 				}
       
   215 			else
       
   216 				{
       
   217 				iInstanceView->FindInstanceL(instanceArray, aTestData.iFilter, timeRange);		
       
   218 				}
       
   219 			}
       
   220 			break;
       
   221 		case EIteratorNextInstance:
       
   222 			{
       
   223 			test.Printf(_L("EIteratorNextInstance\n"));
       
   224 			
       
   225 			if (aTestData.iFilter & CalCommon::EIncludeRptsNextInstanceOnly)
       
   226 				{
       
   227 				// the iterator does not support this filter flag so return
       
   228 				CleanupStack::PopAndDestroy(&instanceArray);
       
   229 				return;			
       
   230 				}
       
   231 			
       
   232 			CCalFindInstanceSettings* findInstanceSettings = CCalFindInstanceSettings::NewL(aTestData.iFilter, timeRange);
       
   233 			CleanupStack::PushL(findInstanceSettings);
       
   234 			
       
   235 			if (aTestData.iSearchString)
       
   236 				{
       
   237 				CCalInstanceView::TCalSearchParams searchParams(*aTestData.iSearchString, aTestData.iSearchBehaviour);
       
   238 				findInstanceSettings->SetTextSearchL(searchParams);
       
   239 				}
       
   240 			
       
   241 			CCalInstanceIterator* iterator = iInstanceView->FindInstanceL(*findInstanceSettings);
       
   242 			CleanupStack::PopAndDestroy(findInstanceSettings);
       
   243 			CleanupStack::PushL(iterator);
       
   244 			
       
   245 			while (CCalInstance* instance = iterator->NextL())
       
   246 				{
       
   247 				instanceArray.AppendL(instance);
       
   248 				}
       
   249 			
       
   250 			CleanupStack::PopAndDestroy(iterator);
       
   251 			}
       
   252 			break;
       
   253 		default:
       
   254 			{
       
   255 			test(EFalse);
       
   256 			}
       
   257 		}
       
   258 	
       
   259 	
       
   260 
       
   261 	TBuf<50> startBuf;
       
   262 	TBuf<50> endBuf;
       
   263 	startDate.TimeLocalL().FormatL(startBuf,KFormatDate());
       
   264 	endDate.TimeLocalL().FormatL(endBuf,KFormatDate());
       
   265 
       
   266 	test.Printf(_L("Checking instances between %S and %S\n"), &startBuf, &endBuf);
       
   267 
       
   268 	if (aTestData.iFunction)
       
   269 		{
       
   270 		ASSERT(aTestData.iFunction->CallBack() == 0); // this fails if test function returns non-zero
       
   271 		aTestData.iFunction->iPtr = NULL;	// the callback is a one shot function that deletes the object..
       
   272 		}
       
   273 	
       
   274 	if (instanceArray.Count() == 0)
       
   275 		{
       
   276 		test.Printf(_L("No instances found\n"));
       
   277 		}
       
   278 	else
       
   279 		{
       
   280 		TTime instTime;
       
   281 		TTime instStartTime;
       
   282 		TTime instEndTime;
       
   283 		
       
   284 		TBuf<50> instBuf;
       
   285 		TBuf<50> instStartBuf;
       
   286 		TBuf<50> instEndBuf;
       
   287 		
       
   288 		for (TInt instanceCount = 0; instanceCount < instanceArray.Count(); ++instanceCount)
       
   289 			{
       
   290 			CCalInstance* inst = instanceArray[instanceCount];
       
   291 			instTime = inst->Time().TimeLocalL();
       
   292 			instStartTime = inst->StartTimeL().TimeLocalL();
       
   293 			instEndTime = inst->EndTimeL().TimeLocalL();
       
   294 			
       
   295 			instTime.FormatL(instBuf,KFormatDate());
       
   296 			instStartTime.FormatL(instStartBuf,KFormatDateTime());
       
   297 			instEndTime.FormatL(instEndBuf,KFormatDateTime());
       
   298 			
       
   299 			instBuf.Append(_L(" "));
       
   300 			instBuf.AppendNum(instTime.DateTime().Hour());
       
   301 			instBuf.Append(_L(":"));
       
   302 			instBuf.AppendNum(instTime.DateTime().Minute());
       
   303 			
       
   304 			TBuf<32>  rptType;
       
   305 			TCalRRule rptDef;
       
   306 			if (!inst->Entry().GetRRuleL(rptDef))
       
   307 				{
       
   308 				rptType.Copy(_L("One off"));
       
   309 				}
       
   310 			else
       
   311 				{
       
   312 				switch (rptDef.Type())
       
   313 					{
       
   314 					case TCalRRule::EDaily:
       
   315 						rptType.Format(_L("Daily every %d days"), rptDef.Interval());
       
   316 						break;
       
   317 					case TCalRRule::EWeekly:
       
   318 						rptType.Format(_L("Weekly every %d weeks"), rptDef.Interval());
       
   319 						break;
       
   320 					case TCalRRule::EMonthly:
       
   321 						rptType.Format(_L("MonthlyByDates every %d months"), rptDef.Interval());
       
   322 						break;
       
   323 					case TCalRRule::EYearly:
       
   324 						rptType.Format(_L("YearlyByDate every %d years"), rptDef.Interval());
       
   325 						break;
       
   326 					default:
       
   327 						test(0); // should never get here unless somehow a repeat rule has an invalid type!
       
   328 						break;
       
   329 					}
       
   330 				}
       
   331 
       
   332 			TBool instanceNotExpected = EFalse;
       
   333 				
       
   334 			if (inst->Entry().EntryTypeL() == CCalEntry::ETodo)
       
   335 				{
       
   336 				// For a todo the 'end time' is the 'time' of the instance
       
   337 				TDateTime dateTime = instEndTime.DateTime();
       
   338 								
       
   339 				//**NOTE**//
       
   340 				//In case of undated ToDo's the instance time is current time, it may change slightly as
       
   341 				//the FindInstance() and other operations take some amount of time thereby changing the
       
   342 				//seconds and microseconds, which will cause the test condition below to fail.
       
   343 				//So,while adding Expected instance time to aTestData.iTimes, we set the seconds
       
   344 				//and microseconds to 0.
       
   345 				//Now, to match that time we need to set the seconds and microseconds
       
   346 				//of retrieved instance time to 0 as well.
       
   347 					
       
   348 				dateTime.SetMicroSecond(0);
       
   349 				dateTime.SetSecond(0);
       
   350 				instEndTime = TTime(dateTime);
       
   351 
       
   352 				if (aTestData.iTimes.Find(instEndTime) == KErrNotFound)
       
   353 					{
       
   354 					instanceNotExpected = ETrue;
       
   355 					}
       
   356 				}
       
   357 			else
       
   358 				{
       
   359 				if (aTestData.iTimes.Find(instTime) == KErrNotFound && instStartTime == instTime)
       
   360 					{
       
   361 					instanceNotExpected = ETrue;
       
   362 					}
       
   363 				}
       
   364 				
       
   365 #ifdef __WINS__
       
   366 			test.Printf(_L("One instance is on %S to %S - %S\n"), &instStartBuf, &instEndBuf, &rptType);
       
   367 			if (instanceNotExpected)
       
   368 				{
       
   369 				test.Printf(_L("THIS ENTRY WAS NOT EXPECTED"));
       
   370 				test(0);
       
   371 				}
       
   372 #else
       
   373 // don't output every instance on non-WINS environment because the log size is too great
       
   374 			if (instanceNotExpected)
       
   375 				{
       
   376 				test.Printf(_L("Unexpected instance at %S to %S - %S\n"), &instStartBuf, &instEndBuf, &rptType);
       
   377 				test(0);
       
   378 				}
       
   379 #endif
       
   380 			}
       
   381 		}
       
   382 		
       
   383 	if (instanceArray.Count() != aTestData.iTimes.Count())
       
   384 		{
       
   385 		test.Printf(_L("Some expected entries were not found!"));
       
   386 		test(0);	
       
   387 		}
       
   388 		
       
   389 	CleanupStack::PopAndDestroy(&instanceArray);
       
   390 	}
       
   391 
       
   392 
       
   393 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   394  * DoTestL()
       
   395  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   396 	
       
   397 LOCAL_C void DoTestL()
       
   398 	{
       
   399 	CInstanceTestManager* testManager = CInstanceTestManager::NewLC();
       
   400 
       
   401 
       
   402 	TPerformanceTimer timer(test);
       
   403 	timer.Start();
       
   404 
       
   405 
       
   406 	// Run the test suite
       
   407 
       
   408 	for (TInt i(0) ; i < CInstanceTestManager::ETotalTestAPI ; ++i)
       
   409 		{
       
   410 		testManager->CleanDatabaseL();
       
   411 		testManager->TestPopulateAndCheckL(i);
       
   412 		testManager->TestEditAndCheckL(i);
       
   413 		}
       
   414 
       
   415 
       
   416 	timer.Stop();
       
   417 	test.Printf(_L("Done\n"));
       
   418 	// printout performance time
       
   419 	timer.PrintOut();	
       
   420 
       
   421 
       
   422 	CleanupStack::PopAndDestroy(testManager);	
       
   423 	}
       
   424 
       
   425 
       
   426 /**
       
   427 @SYMTestCaseID     PIM-TCAL-INSTANCE-0001
       
   428 */
       
   429 TInt E32Main()
       
   430     {
       
   431 	__UHEAP_MARK;
       
   432 
       
   433 	test.Start(_L("@SYMTESTCaseID:PIM-TCAL-INSTANCE-0001 Calendar Interim API Instance test suite"));
       
   434 	test.Title();
       
   435 
       
   436 	CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   437 	if (!trapCleanup)
       
   438 		{
       
   439 		return KErrNoMemory;
       
   440 		}
       
   441 
       
   442 	CActiveScheduler* scheduler = new CActiveScheduler();
       
   443 	if (!scheduler)
       
   444 		{
       
   445 		delete trapCleanup;
       
   446 		return KErrNoMemory;
       
   447 		}
       
   448 	CActiveScheduler::Install(scheduler);	
       
   449 
       
   450 	TRAPD(ret, DoTestL());
       
   451 	test(ret == KErrNone);
       
   452 	
       
   453 	delete scheduler;
       
   454 	delete trapCleanup;	
       
   455 
       
   456 	test.End();
       
   457 	test.Close();
       
   458 
       
   459 	__UHEAP_MARKEND;
       
   460 
       
   461 	return (KErrNone);
       
   462     }
       
   463