pimappservices/calendar/tsrc/tcal_finddaywithinstance.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 "caltestlib.h"
       
    17 #include <calentry.h>
       
    18 #include <calentryview.h>
       
    19 #include <calinstance.h>
       
    20 #include <calinstanceview.h>
       
    21 #include <calsession.h>
       
    22 #include <e32test.h>
       
    23 #include <caliterator.h>
       
    24 #include <caluser.h>
       
    25 #include <calrrule.h>
       
    26 
       
    27 _LIT(KTestName,"tcal_finddaywithinstance");
       
    28 
       
    29 RTest test(KTestName);
       
    30 
       
    31 
       
    32 class CFindInstanceDayTestManager : public CBase
       
    33 	{
       
    34 public:
       
    35 
       
    36     static CFindInstanceDayTestManager* NewLC();
       
    37 	~CFindInstanceDayTestManager();
       
    38 	void StoreEntryL();
       
    39 	TInt FindInstanceL();
       
    40 	void FindDayWithInstanceL();
       
    41 	
       
    42 private:
       
    43 
       
    44 	// avoid any method but NewL instancing the class
       
    45 	CFindInstanceDayTestManager() {	}
       
    46 	// no copy constructor and assignment operator
       
    47 	CFindInstanceDayTestManager(CFindInstanceDayTestManager& );
       
    48 	CFindInstanceDayTestManager& operator = (const CFindInstanceDayTestManager& );
       
    49 
       
    50 	void ConstructL();
       
    51 
       
    52 	
       
    53 private:
       
    54 
       
    55 	CCalTestLibrary* iTestLibrary;
       
    56 	};
       
    57 //entry1
       
    58 const TTime KEntry1Start(TDateTime(2000, EJune, 0, 1, 1, 0, 0));	//Thursday
       
    59 const TTime KEntry1End(TDateTime(2000, EJune, 0, 2, 2, 0, 0));		
       
    60 //entry2
       
    61 const TTime KEntry2Start(TDateTime(2000, EJune, 0, 13, 0, 0, 0));	//Thursday
       
    62 const TTime KEntry2End(TDateTime(2000, EJune, 0, 14, 0, 0, 0));	
       
    63 //entry3 (todo)
       
    64 const TTime KEntry3End(TDateTime(2000, EJune, 0, 23, 0, 0, 0));		//Thursday
       
    65 
       
    66 // Constructor/Destructor
       
    67 
       
    68 CFindInstanceDayTestManager* CFindInstanceDayTestManager::NewLC()
       
    69 	{
       
    70 	CFindInstanceDayTestManager* self = new (ELeave) CFindInstanceDayTestManager();
       
    71 	
       
    72 	CleanupStack::PushL(self);
       
    73 	self->ConstructL();
       
    74 	
       
    75 	return (self);
       
    76 	}
       
    77 
       
    78 
       
    79 void CFindInstanceDayTestManager::ConstructL()
       
    80 	{
       
    81 	iTestLibrary = CCalTestLibrary::NewL();
       
    82 	
       
    83 	iTestLibrary->ReplaceFileL(KTestName);
       
    84 	iTestLibrary->OpenFileL(KTestName);	
       
    85 	}	
       
    86 
       
    87 
       
    88 CFindInstanceDayTestManager::~CFindInstanceDayTestManager()	
       
    89 	{
       
    90 	delete iTestLibrary;
       
    91 	}
       
    92 
       
    93 
       
    94 void CFindInstanceDayTestManager::StoreEntryL()
       
    95 	{
       
    96 	test.Next(_L("Store entries"));
       
    97 
       
    98 
       
    99 	RPointerArray<CCalEntry> entriesToStore;
       
   100 	CleanupResetAndDestroyPushL(entriesToStore);
       
   101 	
       
   102 	//Create first entry appt, starting at midnight 1st June 2000 weekly repeating for three weeks
       
   103 	_LIT8(KGuidId1, "KGuidId1");
       
   104 	HBufC8* guid1 = KGuidId1().AllocLC();
       
   105 
       
   106 	CCalEntry::TType entryType = CCalEntry::EAppt;
       
   107 	CCalEntry* entry1 = iTestLibrary->CreateCalEntryL(entryType, guid1);
       
   108 	CleanupStack::Pop(guid1);
       
   109 	
       
   110 	CleanupStack::PushL(entry1);
       
   111 	
       
   112 	TCalTime start;
       
   113 	TCalTime end;
       
   114 	start.SetTimeLocalL(KEntry1Start);
       
   115 	end.SetTimeLocalL(KEntry1End);
       
   116 	
       
   117 	entry1->SetStartAndEndTimeL(start, end);
       
   118 	TCalRRule rpt1(TCalRRule::EWeekly);
       
   119 	rpt1.SetInterval(1);
       
   120 	TCalTime repeatStart;
       
   121 	repeatStart.SetTimeLocalL(KEntry1Start);
       
   122 	rpt1.SetDtStart(repeatStart);
       
   123 	
       
   124 	RArray<TDay> days1;
       
   125 	CleanupClosePushL(days1);
       
   126 	days1.AppendL(EThursday);
       
   127 	rpt1.SetByDay(days1);
       
   128 	CleanupStack::PopAndDestroy(&days1);
       
   129 
       
   130 	rpt1.SetCount(3);
       
   131 	entry1->SetRRuleL(rpt1);
       
   132 	entriesToStore.AppendL(entry1);
       
   133 	CleanupStack::Pop(entry1);
       
   134 	
       
   135 
       
   136 	//Create the second entry appt, starting at mid day 1st June 2000 weekly repeating for three weeks
       
   137 	_LIT8(KGuidId2, "KGuidId2");
       
   138 	HBufC8* guid2 = KGuidId2().AllocLC();
       
   139 
       
   140 	entryType = CCalEntry::EAppt;
       
   141 	CCalEntry* entry2 = iTestLibrary->CreateCalEntryL(entryType, guid2);
       
   142 	CleanupStack::Pop(guid2);
       
   143 	CleanupStack::PushL(entry2);
       
   144 	
       
   145 	start.SetTimeLocalL(KEntry2Start);
       
   146 	end.SetTimeLocalL(KEntry2End);
       
   147 	entry2->SetStartAndEndTimeL(start, end);
       
   148 	
       
   149 	TCalRRule rpt2(TCalRRule::EWeekly);
       
   150 	rpt2.SetInterval(1);
       
   151 	rpt2.SetDtStart(start);
       
   152 	RArray<TDay> days2;
       
   153 	CleanupClosePushL(days2);
       
   154 	days2.AppendL(EThursday);
       
   155 	rpt2.SetByDay(days2);
       
   156 	CleanupStack::PopAndDestroy(&days2);
       
   157 
       
   158 	rpt2.SetCount(3);
       
   159 	entry2->SetRRuleL(rpt2);
       
   160 	entriesToStore.AppendL(entry2);
       
   161 	CleanupStack::Pop(entry2);
       
   162 
       
   163 	//Create the third entry todo, due date is midnight 2st June 2000 weekly repeating for three weeks
       
   164 	_LIT8(KGuidId3, "KGuidId3");
       
   165 	HBufC8* guid3 = KGuidId3().AllocLC();
       
   166 
       
   167 	entryType = CCalEntry::ETodo;
       
   168 	CCalEntry* entry3 = iTestLibrary->CreateCalEntryL(entryType, guid3);
       
   169 	CleanupStack::Pop(guid3);
       
   170 	CleanupStack::PushL(entry3);
       
   171 	
       
   172 	start.SetTimeLocalL(Time::NullTTime());
       
   173 	end.SetTimeLocalL(KEntry3End);
       
   174 	entry3->SetStartAndEndTimeL(start, end);
       
   175 
       
   176 	TCalRRule rpt3(TCalRRule::EWeekly);
       
   177 	rpt3.SetInterval(1);
       
   178 	rpt3.SetDtStart(end);
       
   179 	RArray<TDay> days3;
       
   180 	CleanupClosePushL(days3);
       
   181 	days3.AppendL(EThursday);
       
   182 	rpt3.SetByDay(days3);
       
   183 	CleanupStack::PopAndDestroy(&days3);
       
   184 
       
   185 	rpt3.SetCount(3);
       
   186 	entry3->SetRRuleL(rpt3);
       
   187 	entriesToStore.AppendL(entry3);
       
   188 	CleanupStack::Pop(entry3);
       
   189 	
       
   190 	TInt num;
       
   191 	iTestLibrary->SynCGetEntryViewL().StoreL(entriesToStore,num);
       
   192 	test(num=3);
       
   193 	CleanupStack::PopAndDestroy(&entriesToStore);
       
   194 	}
       
   195 
       
   196 
       
   197 
       
   198 TInt CFindInstanceDayTestManager::FindInstanceL()
       
   199 	{
       
   200 	test.Next(_L("Find Instance to make sure there are right number of instances"));
       
   201 
       
   202 
       
   203 	TCalTime minTime;
       
   204 	minTime.SetTimeLocalL(TCalTime::MinTime());
       
   205 	TCalTime maxTime;
       
   206 	maxTime.SetTimeLocalL(TCalTime::MaxTime());
       
   207 	CalCommon::TCalTimeRange timeRange(minTime, maxTime);
       
   208 	CalCommon::TCalViewFilter filter = CalCommon::EIncludeAll;
       
   209 
       
   210 	RPointerArray<CCalInstance> instances;
       
   211 	CleanupResetAndDestroyPushL(instances);
       
   212 
       
   213 	iTestLibrary->SynCGetInstanceViewL().FindInstanceL(instances, filter, timeRange);
       
   214 	TInt instanceNum = instances.Count();
       
   215 	for (TInt ii=0; ii<instanceNum; ++ii)
       
   216 		{
       
   217 		TTime instancetime = (instances[ii])->Time().TimeLocalL();
       
   218 	
       
   219 		TBuf<100> tempBuf;	
       
   220 		_LIT(KTTimeDateFormat,"Time: %H:%T:%S:%*C3 Date:%*1/%2/%3");
       
   221 		instancetime.FormatL(tempBuf,KTTimeDateFormat);
       
   222 		RDebug::Print(_L("%S"), &tempBuf);
       
   223 		}
       
   224 	CleanupStack::PopAndDestroy(&instances);
       
   225 	return 	instanceNum;
       
   226 	}
       
   227 	
       
   228 
       
   229 
       
   230 void CFindInstanceDayTestManager::FindDayWithInstanceL()
       
   231 	{
       
   232 	test.Next(_L("Find Instance in the expected day"));
       
   233 
       
   234 
       
   235 	CalCommon::TCalViewFilter filter = CalCommon::EIncludeAll;
       
   236 	
       
   237 	const TTime KFirstDayWithInstance(TDateTime(2000, EJune, 0, 1, 1, 0, 0));
       
   238 	const TTime KSecondDayWithInstance(TDateTime(2000, EJune, 7, 1,1, 0, 0));
       
   239 	const TTime KLastDayWithInstance(TDateTime(2000, EJune, 14, 23, 0, 0, 0));
       
   240 
       
   241 	TCalTime start;
       
   242 	start.SetTimeLocalL(TCalTime::MinTime());
       
   243 	TCalTime instanceFind = iTestLibrary->SynCGetInstanceViewL().NextInstanceL(filter, start);
       
   244 	TTime instanceFindTTime = instanceFind.TimeLocalL();
       
   245 	//The first instance date should be found
       
   246 	test (instanceFindTTime == KFirstDayWithInstance);
       
   247 
       
   248 	RDebug::Print(_L("Min time NextInstanceL"));
       
   249 	TBuf<100> tempBuf;	
       
   250 	_LIT(KTTimeDateFormat,"Time: %H:%T:%S:%*C3 Date:%*1/%2/%3");
       
   251 	instanceFindTTime.FormatL(tempBuf,KTTimeDateFormat);
       
   252 	RDebug::Print(_L("%S\n\n"), &tempBuf);
       
   253 	
       
   254 	start.SetTimeLocalL(TCalTime::MaxTime());
       
   255 	instanceFind = iTestLibrary->SynCGetInstanceViewL().PreviousInstanceL(filter, start);
       
   256 	instanceFindTTime = instanceFind.TimeLocalL();
       
   257 	// The last instance date should be found
       
   258 	test (instanceFindTTime == KLastDayWithInstance);
       
   259 	
       
   260 	RDebug::Print(_L("Max time PreviousInstanceL"));
       
   261 	instanceFindTTime.FormatL(tempBuf,KTTimeDateFormat);
       
   262 	RDebug::Print(_L("%S\n\n"), &tempBuf);
       
   263 
       
   264 	start.SetTimeLocalL(KEntry1Start);
       
   265 	instanceFind = iTestLibrary->SynCGetInstanceViewL().NextInstanceL(filter, start);
       
   266 	instanceFindTTime = instanceFind.TimeLocalL();
       
   267 	//The second Thurday should be found
       
   268 	test (instanceFindTTime == KSecondDayWithInstance );
       
   269 
       
   270 	start.TimeLocalL().FormatL(tempBuf,KTTimeDateFormat);
       
   271 	RDebug::Print(_L("start NextInstanceL - %S"), &tempBuf);
       
   272 	instanceFindTTime.FormatL(tempBuf,KTTimeDateFormat);
       
   273 	RDebug::Print(_L("%S\n\n"), &tempBuf);
       
   274 
       
   275 	start.SetTimeLocalL(KFirstDayWithInstance + TTimeIntervalDays(1));
       
   276 	instanceFind = iTestLibrary->SynCGetInstanceViewL().NextInstanceL(filter, start);
       
   277 	instanceFindTTime = instanceFind.TimeLocalL();
       
   278 	//The second Thurday should be found
       
   279 	test (instanceFindTTime == KSecondDayWithInstance );
       
   280 
       
   281 	start.TimeLocalL().FormatL(tempBuf,KTTimeDateFormat);
       
   282 	RDebug::Print(_L("start NextInstanceL- %S"), &tempBuf);
       
   283 	instanceFindTTime.FormatL(tempBuf,KTTimeDateFormat);
       
   284 	RDebug::Print(_L("%S\n\n"), &tempBuf);
       
   285 
       
   286 	start.SetTimeLocalL(KEntry1Start);
       
   287 	instanceFind=iTestLibrary->SynCGetInstanceViewL().PreviousInstanceL(filter, start);
       
   288 	//There is no instance before the first instance
       
   289 	test(instanceFind.TimeLocalL() == Time::NullTTime());
       
   290 	
       
   291 	start.TimeLocalL().FormatL(tempBuf,KTTimeDateFormat);
       
   292 	RDebug::Print(_L("start PreviousInstanceL- %S"), &tempBuf);
       
   293 	instanceFind.TimeLocalL().FormatL(tempBuf,KTTimeDateFormat);
       
   294 	RDebug::Print(_L("%S\n\n"), &tempBuf);
       
   295 	
       
   296 	start.SetTimeLocalL(KLastDayWithInstance);
       
   297 	instanceFind = iTestLibrary->SynCGetInstanceViewL().NextInstanceL(filter, start);
       
   298 	//There is no instances after the Last instance
       
   299 	test(instanceFind.TimeLocalL() == Time::NullTTime());
       
   300 
       
   301 	start.TimeLocalL().FormatL(tempBuf,KTTimeDateFormat);
       
   302 	RDebug::Print(_L("start NextInstanceL - %S"), &tempBuf);
       
   303 	instanceFind.TimeLocalL().FormatL(tempBuf,KTTimeDateFormat);
       
   304 	RDebug::Print(_L("%S\n\n"), &tempBuf);
       
   305 	}
       
   306 	
       
   307 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   308  * DoTestL()
       
   309  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   310 	
       
   311 static void DoTestL()
       
   312 	{
       
   313 	CFindInstanceDayTestManager* testManager = CFindInstanceDayTestManager::NewLC();
       
   314 	//Store 3 entries each of them is weekly repeating for 3 weeks
       
   315 	testManager->StoreEntryL();
       
   316 	//Make sure there are 9 instances are found
       
   317 	test (testManager->FindInstanceL() == 9);
       
   318 	testManager->FindDayWithInstanceL();
       
   319 	CleanupStack::PopAndDestroy(testManager);
       
   320 	}
       
   321 
       
   322 
       
   323 /**
       
   324 
       
   325 @SYMTestCaseID     PIM-TCAL-FINDDAYWITHINSTANCE-0001
       
   326 
       
   327 */
       
   328 
       
   329 TInt E32Main()
       
   330     {
       
   331 	__UHEAP_MARK;
       
   332 
       
   333 	test.Start(_L("@SYMTESTCaseID:PIM-TCAL-FINDDAYWITHINSTANCE-0001 Calendar Interim API finddaywithinstance test suite"));
       
   334 
       
   335 	test.Title();
       
   336 
       
   337 	CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   338 	if (!trapCleanup)
       
   339 		{
       
   340 		return KErrNoMemory;
       
   341 		}
       
   342 
       
   343 	CActiveScheduler* scheduler = new CActiveScheduler();
       
   344 	if (!scheduler)
       
   345 		{
       
   346 		delete trapCleanup;
       
   347 		return KErrNoMemory;
       
   348 		}
       
   349 	CActiveScheduler::Install(scheduler);	
       
   350 
       
   351 	TRAPD(ret, DoTestL());
       
   352 	test(ret == KErrNone);
       
   353 	
       
   354 	delete scheduler;
       
   355 	delete trapCleanup;	
       
   356 
       
   357 	test.End();
       
   358 	test.Close();
       
   359 
       
   360 	__UHEAP_MARKEND;
       
   361 
       
   362 	return (KErrNone);
       
   363     }