os/ossrv/genericservices/taskscheduler/Test/Scheduling/TC_TSCH_SCHEDULING1.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 <e32base.h>
    17 #include <e32test.h>
    18 #include <csch_cli.h>
    19 #include <f32file.h>
    20 #include "Thelpers.h"
    21 
    22 #include "TestUtils.h"
    23 
    24 // Globals
    25 RTest					TheTest(_L("TC_TSCH_SCHEDULING1"));
    26 static RScheduler		TheScheduler;
    27 static RFs				TheFsSession;
    28 
    29 typedef CArrayFixFlat<TScheduleEntryInfo>	CScheduleEntryInfoArray;
    30 typedef CArrayFixFlat<TTaskInfo>			CTaskInfoArray;
    31 typedef CArrayFixFlat<TSchedulerItemRef>    CSchItemRefArray;
    32 
    33 _LIT(KMinimalTaskHandler, "MinimalTaskHandler");
    34 
    35 _LIT(KTimeFormatString,						"%-B%:0%J%:1%T%:2%S%.%*C4%:3%+B");
    36 _LIT(KCurrentDateTimeChanged,				"Date & Time changed to: [%S]\n");
    37 
    38 const TInt KTimeToWait = 4*1000*1000;
    39 
    40 
    41 //***********************************************************************************
    42 
    43 // Sets time to before specified time by aTimeBeforeInSeconds
    44 static void SetTimeBeforeL(RTest& aTest, TTime& aTime, TInt aTimeBeforeInSeconds)
    45 	{
    46 	TTimeIntervalSeconds secs(aTimeBeforeInSeconds);
    47 	TTime time = aTime-secs;
    48 	SchSvrHelpers::SetHomeTimeL(time);
    49 	TBuf<30> dateString;
    50 	time.FormatL(dateString, KTimeFormatString);
    51 	aTest.Printf(KCurrentDateTimeChanged, &dateString);
    52 	}
    53 
    54 // gets the due time for this schedule
    55 static TTime GetDueTimeL(TInt aScheduleId)
    56 	{
    57 	TTime nextTimeScheduleIsDue;
    58 	TScheduleState state;
    59 	CScheduleEntryInfoArray* entries 
    60 		= new (ELeave) CScheduleEntryInfoArray(3);
    61 	CleanupStack::PushL(entries);
    62 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
    63 	CleanupStack::PushL(tasks);
    64 
    65 	TInt res = TheScheduler.GetScheduleL(aScheduleId, state, *entries, *tasks, nextTimeScheduleIsDue);
    66 	TEST2(res, KErrNone);
    67 
    68 	CleanupStack::PopAndDestroy(2); // entries, tasks
    69 	return state.iDueTime;
    70 	}
    71 	
    72 // Forces the task to be exectued aCount times.
    73 static void ForceTaskExecutionForSpecifiedIdL(TInt aId, TInt aCount)
    74 	{
    75 	TheTest.Printf(_L("Executing %d times\n"), aCount);
    76 	TTime time;
    77 	for (TInt i=0; i<aCount; ++i)
    78 		{
    79 		TheTest.Printf(_L("Execution %d\n"), i+1);
    80 		time = GetDueTimeL(aId);	
    81 		
    82 		SetTimeBeforeL(TheTest, time, 5 /*seconds*/);		
    83 		
    84 		// Wait for notification that schedule has executed.
    85 		TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
    86 		CleanupHelpers::KillProcess(KMinimalTaskHandler);
    87 		}
    88 	}
    89 
    90 //creates a daily schedule with StartTime of aStartTime
    91 static TInt CreateScheduleL(TSchedulerItemRef& aRef, 
    92 							RScheduler& aScheduler, 
    93 							const TTime& aStartTime)
    94 	{
    95 	CScheduleEntryInfoArray* entryList 
    96 		= new (ELeave) CScheduleEntryInfoArray(1);
    97 	CleanupStack::PushL(entryList);
    98 
    99 	TScheduleEntryInfo entry1;
   100 	entry1.iStartTime		= aStartTime;
   101 	entry1.iInterval		= 1; // TTimeIntervalDays
   102 	entry1.iIntervalType	= EDaily;
   103 	entry1.iValidityPeriod	= 30; // minutes
   104 	entryList->AppendL(entry1);
   105 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
   106 	CleanupStack::PopAndDestroy(); // entryList
   107 	return res;
   108 	}
   109 
   110 // counts the number of scheduled items based on the supplied filter.
   111 static TInt CountScheduledItemsL(TScheduleFilter aFilter, 
   112 								RScheduler& aScheduler)
   113 	// Extract schedule references from the schedule server based on a filter. If
   114 	{
   115 	CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
   116 	CleanupStack::PushL(refs);
   117 
   118 	TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
   119 	TEST2(res, KErrNone);
   120 
   121 	TInt count = refs->Count();
   122 	CleanupStack::PopAndDestroy(); // refs
   123 	return count;
   124 	}	
   125 
   126 // Extract task references from the schedule server based on a ID
   127 static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
   128 						TInt aScheduleId)
   129 	{
   130 	aTaskInfoArray.Reset();
   131 	TTime nextTimeScheduleIsDue;
   132 	TScheduleState state;
   133 	CScheduleEntryInfoArray* entries 
   134 		= new (ELeave) CScheduleEntryInfoArray(3);
   135 	CleanupStack::PushL(entries);
   136 	TInt res = TheScheduler.GetScheduleL(aScheduleId, 
   137 										state, 
   138 										*entries, 
   139 										aTaskInfoArray, 
   140 										nextTimeScheduleIsDue);
   141 	TEST2(res, KErrNone);
   142 	CleanupStack::PopAndDestroy(entries);
   143 	}
   144 
   145 
   146 
   147 
   148 // schedules a transient task	
   149 static TInt ScheduleTransientTaskL(TInt& aTaskId, 
   150 								TSchedulerItemRef& aRef, 
   151 								TInt aRepeat, 
   152 								RScheduler& aScheduler)
   153 	{
   154 	CScheduleEntryInfoArray* entryList = new(ELeave) CScheduleEntryInfoArray(3);
   155 	CleanupStack::PushL(entryList);
   156 
   157 	aRef.iName = _L("transient one");
   158 
   159 	// SCEHDULES
   160 	TScheduleEntryInfo entry1;
   161 	entry1.iStartTime = SchSvrHelpers::TimeBasedOnOffset(0, 1); // 1 min in the future
   162 	entry1.iInterval = 1;
   163 	entry1.iIntervalType = EDaily;
   164 	entry1.iValidityPeriod = 20;
   165 	entryList->AppendL(entry1);
   166 	
   167 	TScheduleEntryInfo entry2;
   168 	entry2.iStartTime = SchSvrHelpers::TimeBasedOnOffset(0, 2); // 2 mins in the future
   169 	entry2.iInterval = 1;
   170 	entry2.iIntervalType = EDaily;
   171 	entry2.iValidityPeriod = 500;
   172 	entryList->AppendL(entry2);
   173 
   174 	TScheduleEntryInfo entry3;
   175 	entry3.iStartTime = SchSvrHelpers::TimeBasedOnOffset(0, 3); // 3 mins in the future
   176 	entry3.iInterval = 1;
   177 	entry3.iIntervalType = EDaily;
   178 	entry3.iValidityPeriod = 5;
   179 	entryList->AppendL(entry3);
   180 
   181 	// TASK
   182 	TTaskInfo taskInfo;
   183 	taskInfo.iName = _L("mail");
   184 	taskInfo.iTaskId = aTaskId;
   185 	taskInfo.iRepeat = aRepeat;
   186 	taskInfo.iPriority = 2;
   187 	HBufC* data = _L("the data, some strange new name ").AllocLC();
   188 
   189 	// Schedule the item
   190 	TInt res = aScheduler.ScheduleTask(taskInfo, *data, aRef, *entryList);
   191 	CleanupStack::PopAndDestroy(2); // data, entryList
   192 
   193 	aTaskId = taskInfo.iTaskId;
   194 	return res;
   195 	}
   196 
   197 /*
   198  * Add Persistent and long term schedules to SchedulessBackup.dat files. this function 
   199  * is part of the DEF108026 defect testing called by DEF108026() 		
   200  * It is part of the manual test SYSLIB-SCHSVR-CT-4003
   201  * should be commented and will run as manual test only.
   202  * 
   203  * 
   204   
   205  static void DEF108026_AddPersistentSchedulesL( TBool aImmediate )
   206 	{	
   207 	
   208 	_LIT(KTaskData1, "This is some really exciting task data (number 1)");
   209 	_LIT(KTaskData2, "This is some really exciting task data (number 2)");	
   210 	_LIT(KTestName,	"Def108026_test");
   211 
   212 	TheTest.Next(_L("Def108026_test: Adding schedules"));
   213 	
   214 	RDebug::Print(_L("DEF108026_AddPersistentSchedulesL"));
   215 	
   216 	TheTest.Next(_L("Connect to Scheduler"));
   217 	TInt res = TheScheduler.Connect();
   218 	TEST2(res, KErrNone);
   219 
   220 	TheTest.Next(_L("Registering Client"));
   221 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   222 
   223 	
   224 
   225 	TTime startTimeForSchedule;
   226 	if ( aImmediate )
   227 		{
   228 		// This is the time when we want the schedule to fire
   229 		startTimeForSchedule = TTime( (TDateTime(2007, EJune, 20, 10, 00, 0, 0)) ); 
   230 		RDebug::Print( _L("Added immediate schedule"));
   231 		}
   232 	else
   233 		{
   234 		startTimeForSchedule = TTime( (TDateTime(2037, EJune, 20, 10, 00, 0, 0)) ); // These schedules are due in the future.
   235 		RDebug::Print( _L("Added long-term schedule"));	
   236 		}
   237 
   238 
   239 	// Prepare a schedule describing when we want the tasks to run (10:00 am)
   240 	TSchedulerItemRef ref;
   241 	User::LeaveIfError(CreateScheduleL(ref, TheScheduler, startTimeForSchedule));
   242 
   243 	// Disable the schedule whilst we set it up
   244 	User::LeaveIfError(TheScheduler.DisableSchedule(ref.iHandle));
   245 
   246 	// Associate a task with the schedule
   247 	TTaskInfo taskInfo1;
   248 	taskInfo1.iRepeat = 0;
   249 	taskInfo1.iName = KTestName;
   250 	taskInfo1.iPriority = 2;
   251 
   252 	// Create some data associated with this task
   253 	HBufC* taskData1 = KTaskData1().AllocLC();
   254 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo1, *taskData1, ref.iHandle));
   255 	CleanupStack::PopAndDestroy(); // taskData1
   256 
   257 	// Associate a task (2) with the schedule
   258 	TTaskInfo taskInfo2;
   259 	taskInfo2.iRepeat = 0;
   260 	taskInfo2.iName = KTestName;
   261 	taskInfo2.iPriority = 2;
   262 
   263 	// Create some data associated with this task
   264 	HBufC* taskData2 = KTaskData2().AllocLC();
   265 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo2, *taskData2, ref.iHandle));
   266 	CleanupStack::PopAndDestroy(); // taskData2
   267 	
   268 	// We should now have two tasks scheduled at exactly the same time...
   269 	User::LeaveIfError(TheScheduler.EnableSchedule(ref.iHandle));
   270 	
   271 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   272 	}
   273 	
   274 *
   275 ** end of DEF108026_AddPersistentSchedulesL() **/
   276 	
   277 
   278 /**
   279 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1031
   280 @SYMTestCaseDesc	    Test code for defect "Sending SMS to multiple recipients
   281 						sends message to only one recipient" (EDNEMHE-4Q69BG)
   282 @SYMTestPriority 	    High
   283 @SYMTestActions  	    Schedule two tasks to run at same time
   284 @SYMTestExpectedResults Test must not fail
   285 @SYMREQ                 REQ0000
   286 */		
   287 static void Test1L()
   288 	{	
   289 	_LIT(KTaskData1, "This is some really exciting task data (number 1)");
   290 	_LIT(KTaskData2, "This is some really exciting task data (number 2)");
   291 	_LIT(KTestName,	"SmsTest");
   292 
   293 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1031 TheTest3: SMS:Sending to multiple recipients"));
   294 	
   295 	TheTest.Next(_L("Connect to Scheduler"));
   296 	TInt res = TheScheduler.Connect();
   297 	TEST2(res, KErrNone);
   298 
   299 	TheTest.Next(_L("Registering Client"));
   300 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   301 
   302 	// Set the time to a known value, since this makes testing much easier (and more
   303 	// repeatable).	
   304 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
   305 
   306 	// This is the time when we want the schedule to fire
   307 	TTime startTimeForSchedule(TDateTime(2000, EJanuary, 1, 10, 0, 0, 0)); // 10:00 am
   308 
   309 	// Prepare a schedule describing when we want the tasks to run (10:00 am)
   310 	TSchedulerItemRef ref;
   311 	User::LeaveIfError(CreateScheduleL(ref, TheScheduler, startTimeForSchedule));
   312 
   313 	// Disable the schedule whilst we set it up
   314 	User::LeaveIfError(TheScheduler.DisableSchedule(ref.iHandle));
   315 
   316 	// Associate a task with the schedule
   317 	TTaskInfo taskInfo1;
   318 	taskInfo1.iRepeat = 0;
   319 	taskInfo1.iName = KTestName;
   320 	taskInfo1.iPriority = 2;
   321 
   322 	// Create some data associated with this task
   323 	HBufC* taskData1 = KTaskData1().AllocLC();
   324 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo1, *taskData1, ref.iHandle));
   325 	CleanupStack::PopAndDestroy(); // taskData1
   326 
   327 	// Associate a task (2) with the schedule
   328 	TTaskInfo taskInfo2;
   329 	taskInfo2.iRepeat = 0;
   330 	taskInfo2.iName = KTestName;
   331 	taskInfo2.iPriority = 2;
   332 
   333 	// Create some data associated with this task
   334 	HBufC* taskData2 = KTaskData2().AllocLC();
   335 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo2, *taskData2, ref.iHandle));
   336 	CleanupStack::PopAndDestroy(); // taskData2
   337 
   338 	// We should now have two tasks scheduled at exactly the same time...
   339 	User::LeaveIfError(TheScheduler.EnableSchedule(ref.iHandle));
   340 
   341 	// Set the time to 5 minutes *AFTER* the schedule was due to run (10:05am). In this instance,
   342 	// based on the new fixed Schedule Server, the schedule should still execute since 
   343 	// it falls within the validity period (30 mins), however, in the old scheme of things,
   344 	// the schedule would not be valid again until tomorrow (2/1/2000) at 10:00am and hence
   345 	// would not execute until then.	
   346 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 10, 5, 0, 0))); // 10:05 am
   347 
   348 	// Now wait for something to happen...
   349 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
   350 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   351 	}
   352 
   353 /**
   354 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1032
   355 @SYMTestCaseDesc	    Tests for creation of transient schedule with task repeating 5 times
   356 @SYMTestPriority 	    High
   357 @SYMTestActions  	    There should be no schedules as its a transient one and last schedule
   358 						should have deleted by itself.
   359 @SYMTestExpectedResults Test must not fail
   360 @SYMREQ                 REQ0000
   361 */		
   362 static void Test2L()
   363 	{	
   364 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1032 Testing creation of transient schedule with task repeating 5 times "));
   365 
   366 	TInt tTask = 0;
   367 	TSchedulerItemRef ref;
   368 	// schedule has 3 entries - +1min, +2min and +3min
   369 	TInt res = ScheduleTransientTaskL(tTask, ref, 5, TheScheduler);//5 repeats
   370 	TEST2(res, KErrNone);
   371 	
   372 	TheTest.Printf(_L("Get Task count and repeat count\n"));
   373 
   374 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
   375 	CleanupStack::PushL(tasks);
   376 	GetTaskInfoL(*tasks, ref.iHandle);
   377 	TEST(tasks->Count() == 1);
   378 	TTaskInfo info = tasks->At(0);
   379 	TEST(info.iRepeat == 5);
   380 	ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 3);
   381 	GetTaskInfoL(*tasks, ref.iHandle);
   382 	TEST(tasks->Count() == 1);
   383 	info = tasks->At(0);
   384 	TEST(info.iRepeat == 2); // still 2 repeats to go.
   385 	ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 2);
   386 
   387 	CleanupStack::PopAndDestroy(tasks);
   388 	
   389 	TInt scheduleCount = CountScheduledItemsL(EPendingSchedules, TheScheduler);
   390 	// There should be no schedules as its a transient one and last schedule
   391 	// should have deleted itself.
   392 	TEST(scheduleCount == 0); 
   393 	SchSvrHelpers::Pause(TheTest);
   394 	}
   395 
   396 /**
   397 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1341
   398 @SYMTestCaseDesc	    Tests for defect number DEF055586L
   399 @SYMTestPriority 	    High
   400 @SYMTestActions  	    Check to ensure that last element in schedule entries
   401                         array not skipped by schedule checking functions
   402 @SYMTestExpectedResults Test must not fail
   403 @SYMREQ                 REQ0000
   404 */		
   405 static void DEF055586L()
   406 	{	
   407 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1041 DEF055586 - Last element in array missed by loop"));
   408 	
   409 	TheTest.Next(_L("Connect to Scheduler"));
   410 	TInt res = TheScheduler.Connect();
   411 	TEST2(res, KErrNone);
   412 
   413 	TheTest.Next(_L("Registering Client"));
   414 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   415 
   416 	// Set the time to a known value, since this makes testing much easier (and more
   417 	// repeatable).
   418 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
   419 
   420 	// This is the time when we want the schedule to fire
   421 	TTime startTimeForSchedule(TDateTime(2000, EJanuary, 1, 10, 0, 0, 0)); // 10:00 am
   422 
   423 	// Prepare a schedule describing when we want the tasks to run (10:00 am)
   424 	TSchedulerItemRef ref;
   425 
   426 	CScheduleEntryInfoArray* entryList = new (ELeave) CScheduleEntryInfoArray(1);
   427 	CleanupStack::PushL(entryList);
   428 
   429 	TScheduleEntryInfo entry1;
   430 	entry1.iStartTime		= startTimeForSchedule;
   431 	entry1.iInterval		= 0; // TTimeIntervalDays: set to 0 to induce an error
   432 	entry1.iIntervalType	= EDaily;
   433 	entry1.iValidityPeriod	= 30; // minutes
   434 	entryList->AppendL(entry1);
   435 	TInt err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
   436 
   437 	TEST2(err, KErrArgument);
   438 
   439 	TheTest.Next(_L("DEF055586 - Now checking 0 entries in schedule"));
   440 	entryList->Reset();
   441 	
   442 	err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
   443 	
   444 	TEST2(err, KErrArgument);
   445 	
   446 	CleanupStack::PopAndDestroy(); // entryList
   447 	}
   448 	
   449 /**
   450 @SYMTestCaseID          SYSLIB-SCHSVR-CT-3159
   451 @SYMTestCaseDesc	    Tests for defect number DEF094149
   452 @SYMTestPriority 	    Normal
   453 @SYMTestActions  	    Create one Schedule and two task in it, then delete one task
   454 						check whether Task Scheduler execute disabled Schedule 
   455 @SYMTestExpectedResults Test must not fail
   456 @SYMREQ                 REQ0000
   457 */		
   458 void DEF094149L()
   459 	{	
   460 	// Create  scheduling entries
   461 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3159 DEF094149: RF Task Scheduler execute deleted tasks (in stress conditions) "));
   462 	TTime time;
   463 	time.HomeTime();
   464 	time += TTimeIntervalSeconds(2); // Scheduler will run task in 2 seconds
   465 
   466 	TScheduleEntryInfo entry;
   467 	entry.iIntervalType	= EHourly;
   468 	entry.iStartTime		= time;
   469 	entry.iInterval		= 1;
   470 	entry.iValidityPeriod	= 20;
   471 	
   472 	CArrayFixFlat<TScheduleEntryInfo>* entries = new(ELeave) CArrayFixFlat<TScheduleEntryInfo>(10);
   473 	CleanupStack::PushL(entries);
   474 
   475 	entries->AppendL(entry);
   476 	TSchedulerItemRef ref;
   477 	// Create the schedule for the task... and disable it
   478 	TheScheduler.Connect();
   479 	TheScheduler.CreatePersistentSchedule(ref, *entries);
   480 	TheScheduler.DisableSchedule(ref.iHandle);
   481 	
   482 // CreateNewTask
   483 	TTaskInfo task;
   484 	task.iRepeat	= 1; // repeat once
   485 	task.iName		= _L("Test Task For Defect Verification");
   486 	task.iPriority	= 100;
   487 	
   488 	
   489 	TBuf<255>fileName = _L("Some task");
   490 	TheScheduler.Register( fileName, 1 );
   491 	HBufC* taskData = HBufC::NewLC(1);
   492 
   493 	//first task
   494 	TheScheduler.ScheduleTask(task, *taskData,ref.iHandle);
   495 	TInt taskid1;
   496 	taskid1 = task.iTaskId;
   497 	
   498 	//Second task
   499 	TheScheduler.ScheduleTask(task, *taskData,ref.iHandle);
   500 	TInt taskid2;
   501 	taskid2 = task.iTaskId;
   502 
   503 	TInt err = TheScheduler.DeleteTask(taskid1);
   504 	
   505 	User::After(KTimeToWait);
   506 	err = TheScheduler.DeleteTask(taskid2);
   507 	
   508 	//check whether Task Scheduler execute the second task after deleting first tasks 
   509 	//If the Schedule executed then err == KErrNotFound
   510 	TEST2(err, KErrNone);
   511 	CleanupStack::PopAndDestroy(taskData);	
   512 	CleanupStack::PopAndDestroy(entries);	
   513 
   514 	}
   515 
   516 /**
   517 @SYMTestCaseID          SYSLIB-SCHSVR-CT-3358
   518 @SYMTestCaseDesc	    Tests for defect number  INC098909 
   519 @SYMTestPriority 	    High
   520 @SYMTestActions  	    Mark heap of Scheduler then create a schedule & task wait for its 
   521 						execution then check heap again for memory leaks
   522 @SYMTestExpectedResults Test must not fail
   523 @SYMDEF                 INC098909: Process !TaskScheluder leaks memory in mail polls.
   524 */
   525 static void INC098909()
   526 	{
   527 	const TInt KTimeToWait = 5*1000*1000; 
   528 	//SchSvrHelpers::DeleteScheduleFilesL();
   529 	
   530 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3358 INC098909: Process !TaskScheluder leaks memory in mail polls "));
   531 	TheTest.Next(_L("Connect to Scheduler"));
   532 	TEST2(TheScheduler.Connect(),KErrNone);
   533 
   534 	TheTest.Next(_L("Registering Client"));
   535 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   536 	
   537 	User::LeaveIfError(TheScheduler.__DbgMarkHeap());
   538 
   539 	//Create Schedule
   540 	CArrayFixFlat<TScheduleEntryInfo>* entryList = new(ELeave) CArrayFixFlat<TScheduleEntryInfo>(1);
   541 	CleanupStack::PushL(entryList);
   542 	TSchedulerItemRef ref;
   543 	
   544 	TTime time;
   545 	time.HomeTime();
   546 	time += TTimeIntervalSeconds(0); // Scheduler will run task in  5 sec
   547 
   548 	TScheduleEntryInfo entry1;
   549 	entry1.iIntervalType	= EHourly;
   550 	entry1.iStartTime		= time;
   551 	entry1.iInterval		= 1;
   552 	entry1.iValidityPeriod	= 20;
   553 	entryList->AppendL(entry1);
   554 	
   555 	// Create the schedule for the task...
   556 	TEST2(TheScheduler.CreatePersistentSchedule(ref, *entryList),KErrNone);
   557 	
   558 	//Create Task
   559 	TTaskInfo task;
   560 	task.iRepeat	= 1; // repeat once
   561 	task.iName		= _L("Test ");
   562 	task.iPriority	= 100;
   563 	
   564 	HBufC* taskData = HBufC::NewLC(1);
   565 	TEST2(TheScheduler.ScheduleTask(task, *taskData,ref.iHandle), KErrNone);
   566 	
   567 	CleanupStack::PopAndDestroy(taskData);
   568 	CleanupStack::PopAndDestroy(entryList);	
   569 	
   570 	//Wait schedule to complete
   571 	User::After(KTimeToWait);
   572 
   573 	User::LeaveIfError(TheScheduler.__DbgMarkEnd(0));
   574 
   575 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
   576 	
   577 	// really clean out the scheduler (get rid of all the files and process)
   578 	TheScheduler.Close();
   579 	SchSvrHelpers::DeleteScheduleFilesL();
   580 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   581 	// Now wait for something to happen...
   582 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
   583 	}
   584 
   585 
   586 	
   587 /**
   588 @SYMTestCaseID          SYSLIB-SCHSVR-CT-4003
   589 @SYMTestCaseDesc	    Test for DEF108026:
   590 						The aim of this test is to create enough persistent schedules. This reproduce the defect, when 
   591 						SysStart startup. task scheduler will see that these schedules are due. It will then add them 
   592 						immediately to the timer during SSA phase. As this operation consumes a long time and CSchStartupStateMgr::RunL() 
   593 						is blocking, the domain manager fails to be ackowleged about the state transition, therefore certain processes like 
   594 						Esock workers does not complete.  These schedules are due immediately (at the time we run this test), we need then to 
   595 						set the time of the device at the time where the schedules are due. We set the home time of the device in CSchServer::ConstructL(). 
   596 						I add in comment the modified CSchServer::ConstructL().
   597 						ssch_svr.dll must be rebuilt.
   598 						I could reproduces this defect only on Uiq. I could not reproduce it on techview!
   599 @SYMTestPriority 	    Normal
   600 @SYMTestActions  	    Create persistent and long-term schedules.
   601 @SYMTestExpectedResults Test must create a 
   602 @SYMDEF                 DEF108026:SCHSVR performs SSA-driven actions before acknowledging transition.
   603 */
   604 /*********************** constructL() modified.Needed to explain the manual test DEF108026() ********************************
   605 **
   606 **
   607 void CSchServer::ConstructL()
   608 	{
   609 #ifdef __SCHLOGGING__
   610 	iTheLog = CSheduleServerLog::NewL(_L("SchSvr"));
   611 	Dll::SetTls(iTheLog);
   612 #endif
   613 
   614  	// Create server storage path
   615  	RFs fs;
   616  	User::LeaveIfError(fs.Connect());
   617 
   618 #ifdef __SECURE_DATA__
   619 	TInt err = fs.CreatePrivatePath(EDriveC);
   620 #else
   621 	_LIT(KDirPath, "C:\\System\\Schedules\\");
   622 	TInt err = fs.MkDirAll(KDirPath);
   623 #endif
   624 
   625 	if(err != KErrNone && err != KErrAlreadyExists)
   626 		User::Leave(err);
   627 	
   628 	fs.Close();
   629 	
   630 	
   631 	
   632 	
   633 	// The process that we need to add to set the time of the device.
   634 	// We need to set it to the same time that the schedules added are due.
   635 TTime newTime ( (TDateTime(2007, EJune, 20, 10, 00, 00, 00)) ); // 10:00 am
   636 	{
   637 
   638 	_LIT(KSetHomeTime, "TSetHomeTime");
   639 	_LIT(KTimeFormat, "%F%Y%M%D:%H%T%S.%*C6");
   640 	
   641 	RProcess p;	
   642 	TRequestStatus stat;
   643 	TBuf<128> bufLocalTime;	
   644 	
   645 	newTime.FormatL(bufLocalTime, KTimeFormat);
   646 	
   647 	User::LeaveIfError(p.Create(KSetHomeTime, bufLocalTime));
   648 	
   649 	// Asynchronous logon: completes when process terminates with process exit code
   650 	p.Logon(stat);
   651 	p.Resume();
   652 
   653 	User::WaitForRequest(stat);
   654 	TInt exitReason = p.ExitReason();
   655 	p.Close();
   656    //return (exitReason);
   657 	}	
   658 	
   659 	
   660 	
   661 	
   662 	iTaskScheduler = CTaskScheduler::NewL();
   663 
   664 	iSSAMgr = new(ELeave) CSchStartupStateMgr(KDmHierarchyIdStartup, KBaseServicesDomain3);
   665 	iSSAMgr->RegisterObserverL(iTaskScheduler);
   666 	iSSAMgr->InitialiseL();
   667 
   668 	StartL(KSchSvrName);
   669 	}
   670 **
   671 ** end of the changes we need to do in ssch_svr.cpp **/
   672 
   673 /*
   674  * This should be called by DoTheTestsL () . It is part of the manual test SYSLIB-SCHSVR-CT-4003
   675  * should be commented and will run as manual test only.
   676  *
   677  * 
   678 static void DEF108026()
   679 	{
   680 	TheTest.Next(_L("DEF108026: Creating enough persistent and long-term schedules to make task scheduler slower while adding them to the timer"));
   681 	
   682 	for ( TInt i = 0; i < 500; i++ )
   683 		{
   684 		DEF108026_AddPersistentSchedulesL( ETrue );
   685 		HBufC* debugTxt = HBufC::NewLC( 256 );
   686 		debugTxt->Des().Append( _L( "Added Test... " ) );
   687 		debugTxt->Des().AppendNum( i++ );				
   688 		RDebug::Print( *debugTxt );	
   689 		CleanupStack::PopAndDestroy( debugTxt );		
   690 		}
   691 	
   692 	
   693 	for ( TInt i = 0; i < 1000; i++ )
   694 		{
   695 		DEF108026_AddPersistentSchedulesL( EFalse );
   696 		HBufC* debugTxt = HBufC::NewLC( 256 );
   697 		debugTxt->Des().Append( _L( "Added Test... " ) );
   698 		debugTxt->Des().AppendNum( i++ );				
   699 		RDebug::Print( *debugTxt );	
   700 		CleanupStack::PopAndDestroy( debugTxt );		
   701 		}			
   702 		
   703 	}
   704 ** end of DEF108026 () **/
   705 
   706 GLDEF_C TInt DoTheTestsL()
   707 	{
   708 	
   709 	//Delete old files.
   710 	SchSvrHelpers::DeleteScheduleFilesL();
   711 	
   712 	STaskSemaphore sem;
   713 	sem.CreateL();
   714 	
   715 	TheTest.Next(_L("Start tests"));
   716 	// Add tests here:-
   717 	
   718 	//DEF108026(); //This is a manual test. This test runs only under certain condition,
   719 				 //Read the condition required to run this test in the implementation of the DEF108026() comments.
   720 	
   721 	INC098909();	// Should be commented while running def108026
   722 	DEF094149L();   // Should be commented while running def108026
   723 	Test1L();		//Should be commented while running def108026
   724 	Test2L();		//Should be commented while running def108026
   725 	DEF055586L();	//Should be commented while running def108026
   726 	
   727 	sem.Close();
   728 	
   729 	//Tidying up so next test will be clear.
   730 	TheTest.Next(_L("Delete all schedules"));
   731 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler); // While running def108026() manual test, this should be commented. We
   732 														// want keep the persistent schedules.
   733 	SchSvrHelpers::Pause(TheTest, 2);
   734 	TheTest.Next(_L("Delete old files\n"));
   735 	SchSvrHelpers::DeleteScheduleFilesL(); // While running def108026() manual test, this should be commented. We
   736 											// want keep the persistent schedules.
   737 	TheScheduler.Close();
   738 
   739 	return KErrNone;
   740 	}
   741 
   742 
   743 //***********************************************************************************
   744 GLDEF_C TInt E32Main()
   745     {
   746 	__UHEAP_MARK;
   747 	TheTest.Title();
   748 	TheTest.Start(_L("TC_TSCH_SCHEDULING1"));
   749 
   750 	TInt error = KErrNone;
   751 	CTrapCleanup* cleanup = CTrapCleanup::New();
   752 	if	(!cleanup)
   753 		return KErrNoMemory;
   754 	//If the previous test fails, SCHSVR.exe may stay in memory.
   755 	TRAP(error,CleanupHelpers::TestCleanupL());
   756 	TEST2(error, KErrNone);
   757 
   758 	TEST2(TheFsSession.Connect(), KErrNone);
   759 	TheTest.Next(_L("Do the tests"));
   760 	TRAP(error, DoTheTestsL());
   761 	TEST2(error,KErrNone); // While running DEF108026 this line should be commented. If not, error = -4 (no memory) and this leaves. 
   762 						   //The reason is that our generated schedules take a lot of space. But this is the aim of this manual test:
   763 						   // The SchedulesBackup.dat must be bigger as possible to reproduce the defect
   764 	TheFsSession.Close();
   765 	TRAP(error,CleanupHelpers::TestCleanupL());
   766 	TEST2(error, KErrNone);
   767 	delete cleanup;	
   768 
   769 	TheTest.End();
   770 	TheTest.Close();
   771 	__UHEAP_MARKEND;
   772 	return KErrNone;
   773 	}