Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
23 #include <e32property.h>
24 #include <schinfointernal.h>
26 #include "TestUtils.h"
28 _LIT(KTestName, "Task Scheduler Condition Scheduling Test");
30 RTest TheTest(KTestName);
32 typedef CArrayFixFlat<TTaskInfo> CTaskInfoArray;
33 typedef CArrayFixFlat<TSchedulerItemRef> CSchItemRefArray;
34 typedef CArrayFixFlat<TTaskSchedulerCondition> CSchConditionArray;
36 static RScheduler TheScheduler;
37 static CTrapCleanup* TheCleanup;
38 static RFs TheFsSession;
40 const TInt KTestKey1 = 1;
41 const TInt KTestKey2 = 2;
42 const TInt KTestKey3 = 3;
44 _LIT(KMinimalTaskHandler, "MinimalTaskHandler");
45 _LIT(KSeparator, "|"); // Invalid filepath char used to separate filenames
47 // This function launches the TPropertyDefine process which
48 // has WriteDeviceData Capabilities enabling it to create the P&S
49 // variables used by this test.
50 static void LaunchHelperL(TUid aCategory, TInt aKey, TInt aAttr)
52 _LIT(KConditionHelper, "TPropertyDefine");
57 args.AppendNum(aCategory.iUid);
58 args.Append(KSeparator);
60 args.Append(KSeparator);
61 args.AppendNum(aAttr);
63 User::LeaveIfError(p.Create(KConditionHelper, args,EOwnerProcess));
65 // Asynchronous logon: completes when process terminates with process exit code
69 User::WaitForRequest(stat);
70 TInt exitReason = p.ExitReason();
72 User::LeaveIfError(exitReason);
76 static void CreateTestVariables()
78 LaunchHelperL(KUidSystemCategory, KTestKey1,RProperty::EInt);
79 LaunchHelperL(KUidSystemCategory, KTestKey2,RProperty::EInt);
80 LaunchHelperL(KUidSystemCategory, KTestKey3,RProperty::EInt);
83 static void ResetVariablesL(TInt aKey1Val,
87 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,aKey1Val));
88 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey2,aKey2Val));
89 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey3,aKey3Val));
92 static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
94 // Extract schedule references from the schedule server based on a filter. If
96 aTaskInfoArray.Reset();
99 CSchConditionArray* conditionList
100 = new (ELeave) CSchConditionArray(3);
101 CleanupStack::PushL(conditionList);
102 TInt res = TheScheduler.GetScheduleL(aScheduleId,
107 TEST2(res, KErrNone);
108 CleanupStack::PopAndDestroy(conditionList);
111 static TInt CountTasksL(TInt aScheduleId)
113 CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
114 CleanupStack::PushL(tasks);
115 GetTaskInfoL(*tasks, aScheduleId);
116 TInt ret = tasks->Count();
117 CleanupStack::PopAndDestroy(tasks);
122 static TInt CountScheduledItemsL(TScheduleFilter aFilter,
123 RScheduler& aScheduler)
124 // Extract schedule references from the schedule server based on a filter. If
126 CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
127 CleanupStack::PushL(refs);
129 TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
130 TEST2(res, KErrNone);
132 TInt count = refs->Count();
133 CleanupStack::PopAndDestroy(); // refs
137 static CSchConditionArray* CreateSingleConditionLC(const TUid& aConditionUID,
138 TUint aConditionUInt)
140 CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
141 CleanupStack::PushL(conditionList);
144 TTaskSchedulerCondition condition1;
145 condition1.iCategory = aConditionUID;
146 condition1.iKey = aConditionUInt;
147 condition1.iState = 10;
148 condition1.iType = TTaskSchedulerCondition::EEquals;
149 conditionList->AppendL(condition1);
151 return conditionList;
154 static CSchConditionArray* CreateMultipleConditionsLC()
156 CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
157 CleanupStack::PushL(conditionList);
159 TTaskSchedulerCondition condition1;
160 condition1.iCategory = KUidSystemCategory;
161 condition1.iKey = KTestKey1;
162 condition1.iState = 10;
163 condition1.iType = TTaskSchedulerCondition::EEquals;
164 conditionList->AppendL(condition1);
167 TTaskSchedulerCondition condition2;
168 condition2.iCategory = KUidSystemCategory;
169 condition2.iKey = KTestKey2;
170 condition2.iState = 10;
171 condition2.iType = TTaskSchedulerCondition::ENotEquals;
172 conditionList->AppendL(condition2);
175 TTaskSchedulerCondition condition3;
176 condition3.iCategory = KUidSystemCategory;
177 condition3.iKey = KTestKey3;
178 condition3.iState = 10;
179 condition3.iType = TTaskSchedulerCondition::ELessThan;
180 conditionList->AppendL(condition3);
182 return conditionList;
185 // single condition with default time set to 1 year in the future
186 // As this is a valid time a CTimer object actually gets set unlike
187 // if Time::TTimeMax() is used, hence we need to test for both cases.
188 static TInt CreateScheduleSingle1L(TSchedulerItemRef& aRef,
189 RScheduler& aScheduler,
190 const TUid& aConditionUID,
191 TUint aConditionUInt)
193 aRef.iName = _L("Schedule created using CreateScheduleSingle");
195 CSchConditionArray* conditionList
196 = CreateSingleConditionLC(aConditionUID, aConditionUInt);
197 TTime time = SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1); //1 year in the future
198 TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
199 CleanupStack::PopAndDestroy(); // conditionList
203 // single condition with default time set to Time::MaxTTime()
204 static TInt CreateScheduleSingle2L(TSchedulerItemRef& aRef,
205 RScheduler& aScheduler,
206 const TUid& aConditionUID,
207 TUint aConditionUInt)
209 aRef.iName = _L("Schedule created using CreateScheduleSingle");
211 CSchConditionArray* conditionList
212 = CreateSingleConditionLC(aConditionUID, aConditionUInt);
213 TTime time = Time::MaxTTime();
214 TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
215 CleanupStack::PopAndDestroy(); // conditionList
219 // An empty schedule list.
220 static TInt CreateScheduleEmpty3L(TSchedulerItemRef& aRef,
221 RScheduler& aScheduler,
225 aRef.iName = _L("Empty Schedule list created");
227 CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
228 CleanupStack::PushL(conditionList);
230 TTime time = SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1); //1 year in the future
231 TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
232 CleanupStack::PopAndDestroy(); // conditionList
238 static TInt CreateScheduleSingleNull4L(TSchedulerItemRef& aRef,
239 RScheduler& aScheduler,
243 aRef.iName = _L("One schedule in the list with a NULL uid");
245 CSchConditionArray* conditionList
246 = CreateSingleConditionLC(KNullUid, 0);
248 TTime time = SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1); //1 year in the future
249 TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
250 CleanupStack::PopAndDestroy(); // conditionList
255 static TInt CreateScheduleMultipleL(TSchedulerItemRef& aRef, RScheduler& aScheduler)
257 aRef.iName = _L("Schedule created using CreateScheduleMultiple");
259 CSchConditionArray* conditionList = CreateMultipleConditionsLC();
260 TTime time = SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1); //1 year in the future
261 TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
262 CleanupStack::PopAndDestroy(); // conditionList
266 static TInt SchedulePersistentTaskL(const TDesC& aName,
269 RScheduler& aScheduler)
272 taskInfo.iTaskId = aNewId;
273 taskInfo.iName = aName;
274 taskInfo.iPriority = 2;
275 taskInfo.iRepeat = 0;
276 HBufC* data = _L("the data").AllocLC();
277 TInt res = aScheduler.ScheduleTask(taskInfo, *data, aScheduleId);
278 aNewId = taskInfo.iTaskId;
280 CleanupStack::PopAndDestroy(); // data
285 @SYMTestCaseID SYSLIB-SCHSVR-CT-1024
286 @SYMTestCaseDesc Single condition based test
287 @SYMTestPriority High
288 @SYMTestActions Create a single schedule,check that schedule is of condition type.Wait for the condition to be satisfied
289 Check that persistent schedule has auto-deleted.Repeat the process with another schedule with time set to
290 Time::MaxTTime().Try auto deleting the last schedule and test for not found error.
291 @SYMTestExpectedResults Test must not fail
294 static void DoTest1L()
296 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1024 "));
297 // create a simple condition based schedule, and see if it runs.
299 TheTest.Next(_L("single condition based test"));
302 //reset the p&s variables before creating the schedule
303 ResetVariablesL(0,0,0);
304 TSchedulerItemRef ref1;
305 TheTest.Printf(_L("Create a schedule\n"));
306 res = CreateScheduleSingle1L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
307 TEST2(res, KErrNone);
310 _LIT(KName1, "toothpaste");
311 TheTest.Printf(_L("Schedule some tasks\n"));
313 res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
314 TEST2(res, KErrNone);
316 res = TheScheduler.__DbgMarkHeap();
317 User::LeaveIfError(res); //#1
318 TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
319 TEST(scheduleCount == 1);
321 // Check that schedule is of condition type
322 TScheduleType scheduleType;
323 res = TheScheduler.GetScheduleTypeL(ref1.iHandle, scheduleType);
324 TEST2(res, KErrNone);
325 TEST(scheduleType == EConditionSchedule );
327 res = TheScheduler.__DbgMarkEnd(0);
328 User::LeaveIfError(res); //#1
330 TheScheduler.Close();
331 SchSvrHelpers::Pause(TheTest);
333 // wait for condition to be satisfied
334 res = RProperty::Set(KUidSystemCategory, KTestKey1,10);
335 User::LeaveIfError(res);
336 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
337 CleanupHelpers::KillProcess(KMinimalTaskHandler);
339 res = TheScheduler.Connect();
340 TEST2(res, KErrNone);
341 // Register a client with the server
342 TheTest.Next(_L("===== Registering Client ====="));
343 res = SchSvrHelpers::RegisterClientL(TheScheduler);
344 TEST2(res, KErrNone);
346 // can't check scheduler to see if any tasks left because it's been
347 // deleted as last task has completed
349 //Check that persistent schedule has auto-deleted
351 TheTest.Printf(_L("DEF46200 - Check schedule has auto deleted\n"));
352 scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
353 TEST(scheduleCount == 0);
355 // Attempt to delete auto-deleted schedule, should fail
357 TheTest.Printf(_L("DEF46200 - Attempting to delete schedule with id %d\n"), ref1.iHandle);
358 res = TheScheduler.DeleteSchedule(ref1.iHandle);
359 TEST2(res, KErrNotFound);
361 // now repeat process with singleschedule2
362 ResetVariablesL(0,0,0);
364 TheTest.Printf(_L("Create another schedule\n"));
365 res = CreateScheduleSingle2L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
366 TEST2(res, KErrNone);
368 TheTest.Printf(_L("Create an empty schedule list\n"));
369 res = CreateScheduleEmpty3L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
370 TEST2(res, KErrArgument);
372 TheTest.Printf(_L("Create an empty schedule in a list\n"));
373 res = CreateScheduleSingleNull4L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
374 TEST2(res, KErrArgument);
376 res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
377 TEST2(res, KErrNone);
378 SchSvrHelpers::Pause(TheTest);
380 // we should have one outstanding task (without the check in
381 // schtimer.cpp specifically for Time::MaxTTime() the timer would have
382 // gone off immediately in the past.)
383 TEST(CountTasksL(ref1.iHandle) == 1);
385 scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
386 TEST(scheduleCount == 1);
387 // wait for condition to be satisfied
388 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,10));
389 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
390 CleanupHelpers::KillProcess(KMinimalTaskHandler);
392 // can't check scheduler to see if any tasks left because it's been
393 // deleted as last task has completed
395 TheTest.Printf(_L("DEF46200 - Check schedule has auto deleted\n"));
396 scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
397 TEST(scheduleCount == 0);
399 // Attempt to delete auto-deleted schedule, should fail
401 TheTest.Printf(_L("DEF46200 - Attempting to delete schedule with id %d\n"), ref1.iHandle);
402 res = TheScheduler.DeleteSchedule(ref1.iHandle);
403 TEST2(res, KErrNotFound);
405 SchSvrHelpers::Pause(TheTest);
410 @SYMTestCaseID SYSLIB-SCHSVR-CT-1025
411 @SYMTestCaseDesc Multiple conditions based tests
412 @SYMTestPriority High
413 @SYMTestActions Create a condition based schedule with multiple entries, and see if it runs.
414 Try auto deleting the last schedule and test for not found error.
415 @SYMTestExpectedResults Test must not fail
418 static void DoTest2L()
420 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1025 "));
421 // create a condition based schedule with multiple entries, and see if it runs.
423 TheTest.Next(_L("multiple condition based test"));
426 //reset the p&s variables before creating the schedule
427 ResetVariablesL(0,10,20);
428 TSchedulerItemRef ref1;
429 TheTest.Printf(_L("Create a schedule\n"));
430 res = CreateScheduleMultipleL(ref1, TheScheduler);
431 TEST2(res, KErrNone);
434 _LIT(KName1, "web subscription");
435 TheTest.Printf(_L("Schedule some tasks\n"));
436 res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
437 TEST2(res, KErrNone);
439 TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
440 TEST(scheduleCount == 1);
442 // we should have one task scheduled to run
443 CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
444 CleanupStack::PushL(tasks);
445 GetTaskInfoL(*tasks, ref1.iHandle);
446 TEST(tasks->Count() == 1);
449 // wait for conditions to be satisfied
450 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,10));//"=="
451 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey2,1234));//"!="
452 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey3,9));//"<"
453 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
454 CleanupHelpers::KillProcess(KMinimalTaskHandler);
456 // Can't check schedule for task info because it's gone
459 // we should have no schedule, it has auto-deleted
460 scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
461 TEST(scheduleCount == 0);
464 TheTest.Printf(_L("Reseting variables"));
465 ResetVariablesL(0,10,20);
467 TheTest.Printf(_L("DEF46200 - Attempting to delete schedule with id %d\n"), ref1.iHandle);
468 res = TheScheduler.DeleteSchedule(ref1.iHandle);
469 TEST2(res, KErrNotFound);
470 scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
471 TEST(scheduleCount == 0);
473 CleanupStack::PopAndDestroy(tasks);
475 SchSvrHelpers::Pause(TheTest);
480 Test 3 does a lot of error checking
482 @SYMTestCaseID SYSLIB-SCHSVR-CT-1026
483 @SYMTestCaseDesc Tests for error conditions checking on task scheduler
484 @SYMTestPriority High
485 @SYMTestActions Create invalid P&S variable's and schedule a task,check for no argument error
486 @SYMTestExpectedResults Test must not fail
489 static void DoTest3L()
491 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1026 "));
492 // create a simple condition based schedule, and see if it runs.
494 TheTest.Next(_L("error checking test"));
497 //reset the p&s variables before creating the schedule
498 ResetVariablesL(0,0,0);
499 TSchedulerItemRef ref1;
500 _LIT(KName1, "toothpaste");
502 TheTest.Printf(_L("Create a schedule with a P&S variables that doesnt exist\n"));
504 const TUid KNonexistentUid = TUid::Uid(0x01234566);
505 res = CreateScheduleSingle1L(ref1, TheScheduler, KNonexistentUid, KTestKey1);
506 TEST2(res, KErrNone);
507 TheTest.Printf(_L("Schedule some tasks - error should be returned\n"));
510 taskInfo.iName = KName1;
511 taskInfo.iPriority = 2;
512 taskInfo.iRepeat = 0;
513 HBufC* data = _L("the data").AllocLC();
514 res = TheScheduler.ScheduleTask(taskInfo, *data, ref1.iHandle);
515 // since we have created the schedule using a UID which doesn't exist
516 //we should get an error
517 TEST2(res, KErrArgument);
518 CleanupStack::PopAndDestroy(); // data
520 TheTest.Printf(_L("Deleting schedule with id %d\n"), ref1.iHandle);
521 res = TheScheduler.DeleteSchedule(ref1.iHandle);
522 TEST2(res, KErrNone);
523 TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
524 TEST(scheduleCount == 0);
527 TheTest.Printf(_L("Create a schedule\n"));
528 res = CreateScheduleSingle1L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
529 TEST2(res, KErrNone);
530 TheTest.Printf(_L("Schedule some tasks\n"));
533 taskInfo.iName = KName1;
534 taskInfo.iPriority = 2;
535 taskInfo.iRepeat = 1;
536 HBufC* data = _L("the data").AllocLC();
537 User::LeaveIfError(TheScheduler.__DbgMarkHeap());
538 res = TheScheduler.ScheduleTask(taskInfo, *data, ref1.iHandle);
539 // since we have set repeat to something other than 0, we should get an error
540 TEST2(res, KErrArgument);
541 User::LeaveIfError(TheScheduler.__DbgMarkEnd(0));
542 CleanupStack::PopAndDestroy(); // data
545 SchSvrHelpers::Pause(TheTest);
551 @SYMTestCaseID SYSLIB-SCHSVR-CT-3227
552 @SYMTestCaseDesc Persistent condition based schedule test
553 @SYMTestPriority High
554 @SYMTestActions Create a single persistent condition based schedule and then
555 terminate the task scheduler.
556 Set the condition and then launch the task scheduler with the
557 condition satisfied. Check that the schedule is executed.
558 @SYMTestExpectedResults Schedule must be executed and test must not panic or fail
561 static void DoTest4L()
563 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3227 "));
564 // create a persistent condition based schedule
566 TheTest.Next(_L("single condition based test"));
569 //reset the p&s variables before creating the schedule
570 ResetVariablesL(0,0,0);
571 TSchedulerItemRef ref1;
572 TheTest.Printf(_L("Create a test schedule\n"));
573 res = CreateScheduleSingle1L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
574 TEST2(res, KErrNone);
577 _LIT(KName1, "shutdown task 1");
578 TheTest.Printf(_L("Schedule a persistant task\n"));
580 res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
581 TEST2(res, KErrNone);
583 //Fault the server to force it to shut down
584 TheScheduler.__FaultServer();
586 //close the Scheduler handle
587 TheScheduler.Close();
589 SchSvrHelpers::Pause(TheTest);
592 res = RProperty::Set(KUidSystemCategory, KTestKey1,10);
593 TEST2(res, KErrNone);
595 //Restart the scheduler with the condition for this persistant task
597 res = TheScheduler.Connect();
598 TEST2(res, KErrNone);
600 //wait for task to be executed
601 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
602 CleanupHelpers::KillProcess(KMinimalTaskHandler);
604 SchSvrHelpers::Pause(TheTest);
610 static TInt RunTestsL()
612 TheTest.Next(_L("Delete old files"));
613 SchSvrHelpers::DeleteScheduleFilesL();
615 TheTest.Next(_L("Create Task notification semaphore"));
616 //initialise task notification semaphore
620 // Connect to the server
621 TheTest.Next(_L("===== Connect to Scheduler ====="));
622 TInt res = TheScheduler.Connect();
623 TEST2(res, KErrNone);
624 // Register a client with the server
625 TheTest.Next(_L("===== Registering Client ====="));
626 res = SchSvrHelpers::RegisterClientL(TheScheduler);
627 TEST2(res, KErrNone);
629 // Launch helper process to create P&S variables
630 CreateTestVariables();
632 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
633 CleanupStack::PushL(scheduler);
634 CActiveScheduler::Install(scheduler);
636 TheTest.Next(_L("Start tests"));
642 TheTest.Next(_L("Tidying up"));
643 CleanupStack::PopAndDestroy(scheduler);
644 //close handle to semaphore
647 //Tidying up so next test will be clear.
648 TheTest.Next(_L("Delete all schedules"));
649 SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
650 SchSvrHelpers::Pause(TheTest, 2);
651 TheTest.Next(_L("Delete old files\n"));
652 SchSvrHelpers::DeleteScheduleFilesL();
654 TheScheduler.Close();
658 GLDEF_C TInt E32Main()
661 TheTest.Start(_L("TC_TSCH_CONDITION"));
663 TheCleanup = CTrapCleanup::New();
665 //If the previous test fails, SCHSVR.exe may stay in memory.
666 TRAPD(error,CleanupHelpers::TestCleanupL());
667 TEST2(error, KErrNone);
668 TheTest(TheFsSession.Connect() == KErrNone);;
669 TRAP(error, RunTestsL());
670 TEST2(error, KErrNone);
671 TRAP(error,CleanupHelpers::TestCleanupL());
672 TEST2(error, KErrNone);
675 TheFsSession.Close();