sl@0: // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include <e32base.h>
sl@0: #include <e32test.h>
sl@0: #include <csch_cli.h>
sl@0: #include <f32file.h>
sl@0: #include "Thelpers.h"
sl@0: 
sl@0: #include "TestUtils.h"
sl@0: 
sl@0: // Globals
sl@0: RTest					TheTest(_L("TC_TSCH_SCHEDULING1 - UTC"));
sl@0: static RScheduler		TheScheduler;
sl@0: static RFs				TheFsSession;
sl@0: 
sl@0: typedef CArrayFixFlat<TScheduleEntryInfo2>	CScheduleEntryInfoArray;
sl@0: typedef CArrayFixFlat<TTaskInfo>			CTaskInfoArray;
sl@0: typedef CArrayFixFlat<TSchedulerItemRef>    CSchItemRefArray;
sl@0: 
sl@0: _LIT(KMinimalTaskHandler, "MinimalTaskHandler");
sl@0: 
sl@0: _LIT(KTimeFormatString,						"%-B%:0%J%:1%T%:2%S%.%*C4%:3%+B");
sl@0: _LIT(KCurrentDateTimeChanged,				"Date & Time changed to: [%S]\n");
sl@0: 
sl@0: //***********************************************************************************
sl@0: 
sl@0: // Sets time to before specified time by aTimeBeforeInSeconds
sl@0: static void SetTimeBeforeL(RTest& aTest, TTsTime& aTime, TInt aTimeBeforeInSeconds)
sl@0: 	{
sl@0: 	TTimeIntervalSeconds secs(aTimeBeforeInSeconds);
sl@0: 	TTime time = aTime.GetLocalTime()-secs;
sl@0: 	SchSvrHelpers::SetHomeTimeL(time);
sl@0: 	TBuf<30> dateString;
sl@0: 	time.FormatL(dateString, KTimeFormatString);
sl@0: 	aTest.Printf(KCurrentDateTimeChanged, &dateString);
sl@0: 	}
sl@0: 
sl@0: // gets the due time for this schedule
sl@0: static TTsTime GetDueTimeL(TInt aScheduleId)
sl@0: 	{
sl@0: 	TTsTime nextTimeScheduleIsDue;
sl@0: 	TScheduleState2 state;
sl@0: 	CScheduleEntryInfoArray* entries 
sl@0: 		= new (ELeave) CScheduleEntryInfoArray(3);
sl@0: 	CleanupStack::PushL(entries);
sl@0: 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
sl@0: 	CleanupStack::PushL(tasks);
sl@0: 
sl@0: 	TInt res = TheScheduler.GetScheduleL(aScheduleId, state, *entries, *tasks, nextTimeScheduleIsDue);
sl@0: 	TEST2(res, KErrNone);
sl@0: 
sl@0: 	CleanupStack::PopAndDestroy(2); // entries, tasks
sl@0: 	
sl@0: 	return state.DueTime();
sl@0: 	}
sl@0: 	
sl@0: // Forces the task to be exectued aCount times.
sl@0: static void ForceTaskExecutionForSpecifiedIdL(TInt aId, TInt aCount)
sl@0: 	{
sl@0: 	TheTest.Printf(_L("Executing %d times\n"), aCount);
sl@0: 	TTsTime time;
sl@0: 	for (TInt i=0; i<aCount; ++i)
sl@0: 		{
sl@0: 		TheTest.Printf(_L("Execution %d\n"), i+1);
sl@0: 		time = GetDueTimeL(aId);		
sl@0: 		
sl@0: 		SetTimeBeforeL(TheTest, time, 5 /*seconds*/);		
sl@0: 		
sl@0: 		// Wait for notification that schedule has executed.
sl@0: 		TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
sl@0: 		CleanupHelpers::KillProcess(KMinimalTaskHandler);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: //creates a daily schedule with StartTime of aStartTime
sl@0: static TInt CreateScheduleL(TSchedulerItemRef& aRef, 
sl@0: 							RScheduler& aScheduler, 
sl@0: 							const TTsTime& aStartTime)
sl@0: 	{
sl@0: 	CScheduleEntryInfoArray* entryList 
sl@0: 		= new (ELeave) CScheduleEntryInfoArray(1);
sl@0: 	CleanupStack::PushL(entryList);
sl@0: 
sl@0: 	TScheduleEntryInfo2 entry1 (aStartTime, EDaily, 1, 30);
sl@0: 	entryList->AppendL(entry1);
sl@0: 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
sl@0: 	CleanupStack::PopAndDestroy(); // entryList
sl@0: 	return res;
sl@0: 	}
sl@0: 
sl@0: // counts the number of scheduled items based on the supplied filter.
sl@0: static TInt CountScheduledItemsL(TScheduleFilter aFilter, 
sl@0: 								RScheduler& aScheduler)
sl@0: 	// Extract schedule references from the schedule server based on a filter. If
sl@0: 	{
sl@0: 	CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
sl@0: 	CleanupStack::PushL(refs);
sl@0: 
sl@0: 	TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
sl@0: 	TEST2(res, KErrNone);
sl@0: 
sl@0: 	TInt count = refs->Count();
sl@0: 	CleanupStack::PopAndDestroy(); // refs
sl@0: 	return count;
sl@0: 	}	
sl@0: 
sl@0: // Extract task references from the schedule server based on a ID
sl@0: static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
sl@0: 						TInt aScheduleId)
sl@0: 	{
sl@0: 	aTaskInfoArray.Reset();
sl@0: 	TTsTime nextTimeScheduleIsDue;
sl@0: 	TScheduleState2 state;
sl@0: 	CScheduleEntryInfoArray* entries 
sl@0: 		= new (ELeave) CScheduleEntryInfoArray(3);
sl@0: 	CleanupStack::PushL(entries);
sl@0: 	TInt res = TheScheduler.GetScheduleL(aScheduleId, 
sl@0: 										state, 
sl@0: 										*entries, 
sl@0: 										aTaskInfoArray, 
sl@0: 										nextTimeScheduleIsDue);
sl@0: 	TEST2(res, KErrNone);
sl@0: 	CleanupStack::PopAndDestroy(entries);
sl@0: 	}
sl@0: 
sl@0: // schedules a transient task	
sl@0: static TInt ScheduleTransientTaskL(TInt& aTaskId, 
sl@0: 								TSchedulerItemRef& aRef, 
sl@0: 								TInt aRepeat, 
sl@0: 								RScheduler& aScheduler)
sl@0: 	{
sl@0: 	CScheduleEntryInfoArray* entryList = new(ELeave) CScheduleEntryInfoArray(3);
sl@0: 	CleanupStack::PushL(entryList);
sl@0: 
sl@0: 	aRef.iName = _L("transient one");
sl@0: 
sl@0: 	// SCHEDULES
sl@0: 	TTime ttime1(SchSvrHelpers::UtcTimeBasedOnOffset(0, 1));
sl@0: 	TTsTime startTime1 (ttime1,ETrue); // 1 min in the future
sl@0: 	TScheduleEntryInfo2 entry1 (startTime1, EDaily, 1, 20);
sl@0: 	entryList->AppendL(entry1);
sl@0: 	TTime ttime2(SchSvrHelpers::UtcTimeBasedOnOffset(0, 2));
sl@0: 	TTsTime startTime2 (ttime2,ETrue); // 2 min in the future
sl@0: 	TScheduleEntryInfo2 entry2 (startTime2, EDaily, 1, 500);
sl@0: 	entryList->AppendL(entry2);
sl@0: 
sl@0: 	TTime ttime3(SchSvrHelpers::UtcTimeBasedOnOffset(0, 3));
sl@0: 	TTsTime startTime3 (ttime3,ETrue); // 3 min in the future
sl@0: 	TScheduleEntryInfo2 entry3 (startTime3, EDaily, 1, 5);
sl@0: 	entryList->AppendL(entry3);
sl@0: 
sl@0: 	// TASK
sl@0: 	TTaskInfo taskInfo;
sl@0: 	taskInfo.iName = _L("mail");
sl@0: 	taskInfo.iTaskId = aTaskId;
sl@0: 	taskInfo.iRepeat = aRepeat;
sl@0: 	taskInfo.iPriority = 2;
sl@0: 	HBufC* data = _L("the data, some strange new name ").AllocLC();
sl@0: 
sl@0: 	// Schedule the item
sl@0: 	TInt res = aScheduler.ScheduleTask(taskInfo, *data, aRef, *entryList);
sl@0: 	CleanupStack::PopAndDestroy(2); // data, entryList
sl@0: 
sl@0: 	aTaskId = taskInfo.iTaskId;
sl@0: 	return res;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: //***********************************************************************************
sl@0: 
sl@0: /**
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CT-0254
sl@0: @SYMTestCaseDesc 			Replicated test for for defect (EDNEMHE-4Q69BG) - UTC
sl@0: @SYMTestPriority 			High
sl@0: @SYMTestActions  			Create time based schedules and then jump to a time after the due time but within the validity period and check it schedule still fires
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */
sl@0: static void Test1L()
sl@0: 	{
sl@0: 	_LIT(KTaskData1, "This is some really exciting task data (number 1)");
sl@0: 	_LIT(KTaskData2, "This is some really exciting task data (number 2)");
sl@0: 	_LIT(KTestName,	"SmsTest");
sl@0: 
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0254 TheTest3: SMS:Sending to multiple recipients "));
sl@0: 	
sl@0: 	TheTest.Next(_L("Connect to Scheduler"));
sl@0: 	TInt res = TheScheduler.Connect();
sl@0: 	TEST2(res, KErrNone);
sl@0: 
sl@0: 	TheTest.Next(_L("Registering Client"));
sl@0: 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
sl@0: 
sl@0: 	// Set the time to a known value, since this makes testing much easier (and more
sl@0: 	// repeatable).	
sl@0: 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
sl@0: 
sl@0: 	// This is the time when we want the schedule to fire
sl@0: 	TDateTime datetime(2000, EJanuary, 1, 10, 0, 0, 0);
sl@0: 	TTsTime startTimeForSchedule(datetime, ETrue); // 10:00 am
sl@0: 
sl@0: 
sl@0: 	// Prepare a schedule describing when we want the tasks to run (10:00 am)
sl@0: 	TSchedulerItemRef ref;
sl@0: 	User::LeaveIfError(CreateScheduleL(ref, TheScheduler, startTimeForSchedule));
sl@0: 
sl@0: 	// Disable the schedule whilst we set it up
sl@0: 	User::LeaveIfError(TheScheduler.DisableSchedule(ref.iHandle));
sl@0: 
sl@0: 	// Associate a task with the schedule
sl@0: 	TTaskInfo taskInfo1;
sl@0: 	taskInfo1.iRepeat = 0;
sl@0: 	taskInfo1.iName = KTestName;
sl@0: 	taskInfo1.iPriority = 2;
sl@0: 
sl@0: 	// Create some data associated with this task
sl@0: 	HBufC* taskData1 = KTaskData1().AllocLC();
sl@0: 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo1, *taskData1, ref.iHandle));
sl@0: 	CleanupStack::PopAndDestroy(); // taskData1
sl@0: 
sl@0: 	// Associate a task (2) with the schedule
sl@0: 	TTaskInfo taskInfo2;
sl@0: 	taskInfo2.iRepeat = 0;
sl@0: 	taskInfo2.iName = KTestName;
sl@0: 	taskInfo2.iPriority = 2;
sl@0: 
sl@0: 	// Create some data associated with this task
sl@0: 	HBufC* taskData2 = KTaskData2().AllocLC();
sl@0: 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo2, *taskData2, ref.iHandle));
sl@0: 	CleanupStack::PopAndDestroy(); // taskData2
sl@0: 
sl@0: 	// We should now have two tasks scheduled at exactly the same time...
sl@0: 	User::LeaveIfError(TheScheduler.EnableSchedule(ref.iHandle));
sl@0: 
sl@0: 	// Set the time to 5 minutes *AFTER* the schedule was due to run (10:05am). In this instance,
sl@0: 	// based on the new fixed Schedule Server, the schedule should still execute since 
sl@0: 	// it falls within the validity period (30 mins), however, in the old scheme of things,
sl@0: 	// the schedule would not be valid again until tomorrow (2/1/2000) at 10:00am and hence
sl@0: 	// would not execute until then.	
sl@0: 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 10, 5, 0, 0))); // 10:05 am
sl@0: 
sl@0: 	// Now wait for something to happen...
sl@0: 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
sl@0: 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
sl@0: 	}
sl@0: 
sl@0: //***********************************************************************************
sl@0: 
sl@0: /**
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CT-0255
sl@0: @SYMTestCaseDesc 			Replicated test for for defect (EDNEMHE-4Q69BG) - UTC
sl@0: @SYMTestPriority 			High
sl@0: @SYMTestActions  			Create time based schedules and then jump to a time after the due time but within the validity period and check it schedule still fires
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */
sl@0: static void Test2L()
sl@0: 	{
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0255 Testing creation of transient schedule with task repeating 5 times "));	
sl@0: 	TInt tTask = 0;
sl@0: 	TSchedulerItemRef ref;
sl@0: 	// schedule has 3 entries - +1min, +2min and +3min
sl@0: 	TInt res = ScheduleTransientTaskL(tTask, ref, 5, TheScheduler);//5 repeats
sl@0: 	TEST2(res, KErrNone);
sl@0: 	
sl@0: 	TheTest.Printf(_L("Get Task count and repeat count\n"));
sl@0: 
sl@0: 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
sl@0: 	CleanupStack::PushL(tasks);
sl@0: 	GetTaskInfoL(*tasks, ref.iHandle);
sl@0: 	TEST(tasks->Count() == 1);
sl@0: 	TTaskInfo info = tasks->At(0);
sl@0: 	TEST(info.iRepeat == 5);
sl@0: 	ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 3);
sl@0: 	GetTaskInfoL(*tasks, ref.iHandle);
sl@0: 	TEST(tasks->Count() == 1);
sl@0: 	info = tasks->At(0);
sl@0: 	TEST(info.iRepeat == 2); // still 2 repeats to go.
sl@0: 	ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 2);
sl@0: 
sl@0: 	CleanupStack::PopAndDestroy(tasks);
sl@0: 	
sl@0: 	TInt scheduleCount = CountScheduledItemsL(EPendingSchedules, TheScheduler);
sl@0: 	// There should be no schedules as its a transient one and last schedule
sl@0: 	// should have deleted itself.
sl@0: 	TEST(scheduleCount == 0); 
sl@0: 	SchSvrHelpers::Pause(TheTest);
sl@0: 	}
sl@0: 
sl@0: //***********************************************************************************
sl@0: 
sl@0: /**
sl@0: @file
sl@0: @SYMTestCaseID				SYSLIB-SCHSVR-CT-0256
sl@0: @SYMTestCaseDesc 			Replicated test for for defect (DEF055586L) - UTC
sl@0: @SYMTestPriority 			High
sl@0: @SYMTestActions  			Create a time-based schedule check that there are 0 entries in the scehule after it fires
sl@0: @SYMTestExpectedResults		The test must not fail.
sl@0: @SYMPREQ					PREQ234
sl@0: */
sl@0: static void DEF055586L()
sl@0: 	{
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0256 DEF055586 - Last element in array missed by loop "));
sl@0: 	TheTest.Next(_L("Connect to Scheduler"));
sl@0: 	TInt res = TheScheduler.Connect();
sl@0: 	TEST2(res, KErrNone);
sl@0: 
sl@0: 	TheTest.Next(_L("Registering Client"));
sl@0: 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
sl@0: 
sl@0: 	// Set the time to a known value, since this makes testing much easier (and more
sl@0: 	// repeatable).	
sl@0: 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
sl@0: 
sl@0: 	// This is the time when we want the schedule to fire
sl@0: 	TDateTime datetime(2000, EJanuary, 1, 10, 0, 0, 0);
sl@0: 	TTsTime startTimeForSchedule(datetime, ETrue); // 10:00 am
sl@0: 
sl@0: 	// Prepare a schedule describing when we want the tasks to run (10:00 am)
sl@0: 	TSchedulerItemRef ref;
sl@0: 
sl@0: 	CScheduleEntryInfoArray* entryList = new (ELeave) CScheduleEntryInfoArray(1);
sl@0: 	CleanupStack::PushL(entryList);
sl@0: 
sl@0: 	TScheduleEntryInfo2 entry1 (startTimeForSchedule, EDaily, 0, 30); // TTimeIntervalDays: set to 0 to induce an error
sl@0: 	entryList->AppendL(entry1);
sl@0: 	TInt err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
sl@0: 
sl@0: 	TEST2(err, KErrArgument);
sl@0: 
sl@0: 	TheTest.Next(_L("DEF055586 - Now checking 0 entries in schedule"));
sl@0: 	entryList->Reset();
sl@0: 	
sl@0: 	err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
sl@0: 	
sl@0: 	TEST2(err, KErrArgument);
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(); // entryList
sl@0: 	}
sl@0: 	
sl@0: //DEF061595  Schedule timers incorrectly expire when system time is changed 	
sl@0: //The test will create and submit one schedule which execution time is one year later.
sl@0: //Then the test will change the system time to be current time + 1 day.
sl@0: //Although the schedule time is set to be current time + 1 year, the schedule task(s) will
sl@0: //be executed immediately because the schedule timer expires when the system time changes.
sl@0: void DEF061595L()
sl@0: 	{
sl@0: 	TheTest.Next(_L("DEF061595  Schedule timers incorrectly expire when system time is changed"));
sl@0: 	//Get current time in currTime variable
sl@0: 	TTime currTime;		
sl@0: 	currTime.HomeTime();
sl@0: 	//Prepare the task time (in taskTime variable) to be currTime + 1 year.
sl@0: 	TTime taskTime(currTime + TTimeIntervalYears(1));		
sl@0: 	TInt taskYear = taskTime.DateTime().Year();
sl@0: 	//Connect to the Task Scheduler Server	
sl@0: 	RScheduler	scheduler;
sl@0: 	CleanupClosePushL(scheduler);
sl@0: 	TInt res = scheduler.Connect();
sl@0: 	TEST2(res, KErrNone);
sl@0: 	TEST2(SchSvrHelpers::RegisterClientL(scheduler), KErrNone);
sl@0: 	//Create new schedule. The new schedule task will run 1 year later.
sl@0: 	TTsTime taskTsTime(taskTime, EFalse);
sl@0: 	TSchedulerItemRef ref;
sl@0: 	TEST2(::CreateScheduleL(ref, scheduler, taskTsTime), KErrNone);
sl@0: 	// Disable the schedule whilst we set it up
sl@0: 	TEST2(scheduler.DisableSchedule(ref.iHandle), KErrNone);
sl@0: 	// Associate a task with the schedule
sl@0: 	_LIT(KTaskInfo, "DEF061595");
sl@0: 	TTaskInfo taskInfo;
sl@0: 	taskInfo.iRepeat = 0;
sl@0: 	taskInfo.iName = KTaskInfo;
sl@0: 	taskInfo.iPriority = 2;
sl@0: 	const TInt KTaskDataLen = 1;
sl@0: 	HBufC* taskData = HBufC::NewLC(KTaskDataLen);
sl@0: 	TEST2(scheduler.ScheduleTask(taskInfo, *taskData, ref.iHandle), KErrNone);
sl@0: 	//We should now have one tasks scheduled to be executed after 1 year.
sl@0: 	//Enable schedule.
sl@0: 	TEST2(scheduler.EnableSchedule(ref.iHandle), KErrNone);
sl@0: 	//Get the scheduled task info. The task year should be the same as it was set in the schedule.
sl@0: 	TTime nextTime;	
sl@0: 	TPtr pTaskData = taskData->Des();
sl@0: 	TEST2(scheduler.GetTaskInfoL(taskInfo.iTaskId, taskInfo, pTaskData, ref, nextTime), KErrNone);
sl@0: 	TInt nextTaskYear = nextTime.DateTime().Year();	
sl@0: 	TEST(nextTaskYear == taskYear);
sl@0: 	//Change the system time to be current time + 1 day	
sl@0: 	TEST2(SchSvrHelpers::SetUTCTimeL(currTime + TTimeIntervalDays(1)), KErrNone);
sl@0: 	//Get the scheduled task info again. If the defect is not fixed, the call will fail
sl@0: 	//with KErrNotFound (because SchSvrHelpers::SetUTCTimeL() call will make the schedule timer to expire)
sl@0: 	TInt err = scheduler.GetTaskInfoL(taskInfo.iTaskId, taskInfo, pTaskData, ref, nextTime);
sl@0: //TODO	TEST2(err, KErrNone);
sl@0: //TODO	nextTaskYear = nextTime.DateTime().Year();
sl@0: //TODO	TEST(nextTaskYear == taskYear);
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(taskData);
sl@0: 	CleanupStack::PopAndDestroy(&scheduler);
sl@0: 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-SCHSVR-CT-1655
sl@0: @SYMTestCaseDesc	    Tests for defect number DEF079983
sl@0: @SYMTestPriority 	    High
sl@0: @SYMTestActions  	    Check to ensure all task files have been cleaned up
sl@0: 						after scheduled tasks completed
sl@0: @SYMTestExpectedResults Test must not fail
sl@0: @SYMDEF 				DEF079983
sl@0: */		
sl@0: static void DEF079983L()
sl@0: 	{
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1655 DEF079983: Private directory of schsvr flooded with files "));
sl@0: 	// A dummy scheduled task:
sl@0: 	// Create some transient schedule task and repeat 5 times
sl@0: 	TheTest.Printf(_L("DEF079983: Create a dummy scheduled task"));
sl@0: 	TInt tTask = 0;
sl@0: 	TSchedulerItemRef ref;
sl@0: 	// 5 repeats
sl@0: 	TInt res = ScheduleTransientTaskL(tTask, ref, 5, TheScheduler);
sl@0: 	TEST2(res, KErrNone);
sl@0: 	ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 5);
sl@0: 	
sl@0: 	// Check for left task files after scheduled tasks completed
sl@0: 	// To access private data cage, uses SchSvrHelplers::CheckTaskFilesL()
sl@0: 	TheTest.Next(_L("DEF079983: Now checking no files left when tasks completed"));
sl@0: 	TInt err = SchSvrHelpers::CheckTaskFilesL();
sl@0: 	// If there's any task files left, test fails with error code KErrGeneral
sl@0: 	TEST(err == KErrNone);
sl@0: 	}		
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-SCHSVR-CT-3362
sl@0: @SYMTestCaseDesc	    Replicated test for for defect (INC098909) - UTC   
sl@0: @SYMTestPriority 	    High
sl@0: @SYMTestActions  	    Mark heap of Scheduler then create a schedule & task wait for its 
sl@0: 						execution then check heap again for memory leaks
sl@0: @SYMTestExpectedResults Test must not fail (i.e. No memory leaks)
sl@0: @SYMDEF                INC098909: Process !TaskScheluder leaks memory in mail polls.
sl@0: */
sl@0: static void INC098909()
sl@0: 	{
sl@0: 	const TInt KTimeToWait = 10*1000*1000; 
sl@0: 	SchSvrHelpers::DeleteScheduleFilesL();
sl@0: 	
sl@0: 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3362 INC098909: Process !TaskScheluder leaks memory in mail polls "));
sl@0: 	TheTest.Next(_L("Connect to Scheduler"));
sl@0: 	TEST2(TheScheduler.Connect(),KErrNone);
sl@0: 
sl@0: 	TheTest.Next(_L("Registering Client"));
sl@0: 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
sl@0: 	
sl@0: 	User::LeaveIfError(TheScheduler.__DbgMarkHeap());
sl@0: 
sl@0: 	//Create Schedule
sl@0: 	CArrayFixFlat<TScheduleEntryInfo2>* entryList = new(ELeave) CArrayFixFlat<TScheduleEntryInfo2>(1);
sl@0: 	CleanupStack::PushL(entryList);
sl@0: 	TSchedulerItemRef ref;
sl@0: 	
sl@0: 	TTsTime startTime1(SchSvrHelpers::UtcTimeBasedOnOffset(0, 0), ETrue); // 0m:0s from "now"
sl@0: 
sl@0: 	TScheduleEntryInfo2 entry1 (startTime1, EDaily, 1, 20);
sl@0: 	entryList->AppendL(entry1);
sl@0: 	
sl@0: 	// Create the schedule for the task...
sl@0: 	TEST2(TheScheduler.CreatePersistentSchedule(ref, *entryList),KErrNone);
sl@0: 	
sl@0: 	//Create Task
sl@0: 	TTaskInfo task;
sl@0: 	task.iRepeat	= 1; // repeat once
sl@0: 	task.iName		= _L("Test ");
sl@0: 	task.iPriority	= 100;
sl@0: 	
sl@0: 	HBufC* taskData = HBufC::NewLC(1);
sl@0: 	TEST2(TheScheduler.ScheduleTask(task, *taskData,ref.iHandle), KErrNone);
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(taskData);
sl@0: 	CleanupStack::PopAndDestroy(entryList);	
sl@0: 	
sl@0: 	//Wait schedule to complete
sl@0: 	User::After(KTimeToWait);
sl@0: 
sl@0: 	User::LeaveIfError(TheScheduler.__DbgMarkEnd(0));
sl@0: 	
sl@0: 	TheScheduler.Close();
sl@0: 	// really clean out the scheduler (get rid of all the files and process)
sl@0: 	SchSvrHelpers::DeleteScheduleFilesL();
sl@0: 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
sl@0: 	// Now wait for something to happen...
sl@0: 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);	
sl@0: 	}
sl@0: 
sl@0: GLDEF_C TInt DoTheTestsL()
sl@0: 	{
sl@0: 	//Delete old files.
sl@0: 	SchSvrHelpers::DeleteScheduleFilesL();
sl@0: 	
sl@0: 	STaskSemaphore sem;
sl@0: 	sem.CreateL();
sl@0: 
sl@0: 	TheTest.Next(_L("Start tests"));
sl@0: 	// Add tests here:-
sl@0: 	Test1L();
sl@0: 	Test2L();
sl@0: 	INC098909();
sl@0: 	DEF055586L();
sl@0: 	DEF061595L();
sl@0: 	DEF079983L();	// task file check test
sl@0: 	
sl@0: 	sem.Close();
sl@0: 	
sl@0: 	//Tidying up so next test will be clear.
sl@0: 	TheTest.Next(_L("Delete all schedules"));
sl@0: 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
sl@0: 	SchSvrHelpers::Pause(TheTest, 2);
sl@0: 	TheTest.Next(_L("Delete old files\n"));
sl@0: 	SchSvrHelpers::DeleteScheduleFilesL();
sl@0: 
sl@0: 	TheScheduler.Close();
sl@0: 
sl@0: 	return KErrNone;
sl@0: 	}
sl@0: 
sl@0: //***********************************************************************************
sl@0: GLDEF_C TInt E32Main()
sl@0:     {
sl@0: 	__UHEAP_MARK;
sl@0: 	TheTest.Title();
sl@0: 	TheTest.Start(_L("TC_TSCH_SCHEDULING1 - UTC"));
sl@0: 
sl@0: 	TInt error = KErrNone;
sl@0: 	CTrapCleanup* cleanup = CTrapCleanup::New();
sl@0: 	if	(!cleanup)
sl@0: 		return KErrNoMemory;
sl@0: 	//If the previous test fails, SCHSVR.exe may stay in memory.
sl@0: 	TRAP(error,CleanupHelpers::TestCleanupL());
sl@0: 	TEST2(error, KErrNone);
sl@0: 
sl@0: 	TTime now;
sl@0: 	now.HomeTime();
sl@0: 	// Used to Set the system UTC time and UTC offset
sl@0: 	// so that correct UTC Time values are returned while using time based APIs.
sl@0: 	SchSvrHelpers::SetHomeTimeL(now);
sl@0: 
sl@0: 	TEST2(TheFsSession.Connect(), KErrNone);
sl@0: 	TheTest.Next(_L("Do the tests"));
sl@0: 	TRAP(error, DoTheTestsL());
sl@0: 	TEST2(error,KErrNone);
sl@0: 	TheFsSession.Close();
sl@0: 	TRAP(error,CleanupHelpers::TestCleanupL());
sl@0: 	TEST2(error, KErrNone);
sl@0: 	delete cleanup;	
sl@0: 
sl@0: 	TheTest.End();
sl@0: 	TheTest.Close();
sl@0: 	__UHEAP_MARKEND;
sl@0: 	return KErrNone;
sl@0: 	}