commonappservices/alarmserver/Test/TSessionAlarms.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 // Test session alarms
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "ASTstAlarmTest.h"
       
    19 
       
    20 // Type definitions
       
    21 #define UNUSED_VAR(a) a = a
       
    22 
       
    23 // Constants
       
    24 const TUint KHeapMinSize=0x01000;
       
    25 const TUint KHeapMaxSize=0x10000;
       
    26 
       
    27 // Global data
       
    28 static TAlarmId TheAlarmId;
       
    29 static TBool TheNotifyFlag=EFalse;
       
    30 static TBool TheNotifyThread=ETrue;
       
    31 static TRequestStatus TheSsnStatus;
       
    32 static TRequestStatus ThisRequest=KErrNone;
       
    33 static TRequestStatus CurrentRequest=KErrNone;
       
    34 
       
    35 // Prototypes
       
    36 static void testOtherNotify(TBool aExpected);
       
    37 
       
    38 
       
    39 static TInt ntfThreadMain(TAny* /*aPtr*/)
       
    40 //
       
    41 //	Main thread of notification thread
       
    42 //
       
    43 	{
       
    44 	RASCliSession almSvr;
       
    45 	const TInt r=almSvr.Connect();
       
    46 	TheAlarmTest(r==KErrNone, __LINE__);
       
    47 	//
       
    48 	TAlarmId alarmId;
       
    49 	TRequestStatus stat;
       
    50 	FOREVER
       
    51 		{
       
    52 		FOREVER
       
    53 			{
       
    54 			almSvr.NotifyChange(stat, alarmId);
       
    55 			User::After(KTimeToWait);
       
    56 			if	(stat == KRequestPending)
       
    57 				break;
       
    58 			User::WaitForRequest(stat);
       
    59 			}
       
    60 		User::WaitForRequest(stat);
       
    61 		//
       
    62 		if (!TheNotifyThread)
       
    63 			break;
       
    64 		//
       
    65 		TheAlarmTest(stat >= EAlarmChangeEventState && stat <= EAlarmChangeEventLast, __LINE__);
       
    66 		TheNotifyFlag = ETrue;
       
    67 		}
       
    68 	//
       
    69 	TheNotifyFlag=ETrue;
       
    70 	return(KErrNone);
       
    71 	}
       
    72 
       
    73 
       
    74 static void startNotifyThread(TPtrC16 aThreadName)
       
    75 //
       
    76 //	Start the thread that determines NotifyOnChange
       
    77 //
       
    78 	{
       
    79 	TheAlarmTest.Test().Next(_L("Starting the notification test thread"));
       
    80 	RThread t;
       
    81 	TInt r=t.Create(aThreadName,ntfThreadMain,KDefaultStackSize,KHeapMinSize,KHeapMaxSize,NULL);
       
    82 	TheAlarmTest(r==KErrNone, __LINE__);
       
    83 	TRequestStatus tStat;
       
    84 	t.Logon(tStat);
       
    85 	t.SetPriority(EPriorityMore);
       
    86 	TheAlarmTest(tStat==KRequestPending, __LINE__);
       
    87 	t.Resume();
       
    88 	TheAlarmTest(tStat==KRequestPending, __LINE__);
       
    89 	User::After(KTimeToWait);
       
    90 	}
       
    91 
       
    92 static void testSetupNotify(TPtrC16 aThreadName)
       
    93 //
       
    94 //	Set up the notify requests
       
    95 //
       
    96 	{
       
    97 	TheNotifyFlag=EFalse;
       
    98 	startNotifyThread(aThreadName);
       
    99 	//
       
   100 	TheAlarmTest.Test().Next(_L("Set up notification requests"));
       
   101 	//
       
   102 	TheAlarmTest.Session().NotifyChange(CurrentRequest, TheAlarmId);
       
   103 	User::WaitForRequest(CurrentRequest);
       
   104 	//
       
   105 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(CurrentRequest, TheAlarmId);
       
   106 	TheAlarmTest.Session().NotifyChangeCancel();
       
   107 	//
       
   108 	TheAlarmTest(CurrentRequest == KErrCancel, __LINE__);
       
   109 	TheAlarmTest(TheNotifyFlag == EFalse, __LINE__);
       
   110 	//
       
   111 #if defined(_DEBUG)
       
   112 	TheAlarmTest.Session().__DbgPreventUserNotify(ETrue);
       
   113 #endif
       
   114 	}
       
   115 
       
   116 static void testOtherNotify(TBool aExpected)
       
   117 //
       
   118 //	Check there was a notification to the notify thread
       
   119 //
       
   120 	{
       
   121 	User::After(KTimeToWait);
       
   122 	TheAlarmTest(TheNotifyFlag==aExpected, __LINE__);
       
   123 	TheNotifyFlag=EFalse;
       
   124 	}
       
   125 
       
   126 static TAlarmId SetSessionAlarm(TASShdAlarm& aAlarm, const TTime& aTime, TRequestStatus& aStatus)
       
   127 //
       
   128 //	Set the session alarm
       
   129 //
       
   130 	{
       
   131 	TheAlarmTest(TheNotifyFlag == EFalse, __LINE__);
       
   132 	//
       
   133 	aAlarm.Reset();
       
   134 	aAlarm.Message() = _L("The session alarm");
       
   135 	aAlarm.NextDueTime() = aTime;
       
   136 	aAlarm.Characteristics().Set(EAlarmCharacteristicsSessionSpecific);
       
   137 	//
       
   138 	TheAlarmTest.Session().AlarmAddWithNotification(aStatus, aAlarm);
       
   139 	TheAlarmTest(aStatus == KRequestPending, __LINE__);
       
   140 	//
       
   141 	TheAlarmTest(aAlarm.Id() != KNullAlarmId, __LINE__);
       
   142 	return aAlarm.Id();
       
   143 	}
       
   144 
       
   145 static void testSetSessionAlarm(const TDesC& aDes, const TTime& aDueTime)
       
   146 //
       
   147 //	Test setting a session alarm
       
   148 //
       
   149 	{
       
   150 	TheAlarmTest.Test().Next(aDes);
       
   151 
       
   152 	// Check there's nothing ready
       
   153 	testOtherNotify(EFalse);
       
   154 
       
   155 	// Get all events from the event buffer
       
   156 	TAlarmId alarmId;
       
   157 	TRequestStatus notificationStatus;
       
   158 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   159 	TheAlarmTest(notificationStatus == KRequestPending, __LINE__);
       
   160 
       
   161 	// Queue session alarm
       
   162 	TRequestStatus sessionAlarmStatus;
       
   163 	TASShdAlarm alarm;
       
   164 	TAlarmId sessionAlarmId = SetSessionAlarm(alarm, aDueTime, sessionAlarmStatus);
       
   165 
       
   166 	// Check that the notification thread was updated
       
   167 	testOtherNotify(ETrue);
       
   168 
       
   169 	// Check that the notification request was correctly updated
       
   170 	User::WaitForRequest(notificationStatus);
       
   171 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   172 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   173 
       
   174 	// Get details
       
   175 	TASShdAlarm alarm2;
       
   176 	TInt r = TheAlarmTest.Session().GetAlarmDetails(sessionAlarmId, alarm2);
       
   177 	TheAlarmTest(r == KErrNone, __LINE__);
       
   178 	TDateTime dateTime = aDueTime.DateTime();
       
   179 	TDateTime alarmTime = alarm2.NextDueTime().DateTime();
       
   180 
       
   181 	// Check details
       
   182 	TheAlarmTest(alarmTime.Day()==dateTime.Day(), __LINE__);
       
   183 	TheAlarmTest(alarmTime.Hour()==dateTime.Hour(), __LINE__);
       
   184 	TheAlarmTest(alarmTime.Minute()==dateTime.Minute(), __LINE__);
       
   185 	alarmTime = alarm2.NextDueTime().DateTime();
       
   186 	dateTime = aDueTime.DateTime();
       
   187 	TheAlarmTest(alarmTime.Day()==dateTime.Day(), __LINE__);
       
   188 	TheAlarmTest(alarmTime.Hour()==dateTime.Hour(), __LINE__);
       
   189 	TheAlarmTest(alarmTime.Minute()==dateTime.Minute(), __LINE__);
       
   190 
       
   191 	// Cancel alarm
       
   192 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   193 	TheAlarmTest(notificationStatus == KRequestPending, __LINE__);
       
   194 	TheAlarmTest.Session().AlarmNotificationCancelAndDequeue(sessionAlarmId);
       
   195 	User::WaitForRequest(sessionAlarmStatus);
       
   196 	TheAlarmTest(sessionAlarmStatus == KErrCancel, __LINE__);
       
   197 
       
   198 	// Check that the notification thread was updated
       
   199 	testOtherNotify(ETrue);
       
   200 
       
   201 	// Check that the notification request was correctly updated
       
   202 	User::WaitForRequest(notificationStatus);
       
   203 	TheAlarmTest(notificationStatus == EAlarmChangeEventAlarmDeletion, __LINE__);
       
   204 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   205 
       
   206 	// Add another alarm
       
   207 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   208 	sessionAlarmId = SetSessionAlarm(alarm, aDueTime, sessionAlarmStatus);
       
   209 	testOtherNotify(ETrue);
       
   210 
       
   211 	// Check that the notification request was correctly updated
       
   212 	User::WaitForRequest(notificationStatus);
       
   213 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   214 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   215 
       
   216 	// Orphan alarm
       
   217 	r = TheAlarmTest.Session().SetAlarmCharacteristics(sessionAlarmId, EAlarmCharacteristicsSessionSpecific);
       
   218 	User::WaitForRequest(sessionAlarmStatus);
       
   219 	TheAlarmTest(sessionAlarmStatus == KErrCancel, __LINE__);
       
   220 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   221 
       
   222 	// Get details
       
   223 	r = TheAlarmTest.Session().GetAlarmDetails(sessionAlarmId, alarm);
       
   224 	TheAlarmTest(r == KErrNone, __LINE__);
       
   225 	TheAlarmTest(alarm.HasBecomeOrphaned(), __LINE__);
       
   226 	testOtherNotify(ETrue);
       
   227 	}
       
   228 
       
   229 static void testImmediateClose()
       
   230 //
       
   231 //	Test closing a session after the alarm goes off
       
   232 //	The session alarm must be on the notification queue but not first
       
   233 //	So that its session paramter is not cleared when its owner's name needs
       
   234 //	to be found.
       
   235 //
       
   236 	{
       
   237 
       
   238 	TTime time;
       
   239 	time.HomeTime();
       
   240 	TheAlarmTest(TheNotifyFlag == EFalse, __LINE__);
       
   241 
       
   242 	TASShdAlarm alarm;
       
   243 	alarm.SoundName() = _L("Signal");
       
   244 	alarm.NextDueTime() = time - TTimeIntervalSeconds(75);
       
   245 	alarm.Message() = _L("Session alarm");
       
   246 	//
       
   247 	RASCliSession almSvr;
       
   248 	TheAlarmTest(almSvr.Connect() == KErrNone, __LINE__);
       
   249 	almSvr.AlarmAddWithNotification(TheSsnStatus, alarm);
       
   250 	//
       
   251 	User::After(1000000);
       
   252 	const TAlarmId sessionAlarmId = alarm.Id();
       
   253 	TheAlarmTest(sessionAlarmId != KNullAlarmId, __LINE__);
       
   254 	//
       
   255 	alarm.Reset();
       
   256 	alarm.SoundName() = _L("Signal");
       
   257 	alarm.NextDueTime() = time - TTimeIntervalSeconds(65);
       
   258 	alarm.Message() = _L("Clock alarm");
       
   259 	//
       
   260 	TheAlarmTest(almSvr.AlarmAdd(alarm) == KErrNone, __LINE__);
       
   261 	TheAlarmTest(alarm.Id() != KNullAlarmId && alarm.Id() != sessionAlarmId, __LINE__);
       
   262 	//
       
   263 	almSvr.Close();
       
   264 	//
       
   265 	}
       
   266 
       
   267 static void testSetAlarms()
       
   268 //
       
   269 //	Test setting a day alarm
       
   270 //
       
   271 	{
       
   272 	TTime inAFewMinutes;
       
   273 	inAFewMinutes.HomeTime();
       
   274 	inAFewMinutes += TTimeIntervalMinutes(2);
       
   275 
       
   276 	TTime inThreeDays;
       
   277 	inThreeDays.HomeTime();
       
   278 	inThreeDays += TTimeIntervalDays(3);
       
   279 	//
       
   280 	
       
   281 	testSetSessionAlarm(_L("Session alarm, in a few minutes"), inAFewMinutes);
       
   282 	
       
   283 	
       
   284 	testSetSessionAlarm(_L("Session alarm, in a few days"), inThreeDays);
       
   285 	}
       
   286 
       
   287 
       
   288 static void testTakeOverOrphanL()
       
   289 //
       
   290 //	Test whether a session alarm takes over an orphaned alarm
       
   291 //
       
   292 	{
       
   293 	TheAlarmTest.Test().Next(_L("Take over orphaned"));
       
   294 	TheAlarmTest.TestClearStoreL();
       
   295 	testOtherNotify(ETrue);
       
   296 
       
   297 	// Get all events from the event buffer
       
   298 	TAlarmId alarmId;
       
   299 	TRequestStatus notificationStatus;
       
   300 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   301 	TheAlarmTest(notificationStatus == KRequestPending, __LINE__);
       
   302 
       
   303 	// Add session alarm
       
   304 	TASShdAlarm alarm;
       
   305 	TTime time;
       
   306 	time.HomeTime();
       
   307 	time += TTimeIntervalDays(3);
       
   308 
       
   309 	// Create session alarm
       
   310 	TAlarmId sessionAlarmId = SetSessionAlarm(alarm, time, TheSsnStatus);
       
   311 	TheAlarmTest(TheSsnStatus == KRequestPending, __LINE__);
       
   312 
       
   313 	// Check that the notification thread was updated
       
   314 	testOtherNotify(ETrue);
       
   315 
       
   316 	// Check that the notification request was correctly updated
       
   317 	User::WaitForRequest(notificationStatus);
       
   318 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   319 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   320 
       
   321 	// Check the number of orphaned alarms is 0
       
   322 	TInt n = TheAlarmTest.CountOrphanedAlarmsL();
       
   323 	TheAlarmTest(n == 0, __LINE__);
       
   324 
       
   325 	// Orphan the alarm
       
   326 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   327 	TheAlarmTest(TheAlarmTest.Session().SetAlarmCharacteristics(sessionAlarmId, 0) == KErrNone, __LINE__);
       
   328 	TheAlarmTest(TheSsnStatus == KErrCancel, __LINE__);
       
   329 
       
   330 	// Check that the notification thread was updated
       
   331 	testOtherNotify(ETrue);
       
   332 
       
   333 	// Check that the notification request was correctly updated
       
   334 	User::WaitForRequest(notificationStatus);
       
   335 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   336 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   337 
       
   338 	// Check the number of orphaned alarms is 1
       
   339 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   340 	TheAlarmTest(n == 1, __LINE__);
       
   341 
       
   342 	// Create another session alarm
       
   343 	time.HomeTime();
       
   344 	time += TTimeIntervalDays(4);
       
   345 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   346 	sessionAlarmId = SetSessionAlarm(alarm, time, TheSsnStatus);
       
   347 	TheAlarmTest(TheSsnStatus == KRequestPending, __LINE__);
       
   348 
       
   349 	// Check that the notification thread was updated
       
   350 	testOtherNotify(ETrue);
       
   351 
       
   352 	// Check that the notification request was correctly updated
       
   353 	User::WaitForRequest(notificationStatus);
       
   354 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   355 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   356 
       
   357 	// Check the number of orphaned alarms is 1
       
   358 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   359 	TheAlarmTest(n == 1, __LINE__);
       
   360 
       
   361 	// Orphan the alarm
       
   362 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   363 	TheAlarmTest(TheAlarmTest.Session().SetAlarmCharacteristics(sessionAlarmId, 0) == KErrNone, __LINE__);
       
   364 	TheAlarmTest(TheSsnStatus == KErrCancel, __LINE__);
       
   365 
       
   366 	// Check that the notification thread was updated
       
   367 	testOtherNotify(ETrue);
       
   368 
       
   369 	// Check that the notification request was correctly updated
       
   370 	User::WaitForRequest(notificationStatus);
       
   371 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   372 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   373 
       
   374 	// Check the number of orphaned alarms is 2
       
   375 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   376 	TheAlarmTest(n == 2, __LINE__);
       
   377 
       
   378 	// Create another session alarm
       
   379 	time.HomeTime();
       
   380 	time += TTimeIntervalDays(5);
       
   381 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   382 	sessionAlarmId = SetSessionAlarm(alarm, time, TheSsnStatus);
       
   383 	TheAlarmTest(TheSsnStatus == KRequestPending, __LINE__);
       
   384 
       
   385 	// Check that the notification thread was updated
       
   386 	testOtherNotify(ETrue);
       
   387 
       
   388 	// Check that the notification request was correctly updated
       
   389 	User::WaitForRequest(notificationStatus);
       
   390 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   391 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   392 
       
   393 	// Check the number of orphaned alarms is 2
       
   394 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   395 	TheAlarmTest(n == 2, __LINE__);
       
   396 
       
   397 	// Cancel session alarm
       
   398 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   399 	TheAlarmTest(notificationStatus == KRequestPending, __LINE__);
       
   400 	TheAlarmTest.Session().AlarmNotificationCancelAndDequeue(sessionAlarmId);
       
   401 	User::WaitForRequest(TheSsnStatus);
       
   402 	TheAlarmTest(TheSsnStatus == KErrCancel, __LINE__);
       
   403 
       
   404 	// Check that the notification thread was updated
       
   405 	testOtherNotify(ETrue);
       
   406 
       
   407 	// Check that the notification request was correctly updated
       
   408 	User::WaitForRequest(notificationStatus);
       
   409 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   410 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   411 
       
   412 	// Check the number of orphaned alarms is 2
       
   413 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   414 	TheAlarmTest(n == 2, __LINE__);
       
   415 	}
       
   416 
       
   417 static TAlarmId SetSessionUtcAlarm(TASShdAlarm& aAlarm, const TTime& aTime, TRequestStatus& aStatus)
       
   418 //
       
   419 //	Set the session UTC alarm
       
   420 //
       
   421 	{
       
   422 	TheAlarmTest(TheNotifyFlag == EFalse, __LINE__);
       
   423 	//
       
   424 	aAlarm.Reset();
       
   425 	aAlarm.Message()=(_L("The session UTC alarm"));
       
   426 	aAlarm.SetUtcNextDueTime(aTime);
       
   427 	aAlarm.Characteristics().Set(EAlarmCharacteristicsSessionSpecific);
       
   428 	//
       
   429 	TheAlarmTest.Session().AlarmAddWithNotification(aStatus, aAlarm);
       
   430 	TheAlarmTest(aStatus == KRequestPending, __LINE__);
       
   431 	//
       
   432 	TheAlarmTest(aAlarm.Id() != KNullAlarmId, __LINE__);
       
   433 	return aAlarm.Id();
       
   434 	}
       
   435 
       
   436 static void testSetSessionUtcAlarm(const TDesC& aDes, const TTime& aDueTime)
       
   437 //
       
   438 //	Test setting a session UTC alarm
       
   439 //
       
   440 	{
       
   441 	TheAlarmTest.Test().Next(aDes);
       
   442 
       
   443 	// Check there's nothing ready
       
   444 	testOtherNotify(EFalse);
       
   445 
       
   446 	// Get all events from the event buffer
       
   447 	TAlarmId alarmId;
       
   448 	TRequestStatus notificationStatus;
       
   449 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   450 	TheAlarmTest(notificationStatus == KRequestPending, __LINE__);
       
   451 
       
   452 	// Queue session alarm
       
   453 	TRequestStatus sessionAlarmStatus;
       
   454 	TASShdAlarm alarm;
       
   455 	TAlarmId sessionAlarmId = SetSessionUtcAlarm(alarm, aDueTime, sessionAlarmStatus);
       
   456 
       
   457 	// Check that the notification thread was updated
       
   458 	testOtherNotify(ETrue);
       
   459 
       
   460 	// Check that the notification request was correctly updated
       
   461 	User::WaitForRequest(notificationStatus);
       
   462 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   463 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   464 
       
   465 	// Get details
       
   466 	TASShdAlarm alarm2;
       
   467 	TInt r = TheAlarmTest.Session().GetAlarmDetails(sessionAlarmId, alarm2);
       
   468 	TheAlarmTest(r == KErrNone, __LINE__);
       
   469 	TDateTime dateTime = aDueTime.DateTime();
       
   470 	TDateTime alarmTime = alarm2.NextDueTime().DateTime();
       
   471 
       
   472 	// Check details
       
   473 	TheAlarmTest(alarmTime.Day()==dateTime.Day(), __LINE__);
       
   474 	TheAlarmTest(alarmTime.Hour()==dateTime.Hour(), __LINE__);
       
   475 	TheAlarmTest(alarmTime.Minute()==dateTime.Minute(), __LINE__);
       
   476 	alarmTime = alarm2.NextDueTime().DateTime();
       
   477 	dateTime = aDueTime.DateTime();
       
   478 	TheAlarmTest(alarmTime.Day()==dateTime.Day(), __LINE__);
       
   479 	TheAlarmTest(alarmTime.Hour()==dateTime.Hour(), __LINE__);
       
   480 	TheAlarmTest(alarmTime.Minute()==dateTime.Minute(), __LINE__);
       
   481 
       
   482 	// Cancel alarm
       
   483 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   484 	TheAlarmTest(notificationStatus == KRequestPending, __LINE__);
       
   485 	TheAlarmTest.Session().AlarmNotificationCancelAndDequeue(sessionAlarmId);
       
   486 	User::WaitForRequest(sessionAlarmStatus);
       
   487 	TheAlarmTest(sessionAlarmStatus == KErrCancel, __LINE__);
       
   488 
       
   489 	// Check that the notification thread was updated
       
   490 	testOtherNotify(ETrue);
       
   491 
       
   492 	// Check that the notification request was correctly updated
       
   493 	User::WaitForRequest(notificationStatus);
       
   494 	TheAlarmTest(notificationStatus == EAlarmChangeEventAlarmDeletion, __LINE__);
       
   495 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   496 
       
   497 	// Add another alarm
       
   498 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   499 	sessionAlarmId = SetSessionUtcAlarm(alarm, aDueTime, sessionAlarmStatus);
       
   500 	testOtherNotify(ETrue);
       
   501 
       
   502 	// Check that the notification request was correctly updated
       
   503 	User::WaitForRequest(notificationStatus);
       
   504 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   505 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   506 
       
   507 	// Orphan alarm
       
   508 	r = TheAlarmTest.Session().SetAlarmCharacteristics(sessionAlarmId, EAlarmCharacteristicsSessionSpecific);
       
   509 	User::WaitForRequest(sessionAlarmStatus);
       
   510 	TheAlarmTest(sessionAlarmStatus == KErrCancel, __LINE__);
       
   511 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   512 
       
   513 	// Get details
       
   514 	r = TheAlarmTest.Session().GetAlarmDetails(sessionAlarmId, alarm);
       
   515 	TheAlarmTest(r == KErrNone, __LINE__);
       
   516 	TheAlarmTest(alarm.HasBecomeOrphaned(), __LINE__);
       
   517 	testOtherNotify(ETrue);
       
   518 	}
       
   519 
       
   520 static void testImmediateCloseUtc()
       
   521 //
       
   522 //	Test closing a session after the alarm goes off
       
   523 //	The session alarm must be on the notification queue but not first
       
   524 //	So that its session paramter is not cleared when its owner's name needs
       
   525 //	to be found.
       
   526 //
       
   527 	{
       
   528 	TTime time;
       
   529 	time.UniversalTime();
       
   530 	TheAlarmTest(TheNotifyFlag == EFalse, __LINE__);
       
   531 
       
   532 	TASShdAlarm alarm;
       
   533 	alarm.SoundName()=(_L("Signal"));
       
   534 	alarm.SetUtcNextDueTime(time - TTimeIntervalSeconds(75));
       
   535 	alarm.Message()=(_L("Session alarm"));
       
   536 	//
       
   537 	RASCliSession almSvr;
       
   538 	TheAlarmTest(almSvr.Connect() == KErrNone, __LINE__);
       
   539 	almSvr.AlarmAddWithNotification(TheSsnStatus, alarm);
       
   540 	//
       
   541 	User::After(1000000);
       
   542 	const TAlarmId sessionAlarmId = alarm.Id();
       
   543 	TheAlarmTest(sessionAlarmId != KNullAlarmId, __LINE__);
       
   544 	//
       
   545 	alarm.Reset();
       
   546 	alarm.SoundName()=(_L("Signal"));
       
   547 	alarm.SetUtcNextDueTime(time - TTimeIntervalSeconds(65));
       
   548 	alarm.Message()=(_L("Clock alarm"));
       
   549 	//
       
   550 	TheAlarmTest(almSvr.AlarmAdd(alarm) == KErrNone, __LINE__);
       
   551 	TheAlarmTest(alarm.Id() != KNullAlarmId && alarm.Id() != sessionAlarmId, __LINE__);
       
   552 	//
       
   553 	almSvr.Close();
       
   554 	//
       
   555 	}
       
   556 
       
   557 static void testSetUtcAlarms()
       
   558 //
       
   559 //	Test setting a day alarm
       
   560 //
       
   561 	{
       
   562 	TTime inAFewMinutes;
       
   563 	inAFewMinutes.UniversalTime();
       
   564 	inAFewMinutes += TTimeIntervalMinutes(2);
       
   565 
       
   566 	TTime inThreeDays;
       
   567 	inThreeDays.UniversalTime();
       
   568 	inThreeDays += TTimeIntervalDays(3);
       
   569 	//
       
   570 	
       
   571 	
       
   572 	testSetSessionUtcAlarm(_L("Session alarm, in a few minutes"), inAFewMinutes);
       
   573 	
       
   574 	
       
   575 	testSetSessionUtcAlarm(_L("Session alarm, in a few days"), inThreeDays);
       
   576 	}
       
   577 
       
   578 
       
   579 static void testTakeOverOrphanUtcL()
       
   580 //
       
   581 //	Test whether a session alarm takes over an orphaned alarm
       
   582 //
       
   583 	{
       
   584 	TheAlarmTest.Test().Next(_L("Take over orphaned UTC"));
       
   585 	TheAlarmTest.TestClearStoreL();
       
   586 	testOtherNotify(ETrue);
       
   587 
       
   588 	// Get all events from the event buffer
       
   589 	TAlarmId alarmId;
       
   590 	TRequestStatus notificationStatus;
       
   591 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   592 	TheAlarmTest(notificationStatus == KRequestPending, __LINE__);
       
   593 
       
   594 	// Add session alarm
       
   595 	TASShdAlarm alarm;
       
   596 	TTime time;
       
   597 	time.HomeTime();
       
   598 	time += TTimeIntervalDays(3);
       
   599 
       
   600 	// Create session alarm
       
   601 	TAlarmId sessionAlarmId = SetSessionUtcAlarm(alarm, time, TheSsnStatus);
       
   602 	TheAlarmTest(TheSsnStatus == KRequestPending, __LINE__);
       
   603 
       
   604 	// Check that the notification thread was updated
       
   605 	testOtherNotify(ETrue);
       
   606 
       
   607 	// Check that the notification request was correctly updated
       
   608 	User::WaitForRequest(notificationStatus);
       
   609 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   610 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   611 
       
   612 	// Check the number of orphaned alarms is 0
       
   613 	TInt n = TheAlarmTest.CountOrphanedAlarmsL();
       
   614 	TheAlarmTest(n == 0, __LINE__);
       
   615 
       
   616 	// Orphan the alarm
       
   617 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   618 	TheAlarmTest(TheAlarmTest.Session().SetAlarmCharacteristics(sessionAlarmId, 0) == KErrNone, __LINE__);
       
   619 	TheAlarmTest(TheSsnStatus == KErrCancel, __LINE__);
       
   620 
       
   621 	// Check that the notification thread was updated
       
   622 	testOtherNotify(ETrue);
       
   623 
       
   624 	// Check that the notification request was correctly updated
       
   625 	User::WaitForRequest(notificationStatus);
       
   626 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   627 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   628 
       
   629 	// Check the number of orphaned alarms is 1
       
   630 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   631 	TheAlarmTest(n == 1, __LINE__);
       
   632 
       
   633 	// Create another session alarm
       
   634 	time.HomeTime();
       
   635 	time += TTimeIntervalDays(4);
       
   636 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   637 	sessionAlarmId = SetSessionUtcAlarm(alarm, time, TheSsnStatus);
       
   638 	TheAlarmTest(TheSsnStatus == KRequestPending, __LINE__);
       
   639 
       
   640 	// Check that the notification thread was updated
       
   641 	testOtherNotify(ETrue);
       
   642 
       
   643 	// Check that the notification request was correctly updated
       
   644 	User::WaitForRequest(notificationStatus);
       
   645 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   646 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   647 
       
   648 	// Check the number of orphaned alarms is 1
       
   649 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   650 	TheAlarmTest(n == 1, __LINE__);
       
   651 
       
   652 	// Orphan the alarm
       
   653 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   654 	TheAlarmTest(TheAlarmTest.Session().SetAlarmCharacteristics(sessionAlarmId, 0) == KErrNone, __LINE__);
       
   655 	TheAlarmTest(TheSsnStatus == KErrCancel, __LINE__);
       
   656 
       
   657 	// Check that the notification thread was updated
       
   658 	testOtherNotify(ETrue);
       
   659 
       
   660 	// Check that the notification request was correctly updated
       
   661 	User::WaitForRequest(notificationStatus);
       
   662 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   663 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   664 
       
   665 	// Check the number of orphaned alarms is 2
       
   666 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   667 	TheAlarmTest(n == 2, __LINE__);
       
   668 
       
   669 	// Create another session alarm
       
   670 	time.HomeTime();
       
   671 	time += TTimeIntervalDays(5);
       
   672 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   673 	sessionAlarmId = SetSessionUtcAlarm(alarm, time, TheSsnStatus);
       
   674 	TheAlarmTest(TheSsnStatus == KRequestPending, __LINE__);
       
   675 
       
   676 	// Check that the notification thread was updated
       
   677 	testOtherNotify(ETrue);
       
   678 
       
   679 	// Check that the notification request was correctly updated
       
   680 	User::WaitForRequest(notificationStatus);
       
   681 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   682 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   683 
       
   684 	// Check the number of orphaned alarms is 2
       
   685 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   686 	TheAlarmTest(n == 2, __LINE__);
       
   687 
       
   688 	// Cancel session alarm
       
   689 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(notificationStatus, alarmId);
       
   690 	TheAlarmTest(notificationStatus == KRequestPending, __LINE__);
       
   691 	TheAlarmTest.Session().AlarmNotificationCancelAndDequeue(sessionAlarmId);
       
   692 	User::WaitForRequest(TheSsnStatus);
       
   693 	TheAlarmTest(TheSsnStatus == KErrCancel, __LINE__);
       
   694 
       
   695 	// Check that the notification thread was updated
       
   696 	testOtherNotify(ETrue);
       
   697 
       
   698 	// Check that the notification request was correctly updated
       
   699 	User::WaitForRequest(notificationStatus);
       
   700 	TheAlarmTest(notificationStatus > 0, __LINE__);
       
   701 	TheAlarmTest(alarmId == sessionAlarmId, __LINE__);
       
   702 
       
   703 	// Check the number of orphaned alarms is 2
       
   704 	n = TheAlarmTest.CountOrphanedAlarmsL();
       
   705 	TheAlarmTest(n == 2, __LINE__);
       
   706 	}
       
   707 
       
   708 
       
   709 static void doTestsL()
       
   710 	{
       
   711 	RArray<TInt> instructionSet;
       
   712 	instructionSet.AppendL(EKeyEscape);
       
   713 	instructionSet.AppendL(EKeyEscape);
       
   714 	TheAlarmTest.TestStartServers(&instructionSet);
       
   715 	TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone, __LINE__);
       
   716 
       
   717 #if defined(_DEBUG)
       
   718 // The alarm should only be allowed to display notification in WINS builds.
       
   719 #if defined(__WINSCW__)
       
   720 	TheAlarmTest.Session().__DbgPreventUserNotify(EFalse);
       
   721 #else
       
   722 	TheAlarmTest.Session().__DbgPreventUserNotify(ETrue);
       
   723 #endif
       
   724 #endif
       
   725 
       
   726 	TheAlarmTest.TestClearStoreL();
       
   727 	testImmediateClose();
       
   728 	//
       
   729 	testSetupNotify(_L("NtfThread1"));
       
   730 	//
       
   731 
       
   732 	testSetAlarms();
       
   733 	testTakeOverOrphanL();
       
   734 	//
       
   735 	TheAlarmTest.TestClearStoreL();
       
   736 	// Reset the notify flag for the UTC test.
       
   737 	TheNotifyFlag = EFalse;
       
   738 	// Stop the notify thread so it can be restarted without causing an error.
       
   739 	TheNotifyThread = EFalse;
       
   740 	// Allow time for the thread to stop.
       
   741 	User::After(10000);
       
   742 	// Enable the thread so it can be subsequently restarted.
       
   743 	TheNotifyThread = ETrue;
       
   744 
       
   745 	TheAlarmTest.TestClearStoreL();
       
   746 	testImmediateCloseUtc();
       
   747 	//
       
   748 	testSetupNotify(_L("NtfThread2"));
       
   749 	//
       
   750 
       
   751 	testSetUtcAlarms();
       
   752 	testTakeOverOrphanUtcL();
       
   753 	//
       
   754 	TheAlarmTest.TestClearStoreL();
       
   755 	instructionSet.Close();
       
   756 	}
       
   757 
       
   758 /**
       
   759 @SYMTestCaseID PIM-TSESSIONALARMS-0001
       
   760 */	
       
   761 GLDEF_C TInt E32Main()
       
   762 //
       
   763 // Test the alarm server.
       
   764 //
       
   765     {
       
   766     TInt ret = KErrNone;
       
   767 	TheAlarmTest.Title();
       
   768 	TheAlarmTest.Start(_L("@SYMTestCaseID PIM-TSESSIONALARMS-0001 Session alarms"));
       
   769 
       
   770 	__UHEAP_MARK;
       
   771 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   772 	TRAPD(err, doTestsL());
       
   773 	UNUSED_VAR(err); //Used to supress build warnings
       
   774 
       
   775 	delete cleanup;
       
   776 	__UHEAP_MARKEND;
       
   777 
       
   778     TheAlarmTest.Session().Close();
       
   779     TRAP(ret,TheAlarmTest.EndL());
       
   780 	TheAlarmTest.Test().Close();
       
   781 	ASTstAlarmTest::Close();
       
   782 	//
       
   783 	return ret;
       
   784     }