commonappservices/alarmserver/Test/TBackupAlarms.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 "ASSrvDefs.h"
       
    18 
       
    19 #include <f32file.h>
       
    20 #include <s32file.h>
       
    21 #include <asclisoundplay.h>
       
    22 
       
    23 #include <connect/sbeclient.h>
       
    24 #include <tz.h>
       
    25 // Type definitions
       
    26 typedef CArrayFixFlat<TASCliSoundPlayDefinition> CPlayIntervalArray;
       
    27 
       
    28 
       
    29 // Literal constants
       
    30 _LIT(KAlarmServerTestBackupFile, "C:\\AlarmServer.bak");
       
    31 _LIT8(KSomeAlarmData, "This is some 8-bit data");
       
    32 
       
    33 //Temp fix for wk37, main updates for wk40
       
    34 _LIT(KAlarmServerBackupFileFullPath, "C:\\private\\101F5027\\AlarmServer.ini");
       
    35 
       
    36 static const TTimeIntervalMicroSeconds32 KTenSeconds = 10000000;
       
    37 
       
    38 #define KNewAlarmCycleStartOffsetMinutes		{0,10,20,30,50,100,200,300,450,600,900,1200,1800,2400, 3000, 4000, 5000, 10000,KErrNotFound};
       
    39 
       
    40 static RFs FsSession;
       
    41 static conn::CSBEClient* BackupClient;
       
    42 static TDriveList DriveList;
       
    43 
       
    44 
       
    45 //
       
    46 /* Try to give a fairly thorough test of the Alarm Server
       
    47  * during Startup Internalize, backup and restore
       
    48  */
       
    49 
       
    50 
       
    51 //
       
    52 // building block test Helper functions
       
    53 //
       
    54 
       
    55 static void Helper_StartBackupL()
       
    56 	{
       
    57 	// Use the Secure Backup Engine API to "start" a backup.  Ultimate effect
       
    58 	// is to cause CASSrvAlarmQueue::StartAlarmStoreOperation() to be called.
       
    59 	// This happens indirectly through CASSrvAlarmStore::BackupBeginningL()/
       
    60 	// BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
    61 	// These methods are implementations for MBackupRestoreNotificatioObserver
       
    62 	// which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
    63 	// This P&S property is changed by calling the Secure Backup Engine API
       
    64 	// below.
       
    65 	BackupClient->SetBURModeL(DriveList, conn::EBURBackupFull, conn::EBackupBase);
       
    66 	}
       
    67 
       
    68 
       
    69 static void Helper_EndBackupL()
       
    70 	{
       
    71 	// Use the Secure Backup Engine API to "end" a backup.  Ultimate effect
       
    72 	// is to cause CASSrvAlarmQueue::StartAlarmStoreOperation() to be called.
       
    73 	// This happens indirectly through CASSrvAlarmStore::BackupBeginningL()/
       
    74 	// BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
    75 	// These methods are implementations for MBackupRestoreNotificatioObserver
       
    76 	// which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
    77 	// This P&S property is changed by calling the Secure Backup Engine API
       
    78 	// below.
       
    79 	BackupClient->SetBURModeL(DriveList, conn::EBURNormal, conn::ENoBackup);
       
    80 
       
    81 	// allow Alarm Server observers to be notified
       
    82 	User::After(1000);
       
    83 	}
       
    84 
       
    85 
       
    86 static void Helper_StartRestoreL()
       
    87 	{
       
    88 	// Use the Secure Backup Engine API to "start" a restore.  Ultimate effect
       
    89 	// is to cause CASSrvAlarmQueue::StartAlarmStoreOperation() to be called.
       
    90 	// This happens indirectly through CASSrvAlarmStore::BackupBeginningL()/
       
    91 	// BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
    92 	// These methods are implementations for MBackupRestoreNotificatioObserver
       
    93 	// which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
    94 	// This P&S property is changed by calling the Secure Backup Engine API
       
    95 	// below.
       
    96 	BackupClient->SetBURModeL(DriveList, conn::EBURRestoreFull, conn::EBackupBase);
       
    97 
       
    98 	// allow Alarm Server observers to be notified
       
    99 	User::After(100000);
       
   100 	}
       
   101 
       
   102 
       
   103 static void Helper_EndRestoreL(TBool aSuccess)
       
   104 	{
       
   105 	// Use the Secure Backup Engine API to "end" a restore.  Ultimate effect
       
   106 	// is to cause CASSrvAlarmQueue::StartAlarmStoreOperation() to be called.
       
   107 	// This happens indirectly through CASSrvAlarmStore::BackupBeginningL()/
       
   108 	// BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
   109 	// These methods are implementations for MBackupRestoreNotificatioObserver
       
   110 	// which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
   111 	// This P&S property is changed by calling the Secure Backup Engine API
       
   112 	// below.
       
   113 	if (aSuccess)
       
   114 		{
       
   115 		BackupClient->SetBURModeL(DriveList, conn::EBURNormal, conn::ENoBackup);
       
   116 		}
       
   117 	else
       
   118 		{
       
   119 		// starting a backup before a restore has completed.
       
   120 		BackupClient->SetBURModeL(DriveList, conn::EBURBackupFull, conn::EBackupBase);
       
   121 		}
       
   122 
       
   123 	// allow Alarm Server observers to be notified
       
   124 	User::After(1000000);
       
   125 	}
       
   126 
       
   127 
       
   128 static CDirectFileStore* Helper_StartRestoreAndOpenL()
       
   129 	{
       
   130 	Helper_StartRestoreL();
       
   131 
       
   132 	TheAlarmTest.Test().Printf(_L("- Open and Lock Exclusive AlarmServer.Ini file\r\n"));
       
   133 	// actually lock the file C:\System\Data\AlarmServer.ini -
       
   134 	CDirectFileStore* store = NULL;
       
   135 	for(TUint i=0; i < 10; i++)
       
   136 		{
       
   137 		TRAPD(err, store = CDirectFileStore::OpenL(FsSession,
       
   138 			KAlarmServerBackupFileFullPath, EFileRead | EFileShareExclusive));
       
   139 		// success?
       
   140 		if (err == KErrNone)
       
   141 			break;
       
   142 		if (err != KErrInUse)
       
   143 			User::Leave(err);
       
   144 		// wait a little (10ms) & try again
       
   145 		User::After(10000);
       
   146 		}
       
   147 
       
   148 	return store;
       
   149 	}
       
   150 
       
   151 
       
   152 static CDirectFileStore* Helper_StartBackupAndOpenL()
       
   153 	{
       
   154 	Helper_StartBackupL();
       
   155 
       
   156 	TheAlarmTest.Test().Printf(_L("- Open and Lock Share Readers AlarmServer.Ini file\r\n"));
       
   157 	// actually lock the file C:\System\Data\AlarmServer.ini -
       
   158 	CDirectFileStore* store = NULL;
       
   159 	for(TUint i=0; i < 10; i++)
       
   160 		{
       
   161 		TRAPD(err, store = CDirectFileStore::OpenL(FsSession,
       
   162 			KAlarmServerBackupFileFullPath, EFileRead | EFileShareReadersOnly));
       
   163 		// success?
       
   164 		if (err == KErrNone)
       
   165 			break;
       
   166 		if (err != KErrInUse)
       
   167 			User::Leave(err);
       
   168 		// wait a little (10ms) & try again
       
   169 		User::After(10000);
       
   170 		}
       
   171 
       
   172 	return store;
       
   173 	}
       
   174 
       
   175 
       
   176 //
       
   177 
       
   178 //
       
   179 // Backup & Restore support functions
       
   180 
       
   181 /*
       
   182  * Makes a Backup of the Alarm Server ini file:
       
   183  * 1. Asks for Read Only lock for the AlarmServer.ini file
       
   184  * 2. Copies AlarmServer.ini to c:\AlarmServer.bak
       
   185  * 3. Hands back the file lock
       
   186  */
       
   187 static void	BackupAlarmServerIniFileL()
       
   188 	{
       
   189 	TheAlarmTest.Test().Printf(_L("- Backup AlarmServer Ini file\r\n"));
       
   190 	Helper_StartBackupL();	// make Backup
       
   191 
       
   192 	CFileMan* fm=CFileMan::NewL(FsSession);
       
   193 	FsSession.Delete(KAlarmServerTestBackupFile);		// no old file
       
   194 
       
   195 	// keep on trying to copy AlarmServer.ini file, until it will be released by alarm server
       
   196 	const TInt maxNumOfAttempts = 50;
       
   197 	TInt numOfAttempts = 0;
       
   198 	for (;numOfAttempts < maxNumOfAttempts; ++numOfAttempts)
       
   199 		{
       
   200 		if (KErrNone == fm->Copy(KAlarmServerBackupFileFullPath,KAlarmServerTestBackupFile))
       
   201 			break;
       
   202 
       
   203 		User::After(200000); //Wait for file to be released
       
   204 		}
       
   205 	TheAlarmTest(numOfAttempts < maxNumOfAttempts, __LINE__); //File was not locked after 50 attempts
       
   206 
       
   207 	delete fm;
       
   208 	Helper_EndBackupL();
       
   209 	}
       
   210 
       
   211 
       
   212 /*
       
   213  * Restores from a Backup of the Alarm Server ini file:
       
   214  * 1. Asks for full access file lock for the AlarmServer.ini file
       
   215  * 2. Copies c:\AlarmServer.bak to the AlarmServer.ini file
       
   216  * 3. Hands back the file lock
       
   217  */
       
   218 static void RestoreAlarmServerIniFileL()
       
   219 	{
       
   220 	TheAlarmTest.Test().Printf(_L("- Restore AlarmServer Ini file\r\n"));
       
   221 
       
   222 	// variables used to check Alarm Server notifications of Restore
       
   223 	TAlarmId alarmId;
       
   224 	TRequestStatus status;
       
   225 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
   226 
       
   227 	Helper_StartRestoreL();
       
   228 	TheAlarmTest.TestEventBuffer(EAlarmChangeEventRestoreStarted, status, alarmId);
       
   229 
       
   230 	CFileMan* fm=CFileMan::NewL(FsSession);
       
   231 	// didn't leave yet so:
       
   232 	FsSession.Delete(KAlarmServerBackupFileFullPath);		// zap current file
       
   233 	fm->Copy(KAlarmServerTestBackupFile,KAlarmServerBackupFileFullPath);
       
   234 	delete fm;
       
   235 
       
   236 	Helper_EndRestoreL(ETrue);
       
   237 	}
       
   238 
       
   239 
       
   240 /*
       
   241  * Pretends to start to make a Backup of the Alarm Server ini file:
       
   242  * 1. Asks for Read Only lock for the AlarmServer.ini file
       
   243  * Must delete store object and call SimulateBackupServerBackup_CompleteL()
       
   244  * after calling this!
       
   245  */
       
   246 static CStreamStore* SimulateBackupServerBackup_StartL()
       
   247 	{
       
   248 	TheAlarmTest.Test().Printf(_L("- Claim AlarmServer Ini file for Backup\r\n"));
       
   249 
       
   250 	// want to start a Backup and open the AlarmServer Ini file
       
   251 	return Helper_StartBackupAndOpenL();
       
   252 	}
       
   253 
       
   254 
       
   255 /*
       
   256  * Ends pretend Backup of the Alarm Server ini file:
       
   257  * 1. Hands back the file lock
       
   258  * Must be preceded by a call to SimulateBackupServerBackup_StartL
       
   259  * and delete of the store object.
       
   260  */
       
   261 static void SimulateBackupServerBackup_CompleteL()
       
   262 	{
       
   263 	TheAlarmTest.Test().Printf(_L("- ... Backup finished, return AlarmServer file lock\r\n"));
       
   264 
       
   265 	Helper_EndBackupL();
       
   266 	}
       
   267 
       
   268 /*
       
   269  * Pretends to Restore the Alarm Server ini file:
       
   270  * 1. Asks for full access file lock for the AlarmServer.ini file
       
   271  * 2. Pauses briefly.
       
   272  * 3. Hands back the file lock
       
   273  * Alarm Server should re-Internalize existing alarms;
       
   274  * Session alarms should become orphaned.
       
   275  */
       
   276 static void SimulateBackupServerRestorationL()
       
   277 	{
       
   278 	TheAlarmTest.Test().Printf(_L("- 'Restore' AlarmServer Ini file, (open and release)\r\n"));
       
   279 
       
   280 	// want to start a Restore and open AlarmServer Ini file
       
   281 	CDirectFileStore* store = Helper_StartRestoreAndOpenL();
       
   282 
       
   283 	// take a little time for the Restore (.5s)
       
   284 	User::After(500000);
       
   285 
       
   286 	// now release file lock -
       
   287 	TheAlarmTest.Test().Printf(_L("- Unlock AlarmServer.Ini file\r\n"));
       
   288 	delete store;
       
   289 
       
   290 	// restore success
       
   291 	Helper_EndRestoreL(ETrue);
       
   292 	}
       
   293 
       
   294 /*
       
   295  * Pretends to start to Restore the Alarm Server ini file:
       
   296  * 1. Asks for full access file lock for the AlarmServer.ini file
       
   297  * Must be followed by a call to SimulateBackupServerRestore_Complete!
       
   298  */
       
   299 static CStreamStore* SimulateBackupServerRestore_StartL()
       
   300 	{
       
   301 	CStreamStore* store = NULL;
       
   302 	TInt err = KErrNone;
       
   303 
       
   304 	TheAlarmTest.Test().Printf(_L("- Access AlarmServer Ini file, as if  Restoring\r\n"));
       
   305 
       
   306 	// Use this Secure Backup Engine API to "start" a restore.  Ultimate effect
       
   307 	// is to cause CASSrvAlarmQueue::StartAlarmStoreOperation() to be called.
       
   308 	// This happens indirectly through CASSrvAlarmStore::BackupBeginningL()/
       
   309 	// BackupCompletedL()/RestoreBeginningL()/RestoreCompletedL() methods.
       
   310 	// These methods are implementations for MBackupRestoreNotificatioObserver
       
   311 	// which monitors changes in the P&S property conn::KUidBackupRestoreKey.
       
   312 	// This P&S property is changed by calling the Secure Backup Engine API
       
   313 	// below.
       
   314 	BackupClient->SetBURModeL(DriveList, conn::EBURRestoreFull, conn::EBackupBase);
       
   315 
       
   316 	// actually lock the file C:\System\Data\AlarmServer.ini
       
   317 	const TInt maxNumOfAttempts = 50;
       
   318 	TInt numOfAttempts = 0;
       
   319 	for (;numOfAttempts < maxNumOfAttempts; ++numOfAttempts)
       
   320 		{
       
   321 		TRAP(err,store = CDirectFileStore::OpenL(FsSession, KAlarmServerBackupFileFullPath,
       
   322 												 EFileRead | EFileShareExclusive));
       
   323 		RDebug::Print(_L("CDirectFileStore::OpenL %S, err: %d"), &KAlarmServerBackupFileFullPath, err);
       
   324 		if (err == KErrNone)
       
   325 			break;
       
   326 
       
   327 		User::After(200000); //Wait for file to be released
       
   328 		}
       
   329 	TheAlarmTest(numOfAttempts < maxNumOfAttempts, __LINE__); //File was not locked after 50 attempts
       
   330 
       
   331 	return store;
       
   332 	}
       
   333 
       
   334 /*
       
   335  * Ends pretend Restore of the Alarm Server ini file:
       
   336  * 1. Hands back the file lock
       
   337  * Alarm Server should re-Initernalize existing alarms;
       
   338  * Session alarms should become orphaned.
       
   339  * Must be preceded by acall to SimulateBackupServerRestore_StartLC
       
   340  */
       
   341 static void SimulateBackupServerRestore_CompleteL()
       
   342 	{
       
   343 	TheAlarmTest.Test().Printf(_L("- Restore finished, release AlarmServer Ini file\r\n"));
       
   344 
       
   345 	Helper_EndRestoreL(ETrue);
       
   346 	}
       
   347 
       
   348 /*
       
   349  * Ends pretend Restore of the Alarm Server ini file:
       
   350  * 1. Hands back the file lock
       
   351  * Alarm Server should re-Initernalize existing alarms;
       
   352  * Session alarms should become orphaned.
       
   353  * Must be preceded by a call to SimulateBackupServerRestore_StartL
       
   354  * and delete of the store object.
       
   355  */
       
   356 //
       
   357 // Shutdown & Restart of Alarm Server only possibly in Windows Udeb builds
       
   358 
       
   359 #if defined(_DEBUG) && defined (__WINSCW__)
       
   360 static void closeServers()
       
   361 	{
       
   362 	TInt r=TheAlarmTest.Session().Connect();
       
   363 	TheAlarmTest(r==KErrNone, __LINE__);
       
   364 	TheAlarmTest.Test().Printf(_L("Server shut down\r\n"));
       
   365 	TheAlarmTest.Test().Printf(_L("now closing server...\r\n"));
       
   366 	TheAlarmTest.Session().__DbgShutDownServer();
       
   367 	TheAlarmTest.Session().Close();
       
   368 	}
       
   369 
       
   370 static void shutDownAndStartUpServers()
       
   371 	{
       
   372 	closeServers();
       
   373 	User::After(5000000);
       
   374 	TheAlarmTest.Test().Printf(_L("now restarting server...\r\n"));
       
   375 	TheAlarmTest.TestStartServers();
       
   376 	}
       
   377 #endif
       
   378 
       
   379 static TAlarmId addUtcAlarm(TInt aMinutesFromNow, TAlarmRepeatDefinition aRepeatDefinition = EAlarmRepeatDefintionRepeatOnce)
       
   380 	{
       
   381 	TBuf<200> testTitle;
       
   382 	testTitle.Format(_L("- Adding alarm set with UTC time to expire in %d minutes"), aMinutesFromNow);
       
   383 	TheAlarmTest.Test().Next(testTitle);
       
   384 	//
       
   385 	TASShdAlarm alarm;
       
   386 	alarm.Category()=KASCliCategoryClock;
       
   387 	alarm.RepeatDefinition()=aRepeatDefinition;
       
   388 	TTime time;
       
   389 	time.UniversalTime();
       
   390 	time += TTimeIntervalMinutes(aMinutesFromNow);
       
   391 	alarm.SetUtcNextDueTime(time);
       
   392 	//
       
   393 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   394 	const TAlarmId id = alarm.Id();
       
   395 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
   396 	// allow servers to catch up
       
   397 	User::After(1000000);
       
   398 	return id;
       
   399 	}
       
   400 
       
   401 static TAlarmId addAlarm(TInt aMinutesFromNow, TAlarmRepeatDefinition aRepeatDefinition = EAlarmRepeatDefintionRepeatOnce)
       
   402 	{
       
   403 	TBuf<200> testTitle;
       
   404 	testTitle.Format(_L("- Adding alarm set with local wall-clock time to expire in %d minutes"), aMinutesFromNow);
       
   405 	TheAlarmTest.Test().Next(testTitle);
       
   406 	//
       
   407 	TASShdAlarm alarm;
       
   408 	alarm.Category() = KASCliCategoryClock;
       
   409 	alarm.RepeatDefinition() = aRepeatDefinition;
       
   410 	alarm.NextDueTime().HomeTime();
       
   411 	alarm.NextDueTime() += TTimeIntervalMinutes(aMinutesFromNow);
       
   412 
       
   413 	//
       
   414 	TDateTime date=alarm.NextDueTime().DateTime();
       
   415 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alarm) == KErrNone, __LINE__);
       
   416 	const TAlarmId id = alarm.Id();
       
   417 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
   418 	TheAlarmTest.Test().Printf(_L("alarm %d due time is : hours: %d, minutes: %d, seconds: %d.\r\n"),
       
   419 		     id,date.Hour(), date.Minute(), date.Second());
       
   420 	// allow servers to catch up
       
   421 	User::After(1000000);
       
   422 	return id;
       
   423 	}
       
   424 
       
   425 //
       
   426 //
       
   427 //
       
   428 
       
   429 
       
   430 /*****************************************************************************
       
   431  */
       
   432 
       
   433 static void doTestAlarmQueueOrderShutdownStartupL()
       
   434 	{
       
   435 	TheAlarmTest.Test().Next(_L("* Test alarms set using local wall-clock time order stable over server shutdown/startup"));
       
   436 
       
   437 #if !defined(_DEBUG) || !defined(__WINSCW__)
       
   438 	// Can't shutdown Alarm Server on UREL or target builds
       
   439 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A WINS - DEBUG TEST ONLY\r\n"));
       
   440 #else
       
   441 	TheAlarmTest.TestClearStoreL();
       
   442 
       
   443 	//add alarms
       
   444 	TheAlarmTest.Test().Next(_L("add 5 alarms"));
       
   445 	RArray<TAlarmId> alarmIds;
       
   446 	for	(TInt ii=2; ii<=6; ii++)
       
   447 		{
       
   448 		TAlarmId id = addAlarm(ii);
       
   449 		alarmIds.Append(id);
       
   450 		}
       
   451 
       
   452 	TheAlarmTest.Test().Next(_L("shutdown then restart Alarm Server"));
       
   453 	shutDownAndStartUpServers();
       
   454 
       
   455 	//get server alarms
       
   456 	RArray<TAlarmId> serverAlarmIds;
       
   457 	TheAlarmTest.Session().GetAlarmIdListForCategoryL(KASCliCategoryClock, serverAlarmIds);
       
   458 	alarmIds.Sort();
       
   459 	serverAlarmIds.Sort();
       
   460 	TInt count = alarmIds.Count();
       
   461 	TheAlarmTest(count == serverAlarmIds.Count(), __LINE__);
       
   462 	for	(TInt jj=0; jj<count; jj++)
       
   463 		{
       
   464 		TheAlarmTest(alarmIds[jj] == serverAlarmIds[jj], __LINE__);
       
   465 		}
       
   466 	serverAlarmIds.Close();
       
   467 	alarmIds.Close();
       
   468 #endif
       
   469 	}
       
   470 
       
   471 /*****************************************************************************
       
   472  */
       
   473 
       
   474 static void doTestAlarmQueueOrderBackupRestoreL()
       
   475 	{
       
   476 	TheAlarmTest.Test().Next(_L("* Test alarm order across Backup and Restore"));
       
   477 
       
   478 	TheAlarmTest.TestClearStoreL();
       
   479 
       
   480 	//add alarms
       
   481 	TheAlarmTest.Test().Next(_L("add 5 alarms"));
       
   482 	RArray<TAlarmId> alarmIds;
       
   483 	for	(TInt ii=2; ii<=6; ii++)
       
   484 		{
       
   485 		TAlarmId id = addAlarm(ii);
       
   486 		alarmIds.AppendL(id);
       
   487 		}
       
   488 
       
   489 	// brief pause before backup (.5s)
       
   490 	User::After(500000);
       
   491 
       
   492 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
   493 	TRAPD(err, BackupAlarmServerIniFileL());
       
   494 	TheAlarmTest(err==KErrNone, __LINE__);
       
   495 
       
   496 	// delete alarms
       
   497 	TheAlarmTest.Test().Next(_L("delete alarms"));
       
   498 	TInt jj;
       
   499 	for (jj=0; jj<5; jj++)
       
   500 		{
       
   501 		TheAlarmTest(TheAlarmTest.Session().AlarmDelete(alarmIds[jj]) == KErrNone, __LINE__);
       
   502 		}
       
   503 
       
   504 	// pause before restore (5s)
       
   505 	User::After(5000000);
       
   506 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
   507 	TRAP(err, RestoreAlarmServerIniFileL());
       
   508 	TheAlarmTest(err==KErrNone, __LINE__);
       
   509 
       
   510 	TheAlarmTest.Test().Next(_L("check restored alarms"));
       
   511 	//get server alarms
       
   512 	RArray<TAlarmId> serverAlarmIds;
       
   513 	TheAlarmTest.Session().GetAlarmIdListForCategoryL(KASCliCategoryClock, serverAlarmIds);
       
   514 	alarmIds.Sort();
       
   515 	serverAlarmIds.Sort();
       
   516 	TInt count = alarmIds.Count();
       
   517 	TInt serverCount = serverAlarmIds.Count();
       
   518 	TheAlarmTest.Test().Printf(_L("[Added Alarms count = %i, server Count = %i]\r\n"), count, serverCount);
       
   519 	TheAlarmTest(count == serverCount, __LINE__);
       
   520 	for	(jj=0; jj<count; jj++)
       
   521 		{
       
   522 		TheAlarmTest(alarmIds[jj] == serverAlarmIds[jj], __LINE__);
       
   523 		}
       
   524 	serverAlarmIds.Close();
       
   525 	alarmIds.Close();
       
   526 	}
       
   527 
       
   528 /*****************************************************************************
       
   529  */
       
   530 
       
   531 static void doTestAlarmQueueBackupRestoreL()
       
   532 	{
       
   533 	TheAlarmTest.Test().Next(_L("* Test correct alarms in queue after backup and restore"));
       
   534 
       
   535 	TheAlarmTest.TestClearStoreL();
       
   536 
       
   537 	// add an alarm to expire in 2 minutes
       
   538 	TAlarmId id2 = addAlarm(2);
       
   539 	// add an alarm to expire in 3 minutes
       
   540 	TAlarmId id3 = addAlarm(3);
       
   541 
       
   542 	// brief pause before backup (.5s)
       
   543 	User::After(500000);
       
   544 
       
   545 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
   546 	TRAPD(err, BackupAlarmServerIniFileL());
       
   547 	TheAlarmTest(err==KErrNone, __LINE__);
       
   548 
       
   549 	// delete alarms
       
   550 	User::After(1000000);
       
   551 	TheAlarmTest.Test().Next(_L("delete alarms"));
       
   552 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(id2) == KErrNone, __LINE__);
       
   553 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(id3) == KErrNone, __LINE__);
       
   554 	User::After(1000000);
       
   555 
       
   556 	// add alarms
       
   557 	TheAlarmTest.Test().Next(_L("add alarms"));
       
   558 	// add alarms to expire now
       
   559 	TAlarmId id0 = addAlarm(0);
       
   560 	TAlarmId id1 = addAlarm(1);
       
   561 	TASShdAlarm alarm;
       
   562 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id0, alarm) == KErrNone, __LINE__);
       
   563 
       
   564 	// first alarm (id0) should be notifying
       
   565 	TheAlarmTest(alarm.State() == EAlarmStateNotifying, __LINE__);
       
   566 
       
   567 
       
   568 
       
   569 	// pause briefly before restore (2s)
       
   570 	User::After(2000000);
       
   571 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
   572 	TRAP(err, RestoreAlarmServerIniFileL());
       
   573 	TheAlarmTest(err==KErrNone, __LINE__);
       
   574 
       
   575 	TheAlarmTest.Test().Next(_L("check correct alarms after restore"));
       
   576 
       
   577 	// alarms should now be gone
       
   578 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id0, alarm) == KErrNotFound, __LINE__);
       
   579 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id1, alarm) == KErrNotFound, __LINE__);
       
   580 
       
   581 	// alarms should be restored
       
   582 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id2, alarm) == KErrNone, __LINE__);
       
   583 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id3, alarm) == KErrNone, __LINE__);
       
   584 	}
       
   585 
       
   586 /*****************************************************************************
       
   587  */
       
   588 
       
   589 static void doTestAlarmSoundIntervalsBackupRestoreL()
       
   590 	{
       
   591 	TheAlarmTest.Test().Next(_L("* Test correct Sound Intervals after backup and restore"));
       
   592 
       
   593 	TheAlarmTest.TestClearStoreL();
       
   594 
       
   595 	// Fetch the Alarm Sound Intervals
       
   596 	CPlayIntervalArray* originalSoundArray = new(ELeave) CPlayIntervalArray(20);
       
   597 	CleanupStack::PushL(originalSoundArray);
       
   598 	TheAlarmTest.Session().GetAlarmPlayIntervalsL(*originalSoundArray);
       
   599 
       
   600 	TheAlarmTest.Test().Next(_L("set non-default sound intervals"));
       
   601 
       
   602 	// Initialise sound array with default values
       
   603 	CPlayIntervalArray* newSoundArray = new(ELeave) CPlayIntervalArray(20);
       
   604 	CleanupStack::PushL(newSoundArray);
       
   605 
       
   606 	// Populate the new array to replace the default
       
   607 	const TInt minuteCycle[]=KNewAlarmCycleStartOffsetMinutes;
       
   608 	TInt i = 0;
       
   609 	TInt offset = minuteCycle[i];
       
   610 	while (offset != KErrNotFound)
       
   611 		{
       
   612 		const TASCliSoundPlayDefinition item(offset, KDefaultSoundPlayDurationInSeconds);
       
   613 		newSoundArray->AppendL(item);
       
   614 		offset = minuteCycle[++i];
       
   615 		}
       
   616 
       
   617 	// Try setting the sound details
       
   618 	TheAlarmTest.Session().SetAlarmPlayIntervalsL(*newSoundArray);
       
   619 
       
   620 	// brief pause before backup (.5s)
       
   621 	User::After(500000);
       
   622 
       
   623 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
   624 	TRAPD(err, BackupAlarmServerIniFileL());
       
   625 	TheAlarmTest(err==KErrNone, __LINE__);
       
   626 
       
   627 	TheAlarmTest.Test().Next(_L("reset default sound intervals"));
       
   628 	TheAlarmTest.Session().SetAlarmPlayIntervalsL(*originalSoundArray);
       
   629 
       
   630 	// pause briefly before restore (2s)
       
   631 	User::After(2000000);
       
   632 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
   633 	TRAP(err, RestoreAlarmServerIniFileL());
       
   634 	TheAlarmTest(err==KErrNone, __LINE__);
       
   635 
       
   636 	TheAlarmTest.Test().Next(_L("check correct sound intervals after restore"));
       
   637 
       
   638 	// Fetch the details back again.
       
   639 	CPlayIntervalArray* restoreSoundArray = new(ELeave) CPlayIntervalArray(20);
       
   640 	CleanupStack::PushL(restoreSoundArray);
       
   641 	TheAlarmTest.Session().GetAlarmPlayIntervalsL(*restoreSoundArray);
       
   642 
       
   643 	// Test that there are the same number of entries
       
   644 	TheAlarmTest(newSoundArray->Count() == restoreSoundArray->Count(), __LINE__);
       
   645 
       
   646 	// Test entries are the same
       
   647 	TInt count = newSoundArray->Count();
       
   648 	for(TInt j=0; j<count; j++)
       
   649 		{
       
   650 		const TASCliSoundPlayDefinition& item = newSoundArray->At(j);
       
   651 		const TASCliSoundPlayDefinition& item2 = restoreSoundArray->At(j);
       
   652 		TheAlarmTest(item.Offset().Int() == item2.Offset().Int(), __LINE__);
       
   653 		TheAlarmTest(item.Duration().Int() == item2.Duration().Int(), __LINE__);
       
   654 		}
       
   655 
       
   656 	TheAlarmTest.Test().Next(_L("reset default sound intervals"));
       
   657 	TheAlarmTest.Session().SetAlarmPlayIntervalsL(*originalSoundArray);
       
   658 
       
   659 	CleanupStack::PopAndDestroy(3);// originalSoundArray, newSoundArray, restoreSoundArray
       
   660 	}
       
   661 
       
   662 /*****************************************************************************
       
   663  */
       
   664 
       
   665 static void doTestAlarmAddDuringBackupL()
       
   666 	{
       
   667 	TheAlarmTest.Test().Next(_L("* Test alarm changes during backup"));
       
   668 
       
   669 	TheAlarmTest.TestClearStoreL();
       
   670 
       
   671 	// Attempt to add an alarm while the backup server is backing up the alarms
       
   672 	CStreamStore* store = NULL;
       
   673 	TRAPD(err, store = SimulateBackupServerBackup_StartL());
       
   674 	TheAlarmTest(err==KErrNone, __LINE__);
       
   675 
       
   676 	CleanupStack::PushL(store);
       
   677 
       
   678 	TheAlarmTest.Test().Next(_L("adding alarm should work"));
       
   679 
       
   680 	TASShdAlarm alm;
       
   681 	TheAlarmTest(addAlarm(0, alm.RepeatDefinition()) != KNullAlarmId, __LINE__);
       
   682 
       
   683 	TheAlarmTest.Test().Next(_L("changing time forces Alarm Store class to queue Externalize"));
       
   684 	// Locale change (time) x2 (INC038240 regression)
       
   685 	// Exercise CASSrvAlarmStore queue an Externalize
       
   686 	TTime time;
       
   687 	time.UniversalTime();
       
   688 	time += TTimeIntervalSeconds(10);
       
   689 	User::SetUTCTime(time);
       
   690 	User::After(1000000);
       
   691 	time -= TTimeIntervalSeconds(10);
       
   692 	User::SetUTCTime(time);
       
   693 	User::After(1000000);
       
   694 
       
   695 	// release file lock -
       
   696 	CleanupStack::PopAndDestroy(store); // store
       
   697 	// backup complete
       
   698 	TRAP(err, SimulateBackupServerBackup_CompleteL());
       
   699 	TheAlarmTest(err==KErrNone, __LINE__);
       
   700 
       
   701 	// wait for AlarmServer to catch-up
       
   702 	User::After(1000000);
       
   703 
       
   704 	TheAlarmTest.Test().Next(_L("check we can now add alarm"));
       
   705 
       
   706 	// re-try alarm add after backup complete - should be successful
       
   707 	TAlarmId id = addAlarm(0);
       
   708 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
   709 
       
   710 	// wait for AlarmServer to catch-up
       
   711 	User::After(1000000);
       
   712 
       
   713 	// make Alarm Server re-Internalize
       
   714 	TheAlarmTest.Test().Next(_L("now simulating restore..."));
       
   715 	TRAP(err, SimulateBackupServerRestorationL());
       
   716 	TheAlarmTest(err==KErrNone, __LINE__);
       
   717 
       
   718 	// check absence of alarm
       
   719 	TheAlarmTest.Test().Next(_L("check alarm has gone"));
       
   720 	TASShdAlarm alarm;
       
   721 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNotFound, __LINE__);
       
   722 	}
       
   723 
       
   724 /*****************************************************************************
       
   725  */
       
   726 
       
   727 static void doTestAlarmAddDuringRestoreL()
       
   728 	{
       
   729 	// variables used to check Alarm Server notifications of Restore
       
   730 	TAlarmId alarmId;
       
   731 	TRequestStatus status;
       
   732 
       
   733 	TheAlarmTest.Test().Next(_L("* Test alarm changes during restore"));
       
   734 
       
   735 	TheAlarmTest.TestClearStoreL();
       
   736 
       
   737 	// Attempt to add an alarm while the backup server is restoring up the alarms
       
   738 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
   739 	CStreamStore* store = NULL;
       
   740 	TRAPD(err, store = SimulateBackupServerRestore_StartL());
       
   741 	CleanupStack::PushL(store);
       
   742 	TheAlarmTest(err==KErrNone, __LINE__);
       
   743 
       
   744 	// check Restore Start notification received
       
   745 	TheAlarmTest.Test().Next(_L("check for Alarm Restore Start notification"));
       
   746 	User::After(1000000);
       
   747 	TheAlarmTest(status == EAlarmChangeEventRestoreStarted, __LINE__);
       
   748 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
   749 
       
   750 	TheAlarmTest(alarmId==KNullAlarmId, __LINE__);
       
   751 
       
   752 	TheAlarmTest.Test().Next(_L("adding alarm should fail"));
       
   753 
       
   754 	TASShdAlarm alm;
       
   755 	alm.NextDueTime().HomeTime();
       
   756 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alm) == KErrLocked, __LINE__);
       
   757 
       
   758 	// allow servers to catch up
       
   759 	User::After(1000000);
       
   760 
       
   761 	TheAlarmTest.Test().Next(_L("changing time forces Alarm Store class to queue Externalize"));
       
   762 	// Locale change (time) x2 (INC038240 regression)
       
   763 	// Exercise CASSrvAlarmStore
       
   764 
       
   765 	TTime time;
       
   766 	time.UniversalTime();
       
   767 	time += TTimeIntervalSeconds(10);
       
   768 	User::SetUTCTime(time);
       
   769 	User::After(1000000);
       
   770 	time -= TTimeIntervalSeconds(10);
       
   771 	User::SetUTCTime(time);
       
   772 	User::After(1000000);
       
   773 
       
   774 	CleanupStack::PopAndDestroy(store);
       
   775 
       
   776 	// restore complete
       
   777 	TRAP(err, SimulateBackupServerRestore_CompleteL());
       
   778 	TheAlarmTest(err==KErrNone, __LINE__);
       
   779 
       
   780 	// check Restore Complete notification received
       
   781 	TheAlarmTest.Test().Next(_L("check for Alarm Restore Complete notification"));
       
   782 	TheAlarmTest.TestEventBuffer(EAlarmChangeEventRestoreCompleted, status, alarmId);
       
   783 	TheAlarmTest(alarmId==KNullAlarmId, __LINE__);
       
   784 
       
   785 
       
   786 	// wait for AlarmServer to catch-up
       
   787 	User::After(1000000);
       
   788 
       
   789 	TheAlarmTest.Test().Next(_L("check we can now add alarm"));
       
   790 
       
   791 	// re-try alarm add after restore complete - should be successful
       
   792 	TAlarmId id = addAlarm(0);
       
   793 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
   794 
       
   795 	// wait for Alarm Server to externalize
       
   796 	User::After(3000000);
       
   797 	}
       
   798 
       
   799 /*****************************************************************************
       
   800  */
       
   801 
       
   802 
       
   803 
       
   804 
       
   805 /*****************************************************************************
       
   806  */
       
   807 
       
   808 static void doTestNotifyingAlarmShutdownStartupL()
       
   809 	{
       
   810 	TheAlarmTest.Test().Next(_L("* Test Notifying alarm across shutdown / startup"));
       
   811 
       
   812 	TheAlarmTest.TestClearStoreL();
       
   813 
       
   814 	TheAlarmTest.Test().Next(_L("add an alarm"));
       
   815 	// add an alarm to expire in 15 minutes
       
   816 	TAlarmId id = addAlarm(15);
       
   817 
       
   818 	// get alarm expiry time
       
   819 	TASShdAlarm alarm;
       
   820 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNone, __LINE__);
       
   821 	TTime time = alarm.NextDueTime();
       
   822 
       
   823 #if !defined(_DEBUG) || !defined(__WINSCW__)
       
   824 	// Can't shutdown Alarm Server on UREL or target builds
       
   825 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A WINS - DEBUG TEST ONLY\r\n"));
       
   826 
       
   827 	TheAlarmTest.Test().Printf(_L("Waiting for Alarm to expire\r\n"));
       
   828 	// target Alternative: wait until after alarm due time
       
   829 	User::SetHomeTime(time - TTimeIntervalSeconds(3));
       
   830 	User::After(KTenSeconds);
       
   831 #else
       
   832 	// UDEB:
       
   833 	// shut down the server
       
   834 	closeServers();
       
   835 
       
   836 	// re-start server after alarm expires
       
   837 	User::SetHomeTime(time - TTimeIntervalSeconds(3));
       
   838 	User::After(KTenSeconds);
       
   839 	TheAlarmTest.Test().Next(_L("now restarting server...\r\n"));
       
   840 	TheAlarmTest.TestStartServers();
       
   841 	User::After(1000000);
       
   842 #endif
       
   843 
       
   844 	// Internalise on startup has a 59s window for recent alarms
       
   845 	TheAlarmTest.Test().Next(_L("check that alarm is now set and notifying"));
       
   846 	//check status of alarm
       
   847 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNone, __LINE__);
       
   848 	TheAlarmTest(alarm.State() == EAlarmStateNotifying, __LINE__);
       
   849 
       
   850 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(id) == KErrNone, __LINE__);
       
   851 	}
       
   852 
       
   853 /*****************************************************************************
       
   854  */
       
   855 
       
   856 static void doTestNotifyingAlarmBackupRestoreL()
       
   857 	{
       
   858 	TheAlarmTest.Test().Next(_L("* Test Notifying alarm removed by backup / restore"));
       
   859 
       
   860 	TheAlarmTest.TestClearStoreL();
       
   861 
       
   862 	TheAlarmTest.Test().Next(_L("add an alarm"));
       
   863 	// add an alarm to expire in 15 minutes
       
   864 	TAlarmId id = addAlarm(15);
       
   865 
       
   866 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
   867 	TRAPD(err, BackupAlarmServerIniFileL());
       
   868 	TheAlarmTest(err==KErrNone, __LINE__);
       
   869 
       
   870 	// get alarm expiry time
       
   871 	TASShdAlarm alarm;
       
   872 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNone, __LINE__);
       
   873 	TTime time = alarm.NextDueTime();
       
   874 
       
   875 	TheAlarmTest.Test().Printf(_L("Waiting for Alarm to expire\r\n"));
       
   876 
       
   877 	// wait until after alarm due time
       
   878 	User::SetHomeTime(time - TTimeIntervalSeconds(3));
       
   879 	User::After(KTenSeconds);
       
   880 
       
   881 	// Internalise on startup has a 59s window for recent alarms
       
   882 	TheAlarmTest.Test().Next(_L("check that alarm is now set and notifying"));
       
   883 	//check status of alarm
       
   884 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNone, __LINE__);
       
   885 
       
   886 	TheAlarmTest(alarm.State() == EAlarmStateNotifying, __LINE__);
       
   887 
       
   888 
       
   889 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
   890 	TRAP(err, RestoreAlarmServerIniFileL());
       
   891 	TheAlarmTest(err==KErrNone, __LINE__);
       
   892 
       
   893 	// allow Alarm server to notice file lock has been released
       
   894 	User::After(1000000);
       
   895 
       
   896 	// Internalise after Restore has a 0s window for recent alarms
       
   897 	// check absence of alarm
       
   898 	TheAlarmTest.Test().Next(_L("check alarm has gone\r\n"));
       
   899 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNotFound, __LINE__);
       
   900 	}
       
   901 
       
   902 /*****************************************************************************
       
   903  */
       
   904 
       
   905 static void doTestSessionAlarmShutdownRestartL()
       
   906 	{
       
   907 	TheAlarmTest.Test().Next(_L("* Test that Session alarm is removed by shutdown"));
       
   908 
       
   909 #if !defined(_DEBUG) || !defined(__WINSCW__)
       
   910 	// Can't shutdown Alarm Server on UREL or target builds
       
   911 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A WINS - DEBUG TEST ONLY\r\n"));
       
   912 #else
       
   913 	TheAlarmTest.TestClearStoreL();
       
   914 
       
   915 	// Set the session alarm
       
   916 	TheAlarmTest.Test().Next(_L("set session alarm in 2 minutes"));
       
   917 	TASShdAlarm alarm;
       
   918 	alarm.Category() = KASCliCategoryClock;
       
   919 	alarm.RepeatDefinition() = EAlarmRepeatDefintionRepeatOnce;
       
   920 	alarm.NextDueTime().HomeTime();
       
   921 	alarm.NextDueTime() += TTimeIntervalMinutes(2);
       
   922 	// session specific alarm (has a TRequestStatus to complete)
       
   923 	alarm.Characteristics().Set(EAlarmCharacteristicsSessionSpecific);
       
   924 	TRequestStatus status;
       
   925 	TheAlarmTest.Session().AlarmAddWithNotification(status, alarm);
       
   926 	const TAlarmId id = alarm.Id();
       
   927 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
   928 
       
   929 	// status should be pending
       
   930 	TheAlarmTest.Test().Next(_L("checking request is pending"));
       
   931 	TheAlarmTest(status == KRequestPending, __LINE__);
       
   932 
       
   933 	TheAlarmTest.Test().Next(_L("Shutdown Alarm Server"));
       
   934 	closeServers();
       
   935 
       
   936 	User::After(10000000);
       
   937 
       
   938 	// check absence of alarm
       
   939 	TheAlarmTest.Test().Next(_L("check alarm notification has been aborted"));
       
   940 
       
   941 	TheAlarmTest(status != KRequestPending, __LINE__);
       
   942 
       
   943 	// Get the abort
       
   944 	// (Probably KErrDied though KErrServerTerminated should be okay too)
       
   945 	User::WaitForRequest(status);
       
   946 	TInt stat = status.Int();
       
   947 	TheAlarmTest((stat == KErrDied) || (stat == KErrServerTerminated), __LINE__);
       
   948 
       
   949 	// now restart Alarm Server
       
   950 	TheAlarmTest.Test().Next(_L("restart Alarm Server"));
       
   951 	TheAlarmTest.TestStartServers();
       
   952 
       
   953 	// check presence of alarm
       
   954 	TheAlarmTest.Test().Next(_L("check shutdown deleted alarm"));
       
   955 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNotFound, __LINE__);
       
   956 #endif
       
   957 	}
       
   958 
       
   959 /*****************************************************************************
       
   960  */
       
   961 
       
   962 static void doTestSessionAlarmBackupRestoreL()
       
   963 	{
       
   964 	TheAlarmTest.Test().Next(_L("* Test that Session alarm is orphaned by restore"));
       
   965 
       
   966 	TheAlarmTest.TestClearStoreL();
       
   967 
       
   968 	// Set the session alarm
       
   969 	TheAlarmTest.Test().Next(_L("set session alarm in 20 minutes"));
       
   970 	TASShdAlarm alarmWrite;
       
   971 	alarmWrite.Category() = KASCliCategoryClock;
       
   972 	alarmWrite.RepeatDefinition() = EAlarmRepeatDefintionRepeatOnce;
       
   973 	alarmWrite.NextDueTime().HomeTime();
       
   974 	alarmWrite.NextDueTime() += TTimeIntervalMinutes(20);
       
   975 	// session specific alarm (has a TRequestStatus to complete)
       
   976 	alarmWrite.Characteristics().Set(EAlarmCharacteristicsSessionSpecific);
       
   977 	TRequestStatus status;
       
   978 	TheAlarmTest.Session().AlarmAddWithNotification(status, alarmWrite);
       
   979 	// check that alarm was set okay
       
   980 	const TAlarmId id = alarmWrite.Id();
       
   981 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
   982 
       
   983 	// status should be pending
       
   984 	TheAlarmTest.Test().Next(_L("checking request is pending"));
       
   985 	TheAlarmTest(status == KRequestPending, __LINE__);
       
   986 
       
   987 	// remember the alarm time
       
   988 	TTime alarmTime = alarmWrite.NextDueTime();
       
   989 
       
   990 	// make the Alarm server re-internlize ....
       
   991 	TheAlarmTest.Test().Next(_L("now simulating restore..."));
       
   992 	TRAPD(err, SimulateBackupServerRestorationL());
       
   993 	TheAlarmTest(err==KErrNone, __LINE__);
       
   994 
       
   995 
       
   996 	// allow Alarm server to Internalize...
       
   997 	User::After(1000000);	// 1s
       
   998 
       
   999 
       
  1000 	// check old alarm queue has been cleared
       
  1001 	TheAlarmTest.Test().Next(_L("check alarm notification has been aborted"));
       
  1002 	TheAlarmTest(status != KRequestPending, __LINE__);
       
  1003 
       
  1004 	// Get the abort result
       
  1005 	User::WaitForRequest(status);
       
  1006 	TheAlarmTest(status.Int() == KErrCancel, __LINE__);
       
  1007 
       
  1008 	// check presence of alarm
       
  1009 	TASShdAlarm alarmRead;
       
  1010 	TheAlarmTest.Test().Next(_L("check alarm is no longer a session alarm"));
       
  1011 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarmRead) == KErrNone, __LINE__);
       
  1012 
       
  1013 	TheAlarmTest(!alarmRead.HasOwningSession(), __LINE__);
       
  1014 	TheAlarmTest(alarmRead.HasBecomeOrphaned(), __LINE__);
       
  1015 
       
  1016 	// advance clock, wait for alarm
       
  1017 	User::SetHomeTime(alarmWrite.NextDueTime() - TTimeIntervalSeconds(3));
       
  1018 	TheAlarmTest.Test().Next(_L("waiting for alarm to notify"));
       
  1019 	// wait until after alarm due time
       
  1020 	User::After(KTenSeconds);
       
  1021 
       
  1022 	// alarm should be notifying
       
  1023 	// if Alarm Server server tries to access the TRequestStatus
       
  1024 	// there will be an Access Violation
       
  1025 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarmRead) == KErrNone, __LINE__);
       
  1026 
       
  1027 	//TheAlarmTest(alarm.State() == EAlarmStateNotifying, __LINE__);
       
  1028 
       
  1029 	const TAlarmState alarmState = alarmRead.State();
       
  1030 	TheAlarmTest(alarmState == EAlarmStateNotifying, __LINE__);
       
  1031 	}
       
  1032 
       
  1033 
       
  1034 /*****************************************************************************
       
  1035  * Test adapted from TAlarmData, checks alarm data across Backup & Restore.
       
  1036  * (Original tested across Alarm Server shutdown & re-start.)
       
  1037  */
       
  1038 
       
  1039 static void doTestRestoreValidAlarmDataL()
       
  1040 	{
       
  1041 	TheAlarmTest.Test().Next(_L("Data API test"));
       
  1042 	TheAlarmTest.TestClearStoreL();
       
  1043 	TInt r;
       
  1044 	TAlarmId alarmId;
       
  1045 	TASShdAlarm alarm;
       
  1046 	TRequestStatus status;
       
  1047 	//
       
  1048 	alarm.Message() = _L("Alarm with data");
       
  1049 	alarm.NextDueTime().HomeTime();
       
  1050 	alarm.NextDueTime() += TTimeIntervalMinutes(2);
       
  1051 	//
       
  1052 	r = TheAlarmTest.Session().AlarmAdd(alarm);
       
  1053 	TheAlarmTest(r==KErrNone, __LINE__);
       
  1054 	//
       
  1055 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1056 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1057 	//
       
  1058 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  1059 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1060 	//
       
  1061 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  1062 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  1063 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1064 	TheAlarmTest(status == EAlarmChangeEventAlarmData, __LINE__);
       
  1065 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  1066 	User::WaitForRequest(status);
       
  1067 	//
       
  1068 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  1069 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  1070 	TheAlarmTest(r == KErrInUse, __LINE__);
       
  1071 	TheAlarmTest(status == KRequestPending, __LINE__);
       
  1072 	//
       
  1073 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1074 	TheAlarmTest(r == KSomeAlarmData().Size());
       
  1075 	//
       
  1076 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  1077 	r = TheAlarmTest.Session().AlarmDelete(alarm.Id());
       
  1078 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1079 	TheAlarmTest.TestEventBuffer(EAlarmChangeEventAlarmData, status, alarmId);
       
  1080 	//
       
  1081 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1082 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1083 	//
       
  1084 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  1085 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1086 	//
       
  1087 	alarm.NextDueTime().HomeTime();
       
  1088 	alarm.NextDueTime() += TTimeIntervalMinutes(2);
       
  1089 	r = TheAlarmTest.Session().AlarmAdd(alarm);
       
  1090 	TheAlarmTest(r==KErrNone, __LINE__);
       
  1091 	//
       
  1092 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  1093 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  1094 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1095 	TheAlarmTest(status == EAlarmChangeEventAlarmData, __LINE__);
       
  1096 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  1097 	User::WaitForRequest(status);
       
  1098 	//
       
  1099 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  1100 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  1101 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1102 	TheAlarmTest.TestEventBuffer(EAlarmChangeEventAlarmData, status, alarmId);
       
  1103 	//
       
  1104 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  1105 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1106 	//
       
  1107 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1108 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1109 	//
       
  1110 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  1111 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  1112 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1113 	TheAlarmTest(status == EAlarmChangeEventAlarmData, __LINE__);
       
  1114 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  1115 	User::WaitForRequest(status);
       
  1116 	//
       
  1117 	HBufC8* buf = HBufC8::NewLC(KSomeAlarmData().Size());
       
  1118 	TPtr8 pBuf(buf->Des());
       
  1119 	r = TheAlarmTest.Session().GetAlarmData(alarm.Id(), pBuf);
       
  1120 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1121 	TheAlarmTest(*buf == KSomeAlarmData, __LINE__);
       
  1122 	//
       
  1123 	TheAlarmTest.Test().Next(_L("Waiting for alarm to expire"));
       
  1124 	// advance clock to 3s before alarm due
       
  1125 	User::SetHomeTime(alarm.NextDueTime() - TTimeIntervalSeconds(3));
       
  1126 	TheAlarmTest.WaitForEvent(EAlarmChangeEventTimerExpired, status, alarmId);
       
  1127 	TheAlarmTest(alarmId == alarm.Id(), __LINE__);
       
  1128 	//
       
  1129 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  1130 	TheAlarmTest(r == KErrInUse, __LINE__);
       
  1131 	//
       
  1132 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1133 	TheAlarmTest(r == KSomeAlarmData().Size(), __LINE__);
       
  1134 	//
       
  1135 	r = TheAlarmTest.Session().AlarmDelete(alarm.Id());
       
  1136 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1137 	//
       
  1138 
       
  1139 	TheAlarmTest.Session().AlarmAddWithNotification(status, alarm, KSomeAlarmData);
       
  1140 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  1141 	HBufC8* alarmdata = HBufC8::NewLC(KSomeAlarmData().Size());
       
  1142 	TPtr8 pAlarmdata(alarmdata->Des());
       
  1143 	r = TheAlarmTest.Session().GetAlarmData(alarm.Id(), pAlarmdata);
       
  1144 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1145 	TheAlarmTest(*alarmdata == KSomeAlarmData, __LINE__);
       
  1146 	TheAlarmTest.Session().AlarmNotificationCancelAndDequeue(alarmId);
       
  1147 	CleanupStack::PopAndDestroy(alarmdata);
       
  1148 	//
       
  1149 	alarm.Reset();
       
  1150 	alarm.Message() = _L("Alarm with data");
       
  1151 	alarm.NextDueTime().HomeTime();
       
  1152 	alarm.NextDueTime() += TTimeIntervalMinutes(2);
       
  1153 	//
       
  1154 	r =	TheAlarmTest.Session().AlarmAdd(alarm, KSomeAlarmData);
       
  1155 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  1156 	HBufC8* alarmdata1 = HBufC8::NewLC(KSomeAlarmData().Size());
       
  1157 	TPtr8 pAlarmdata1(alarmdata1->Des());
       
  1158 	r = TheAlarmTest.Session().GetAlarmData(alarm.Id(), pAlarmdata1);
       
  1159 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1160 	TheAlarmTest(*alarmdata1 == KSomeAlarmData, __LINE__);
       
  1161 	CleanupStack::PopAndDestroy(alarmdata1);
       
  1162 
       
  1163 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
  1164 	User::After(1000000); // 1s
       
  1165 	BackupAlarmServerIniFileL();
       
  1166 	User::After(1000000); // 1s
       
  1167 
       
  1168 	TheAlarmTest.Test().Next(_L("Delete Alarm"));
       
  1169 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(alarmId) == KErrNone, __LINE__);
       
  1170 
       
  1171 	User::After(1000000); // 1s
       
  1172 
       
  1173 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
  1174 	RestoreAlarmServerIniFileL();
       
  1175 
       
  1176 	User::After(1000000); // 1s
       
  1177 
       
  1178 	//
       
  1179 	TheAlarmTest.Test().Next(_L("testing data restoration"));
       
  1180 	//
       
  1181 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  1182 	TheAlarmTest(r == KErrInUse, __LINE__);
       
  1183 	//
       
  1184 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1185 	TheAlarmTest(r == KSomeAlarmData().Size(), __LINE__);
       
  1186 	//
       
  1187 	r = TheAlarmTest.Session().AlarmDelete(alarm.Id());
       
  1188 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1189 	//
       
  1190 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1191 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1192 	//
       
  1193 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  1194 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1195 	//
       
  1196 	r = TheAlarmTest.Session().AlarmAdd(alarm);
       
  1197 	TheAlarmTest(r==KErrNone, __LINE__);
       
  1198 	//
       
  1199 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  1200 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1201 	//
       
  1202 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  1203 	TheAlarmTest(r == KErrNone, __LINE__);
       
  1204 	//
       
  1205 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1206 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1207 
       
  1208 	CleanupStack::PopAndDestroy(buf);
       
  1209 	}
       
  1210 
       
  1211 
       
  1212 //
       
  1213 //
       
  1214 //
       
  1215 
       
  1216 
       
  1217 static void doTestUtcAlarmQueueOrderShutdownStartupL()
       
  1218 	{
       
  1219 	TheAlarmTest.Test().Next(_L("* Test alarms set with UTC time order stable over server shutdown/startup"));
       
  1220 
       
  1221 #if !defined(_DEBUG) || !defined(__WINSCW__)
       
  1222 	// Can't shutdown Alarm Server on UREL or target builds
       
  1223 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A WINS - DEBUG TEST ONLY\r\n"));
       
  1224 #else
       
  1225 	TheAlarmTest.TestClearStoreL();
       
  1226 
       
  1227 	//add alarms
       
  1228 	TheAlarmTest.Test().Next(_L("add 5 alarms"));
       
  1229 	RArray<TAlarmId> alarmIds;
       
  1230 	for	(TInt ii=2; ii<=6; ii++)
       
  1231 		{
       
  1232 		TAlarmId id = addUtcAlarm(ii);
       
  1233 		alarmIds.Append(id);
       
  1234 		}
       
  1235 
       
  1236 	TheAlarmTest.Test().Next(_L("shutdown then restart Alarm Server"));
       
  1237 	shutDownAndStartUpServers();
       
  1238 
       
  1239 	//get server alarms
       
  1240 	RArray<TAlarmId> serverAlarmIds;
       
  1241 	TheAlarmTest.Session().GetAlarmIdListForCategoryL(KASCliCategoryClock, serverAlarmIds);
       
  1242 	alarmIds.Sort();
       
  1243 	serverAlarmIds.Sort();
       
  1244 	TInt count = alarmIds.Count();
       
  1245 	TheAlarmTest(count == serverAlarmIds.Count(), __LINE__);
       
  1246 	for	(TInt jj=0; jj<count; jj++)
       
  1247 		{
       
  1248 		TheAlarmTest(alarmIds[jj] == serverAlarmIds[jj], __LINE__);
       
  1249 		}
       
  1250 	serverAlarmIds.Close();
       
  1251 	alarmIds.Close();
       
  1252 #endif
       
  1253 	}
       
  1254 
       
  1255 static void doTestUtcAlarmDSTShutdownStartupL()
       
  1256 	{
       
  1257 	TheAlarmTest.Test().Next(_L("* Test alarms set stable over server shutdown in DST off/startup in DST on"));
       
  1258 #if !defined(_DEBUG) || !defined(__WINSCW__)
       
  1259 	// Can't shutdown Alarm Server on UREL or target builds
       
  1260 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A WINS - DEBUG TEST ONLY\r\n"));
       
  1261 #else
       
  1262 	_LIT(KDSTON,"DST is on\r\n");
       
  1263 	_LIT(KDSTOFF,"DST is off\r\n");
       
  1264 	_LIT8(KTzEuropeLondon, "Europe/London");
       
  1265 
       
  1266 	TheAlarmTest.TestClearStoreL();
       
  1267 
       
  1268 	//Set time zone and system time at first
       
  1269 	const TInt currYear = 2007;
       
  1270 
       
  1271  	TTime oldTime;
       
  1272  	oldTime.HomeTime();
       
  1273 
       
  1274  	RTz tz;
       
  1275  	User::LeaveIfError(tz.Connect());
       
  1276 	CleanupClosePushL(tz);
       
  1277 	CTzId* oldTz=tz.GetTimeZoneIdL();
       
  1278 	CleanupStack::PushL(oldTz);
       
  1279     CTzId* zoneId = CTzId::NewL(KTzEuropeLondon);
       
  1280 	CleanupStack::PushL(zoneId);
       
  1281 	tz.SetTimeZoneL(*zoneId);
       
  1282 	tz.SetHomeTime(TDateTime(currYear, EMarch, 24, 00, 58, 0, 0));
       
  1283 	tz.SetAutoUpdateBehaviorL(RTz::ETZAutoDSTUpdateOn);
       
  1284 
       
  1285     //add alarms
       
  1286 	TheAlarmTest.Test().Next(_L("add 2 alarms"));
       
  1287 	RArray<TAlarmId> alarmIds;
       
  1288 	//Create an alarm at 5.00 am
       
  1289 	TAlarmId id = addAlarm(242);
       
  1290 	alarmIds.Append(id);
       
  1291 	//Create an alarm at 6.00 am
       
  1292 	id = addAlarm(302);
       
  1293 	alarmIds.Append(id);
       
  1294 
       
  1295 	//Get the due time of the returned back alarms
       
  1296 	RArray<TTime> dueTimes;
       
  1297 	TASShdAlarm orginalAlarm;
       
  1298 	TheAlarmTest.Session().GetAlarmDetails(alarmIds[0], orginalAlarm);
       
  1299 	dueTimes.Append(orginalAlarm.NextDueTime());
       
  1300 	TheAlarmTest.Session().GetAlarmDetails(alarmIds[1], orginalAlarm);
       
  1301 	dueTimes.Append(orginalAlarm.NextDueTime());
       
  1302 
       
  1303 	TTime curTime;
       
  1304 	curTime.HomeTime();
       
  1305 	TDateTime date=curTime.DateTime();
       
  1306 	TBool onDST=tz.IsDaylightSavingOnL(*zoneId);
       
  1307 
       
  1308 	TheAlarmTest.Test().Printf(_L("system time is now: hours: %d, minutes: %d, seconds: %d.\r\n"), date.Hour(), date.Minute(), date.Second());
       
  1309 	if (onDST)
       
  1310 		{
       
  1311 		TheAlarmTest.Test().Printf(KDSTON);
       
  1312 		}
       
  1313 	else
       
  1314 		{
       
  1315 		TheAlarmTest.Test().Printf(KDSTOFF);
       
  1316 		}
       
  1317 
       
  1318 	TheAlarmTest.Test().Next(_L("shutdown then restart Alarm Server"));
       
  1319 	closeServers();
       
  1320 	User::After(1000000*60*3);  //wait three minutes
       
  1321 	TheAlarmTest.Test().Printf(_L("now restarting server...\r\n"));
       
  1322 	TheAlarmTest.TestStartServers();
       
  1323 
       
  1324 	curTime.HomeTime();
       
  1325 	date=curTime.DateTime();
       
  1326 	onDST=tz.IsDaylightSavingOnL(*zoneId);
       
  1327 	CleanupStack::PopAndDestroy(zoneId);
       
  1328 
       
  1329 	TheAlarmTest.Test().Printf(_L("system time is now: hours: %d, minutes: %d, seconds: %d.\r\n"), date.Hour(), date.Minute(), date.Second());
       
  1330 	if (onDST)
       
  1331 		{
       
  1332 		TheAlarmTest.Test().Printf(KDSTON);
       
  1333 		}
       
  1334 	else
       
  1335 		{
       
  1336 		TheAlarmTest.Test().Printf(KDSTOFF);
       
  1337 		}
       
  1338 
       
  1339 	//get server alarms
       
  1340 	RArray<TAlarmId> serverAlarmIds;
       
  1341 	TheAlarmTest.Session().GetAlarmIdListForCategoryL(KASCliCategoryClock, serverAlarmIds);
       
  1342 	alarmIds.Sort();
       
  1343 	serverAlarmIds.Sort();
       
  1344 	TInt count = alarmIds.Count();
       
  1345 	TheAlarmTest(count == serverAlarmIds.Count(), __LINE__);
       
  1346 	TASShdAlarm alarm;
       
  1347 	for	(TInt jj=0; jj<count; jj++)
       
  1348 		{
       
  1349 		TheAlarmTest(alarmIds[jj] == serverAlarmIds[jj], __LINE__);
       
  1350 		TheAlarmTest.Session().GetAlarmDetails(alarmIds[jj], alarm);
       
  1351 		TheAlarmTest(alarm.NextDueTime() == dueTimes[jj], __LINE__);
       
  1352 		date = alarm.NextDueTime().DateTime();
       
  1353 		TheAlarmTest.Test().Printf(_L("alarm %d due time is : hours: %d, minutes: %d, seconds: %d.\r\n"),
       
  1354 		     alarmIds[jj],date.Hour(), date.Minute(), date.Second());
       
  1355 		}
       
  1356 
       
  1357 
       
  1358 	serverAlarmIds.Close();
       
  1359 	alarmIds.Close();
       
  1360 	dueTimes.Close();
       
  1361 
       
  1362 	tz.SetTimeZoneL(*oldTz);
       
  1363 	CleanupStack::PopAndDestroy(oldTz);
       
  1364   	tz.SetHomeTime(oldTime);
       
  1365  	CleanupStack::PopAndDestroy(&tz);
       
  1366 #endif
       
  1367 	}
       
  1368 
       
  1369 static void doTestFloatingAlarmDSTShutdownStartupHelsinkiL()
       
  1370 	{
       
  1371 	TheAlarmTest.Test().Next(_L("* Test alarms set stable over server shutdown in DST on/startup in DST off"));
       
  1372 	
       
  1373 #if !defined(_DEBUG) || !defined(__WINSCW__)
       
  1374 	// Can't shutdown Alarm Server on UREL or target builds
       
  1375 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A WINS - DEBUG TEST ONLY\r\n"));
       
  1376 #else
       
  1377 	_LIT8(KTzEuropeHelsinki, "Europe/Helsinki");
       
  1378 	_LIT(KDSTON,"DST is on\r\n");
       
  1379 	_LIT(KDSTOFF,"DST is off\r\n");
       
  1380 
       
  1381 	TheAlarmTest.TestClearStoreL();
       
  1382 	
       
  1383 	//Set time zone and system time at first
       
  1384 	const TInt currYear = 2007;
       
  1385  
       
  1386  	TTime oldTime; 
       
  1387  	oldTime.HomeTime();
       
  1388  
       
  1389  	RTz tz;
       
  1390  	User::LeaveIfError(tz.Connect());
       
  1391 	CleanupClosePushL(tz);
       
  1392 	CTzId* oldTz=tz.GetTimeZoneIdL();
       
  1393 	CleanupStack::PushL(oldTz);
       
  1394     CTzId* zoneId = CTzId::NewL(KTzEuropeHelsinki);
       
  1395 	CleanupStack::PushL(zoneId);
       
  1396 	tz.SetTimeZoneL(*zoneId);
       
  1397 	tz.SetHomeTime(TDateTime(currYear, EOctober, 27, 03, 58, 0, 0));
       
  1398 	tz.SetAutoUpdateBehaviorL(RTz::ETZAutoDSTUpdateOn);	
       
  1399 
       
  1400     //add alarms
       
  1401 	TheAlarmTest.Test().Next(_L("add alarms"));
       
  1402 	RArray<TAlarmId> alarmIds;
       
  1403 	//Create an alarm at 4.02 am
       
  1404 	TAlarmId id = addAlarm(4);
       
  1405 	alarmIds.Append(id);
       
  1406 		
       
  1407 	id = addAlarm(7);
       
  1408 	alarmIds.Append(id);
       
  1409 	
       
  1410 	id = addAlarm(10);
       
  1411 	alarmIds.Append(id);
       
  1412 	
       
  1413 	//Get the due time of the returned back alarms
       
  1414 	RArray<TTime> dueTimes;
       
  1415 	TASShdAlarm floatingAlarm;
       
  1416 	TheAlarmTest.Session().GetAlarmDetails(alarmIds[0], floatingAlarm);
       
  1417 	dueTimes.Append(floatingAlarm.NextDueTime());
       
  1418 	
       
  1419 	TheAlarmTest.Session().GetAlarmDetails(alarmIds[1], floatingAlarm);
       
  1420 	dueTimes.Append(floatingAlarm.NextDueTime());
       
  1421 	
       
  1422 	TheAlarmTest.Session().GetAlarmDetails(alarmIds[2], floatingAlarm);
       
  1423 	dueTimes.Append(floatingAlarm.NextDueTime());
       
  1424 	
       
  1425 	TTime curTime;
       
  1426 	curTime.HomeTime();
       
  1427 	TDateTime date=curTime.DateTime();
       
  1428 	TBool onDST=tz.IsDaylightSavingOnL(*zoneId);
       
  1429 	
       
  1430 	TheAlarmTest.Test().Printf(_L("system time is now: hours: %d, minutes: %d, seconds: %d.\r\n"), date.Hour(), date.Minute(), date.Second());
       
  1431 	if (onDST)
       
  1432 		{
       
  1433 		TheAlarmTest.Test().Printf(KDSTON);
       
  1434 		}
       
  1435 	else
       
  1436 		{
       
  1437 		TheAlarmTest.Test().Printf(KDSTOFF);
       
  1438 		}
       
  1439 		
       
  1440 	TheAlarmTest.Test().Next(_L("shutdown then restart Alarm Server"));
       
  1441 	closeServers();
       
  1442 	User::After(1000000*60*4);  //wait for 4 mins
       
  1443 	
       
  1444 	tz.SetHomeTime(TDateTime(currYear, EOctober, 27, 04, 02, 0, 0));
       
  1445 	TheAlarmTest.Test().Printf(_L("now restarting server...\r\n"));
       
  1446 	TheAlarmTest.TestStartServers();
       
  1447 
       
  1448 	curTime.HomeTime();
       
  1449 	date=curTime.DateTime();
       
  1450 	onDST=tz.IsDaylightSavingOnL(*zoneId);
       
  1451 	CleanupStack::PopAndDestroy(zoneId);
       
  1452 
       
  1453 	TheAlarmTest.Test().Printf(_L("system time is now: hours: %d, minutes: %d, seconds: %d.\r\n"), date.Hour(), date.Minute(), date.Second());
       
  1454 	if (onDST)
       
  1455 		{
       
  1456 		TheAlarmTest.Test().Printf(KDSTON);
       
  1457 		}
       
  1458 	else
       
  1459 		{
       
  1460 		TheAlarmTest.Test().Printf(KDSTOFF);
       
  1461 		}
       
  1462 	
       
  1463 	//get server alarms
       
  1464 	RArray<TAlarmId> serverAlarmIds;
       
  1465 	TheAlarmTest.Session().GetAlarmIdListForCategoryL(KASCliCategoryClock, serverAlarmIds);
       
  1466 	alarmIds.Sort();
       
  1467 	serverAlarmIds.Sort();
       
  1468 	TInt count = alarmIds.Count();
       
  1469 	if(count != serverAlarmIds.Count())
       
  1470 		{
       
  1471 		TheAlarmTest.Test().Printf(_L("No alarms found after DST change \r\n"));
       
  1472 		}
       
  1473 	else
       
  1474 		{
       
  1475 		TASShdAlarm alarm;
       
  1476 		for	(TInt jj=0; jj<count; jj++)
       
  1477 			{
       
  1478 			TheAlarmTest(alarmIds[jj] == serverAlarmIds[jj], __LINE__);
       
  1479 			TheAlarmTest.Session().GetAlarmDetails(alarmIds[jj], alarm);
       
  1480 			TheAlarmTest(alarm.NextDueTime() == dueTimes[jj], __LINE__);
       
  1481 			date = alarm.NextDueTime().DateTime();
       
  1482 			TheAlarmTest.Test().Printf(_L("alarm %d due time is : hours: %d, minutes: %d, seconds: %d.\r\n"), 
       
  1483 			     alarmIds[jj],date.Hour(), date.Minute(), date.Second());
       
  1484 			}
       
  1485 			
       
  1486 		}
       
  1487 	serverAlarmIds.Close();
       
  1488 	alarmIds.Close();
       
  1489 	dueTimes.Close();
       
  1490 	
       
  1491 	tz.SetTimeZoneL(*oldTz);
       
  1492 	CleanupStack::PopAndDestroy(oldTz);
       
  1493   	tz.SetHomeTime(oldTime);
       
  1494  	CleanupStack::PopAndDestroy(&tz);
       
  1495 #endif
       
  1496 	}
       
  1497 
       
  1498 
       
  1499 static void doTestUtcAlarmQueueOrderBackupRestoreL()
       
  1500 	{
       
  1501 	TheAlarmTest.Test().Next(_L("* Test alarm set with UTC time order across Backup and Restore"));
       
  1502 
       
  1503 	TheAlarmTest.TestClearStoreL();
       
  1504 
       
  1505 	//add alarms
       
  1506 	TheAlarmTest.Test().Next(_L("add 5 alarms"));
       
  1507 	RArray<TAlarmId> alarmIds;
       
  1508 	for	(TInt ii=2; ii<=6; ii++)
       
  1509 		{
       
  1510 		TAlarmId id = addUtcAlarm(ii);
       
  1511 		alarmIds.AppendL(id);
       
  1512 		}
       
  1513 
       
  1514 	// brief pause before backup (1s)
       
  1515 	User::After(1000000);
       
  1516 
       
  1517 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
  1518 	TRAPD(err, BackupAlarmServerIniFileL());
       
  1519 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1520 
       
  1521 	// delete alarms
       
  1522 	TheAlarmTest.Test().Next(_L("delete alarms"));
       
  1523 	TInt jj;
       
  1524 	for (jj=0; jj<5; jj++)
       
  1525 		{
       
  1526 		TheAlarmTest(TheAlarmTest.Session().AlarmDelete(alarmIds[jj]) == KErrNone, __LINE__);
       
  1527 		}
       
  1528 
       
  1529 	// pause before restore (10s)
       
  1530 	User::After(10000000);
       
  1531 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
  1532 	TRAP(err, RestoreAlarmServerIniFileL());
       
  1533 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1534 
       
  1535 	TheAlarmTest.Test().Next(_L("check restored alarms"));
       
  1536 	//get server alarms
       
  1537 	RArray<TAlarmId> serverAlarmIds;
       
  1538 	TheAlarmTest.Session().GetAlarmIdListForCategoryL(KASCliCategoryClock, serverAlarmIds);
       
  1539 	alarmIds.Sort();
       
  1540 	serverAlarmIds.Sort();
       
  1541 	TInt count = alarmIds.Count();
       
  1542 	TInt serverCount = serverAlarmIds.Count();
       
  1543 	TheAlarmTest.Test().Printf(_L("[Added Alarms count = %i, server Count = %i]\r\n"), count, serverCount);
       
  1544 	TheAlarmTest(count == serverCount, __LINE__);
       
  1545 	for	(jj=0; jj<count; jj++)
       
  1546 		{
       
  1547 		TheAlarmTest(alarmIds[jj] == serverAlarmIds[jj], __LINE__);
       
  1548 		}
       
  1549 	serverAlarmIds.Close();
       
  1550 	alarmIds.Close();
       
  1551 	}
       
  1552 
       
  1553 
       
  1554 static void doTestUtcAlarmQueueBackupRestoreL()
       
  1555 	{
       
  1556 	TheAlarmTest.Test().Next(_L("* Test correct alarms set with UTC time in queue after backup and restore"));
       
  1557 
       
  1558 	TheAlarmTest.TestClearStoreL();
       
  1559 
       
  1560 	// add an alarm to expire in 2 minutes
       
  1561 	TAlarmId id2 = addUtcAlarm(2);
       
  1562 	// add an alarm to expire in 3 minutes
       
  1563 	TAlarmId id3 = addUtcAlarm(3);
       
  1564 
       
  1565 	// brief pause before backup (.5s)
       
  1566 	User::After(500000);
       
  1567 
       
  1568 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
  1569 	TRAPD(err, BackupAlarmServerIniFileL());
       
  1570 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1571 
       
  1572 	// delete alarms
       
  1573 	TheAlarmTest.Test().Next(_L("delete alarms"));
       
  1574 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(id2) == KErrNone, __LINE__);
       
  1575 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(id3) == KErrNone, __LINE__);
       
  1576 
       
  1577 	// add alarms
       
  1578 	TheAlarmTest.Test().Next(_L("add alarms"));
       
  1579 	// add alarms to expire now
       
  1580 	TAlarmId id0 = addUtcAlarm(0);
       
  1581 	TAlarmId id1 = addUtcAlarm(1);
       
  1582 
       
  1583 	// first alarm (id0) should be notifying
       
  1584 	TASShdAlarm alarm;
       
  1585 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id0, alarm) == KErrNone, __LINE__);
       
  1586 	TheAlarmTest(alarm.State() == EAlarmStateNotifying, __LINE__);
       
  1587 
       
  1588 
       
  1589 	// pause briefly before restore (2s)
       
  1590 	User::After(2000000);
       
  1591 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
  1592 	TRAP(err, RestoreAlarmServerIniFileL());
       
  1593 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1594 
       
  1595 	TheAlarmTest.Test().Next(_L("check correct alarms after restore"));
       
  1596 
       
  1597 	// alarms should now be gone
       
  1598 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id0, alarm) == KErrNotFound, __LINE__);
       
  1599 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id1, alarm) == KErrNotFound, __LINE__);
       
  1600 
       
  1601 	// alarms should be restored
       
  1602 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id2, alarm) == KErrNone, __LINE__);
       
  1603 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id3, alarm) == KErrNone, __LINE__);
       
  1604 	}
       
  1605 
       
  1606 
       
  1607 static void doTestUtcAlarmAddDuringBackupL()
       
  1608 	{
       
  1609 	TheAlarmTest.Test().Next(_L("* Test alarm changes during backup"));
       
  1610 
       
  1611 	TheAlarmTest.TestClearStoreL();
       
  1612 
       
  1613 	// Attempt to add an alarm while the backup server is backing up the alarms
       
  1614 	CStreamStore* store = NULL;
       
  1615 	TRAPD(err, store = SimulateBackupServerBackup_StartL());
       
  1616 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1617 
       
  1618 	CleanupStack::PushL(store);
       
  1619 
       
  1620 	TheAlarmTest.Test().Next(_L("adding alarm should work"));
       
  1621 
       
  1622 	TASShdAlarm alm;
       
  1623 	TTime time;
       
  1624 	time.UniversalTime();
       
  1625 	alm.SetUtcNextDueTime(time);
       
  1626 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alm) == KErrNone, __LINE__);
       
  1627 
       
  1628 	TheAlarmTest.Test().Next(_L("changing time forces Alarm Store class to queue Externalize"));
       
  1629 	// Locale change (time) x2 (INC038240 regression)
       
  1630 	// Exercise CASSrvAlarmStore queue an Externalize
       
  1631 	time.UniversalTime();
       
  1632 	time += TTimeIntervalSeconds(10);
       
  1633 	User::SetUTCTime(time);
       
  1634 	User::After(1000000);
       
  1635 	time -= TTimeIntervalSeconds(10);
       
  1636 	User::SetUTCTime(time);
       
  1637 	User::After(1000000);
       
  1638 
       
  1639 	// release file lock -
       
  1640 	CleanupStack::PopAndDestroy(store); // store
       
  1641 	// backup complete
       
  1642 	TRAP(err, SimulateBackupServerBackup_CompleteL());
       
  1643 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1644 
       
  1645 	// wait for AlarmServer to catch-up
       
  1646 	User::After(1000000);
       
  1647 
       
  1648 
       
  1649 	TheAlarmTest.Test().Next(_L("check we can now add alarm"));
       
  1650 
       
  1651 	// re-try alarm add after backup complete - should be successful
       
  1652 	TAlarmId id = addUtcAlarm(0);
       
  1653 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
  1654 
       
  1655 	// make Alarm Server re-Internalize
       
  1656 	User::After(1000000);
       
  1657 	TheAlarmTest.Test().Next(_L("now simulating restore..."));
       
  1658 	TRAP(err, SimulateBackupServerRestorationL());
       
  1659 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1660 	TASShdAlarm alarm;
       
  1661 
       
  1662 
       
  1663 	// check absence of alarm
       
  1664 	TheAlarmTest.Test().Next(_L("check alarm has gone"));
       
  1665 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNotFound, __LINE__);
       
  1666 
       
  1667 	}
       
  1668 
       
  1669 
       
  1670 static void doTestUtcAlarmAddDuringRestoreL()
       
  1671 	{
       
  1672 	// variables used to check Alarm Server notifications of Restore
       
  1673 	TAlarmId alarmId;
       
  1674 	TRequestStatus status;
       
  1675 
       
  1676 	TheAlarmTest.Test().Next(_L("* Test alarm changes during restore"));
       
  1677 
       
  1678 	TheAlarmTest.TestClearStoreL();
       
  1679 
       
  1680 	// Attempt to add an alarm while the backup server is restoring up the alarms
       
  1681 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  1682 	CStreamStore* store = NULL;
       
  1683 	TRAPD(err, store = SimulateBackupServerRestore_StartL());
       
  1684 	CleanupStack::PushL(store);
       
  1685 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1686 
       
  1687 	// check Restore Start notification received
       
  1688 	TheAlarmTest.Test().Next(_L("check for Alarm Restore Start notification"));
       
  1689 	User::After(1000000);
       
  1690 	TheAlarmTest(status == EAlarmChangeEventRestoreStarted, __LINE__);
       
  1691 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  1692 
       
  1693 	TheAlarmTest(alarmId==KNullAlarmId, __LINE__);
       
  1694 
       
  1695 	TheAlarmTest.Test().Next(_L("adding alarm should fail"));
       
  1696 
       
  1697 	TASShdAlarm alm;
       
  1698 	TTime time;
       
  1699 	time.UniversalTime();
       
  1700 	alm.SetUtcNextDueTime(time);
       
  1701 	TheAlarmTest(TheAlarmTest.Session().AlarmAdd(alm) == KErrLocked, __LINE__);
       
  1702 
       
  1703 	TheAlarmTest.Test().Next(_L("changing time forces Alarm Store class to queue Externalize"));
       
  1704 	// Locale change (time) x2 (INC038240 regression)
       
  1705 	// Exercise CASSrvAlarmStore
       
  1706 
       
  1707 	time.UniversalTime();
       
  1708 	time += TTimeIntervalSeconds(10);
       
  1709 	User::SetUTCTime(time);
       
  1710 	User::After(1000000);
       
  1711 	time -= TTimeIntervalSeconds(10);
       
  1712 	User::SetUTCTime(time);
       
  1713 	User::After(1000000);
       
  1714 
       
  1715 	CleanupStack::PopAndDestroy(store);
       
  1716 	// restore complete
       
  1717 	TRAP(err, SimulateBackupServerRestore_CompleteL());
       
  1718 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1719 
       
  1720 	// check Restore Complete notification received
       
  1721 	TheAlarmTest.Test().Next(_L("check for Alarm Restore Complete notification"));
       
  1722 	TheAlarmTest.TestEventBuffer(EAlarmChangeEventRestoreCompleted, status, alarmId);
       
  1723 	TheAlarmTest(alarmId==KNullAlarmId, __LINE__);
       
  1724 
       
  1725 
       
  1726 	// wait for AlarmServer to catch-up
       
  1727 	User::After(1000000);
       
  1728 
       
  1729 	TheAlarmTest.Test().Next(_L("check we can now add alarm"));
       
  1730 
       
  1731 	// re-try alarm add after restore complete - should be successful
       
  1732 	TAlarmId id = addUtcAlarm(0);
       
  1733 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
  1734 
       
  1735 	// wait for Alarm Server to externalize
       
  1736 	User::After(3000000);
       
  1737 	}
       
  1738 
       
  1739 
       
  1740 
       
  1741 
       
  1742 
       
  1743 static void doTestNotifyingUtcAlarmShutdownStartupL()
       
  1744 	{
       
  1745 	TheAlarmTest.Test().Next(_L("* Test Notifying alarm across shutdown / startup"));
       
  1746 
       
  1747 	TheAlarmTest.TestClearStoreL();
       
  1748 
       
  1749 	TheAlarmTest.Test().Next(_L("add an alarm"));
       
  1750 	// add an alarm to expire in 15 minutes
       
  1751 	TAlarmId id = addUtcAlarm(15);
       
  1752 
       
  1753 	// get alarm expiry time
       
  1754 	TASShdAlarm alarm;
       
  1755 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNone, __LINE__);
       
  1756 	TTime time = alarm.NextDueTime();
       
  1757 
       
  1758 #if !defined(_DEBUG) || !defined(__WINSCW__)
       
  1759 	// Can't shutdown Alarm Server on UREL or target builds
       
  1760 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A WINS - DEBUG TEST ONLY\r\n"));
       
  1761 
       
  1762 	TheAlarmTest.Test().Printf(_L("Waiting for Alarm to expire\r\n"));
       
  1763 #if defined(_DEBUG)
       
  1764 	// UREL Alternative: wait until after alarm due time
       
  1765 	User::SetUTCTime(time - TTimeIntervalSeconds(3));
       
  1766 	User::After(KTenSeconds);
       
  1767 #endif
       
  1768 #else
       
  1769 	// UDEB:
       
  1770 	// shut down the server
       
  1771 	closeServers();
       
  1772 
       
  1773 	// re-start server after alarm expires
       
  1774 	User::SetUTCTime(time - TTimeIntervalSeconds(3));
       
  1775 	User::After(KTenSeconds);
       
  1776 	TheAlarmTest.Test().Next(_L("now restarting server...\r\n"));
       
  1777 	TheAlarmTest.TestStartServers();
       
  1778 	User::After(1000000);
       
  1779 #endif
       
  1780 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNone, __LINE__);
       
  1781 
       
  1782 #if	defined(_DEBUG)
       
  1783 	//check status of alarm
       
  1784 	// Internalise on startup has a 59s window for recent alarms
       
  1785 	TheAlarmTest.Test().Next(_L("check that alarm is now set and notifying"));
       
  1786 	TheAlarmTest(alarm.State() == EAlarmStateNotifying, __LINE__);
       
  1787 #else
       
  1788 	// Test only possible if in _DEBUG build.
       
  1789 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A DEBUG TEST ONLY\r\n"));
       
  1790 #endif
       
  1791 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(id) == KErrNone, __LINE__);
       
  1792 	}
       
  1793 
       
  1794 
       
  1795 static void doTestNotifyingUtcAlarmBackupRestoreL()
       
  1796 	{
       
  1797 	TheAlarmTest.Test().Next(_L("* Test Notifying alarm removed by backup / restore"));
       
  1798 
       
  1799 	TheAlarmTest.TestClearStoreL();
       
  1800 
       
  1801 	TheAlarmTest.Test().Next(_L("add an alarm"));
       
  1802 	// add an alarm to expire in 15 minutes
       
  1803 	TAlarmId id = addUtcAlarm(15);
       
  1804 
       
  1805 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
  1806 	TRAPD(err, BackupAlarmServerIniFileL());
       
  1807 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1808 
       
  1809 	// get alarm expiry time
       
  1810 	TASShdAlarm alarm;
       
  1811 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNone, __LINE__);
       
  1812 	TTime time = alarm.NextDueTime();
       
  1813 	TheAlarmTest.Test().Printf(_L("Waiting for Alarm to expire\r\n"));
       
  1814 	TheAlarmTest.Test().Printf(_L("Alarm set to expire at: hours: %d, minutes: %d, seconds: %d.\r\n"), time.DateTime().Hour(), time.DateTime().Minute(), time.DateTime().Second());
       
  1815 
       
  1816 
       
  1817 	// wait until after alarm due time
       
  1818 	User::SetUTCTime(time - TTimeIntervalSeconds(3));
       
  1819 	TTime now;
       
  1820 	now.UniversalTime();
       
  1821 	TheAlarmTest.Test().Printf(_L("System UTC time is now: hours: %d, minutes: %d, seconds: %d.\r\n"), now.DateTime().Hour(), now.DateTime().Minute(), now.DateTime().Second());
       
  1822 	TheAlarmTest.Test().Printf(_L("Wait for approximately 10 seconds for alarm to expire...\r\n"));
       
  1823 	User::After(KTenSeconds);
       
  1824 	now.UniversalTime();
       
  1825 	TheAlarmTest.Test().Printf(_L("System UTC time is now: hours: %d, minutes: %d, seconds: %d.\r\n"), now.DateTime().Hour(), now.DateTime().Minute(), now.DateTime().Second());
       
  1826 
       
  1827 	// Internalise on startup has a 59s window for recent alarms
       
  1828 	TheAlarmTest.Test().Next(_L("check that alarm is now set and notifying"));
       
  1829 
       
  1830 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNone, __LINE__);
       
  1831 	TheAlarmTest.Test().Printf(_L("Alarm state: %d.\r\n"), alarm.State());
       
  1832 	TheAlarmTest(alarm.State() == EAlarmStateNotifying, __LINE__);
       
  1833 
       
  1834 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
  1835 	TRAP(err, RestoreAlarmServerIniFileL());
       
  1836 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1837 
       
  1838 	// allow Alarm server to notice file lock has been released
       
  1839 	User::After(1000000);
       
  1840 
       
  1841 	// Internalise after Restore has a 0s window for recent alarms
       
  1842 	// check absence of alarm
       
  1843 	TheAlarmTest.Test().Next(_L("check alarm has gone\r\n"));
       
  1844 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNotFound, __LINE__);
       
  1845 	}
       
  1846 
       
  1847 
       
  1848 static void doTestSessionUtcAlarmShutdownRestartL()
       
  1849 	{
       
  1850 	TheAlarmTest.Test().Next(_L("* Test that Session alarm is removed by shutdown"));
       
  1851 
       
  1852 #if !defined(_DEBUG) || !defined(__WINSCW__)
       
  1853 	// Can't shutdown Alarm Server on UREL or target builds
       
  1854 	TheAlarmTest.Test().Printf(_L("THIS TEST IS A WINS - DEBUG TEST ONLY\r\n"));
       
  1855 #else
       
  1856 	TheAlarmTest.TestClearStoreL();
       
  1857 
       
  1858 	// Set the session alarm
       
  1859 	TheAlarmTest.Test().Next(_L("set session alarm in 2 minutes"));
       
  1860 	TASShdAlarm alarm;
       
  1861 	alarm.Category()=(KASCliCategoryClock);
       
  1862 	alarm.RepeatDefinition()=(EAlarmRepeatDefintionRepeatOnce);
       
  1863 	TTime time;
       
  1864 	time.UniversalTime();
       
  1865 	time += TTimeIntervalMinutes(2);
       
  1866 	alarm.SetUtcNextDueTime(time);
       
  1867 	// session specific alarm (has a TRequestStatus to complete)
       
  1868 	alarm.Characteristics().Set(EAlarmCharacteristicsSessionSpecific);
       
  1869 	TRequestStatus status;
       
  1870 	TheAlarmTest.Session().AlarmAddWithNotification(status, alarm);
       
  1871 	const TAlarmId id = alarm.Id();
       
  1872 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
  1873 
       
  1874 	// status should be pending
       
  1875 	TheAlarmTest.Test().Next(_L("checking request is pending"));
       
  1876 	TheAlarmTest(status == KRequestPending, __LINE__);
       
  1877 
       
  1878 	TheAlarmTest.Test().Next(_L("Shutdown Alarm Server"));
       
  1879 	closeServers();
       
  1880 
       
  1881 	User::After(10000000);
       
  1882 
       
  1883 	// check absence of alarm
       
  1884 	TheAlarmTest.Test().Next(_L("check alarm notification has been aborted"));
       
  1885 
       
  1886 	TheAlarmTest(status != KRequestPending, __LINE__);
       
  1887 
       
  1888 	// Get the abort
       
  1889 	// (Probably KErrDied though KErrServerTerminated should be okay too)
       
  1890 	User::WaitForRequest(status);
       
  1891 	TInt stat = status.Int();
       
  1892 	TheAlarmTest((stat == KErrDied) || (stat == KErrServerTerminated), __LINE__);
       
  1893 
       
  1894 	// now restart Alarm Server
       
  1895 	TheAlarmTest.Test().Next(_L("restart Alarm Server"));
       
  1896 	TheAlarmTest.TestStartServers();
       
  1897 
       
  1898 	// check presence of alarm
       
  1899 	TheAlarmTest.Test().Next(_L("check shutdown deleted alarm"));
       
  1900 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarm) == KErrNotFound, __LINE__);
       
  1901 #endif
       
  1902 	}
       
  1903 
       
  1904 
       
  1905 static void doTestSessionUtcAlarmBackupRestoreL()
       
  1906 	{
       
  1907 	TheAlarmTest.Test().Next(_L("* Test that Session alarm is orphaned by restore"));
       
  1908 
       
  1909 	TheAlarmTest.TestClearStoreL();
       
  1910 
       
  1911 	// Set the session alarm
       
  1912 	TheAlarmTest.Test().Next(_L("set session alarm in 20 minutes"));
       
  1913 	TASShdAlarm alarmWrite;
       
  1914 	alarmWrite.Category()=(KASCliCategoryClock);
       
  1915 	alarmWrite.RepeatDefinition()=EAlarmRepeatDefintionRepeatOnce;
       
  1916 	TTime time;
       
  1917 	time.UniversalTime();
       
  1918 	time += TTimeIntervalMinutes(20);
       
  1919 	alarmWrite.SetUtcNextDueTime(time);
       
  1920 	// session specific alarm (has a TRequestStatus to complete)
       
  1921 	alarmWrite.Characteristics().Set(EAlarmCharacteristicsSessionSpecific);
       
  1922 	TRequestStatus status;
       
  1923 	TheAlarmTest.Session().AlarmAddWithNotification(status, alarmWrite);
       
  1924 	// check that alarm was set okay
       
  1925 	const TAlarmId id = alarmWrite.Id();
       
  1926 	TheAlarmTest(id != KNullAlarmId, __LINE__);
       
  1927 
       
  1928 	// status should be pending
       
  1929 	TheAlarmTest.Test().Next(_L("checking request is pending"));
       
  1930 	TheAlarmTest(status == KRequestPending, __LINE__);
       
  1931 
       
  1932 	// remember the alarm time
       
  1933 	TTime alarmTime = alarmWrite.NextDueTime();
       
  1934 
       
  1935 	// make the Alarm server re-internlize ....
       
  1936 	TheAlarmTest.Test().Next(_L("now simulating restore..."));
       
  1937 	TRAPD(err, SimulateBackupServerRestorationL());
       
  1938 	TheAlarmTest(err==KErrNone, __LINE__);
       
  1939 
       
  1940 
       
  1941 	// allow Alarm server to Internalize...
       
  1942 	User::After(1000000);	// 1s
       
  1943 
       
  1944 
       
  1945 	// check old alarm queue has been cleared
       
  1946 	TheAlarmTest.Test().Next(_L("check alarm notification has been aborted"));
       
  1947 	TheAlarmTest(status != KRequestPending, __LINE__);
       
  1948 
       
  1949 	// Get the abort result
       
  1950 	User::WaitForRequest(status);
       
  1951 	TheAlarmTest(status.Int() == KErrCancel, __LINE__);
       
  1952 
       
  1953 	// check presence of alarm
       
  1954 	TASShdAlarm alarmRead;
       
  1955 	TheAlarmTest.Test().Next(_L("check alarm is no longer a session alarm"));
       
  1956 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarmRead) == KErrNone, __LINE__);
       
  1957 
       
  1958 	TheAlarmTest(!alarmRead.HasOwningSession(), __LINE__);
       
  1959 	TheAlarmTest(alarmRead.HasBecomeOrphaned(), __LINE__);
       
  1960 
       
  1961 	// advance clock, wait for alarm
       
  1962 	User::SetUTCTime(alarmWrite.NextDueTime() - TTimeIntervalSeconds(3));
       
  1963 	TheAlarmTest.Test().Next(_L("waiting for alarm to notify"));
       
  1964 	// wait until after alarm due time
       
  1965 	User::After(KTenSeconds);
       
  1966 
       
  1967 	// alarm should be notifying
       
  1968 	// if Alarm Server server tries to access the TRequestStatus
       
  1969 	// there will be an Access Violation
       
  1970 	TheAlarmTest(TheAlarmTest.Session().GetAlarmDetails(id, alarmRead) == KErrNone, __LINE__);
       
  1971 
       
  1972 	//TheAlarmTest(alarm.State() == EAlarmStateNotifying, __LINE__);
       
  1973 	const TAlarmState alarmState = alarmRead.State();
       
  1974 	TheAlarmTest(alarmState == EAlarmStateNotifying, __LINE__);
       
  1975 	}
       
  1976 
       
  1977 
       
  1978 static void doTestRestoreValidUtcAlarmDataL()
       
  1979 	{
       
  1980 	TheAlarmTest.Test().Next(_L("Data API test"));
       
  1981 	TheAlarmTest.TestClearStoreL();
       
  1982 	TInt r;
       
  1983 	TAlarmId alarmId;
       
  1984 	TASShdAlarm alarm;
       
  1985 	TRequestStatus status;
       
  1986 	//
       
  1987 	alarm.Message()=(_L("Alarm with data"));
       
  1988 	TTime time;
       
  1989 	time.UniversalTime();
       
  1990 	time += TTimeIntervalMinutes(2);
       
  1991 	alarm.SetUtcNextDueTime(time);
       
  1992 	//
       
  1993 	r = TheAlarmTest.Session().AlarmAdd(alarm);
       
  1994 	TheAlarmTest(r==KErrNone, __LINE__);
       
  1995 	//
       
  1996 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  1997 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  1998 	//
       
  1999 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  2000 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  2001 	//
       
  2002 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  2003 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  2004 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2005 	TheAlarmTest(status == EAlarmChangeEventAlarmData, __LINE__);
       
  2006 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  2007 	User::WaitForRequest(status);
       
  2008 	//
       
  2009 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  2010 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  2011 	TheAlarmTest(r == KErrInUse, __LINE__);
       
  2012 	TheAlarmTest(status == KRequestPending, __LINE__);
       
  2013 	//
       
  2014 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  2015 	TheAlarmTest(r == KSomeAlarmData().Size());
       
  2016 	//
       
  2017 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  2018 	r = TheAlarmTest.Session().AlarmDelete(alarm.Id());
       
  2019 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2020 	TheAlarmTest.TestEventBuffer(EAlarmChangeEventAlarmData, status, alarmId);
       
  2021 	//
       
  2022 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  2023 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  2024 	//
       
  2025 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  2026 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  2027 	//
       
  2028 	time.UniversalTime();
       
  2029 	time += TTimeIntervalMinutes(2);
       
  2030 	alarm.SetUtcNextDueTime(time);
       
  2031 	r = TheAlarmTest.Session().AlarmAdd(alarm);
       
  2032 	TheAlarmTest(r==KErrNone, __LINE__);
       
  2033 	//
       
  2034 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  2035 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  2036 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2037 	TheAlarmTest(status == EAlarmChangeEventAlarmData, __LINE__);
       
  2038 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  2039 	User::WaitForRequest(status);
       
  2040 	//
       
  2041 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  2042 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  2043 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2044 	TheAlarmTest.TestEventBuffer(EAlarmChangeEventAlarmData, status, alarmId);
       
  2045 	//
       
  2046 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  2047 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  2048 	//
       
  2049 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  2050 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  2051 	//
       
  2052 	TheAlarmTest.WaitForNotificationBufferToBeEmptied(status, alarmId);
       
  2053 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  2054 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2055 	TheAlarmTest(status == EAlarmChangeEventAlarmData, __LINE__);
       
  2056 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  2057 	User::WaitForRequest(status);
       
  2058 	//
       
  2059 	HBufC8* buf = HBufC8::NewLC(KSomeAlarmData().Size());
       
  2060 	TPtr8 pBuf(buf->Des());
       
  2061 	r = TheAlarmTest.Session().GetAlarmData(alarm.Id(), pBuf);
       
  2062 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2063 	TheAlarmTest(*buf == KSomeAlarmData, __LINE__);
       
  2064 	//
       
  2065 	TheAlarmTest.Test().Next(_L("Waiting for alarm to expire"));
       
  2066 	// advance clock to 3s before alarm due
       
  2067 	User::SetUTCTime(alarm.NextDueTime() - TTimeIntervalSeconds(3));
       
  2068 	TheAlarmTest.WaitForEvent(EAlarmChangeEventTimerExpired, status, alarmId);
       
  2069 	TheAlarmTest(alarmId == alarm.Id(), __LINE__);
       
  2070 	//
       
  2071 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  2072 	TheAlarmTest(r == KErrInUse, __LINE__);
       
  2073 	//
       
  2074 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  2075 	TheAlarmTest(r == KSomeAlarmData().Size(), __LINE__);
       
  2076 	//
       
  2077 	r = TheAlarmTest.Session().AlarmDelete(alarm.Id());
       
  2078 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2079 	//
       
  2080 
       
  2081 	TheAlarmTest.Session().AlarmAddWithNotification(status, alarm, KSomeAlarmData);
       
  2082 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  2083 	HBufC8* alarmdata = HBufC8::NewLC(KSomeAlarmData().Size());
       
  2084 	TPtr8 pAlarmdata(alarmdata->Des());
       
  2085 	r = TheAlarmTest.Session().GetAlarmData(alarm.Id(), pAlarmdata);
       
  2086 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2087 	TheAlarmTest(*alarmdata == KSomeAlarmData, __LINE__);
       
  2088 	TheAlarmTest.Session().AlarmNotificationCancelAndDequeue(alarmId);
       
  2089 	CleanupStack::PopAndDestroy(alarmdata);
       
  2090 	//
       
  2091 	alarm.Reset();
       
  2092 	alarm.Message()=(_L("Alarm with data"));
       
  2093 	time.UniversalTime();
       
  2094 	time += TTimeIntervalMinutes(2);
       
  2095 	alarm.SetUtcNextDueTime(time);
       
  2096 	//
       
  2097 	r =	TheAlarmTest.Session().AlarmAdd(alarm, KSomeAlarmData);
       
  2098 	TheAlarmTest(alarmId = alarm.Id(), __LINE__);
       
  2099 	HBufC8* alarmdata1 = HBufC8::NewLC(KSomeAlarmData().Size());
       
  2100 	TPtr8 pAlarmdata1(alarmdata1->Des());
       
  2101 	r = TheAlarmTest.Session().GetAlarmData(alarm.Id(), pAlarmdata1);
       
  2102 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2103 	TheAlarmTest(*alarmdata1 == KSomeAlarmData, __LINE__);
       
  2104 	CleanupStack::PopAndDestroy(alarmdata1);
       
  2105 
       
  2106 	TheAlarmTest.Test().Next(_L("make real backup of alarmserver ini file"));
       
  2107 	User::After(1000000); // 1s
       
  2108 	BackupAlarmServerIniFileL();
       
  2109 	User::After(1000000); // 1s
       
  2110 
       
  2111 	TheAlarmTest.Test().Next(_L("Delete Alarm"));
       
  2112 	TheAlarmTest(TheAlarmTest.Session().AlarmDelete(alarmId) == KErrNone, __LINE__);
       
  2113 
       
  2114 	User::After(1000000); // 1s
       
  2115 
       
  2116 	TheAlarmTest.Test().Next(_L("restore alarmserver ini file"));
       
  2117 	RestoreAlarmServerIniFileL();
       
  2118 
       
  2119 	User::After(1000000); // 1s
       
  2120 
       
  2121 	//
       
  2122 	TheAlarmTest.Test().Next(_L("testing data restoration"));
       
  2123 	//
       
  2124 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  2125 	TheAlarmTest(r == KErrInUse, __LINE__);
       
  2126 	//
       
  2127 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  2128 	TheAlarmTest(r == KSomeAlarmData().Size(), __LINE__);
       
  2129 	//
       
  2130 	r = TheAlarmTest.Session().AlarmDelete(alarm.Id());
       
  2131 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2132 	//
       
  2133 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  2134 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  2135 	//
       
  2136 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  2137 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  2138 	//
       
  2139 	r = TheAlarmTest.Session().AlarmAdd(alarm);
       
  2140 	TheAlarmTest(r==KErrNone, __LINE__);
       
  2141 	//
       
  2142 	r = TheAlarmTest.Session().AlarmDataAttachL(alarm.Id(), KSomeAlarmData);
       
  2143 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2144 	//
       
  2145 	r = TheAlarmTest.Session().AlarmDataDetach(alarm.Id());
       
  2146 	TheAlarmTest(r == KErrNone, __LINE__);
       
  2147 	//
       
  2148 	r = TheAlarmTest.Session().AlarmDataSize(alarm.Id());
       
  2149 	TheAlarmTest(r == KErrNotFound, __LINE__);
       
  2150 
       
  2151 	CleanupStack::PopAndDestroy(buf);
       
  2152 	}
       
  2153 
       
  2154 
       
  2155 //
       
  2156 
       
  2157 static void doTestsL()
       
  2158 	{
       
  2159 	// Initialise the drive list to empty and then get drive list data from
       
  2160 	// File Server.
       
  2161 	DriveList.FillZ();
       
  2162 	User::LeaveIfError(FsSession.DriveList(DriveList));
       
  2163 
       
  2164 	BackupClient = conn::CSBEClient::NewL();
       
  2165 	CleanupStack::PushL(BackupClient);
       
  2166 
       
  2167 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
  2168 	CleanupStack::PushL(scheduler);
       
  2169 	CActiveScheduler::Install(scheduler);
       
  2170 
       
  2171 	TheAlarmTest.TestStartServers();
       
  2172 	TheAlarmTest(TheAlarmTest.Session().Connect() == KErrNone, __LINE__);
       
  2173 	TheAlarmTest.TestClearStoreL();
       
  2174 
       
  2175 	User::After(100000);
       
  2176 
       
  2177 	// delete AlarmServer.ini file
       
  2178 	FsSession.Delete(KAlarmServerBackupFileFullPath);
       
  2179 	User::After(1000000);
       
  2180 
       
  2181 
       
  2182 	// 1.
       
  2183 	// Baseline for Internalize behaviour
       
  2184 	// [add alarms, shutdown, re-start alarm server, check alarms still queued]
       
  2185 	doTestAlarmQueueOrderShutdownStartupL();
       
  2186 
       
  2187 	// 2.
       
  2188 	// Backup, Restore Internalize behaviour
       
  2189 	// [add alarms, pretend backup/restore, check alarms queued again]
       
  2190 	doTestAlarmQueueOrderBackupRestoreL();
       
  2191 
       
  2192 
       
  2193 	// 3.
       
  2194 	// Correct Alarms are Backed up and Restored
       
  2195 	// [add alarms, backup, delete alarms, add different alarms, restore, check queued alarms]
       
  2196 	doTestAlarmQueueBackupRestoreL();
       
  2197 
       
  2198 	// 4.
       
  2199 	// Alarm Sound Intervals are Backed up and Restored.
       
  2200 	doTestAlarmSoundIntervalsBackupRestoreL();
       
  2201 
       
  2202 	/* 5.
       
  2203 	 * Should be able to add alarm or change Locale during Backup
       
  2204 	 */
       
  2205 	doTestAlarmAddDuringBackupL();
       
  2206 
       
  2207 	/* 6.
       
  2208 	 * Testing Restore behaviour
       
  2209 	 *    a) Should not be able to add alarm during Restore
       
  2210 	 *    b) Should get Notifications at Start & End of Restore
       
  2211 	 *    c) Locale (time) changes during Restore should be processed,
       
  2212 	 *       (but Externalize happens when Restore ends).
       
  2213 	 */
       
  2214 	doTestAlarmAddDuringRestoreL();
       
  2215 
       
  2216 	/* 7.
       
  2217 	 * Testing Failed Restore behaviour
       
  2218 	 *    a) Should get Notifications at Start & End of Restore
       
  2219 	 *    b) Alarm Queue should not change
       
  2220 	 *    c) Alarm Server should perform a new Externalize
       
  2221 	 */
       
  2222 	/* 8, 9.
       
  2223 	 * Handling of Notifying alarm across Shutdown/Startup and Backup/Restore
       
  2224 	 */
       
  2225 	doTestNotifyingAlarmShutdownStartupL();
       
  2226 	doTestNotifyingAlarmBackupRestoreL();
       
  2227 
       
  2228 	/* 10, 11.
       
  2229 	 * Handling of Session alarm
       
  2230 	 */
       
  2231 	doTestSessionAlarmShutdownRestartL();
       
  2232 	doTestSessionAlarmBackupRestoreL();
       
  2233 
       
  2234 	/* 12.
       
  2235 	 * Test restore of alarm with attached data
       
  2236 	 * [create alarm with data, backup, delete alarm, restore, check alarm & data]
       
  2237 	 * (Based on test case in TAlarmData.)
       
  2238 	 */
       
  2239 	doTestRestoreValidAlarmDataL();
       
  2240 
       
  2241 	// UTC Tests
       
  2242 
       
  2243 	// 13.
       
  2244 	// Baseline for Internalize behaviour
       
  2245 	// [add alarms, shutdown, re-start alarm server, check alarms still queued]
       
  2246 	doTestUtcAlarmQueueOrderShutdownStartupL();
       
  2247 
       
  2248 	// 14.
       
  2249 	// Backup, Restore Internalize behaviour
       
  2250 	// [add utc alarms, pretend backup/restore, check alarms queued again]
       
  2251 	doTestUtcAlarmQueueOrderBackupRestoreL();
       
  2252 
       
  2253 
       
  2254 	// 15.
       
  2255 	// Correct UTC Alarms are Backed up and Restored
       
  2256 	// [add utc alarms, backup, delete alarms, add different alarms, restore, check queued alarms]
       
  2257 	doTestUtcAlarmQueueBackupRestoreL();
       
  2258 
       
  2259 	/* 16.
       
  2260 	 * Should be able to add utc alarm or change Locale during Backup
       
  2261 	 */
       
  2262 	doTestUtcAlarmAddDuringBackupL();
       
  2263 
       
  2264 	/* 17.
       
  2265 	 * Testing Restore behaviour
       
  2266 	 *    a) Should not be able to add utc alarm during Restore
       
  2267 	 *    b) Should get Notifications at Start & End of Restore
       
  2268 	 *    c) Locale (time) changes during Restore should be processed,
       
  2269 	 *       (but Externalize happens when Restore ends).
       
  2270 	 */
       
  2271 	doTestUtcAlarmAddDuringRestoreL();
       
  2272 
       
  2273 	/* 18.
       
  2274 	 * Testing Failed Restore behaviour
       
  2275 	 *    a) Should get Notifications at Start & End of Restore
       
  2276 	 *    b) Alarm Queue should not change
       
  2277 	 *    c) Alarm Server should perform a new Externalize
       
  2278 	 */
       
  2279 	/* 19, 20.
       
  2280 	 * Handling of Notifying UTC alarm across Shutdown/Startup and Backup/Restore
       
  2281 	 */
       
  2282 	doTestNotifyingUtcAlarmShutdownStartupL();
       
  2283 	doTestNotifyingUtcAlarmBackupRestoreL();
       
  2284 
       
  2285 	/* 21, 22.
       
  2286 	 * Handling of Session UTC alarm
       
  2287 	 */
       
  2288 	doTestSessionUtcAlarmShutdownRestartL();
       
  2289 	doTestSessionUtcAlarmBackupRestoreL();
       
  2290 
       
  2291 	/* 23.
       
  2292 	 * Test restore of alarm with attached data
       
  2293 	 * [create utc alarm with data, backup, delete alarm, restore, check alarm & data]
       
  2294 	 * (Based on test case in TAlarmData.)
       
  2295 	 */
       
  2296 	doTestRestoreValidUtcAlarmDataL();
       
  2297 
       
  2298 	/* 24.
       
  2299 	 * Baseline for Internalize behaviour
       
  2300 	 * [add alarms, shutdown, re-start alarm server, check local due time is unchanged]
       
  2301 	 */
       
  2302 	doTestUtcAlarmDSTShutdownStartupL();
       
  2303 	doTestFloatingAlarmDSTShutdownStartupHelsinkiL();
       
  2304 
       
  2305 	// cleanup
       
  2306 	CleanupStack::PopAndDestroy(scheduler);
       
  2307 	CleanupStack::PopAndDestroy(BackupClient);
       
  2308 	TheAlarmTest.TestClearStoreL();
       
  2309 	}
       
  2310 
       
  2311 //
       
  2312 
       
  2313 /**
       
  2314 
       
  2315 @SYMTestCaseID PIM-TBACKUPALARMS-0001
       
  2316 
       
  2317 */
       
  2318 
       
  2319 GLDEF_C TInt E32Main()
       
  2320     {
       
  2321 	__UHEAP_MARK;
       
  2322 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
  2323 	if	(!cleanup)
       
  2324 		return KErrNoMemory;
       
  2325 
       
  2326 	TheAlarmTest.Test().Title();
       
  2327 	TheAlarmTest.Test().Start(_L("@SYMTestCaseID:PIM-TBACKUPALARMS-0001 Backup Alarms"));
       
  2328 	// initialise globals
       
  2329 	FsSession.Connect();
       
  2330 
       
  2331 	TheAlarmTest.Test().Next(_L("SECURE_DATA mode"));
       
  2332 
       
  2333 	// actually do tests
       
  2334 	TRAPD(error, doTestsL());
       
  2335 
       
  2336 	// Cleanup files
       
  2337 	// delete test file
       
  2338 	FsSession.Delete(KAlarmServerTestBackupFile);
       
  2339 	// delete AlarmServer.ini file
       
  2340 	FsSession.Delete(KAlarmServerBackupFileFullPath);
       
  2341 	FsSession.Close();
       
  2342 
       
  2343 	// did any test Leave?
       
  2344 	TheAlarmTest(error == KErrNone, __LINE__);
       
  2345 
       
  2346 	//
       
  2347 	TheAlarmTest.Test().End();
       
  2348 	TheAlarmTest.Test().Close();
       
  2349 	ASTstAlarmTest::Close();
       
  2350 	delete cleanup;
       
  2351 	__UHEAP_MARKEND;
       
  2352 	return KErrNone;
       
  2353     }