pimappservices/calendar/tsrc/tcal_QueueAlarmOnStartup.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 
       
    18 // System includes
       
    19 #include <e32debug.h>
       
    20 #include <e32test.h>
       
    21 
       
    22 #include <asclisession.h>
       
    23 #include <calalarm.h>
       
    24 #include <calentryview.h>
       
    25 #include <calrrule.h>
       
    26 #include <calsession.h>
       
    27 #include <tz.h>
       
    28 
       
    29 #include <coreappstest/utilities.h>
       
    30 
       
    31 RTest test(_L("tcal_queueAlarmsOnStartup"));
       
    32 
       
    33 _LIT8(KEntryGuid, "A_DUMMY_GUID");
       
    34 _LIT(KCalendarFile, "tcal_queueAlarmsOnStartup");
       
    35 _LIT(KFormatDateTime, "%D%M%Y%/0%1%/1%2%/2%3%/3 %H:%T:%S");
       
    36 
       
    37 	
       
    38 class CTestApp : public CBase, public MCalProgressCallBack
       
    39 	{
       
    40 public:
       
    41 	~CTestApp();
       
    42 	
       
    43 	void DoOpenCalendarL(TTime aAlarmTime);
       
    44 	void DoNotOpenCalendarL(TTime aSampleTime, TTimeIntervalSeconds aTimeout, TInt aInitialExpectedCount, TInt aFinalExpectedCount);
       
    45 	
       
    46 	static void ClearAlarmServerL();
       
    47 	static void CreateCalendarFileL(CCalTestLibrary* aTestLib);
       
    48 	
       
    49 	// from MCalProgressCallBack
       
    50 	void Progress(TInt ) {};
       
    51 	TBool NotifyProgress() {return EFalse; };
       
    52 	void Completed(TInt aError);
       
    53 	
       
    54 private:
       
    55 	static void AddAlarmedEntryL( CCalEntryView& aEntryView, CCalEntry::TType aType, 
       
    56 	    TTime aStartTime, TCalRRule* aRule, TInt aAlarmOffset = 1, 
       
    57 	    TBool aFloating = EFalse );
       
    58 	void CompletedEntryViewOpenL();
       
    59 	    
       
    60 private:
       
    61 	CCalSession* iSession;
       
    62 	CCalEntryView* iEntryView;
       
    63 	TTime iAlarmTime;
       
    64 	};
       
    65 
       
    66 // Add an alarmed entry, alarm to expire 1 minute before aStartTime, except for Todos.
       
    67 // Entry end time is one hour after start time (so Todo alarm time will be 59 minutes after aStartTime)
       
    68 void CTestApp::AddAlarmedEntryL( CCalEntryView& aEntryView, CCalEntry::TType aType, 
       
    69     TTime aStartTime, TCalRRule* aRule, TInt aAlarmOffset, TBool aFloating )
       
    70 	{
       
    71 	test.Printf(_L("Adding entry of type %d\n"), aType);
       
    72 	
       
    73 	RPointerArray<CCalEntry> entriesToAdd;
       
    74 	CleanupClosePushL(entriesToAdd);
       
    75 	
       
    76 	TCalTime calStartTime;
       
    77 	TTime endTime(aStartTime + TTimeIntervalHours(1));
       
    78 	TCalTime calEndTime;
       
    79 
       
    80 	if (aFloating)
       
    81 		{
       
    82 		calStartTime.SetTimeLocalFloatingL(aStartTime);
       
    83 		calEndTime.SetTimeLocalFloatingL(endTime);
       
    84 		}
       
    85 	else
       
    86 		{
       
    87 		calStartTime.SetTimeLocalL(aStartTime);
       
    88 		calEndTime.SetTimeLocalL(endTime);
       
    89 		}
       
    90 
       
    91 	TBuf<50> startBuf;
       
    92     if ( aType == CCalEntry::ETodo )
       
    93         {
       
    94         endTime.FormatL(startBuf, KFormatDateTime());
       
    95         }
       
    96     else
       
    97         {
       
    98         aStartTime.FormatL(startBuf, KFormatDateTime());
       
    99         }
       
   100 	test.Printf(_L("Entry Time is %S, Alarm Offset is %d minute\n"), &startBuf, aAlarmOffset);
       
   101 
       
   102 	
       
   103 	TBuf8<64> uid;
       
   104 	uid.Copy(KEntryGuid());
       
   105 	uid.AppendNum(aStartTime.Int64());
       
   106 	if ( aAlarmOffset >= 0 )
       
   107 	    {
       
   108 	    uid.Append(_L("+"));
       
   109 	    }
       
   110 	uid.AppendNum(aAlarmOffset);
       
   111 	HBufC8* guid = uid.AllocLC();
       
   112 	CCalEntry* entry = CCalEntry::NewL(aType, guid, CCalEntry::EMethodNone, 0);
       
   113 	CleanupStack::Pop(guid);
       
   114 	CleanupStack::PushL(entry);
       
   115 	
       
   116 	entry->SetStartAndEndTimeL(calStartTime, calEndTime);
       
   117 	if (aRule)
       
   118 		{
       
   119 		entry->SetRRuleL(*aRule);
       
   120 		}
       
   121 	
       
   122 	CCalAlarm* alarm = CCalAlarm::NewL();
       
   123 	CleanupStack::PushL(alarm);
       
   124 	alarm->SetTimeOffset(aAlarmOffset);
       
   125 	entry->SetAlarmL(alarm);
       
   126 	CleanupStack::PopAndDestroy(alarm);
       
   127 
       
   128 	entriesToAdd.AppendL(entry);
       
   129 	CleanupStack::Pop(entry);
       
   130 	
       
   131 	TInt entriesAdded(0);
       
   132 	aEntryView.StoreL(entriesToAdd, entriesAdded);
       
   133 	test(entriesAdded == 1);
       
   134 
       
   135 	entriesToAdd.ResetAndDestroy();
       
   136 	CleanupStack::Pop(); // entriesToAdd.Close()
       
   137 
       
   138 	}
       
   139 
       
   140 // clear all alarms
       
   141 void CTestApp::ClearAlarmServerL()
       
   142 	{
       
   143 	RASCliSession almSession;
       
   144 	test(almSession.Connect() == KErrNone);
       
   145 	CleanupClosePushL(almSession);
       
   146 
       
   147 	// delete all alarms
       
   148 	RArray<TAlarmId> idArray;
       
   149 	CleanupClosePushL(idArray);
       
   150 	almSession.GetAlarmIdListL(idArray);
       
   151 	for (TInt index = idArray.Count() - 1; index >= 0; --index)
       
   152 		{
       
   153 		almSession.AlarmDelete(idArray[index]);
       
   154 		}
       
   155 	CleanupStack::PopAndDestroy(&idArray);
       
   156 
       
   157 	CleanupStack::PopAndDestroy(&almSession);
       
   158 	}
       
   159 
       
   160 // set up the calendar file with 4 alarms
       
   161 void CTestApp::CreateCalendarFileL(CCalTestLibrary* aTestLib)
       
   162 	{
       
   163 	test.Printf(_L("Creating Calendar File\n"));
       
   164 	
       
   165 	aTestLib->ReplaceFileL(KCalendarFile());
       
   166 	aTestLib->OpenFileL(KCalendarFile());
       
   167 	
       
   168 	CCalEntryView& entryView = aTestLib->SynCGetEntryViewL();
       
   169 
       
   170 	TBuf<50> homeBuf;
       
   171 	TTime homeTime;
       
   172 	homeTime.HomeTime();
       
   173     homeTime.FormatL(homeBuf,KFormatDateTime());
       
   174 	test.Printf(_L("Current Time is %S\n"), &homeBuf);
       
   175 
       
   176     //
       
   177 	// Create the entries for orphaned alarms - set to expire while Calendar is closed.
       
   178     //
       
   179         // With positive alarm offsets (1):
       
   180 	// 1st January, 8:05:00 - alarm at 8:04
       
   181 	TTime appt1Time(TDateTime(2005, EJanuary, 0, 8, 5, 0, 0));
       
   182 	AddAlarmedEntryL(entryView, CCalEntry::EAppt, appt1Time, NULL);
       
   183 
       
   184 	// 1st January, 8:10:00 - alarm at 8:09
       
   185 	TTime rem1Time(TDateTime(2005, EJanuary, 0, 8, 10, 0, 0));
       
   186 	AddAlarmedEntryL(entryView, CCalEntry::EReminder, rem1Time, NULL);
       
   187 
       
   188 	// 1st January, 8:15:00 - alarm at 8:14
       
   189 	TTime todo1Time(TDateTime(2005, EJanuary, 0, 7, 15, 0, 0));
       
   190 	AddAlarmedEntryL(entryView, CCalEntry::ETodo, todo1Time, NULL);
       
   191 
       
   192 	// 1st January, 8:20:00 - alarm at 8:19
       
   193 	TTime anniv1Time(TDateTime(2005, EJanuary, 0, 8, 20, 0, 0));
       
   194 	AddAlarmedEntryL(entryView, CCalEntry::EAnniv, anniv1Time, NULL);
       
   195 
       
   196 	// 1st January, 8:25:00 - alarm at 8:24
       
   197 	TTime event1Time(TDateTime(2005, EJanuary, 0, 8, 25, 0, 0));
       
   198 	AddAlarmedEntryL(entryView, CCalEntry::EEvent, event1Time, NULL);
       
   199 
       
   200         // With Negative alarm offsets (-1):
       
   201 	// 1st January, 10:05:00 - alarm at 10:06
       
   202 	TTime appt2Time(TDateTime(2005, EJanuary, 0, 10, 5, 0, 0));
       
   203 	AddAlarmedEntryL(entryView, CCalEntry::EAppt, appt2Time, NULL, -1);
       
   204 
       
   205 	// 1st January, 10:10:00 - alarm at 10:11
       
   206 	TTime rem2Time(TDateTime(2005, EJanuary, 0, 10, 10, 0, 0));
       
   207 	AddAlarmedEntryL(entryView, CCalEntry::EReminder, rem2Time, NULL, -1);
       
   208 
       
   209 	// 1st January, 10:15:00 - alarm at 10:16
       
   210 	TTime todo2Time(TDateTime(2005, EJanuary, 0, 9, 15, 0, 0));
       
   211 	AddAlarmedEntryL(entryView, CCalEntry::ETodo, todo2Time, NULL, -1);
       
   212 
       
   213 	// 1st January, 10:20:00 - alarm at 10:21
       
   214 	TTime anniv2Time(TDateTime(2005, EJanuary, 0, 10, 20, 0, 0));
       
   215 	AddAlarmedEntryL(entryView, CCalEntry::EAnniv, anniv2Time, NULL, -1);
       
   216 
       
   217 	// 1st January, 10:25:00 - alarm at 10:26
       
   218 	TTime event2Time(TDateTime(2005, EJanuary, 0, 10, 25, 0, 0));
       
   219 	AddAlarmedEntryL(entryView, CCalEntry::EEvent, event2Time, NULL, -1);
       
   220 
       
   221     //
       
   222     // Create the entries for session alarms - set to expire while Calendar is opened
       
   223     //
       
   224 	// 5th April, 16:03:00 - alarm at 16:02
       
   225 	TTime apptTime(TDateTime(2005, EApril, 4, 16, 3, 0, 0));
       
   226 	AddAlarmedEntryL(entryView, CCalEntry::EAppt, apptTime, NULL);
       
   227 	
       
   228 	// 23rd April, 11:26:00 - alarm at 12:25
       
   229 	TTime todoTime(TDateTime(2005, EApril, 22, 11, 26, 0, 0));
       
   230 	AddAlarmedEntryL(entryView, CCalEntry::ETodo, todoTime, NULL);
       
   231 
       
   232 	TCalTime calTime;
       
   233 		
       
   234 	// 6th May, 09:47:00 - alarm at 09:46 on 10th
       
   235 	TTime apptRptTime(TDateTime(2005, EMay, 5, 9, 47, 0, 0));
       
   236 	TCalRRule rule(TCalRRule::EDaily);
       
   237 	rule.SetInterval(2);
       
   238 	rule.SetCount(5);
       
   239 	calTime.SetTimeLocalL(apptRptTime);
       
   240 	rule.SetDtStart(calTime);
       
   241 	AddAlarmedEntryL(entryView, CCalEntry::EAppt, apptRptTime, &rule);
       
   242 
       
   243 	// 18th June, 14:58:00 - alarm at 15:57 on 22nd
       
   244 	TTime todoRptTime(TDateTime(2005, EJune, 17, 14, 58, 0, 0));
       
   245 	TCalRRule rule2(TCalRRule::EDaily);
       
   246 	rule2.SetInterval(2);
       
   247 	rule2.SetCount(5);
       
   248 	calTime.SetTimeLocalL(todoRptTime);
       
   249 	rule2.SetDtStart(calTime);
       
   250 	AddAlarmedEntryL(entryView, CCalEntry::ETodo, todoRptTime, &rule2);
       
   251 	
       
   252 	//-------------------
       
   253 	// Negative alarms.
       
   254 	//-------------------
       
   255 
       
   256 	// 00:00 on April 1st 05
       
   257 	TTime negAppt1Time(TDateTime(2005, EApril, 0, 0, 0, 0, 0)); 
       
   258 	// alarm at 09:00
       
   259 	AddAlarmedEntryL(entryView, CCalEntry::EAppt, negAppt1Time, NULL, -540); 
       
   260 
       
   261     // 00:00 on April 1st 05
       
   262 	TTime negTodo1Time(TDateTime(2005, EApril, 0, 0, 0, 0, 0)); 
       
   263 	// alarm at 11:00
       
   264 	AddAlarmedEntryL(entryView, CCalEntry::ETodo, negTodo1Time, NULL, -600); 
       
   265 
       
   266     //---------------------------
       
   267 	// Negative floating alarms.
       
   268     //---------------------------
       
   269 
       
   270 	// 00:00 on April 11th 05
       
   271 	TTime negAppt2Time(TDateTime(2005, EApril, 10, 0, 0, 0, 0)); 
       
   272 	// alarm at 09:00
       
   273 	AddAlarmedEntryL(entryView, CCalEntry::EAppt, negAppt2Time, NULL, -540, ETrue); 
       
   274 
       
   275     // 00:00 on April 11th 05
       
   276 	TTime negTodo2Time(TDateTime(2005, EApril, 10, 0, 0, 0, 0)); 
       
   277 	// alarm at 11:00
       
   278 	AddAlarmedEntryL(entryView, CCalEntry::ETodo, negTodo2Time, NULL, -600, ETrue); 
       
   279 
       
   280     //----------------------------
       
   281 	// Repeating negative alarms.
       
   282     //----------------------------
       
   283 
       
   284 	TTime negAppt1RptTime(TDateTime(2005, EMay, 0, 0, 0, 0, 0));
       
   285 	TCalRRule rule3(TCalRRule::EDaily);
       
   286 	rule3.SetInterval(1);
       
   287 	rule3.SetCount(5);
       
   288 	calTime.SetTimeLocalL(negAppt1RptTime);
       
   289 	rule3.SetDtStart(calTime);
       
   290 	// alarms at 09:00 on 1st-5th May 05
       
   291 	AddAlarmedEntryL(entryView, CCalEntry::EAppt, negAppt1RptTime, &rule3, -540); 
       
   292 	// alarms at 11:00 on 1st-5th May 05
       
   293 	AddAlarmedEntryL(entryView, CCalEntry::ETodo, negAppt1RptTime, &rule3, -600); 
       
   294 
       
   295 	TTime negAppt2RptTime(TDateTime(2005, EMay, 10, 0, 0, 0, 0));
       
   296 	TCalRRule rule4(TCalRRule::EDaily);
       
   297 	rule4.SetInterval(1);
       
   298 	rule4.SetCount(5);
       
   299 	calTime.SetTimeLocalFloatingL(negAppt2RptTime);
       
   300 	rule4.SetDtStart(calTime);
       
   301 	// alarms at 09:00 on 11th-15th May 05	
       
   302 	AddAlarmedEntryL(entryView, CCalEntry::EAppt, negAppt2RptTime, &rule4, -540, ETrue); 
       
   303 	// alarms at 11:00 on 11th-15th May 05
       
   304 	AddAlarmedEntryL(entryView, CCalEntry::ETodo, negAppt2RptTime, &rule4, -600, ETrue); 
       
   305 	}
       
   306 	
       
   307 CTestApp::~CTestApp()
       
   308 	{
       
   309 	delete iEntryView;
       
   310 	delete iSession;
       
   311 	CCalTestLibrary::WaitForAgendaServerClose();
       
   312 	}
       
   313 	
       
   314 void CTestApp::Completed(TInt /*aError*/)
       
   315 	{
       
   316 	RDebug::Print(_L("Entry view opening complete\n"));
       
   317 	TRAP_IGNORE(CompletedEntryViewOpenL());
       
   318 		
       
   319 	CActiveScheduler::Stop();
       
   320 	}
       
   321 
       
   322 void CTestApp::CompletedEntryViewOpenL()
       
   323 	{
       
   324 	RASCliSession almSession;
       
   325 	if (almSession.Connect() == KErrNone)
       
   326 		{
       
   327 		TInt notified = almSession.AlarmCountByState(EAlarmStateNotified) + almSession.AlarmCountByState(EAlarmStateNotifying);
       
   328 		TTime homeTime;
       
   329 		homeTime.HomeTime();
       
   330 		RDebug::Print(_L("Time=%d:%d:%d\n"), homeTime.DateTime().Hour(), homeTime.DateTime().Minute(), homeTime.DateTime().Second());
       
   331 		RDebug::Print(_L("Expecting alarm at %d:%d\n"), iAlarmTime.DateTime().Hour(), iAlarmTime.DateTime().Minute());
       
   332 		RDebug::Print(_L("Alarms notified: %d\n"), notified);
       
   333 		
       
   334 		// keep waiting until an alarm goes off
       
   335 		while (!notified)
       
   336 			{
       
   337 			User::After(1000000); // wait 1s
       
   338 			notified = almSession.AlarmCountByState(EAlarmStateNotified) + almSession.AlarmCountByState(EAlarmStateNotifying);
       
   339 			homeTime.HomeTime();
       
   340 			RDebug::Print(_L("Time=%d:%d:%d, alarms notified=%d\n"), homeTime.DateTime().Hour(), homeTime.DateTime().Minute(), homeTime.DateTime().Second(), notified);
       
   341 			}
       
   342 		RDebug::Print(_L("Alarm went off\n"));
       
   343 		
       
   344 		
       
   345 		// delete all alrms in state EAlarmStateNotifying
       
   346 		RArray<TAlarmId> idArray;
       
   347 		CleanupClosePushL(idArray);
       
   348 		almSession.GetAlarmIdListByStateL(EAlarmStateNotifying, idArray);
       
   349 		for (TInt index = idArray.Count() - 1; index >= 0; --index)
       
   350 			{
       
   351 			almSession.AlarmDelete(idArray[index]);
       
   352 			}
       
   353 		CleanupStack::PopAndDestroy(&idArray);
       
   354 		
       
   355 		// delete all alrms in state EAlarmStateNotified
       
   356 		CleanupClosePushL(idArray);
       
   357 		almSession.GetAlarmIdListByStateL(EAlarmStateNotified, idArray);
       
   358 		for (TInt index = idArray.Count() - 1; index >= 0; --index)
       
   359 			{
       
   360 			almSession.AlarmDelete(idArray[index]);
       
   361 			}
       
   362 		CleanupStack::PopAndDestroy(&idArray);
       
   363 		}
       
   364 	else
       
   365 		{
       
   366 		RDebug::Print(_L("Error connecting to alarm server. Test failure!!\n"));
       
   367 		User::Panic(_L("Error connecting to alarm server"), 0);
       
   368 		}
       
   369 	}
       
   370 
       
   371 void CTestApp::DoNotOpenCalendarL(TTime aSampleTime, TTimeIntervalSeconds aTimeout, TInt aInitialExpectedCount, TInt aFinalExpectedCount)
       
   372     {
       
   373 	iAlarmTime = aSampleTime;
       
   374 
       
   375 	RTz tz;
       
   376 	User::LeaveIfError(tz.Connect());
       
   377 	CleanupClosePushL(tz);
       
   378 	User::LeaveIfError(tz.SetHomeTime(iAlarmTime));
       
   379 	CleanupStack::PopAndDestroy(&tz); // tz.Close()
       
   380 	
       
   381 	RASCliSession almSession;
       
   382 	CleanupClosePushL(almSession);
       
   383 	if (almSession.Connect() != KErrNone)
       
   384 	    {
       
   385 	    RDebug::Print(_L("Error connecting to alarm server. Test failure!!\n"));
       
   386 		User::Panic(_L("Error connecting to alarm server"), 0);
       
   387 	    }
       
   388 
       
   389 	RDebug::Print(_L("Testing for alarms without Opening Calendar File\n"));
       
   390 	TInt notifying = almSession.AlarmCountByState(EAlarmStateNotifying);
       
   391 	TInt notified = almSession.AlarmCountByState(EAlarmStateNotified);
       
   392 	TTime homeTime;
       
   393 	homeTime.HomeTime();
       
   394 	TBuf<50> homeBuf;
       
   395     homeTime.FormatL(homeBuf,KFormatDateTime());
       
   396 	RDebug::Print(_L("Time=%S, alarms notifying=%d, notified=%d, expected=%d\n"), &homeBuf, notifying, notified, aInitialExpectedCount);
       
   397 	if (notified+notifying != aInitialExpectedCount)
       
   398 	    {
       
   399 	    User::Panic(_L("Unexpected Notifying Alarms"), 0);
       
   400 	    }
       
   401 
       
   402 	// wait for the event or until the timeout after aTimeout seconds
       
   403 	CAlarmEventWatcher* alarmwatcher = CAlarmEventWatcher::NewL(almSession);
       
   404 	CleanupStack::PushL(alarmwatcher);
       
   405 	const TInt KTimeOutPeriod(aTimeout.Int() * 1000000);
       
   406 	alarmwatcher->StartWaitingForEventL(EAlarmChangeEventTimerExpired, KTimeOutPeriod);
       
   407 	CleanupStack::PopAndDestroy(alarmwatcher);
       
   408 	
       
   409     notifying = almSession.AlarmCountByState(EAlarmStateNotifying);
       
   410 	notified = almSession.AlarmCountByState(EAlarmStateNotified);
       
   411 	homeTime.HomeTime();
       
   412     homeTime.FormatL(homeBuf,KFormatDateTime());
       
   413 	RDebug::Print(_L("Time=%S, alarms notifying=%d, notified=%d, expected=%d\n"), &homeBuf, notifying, notified, aFinalExpectedCount);
       
   414 	if (notified+notifying != aFinalExpectedCount)
       
   415 	    {
       
   416 		User::Panic(_L("Unexpected Notifying Alarms"), 0);
       
   417 	    }
       
   418 
       
   419 	RArray<TAlarmId> idArray;
       
   420 	CleanupClosePushL(idArray);
       
   421 	almSession.GetAlarmIdListByStateL(EAlarmStateNotifying, idArray);
       
   422 	for (TInt index = idArray.Count() - 1; index >= 0; --index)
       
   423 		{
       
   424 		almSession.AlarmDelete(idArray[index]);
       
   425 		}
       
   426 	CleanupStack::PopAndDestroy(&idArray);
       
   427 		
       
   428 	// delete all alrms in state EAlarmStateNotified
       
   429 	CleanupClosePushL(idArray);
       
   430 	almSession.GetAlarmIdListByStateL(EAlarmStateNotified, idArray);
       
   431 	for (TInt index = idArray.Count() - 1; index >= 0; --index)
       
   432 		{
       
   433 		almSession.AlarmDelete(idArray[index]);
       
   434 		}
       
   435 	CleanupStack::PopAndDestroy(&idArray);
       
   436 	
       
   437     CleanupStack::PopAndDestroy(&almSession);
       
   438     }
       
   439 
       
   440 void CTestApp::DoOpenCalendarL(TTime aAlarmTime)
       
   441 	{
       
   442 	iAlarmTime = aAlarmTime;
       
   443 	
       
   444 	RTz tz;
       
   445 	User::LeaveIfError(tz.Connect());
       
   446 	CleanupClosePushL(tz);
       
   447 	User::LeaveIfError(tz.SetHomeTime(iAlarmTime - TTimeIntervalSeconds(3)));
       
   448 	CleanupStack::PopAndDestroy(&tz); // tz.Close()
       
   449 	
       
   450 	iSession = CCalSession::NewL();
       
   451 	
       
   452 	RDebug::Print(_L("Opening calendar file...\n"));
       
   453 	iSession->OpenL(KCalendarFile);
       
   454 	
       
   455 	TTime t1, t2;
       
   456 	t1.UniversalTime();
       
   457 	
       
   458 	RDebug::Print(_L("Opening entry view...\n"));
       
   459 	iEntryView = CCalEntryView::NewL(*iSession, *this);
       
   460 	CActiveScheduler::Start();
       
   461 
       
   462 	t2.UniversalTime();
       
   463 	
       
   464 	TTimeIntervalMicroSeconds ms = t2.MicroSecondsFrom(t1);
       
   465 	RDebug::Print(_L("Time to open calendar file: %d ms"), ms.Int64()/1000);
       
   466 	}
       
   467 
       
   468 static void TestClosedAlarmL(TDateTime aDateTime, TTimeIntervalSeconds aTimeout, TInt aInitialExpectedCount, TInt aFinalExpectedCount)
       
   469     {
       
   470     CTestApp* testApp = new (ELeave) CTestApp();
       
   471     CleanupStack::PushL(testApp);
       
   472     testApp->DoNotOpenCalendarL(TTime(aDateTime), aTimeout, aInitialExpectedCount, aFinalExpectedCount);
       
   473     CleanupStack::PopAndDestroy(testApp);
       
   474     }
       
   475 
       
   476 static void TestThisAlarmTimeL(TDateTime aDateTime)
       
   477 	{
       
   478 	CTestApp* testApp = new (ELeave) CTestApp();
       
   479 	CleanupStack::PushL(testApp);
       
   480 	testApp->DoOpenCalendarL(TTime(aDateTime));
       
   481 	CleanupStack::PopAndDestroy(testApp);
       
   482 	}
       
   483 
       
   484 static void SetTimeZoneL(const TDesC8& aTimeZoneName)
       
   485 	{
       
   486 	CCalTestLibrary* testLib = CCalTestLibrary::NewLC();
       
   487 	testLib->SetTimeZoneL(aTimeZoneName);
       
   488 	CleanupStack::PopAndDestroy(testLib);
       
   489 	}
       
   490 	
       
   491 static void DoTestsL()
       
   492 	{
       
   493 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 8,  3, 55, 0), TTimeIntervalSeconds(10), 0, 1); // appt
       
   494 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 8,  8, 55, 0), TTimeIntervalSeconds(10), 0, 1); // reminder
       
   495 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 8, 13, 55, 0), TTimeIntervalSeconds(10), 0, 1); // todo
       
   496 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 8, 18, 55, 0), TTimeIntervalSeconds(10), 0, 1); // anniv
       
   497 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 8, 23, 55, 0), TTimeIntervalSeconds(10), 0, 1); // event
       
   498 
       
   499 	TestThisAlarmTimeL(TDateTime(2005, EJanuary, 0, 8,  4, 0, 0)); // appt
       
   500 	TestThisAlarmTimeL(TDateTime(2005, EJanuary, 0, 8,  9, 0, 0)); // reminder
       
   501 	TestThisAlarmTimeL(TDateTime(2005, EJanuary, 0, 8, 14, 0, 0)); // todo
       
   502 	TestThisAlarmTimeL(TDateTime(2005, EJanuary, 0, 8, 19, 0, 0)); // anniv
       
   503 	TestThisAlarmTimeL(TDateTime(2005, EJanuary, 0, 8, 24, 0, 0)); // event
       
   504 
       
   505 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 10,  5, 55, 0), TTimeIntervalSeconds(10), 0, 1); // appt
       
   506 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 10, 10, 55, 0), TTimeIntervalSeconds(10), 0, 1); // reminder
       
   507 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 10, 15, 55, 0), TTimeIntervalSeconds(10), 0, 1); // todo
       
   508 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 10, 20, 55, 0), TTimeIntervalSeconds(10), 0, 1); // anniv
       
   509 	TestClosedAlarmL(TDateTime(2005, EJanuary, 0, 10, 25, 55, 0), TTimeIntervalSeconds(10), 0, 1); // event
       
   510 
       
   511 	TestThisAlarmTimeL(TDateTime(2005, EApril, 4, 16, 2, 0, 0));	// non-repeating appt
       
   512 	TestThisAlarmTimeL(TDateTime(2005, EApril, 22, 12, 25, 0, 0));	// non-repeating todo
       
   513 	TestThisAlarmTimeL(TDateTime(2005, EMay, 9, 9, 46, 0, 0));		// repeating appt
       
   514 	TestThisAlarmTimeL(TDateTime(2005, EJune, 21, 15, 57, 0, 0));	// repeating todo
       
   515 
       
   516 	// appt, negative alarm
       
   517 	TestThisAlarmTimeL(TDateTime(2005, EApril, 0, 9, 0, 0, 0)); 
       
   518 	// todo, negative alarm
       
   519 	TestThisAlarmTimeL(TDateTime(2005, EApril, 0, 11, 0, 0, 0)); 
       
   520 	// appt, negative alarm (floating)
       
   521 	TestThisAlarmTimeL(TDateTime(2005, EApril, 10, 9, 0, 0, 0)); 
       
   522 	// todo, negative alarm (floating)
       
   523 	TestThisAlarmTimeL(TDateTime(2005, EApril, 10, 11, 0, 0, 0)); 
       
   524 
       
   525   	// appt, repeating negative alarm
       
   526   	TestThisAlarmTimeL(TDateTime(2005, EMay, 1, 9, 0, 0, 0)); 
       
   527 	// todo, repeating negative alarm
       
   528 	TestThisAlarmTimeL(TDateTime(2005, EMay, 1, 11, 0, 0, 0)); 
       
   529 	// appt, repeating negative alarm (floating)
       
   530 	TestThisAlarmTimeL(TDateTime(2005, EMay, 11, 9, 0, 0, 0)); 
       
   531 	// todo, repeating negative alarm (floating)
       
   532 	TestThisAlarmTimeL(TDateTime(2005, EMay, 11, 11, 0, 0, 0)); 
       
   533 	
       
   534 	// Change time zone and retest the above times (6 or 7 hours earlier)
       
   535 	// Note that fixed negative alarms don't work when the time zone changes - 
       
   536 	// this is a known limitation.
       
   537 	_LIT8(KChicago, "America/Chicago");
       
   538 	SetTimeZoneL(KChicago);
       
   539 
       
   540     // non-repeating appt
       
   541 	TestThisAlarmTimeL(TDateTime(2005, EApril, 4, 10, 2, 0, 0));	
       
   542 	// non-repeating todo
       
   543 	TestThisAlarmTimeL(TDateTime(2005, EApril, 22, 6, 25, 0, 0));	
       
   544 	// repeating appt
       
   545 	TestThisAlarmTimeL(TDateTime(2005, EMay, 9, 3, 46, 0, 0));		
       
   546 	// repeating todo
       
   547 	TestThisAlarmTimeL(TDateTime(2005, EJune, 21, 9, 57, 0, 0));	
       
   548 
       
   549     // appt, negative alarm (floating)
       
   550 	TestThisAlarmTimeL(TDateTime(2005, EApril, 10, 9, 0, 0, 0)); 
       
   551     // todo, negative alarm (floating)	
       
   552 	TestThisAlarmTimeL(TDateTime(2005, EApril, 10, 11, 0, 0, 0)); 
       
   553 	// appt, repeating negative alarm (floating)
       
   554 	TestThisAlarmTimeL(TDateTime(2005, EMay, 11, 9, 0, 0, 0)); 
       
   555 	 // todo, repeating negative alarm (floating)
       
   556 	TestThisAlarmTimeL(TDateTime(2005, EMay, 11, 11, 0, 0, 0));
       
   557 	}
       
   558 	
       
   559 static TInt TestAlarms(TAny* )
       
   560 	{
       
   561 	RDebug::Print(_L("New Thread started...\n"));
       
   562 	TInt err(KErrNone);
       
   563 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   564 	CActiveScheduler* scheduler = new CActiveScheduler();
       
   565 	CActiveScheduler::Install(scheduler);
       
   566 
       
   567 	TRAP(err, DoTestsL());
       
   568 	
       
   569 	delete scheduler;
       
   570 	delete cleanup;
       
   571 	
       
   572 	RDebug::Print(_L("...Thread closed\n"));
       
   573 	return err;
       
   574 	}
       
   575 
       
   576 
       
   577 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   578  * DoTestL()
       
   579  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   580 	
       
   581 static void DoTestL()
       
   582 	{
       
   583 	test.Printf(_L("Deleting Alarmerver.ini file\n"));
       
   584 	_LIT(KIniFileName, "c:\\private\\101f5027\\AlarmServer.ini");
       
   585 		
       
   586 	TPerformanceTimer timer(test);
       
   587 	timer.Start();
       
   588 
       
   589 	// Run the test suite
       
   590 
       
   591 	RFs fs;
       
   592 	User::LeaveIfError(fs.Connect());
       
   593 	fs.Delete(KIniFileName);	//IgnoreError
       
   594 	fs.Close();
       
   595 	
       
   596 	CCalTestLibrary* testLib = CCalTestLibrary::NewLC(EFalse);
       
   597 
       
   598 	RTz tz;
       
   599 	User::LeaveIfError(tz.Connect());
       
   600 	CleanupClosePushL(tz);
       
   601 	User::LeaveIfError(tz.SetHomeTime(TTime(TDateTime(2005, EJanuary, 0, 0, 0, 0, 0))));
       
   602 	CleanupStack::PopAndDestroy(&tz); // tz.Close()
       
   603 
       
   604 	// clear all alarms
       
   605 	CTestApp::ClearAlarmServerL();
       
   606 
       
   607 	// initialise the calendar file
       
   608 	CTestApp::CreateCalendarFileL(testLib);
       
   609     CleanupStack::PopAndDestroy(testLib);
       
   610     
       
   611 	// create a new thread to test that alarms are queued correctly on startup
       
   612 	test.Printf(_L("Opening new thread to test alarms"));
       
   613 	RThread thread;
       
   614 	thread.Create(_L("test on startup"), TestAlarms, KDefaultStackSize, &User::Allocator(), NULL);
       
   615 	
       
   616     thread.Resume();
       
   617     TRequestStatus exitStatus;
       
   618     thread.Logon(exitStatus);
       
   619     User::WaitForRequest(exitStatus);
       
   620     test.Printf(_L("Thread Closed: Status=%d, ExitType=%d, ExitReason=%d)"), exitStatus.Int(), thread.ExitType(), thread.ExitReason());
       
   621     test(exitStatus.Int() == KErrNone);
       
   622     test(thread.ExitType() == EExitKill);
       
   623     test(thread.ExitReason() == KErrNone);
       
   624 
       
   625 	testLib = CCalTestLibrary::NewLC(EFalse);
       
   626 	testLib->DeleteFileL(KCalendarFile);
       
   627 	CleanupStack::PopAndDestroy(testLib);
       
   628 	    
       
   629 	timer.Stop();
       
   630 	test.Printf(_L("Done\n"));
       
   631 	// printout performance time
       
   632 	timer.PrintOut();
       
   633 	}
       
   634 
       
   635 
       
   636 /**
       
   637 
       
   638 @SYMTestCaseID     PIM-TCAL-QUEUEALARMONSTARTUP-0001
       
   639 
       
   640 */
       
   641 
       
   642 TInt E32Main()
       
   643     {
       
   644 	__UHEAP_MARK;
       
   645 
       
   646 	test.Start(_L("@SYMTESTCaseID:PIM-TCAL-QUEUEALARMONSTARTUP-0001 Calendar Interim API Alarms Queued While Building Indexes test suite"));
       
   647 
       
   648 	test.Title();
       
   649 
       
   650 	CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   651 	if (!trapCleanup)
       
   652 		{
       
   653 		return KErrNoMemory;
       
   654 		}
       
   655 
       
   656 	CActiveScheduler* scheduler = new CActiveScheduler();
       
   657 	if (!scheduler)
       
   658 		{
       
   659 		delete trapCleanup;
       
   660 		return KErrNoMemory;
       
   661 		}
       
   662 	CActiveScheduler::Install(scheduler);	
       
   663 
       
   664 	TRAPD(ret, DoTestL());
       
   665 	test(ret == KErrNone);
       
   666 	
       
   667 	delete scheduler;
       
   668 	delete trapCleanup;	
       
   669 
       
   670 	test.End();
       
   671 	test.Close();
       
   672 
       
   673 	__UHEAP_MARKEND;
       
   674 
       
   675 	return (KErrNone);
       
   676     }
       
   677