commonappservices/alarmserver/Test/TRepeatDefinitions.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     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 //
       
    15 
       
    16 #include "ASTstAlarmTest.h"
       
    17 #include "testserver.h"
       
    18 #include <tz.h>
       
    19 
       
    20 // Globals
       
    21 static TTime TheOriginalTime;
       
    22 static TTime TheInitialAlarmTime;
       
    23 static TTime TheCurrentTime;
       
    24 TBool AlarmSetInThePastTest = EFalse;
       
    25 
       
    26 // Definitions:
       
    27 
       
    28 /* These two #defines make the code a lot safer, since the enums in TDay
       
    29 	are not tied to any specific values. All code expecting specific values
       
    30 	need to use them relative to EMonday  (DoW = Day of week) */
       
    31 #define DoWNumber(x)   ((TInt)(x-EMonday))
       
    32 #define DoWFlag(x)   (1<<((TInt)(x-EMonday)))
       
    33 
       
    34 _LIT(KDateTimeFormat, "%F %*E %*N %D %Y %J:%T:%S%B");
       
    35 
       
    36 static void PrintTimeL(const TTime& aTime, const TDesC& aPrefix)
       
    37 	{
       
    38 	TBuf<30> dateTimeString;
       
    39 	aTime.FormatL(dateTimeString, KDateTimeFormat);
       
    40 	TheAlarmTest.Test().Printf(_L("%S %S\n"), &aPrefix, &dateTimeString);
       
    41 	}
       
    42 
       
    43 static void AdjustToNextWorkDay(TTime& aSetTime, const TTime& aCurrentTime)
       
    44 	{
       
    45 	TDateTime setDT=aSetTime.DateTime();
       
    46 	TDateTime currDT=aCurrentTime.DateTime();
       
    47 	setDT.SetYear(currDT.Year());
       
    48 	setDT.SetDay(0);	// ensure SetMonth cannot fail.
       
    49 	setDT.SetMonth(currDT.Month());
       
    50 	setDT.SetDay(currDT.Day());
       
    51 	aSetTime=setDT;
       
    52 	TUint workdays=TLocale().WorkDays();
       
    53 	TheAlarmTest(workdays!=0, __LINE__);
       
    54 	TUint dayFlag;
       
    55 
       
    56 
       
    57 #if defined(_DEBUG)
       
    58 		{
       
    59 		FOREVER
       
    60 			{
       
    61 			dayFlag=DoWFlag(aSetTime.DayNoInWeek());
       
    62 			if	(workdays&dayFlag && aSetTime>aCurrentTime)
       
    63 				return;
       
    64 			aSetTime+=TTimeIntervalDays(1);
       
    65 			}
       
    66 		}
       
    67 #else
       
    68 	if (AlarmSetInThePastTest)
       
    69 		{
       
    70 		//When moving forwards the system time the Next alarm time will be in the past
       
    71 		FOREVER
       
    72 			{
       
    73 			dayFlag=DoWFlag(aSetTime.DayNoInWeek());
       
    74 			if(workdays&dayFlag )
       
    75 				return;
       
    76 			aSetTime+=TTimeIntervalDays(1);
       
    77 			}
       
    78 		}
       
    79 	else
       
    80 		{
       
    81 		FOREVER
       
    82 			{
       
    83 			dayFlag=DoWFlag(aSetTime.DayNoInWeek());
       
    84 			if(workdays&dayFlag && aSetTime>aCurrentTime)
       
    85 				return;
       
    86 			aSetTime+=TTimeIntervalDays(1);
       
    87 			}
       
    88 		}
       
    89 #endif
       
    90 	}
       
    91 
       
    92 static void TestCompareTimeL(const TTime& aExpectedTime, const TTime& aAlarmTime, TInt aLineNum)
       
    93 //
       
    94 //  Compare times to required degree of precision
       
    95 //
       
    96 	{
       
    97 	// determine the difference, in seconds
       
    98 	TTimeIntervalSeconds timeDiffSeconds;
       
    99 	const TInt error = aExpectedTime.SecondsFrom(aAlarmTime, timeDiffSeconds);
       
   100 
       
   101 	TheAlarmTest(error == KErrNone, __LINE__);
       
   102 
       
   103 	// check the times are within 1min
       
   104 	// as alarms are rounded to the minute
       
   105 	const TTimeIntervalSeconds maxDiff(60);
       
   106 	const TTimeIntervalSeconds minDiff(-60);
       
   107 	if ((timeDiffSeconds > maxDiff) || (timeDiffSeconds < minDiff))
       
   108 		{
       
   109 		TheAlarmTest.Test().Printf(
       
   110 			_L("Compared times more than 1 minute different (%is) at line %u"),
       
   111 			timeDiffSeconds.Int(),
       
   112 			aLineNum);
       
   113 		TRAP_IGNORE(PrintTimeL(aExpectedTime, _L("Expected time: ")));
       
   114 		TRAP_IGNORE(PrintTimeL(aAlarmTime, _L("Alarm time: ")));
       
   115 		TheAlarmTest(EFalse, __LINE__);
       
   116 		}
       
   117 	}
       
   118 
       
   119 static void TestClockAlarmsL(const RArray<TAlarmId>& aIds)
       
   120 //
       
   121 //	We expect and TheAlarmTest.TheTest that:
       
   122 //
       
   123 //	1) Once alarm to remain unchanged at initial alarm time unless time in past
       
   124 //	2) Next24 to advance to the first 'initial alarm time' falling due after the original time
       
   125 //	3) Daily to advance to the first 'initial alarm time' falling due after the currrent time
       
   126 //	4) Weekday to advance to the 'initial alarm time' on the next weekday
       
   127 //	5) Weekly to advance to the 'initial alarm time' in the next week
       
   128 //
       
   129 	{
       
   130 	TASShdAlarm alarm;
       
   131 	TheCurrentTime.HomeTime();
       
   132 	//
       
   133 	for (TInt i=0; i<aIds.Count(); i++)
       
   134 		{
       
   135 		const TAlarmId id = aIds[i];
       
   136 		//
       
   137 		TInt r = TheAlarmTest.Session().GetAlarmDetails(id, alarm);
       
   138 		if	(r != KErrNone)
       
   139 			{
       
   140 			// Its acceptable for the first item to be missing
       
   141 			// due to a date/time change. The first item is a repeat-once
       
   142 			// alarm, scheduled 1 hour in the past (i.e one hour before the
       
   143 			// current time). As soon as the date/time changes, this alarm
       
   144 			// will be removed from the alarm queue because it is before
       
   145 			// the new date time.
       
   146 			TheAlarmTest(r == KErrNotFound && i==0, __LINE__);
       
   147 			}
       
   148 		else
       
   149 			TheAlarmTest(r == KErrNone, __LINE__);
       
   150 		//
       
   151 		if (alarm.Status() != EAlarmStatusEnabled)
       
   152 			continue;
       
   153 		//
       
   154 		TTime setTime(TheInitialAlarmTime);
       
   155 		switch(alarm.RepeatDefinition())
       
   156 			{
       
   157 		case EAlarmRepeatDefintionRepeatOnce:
       
   158 			{
       
   159 			TestCompareTimeL(TheInitialAlarmTime, alarm.NextDueTime(), __LINE__);
       
   160 			break;
       
   161 			}
       
   162 
       
   163 		case EAlarmRepeatDefintionRepeatNext24Hours:
       
   164 			{
       
   165 			// Should be be within 24 hours of the original time
       
   166 			TTimeIntervalDays daysBetweenInitialAndOriginal(TheOriginalTime.DaysFrom(TheInitialAlarmTime));
       
   167 			setTime+=daysBetweenInitialAndOriginal;
       
   168 			setTime+=TTimeIntervalHours(24);
       
   169 			TestCompareTimeL(setTime, alarm.NextDueTime(), __LINE__);
       
   170 			}
       
   171 			break;
       
   172 
       
   173 		case EAlarmRepeatDefintionRepeatDaily:
       
   174 			{
       
   175 			// Should be within next 24 hours of current time at initial time
       
   176 			TTimeIntervalDays daysBetweenInitialAndCurrent(TheCurrentTime.DaysFrom(TheInitialAlarmTime));
       
   177 			setTime+=daysBetweenInitialAndCurrent;
       
   178 
       
   179 #if!defined(_DEBUG)
       
   180 			// UREL
       
   181 			if (!(alarm.NextDueTime()<TheCurrentTime))  // Alarm not in the past
       
   182 #endif
       
   183 			setTime+=TTimeIntervalHours(24);
       
   184 			TestCompareTimeL(setTime, alarm.NextDueTime(), __LINE__);
       
   185 			}
       
   186 			break;
       
   187 
       
   188 		case EAlarmRepeatDefintionRepeatWeekly:
       
   189 			{
       
   190 			TTimeIntervalDays daysBetweenInitialAndCurrent(TheCurrentTime.DaysFrom(TheInitialAlarmTime));
       
   191 			TInt weeks=daysBetweenInitialAndCurrent.Int()/7;
       
   192 			if (weeks!=0)
       
   193 				setTime+=TTimeIntervalDays(weeks*7);
       
   194 			setTime+=TTimeIntervalDays(7);
       
   195 			TestCompareTimeL(setTime, alarm.NextDueTime(), __LINE__);
       
   196 			}
       
   197 			break;
       
   198 
       
   199 		case EAlarmRepeatDefintionRepeatWorkday:
       
   200 			{
       
   201 			AdjustToNextWorkDay(setTime,TheCurrentTime);
       
   202 			TestCompareTimeL(setTime, alarm.NextDueTime(), __LINE__);
       
   203 			}
       
   204 			break;
       
   205 
       
   206 		default:
       
   207 			TheAlarmTest(0, __LINE__);
       
   208 			break;
       
   209 			}
       
   210 		}
       
   211 	}
       
   212 
       
   213 static void TestClockUtcAlarmsL(const RArray<TAlarmId>& aIds)
       
   214 //
       
   215 //	We expect and TheAlarmTest.TheTest that:
       
   216 //
       
   217 //	1) Once alarm to remain unchanged at initial alarm time unless time in past
       
   218 //	2) Next24 to advance to the first 'initial alarm time' falling due after the original time
       
   219 //	3) Daily to advance to the first 'initial alarm time' falling due after the currrent time
       
   220 //	4) Weekday to advance to the 'initial alarm time' on the next weekday
       
   221 //	5) Weekly to advance to the 'initial alarm time' in the next week
       
   222 //
       
   223 	{
       
   224 	TASShdAlarm alarm;
       
   225 	TheCurrentTime.HomeTime();
       
   226 	//
       
   227 	for (TInt i=0; i<aIds.Count(); i++)
       
   228 		{
       
   229 		const TAlarmId id = aIds[i];
       
   230 		//
       
   231 		TInt r = TheAlarmTest.Session().GetAlarmDetails(id, alarm);
       
   232 		if	(r != KErrNone)
       
   233 			{
       
   234 			// Its acceptable for the first item to be missing
       
   235 			// due to a date/time change. The first item is a repeat-once
       
   236 			// alarm, scheduled 1 hour in the past (i.e one hour before the
       
   237 			// current time). As soon as the date/time changes, this alarm
       
   238 			// will be removed from the alarm queue because it is before
       
   239 			// the new date time.
       
   240 			TheAlarmTest(r == KErrNotFound && i==0, __LINE__);
       
   241 			}
       
   242 		else
       
   243 			TheAlarmTest(r == KErrNone, __LINE__);
       
   244 		//
       
   245 		if	(alarm.Status() != EAlarmStatusEnabled)
       
   246 			continue;
       
   247 		//
       
   248 		TTime setTime(TheInitialAlarmTime);
       
   249 		switch(alarm.RepeatDefinition())
       
   250 			{
       
   251 		case EAlarmRepeatDefintionRepeatOnce:
       
   252 			{
       
   253 			TestCompareTimeL(TheInitialAlarmTime, (alarm.NextDueTime() + User::UTCOffset()), __LINE__);
       
   254 			break;
       
   255 			}
       
   256 
       
   257 		case EAlarmRepeatDefintionRepeatNext24Hours:
       
   258 			{
       
   259 			// Should be be within 24 hours of the original time
       
   260 			TTimeIntervalDays daysBetweenInitialAndOriginal(TheOriginalTime.DaysFrom(TheInitialAlarmTime));
       
   261 			setTime+=daysBetweenInitialAndOriginal;
       
   262 			setTime+=TTimeIntervalHours(24);
       
   263 			TestCompareTimeL(setTime, (alarm.NextDueTime() + User::UTCOffset()), __LINE__);
       
   264 			}
       
   265 			break;
       
   266 
       
   267 		case EAlarmRepeatDefintionRepeatDaily:
       
   268 			{
       
   269 			// Should be within next 24 hours of current time at initial time
       
   270 			TTimeIntervalDays daysBetweenInitialAndCurrent(TheCurrentTime.DaysFrom(TheInitialAlarmTime));
       
   271 			setTime+=daysBetweenInitialAndCurrent;
       
   272 
       
   273 #if!defined(_DEBUG)
       
   274 			// UREL
       
   275 			if (!((alarm.NextDueTime() + User::UTCOffset()) < TheCurrentTime ))  // Alarm not in the past
       
   276 #endif
       
   277 			setTime+=TTimeIntervalHours(24);
       
   278 			TestCompareTimeL(setTime, (alarm.NextDueTime() + User::UTCOffset()), __LINE__);
       
   279 			}
       
   280 			break;
       
   281 
       
   282 		case EAlarmRepeatDefintionRepeatWeekly:
       
   283 			{
       
   284 			TTimeIntervalDays daysBetweenInitialAndCurrent(TheCurrentTime.DaysFrom(TheInitialAlarmTime));
       
   285 			TInt weeks=daysBetweenInitialAndCurrent.Int()/7;
       
   286 			if (weeks!=0)
       
   287 				setTime+=TTimeIntervalDays(weeks*7);
       
   288 			setTime+=TTimeIntervalDays(7);
       
   289 			TestCompareTimeL(setTime, (alarm.NextDueTime() + User::UTCOffset()), __LINE__);
       
   290 			}
       
   291 			break;
       
   292 
       
   293 		case EAlarmRepeatDefintionRepeatWorkday:
       
   294 			{
       
   295 			AdjustToNextWorkDay(setTime,TheCurrentTime);
       
   296 			TestCompareTimeL(setTime, (alarm.NextDueTime() + User::UTCOffset()), __LINE__);
       
   297 			}
       
   298 			break;
       
   299 
       
   300 		default:
       
   301 			TheAlarmTest(0, __LINE__);
       
   302 			break;
       
   303 			}
       
   304 		}
       
   305 	}
       
   306 
       
   307 
       
   308 static void TestAdvanceTimeToBeforeNextAlarmsL()
       
   309 //
       
   310 //	Advance time by 22 hours, just before next alarms.
       
   311 //
       
   312 	{
       
   313 	TTime newTime=TheOriginalTime;
       
   314 	newTime+=TTimeIntervalHours(22);
       
   315 
       
   316     RPIMTestServer serv;
       
   317     User::LeaveIfError(serv.Connect());
       
   318     serv.SetHomeTime(newTime);
       
   319     serv.Close();
       
   320 
       
   321 	User::After(KTimeToWait);
       
   322 	}
       
   323 
       
   324 static void TestAdvanceTimeAfterAlarmsL()
       
   325 //
       
   326 //	Advance time by a further 24 hours past daily, next24, and weekday
       
   327 //	alarms if tomorrow is a weekday.
       
   328 //
       
   329 	{
       
   330 	TTime newTime=TheOriginalTime;
       
   331 	newTime+=TTimeIntervalHours(24);
       
   332 
       
   333     RPIMTestServer serv;
       
   334     User::LeaveIfError(serv.Connect());
       
   335     serv.SetHomeTime(newTime);
       
   336     serv.Close();
       
   337 
       
   338 	User::After(KTimeToWait);
       
   339 	}
       
   340 
       
   341 static void TestSetClockAlarmsL(TInt aAlarmOffsetHours,TInt aAlarmOffsetDays)
       
   342 //
       
   343 //	Set five alarms with each having a different alarm repeat mode.
       
   344 //	Each is set with the same past time an hour ago.
       
   345 //
       
   346 	{
       
   347 			
       
   348 	TheAlarmTest.Test().Next(_L("Clock alarms are set properly"));
       
   349 	TInt n=TheAlarmTest.Session().NumberOfAlarmsActiveInQueue();
       
   350 	TheAlarmTest(n==0, __LINE__);
       
   351 
       
   352 	TheCurrentTime=TheOriginalTime;
       
   353 	TheInitialAlarmTime=TheOriginalTime;
       
   354 
       
   355 	if (aAlarmOffsetHours!=0)
       
   356 		TheInitialAlarmTime+=TTimeIntervalHours(aAlarmOffsetHours);
       
   357 	if (aAlarmOffsetDays!=0)
       
   358 		TheInitialAlarmTime+=TTimeIntervalDays(aAlarmOffsetDays);
       
   359 	//
       
   360 	TASShdAlarm alarm;
       
   361 	RArray<TInt> array;
       
   362 	//
       
   363 	for (TInt i=EAlarmRepeatDefintionRepeatOnce; i<=EAlarmRepeatDefintionRepeatWeekly; i++)
       
   364 		{
       
   365 		alarm.Reset();
       
   366 		alarm.Message().Num(i);
       
   367 		alarm.NextDueTime() = TheInitialAlarmTime;
       
   368 		alarm.RepeatDefinition() = TAlarmRepeatDefinition(i);
       
   369 		const TInt error = TheAlarmTest.Session().AlarmAdd(alarm);
       
   370 		TheAlarmTest(error == KErrNone, __LINE__);
       
   371 		array.AppendL(alarm.Id());
       
   372 		}
       
   373 
       
   374 	// Check that the alarms were all queued okay
       
   375 	TestClockAlarmsL(array);
       
   376 
       
   377 	
       
   378 	TheAlarmTest.Test().Next(_L("Time adjusted to before repeats +22hr"));
       
   379 	//	We expect and test that:
       
   380 	//
       
   381 	//	1) Once alarm to remain unchanged at initial time
       
   382 	//	2) Next24 to remain at 24 hours from initial time
       
   383 	//	3) Daily to remain at 24 hours from initial time
       
   384 	//	4) Weekday to remain at next weekday from initial time
       
   385 	//	5) Weekly to remain at 7 days from initial time
       
   386 	//
       
   387 	TestAdvanceTimeToBeforeNextAlarmsL();
       
   388 	TestClockAlarmsL(array);
       
   389 
       
   390 	
       
   391 
       
   392 	TheAlarmTest.Test().Next(_L("Time adjusted to after repeats +24hr"));
       
   393 	//	We expect and test that;
       
   394 	//
       
   395 	//	1) Once alarm to remain unchanged at initial time
       
   396 	//	2) Next24 to remain at 24 hours from initial time
       
   397 	//	3) Daily to advance to 48 hours in udeb and 24 hours in urel from initial time.
       
   398 	//	4) Weekday to advance two second weekday after
       
   399 	//	   initial time if day after initial time was a weekday
       
   400 	//	5) Weekly to remain at 7 days from initial time
       
   401 	//
       
   402 	AlarmSetInThePastTest = ETrue;
       
   403 	TestAdvanceTimeAfterAlarmsL();
       
   404 	TestClockAlarmsL(array);
       
   405 	AlarmSetInThePastTest = EFalse;
       
   406 
       
   407 	// Tidy up and finish
       
   408 	// Delete the alarm currently tested
       
   409 	while(array.Count())
       
   410 		{
       
   411 		TheAlarmTest(array[array.Count()-1]);
       
   412 		const TInt error = TheAlarmTest.Session().AlarmDelete(array[array.Count()-1]);
       
   413 		TheAlarmTest(error == KErrNone || error == KErrNotFound);
       
   414 		array.Remove(array.Count()-1);
       
   415 		}
       
   416 	TheAlarmTest(TheAlarmTest.Session().NumberOfAlarmsActiveInQueue() == 0);
       
   417 	TheAlarmTest.TestClearStoreL();
       
   418 	array.Close();
       
   419 	}
       
   420 
       
   421 	
       
   422 static void testNext24hours()
       
   423 //
       
   424 //	Test that an alarm set for 1 second ago gets set for tomorrow
       
   425 //
       
   426 	{
       
   427 	TheAlarmTest.Test().Next(_L("Next 24 hours"));
       
   428 	TInt n=TheAlarmTest.Session().NumberOfAlarmsActiveInQueue();
       
   429 	TheAlarmTest(n==0, __LINE__);
       
   430 
       
   431 	TASShdAlarm alarm;
       
   432 	TTime timeNow;
       
   433 	timeNow.HomeTime();
       
   434 	alarm.NextDueTime() = timeNow-TTimeIntervalSeconds(1);
       
   435 	alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatNext24Hours;
       
   436 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   437 	TheAlarmTest(alarm.Id() != KNullAlarmId, __LINE__);
       
   438 	TheAlarmTest(alarm.State() == EAlarmStateQueued, __LINE__);
       
   439 	TheAlarmTest(alarm.Status() == EAlarmStatusEnabled, __LINE__);
       
   440 	//
       
   441 	TASShdAlarm alarm2;
       
   442 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(alarm.Id(), alarm2) == KErrNone, __LINE__);
       
   443 	TheAlarmTest(alarm2.NextDueTime() == alarm.NextDueTime(), __LINE__);
       
   444 	TheAlarmTest(alarm2.NextDueTime() > timeNow, __LINE__);
       
   445 	TInt error = TheAlarmTest.Session().AlarmDelete(alarm.Id());
       
   446 	TheAlarmTest(error == KErrNone, __LINE__);
       
   447 	}
       
   448 
       
   449 static void testSetClockUtcAlarmsL(TInt aAlarmOffsetHours,TInt aAlarmOffsetDays)
       
   450 //
       
   451 //	Set five alarms with each having a different alarm repeat mode.
       
   452 //	Each is set with the same past time an hour ago.
       
   453 //
       
   454 	{
       
   455 			
       
   456 	TheAlarmTest.Test().Next(_L("Clock UTC alarms are set properly"));
       
   457 	TInt n=TheAlarmTest.Session().NumberOfAlarmsActiveInQueue();
       
   458 	TheAlarmTest(n==0, __LINE__);
       
   459 
       
   460 	TheCurrentTime=TheOriginalTime;
       
   461 	TheInitialAlarmTime=TheOriginalTime;
       
   462 
       
   463 	if (aAlarmOffsetHours!=0)
       
   464 		TheInitialAlarmTime+=TTimeIntervalHours(aAlarmOffsetHours);
       
   465 	if (aAlarmOffsetDays!=0)
       
   466 		TheInitialAlarmTime+=TTimeIntervalDays(aAlarmOffsetDays);
       
   467 	//
       
   468 	TASShdAlarm alarm;
       
   469 	RArray<TInt> array;
       
   470 	//
       
   471 	for (TInt i=EAlarmRepeatDefintionRepeatOnce; i<=EAlarmRepeatDefintionRepeatWeekly; i++)
       
   472 		{
       
   473 		alarm.Reset();
       
   474 		TAlarmMessage m = alarm.Message();
       
   475 		m.Num(i);
       
   476 		alarm.Message()=m;
       
   477 		alarm.SetUtcNextDueTime(TheInitialAlarmTime - User::UTCOffset());
       
   478 		alarm.RepeatDefinition()=TAlarmRepeatDefinition(i);
       
   479 		const TInt error = TheAlarmTest.Session().AlarmAdd(alarm);
       
   480 		TheAlarmTest(error == KErrNone, __LINE__);
       
   481 		array.AppendL(alarm.Id());
       
   482 		}
       
   483 
       
   484 	// Check that the alarms were all queued okay
       
   485 	TestClockUtcAlarmsL(array);
       
   486 
       
   487 		
       
   488 	TheAlarmTest.Test().Next(_L("Time adjusted to before repeats +22hr"));
       
   489 	//	We expect and test that:
       
   490 	//
       
   491 	//	1) Once alarm to remain unchanged at initial time
       
   492 	//	2) Next24 to remain at 24 hours from initial time
       
   493 	//	3) Daily to remain at 24 hours from initial time
       
   494 	//	4) Weekday to remain at next weekday from initial time
       
   495 	//	5) Weekly to remain at 7 days from initial time
       
   496 	//
       
   497 	TestAdvanceTimeToBeforeNextAlarmsL();
       
   498 	TestClockUtcAlarmsL(array);
       
   499 
       
   500 	
       
   501 	TheAlarmTest.Test().Next(_L("Time adjusted to after repeats +24hr"));
       
   502 	//	We expect and test that;
       
   503 	//
       
   504 	//	1) Once alarm to remain unchanged at initial time
       
   505 	//	2) Next24 to remain at 24 hours from initial time
       
   506 	//	3) Daily to advance to 48 hours in udeb and 24 hours in urel from initial time.
       
   507 	//	4) Weekday to advance two second weekday after
       
   508 	//	   initial time if day after initial time was a weekday
       
   509 	//	5) Weekly to remain at 7 days from initial time
       
   510 	//
       
   511 	AlarmSetInThePastTest = ETrue;
       
   512 	TestAdvanceTimeAfterAlarmsL();
       
   513 	TestClockUtcAlarmsL(array);
       
   514 	AlarmSetInThePastTest = EFalse;
       
   515 
       
   516 	// Tidy up and finish
       
   517 	// Delete the alarm currently tested
       
   518 	while(array.Count())
       
   519 		{
       
   520 		TheAlarmTest(array[array.Count()-1]);
       
   521 		const TInt error = TheAlarmTest.Session().AlarmDelete(array[array.Count()-1]);
       
   522 		TheAlarmTest(error == KErrNone || error == KErrNotFound);
       
   523 		array.Remove(array.Count()-1);
       
   524 		}
       
   525 	TheAlarmTest(TheAlarmTest.Session().NumberOfAlarmsActiveInQueue() == 0);
       
   526 	TheAlarmTest.TestClearStoreL();
       
   527 	array.Close();
       
   528 	}
       
   529 
       
   530 	
       
   531 static void testNext24hoursUtc()
       
   532 //
       
   533 //	Test that an alarm set for 1 second ago gets set for tomorrow
       
   534 //
       
   535 	{
       
   536 	TheAlarmTest.Test().Next(_L("Next 24 hours UTC"));
       
   537 	TInt n=TheAlarmTest.Session().NumberOfAlarmsActiveInQueue();
       
   538 	TheAlarmTest(n==0, __LINE__);
       
   539 
       
   540 	TASShdAlarm alarm;
       
   541 	TTime timeNow;
       
   542 	timeNow.UniversalTime();
       
   543 	alarm.SetUtcNextDueTime(timeNow-TTimeIntervalSeconds(1));
       
   544 	alarm.RepeatDefinition()=EAlarmRepeatDefintionRepeatNext24Hours;
       
   545 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   546 	TheAlarmTest(alarm.Id() != KNullAlarmId, __LINE__);
       
   547 	TheAlarmTest(alarm.State() == EAlarmStateQueued, __LINE__);
       
   548 	TheAlarmTest(alarm.Status() == EAlarmStatusEnabled, __LINE__);
       
   549 	//
       
   550 	TASShdAlarm alarm2;
       
   551 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(alarm.Id(), alarm2) == KErrNone, __LINE__);
       
   552 	TheAlarmTest(alarm2.NextDueTime() == alarm.NextDueTime(), __LINE__);
       
   553 	TheAlarmTest(alarm2.NextDueTime() > timeNow, __LINE__);
       
   554 	TInt error = TheAlarmTest.Session().AlarmDelete(alarm.Id());
       
   555 	TheAlarmTest(error == KErrNone, __LINE__);
       
   556 	}
       
   557 
       
   558 	
       
   559 static void testNextWorkdayAlarmTimePositiveOffsetL()
       
   560 //
       
   561 //  Test the next workday alarm is calculated correctly for
       
   562 //  when the UTC and Local times of the alarms are on different days.
       
   563 //  for a positive UTC offset
       
   564 //
       
   565 	{
       
   566 	TheAlarmTest.Test().Next(_L("Next due workday alarm when UTc and local current time are on different days (with positive UTC offset)"));
       
   567 
       
   568 	RTz tz;
       
   569 	CleanupClosePushL(tz);
       
   570 	User::LeaveIfError(tz.Connect());
       
   571 
       
   572 	// Store the current time zone
       
   573 	CTzId* storedTzId = tz.GetTimeZoneIdL();
       
   574 	CleanupStack::PushL(storedTzId);
       
   575 
       
   576 	// Set the current local time to be 10:58am
       
   577 	// in a time zone that is GMT+11
       
   578 	TTime newSystemTimeUTC(TDateTime(2007, EFebruary, 16, 23, 58, 0, 0));
       
   579 	const TInt KNumberOfMinutesInElevenHours(660);
       
   580 	tz.SetUnknownZoneTimeL(newSystemTimeUTC, KNumberOfMinutesInElevenHours);
       
   581 
       
   582 	// Add a workday alarm for 05:00am local time
       
   583 	const TTime KAlarmTimeLocal(TDateTime(2000, EJanuary, 0, 5, 0, 0, 0));
       
   584 	TASShdAlarm alarm;
       
   585 	alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatWorkday;
       
   586 	alarm.NextDueTime() = KAlarmTimeLocal;
       
   587 	User::LeaveIfError(TheAlarmTest.Session().AlarmAdd(alarm));
       
   588 
       
   589 	// Test that the next due alarm is on Monday
       
   590 	const TTime KNextDueTimeLocal = alarm.NextDueTime();
       
   591 	const TTime KExpectedNextDueTimeLocal(TDateTime(2007, EFebruary, 18, 5, 0, 0, 0));
       
   592 	TheAlarmTest(KNextDueTimeLocal == KExpectedNextDueTimeLocal, __LINE__);
       
   593 
       
   594 	// Set the system local time to be just after 11:00am
       
   595 	// This means that UTC and local system times are on the same day.
       
   596 	newSystemTimeUTC += TTimeIntervalMinutes(3);
       
   597 	tz.SetUnknownZoneTimeL(newSystemTimeUTC, KNumberOfMinutesInElevenHours);
       
   598 
       
   599 	// Test that the next due alarm is still on Monday
       
   600 	User::LeaveIfError(TheAlarmTest.Session().GetAlarmDetails(alarm.Id(), alarm));
       
   601 	const TTime KNextDueTimeLocal2 = alarm.NextDueTime();
       
   602 	TheAlarmTest(KNextDueTimeLocal == KNextDueTimeLocal2, __LINE__);
       
   603 
       
   604 	User::LeaveIfError(TheAlarmTest.Session().AlarmDelete(alarm.Id()));
       
   605 
       
   606 	// Restore the stored time zone
       
   607 	tz.SetTimeZoneL(*storedTzId);
       
   608 
       
   609 	CleanupStack::PopAndDestroy(storedTzId);
       
   610 	CleanupStack::PopAndDestroy(&tz);
       
   611 	}
       
   612 
       
   613 
       
   614 static void testNextWorkdayAlarmTimeNegativeOffsetL()
       
   615 //
       
   616 //  Test the next workday alarm is calculated correctly for
       
   617 //  when the UTC and Local times of the alarms are on different days.
       
   618 //  for a negative UTC offset
       
   619 //
       
   620 	{
       
   621 	TheAlarmTest.Test().Next(_L("Next due workday alarm when UTc and local current time are on different days (with negative UTC offset)"));
       
   622 
       
   623 	RTz tz;
       
   624 	CleanupClosePushL(tz);
       
   625 	User::LeaveIfError(tz.Connect());
       
   626 
       
   627 	// Store the current time zone
       
   628 	CTzId* storedTzId = tz.GetTimeZoneIdL();
       
   629 	CleanupStack::PushL(storedTzId);
       
   630 
       
   631 	// Set the current local time to be 01:02pm
       
   632 	// in a time zone that is GMT-11
       
   633 	TTime newSystemTimeUTC(TDateTime(2007, EFebruary, 18, 0, 02, 0, 0));
       
   634 	const TInt KNumberOfMinutesInNegativeElevenHours(-660);
       
   635 	tz.SetUnknownZoneTimeL(newSystemTimeUTC, KNumberOfMinutesInNegativeElevenHours);
       
   636 
       
   637 	// Add a workday alarm for 06:00pm local time
       
   638 	const TTime KAlarmTimeLocal(TDateTime(2000, EJanuary, 0, 18, 0, 0, 0));
       
   639 	TASShdAlarm alarm;
       
   640 	alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatWorkday;
       
   641 	alarm.NextDueTime() = KAlarmTimeLocal;
       
   642 	User::LeaveIfError(TheAlarmTest.Session().AlarmAdd(alarm));
       
   643 	// Test that the next due alarm is on Monday
       
   644 	const TTime KNextDueTimeLocal = alarm.NextDueTime();
       
   645 	const TTime KExpectedNextDueTimeLocal(TDateTime(2007, EFebruary, 18, 18, 0, 0, 0));
       
   646 	TheAlarmTest(KNextDueTimeLocal == KExpectedNextDueTimeLocal, __LINE__);
       
   647 
       
   648 	// Set the system local time to be just before 01:00pm
       
   649 	// This means that UTC and local system times are on the same day.
       
   650 	newSystemTimeUTC -= TTimeIntervalMinutes(4);
       
   651 	tz.SetUnknownZoneTimeL(newSystemTimeUTC, KNumberOfMinutesInNegativeElevenHours);
       
   652 
       
   653 	// Test that the next due alarm is still on Monday
       
   654 	User::LeaveIfError(TheAlarmTest.Session().GetAlarmDetails(alarm.Id(), alarm));
       
   655 	const TTime KNextDueTimeLocal2 = alarm.NextDueTime();
       
   656 	TheAlarmTest(KNextDueTimeLocal == KNextDueTimeLocal2, __LINE__);
       
   657 
       
   658 	User::LeaveIfError(TheAlarmTest.Session().AlarmDelete(alarm.Id()));
       
   659 
       
   660 	// Restore the stored time zone
       
   661 	tz.SetTimeZoneL(*storedTzId);
       
   662 
       
   663 	CleanupStack::PopAndDestroy(storedTzId);
       
   664 	CleanupStack::PopAndDestroy(&tz);
       
   665 	}
       
   666 
       
   667 /**
       
   668 Create an alarm that repeats on given days of the week but 
       
   669 don’t select any days. Try to update an alarm that repeats
       
   670 on given days of the week so that no days are selected.
       
   671 
       
   672 @SYMTestCaseID	PIM-APPSRV-ALARMSERVER-WEEKLYGIVENDAYS-0003
       
   673 					
       
   674 @SYMTestCaseDesc	To verify that it is not possible to 
       
   675 					create a repeating alarm that doesn’t
       
   676 					expire on any days of the week.
       
   677 					
       
   678 					To verify that it is not possible to
       
   679 					update a repeating alarm so that it 
       
   680 					doesn’t expire on any days of the week.    
       
   681 
       
   682 @SYMTestActions
       
   683 For PIM-APPSRV-ALARMSERVER-WEEKLYGIVENDAYS-0003:    
       
   684 1.	Create an alarm and call SetAlarmDays with 0 as argument
       
   685  	(this means no days are selected).
       
   686 2. 	Try to add this alarm using RAsCliSession::AddAlarm. [1]
       
   687 
       
   688 For PIM-APPSRV-ALARMSERVER-WEEKLYGIVENDAYS-0004:
       
   689 1. Create an alarm that repeats on Saturday and Sunday.
       
   690 2. Try to update this alarm so that no days are selected. [2]
       
   691 
       
   692 @SYMTestExpectedResults [1] The AddAlarm function returns KErrArgument.
       
   693 						[2] The update function returns KErrArgument.
       
   694 
       
   695 @SYMTestType                CT
       
   696 @SYMTestPriority            1
       
   697 */
       
   698 void TestDailyOnGivenDaysL()
       
   699 	{
       
   700 #ifdef SYMBIAN_ALARM_REPEAT_EXTENSIONS
       
   701 	TheAlarmTest.Test().Next(_L("@SYMTestCaseID:PIM-APPSRV-ALARMSERVER-WEEKLYGIVENDAYS-0003  Test 'Daily On Given Days' Repeat Definition"));
       
   702 
       
   703 	// Precondition - there must be no active alarms in the alarm queue.
       
   704 	TInt numActiveAlarms = TheAlarmTest.Session().NumberOfAlarmsActiveInQueue();
       
   705 	TheAlarmTest(numActiveAlarms == 0, __LINE__);
       
   706 
       
   707 	TASShdAlarm alarm;
       
   708 	
       
   709 	// Verify that attempting to set the active alarm days to no days returns
       
   710 	// the appropriate error code.
       
   711 	TheAlarmTest(alarm.SetAlarmDays(0) == KErrArgument, __LINE__);
       
   712 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrArgument, __LINE__);
       
   713 
       
   714 	// Verify that attempting to set the active alarm days to an invalid day
       
   715 	// returns the appropriate error code.
       
   716 	TheAlarmTest(alarm.SetAlarmDays(EAlarmDaySunday << 1) == KErrArgument, __LINE__);
       
   717 
       
   718 	// Verify that active days of the week can be set individually.
       
   719 	TheAlarmTest(alarm.SetAlarmDays(EAlarmDayMonday) == KErrNone, __LINE__);
       
   720 	TheAlarmTest(alarm.AlarmDays() == EAlarmDayMonday, __LINE__);
       
   721 	
       
   722 	// Verify that active days of the week can be combined.
       
   723 	TheAlarmTest(alarm.SetAlarmDays(EAlarmDaySaturday | EAlarmDaySunday) == KErrNone, __LINE__);
       
   724 	TheAlarmTest(alarm.AlarmDays() == EAlarmDaySaturday | EAlarmDaySunday, __LINE__);
       
   725 	
       
   726 	// Set alarm to expire 1s from now.
       
   727 	TTime timeNow;
       
   728 	timeNow.HomeTime();
       
   729 	alarm.NextDueTime() = timeNow - TTimeIntervalSeconds(1);
       
   730 		
       
   731 	// Set alarm repeat defintion.
       
   732 	alarm.RepeatDefinition() = EAlarmRepeatDefinitionRepeatDailyOnGivenDays;
       
   733 
       
   734 	// Add alarm to the Alarm Server.
       
   735 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   736 		
       
   737 	// For an existing alarm, verify that attempting to update the active alarm
       
   738 	// days to no days returns the appropriate error code.
       
   739 	TheAlarmTest(TheAlarmTest.Session().SetAlarmDays(alarm.Id(), 0) == KErrArgument, __LINE__);
       
   740 
       
   741 	// For an existing alarm, verify that active days of the week can be set.
       
   742 	TheAlarmTest(TheAlarmTest.Session().SetAlarmDays(alarm.Id(), EAlarmDayMonday) == KErrNone, __LINE__);
       
   743 	TUint8 alarmDays;
       
   744 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDays(alarm.Id(), alarmDays) == KErrNone, __LINE__);
       
   745 	TheAlarmTest(alarmDays == EAlarmDayMonday, __LINE__);
       
   746 		
       
   747 	TheAlarmTest(alarm.Id() != KNullAlarmId, __LINE__);
       
   748 	TheAlarmTest(alarm.State() == EAlarmStateQueued, __LINE__);
       
   749 	TheAlarmTest(alarm.Status() == EAlarmStatusEnabled, __LINE__);
       
   750 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(alarm.Id()) == KErrNone, __LINE__);
       
   751 #endif
       
   752 	}
       
   753 
       
   754 static void DoTestsL()
       
   755 	{
       
   756 	TTime t;
       
   757 	t.HomeTime();
       
   758 	PrintTimeL(t,_L("Starting tests at: "));
       
   759 
       
   760 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
   761 	CleanupStack::PushL(scheduler);
       
   762 	CActiveScheduler::Install(scheduler);
       
   763 
       
   764 	TheAlarmTest.TestStartServers();
       
   765 	TheAlarmTest(TheAlarmTest.Session().Connect()==KErrNone);
       
   766 	TheAlarmTest.TestClearStoreL();
       
   767 
       
   768 	TestDailyOnGivenDaysL();
       
   769 	
       
   770 	//
       
   771 	testNext24hours();
       
   772 	//
       
   773 	TUint workdays=DoWFlag(EMonday)|DoWFlag(ETuesday)|DoWFlag(EWednesday)|DoWFlag(EThursday)|DoWFlag(EFriday);
       
   774 	TLocale locale;
       
   775 	locale.SetWorkDays(workdays);
       
   776 	locale.Set();
       
   777 	User::After(KTimeToWait);
       
   778 
       
   779 #if defined(_DEBUG)
       
   780 	TheAlarmTest.Session().__DbgPreventUserNotify(ETrue);
       
   781 #endif
       
   782 	//
       
   783 	TheOriginalTime.HomeTime();
       
   784 	
       
   785 	
       
   786 	TheAlarmTest.Test().Next(_L("Alarm 1 hour before current time"));
       
   787 	TestSetClockAlarmsL(-1,  0);
       
   788 	//
       
   789     RPIMTestServer serv;
       
   790     User::LeaveIfError(serv.Connect());
       
   791     serv.SetHomeTime(TheOriginalTime);
       
   792 
       
   793 	TheAlarmTest.Test().Next(_L("Alarm 1 hour, 1 day before current time"));
       
   794 	TestSetClockAlarmsL(-1, -1);
       
   795 	//
       
   796     serv.SetHomeTime(TheOriginalTime);
       
   797     serv.Close();
       
   798 	
       
   799 	TheAlarmTest.Test().Next(_L("Alarm 1 hour, 8 days before current time"));
       
   800 	TestSetClockAlarmsL(-1, -8);
       
   801 	//
       
   802 	TheAlarmTest.TestClearStoreL();
       
   803 
       
   804 	//
       
   805 	testNext24hoursUtc();
       
   806 	//
       
   807 
       
   808 	workdays=DoWFlag(EMonday)|DoWFlag(ETuesday)|DoWFlag(EWednesday)|DoWFlag(EThursday)|DoWFlag(EFriday);
       
   809 	locale.Refresh();
       
   810 	locale.SetWorkDays(workdays);
       
   811 	locale.Set();
       
   812 	User::After(KTimeToWait);
       
   813 
       
   814 #if defined(_DEBUG)
       
   815 	TheAlarmTest.Session().__DbgPreventUserNotify(ETrue);
       
   816 #endif
       
   817 	//
       
   818 	TheOriginalTime.HomeTime();
       
   819 	
       
   820 	TheAlarmTest.Test().Next(_L("UTC Alarm 1 hour before current time"));
       
   821 	testSetClockUtcAlarmsL(-1,  0);
       
   822 	//
       
   823     RPIMTestServer serv2;
       
   824     User::LeaveIfError(serv2.Connect());
       
   825     serv2.SetHomeTime(TheOriginalTime);
       
   826 	
       
   827 	TheAlarmTest.Test().Next(_L("UTC Alarm 1 hour, 1 day before current time"));
       
   828 	testSetClockUtcAlarmsL(-1, -1);
       
   829 	//
       
   830     serv2.SetHomeTime(TheOriginalTime);
       
   831     serv2.Close();
       
   832 
       
   833 	
       
   834 	TheAlarmTest.Test().Next(_L("UTC Alarm 1 hour, 8 days before current time"));
       
   835 	testSetClockUtcAlarmsL(-1, -8);
       
   836 	//
       
   837 	TheAlarmTest.TestClearStoreL();
       
   838 
       
   839 	testNextWorkdayAlarmTimePositiveOffsetL();
       
   840 	testNextWorkdayAlarmTimeNegativeOffsetL();
       
   841 
       
   842 	CleanupStack::PopAndDestroy(scheduler);
       
   843 	}
       
   844 
       
   845 /**
       
   846  @SYMTestCaseID PIM-TREPEATDEFINITIONS-0001
       
   847  */
       
   848 	
       
   849 GLDEF_C TInt E32Main()
       
   850     {
       
   851     TInt ret = KErrNone;
       
   852 	TTime t;
       
   853 	t.HomeTime();
       
   854 	TRAP_IGNORE(PrintTimeL(t,_L("Starting test at: ")));
       
   855 
       
   856 	// Begin test
       
   857 	TheAlarmTest.Title();
       
   858 	TheAlarmTest.Start(_L("@SYMTestCaseID PIM-TREPEATDEFINITIONS-0001"));
       
   859 
       
   860 	__UHEAP_MARK;
       
   861 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   862 	TRAP_IGNORE(DoTestsL());
       
   863 	delete cleanup;
       
   864 	__UHEAP_MARKEND;
       
   865 
       
   866 	// Postcondition - there must be no active alarms in the alarm queue.
       
   867 	TInt numActiveAlarms = TheAlarmTest.Session().NumberOfAlarmsActiveInQueue();
       
   868 	TheAlarmTest(numActiveAlarms == 0, __LINE__);
       
   869 
       
   870 	TheAlarmTest.Session().Close();
       
   871 	TRAP(ret,TheAlarmTest.EndL());
       
   872 
       
   873 	TheAlarmTest.Test().Close();
       
   874 	ASTstAlarmTest::Close();
       
   875     	
       
   876 	return ret;
       
   877 	}