os/ossrv/genericservices/taskscheduler/Test/Scheduling/TC_TSCH_SCHEDULING2.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2004-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 <csch_cli.h>
    17 #include "Thelpers.h"
    18 
    19 #include <e32base.h>
    20 #include <e32test.h>
    21 #include <f32file.h>
    22 #include <s32file.h>
    23 
    24 #include "TestUtils.h"
    25 
    26 
    27 //If suddenly all SCHSVR tests begin failing, the OOM conditions might be the reason.
    28 //TScheduling test tries to create KNumberOfSchedulesToCreate tasks, loading enormously 
    29 //the task scheduling server. The task scheduling server fails and stays loaded in memory
    30 //with many scheduling tasks holding large amounts of allocated memory in this way.
    31 //The next SCHSVR tests may fail because of OOM.
    32 //KNumberOfSchedulesToCreate value was 100 originally. Now it is 20.
    33 
    34 //
    35 // Literal constants
    36 //
    37 _LIT(KTestName,								"TC_TSCH_SCHEDULING2");
    38 
    39 //
    40 // Type definitions
    41 //
    42 typedef CArrayFixFlat<TScheduleEntryInfo>	CSchEntryInfoArray;
    43 typedef CArrayFixFlat<TTaskInfo>			CTaskInfoArray;
    44 typedef CArrayFixFlat<TSchedulerItemRef>    CSchItemRefArray;
    45 
    46 //
    47 // Global data
    48 //
    49 RTest										TheTest(KTestName);
    50 static TInt64								TheSeed;
    51 static RScheduler							TheScheduler;
    52 static CTrapCleanup*						TheCleanup;
    53 static RFs									TheFsSession;
    54 
    55 _LIT(KMinimalTaskHandler, "MinimalTaskHandler");
    56 
    57 
    58 //***********************************************************************************
    59 // Extract task info from the schedule server based on a schedule ID
    60 static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
    61 						TInt aScheduleId)
    62 	{
    63 	aTaskInfoArray.Reset();
    64 	TTime nextTimeScheduleIsDue;
    65 	TScheduleState state;
    66 	CSchEntryInfoArray* entries 
    67 		= new (ELeave) CSchEntryInfoArray(3);
    68 	CleanupStack::PushL(entries);
    69 	TInt res = TheScheduler.GetScheduleL(aScheduleId, 
    70 										state, 
    71 										*entries, 
    72 										aTaskInfoArray, 
    73 										nextTimeScheduleIsDue);
    74 	TEST2(res, KErrNone);
    75 	CleanupStack::PopAndDestroy(entries);
    76 	}
    77 
    78 // count the number of tasks associated with this schedule
    79 static TInt CountTasksL(TInt aScheduleId)	
    80 	{
    81 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
    82 	CleanupStack::PushL(tasks);
    83 	GetTaskInfoL(*tasks, aScheduleId);
    84 	TInt ret = tasks->Count();
    85 	CleanupStack::PopAndDestroy(tasks);
    86 	return ret;
    87 	}
    88 
    89 // count the number of schedules based on a filter.
    90 static TInt CountScheduledItemsL(TScheduleFilter aFilter, 
    91 								RScheduler& aScheduler)
    92 	{
    93 	CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
    94 	CleanupStack::PushL(refs);
    95 
    96 	TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
    97 	TEST2(res, KErrNone);
    98 
    99 	TInt count = refs->Count();
   100 	CleanupStack::PopAndDestroy(); // refs
   101 	return count;
   102 	}
   103 
   104 //creates a daily schedule with StartTime of aStartTime
   105 static TInt CreateScheduleL(TSchedulerItemRef& aRef, 
   106 							RScheduler& aScheduler, 
   107 							const TTime& aStartTime)
   108 	{
   109 	CSchEntryInfoArray* entryList 
   110 		= new (ELeave) CSchEntryInfoArray(1);
   111 	CleanupStack::PushL(entryList);
   112 
   113 	TScheduleEntryInfo entry1;
   114 	entry1.iStartTime		= aStartTime;
   115 	entry1.iInterval		= 1; // TTimeIntervalDays
   116 	entry1.iIntervalType	= EDaily;
   117 	entry1.iValidityPeriod	= 30; // minutes
   118 	entryList->AppendL(entry1);
   119 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
   120 	CleanupStack::PopAndDestroy(); // entryList
   121 	return res;
   122 	}
   123 	
   124 // creates a schedule with numerous entries	
   125 static TInt CreateSchedule1L(TSchedulerItemRef& aRef, RScheduler& aScheduler)
   126 	{
   127 	aRef.iName = _L("Schedule created using \"CreateSchedule1L\"");
   128 
   129 	CSchEntryInfoArray* entryList = new (ELeave) CSchEntryInfoArray(3);
   130 	CleanupStack::PushL(entryList);
   131 
   132 	{
   133 	TScheduleEntryInfo entry1;
   134 	entry1.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(20, 0); // 0m:20s from "now"
   135 	entry1.iInterval		= 1;
   136 	entry1.iIntervalType	= EDaily;
   137 	entry1.iValidityPeriod	= 20;
   138 	entryList->AppendL(entry1);
   139 	}
   140 	{
   141 	TScheduleEntryInfo entry2;
   142 	entry2.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(00, 1); // 1m:00s from "now"
   143 	entry2.iInterval		= 1;
   144 	entry2.iIntervalType	= EDaily;
   145 	entry2.iValidityPeriod	= 20;
   146 	entryList->AppendL(entry2);
   147 	}
   148 	{
   149 	TScheduleEntryInfo entry3;
   150 	entry3.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, -1); // -1 year from "now"
   151 	entry3.iInterval		= 1;
   152 	entry3.iIntervalType	= EDaily;
   153 	entry3.iValidityPeriod	= 20;
   154 	entryList->AppendL(entry3);
   155 	}
   156 
   157 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
   158 	CleanupStack::PopAndDestroy(); // entryList
   159 	return res;
   160 	}
   161 
   162 // creates a schedule with numerous entries	
   163 static TInt CreateSchedule2L(TSchedulerItemRef& aRef, RScheduler& aScheduler)
   164 	{
   165 	CSchEntryInfoArray* entryList = new (ELeave) CSchEntryInfoArray(3);
   166 	CleanupStack::PushL(entryList);
   167 
   168 	aRef.iName = _L("Schedule created using \"CreateSchedule2L\"");
   169 
   170 	{
   171 	TScheduleEntryInfo entry1;
   172 	entry1.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(30, 2); // 2m:30s from "now"
   173 	entry1.iInterval		= 1;
   174 	entry1.iIntervalType	= EDaily;
   175 	entry1.iValidityPeriod	= 20;
   176 	entryList->AppendL(entry1);
   177 	}
   178 	{
   179 	TScheduleEntryInfo entry2;
   180 	entry2.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(0, 5);  // 5m:00s from "now"
   181 	entry2.iInterval		= 1;
   182 	entry2.iIntervalType	= EDaily;
   183 	entry2.iValidityPeriod	= 20;
   184 	entryList->AppendL(entry2);
   185 	}
   186 
   187 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
   188 	CleanupStack::PopAndDestroy(); // entryList
   189 	return res;
   190 	}
   191 
   192 // creates a schedule with numerous entries	
   193 static TInt CreateSchedule3L(TSchedulerItemRef& aRef, RScheduler& aScheduler)
   194 	{
   195 	CSchEntryInfoArray* entryList = new (ELeave) CSchEntryInfoArray(3);
   196 	CleanupStack::PushL(entryList);
   197 
   198 	aRef.iName = _L("Schedule created using \"CreateSchedule3L\"");
   199 
   200 	{
   201 	TScheduleEntryInfo entry1;
   202 	entry1.iIntervalType	= EDaily;
   203 	entry1.iInterval		= 1; // repeat every day
   204 	entry1.iValidityPeriod	= 5; // valid for only 5 minutes 
   205 	entry1.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(0, 9, 0, 20); // 9mins and 20days in the future
   206 	entryList->AppendL(entry1);
   207 	}
   208 	
   209 	{
   210 	TScheduleEntryInfo entry2;
   211 	entry2.iIntervalType	= EDaily;
   212 	entry2.iInterval		= 1;
   213 	entry2.iValidityPeriod	= 5;
   214 	entry2.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(0, 11, 0, 20); // 11mins and 20days in the future
   215 	entryList->AppendL(entry2);
   216 	}
   217 
   218 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
   219 	CleanupStack::PopAndDestroy(); // entryList
   220 
   221 	return res;
   222 	}
   223 
   224 // schedules a persistent task associated with the supplied schedule ID
   225 static TInt SchedulePersistentTaskL(const TName& aName, 
   226 									TInt& aNewId, 
   227 									TInt aScheduleId, 
   228 									TInt aRepeat, 
   229 									RScheduler& aScheduler)
   230 	{
   231 	TTaskInfo taskInfo;
   232 	taskInfo.iTaskId = aNewId;
   233 	taskInfo.iName = aName;
   234 	taskInfo.iPriority = 2;
   235 	taskInfo.iRepeat = aRepeat;
   236 	HBufC* data = _L("the data").AllocLC();
   237 	TInt res = aScheduler.ScheduleTask(taskInfo, *data, aScheduleId);
   238 	aNewId = taskInfo.iTaskId;
   239 
   240 	CleanupStack::PopAndDestroy(); // data
   241 	return res;
   242 	}
   243 
   244 /**
   245 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1033
   246 @SYMTestCaseDesc	    Test 1 - verifies fix for defect:
   247 						Registering twice with the task scheduler causes a memory leak(EDNAWIR-4FQJ6A)
   248 @SYMTestPriority 	    High
   249 @SYMTestActions  	    Connect to scheduler and register twice with the task scheduler.Check for memory errors,
   250 @SYMTestExpectedResults Test must not fail
   251 @SYMREQ                 REQ0000
   252 */		
   253 static void Test1L()
   254 	{
   255 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1033 TheTest1: Registering with the tasks scheduler without disconnecting "));    
   256 	__UHEAP_MARK;
   257 	TheTest.Next(_L("Connect to Scheduler"));
   258 	TInt res = TheScheduler.Connect();
   259 	TEST2(res, KErrNone);
   260 
   261 	TheTest.Next(_L("Registering Client"));
   262 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   263 
   264 	TheScheduler.Close();
   265 	__UHEAP_MARKEND;
   266 
   267 	__UHEAP_MARK;
   268 	TheTest.Next(_L("Connect to Scheduler again"));
   269 	res = TheScheduler.Connect();
   270 	TEST2(res, KErrNone);
   271 
   272 	TheTest.Next(_L("Registering client again"));
   273 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   274 	TheScheduler.__DbgMarkHeap();
   275 
   276 	TheTest.Next(_L("Register when already registered"));
   277 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   278 
   279 	TheTest.Next(_L("Cancel registering client and check for memory leak"));
   280 	TheScheduler.__DbgMarkEnd(0);
   281 	TheScheduler.Close();
   282 	__UHEAP_MARKEND;
   283 	}
   284 
   285 /**
   286 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1034
   287 @SYMTestCaseDesc	    Test 2 - verifies fix for defect
   288 						Messages with "Resend" status are not sent after reboot (EDNHLJT-4TRAAE)
   289 @SYMTestPriority 	    High
   290 @SYMTestActions  	    Add a task to the schedule,kill the server and re-register the client,wait for one minute for task to fire
   291 @SYMTestExpectedResults Test must not fail
   292 @SYMREQ                 REQ0000
   293 */		
   294 static void Test2L()
   295 //
   296 //
   297 	{
   298 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1034 TheTest2: Resend after hard reset (simulation) "));
   299 
   300 	TheTest.Next(_L("Connect to Scheduler"));
   301 	TInt res = TheScheduler.Connect();
   302 	TEST2(res, KErrNone);
   303 
   304 	TheTest.Next(_L("Registering Client"));
   305 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   306 
   307 	// Create a schedule
   308 	TheTest.Next(_L("Creating schedule"));
   309 	TSchedulerItemRef scheduleHandle;
   310 	TTime time;
   311 	time.HomeTime();
   312 	time += TTimeIntervalSeconds(2); //Task to go off two seconds from now
   313 	User::LeaveIfError(CreateScheduleL(scheduleHandle, TheScheduler, time));  
   314 
   315 	// Add a task to the schedule
   316 	TheTest.Next(_L("Creating task for schedule"));
   317 	{
   318 	TTaskInfo taskInfo;
   319 	taskInfo.iName = _L("MyTaskName");
   320 	taskInfo.iPriority = 2;
   321 	taskInfo.iTaskId = 0;
   322 	taskInfo.iRepeat = 1;
   323 	HBufC* data = _L("Task Data").AllocLC();
   324 	TInt res = TheScheduler.ScheduleTask(taskInfo, *data, scheduleHandle.iHandle);
   325 	CleanupStack::PopAndDestroy(); // data
   326 	TEST2(res, KErrNone);
   327 	}
   328 
   329 	// Kill the server !!
   330 	TheTest.Next(_L("Killing server"));
   331 	// Need to turn off JIT dubugging as we are panicking server and we 
   332 	// want test to keep running.
   333 	TBool jit = User::JustInTime();
   334 	User::SetJustInTime(EFalse);
   335 
   336 	TheScheduler.__FaultServer();
   337 	User::After(100000);
   338 	
   339 	// Turn on JIT again.
   340 	User::SetJustInTime(jit);
   341 	
   342 	// Connect to the server again
   343 	TheTest.Next(_L("Re-connect to Scheduler"));
   344 	res = TheScheduler.Connect();
   345 	TEST2(res, KErrNone);
   346 
   347 	// Re-register
   348 	TheTest.Next(_L("Re-register Client"));
   349 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   350 
   351 	TheTest.Next(_L("Check schedule count and task count"));
   352 	TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   353 	TEST(scheduleCount == 1);
   354 	TInt taskCount = CountTasksL(scheduleHandle.iHandle);
   355 	TEST(taskCount == 1);
   356 	
   357 	// Wait for task to fire... It should happen in about 2 seconds
   358 	TheTest.Next(_L("Waiting for task to complete"));
   359 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
   360 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   361 	}
   362 
   363 /**
   364 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1035
   365 @SYMTestCaseDesc	    This test establishes that the change to RScheduler::GetScheduleL is functionally correct
   366 @SYMTestPriority 	    High
   367 @SYMTestActions  	    Tests changes to Schedule Server API as of Change Request document AALR-4EDG75
   368 @SYMTestExpectedResults Test must not fail
   369 @SYMREQ                 REQ0000
   370 */		
   371 static void Test3L()
   372 	{
   373 	//
   374 	//
   375 	// Test changes to Schedule Server API as of Change Request document AALR-4EDG75
   376 	// (GT change requests database)
   377 	//
   378 	//
   379 	//
   380 	// This test establishes that the change to...
   381 	//
   382 	//	RScheduler::GetScheduleL(const TInt aScheduleHandle,
   383 	//								 TScheduleState& aState,
   384 	//								 CArrayFixFlat<TScheduleEntryInfo>& aEntries,
   385 	//								 CArrayFixFlat<TTaskInfo>& aTasks,
   386 	//								 TTime& aNextDue)
   387 	//
   388 	// ...is functionally correct.
   389 	// 
   390 	//
   391 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1035 Test change request AALR-4EDG75 implementation "));
   392 	
   393 	TheTest.Next(_L("Connect to Scheduler"));
   394 	TInt res = TheScheduler.Connect();
   395 	TEST2(res, KErrNone);
   396 
   397 	TheTest.Next(_L("Registering Client"));
   398 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   399 
   400 	const TDateTime KTimeToStartTask(2000, EJanuary, 10, 15, 30, 0, 0);
   401 
   402 	TSchedulerItemRef schedulerItemReference;
   403 	CSchEntryInfoArray* entryArray = new(ELeave) CSchEntryInfoArray(1);
   404 	CleanupStack::PushL(entryArray);
   405 	
   406 	HBufC* taskData = _L("This is some dummy task data created for testing").AllocL();
   407 	CleanupStack::PushL(taskData);
   408 	
   409 	// Prepare the task info - this describes the tasks that are contained within the task
   410 	// entry array
   411 	TTaskInfo taskInfo = SchSvrHelpers::TaskInfo(_L("A transient test task"), 100);
   412 		
   413 	// Create a schedule entry and append it to the entry array
   414 	{
   415 	TScheduleEntryInfo scheduleEntry = SchSvrHelpers::ScheduleEntryInfo(EDaily, TTime(KTimeToStartTask), 7, 2);
   416 	entryArray->AppendL(scheduleEntry);
   417 	}
   418 
   419 	// Create the transient task
   420 	TInt ret = TheScheduler.ScheduleTask(taskInfo, *taskData, schedulerItemReference, *entryArray);
   421 	TEST2(ret, KErrNone);
   422 
   423 	// Check that the task Id after scheduling the event is not 
   424 	// the same as it was prior to the requesting the service
   425 	TEST(taskInfo.iTaskId != -1);
   426 
   427 	//
   428 	// Now obtain info about the scheduled transient task...
   429 	//
   430 	TScheduleState scheduleState;
   431 	TTime nextTaskDueTime;
   432 
   433 	// Reset the schedule entry info array as the server now has copies of this and it is
   434 	// no longer required client-side
   435 	entryArray->Reset();
   436 	CTaskInfoArray* taskInfoArray = new(ELeave) CTaskInfoArray(4);
   437 	CleanupStack::PushL(taskInfoArray);
   438 
   439 	// Request task schedule information from the server
   440 	ret = TheScheduler.GetScheduleL(schedulerItemReference.iHandle, scheduleState, *entryArray, *taskInfoArray, nextTaskDueTime);
   441 	TEST2(ret, KErrNone);
   442 
   443 	// Because this is a daily task which is scheduled to occur at a specific time (but the date
   444 	// cannot necessarily be established, we can perform a simple check to ensure that the
   445 	// time when the task is next scheduled to run falls within the 15:30 - 17:30 bracket.
   446 	TEST(SchSvrHelpers::IsTimeTheSame(nextTaskDueTime, TTime(KTimeToStartTask)) == (TInt) ETrue);
   447 
   448 	// Establish and test the size of the task data for the specified task object
   449 	TInt sizeOfTaskData = 0;
   450 	TEST2(TheScheduler.GetTaskDataSize(taskInfo.iTaskId, sizeOfTaskData), KErrNone);
   451 	TEST(sizeOfTaskData == taskData->Length());
   452 
   453 	// Now check the information return from the server pertaining to a specific task...
   454 	{
   455 	TTaskInfo taskFromServer;
   456 	HBufC* taskDataFromServer					= HBufC::NewLC(sizeOfTaskData);
   457 	TPtr pTaskDataFromServer					= taskDataFromServer->Des();
   458 	TTime nextDueTimeFromServer					= Time::NullTTime();
   459 	TSchedulerItemRef schedulerRefFromServer;
   460 	TEST2(TheScheduler.GetTaskInfoL(taskInfo.iTaskId, taskFromServer, pTaskDataFromServer, schedulerRefFromServer, nextDueTimeFromServer), KErrNone);
   461 	TEST(SchSvrHelpers::IsTimeTheSame(nextTaskDueTime, TTime(KTimeToStartTask)) == (TInt) ETrue);
   462 	TEST(SchSvrHelpers::IsTaskInfoTheSame(taskFromServer, taskInfo) == (TInt) ETrue);
   463 	TEST(SchSvrHelpers::IsItemRefTheSame(schedulerRefFromServer, schedulerItemReference) == (TInt) ETrue);
   464 	CleanupStack::PopAndDestroy(); // taskDataFromServer
   465 	}
   466 
   467 	// Disable the schedule and check when it is next schedule to run
   468 	TEST2(TheScheduler.DisableSchedule(schedulerItemReference.iHandle), KErrNone);
   469 
   470 	// Get the new schedule info - check that the nextDueTime is still reported even
   471 	// though the schedule has been disabled
   472 	nextTaskDueTime = Time::NullTTime();
   473 	TEST2(TheScheduler.GetScheduleL(schedulerItemReference.iHandle, scheduleState, *entryArray, *taskInfoArray, nextTaskDueTime), KErrNone);
   474 	TEST(SchSvrHelpers::IsTimeTheSame(nextTaskDueTime, TTime(KTimeToStartTask)) == (TInt) ETrue);
   475 	TEST(SchSvrHelpers::IsTaskInfoTheSame(taskInfoArray->At(0), taskInfo) == (TInt) ETrue);
   476 
   477 	// Re-enable the schedule
   478 	TEST2(TheScheduler.EnableSchedule(schedulerItemReference.iHandle), KErrNone);
   479 
   480 	// Delete the only task (relating to this test) from the server
   481 	TEST2(TheScheduler.DeleteTask(taskInfo.iTaskId), KErrNone);
   482 	ret = TheScheduler.GetScheduleL(schedulerItemReference.iHandle, scheduleState, *entryArray, *taskInfoArray, nextTaskDueTime);
   483 	TEST2(ret, KErrNotFound); // there is no longer any tasks associated with this schedule
   484 
   485 	CleanupStack::PopAndDestroy(3); // taskInfoArray, entryArray, taskData
   486 	}
   487 
   488 /**
   489 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1036
   490 @SYMTestCaseDesc	    Tests for defect EDNAALR-4JKEFC
   491 @SYMTestPriority 	    High
   492 @SYMTestActions  	    Create the schedule for the task,re-register the client with the server.
   493                         check for the information that the scheduler knows about.
   494 @SYMTestExpectedResults Test must not fail
   495 @SYMREQ                 REQ0000
   496 */		
   497 static void Test4L()
   498 	{
   499 	// Test title
   500 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1036 \nTest for defect EDNAALR-4JKEFC "));
   501 	
   502 	TheTest.Next(_L("Connect to Scheduler"));
   503 	TInt res = TheScheduler.Connect();
   504 	TEST2(res, KErrNone);
   505 
   506 	TheTest.Next(_L("Registering Client"));
   507 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   508 
   509 	// Constants / vars used in this function
   510 	TSchedulerItemRef itemRef;
   511 
   512 	// Create some scheduling entries
   513 	CArrayFixFlat<TScheduleEntryInfo>* entries = new(ELeave) CArrayFixFlat<TScheduleEntryInfo>(10);
   514 	CleanupStack::PushL(entries);
   515 
   516 	TScheduleEntryInfo entry1;
   517 	entry1.iIntervalType	= EHourly;
   518 	entry1.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(1, 1); // 1m:01s from "now"
   519 	entry1.iInterval		= 1;
   520 	entry1.iValidityPeriod	= 20;
   521 	entries->AppendL(entry1);
   522 
   523 	TScheduleEntryInfo entry2;
   524 	entry2.iIntervalType	= EHourly;
   525 	entry2.iStartTime		= SchSvrHelpers::TimeBasedOnOffset(5, 5); // 5m:05s from "now"
   526 	entry2.iInterval		= 1;
   527 	entry2.iValidityPeriod	= 20;
   528 	entries->AppendL(entry2);
   529 
   530 	// Create the schedule for the task...
   531 	res = TheScheduler.CreatePersistentSchedule(itemRef, *entries);
   532 	TEST2(res, KErrNone);
   533 
   534 	// Create the tasks themselves..
   535 	TTaskInfo task;
   536 	task.iRepeat	= 10; // repeat once
   537 	task.iName		= _L("Test Task For Defect Verification");
   538 	task.iPriority	= 100;
   539 	HBufC* taskData = task.iName.AllocLC();
   540 	res = TheScheduler.ScheduleTask(task, *taskData, itemRef.iHandle);
   541 	CleanupStack::PopAndDestroy(); // taskData
   542 
   543 	{
   544 	CArrayFixFlat<TSchedulerItemRef>* refs = new CArrayFixFlat<TSchedulerItemRef>(3);
   545 	CleanupStack::PushL(refs);
   546 	TInt res = TheScheduler.GetScheduleRefsL(*refs, EAllSchedules); 
   547 	TEST2(res, KErrNone);
   548 	CleanupStack::PopAndDestroy(); // refs
   549 	}
   550 
   551 	// Re-register theclient with the server
   552 	for(TInt i=0; i<5; i++)
   553 		{
   554 		// Log off from the task scheduler
   555 		TheScheduler.Close();
   556 		res = TheScheduler.Connect();
   557 		TEST2(res, KErrNone);
   558 
   559 		User::After(1000000); 
   560 
   561 		TheTest.Next(_L("===== Re-registering Client (wait 10 secs) ====="));
   562 		res = SchSvrHelpers::RegisterClientL(TheScheduler);
   563 		TEST2(res, KErrNone);
   564 			{
   565 			CArrayFixFlat<TSchedulerItemRef>* refs = new CArrayFixFlat<TSchedulerItemRef>(3);
   566 			CleanupStack::PushL(refs);
   567 			TInt res = TheScheduler.GetScheduleRefsL(*refs, EAllSchedules);
   568 			TEST2(res, 0);
   569 			CleanupStack::PopAndDestroy(); // refs
   570 			}
   571 
   572 		// Check the information that the scheduler knows about...
   573 		TInt taskDataSize = 0;
   574 		res = TheScheduler.GetTaskDataSize(task.iTaskId, taskDataSize);
   575 		TEST2(res, KErrNone);
   576 		TEST(taskDataSize == task.iName.Length());
   577 		TTaskInfo taskInfoFromServer;
   578 		taskData = HBufC::NewLC(taskDataSize);
   579 		TPtr pTaskData = taskData->Des();
   580 
   581 		TTime nextDueTime(Time::NullTTime());
   582 		res = TheScheduler.GetTaskInfoL(task.iTaskId, taskInfoFromServer, pTaskData, itemRef, nextDueTime);
   583 		TEST2(res, KErrNone);
   584 		TEST(pTaskData == task.iName);
   585 		CleanupStack::PopAndDestroy(); // taskData
   586 		}
   587 
   588 	CleanupStack::PopAndDestroy(); // entries
   589 
   590 	}
   591 
   592 /**
   593 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1037
   594 @SYMTestCaseDesc	    Tests for basic scheduler implementations 
   595 @SYMTestPriority 	    High
   596 @SYMTestActions  	    Mark heap,create persistent schedules, schedule tasks,transient schedules,
   597                         delete tasks,delete remaing schedules,check heap
   598 @SYMTestExpectedResults Test must not fail
   599 @SYMREQ                 REQ0000
   600 */		
   601 static void Test5L()
   602 	{
   603 	TInt res = KErrNone;
   604 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1037 ===== Starting test 1 ====="));
   605 	__UHEAP_MARK;
   606 	
   607 	TSchedulerItemRef ref1;
   608 	TSchedulerItemRef ref2;
   609 	TSchedulerItemRef ref3;
   610 
   611 	// Remove all existing schedules before starting the test
   612 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
   613 	
   614 	TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   615 	TEST(scheduleCount == 0); // check that no schedules are present.
   616 	
   617 	TheTest.Printf(_L("Create some schedules\n"));
   618 	res = CreateSchedule1L(ref1, TheScheduler); // +20sec, +1min, -1year
   619 	TEST2(res, KErrNone);	
   620 	res = CreateSchedule2L(ref2, TheScheduler); // +2min 30sec, +5min
   621 	TEST2(res, KErrNone);
   622 	res = CreateSchedule3L(ref3, TheScheduler); // +20day 9min, +20day 11min
   623 	TEST2(res, KErrNone);
   624 
   625 	TSchedulerItemRef ref4;
   626 	TSchedulerItemRef ref5;
   627 	res = CreateSchedule2L(ref4, TheScheduler); // +2min 30sec, 5min
   628 	TEST2(res, KErrNone);
   629 	res = CreateSchedule3L(ref5, TheScheduler); // +20day 9min, +20day 11min
   630 	TEST2(res, KErrNone);
   631 
   632 	TInt task1 = 0;
   633 	TInt task2 = 0;
   634 	TInt task3 = 0;
   635 	TInt task4 = 0;
   636 	TName name1 = (_L("web subscription"));
   637 	TName name2 = (_L("another web subscription"));
   638 	TName name3 = (_L("third web subscription"));
   639 
   640 	TheTest.Printf(_L("Schedule some tasks\n"));
   641 
   642 	// NOTE: have to put repeats here of > 0 otherwise the task will run immediately
   643 	// (because it's schedule specifies a date of 1 year in the past!) and be
   644 	// removed (by the server) before we have a chance to delete it....
   645 	res = SchedulePersistentTaskL(name1, task1, ref1.iHandle, 3, TheScheduler);
   646 	TEST2(res, KErrNone);
   647 	res = SchedulePersistentTaskL(name2, task2, ref2.iHandle, 3, TheScheduler);
   648 	TEST2(res, KErrNone);
   649 	res = SchedulePersistentTaskL(name3, task3, ref3.iHandle, 3, TheScheduler);
   650 	TEST2(res, KErrNone);
   651 	res = SchedulePersistentTaskL(name3, task4, ref3.iHandle, 3, TheScheduler);
   652 	TEST2(res, KErrNone);
   653 
   654 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   655 	TEST(scheduleCount == 5); // 5 persistant
   656 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   657 
   658 	TheTest.Printf(_L("Deleting task with id %d\n"), task1);
   659 	res = TheScheduler.DeleteTask(task1);
   660 	TEST2(res, KErrNone);
   661 	TheTest.Printf(_L("Deleting schedule with id %d\n"), ref1.iHandle);
   662 	res = TheScheduler.DeleteSchedule(ref1.iHandle);
   663 	TEST2(res, KErrNone);
   664 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   665 	// 4 persistant expected as we have deleted one
   666 	TEST(scheduleCount == 4); 
   667 
   668 	TheTest.Printf(_L("Deleting task with id %d\n"), task2);
   669 	res = TheScheduler.DeleteTask(task2);
   670 	TEST2(res, KErrNone);
   671 	TheTest.Printf(_L("Deleting schedule with id %d\n"), ref2.iHandle);
   672 	res = TheScheduler.DeleteSchedule(ref2.iHandle);
   673 	TEST2(res, KErrNone);
   674 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   675 	// 3 persistant  expected as we have deleted one
   676 	TEST(scheduleCount == 3); 
   677 
   678 	TheTest.Printf(_L("Deleting task with id %d\n"), task3);
   679 	res = TheScheduler.DeleteTask(task3);
   680 	TEST2(res, KErrNone);
   681 	TheTest.Printf(_L("Deleting task with id %d\n"), task4);
   682 	res = TheScheduler.DeleteTask(task4);
   683 	TEST2(res, KErrNone);
   684 	TheTest.Printf(_L("Deleting schedule with id %d\n"), ref3.iHandle);
   685 	res = TheScheduler.DeleteSchedule(ref3.iHandle);
   686 	TEST2(res, KErrNone);
   687 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   688 	// 2 persistant  expected as we have deleted one
   689 	TEST(scheduleCount == 2); 
   690 
   691 	TheTest.Printf(_L("Deleting schedule with id %d\n"), ref4.iHandle);
   692 	res = TheScheduler.DeleteSchedule(ref4.iHandle);
   693 	TEST2(res, KErrNone);
   694 	TheTest.Printf(_L("Deleting schedule with id %d\n"), ref5.iHandle);
   695 	res = TheScheduler.DeleteSchedule(ref5.iHandle);
   696 	TEST2(res, KErrNone);
   697 
   698 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   699 	TEST(scheduleCount == 0); 
   700 	
   701 	SchSvrHelpers::Pause(TheTest);
   702 	__UHEAP_MARKEND;
   703 	}
   704 
   705 /**
   706 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1038
   707 @SYMTestCaseDesc	    Tests for Transient,non-repeating - waits for schedule to activate
   708 @SYMTestPriority 	    High
   709 @SYMTestActions  	    Create transient schedule with non-repeating task
   710                         Tests for no items as schedule deletes itself after task has completed
   711 @SYMTestExpectedResults Test must not fail
   712 @SYMREQ                 REQ0000
   713 */		
   714 static void Test6L()
   715 	{
   716 	// Heap testing removed because this is a flakey bit of code.
   717 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1038 Transient, non-repeating - waits for schedule to activate "));
   718 	
   719 	// Remove all existing schedules before starting the test
   720 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
   721 
   722 	// Create transient schedule
   723 	TheTest.Printf(_L("Create transient schedule with non-repeating task\n"));
   724 	TSchedulerItemRef ref;
   725 	CSchEntryInfoArray* entryList = new(ELeave) CSchEntryInfoArray(1);
   726 	CleanupStack::PushL(entryList);
   727 	ref.iName = _L("A Transient Schedule");
   728 
   729 	// Create schedule entry
   730 	TScheduleEntryInfo entry;
   731 	entry.iStartTime = SchSvrHelpers::TimeBasedOnOffset(5); // 5 secs in the future
   732 	entry.iInterval = 1;
   733 	entry.iIntervalType = EDaily;
   734 	entry.iValidityPeriod = 20;
   735 	entryList->AppendL(entry);
   736 	
   737 	// Create schedule task
   738 	TTaskInfo taskInfo;
   739 	taskInfo.iName = _L("mail");
   740 	taskInfo.iTaskId = 0;
   741 	taskInfo.iRepeat = 1;
   742 	taskInfo.iPriority = 2;
   743 	HBufC* data = _L("This is the data for the task").AllocLC();
   744 
   745 	// Schedule the item
   746 	TInt res = TheScheduler.ScheduleTask(taskInfo, *data, ref, *entryList);
   747 	TEST2(res, KErrNone);
   748 	CleanupStack::PopAndDestroy(2, entryList); // data, entryList
   749 
   750 	// Should be one item
   751 	TEST(CountScheduledItemsL(EAllSchedules, TheScheduler) == 1);
   752 
   753 	// Waiting for entry to complete
   754 	TheTest.Next(_L("Waiting for task to complete"));
   755 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
   756 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   757 	// Should be no items as schedule deletes itself after task has completed
   758 	TEST(CountScheduledItemsL(EAllSchedules, TheScheduler) == 0);
   759 
   760 	SchSvrHelpers::Pause(TheTest);
   761 	}
   762 
   763 /**
   764 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1039
   765 @SYMTestCaseDesc	    Tests for persistent scheduling, repeating and non-repeating task schedules
   766 @SYMTestPriority 	    High
   767 @SYMTestActions  	    Persistent schedule,repeating task,non-repeating task,go off,check task's still there,
   768 						go off again,check it's still there,delete task,delete schedule
   769 @SYMTestExpectedResults Test must not fail
   770 @SYMREQ                 REQ0000
   771 */		
   772 static void Test7L()
   773 	{
   774 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1039 Test persistent scheduling, repeating and non-repeating task schedules "));
   775 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
   776 
   777 	// Transient
   778 	TSchedulerItemRef ref;
   779 
   780 	// We shouldn't have any outstanding schedules registered with the server
   781 	TInt count = CountScheduledItemsL(EAllSchedules, TheScheduler);
   782 	TEST(count == 0);
   783 
   784 	// This creates 3 schedule entries, each with a validity period of 20 minutes, and are
   785 	// due to run in 20s, 1m, and over a year ago (a year in the past)
   786 	TheTest.Printf(_L("Create Persistent schedule\n"));
   787 	TInt res = CreateSchedule1L(ref, TheScheduler);
   788 	TEST2(res, KErrNone);
   789 
   790 	// We should now have one registered schedule
   791 	count = CountScheduledItemsL(EAllSchedules, TheScheduler);
   792 	TEST(count == 1);
   793 
   794 	//
   795 	TheTest.Printf(_L("\nSchedule two tasks: one repeating....\n"));
   796 	//
   797 	TInt task1 = 0;
   798 	TName name1 = (_L("web subscription(rpts)"));
   799 	// -1 indicates repeating schedule
   800 	res = SchedulePersistentTaskL(name1, task1, ref.iHandle, -1, TheScheduler); // -1 indicates repeat until explicitly deleted
   801 	TEST2(res, KErrNone);
   802 
   803 	// List those schedules that are pending
   804 	count = CountScheduledItemsL(EPendingSchedules, TheScheduler);
   805 	TEST(count == 1);
   806 
   807 	//
   808 	TheTest.Printf(_L("\n... and one non-repeating\n"));
   809 	//
   810 	TInt task2 = 0;
   811 	TName name2 = (_L("non-repeating"));
   812 	res = SchedulePersistentTaskL(name2, task2, ref.iHandle, 1, TheScheduler); // only runs once
   813 	TEST2(res, KErrNone);
   814 
   815 	TheTest.Printf(_L("Waiting for tasks to run\n"));
   816 	// Wait for notification that schedule has executed.
   817 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
   818 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   819 
   820 	TheTest.Printf(_L("...and wait again for repeating one to execute again\n"));
   821 	// Wait for notification that schedule has executed.
   822 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
   823 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   824 
   825 	TheTest.Printf(_L("Delete the repeating task, and the schedule \n"));
   826 	res = TheScheduler.DeleteTask(task1);
   827 	TEST2(res, KErrNone);
   828 	res = TheScheduler.DeleteTask(task2);
   829 	TEST2(res, KErrNotFound); //Should be not found since its only executed once.
   830 	res = TheScheduler.DeleteSchedule(ref.iHandle);
   831 	TEST2(res, KErrNone);
   832 
   833 	count = CountScheduledItemsL(EPendingSchedules, TheScheduler);
   834 	TEST(count == 0);
   835 	SchSvrHelpers::Pause(TheTest);
   836 	}
   837 
   838 
   839 static CSchEntryInfoArray* CreateSchEntryInfoArrayLC(TInt aGranularity)
   840 	{
   841 	CSchEntryInfoArray*	scheduleEntries	= new (ELeave) CSchEntryInfoArray(aGranularity);
   842 	CleanupStack::PushL(scheduleEntries);
   843 	return scheduleEntries;
   844 	}
   845 
   846 static CArrayFixFlat<TTaskInfo>* CreateTaskInfoLC(TInt aGranularity)
   847 	{
   848 	CArrayFixFlat<TTaskInfo>* taskInfos	= new (ELeave) CArrayFixFlat<TTaskInfo>(aGranularity);
   849 	CleanupStack::PushL(taskInfos);
   850 	return taskInfos;
   851 	}
   852 
   853 static CSchItemRefArray* CreateScheduleRefsLC(TInt aGranularity)
   854 	{
   855 	CSchItemRefArray* scheduleReferences = new (ELeave) CSchItemRefArray(aGranularity);
   856 	CleanupStack::PushL(scheduleReferences);
   857 	return scheduleReferences;
   858 	}
   859 
   860 static void CreateTransientScheduledTasksL(TInt aNumScheduleEntries,
   861 										   TInt aNumSchedules,
   862 										   CSchEntryInfoArray* aScheduleEntries,
   863 										   CArrayFixFlat<TTaskInfo>* aTaskInfos,
   864 										   CSchItemRefArray* aScheduleReferences)
   865 	{
   866 	const TTimeIntervalMicroSeconds timeToAdd = 10000000; //10 seconds
   867 	const TTimeIntervalMicroSeconds timeLimit = 5000000; // 5 seconds
   868 	_LIT(KTaskDataPrefix, "Task Data Entry: ");
   869 	// Prepare keys required
   870 	TKeyArrayFix KTaskInfoSortKey(_FOFF(TTaskInfo, iTaskId), ECmpTInt);
   871 
   872 	for(TInt i=0;i<aNumSchedules;i++)
   873 		{	
   874 		// Remove any existing schedule entry info's
   875 		aScheduleEntries->Reset();
   876 		//
   877 		// Populate the schedule entry array with a varying list of 
   878 		// start-times, intervals, etc for this particular task
   879 		//
   880 		for(TInt j=0; j<aNumScheduleEntries; ++j)
   881 			{
   882 			TScheduleEntryInfo scheduleEntry = SchSvrHelpers::RandomScheduleEntryInfo(TheSeed);
   883 			TTime now;
   884 			now.HomeTime(); // sets now to home time
   885 			//if iStartTime is set lower then 5 sec into the future, postpone iStartTime 10 sec into the future
   886 			if(scheduleEntry.iStartTime < now + timeLimit) 
   887 				scheduleEntry.iStartTime = now + timeToAdd; 
   888 			aScheduleEntries->AppendL(scheduleEntry);
   889 			}
   890 		//
   891 		// Create some dummy task data
   892 		//
   893 		HBufC* taskData = HBufC::NewLC(KTaskDataPrefix().Length()+4);
   894 		taskData->Des().Copy(KTaskDataPrefix());
   895 		taskData->Des().AppendNum(i);
   896 		//
   897 		// Munge the task name
   898 		//
   899 		TTaskInfo taskInfo;
   900 		taskInfo.iName = *taskData;
   901 		taskInfo.iRepeat = 1;
   902 		taskInfo.iPriority = 1;
   903 		// Schedule the transient task
   904 		TSchedulerItemRef scheduleReference;
   905 		TInt result = TheScheduler.ScheduleTask(taskInfo, 
   906 												*taskData, 
   907 												scheduleReference, 
   908 												*aScheduleEntries);
   909 		TEST2(result, KErrNone);
   910 		TheTest.Printf(_L("TaskId: %d => TaskName: %S\n"), taskInfo.iTaskId, &taskInfo.iName);
   911 		CleanupStack::PopAndDestroy(taskData);
   912 		//
   913 		// Save the taskInfo so we can check it later - this inserts the taskinfo into
   914 		// the array (preserves sorted order by TTaskInfo.iTaskId) but also has the
   915 		// added bonus of preventing duplicate task ids...
   916 		//
   917 		aTaskInfos->InsertIsqL(taskInfo, KTaskInfoSortKey);
   918 		// Disable all schedules once added, just to stop them going off
   919 		// and therefore being deleted when we are trying to compare them 
   920 		result = TheScheduler.DisableSchedule(scheduleReference.iHandle);
   921 		TEST2(result, KErrNone);
   922 		// Save the sever generated schedule reference and taskId for checking later
   923 		aScheduleReferences->AppendL(scheduleReference);
   924 		}
   925 	}
   926 
   927 static void CheckScheduledRefs(TInt aNumSchedules)
   928 	{
   929 	CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
   930 	CleanupStack::PushL(refs);
   931 	TInt res = TheScheduler.GetScheduleRefsL(*refs, EAllSchedules); 
   932 	TEST2(res, KErrNone);
   933 	TInt count = refs->Count();
   934 	TEST(count == aNumSchedules);
   935 	CleanupStack::PopAndDestroy(refs);
   936 	}
   937 
   938 /**
   939 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1342
   940 @SYMTestCaseDesc	    Scheduling tasks test
   941 @SYMTestPriority 	    High
   942 @SYMTestActions  	    Tests for RScheduler::GetTaskDataSize(),RScheduler::GetTaskInfoL() functions
   943 @SYMTestExpectedResults Test must not fail
   944 @SYMREQ                 REQ0000
   945 */		
   946 static void TestScheduledTasksL(TInt aNumSchedules,
   947 								CArrayFixFlat<TTaskInfo>* aTaskInfos)
   948 	{
   949 	for(TInt n=0; n<aNumSchedules; ++n)
   950 		{
   951 		const TTaskInfo& taskInfo = aTaskInfos->At(n);
   952 		TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1342 "));
   953 
   954 		// First retrieve the task size
   955 		TInt taskSize;
   956 		TInt result = TheScheduler.GetTaskDataSize(taskInfo.iTaskId, taskSize);
   957 		TEST2(result, KErrNone);
   958 		TEST(taskSize > 0);
   959 
   960 		// Next retrieve the task info associated with a particular task Id
   961 		HBufC* taskData = HBufC::NewLC(taskSize);
   962 		TPtr pTaskData = taskData->Des();
   963 
   964 		TTime scheduleNextDueTime;
   965 		TTaskInfo taskFromServer;
   966 		TSchedulerItemRef scheduleReference;
   967 
   968 		result = TheScheduler.GetTaskInfoL(taskInfo.iTaskId,
   969 										   taskFromServer, 
   970 										   pTaskData, 
   971 										   scheduleReference, 
   972 										   scheduleNextDueTime);
   973 		TEST2(result, KErrNone);
   974 		TEST(taskData->Length() == taskSize);
   975 
   976 		// Now check that the task returned by the server matches that held locallly....
   977 		TBool bbb = SchSvrHelpers::IsTaskInfoTheSame(taskFromServer, taskInfo);
   978 		if(!bbb)
   979 			{
   980 			RDebug::Print(_L("TaskInfo1. repeat=%x, id=%d, name=%S, priority=%x\n"),
   981 				taskFromServer.iRepeat, taskFromServer.iTaskId, &taskFromServer.iName, taskFromServer.iPriority);
   982 			RDebug::Print(_L("TaskInfo2. repeat=%x, id=%d, name=%S, priority=%x\n"),
   983 				taskInfo.iRepeat, taskInfo.iTaskId, &taskInfo.iName, taskInfo.iPriority);
   984 			}
   985 		TEST(bbb);
   986 		
   987 		// Check taskData is the same (was originally held in the TTaskInfo.iName field)
   988 		const TDesC& des1 = taskInfo.iName;
   989 		TDes& des2 = pTaskData;
   990 		TEST(des1 == des2);
   991 		CleanupStack::PopAndDestroy(taskData);
   992 		}
   993 	}
   994 /**
   995 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1040
   996 @SYMTestCaseDesc	    Tests for retrieving of task data and info.
   997 @SYMTestPriority 	    High
   998 @SYMTestActions  	    Create a large number of transient scheduled tasks to test Id generation
   999                         Tests tasks for a given taskid is the same
  1000 						Tests reference can be retrieved for a given handle.
  1001 @SYMTestExpectedResults Test must not fail
  1002 @SYMREQ                 REQ0000
  1003 */		
  1004 static void Test8L()
  1005 	{
  1006 	// Test title
  1007 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1040 Create a large number of tasks, test retrieval of task data and task info "));
  1008 	
  1009 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
  1010 
  1011 	// Constants used in this function
  1012 	const TInt KNumberOfSchedulesToCreate		= 20;
  1013 	const TInt KNumberOfScheduleEntriesToCreate = 5;
  1014 
  1015 	// Prepare arrays required for the tests below
  1016 	CSchEntryInfoArray* scheduleEntries = ::CreateSchEntryInfoArrayLC(KNumberOfScheduleEntriesToCreate);
  1017 	CArrayFixFlat<TTaskInfo>* taskInfos = ::CreateTaskInfoLC(KNumberOfSchedulesToCreate);
  1018 	CSchItemRefArray* scheduleReferences = ::CreateScheduleRefsLC(KNumberOfSchedulesToCreate);
  1019 
  1020 	//
  1021 	// Create a large number of transient scheduled tasks 
  1022 	// to test Id generation
  1023 	//
  1024 	::CreateTransientScheduledTasksL(KNumberOfScheduleEntriesToCreate,
  1025 									 KNumberOfSchedulesToCreate,
  1026 									 scheduleEntries,
  1027 									 taskInfos,
  1028 									 scheduleReferences);
  1029 
  1030 
  1031 	::CheckScheduledRefs(KNumberOfSchedulesToCreate);
  1032 
  1033 	// Test tasks for a given taskid is the same
  1034 	::TestScheduledTasksL(KNumberOfSchedulesToCreate, taskInfos);
  1035 
  1036 	// Test reference can be retrieved for a given handle.
  1037 	CleanupStack::PopAndDestroy(scheduleReferences);
  1038 	CleanupStack::PopAndDestroy(taskInfos);
  1039 	CleanupStack::PopAndDestroy(scheduleEntries);
  1040 
  1041 	// now delete all schedules
  1042 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
  1043 	TInt ccc = CountScheduledItemsL(EAllSchedules, TheScheduler);
  1044 	TEST(ccc == 0);
  1045 
  1046 	SchSvrHelpers::Pause(TheTest);
  1047 	}
  1048 /**
  1049 @SYMTestCaseID          SYSLIB-SCHSVR-CT-3544
  1050 @SYMTestCaseDesc	    This test establishes transient tasks do not persist to disk.
  1051 @SYMTestPriority 	    High
  1052 @SYMTestActions  	    create persistent and transient schedules with tasks. Wait for tasks to execute.
  1053 			    Cause server to shutdown and restart. Check file size against known good size.
  1054 @SYMTestExpectedResults Persistent schedules and tasks are saved to file, but transient ones are not.
  1055 @SYMDEF                 DEF109371
  1056 */		
  1057 static void Test9L()
  1058 	{
  1059 //Clean up before running test
  1060 	SchSvrHelpers::DeleteScheduleFilesL();
  1061 	TheScheduler.Close();
  1062 	
  1063 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3544 Test transient schedules and tasks do not persist after power outages "));
  1064 	
  1065 	TheTest.Next(_L("Connect to Scheduler"));
  1066 	TInt res = TheScheduler.Connect();
  1067 	TEST2(res, KErrNone);
  1068 
  1069 	TheTest.Next(_L("Registering Client"));
  1070 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
  1071 
  1072 
  1073 	//Create a transient schedule with a task set to go off 10 minutes from server time.
  1074 	const TDateTime KTimeToStartTask(2000, EJanuary, 10, 15, 30, 0, 0);
  1075 
  1076 	TSchedulerItemRef schedulerItemReference;
  1077 	CSchEntryInfoArray* entryArray = new(ELeave) CSchEntryInfoArray(1);
  1078 	CleanupStack::PushL(entryArray);
  1079 	
  1080 	HBufC* taskData = _L("This is some transient task data created for testing").AllocL();
  1081 	CleanupStack::PushL(taskData);
  1082 	
  1083 	// Prepare the task info - this describes the tasks that are contained within the task
  1084 	// entry array
  1085 	TTaskInfo taskInfo = SchSvrHelpers::TaskInfo(_L("A transient test task"), 100);
  1086 		
  1087 	// Create a schedule entry and append it to the entry array
  1088 	{
  1089 	TScheduleEntryInfo scheduleEntry = SchSvrHelpers::ScheduleEntryInfo(EDaily, TTime(KTimeToStartTask), 7, 2);
  1090 	entryArray->AppendL(scheduleEntry);
  1091 	}
  1092 
  1093 	// Create the transient task
  1094 	TInt ret = TheScheduler.ScheduleTask(taskInfo, *taskData, schedulerItemReference, *entryArray);
  1095 	TEST2(ret, KErrNone);
  1096 
  1097 	// Check that the task Id after scheduling the event is not 
  1098 	// the same as it was prior to the requesting the service
  1099 	TEST(taskInfo.iTaskId != -1);
  1100 
  1101 
  1102 	// Create a persistent schedule
  1103 	TheTest.Next(_L("Creating Persistent schedule"));
  1104 	//Set server time to known time
  1105 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 10, 5, 0, 0))); // 10:05 am
  1106 
  1107 	TSchedulerItemRef scheduleHandle;
  1108 	TTime time;
  1109 	time.HomeTime();//Get time
  1110 	time += TTimeIntervalSeconds(5); //Task to go off five seconds from now
  1111 	User::LeaveIfError(CreateScheduleL(scheduleHandle, TheScheduler, time)); 
  1112 
  1113 	// Add a repeating task to the persistent schedule
  1114 	TheTest.Next(_L("Creating task for persistent schedule"));
  1115 	TTaskInfo persisttaskInfo;
  1116 	{
  1117 	persisttaskInfo.iName = _L("MyPersistent TaskName");
  1118 	persisttaskInfo.iPriority = 2;
  1119 	persisttaskInfo.iTaskId = 0;
  1120 	persisttaskInfo.iRepeat = 3;
  1121 	HBufC* data = _L("Persistent Task Data").AllocLC();
  1122 	TInt res = TheScheduler.ScheduleTask(persisttaskInfo, *data, scheduleHandle.iHandle);
  1123 	CleanupStack::PopAndDestroy(); // data
  1124 	TEST2(res, KErrNone);
  1125 	}
  1126 	
  1127 	//Now get the filesize of the servers schedule.dat.
  1128 	TFileName templateFileName;
  1129 	_LIT(KFileName,"C:\\private\\10005399\\SchedulesBackup.dat");
  1130 	templateFileName.Copy(KFileName);
  1131 	templateFileName[0] = 'A' + static_cast<TInt>(RFs::GetSystemDrive());
  1132 	RFile file;
  1133 	CleanupClosePushL(file);
  1134 	
  1135 	SchSvrHelpers::Pause(TheTest);
  1136 	//AllFiles capability required to access the private cage of the server
  1137 	TInt ferr = file.Open(TheFsSession, templateFileName, EFileShareReadersOrWriters | EFileRead);
  1138 	TEST2(ferr,KErrNone);
  1139 	TInt filesize1 = 0;
  1140 	//The below const is the measured file size for the creation of the above schedule.
  1141 	//It will change if new data members are added to task or schedule classes
  1142 	const TInt KExpectedFileSize = 374;
  1143 	TInt sErr = file.Size(filesize1);
  1144 	TEST2(sErr,KErrNone);
  1145 	TheTest.Printf(_L("Expected filesize is 374b Filesize is [%db]\n"), filesize1);
  1146 	TEST(filesize1 == KExpectedFileSize);
  1147 
  1148 	CleanupStack::PopAndDestroy(3);
  1149 }
  1150 
  1151 
  1152 //***********************************************************************************
  1153 static TInt RunTestsL()
  1154 	{
  1155 	TheTest.Next(_L("Delete old files"));
  1156 	SchSvrHelpers::DeleteScheduleFilesL();
  1157 	
  1158 	TheTest.Next(_L("Create Task notification semaphore"));
  1159 	//initialise task notification semaphore
  1160 	STaskSemaphore sem;
  1161 	sem.CreateL();
  1162 
  1163 	// Prepare random number seed
  1164 	TheTest.Next(_L("Prepare random number"));
  1165 	TTime now;
  1166 	now.UniversalTime();
  1167 	TheSeed = now.Int64();
  1168 
  1169 	// Connect to the server
  1170 	TheTest.Next(_L("===== Connect to Scheduler ====="));
  1171 	TInt res = TheScheduler.Connect();
  1172 	TEST2(res, KErrNone);
  1173 	
  1174 	// Register a client with the server
  1175 	TheTest.Next(_L("===== Registering Client ====="));
  1176 	res = SchSvrHelpers::RegisterClientL(TheScheduler);
  1177 	TEST2(res, KErrNone);
  1178 
  1179 	TheTest.Next(_L("Start tests"));
  1180 	Test1L();
  1181 	Test2L();
  1182 	Test3L();
  1183 	Test4L();
  1184 	Test5L();
  1185 	Test6L();
  1186 	Test7L();
  1187 	Test8L();
  1188 	Test9L();
  1189 
  1190 	TheTest.Next(_L("Tidying up"));
  1191 	//Tidying up so next test will be clear.
  1192 	TheTest.Next(_L("Delete all schedules"));
  1193 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
  1194 	SchSvrHelpers::Pause(TheTest, 2);
  1195 	TheTest.Next(_L("Delete old files\n"));
  1196 	SchSvrHelpers::DeleteScheduleFilesL();
  1197 
  1198 	TheScheduler.Close();
  1199 	
  1200 	//close handle to semaphore
  1201 	sem.Close();
  1202 
  1203 	return KErrNone;
  1204 	}
  1205 
  1206 //***********************************************************************************
  1207 GLDEF_C TInt E32Main()
  1208 //	
  1209 // TheTest the scheduler
  1210 //
  1211     {
  1212 	__UHEAP_MARK;
  1213 	TheTest.Start(_L("TC_TSCH_SCHEDULING2"));
  1214 	TheTest.Title();
  1215 	TheCleanup = CTrapCleanup::New();
  1216 	//If the previous test fails, SCHSVR.exe may stay in memory.
  1217 	TRAPD(error,CleanupHelpers::TestCleanupL());
  1218 	TEST2(error, KErrNone);
  1219 
  1220 	TEST2(TheFsSession.Connect(), KErrNone);;
  1221 	TRAP(error, RunTestsL());	
  1222 	TRAP(error,CleanupHelpers::TestCleanupL());
  1223 	TEST2(error, KErrNone);
  1224 	delete TheCleanup;
  1225 	
  1226 	TheFsSession.Close();
  1227 	TheTest.End();
  1228 	TheTest.Close();
  1229 	__UHEAP_MARKEND;
  1230 	return(KErrNone);
  1231 	}