os/ossrv/genericservices/taskscheduler/Test/Scheduling/TC_TSCH_SCHEDULING1_HOMETIME.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 - Hometime"));
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);
96 TScheduleEntryInfo2 entry1 (aStartTime, EDaily, 1, 30);
97 entryList->AppendL(entry1);
98 TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
99 CleanupStack::PopAndDestroy(); // entryList
103 // counts the number of scheduled items based on the supplied filter.
104 static TInt CountScheduledItemsL(TScheduleFilter aFilter,
105 RScheduler& aScheduler)
106 // Extract schedule references from the schedule server based on a filter. If
108 CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
109 CleanupStack::PushL(refs);
111 TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
112 TEST2(res, KErrNone);
114 TInt count = refs->Count();
115 CleanupStack::PopAndDestroy(); // refs
119 // Extract task references from the schedule server based on a ID
120 static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
123 aTaskInfoArray.Reset();
124 TTsTime nextTimeScheduleIsDue;
125 TScheduleState2 state;
126 CScheduleEntryInfoArray* entries
127 = new (ELeave) CScheduleEntryInfoArray(3);
128 CleanupStack::PushL(entries);
129 TInt res = TheScheduler.GetScheduleL(aScheduleId,
133 nextTimeScheduleIsDue);
134 TEST2(res, KErrNone);
135 CleanupStack::PopAndDestroy(entries);
138 // schedules a transient task
139 static TInt ScheduleTransientTaskL(TInt& aTaskId,
140 TSchedulerItemRef& aRef,
142 RScheduler& aScheduler)
144 CScheduleEntryInfoArray* entryList = new(ELeave) CScheduleEntryInfoArray(3);
145 CleanupStack::PushL(entryList);
147 aRef.iName = _L("transient one");
150 TTime ttime1(SchSvrHelpers::TimeBasedOnOffset(0, 1));
151 TTsTime startTime1 (ttime1,EFalse); // 1 min in the future
152 TScheduleEntryInfo2 entry1 (startTime1, EDaily, 1, 20);
153 entryList->AppendL(entry1);
154 TTime ttime2(SchSvrHelpers::TimeBasedOnOffset(0, 2));
155 TTsTime startTime2 (ttime2,EFalse); // 2 min in the future
156 TScheduleEntryInfo2 entry2 (startTime2, EDaily, 1, 500);
157 entryList->AppendL(entry2);
159 TTime ttime3(SchSvrHelpers::TimeBasedOnOffset(0, 3));
160 TTsTime startTime3 (ttime3,EFalse); // 3 min in the future
161 TScheduleEntryInfo2 entry3 (startTime3, EDaily, 1, 5);
162 entryList->AppendL(entry3);
166 taskInfo.iName = _L("mail");
167 taskInfo.iTaskId = aTaskId;
168 taskInfo.iRepeat = aRepeat;
169 taskInfo.iPriority = 2;
170 HBufC* data = _L("the data, some strange new name ").AllocLC();
173 TInt res = aScheduler.ScheduleTask(taskInfo, *data, aRef, *entryList);
174 CleanupStack::PopAndDestroy(2); // data, entryList
176 aTaskId = taskInfo.iTaskId;
181 //***********************************************************************************
185 @SYMTestCaseID SYSLIB-SCHSVR-CT-0251
186 @SYMTestCaseDesc Replicated test for for defect (EDNEMHE-4Q69BG) - Hometime
187 @SYMTestPriority High
188 @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
189 @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-0251 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, EFalse); // 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-0252
266 @SYMTestCaseDesc Replicated test for for defect (EDNEMHE-4Q69BG) - Hometime
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-0252 Testing creation of transient schedule with task repeating 5 times "));
277 TSchedulerItemRef ref;
278 // schedule has 3 entries - +1min, +2min and +3min
279 TInt res = ScheduleTransientTaskL(tTask, ref, 5, TheScheduler);//5 repeats
280 TEST2(res, KErrNone);
282 TheTest.Printf(_L("Get Task count and repeat count\n"));
284 CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
285 CleanupStack::PushL(tasks);
286 GetTaskInfoL(*tasks, ref.iHandle);
287 TEST(tasks->Count() == 1);
288 TTaskInfo info = tasks->At(0);
289 TEST(info.iRepeat == 5);
290 ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 3);
291 GetTaskInfoL(*tasks, ref.iHandle);
292 TEST(tasks->Count() == 1);
294 TEST(info.iRepeat == 2); // still 2 repeats to go.
295 ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 2);
297 CleanupStack::PopAndDestroy(tasks);
299 TInt scheduleCount = CountScheduledItemsL(EPendingSchedules, TheScheduler);
300 // There should be no schedules as its a transient one and last schedule
301 // should have deleted itself.
302 TEST(scheduleCount == 0);
303 SchSvrHelpers::Pause(TheTest);
306 //**********************************************************************************
310 @SYMTestCaseID SYSLIB-SCHSVR-CT-0253
311 @SYMTestCaseDesc Replicated test for for defect (DEF055586L) - Hometime
312 @SYMTestPriority High
313 @SYMTestActions Create a time-based schedule check that there are 0 entries in the scehule after it fires
314 @SYMTestExpectedResults The test must not fail.
317 static void DEF055586L()
319 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0253 DEF055586 - Last element in array missed by loop "));
321 TheTest.Next(_L("Connect to Scheduler"));
322 TInt res = TheScheduler.Connect();
323 TEST2(res, KErrNone);
325 TheTest.Next(_L("Registering Client"));
326 TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
328 // Set the time to a known value, since this makes testing much easier (and more
330 SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
332 // This is the time when we want the schedule to fire
333 TDateTime datetime(2000, EJanuary, 1, 10, 0, 0, 0);
334 TTsTime startTimeForSchedule(datetime, EFalse); // 10:00 am
336 // Prepare a schedule describing when we want the tasks to run (10:00 am)
337 TSchedulerItemRef ref;
339 CScheduleEntryInfoArray* entryList = new (ELeave) CScheduleEntryInfoArray(1);
340 CleanupStack::PushL(entryList);
342 TScheduleEntryInfo2 entry1 (startTimeForSchedule, EDaily, 0, 30);
343 entryList->AppendL(entry1);
344 TInt err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
346 TEST2(err, KErrArgument);
348 TheTest.Next(_L("DEF055586 - Now checking 0 entries in schedule"));
351 err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
353 TEST2(err, KErrArgument);
355 CleanupStack::PopAndDestroy(); // entryList
359 @SYMTestCaseID SYSLIB-SCHSVR-CT-3361
360 @SYMTestCaseDesc Replicated test for for defect (INC098909) - Hometime
361 @SYMTestPriority High
362 @SYMTestActions Mark heap of Scheduler then create a schedule & task wait for its
363 execution then check heap again for memory leaks
364 @SYMTestExpectedResults Test must not fail (i.e. No memory leaks)
365 @SYMDEF INC098909: Process !TaskScheluder leaks memory in mail polls.
367 static void INC098909()
369 const TInt KTimeToWait = 10*1000*1000;
370 SchSvrHelpers::DeleteScheduleFilesL();
371 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3361 INC098909: Process !TaskScheluder leaks memory in mail polls "));
372 TheTest.Next(_L("Connect to Scheduler"));
373 TEST2(TheScheduler.Connect(),KErrNone);
375 TheTest.Next(_L("Registering Client"));
376 TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
378 User::LeaveIfError(TheScheduler.__DbgMarkHeap());
381 CArrayFixFlat<TScheduleEntryInfo2>* entryList = new(ELeave) CArrayFixFlat<TScheduleEntryInfo2>(1);
382 CleanupStack::PushL(entryList);
383 TSchedulerItemRef ref;
385 TTime ttime(SchSvrHelpers::TimeBasedOnOffset(0));
386 TTsTime startTime1(ttime, EFalse);
388 TScheduleEntryInfo2 entry1 (startTime1, EDaily, 1, 20);
389 entryList->AppendL(entry1);
391 // Create the schedule for the task...
392 TEST2(TheScheduler.CreatePersistentSchedule(ref, *entryList),KErrNone);
396 task.iRepeat = 1; // repeat once
397 task.iName = _L("Test ");
398 task.iPriority = 100;
400 HBufC* taskData = HBufC::NewLC(1);
401 TEST2(TheScheduler.ScheduleTask(task, *taskData,ref.iHandle), KErrNone);
403 CleanupStack::PopAndDestroy(taskData);
404 CleanupStack::PopAndDestroy(entryList);
406 //Wait schedule to complete
407 User::After(KTimeToWait);
409 User::LeaveIfError(TheScheduler.__DbgMarkEnd(0));
411 // really clean out the scheduler (get rid of all the files and process)
412 SchSvrHelpers::DeleteScheduleFilesL();
413 CleanupHelpers::KillProcess(KMinimalTaskHandler);
414 // Now wait for something to happen...
415 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
419 GLDEF_C TInt DoTheTestsL()
422 SchSvrHelpers::DeleteScheduleFilesL();
427 TheTest.Next(_L("Start tests"));
436 //Tidying up so next test will be clear.
437 TheTest.Next(_L("Delete all schedules"));
438 SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
439 SchSvrHelpers::Pause(TheTest, 2);
440 TheTest.Next(_L("Delete old files\n"));
441 SchSvrHelpers::DeleteScheduleFilesL();
443 TheScheduler.Close();
448 //***********************************************************************************
449 GLDEF_C TInt E32Main()
453 TheTest.Start(_L("TC_TSCH_SCHEDULING1 - Hometime"));
455 TInt error = KErrNone;
456 CTrapCleanup* cleanup = CTrapCleanup::New();
459 //If the previous test fails, SCHSVR.exe may stay in memory.
460 TRAP(error,CleanupHelpers::TestCleanupL());
461 TEST2(error, KErrNone);
463 TEST2(TheFsSession.Connect(), KErrNone);
464 TheTest.Next(_L("Do the tests"));
465 TRAP(error, DoTheTestsL());
466 TEST2(error,KErrNone);
467 TheFsSession.Close();
468 TRAP(error,CleanupHelpers::TestCleanupL());
469 TEST2(error, KErrNone);