os/ossrv/genericservices/taskscheduler/Test/Scheduling/TC_TSCH_SCHEDULING1_UTC.cpp
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
22 #include "TestUtils.h"
25 RTest TheTest(_L("TC_TSCH_SCHEDULING1 - UTC"));
26 static RScheduler TheScheduler;
27 static RFs TheFsSession;
29 typedef CArrayFixFlat<TScheduleEntryInfo2> CScheduleEntryInfoArray;
30 typedef CArrayFixFlat<TTaskInfo> CTaskInfoArray;
31 typedef CArrayFixFlat<TSchedulerItemRef> CSchItemRefArray;
33 _LIT(KMinimalTaskHandler, "MinimalTaskHandler");
35 _LIT(KTimeFormatString, "%-B%:0%J%:1%T%:2%S%.%*C4%:3%+B");
36 _LIT(KCurrentDateTimeChanged, "Date & Time changed to: [%S]\n");
38 //***********************************************************************************
40 // Sets time to before specified time by aTimeBeforeInSeconds
41 static void SetTimeBeforeL(RTest& aTest, TTsTime& aTime, TInt aTimeBeforeInSeconds)
43 TTimeIntervalSeconds secs(aTimeBeforeInSeconds);
44 TTime time = aTime.GetLocalTime()-secs;
45 SchSvrHelpers::SetHomeTimeL(time);
47 time.FormatL(dateString, KTimeFormatString);
48 aTest.Printf(KCurrentDateTimeChanged, &dateString);
51 // gets the due time for this schedule
52 static TTsTime GetDueTimeL(TInt aScheduleId)
54 TTsTime nextTimeScheduleIsDue;
55 TScheduleState2 state;
56 CScheduleEntryInfoArray* entries
57 = new (ELeave) CScheduleEntryInfoArray(3);
58 CleanupStack::PushL(entries);
59 CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
60 CleanupStack::PushL(tasks);
62 TInt res = TheScheduler.GetScheduleL(aScheduleId, state, *entries, *tasks, nextTimeScheduleIsDue);
65 CleanupStack::PopAndDestroy(2); // entries, tasks
67 return state.DueTime();
70 // Forces the task to be exectued aCount times.
71 static void ForceTaskExecutionForSpecifiedIdL(TInt aId, TInt aCount)
73 TheTest.Printf(_L("Executing %d times\n"), aCount);
75 for (TInt i=0; i<aCount; ++i)
77 TheTest.Printf(_L("Execution %d\n"), i+1);
78 time = GetDueTimeL(aId);
80 SetTimeBeforeL(TheTest, time, 5 /*seconds*/);
82 // Wait for notification that schedule has executed.
83 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
84 CleanupHelpers::KillProcess(KMinimalTaskHandler);
88 //creates a daily schedule with StartTime of aStartTime
89 static TInt CreateScheduleL(TSchedulerItemRef& aRef,
90 RScheduler& aScheduler,
91 const TTsTime& aStartTime)
93 CScheduleEntryInfoArray* entryList
94 = new (ELeave) CScheduleEntryInfoArray(1);
95 CleanupStack::PushL(entryList);
97 TScheduleEntryInfo2 entry1 (aStartTime, EDaily, 1, 30);
98 entryList->AppendL(entry1);
99 TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
100 CleanupStack::PopAndDestroy(); // entryList
104 // counts the number of scheduled items based on the supplied filter.
105 static TInt CountScheduledItemsL(TScheduleFilter aFilter,
106 RScheduler& aScheduler)
107 // Extract schedule references from the schedule server based on a filter. If
109 CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
110 CleanupStack::PushL(refs);
112 TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
113 TEST2(res, KErrNone);
115 TInt count = refs->Count();
116 CleanupStack::PopAndDestroy(); // refs
120 // Extract task references from the schedule server based on a ID
121 static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
124 aTaskInfoArray.Reset();
125 TTsTime nextTimeScheduleIsDue;
126 TScheduleState2 state;
127 CScheduleEntryInfoArray* entries
128 = new (ELeave) CScheduleEntryInfoArray(3);
129 CleanupStack::PushL(entries);
130 TInt res = TheScheduler.GetScheduleL(aScheduleId,
134 nextTimeScheduleIsDue);
135 TEST2(res, KErrNone);
136 CleanupStack::PopAndDestroy(entries);
139 // schedules a transient task
140 static TInt ScheduleTransientTaskL(TInt& aTaskId,
141 TSchedulerItemRef& aRef,
143 RScheduler& aScheduler)
145 CScheduleEntryInfoArray* entryList = new(ELeave) CScheduleEntryInfoArray(3);
146 CleanupStack::PushL(entryList);
148 aRef.iName = _L("transient one");
151 TTime ttime1(SchSvrHelpers::UtcTimeBasedOnOffset(0, 1));
152 TTsTime startTime1 (ttime1,ETrue); // 1 min in the future
153 TScheduleEntryInfo2 entry1 (startTime1, EDaily, 1, 20);
154 entryList->AppendL(entry1);
155 TTime ttime2(SchSvrHelpers::UtcTimeBasedOnOffset(0, 2));
156 TTsTime startTime2 (ttime2,ETrue); // 2 min in the future
157 TScheduleEntryInfo2 entry2 (startTime2, EDaily, 1, 500);
158 entryList->AppendL(entry2);
160 TTime ttime3(SchSvrHelpers::UtcTimeBasedOnOffset(0, 3));
161 TTsTime startTime3 (ttime3,ETrue); // 3 min in the future
162 TScheduleEntryInfo2 entry3 (startTime3, EDaily, 1, 5);
163 entryList->AppendL(entry3);
167 taskInfo.iName = _L("mail");
168 taskInfo.iTaskId = aTaskId;
169 taskInfo.iRepeat = aRepeat;
170 taskInfo.iPriority = 2;
171 HBufC* data = _L("the data, some strange new name ").AllocLC();
174 TInt res = aScheduler.ScheduleTask(taskInfo, *data, aRef, *entryList);
175 CleanupStack::PopAndDestroy(2); // data, entryList
177 aTaskId = taskInfo.iTaskId;
182 //***********************************************************************************
186 @SYMTestCaseID SYSLIB-SCHSVR-CT-0254
187 @SYMTestCaseDesc Replicated test for for defect (EDNEMHE-4Q69BG) - UTC
188 @SYMTestPriority High
189 @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
190 @SYMTestExpectedResults The test must not fail.
195 _LIT(KTaskData1, "This is some really exciting task data (number 1)");
196 _LIT(KTaskData2, "This is some really exciting task data (number 2)");
197 _LIT(KTestName, "SmsTest");
199 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0254 TheTest3: SMS:Sending to multiple recipients "));
201 TheTest.Next(_L("Connect to Scheduler"));
202 TInt res = TheScheduler.Connect();
203 TEST2(res, KErrNone);
205 TheTest.Next(_L("Registering Client"));
206 TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
208 // Set the time to a known value, since this makes testing much easier (and more
210 SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
212 // This is the time when we want the schedule to fire
213 TDateTime datetime(2000, EJanuary, 1, 10, 0, 0, 0);
214 TTsTime startTimeForSchedule(datetime, ETrue); // 10:00 am
217 // Prepare a schedule describing when we want the tasks to run (10:00 am)
218 TSchedulerItemRef ref;
219 User::LeaveIfError(CreateScheduleL(ref, TheScheduler, startTimeForSchedule));
221 // Disable the schedule whilst we set it up
222 User::LeaveIfError(TheScheduler.DisableSchedule(ref.iHandle));
224 // Associate a task with the schedule
226 taskInfo1.iRepeat = 0;
227 taskInfo1.iName = KTestName;
228 taskInfo1.iPriority = 2;
230 // Create some data associated with this task
231 HBufC* taskData1 = KTaskData1().AllocLC();
232 User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo1, *taskData1, ref.iHandle));
233 CleanupStack::PopAndDestroy(); // taskData1
235 // Associate a task (2) with the schedule
237 taskInfo2.iRepeat = 0;
238 taskInfo2.iName = KTestName;
239 taskInfo2.iPriority = 2;
241 // Create some data associated with this task
242 HBufC* taskData2 = KTaskData2().AllocLC();
243 User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo2, *taskData2, ref.iHandle));
244 CleanupStack::PopAndDestroy(); // taskData2
246 // We should now have two tasks scheduled at exactly the same time...
247 User::LeaveIfError(TheScheduler.EnableSchedule(ref.iHandle));
249 // Set the time to 5 minutes *AFTER* the schedule was due to run (10:05am). In this instance,
250 // based on the new fixed Schedule Server, the schedule should still execute since
251 // it falls within the validity period (30 mins), however, in the old scheme of things,
252 // the schedule would not be valid again until tomorrow (2/1/2000) at 10:00am and hence
253 // would not execute until then.
254 SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 10, 5, 0, 0))); // 10:05 am
256 // Now wait for something to happen...
257 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
258 CleanupHelpers::KillProcess(KMinimalTaskHandler);
261 //***********************************************************************************
265 @SYMTestCaseID SYSLIB-SCHSVR-CT-0255
266 @SYMTestCaseDesc Replicated test for for defect (EDNEMHE-4Q69BG) - UTC
267 @SYMTestPriority High
268 @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
269 @SYMTestExpectedResults The test must not fail.
274 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0255 Testing creation of transient schedule with task repeating 5 times "));
276 TSchedulerItemRef ref;
277 // schedule has 3 entries - +1min, +2min and +3min
278 TInt res = ScheduleTransientTaskL(tTask, ref, 5, TheScheduler);//5 repeats
279 TEST2(res, KErrNone);
281 TheTest.Printf(_L("Get Task count and repeat count\n"));
283 CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
284 CleanupStack::PushL(tasks);
285 GetTaskInfoL(*tasks, ref.iHandle);
286 TEST(tasks->Count() == 1);
287 TTaskInfo info = tasks->At(0);
288 TEST(info.iRepeat == 5);
289 ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 3);
290 GetTaskInfoL(*tasks, ref.iHandle);
291 TEST(tasks->Count() == 1);
293 TEST(info.iRepeat == 2); // still 2 repeats to go.
294 ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 2);
296 CleanupStack::PopAndDestroy(tasks);
298 TInt scheduleCount = CountScheduledItemsL(EPendingSchedules, TheScheduler);
299 // There should be no schedules as its a transient one and last schedule
300 // should have deleted itself.
301 TEST(scheduleCount == 0);
302 SchSvrHelpers::Pause(TheTest);
305 //***********************************************************************************
309 @SYMTestCaseID SYSLIB-SCHSVR-CT-0256
310 @SYMTestCaseDesc Replicated test for for defect (DEF055586L) - UTC
311 @SYMTestPriority High
312 @SYMTestActions Create a time-based schedule check that there are 0 entries in the scehule after it fires
313 @SYMTestExpectedResults The test must not fail.
316 static void DEF055586L()
318 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0256 DEF055586 - Last element in array missed by loop "));
319 TheTest.Next(_L("Connect to Scheduler"));
320 TInt res = TheScheduler.Connect();
321 TEST2(res, KErrNone);
323 TheTest.Next(_L("Registering Client"));
324 TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
326 // Set the time to a known value, since this makes testing much easier (and more
328 SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
330 // This is the time when we want the schedule to fire
331 TDateTime datetime(2000, EJanuary, 1, 10, 0, 0, 0);
332 TTsTime startTimeForSchedule(datetime, ETrue); // 10:00 am
334 // Prepare a schedule describing when we want the tasks to run (10:00 am)
335 TSchedulerItemRef ref;
337 CScheduleEntryInfoArray* entryList = new (ELeave) CScheduleEntryInfoArray(1);
338 CleanupStack::PushL(entryList);
340 TScheduleEntryInfo2 entry1 (startTimeForSchedule, EDaily, 0, 30); // TTimeIntervalDays: set to 0 to induce an error
341 entryList->AppendL(entry1);
342 TInt err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
344 TEST2(err, KErrArgument);
346 TheTest.Next(_L("DEF055586 - Now checking 0 entries in schedule"));
349 err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
351 TEST2(err, KErrArgument);
353 CleanupStack::PopAndDestroy(); // entryList
356 //DEF061595 Schedule timers incorrectly expire when system time is changed
357 //The test will create and submit one schedule which execution time is one year later.
358 //Then the test will change the system time to be current time + 1 day.
359 //Although the schedule time is set to be current time + 1 year, the schedule task(s) will
360 //be executed immediately because the schedule timer expires when the system time changes.
363 TheTest.Next(_L("DEF061595 Schedule timers incorrectly expire when system time is changed"));
364 //Get current time in currTime variable
367 //Prepare the task time (in taskTime variable) to be currTime + 1 year.
368 TTime taskTime(currTime + TTimeIntervalYears(1));
369 TInt taskYear = taskTime.DateTime().Year();
370 //Connect to the Task Scheduler Server
371 RScheduler scheduler;
372 CleanupClosePushL(scheduler);
373 TInt res = scheduler.Connect();
374 TEST2(res, KErrNone);
375 TEST2(SchSvrHelpers::RegisterClientL(scheduler), KErrNone);
376 //Create new schedule. The new schedule task will run 1 year later.
377 TTsTime taskTsTime(taskTime, EFalse);
378 TSchedulerItemRef ref;
379 TEST2(::CreateScheduleL(ref, scheduler, taskTsTime), KErrNone);
380 // Disable the schedule whilst we set it up
381 TEST2(scheduler.DisableSchedule(ref.iHandle), KErrNone);
382 // Associate a task with the schedule
383 _LIT(KTaskInfo, "DEF061595");
385 taskInfo.iRepeat = 0;
386 taskInfo.iName = KTaskInfo;
387 taskInfo.iPriority = 2;
388 const TInt KTaskDataLen = 1;
389 HBufC* taskData = HBufC::NewLC(KTaskDataLen);
390 TEST2(scheduler.ScheduleTask(taskInfo, *taskData, ref.iHandle), KErrNone);
391 //We should now have one tasks scheduled to be executed after 1 year.
393 TEST2(scheduler.EnableSchedule(ref.iHandle), KErrNone);
394 //Get the scheduled task info. The task year should be the same as it was set in the schedule.
396 TPtr pTaskData = taskData->Des();
397 TEST2(scheduler.GetTaskInfoL(taskInfo.iTaskId, taskInfo, pTaskData, ref, nextTime), KErrNone);
398 TInt nextTaskYear = nextTime.DateTime().Year();
399 TEST(nextTaskYear == taskYear);
400 //Change the system time to be current time + 1 day
401 TEST2(SchSvrHelpers::SetUTCTimeL(currTime + TTimeIntervalDays(1)), KErrNone);
402 //Get the scheduled task info again. If the defect is not fixed, the call will fail
403 //with KErrNotFound (because SchSvrHelpers::SetUTCTimeL() call will make the schedule timer to expire)
404 TInt err = scheduler.GetTaskInfoL(taskInfo.iTaskId, taskInfo, pTaskData, ref, nextTime);
405 //TODO TEST2(err, KErrNone);
406 //TODO nextTaskYear = nextTime.DateTime().Year();
407 //TODO TEST(nextTaskYear == taskYear);
409 CleanupStack::PopAndDestroy(taskData);
410 CleanupStack::PopAndDestroy(&scheduler);
411 CleanupHelpers::KillProcess(KMinimalTaskHandler);
415 @SYMTestCaseID SYSLIB-SCHSVR-CT-1655
416 @SYMTestCaseDesc Tests for defect number DEF079983
417 @SYMTestPriority High
418 @SYMTestActions Check to ensure all task files have been cleaned up
419 after scheduled tasks completed
420 @SYMTestExpectedResults Test must not fail
423 static void DEF079983L()
425 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1655 DEF079983: Private directory of schsvr flooded with files "));
426 // A dummy scheduled task:
427 // Create some transient schedule task and repeat 5 times
428 TheTest.Printf(_L("DEF079983: Create a dummy scheduled task"));
430 TSchedulerItemRef ref;
432 TInt res = ScheduleTransientTaskL(tTask, ref, 5, TheScheduler);
433 TEST2(res, KErrNone);
434 ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 5);
436 // Check for left task files after scheduled tasks completed
437 // To access private data cage, uses SchSvrHelplers::CheckTaskFilesL()
438 TheTest.Next(_L("DEF079983: Now checking no files left when tasks completed"));
439 TInt err = SchSvrHelpers::CheckTaskFilesL();
440 // If there's any task files left, test fails with error code KErrGeneral
441 TEST(err == KErrNone);
445 @SYMTestCaseID SYSLIB-SCHSVR-CT-3362
446 @SYMTestCaseDesc Replicated test for for defect (INC098909) - UTC
447 @SYMTestPriority High
448 @SYMTestActions Mark heap of Scheduler then create a schedule & task wait for its
449 execution then check heap again for memory leaks
450 @SYMTestExpectedResults Test must not fail (i.e. No memory leaks)
451 @SYMDEF INC098909: Process !TaskScheluder leaks memory in mail polls.
453 static void INC098909()
455 const TInt KTimeToWait = 10*1000*1000;
456 SchSvrHelpers::DeleteScheduleFilesL();
458 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3362 INC098909: Process !TaskScheluder leaks memory in mail polls "));
459 TheTest.Next(_L("Connect to Scheduler"));
460 TEST2(TheScheduler.Connect(),KErrNone);
462 TheTest.Next(_L("Registering Client"));
463 TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
465 User::LeaveIfError(TheScheduler.__DbgMarkHeap());
468 CArrayFixFlat<TScheduleEntryInfo2>* entryList = new(ELeave) CArrayFixFlat<TScheduleEntryInfo2>(1);
469 CleanupStack::PushL(entryList);
470 TSchedulerItemRef ref;
472 TTsTime startTime1(SchSvrHelpers::UtcTimeBasedOnOffset(0, 0), ETrue); // 0m:0s from "now"
474 TScheduleEntryInfo2 entry1 (startTime1, EDaily, 1, 20);
475 entryList->AppendL(entry1);
477 // Create the schedule for the task...
478 TEST2(TheScheduler.CreatePersistentSchedule(ref, *entryList),KErrNone);
482 task.iRepeat = 1; // repeat once
483 task.iName = _L("Test ");
484 task.iPriority = 100;
486 HBufC* taskData = HBufC::NewLC(1);
487 TEST2(TheScheduler.ScheduleTask(task, *taskData,ref.iHandle), KErrNone);
489 CleanupStack::PopAndDestroy(taskData);
490 CleanupStack::PopAndDestroy(entryList);
492 //Wait schedule to complete
493 User::After(KTimeToWait);
495 User::LeaveIfError(TheScheduler.__DbgMarkEnd(0));
497 TheScheduler.Close();
498 // really clean out the scheduler (get rid of all the files and process)
499 SchSvrHelpers::DeleteScheduleFilesL();
500 CleanupHelpers::KillProcess(KMinimalTaskHandler);
501 // Now wait for something to happen...
502 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
505 GLDEF_C TInt DoTheTestsL()
508 SchSvrHelpers::DeleteScheduleFilesL();
513 TheTest.Next(_L("Start tests"));
520 DEF079983L(); // task file check test
524 //Tidying up so next test will be clear.
525 TheTest.Next(_L("Delete all schedules"));
526 SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
527 SchSvrHelpers::Pause(TheTest, 2);
528 TheTest.Next(_L("Delete old files\n"));
529 SchSvrHelpers::DeleteScheduleFilesL();
531 TheScheduler.Close();
536 //***********************************************************************************
537 GLDEF_C TInt E32Main()
541 TheTest.Start(_L("TC_TSCH_SCHEDULING1 - UTC"));
543 TInt error = KErrNone;
544 CTrapCleanup* cleanup = CTrapCleanup::New();
547 //If the previous test fails, SCHSVR.exe may stay in memory.
548 TRAP(error,CleanupHelpers::TestCleanupL());
549 TEST2(error, KErrNone);
553 // Used to Set the system UTC time and UTC offset
554 // so that correct UTC Time values are returned while using time based APIs.
555 SchSvrHelpers::SetHomeTimeL(now);
557 TEST2(TheFsSession.Connect(), KErrNone);
558 TheTest.Next(_L("Do the tests"));
559 TRAP(error, DoTheTestsL());
560 TEST2(error,KErrNone);
561 TheFsSession.Close();
562 TRAP(error,CleanupHelpers::TestCleanupL());
563 TEST2(error, KErrNone);