commonappservices/alarmserver/Test/TStartupExpire.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 
       
    19 #if defined(__WINS__)
       
    20 #include "consoleantestclient.h" //for RASAltClientSession
       
    21 // Globals
       
    22 static RANTestClient TheAlertSrv;
       
    23 #endif
       
    24 TLocale locale;
       
    25 //
       
    26 // Test setting an alarm, closing down the alarm server,
       
    27 // re-starting the server and seeing the alarm expire if the
       
    28 // re-start is within 59 seconds of the intended expiry
       
    29 //
       
    30 
       
    31 // Literal constants
       
    32 _LIT(KFormatTime,"%H:%T:%S.   ");
       
    33 _LIT(KSeparator,": ");
       
    34 _LIT(KCarriageReturn,"\n\r");
       
    35 
       
    36 #if defined(_DEBUG) && defined(__WINS__)
       
    37 static TBool IsNotifying()
       
    38 	{
       
    39 	User::After(1000000);
       
    40 	return TheAlertSrv.IsNotifying();
       
    41 	}
       
    42 #endif
       
    43 
       
    44 static void closeServers()
       
    45 	{
       
    46 	const TInt r = TheAlarmTest.Session().Connect();
       
    47 	TheAlarmTest(r==KErrNone, __LINE__);
       
    48 	TheAlarmTest.Test().Printf(_L("Server shut down\r\n"));
       
    49 #if !defined(_DEBUG)
       
    50 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A DEBUG TEST ONLY\r\n"));
       
    51 #else
       
    52 #ifdef __WINS__
       
    53 	TheAlarmTest.Test().Printf(_L("now closing server...\r\n"));
       
    54 	TheAlarmTest.Session().__DbgShutDownServer();
       
    55     TheAlarmTest.Session().Close();
       
    56 #endif
       
    57 #endif
       
    58 	}
       
    59 
       
    60 static void logTimeL(const TDesC& aText,const TTime& aTime)
       
    61 	{
       
    62 	TBuf<128> timeTxt;
       
    63 	aTime.FormatL(timeTxt,KFormatTime);
       
    64 	TheAlarmTest.Test().Printf(aText);
       
    65 	TheAlarmTest.Test().Printf(KSeparator);
       
    66 	TheAlarmTest.Test().Printf(timeTxt);
       
    67 	TheAlarmTest.Test().Printf(KCarriageReturn);
       
    68 	}
       
    69 
       
    70 static void logTimeSameLineL(const TDesC& aText,const TTime& aTime)
       
    71 	{
       
    72 	TPoint cursor=TheAlarmTest.Test().Console()->CursorPos();
       
    73 	cursor.iX=0;
       
    74 	TheAlarmTest.Test().Console()->SetCursorPosAbs(cursor);
       
    75 	TheAlarmTest.Test().Console()->ClearToEndOfLine();
       
    76 	TBuf<128> timeTxt;
       
    77 	aTime.FormatL(timeTxt,KFormatTime);
       
    78 	TheAlarmTest.Test().Printf(aText);
       
    79 	TheAlarmTest.Test().Printf(KSeparator);
       
    80 	TheAlarmTest.Test().Printf(timeTxt);
       
    81 	}
       
    82 
       
    83 void JumpJustBeforeL(const TTime& aDueTime)
       
    84 	{
       
    85 	TTime jumpTo = aDueTime - TTimeIntervalSeconds(5);
       
    86 
       
    87     RPIMTestServer serv;
       
    88     User::LeaveIfError(serv.Connect());
       
    89     serv.SetHomeTime(jumpTo);
       
    90     // wait for time change notifications to occur.
       
    91     User::After(10000);
       
    92     // set again to ensure time value is correct when update occurs.
       
    93     serv.SetHomeTime(jumpTo);
       
    94     serv.Close();
       
    95 
       
    96 	logTimeSameLineL(_L("System time is set to: "), jumpTo);
       
    97 	TheAlarmTest.Test().Printf(KCarriageReturn);
       
    98 
       
    99 	jumpTo.HomeTime();
       
   100 	}
       
   101 
       
   102 static void tickUntilTimeL(const TTime& aStopTime)
       
   103 	{
       
   104 	TheAlarmTest.Test().Printf(_L("Entering tickUntilTime\n"));
       
   105 	RTimer tickTimer;
       
   106 	tickTimer.CreateLocal();
       
   107 	TTime now;
       
   108 	now.HomeTime();
       
   109 	TInt loop=0;
       
   110 	TheAlarmTest.Test().Printf(_L("Begin Loop\n"));
       
   111 	while (now<aStopTime)
       
   112 		{
       
   113 		TTime nowx(now.Int64());
       
   114 		TTime stopx(aStopTime.Int64());
       
   115 		TheAlarmTest.Test().Printf(_L("\nStart of Loop %d\n"),++loop);
       
   116 		TheAlarmTest.Test().Printf(_L("now<aStopTime %d\n"),now<aStopTime);
       
   117 		TheAlarmTest.Test().Printf(_L("now>aStopTime %d\n"),now>aStopTime);
       
   118 		TheAlarmTest.Test().Printf(_L("now<=aStopTime %d\n"),now<=aStopTime);
       
   119 		TheAlarmTest.Test().Printf(_L("now>=aStopTime %d\n"),now>=aStopTime);
       
   120 		logTimeL(_L("---Nowx at"),nowx);
       
   121 		logTimeL(_L("---Stopx at"),stopx);
       
   122 		TheAlarmTest.Test().Printf(_L("nowx<stopx %d\n"),nowx<stopx);
       
   123 
       
   124 		TheAlarmTest.Test().Printf(_L("\nNow1 TInt64 %d\n"),now.Int64());
       
   125 		TheAlarmTest.Test().Printf(_L("Stop1 TInt64 %d\n\n"),aStopTime.Int64());
       
   126 		logTimeSameLineL(_L("Time is"),now);
       
   127 		//TheAlarmTest.Test().Printf(_L("Now2 TInt64 %d\n"),now.Int64());
       
   128 		//TheAlarmTest.Test().Printf(_L("Stop2 TInt64 %d\n"),aStopTime.Int64());
       
   129 		TRequestStatus status;
       
   130 		tickTimer.After(status,1000000);
       
   131 		User::WaitForRequest(status);
       
   132 		//TheAlarmTest.Test().Printf(_L("Now3 TInt64 %d\n"),now.Int64());
       
   133 		//TheAlarmTest.Test().Printf(_L("Stop3 TInt64 %d\n"),aStopTime.Int64());
       
   134 		now.HomeTime();
       
   135 		//TheAlarmTest.Test().Printf(_L("Now4 TInt64 %d\n"),now.Int64());
       
   136 		//TheAlarmTest.Test().Printf(_L("Stop4 TInt64 %d\n"),aStopTime.Int64());
       
   137 		logTimeL(_L("\nDebug now      :"),now);
       
   138 		logTimeL(_L("Debug aStopTime:"),aStopTime);
       
   139 		TheAlarmTest.Test().Printf(_L("Now5 TInt64 %d\n"),now.Int64());
       
   140 		TheAlarmTest.Test().Printf(_L("Stop5 TInt64 %d\n"),aStopTime.Int64());
       
   141 
       
   142 		nowx=now;
       
   143 		stopx=aStopTime;
       
   144 
       
   145 		TheAlarmTest.Test().Printf(_L("\nnow<aStopTime %d\n"),now<aStopTime);
       
   146 		TheAlarmTest.Test().Printf(_L("now>aStopTime %d\n"),now>aStopTime);
       
   147 		TheAlarmTest.Test().Printf(_L("now<=aStopTime %d\n"),now<=aStopTime);
       
   148 		TheAlarmTest.Test().Printf(_L("now>=aStopTime %d\n"),now>=aStopTime);
       
   149 		TheAlarmTest.Test().Printf(_L("Nowx TInt64 %d\n"),nowx.Int64());
       
   150 		TheAlarmTest.Test().Printf(_L("Stopx TInt64 %d\n"),stopx.Int64());
       
   151 		TheAlarmTest.Test().Printf(_L("nowx<stopx %d\n"),nowx<stopx);
       
   152 		TheAlarmTest.Test().Printf(_L("End of Loop %d\n\n"),loop);
       
   153 		}
       
   154 	TheAlarmTest.Test().Printf(_L("leaving tickUntilTime\n"));
       
   155 	}
       
   156 
       
   157 static void finishTestL(TTime& aExpireTime, const TBool aStartServerWithinMinute)
       
   158 	{
       
   159 	closeServers();
       
   160 	//
       
   161 	if	(aStartServerWithinMinute)
       
   162 		aExpireTime += TTimeIntervalSeconds(5);
       
   163 	else
       
   164 		{
       
   165 		aExpireTime += TTimeIntervalSeconds(60);
       
   166 		JumpJustBeforeL(aExpireTime);
       
   167 		}
       
   168 	//
       
   169 	logTimeL(_L("Starting servers at"),aExpireTime);
       
   170 	tickUntilTimeL(aExpireTime);
       
   171 	//
       
   172 	TheAlarmTest.TestStartServers();
       
   173 	//
       
   174 	if	(aStartServerWithinMinute)
       
   175 		{
       
   176 		TheAlarmTest.Test().Printf(_L("You SHOULD see an Alert!\n\r"));
       
   177 #if defined(_DEBUG) && defined(__WINS__)
       
   178 		TheAlarmTest(IsNotifying());
       
   179 #endif
       
   180 		}
       
   181 	else
       
   182 		{
       
   183 		TheAlarmTest.Test().Printf(_L("You SHOULD NOT see an Alert!\n\r"));
       
   184 #if defined(_DEBUG) && defined(__WINS__)
       
   185 		TheAlarmTest(!IsNotifying());
       
   186 #endif
       
   187 		}
       
   188 	//
       
   189 
       
   190 	TheAlarmTest.TestClearStoreL();
       
   191 	closeServers();
       
   192 	}
       
   193 
       
   194 
       
   195 static void startTestUtcL(TASShdAlarm& aAlarm, TAlarmMessage& aMessage, TInt aTestNumber)
       
   196 	{
       
   197 	TheAlarmTest.Test().Console()->ClearScreen();
       
   198 	TheAlarmTest.Test().Printf(_L("Starting test [%2d/14]\n\r"), aTestNumber);
       
   199 
       
   200 	TheAlarmTest.TestStartServers();
       
   201 	//
       
   202 	TInt r=TheAlarmTest.Session().Connect();
       
   203 	TheAlarmTest(r==KErrNone, __LINE__);
       
   204 	TheAlarmTest.TestClearStoreL();
       
   205 	//
       
   206 	TTime expireTime;
       
   207 	expireTime.UniversalTime();
       
   208 	TDateTime expireDateTime=expireTime.DateTime();
       
   209 	logTimeL(_L("UTC Time now is"), expireTime);
       
   210 	//
       
   211 	expireTime.RoundUpToNextMinute();
       
   212 	if	(expireDateTime.Second() > 40)
       
   213 		expireTime += TTimeIntervalMinutes(1);
       
   214 	//
       
   215 	expireTime.FormatL(aMessage, _L("expire time %H:%T:%S.%C:"));
       
   216     // Note, JumpJustBefore takes a Floating Time and not a Universal Time.
       
   217 	JumpJustBeforeL(expireTime + User::UTCOffset());
       
   218 	aAlarm.SetUtcNextDueTime(expireTime);
       
   219 	}
       
   220 
       
   221 static void startTestL(TTime& aExpireTime, TAlarmMessage& aMessage, TInt aTestNumber)
       
   222 	{
       
   223 	TheAlarmTest.Test().Console()->ClearScreen();
       
   224 	TheAlarmTest.Test().Printf(_L("Starting test [%2d/14]\n\r"), aTestNumber);
       
   225 
       
   226 	TheAlarmTest.TestStartServers();
       
   227 	//
       
   228 	TInt r=TheAlarmTest.Session().Connect();
       
   229 	TheAlarmTest(r==KErrNone, __LINE__);
       
   230 	TheAlarmTest.TestClearStoreL();
       
   231 	//
       
   232 	aExpireTime.HomeTime();
       
   233 	TDateTime expireDateTime=aExpireTime.DateTime();
       
   234 	logTimeL(_L("Time now is"), aExpireTime);
       
   235 	//
       
   236 	aExpireTime.RoundUpToNextMinute();
       
   237 	if	(expireDateTime.Second() > 40)
       
   238 		aExpireTime += TTimeIntervalMinutes(1);
       
   239 	//
       
   240 	aExpireTime.FormatL(aMessage, _L("expire time %H:%T:%S.%C:"));
       
   241 
       
   242 	JumpJustBeforeL(aExpireTime);
       
   243 	}
       
   244 
       
   245 static void clockAlarmTestL(TAlarmRepeatDefinition aRepeatType, TBool aStartServerWithinMinute, TInt aNumber)
       
   246 // aRepeatType - standard repeat choice, allows testing of different style clock alarms
       
   247 // aStartServerWithinMinute - whether to start the servers within the minute after expiry of the TheAlarmTest.Test() alarm
       
   248 	{
       
   249 	TASShdAlarm alarm;
       
   250 	startTestL(alarm.NextDueTime(), alarm.Message(), aNumber);
       
   251 	alarm.RepeatDefinition() = aRepeatType;
       
   252 	TheAlarmTest.Test().Printf(_L("Setting clock alarm, type:%d\n\r"), aRepeatType);
       
   253 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   254 
       
   255 	logTimeL(_L("Set clock alarm to expire at"),alarm.NextDueTime());
       
   256 	finishTestL(alarm.NextDueTime(),aStartServerWithinMinute);
       
   257 	}
       
   258 
       
   259 static void agendaAlarmTestL(TBool aStartServerWithinMinute, TInt aNumber)
       
   260 // aSessionType - standard session choice, allows testing of different style agenda alarms
       
   261 // aStartServerWithinMinute - whether to start the servers within the minute after expiry of the TheAlarmTest.Test() alarm
       
   262 	{
       
   263 	TASShdAlarm alarm;
       
   264 	startTestL(alarm.NextDueTime(), alarm.Message(), aNumber);
       
   265 	TheAlarmTest.Test().Printf(_L("Setting agenda alarm\n\r"));
       
   266 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   267 
       
   268 	logTimeL(_L("Set agenda alarm to expire at"),alarm.NextDueTime());
       
   269 
       
   270 	finishTestL(alarm.NextDueTime(),aStartServerWithinMinute);
       
   271 	}
       
   272 
       
   273 static void orphanAlarmTestL(TBool aStartServerWithinMinute, TInt aNumber)
       
   274 // aSessionType - standard session choice, allows testing of different style orphan alarms
       
   275 // aStartServerWithinMinute - whether to start the servers within the minute after expiry of the TheAlarmTest.Test() alarm
       
   276 	{
       
   277 	TASShdAlarm alarm;
       
   278 	alarm.Characteristics().Set(EAlarmCharacteristicsSessionSpecific);
       
   279 	startTestL(alarm.NextDueTime(), alarm.Message(), aNumber);
       
   280 
       
   281 	TheAlarmTest.Test().Printf(_L("Setting session alarm\n\r"));
       
   282 
       
   283 	// Set the session alarm
       
   284 	TRequestStatus status;
       
   285 	TheAlarmTest.Session().AlarmAddWithNotification(status, alarm);
       
   286 
       
   287 	// Now orphan it
       
   288 	TheAlarmTest.Test().Printf(_L("Session alarm set, now orphaning\n\r"));
       
   289 	TheAlarmTest.Session().SetAlarmCharacteristics(alarm.Id(), 0); // clear session specific flag
       
   290 
       
   291 	// Get the abort
       
   292 	User::WaitForRequest(status);
       
   293 	TheAlarmTest(status.Int() == KErrCancel, __LINE__);
       
   294 
       
   295 	logTimeL(_L("Created orphan alarm to expire at"),alarm.NextDueTime());
       
   296 
       
   297 	finishTestL(alarm.NextDueTime(),aStartServerWithinMinute);
       
   298 	}
       
   299 
       
   300 void serverRestartWithNotifyingAlarmTestL()
       
   301 	{
       
   302 #if defined(_DEBUG) && defined(__WINS__)
       
   303 	TheAlarmTest.TestStartServers();
       
   304 	TheAlarmTest.TestClearStoreL();
       
   305 	TheAlarmTest(TheAlertSrv.Connect() == KErrNone);
       
   306 	// Add an alarm due immediately
       
   307 	TASShdAlarm alarm;
       
   308 	TTime time;
       
   309 	time.HomeTime();
       
   310 	alarm.NextDueTime() = time;
       
   311 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone);
       
   312 	TAlarmId id = alarm.Id();
       
   313 	// Check that the alarm is notifying
       
   314 	TheAlarmTest(IsNotifying());
       
   315 	// Shut down and restart the server
       
   316 	closeServers();
       
   317 	TheAlarmTest.TestStartServers();
       
   318 	TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone);
       
   319 	// Check that the alarm is still notifying
       
   320 	TheAlarmTest.Test().Printf(_L("Check alarm still notifying after shutdown and restart\n"));
       
   321 	TheAlarmTest(IsNotifying());
       
   322 	// Snooze the alarm, and restart the server
       
   323 	TTime timeToSnoozeUntil = time + TTimeIntervalMinutes(1);
       
   324 	TheAlarmTest(TheAlarmTest.Session().__DbgSnoozeAlarm(id, timeToSnoozeUntil - User::UTCOffset()) == KErrNone);
       
   325 	TheAlarmTest(!IsNotifying());
       
   326 	closeServers();
       
   327 	TheAlarmTest.TestStartServers();
       
   328 	TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone);
       
   329 	// Check that the alarm is still snoozed
       
   330 	TheAlarmTest(!IsNotifying());
       
   331 	// Check the alarm notifies again after the snooze period
       
   332 	User::At(timeToSnoozeUntil);
       
   333 	TheAlarmTest.Test().Printf(_L("Check snoozed alarm notifies after shutdown and restart\n"));
       
   334 	TheAlarmTest(IsNotifying());
       
   335 	// Snooze the alarm again and shut down the server -
       
   336 	// Restart the server 3 months in the future
       
   337 	timeToSnoozeUntil.HomeTime();
       
   338 	timeToSnoozeUntil += TTimeIntervalMinutes(1);
       
   339 	TheAlarmTest(TheAlarmTest.Session().__DbgSnoozeAlarm(id, timeToSnoozeUntil - User::UTCOffset()) == KErrNone);
       
   340 	TheAlarmTest(!IsNotifying());
       
   341 	closeServers();
       
   342 
       
   343     RPIMTestServer serv;
       
   344     User::LeaveIfError(serv.Connect());
       
   345     serv.SetHomeTime(timeToSnoozeUntil + TTimeIntervalMonths(3));
       
   346     serv.Close();
       
   347 
       
   348 	TheAlarmTest.TestStartServers();
       
   349 	TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone);
       
   350 	// Check the alarm is notifying
       
   351 	TheAlarmTest.Test().Printf(_L("Check snoozed alarm notifies again after shutdown and restart\n"));
       
   352 	TheAlarmTest(IsNotifying());
       
   353 
       
   354 	TheAlertSrv.Close();
       
   355 #else
       
   356 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A DEBUG, WINS TEST ONLY\r\n"));
       
   357 #endif
       
   358 	}
       
   359 
       
   360 // UTC tests
       
   361 
       
   362 
       
   363 static void clockUtcAlarmTestL(TAlarmRepeatDefinition aRepeatType, TBool aStartServerWithinMinute, TInt aNumber)
       
   364 // aRepeatType - standard repeat choice, allows testing of different style clock alarms
       
   365 // aStartServerWithinMinute - whether to start the servers within the minute after expiry of the TheAlarmTest.Test() alarm
       
   366 	{
       
   367 	TASShdAlarm alarm;
       
   368 	TBuf<KMaxAlarmMessageLength> message = alarm.Message();
       
   369 	startTestUtcL(alarm, message, aNumber);
       
   370 	alarm.RepeatDefinition()=aRepeatType;
       
   371 	TheAlarmTest.Test().Printf(_L("Setting clock alarm, type:%d\n\r"), aRepeatType);
       
   372 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   373 
       
   374 	TTime time = alarm.NextDueTime() + User::UTCOffset();
       
   375 	logTimeL(_L("Set clock alarm to expire at"), time);
       
   376 
       
   377 	finishTestL(time, aStartServerWithinMinute);
       
   378 	}
       
   379 
       
   380 static void agendaUtcAlarmTestL(TBool aStartServerWithinMinute, TInt aNumber)
       
   381 // aSessionType - standard session choice, allows testing of different style agenda alarms
       
   382 // aStartServerWithinMinute - whether to start the servers within the minute after expiry of the TheAlarmTest.Test() alarm
       
   383 	{
       
   384 	TASShdAlarm alarm;
       
   385 	TBuf<KMaxAlarmMessageLength> message = alarm.Message();
       
   386 	startTestUtcL(alarm, message, aNumber);
       
   387 	TheAlarmTest.Test().Printf(_L("Setting agenda alarm\n\r"));
       
   388 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   389 
       
   390 	TTime time = alarm.NextDueTime() + User::UTCOffset();
       
   391 	logTimeL(_L("Set agenda alarm to expire at"), time);
       
   392 
       
   393 	finishTestL(time, aStartServerWithinMinute);
       
   394 	}
       
   395 
       
   396 static void orphanUtcAlarmTestL(TBool aStartServerWithinMinute, TInt aNumber)
       
   397 // aSessionType - standard session choice, allows testing of different style orphan alarms
       
   398 // aStartServerWithinMinute - whether to start the servers within the minute after expiry of the TheAlarmTest.Test() alarm
       
   399 	{
       
   400 	TASShdAlarm alarm;
       
   401 	alarm.Characteristics().Set(EAlarmCharacteristicsSessionSpecific);
       
   402 	TBuf<KMaxAlarmMessageLength> message = alarm.Message();
       
   403 	startTestUtcL(alarm, message, aNumber);
       
   404 
       
   405 	TheAlarmTest.Test().Printf(_L("Setting session alarm\n\r"));
       
   406 
       
   407 	// Set the session alarm
       
   408 	TRequestStatus status;
       
   409 	TheAlarmTest.Session().AlarmAddWithNotification(status, alarm);
       
   410 
       
   411 	// Now orphan it
       
   412 	TheAlarmTest.Test().Printf(_L("Session alarm set, now orphaning\n\r"));
       
   413 	TheAlarmTest.Session().SetAlarmCharacteristics(alarm.Id(), 0); // clear session specific flag
       
   414 
       
   415 	// Get the abort
       
   416 	User::WaitForRequest(status);
       
   417 	TheAlarmTest(status.Int() == KErrCancel, __LINE__);
       
   418 
       
   419 	TTime time = alarm.NextDueTime() + User::UTCOffset();
       
   420 	logTimeL(_L("Created orphan alarm to expire at"), time);
       
   421 
       
   422 	finishTestL(time, aStartServerWithinMinute);
       
   423 	}
       
   424 
       
   425 void serverRestartWithNotifyingUtcAlarmTestL()
       
   426 	{
       
   427 #if defined(_DEBUG) && defined(__WINS__)
       
   428 	TheAlarmTest.TestStartServers();
       
   429 	TheAlarmTest.TestClearStoreL();
       
   430 	TheAlarmTest(TheAlertSrv.Connect() == KErrNone);
       
   431 	// Add an alarm due immediately
       
   432 	TASShdAlarm alarm;
       
   433 	TTime time;
       
   434 	time.UniversalTime();
       
   435 	alarm.SetUtcNextDueTime(time);
       
   436 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone);
       
   437 	TAlarmId id = alarm.Id();
       
   438 	// Check that the alarm is notifying
       
   439 	TheAlarmTest(IsNotifying());
       
   440 	// Shut down and restart the server
       
   441 	closeServers();
       
   442 	TheAlarmTest.TestStartServers();
       
   443 	TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone);
       
   444 	// Check that the alarm is still notifying
       
   445 	TheAlarmTest.Test().Printf(_L("Check alarm still notifying after shutdown and restart\n"));
       
   446 	TheAlarmTest(IsNotifying());
       
   447 	// Snooze the alarm, and restart the server
       
   448 	TTime timeToSnoozeUntil = time + TTimeIntervalMinutes(1);
       
   449 	TheAlarmTest(TheAlarmTest.Session().__DbgSnoozeAlarm(id, timeToSnoozeUntil) == KErrNone);
       
   450 	TheAlarmTest(!IsNotifying());
       
   451 	closeServers();
       
   452 	TheAlarmTest.TestStartServers();
       
   453 	TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone);
       
   454 	// Check that the alarm is still snoozed
       
   455 	TheAlarmTest(!IsNotifying());
       
   456 	// Check the alarm notifies again after the snooze period
       
   457 	// Note, User::At takes a Floating Time (wall-time) and not a Universal Time
       
   458 	User::At(timeToSnoozeUntil+User::UTCOffset());
       
   459 	TheAlarmTest.Test().Printf(_L("Check snoozed alarm notifies after shutdown and restart\n"));
       
   460 	TheAlarmTest(IsNotifying());
       
   461 	// Snooze the alarm again and shut down the server -
       
   462 	// Restart the server 3 months in the future
       
   463 	timeToSnoozeUntil.UniversalTime();
       
   464 	timeToSnoozeUntil += TTimeIntervalMinutes(1);
       
   465 	TheAlarmTest(TheAlarmTest.Session().__DbgSnoozeAlarm(id, timeToSnoozeUntil) == KErrNone);
       
   466 	TheAlarmTest(!IsNotifying());
       
   467 	closeServers();
       
   468 
       
   469     User::SetUTCTime(timeToSnoozeUntil + TTimeIntervalMonths(3));
       
   470 
       
   471 	TheAlarmTest.TestStartServers();
       
   472 	TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone);
       
   473 	// Check the alarm is notifying
       
   474 	TheAlarmTest.Test().Printf(_L("Check snoozed alarm notifies again after shutdown and restart\n"));
       
   475 	TheAlarmTest(IsNotifying());
       
   476 
       
   477 	TheAlertSrv.Close();
       
   478 #else
       
   479 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A DEBUG, WINS TEST ONLY\r\n"));
       
   480 #endif
       
   481 	}
       
   482 
       
   483 	
       
   484 void serverRestartWithNotifiedUtcAlarmTestL()
       
   485 	{
       
   486 #if defined(_DEBUG) && defined(__WINS__)
       
   487     TheAlarmTest.Next(_L("Acknowledge repeating alarm after restart in less then 1 minute) does not notify"));	
       
   488 
       
   489     TheAlarmTest.TestStartServers();
       
   490     TheAlarmTest.TestClearStoreL();
       
   491     TheAlarmTest(TheAlertSrv.Connect() == KErrNone);
       
   492 
       
   493     // Add an alarm due in the future
       
   494     TheAlarmTest.Test().Printf(_L("Add a repeating alarm to expire in the future\n"));
       
   495     TASShdAlarm alarm;
       
   496     alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatDaily;	
       
   497 
       
   498     TTime alarmTime;
       
   499     alarmTime.UniversalTime();
       
   500     alarmTime.RoundUpToNextMinute();
       
   501     alarmTime += TTimeIntervalMinutes(1);
       
   502 
       
   503     alarm.SetUtcNextDueTime(alarmTime);
       
   504 
       
   505     TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone);
       
   506     TAlarmId id = alarm.Id();
       
   507 
       
   508     // wait until that time has occurred before checking
       
   509     TheAlarmTest.Test().Printf(_L("Wait for alarm time to expire\n"));
       
   510      
       
   511 	User::At(alarmTime + User::UTCOffset());
       
   512 
       
   513 	TheAlarmTest.Test().Printf(_L("Check that alarm is notifying\n"));
       
   514     TheAlarmTest(IsNotifying());
       
   515     TheAlarmTest.Test().Printf(_L("Acknowledge alarm\n"));
       
   516     TheAlertSrv.AcknowledgeAlarm();
       
   517     TheAlarmTest.Test().Printf(_L("Check that alarm is cleared\n"));
       
   518     TheAlarmTest(!IsNotifying());
       
   519 
       
   520     TheAlarmTest.Test().Printf(_L("Shutdown servers\n"));
       
   521     // Shut down and restart the server
       
   522     TheAlertSrv.Close();
       
   523     closeServers();
       
   524 
       
   525     TheAlarmTest.TestStartServers();
       
   526     TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone);
       
   527     TheAlarmTest(TheAlertSrv.Connect() == KErrNone);
       
   528 
       
   529     TheAlarmTest.Test().Printf(_L("Servers restarted\n"));
       
   530 
       
   531     // Make sure that server was restarted in the same minute as the alarm
       
   532     TTime currentTime;
       
   533     currentTime.UniversalTime();
       
   534     TTimeIntervalHours hours;
       
   535     TTimeIntervalMinutes minutes;
       
   536 
       
   537     TheAlarmTest.Test().Printf(_L("Check servers restarted in 1 minute\n"));
       
   538     TheAlarmTest(currentTime.HoursFrom(alarmTime, hours) == KErrNone);
       
   539     TheAlarmTest(currentTime.MinutesFrom(alarmTime, minutes) == KErrNone);
       
   540     TheAlarmTest((hours == TTimeIntervalHours(0)) && (minutes == TTimeIntervalMinutes(0)));
       
   541 
       
   542 
       
   543     // Check that the alarm is not notifying
       
   544     TheAlarmTest.Test().Printf(_L("Check alarm still not notifying after shutdown and restart\n"));
       
   545     TheAlarmTest(!IsNotifying());
       
   546 
       
   547     closeServers();
       
   548     TheAlertSrv.Close();
       
   549 #else
       
   550     TheAlarmTest.Test().Printf(_L("THIS TEST IS A DEBUG, WINS TEST ONLY\r\n"));
       
   551 #endif
       
   552 	}
       
   553 
       
   554 static void doTestsL()
       
   555 	{
       
   556 #if defined(_DEBUG) && defined(__WINS__)
       
   557 	TheAlarmTest(TheAlertSrv.Connect() == KErrNone);
       
   558 #endif
       
   559 
       
   560 #if defined(_DEBUG)
       
   561 // The alarm should only be allowed to display notification in WINS builds.
       
   562 #if defined(__WINSCW__)
       
   563 	TheAlarmTest.Session().__DbgPreventUserNotify(EFalse);
       
   564 #else
       
   565 	TheAlarmTest.Session().__DbgPreventUserNotify(ETrue);
       
   566 #endif
       
   567 #endif
       
   568 
       
   569 	// Set all days to workdays.
       
   570 	TLocale previous;
       
   571 	locale.SetWorkDays(127);
       
   572 	locale.Set(); 
       
   573 	orphanAlarmTestL(ETrue, 1);
       
   574 	orphanAlarmTestL(EFalse, 2);
       
   575 
       
   576 	agendaAlarmTestL(ETrue, 3);
       
   577 	agendaAlarmTestL(EFalse, 4);
       
   578 
       
   579 
       
   580 	clockAlarmTestL(EAlarmRepeatDefintionRepeatWorkday,ETrue, 5);
       
   581 	clockAlarmTestL(EAlarmRepeatDefintionRepeatWorkday,EFalse, 6);
       
   582 
       
   583 	clockAlarmTestL(EAlarmRepeatDefintionRepeatWeekly,ETrue, 7);
       
   584 	clockAlarmTestL(EAlarmRepeatDefintionRepeatWeekly,EFalse, 8);
       
   585 
       
   586 	clockAlarmTestL(EAlarmRepeatDefintionRepeatDaily,ETrue, 9);
       
   587 	clockAlarmTestL(EAlarmRepeatDefintionRepeatDaily,EFalse, 10);
       
   588 
       
   589 	clockAlarmTestL(EAlarmRepeatDefintionRepeatNext24Hours,ETrue, 11);
       
   590 	clockAlarmTestL(EAlarmRepeatDefintionRepeatNext24Hours,EFalse, 12);
       
   591 
       
   592 	clockAlarmTestL(EAlarmRepeatDefintionRepeatOnce,ETrue, 13);
       
   593 	clockAlarmTestL(EAlarmRepeatDefintionRepeatOnce,EFalse, 14);
       
   594 
       
   595 #if defined(_DEBUG) && defined(__WINS__)
       
   596 	TheAlertSrv.Close();
       
   597 #endif
       
   598 
       
   599 	serverRestartWithNotifyingAlarmTestL();
       
   600 
       
   601 #if defined(_DEBUG) && defined(__WINS__)
       
   602 	TheAlarmTest(TheAlertSrv.Connect() == KErrNone);
       
   603 #endif 
       
   604 	
       
   605 	orphanUtcAlarmTestL(ETrue, 1);
       
   606 	orphanUtcAlarmTestL(EFalse, 2);
       
   607 
       
   608 	agendaUtcAlarmTestL(ETrue, 3);
       
   609 	agendaUtcAlarmTestL(EFalse, 4);
       
   610 
       
   611 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatWorkday,ETrue, 5);
       
   612 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatWorkday,EFalse, 6);
       
   613 
       
   614 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatWeekly,ETrue, 7);
       
   615 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatWeekly,EFalse, 8);
       
   616 
       
   617 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatDaily,ETrue, 9);
       
   618 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatDaily,EFalse, 10);
       
   619 
       
   620 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatNext24Hours,ETrue, 11);
       
   621 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatNext24Hours,EFalse, 12);
       
   622 
       
   623 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatOnce,ETrue, 13);
       
   624 	clockUtcAlarmTestL(EAlarmRepeatDefintionRepeatOnce,EFalse, 14);
       
   625 
       
   626 #if defined(_DEBUG) && defined(__WINS__)
       
   627 	TheAlertSrv.Close();
       
   628 #endif
       
   629 
       
   630 	serverRestartWithNotifyingUtcAlarmTestL();
       
   631 	serverRestartWithNotifiedUtcAlarmTestL();
       
   632 
       
   633 	// Reset the previous locale.
       
   634 	previous.Set();
       
   635 	}
       
   636 
       
   637 /**
       
   638 @SYMTestCaseID PIM-TSTARTUPEXPIRE-0001
       
   639 */	
       
   640 GLDEF_C TInt E32Main()
       
   641     {
       
   642 	__UHEAP_MARK;
       
   643 	TInt ret = KErrNone;
       
   644 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   645 	if	(!cleanup)
       
   646 		return KErrNoMemory;
       
   647 
       
   648 	TheAlarmTest.Title();
       
   649 	TheAlarmTest.Start(_L("@SYMTestCaseID PIM-TSTARTUPEXPIRE-0001"));
       
   650 	TheAlarmTest.TestStartServers();
       
   651 	//
       
   652 	TRAPD(error, doTestsL());
       
   653 	TheAlarmTest(error == KErrNone, __LINE__);
       
   654 	//
       
   655     TRAP(ret,TheAlarmTest.EndL());
       
   656 	TheAlarmTest.Test().Close();
       
   657 	ASTstAlarmTest::Close();
       
   658 
       
   659 	delete cleanup;
       
   660 	__UHEAP_MARKEND;
       
   661 	return ret;
       
   662     }