os/ossrv/genericservices/taskscheduler/Test/Conditions/TC_TSCH_CONDITION.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <csch_cli.h>
    17 #include "Thelpers.h"
    18 
    19 #include <e32base.h>
    20 #include <e32test.h>
    21 #include <f32file.h>
    22 #include <s32file.h>
    23 #include <e32property.h>
    24 #include <schinfointernal.h>
    25 
    26 #include "TestUtils.h"
    27 
    28 _LIT(KTestName,	"Task Scheduler Condition Scheduling Test");
    29 
    30 RTest	TheTest(KTestName);
    31 
    32 typedef CArrayFixFlat<TTaskInfo>			CTaskInfoArray;
    33 typedef CArrayFixFlat<TSchedulerItemRef>    CSchItemRefArray;
    34 typedef CArrayFixFlat<TTaskSchedulerCondition>	CSchConditionArray;
    35 
    36 static RScheduler	TheScheduler;
    37 static CTrapCleanup*	TheCleanup;
    38 static RFs			TheFsSession;
    39 
    40 const TInt KTestKey1 = 1;
    41 const TInt KTestKey2 = 2;
    42 const TInt KTestKey3 = 3;
    43 
    44 _LIT(KMinimalTaskHandler, "MinimalTaskHandler");
    45 _LIT(KSeparator, "|"); // Invalid filepath char used to separate filenames
    46 
    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)
    51 	{
    52 	_LIT(KConditionHelper, "TPropertyDefine");
    53 	TRequestStatus stat;
    54 	RProcess p;
    55 	
    56 	TBuf<32> args;
    57 	args.AppendNum(aCategory.iUid);
    58 	args.Append(KSeparator);
    59 	args.AppendNum(aKey);
    60 	args.Append(KSeparator);
    61 	args.AppendNum(aAttr);
    62 	
    63 	User::LeaveIfError(p.Create(KConditionHelper, args,EOwnerProcess));
    64 	
    65 	// Asynchronous logon: completes when process terminates with process exit code
    66 	p.Logon(stat);
    67 	p.Resume();
    68 
    69 	User::WaitForRequest(stat);
    70 	TInt exitReason = p.ExitReason();
    71 	p.Close();
    72 	User::LeaveIfError(exitReason);
    73 	}
    74 	
    75 	
    76 static void CreateTestVariables()
    77 	{
    78 	LaunchHelperL(KUidSystemCategory, KTestKey1,RProperty::EInt);
    79 	LaunchHelperL(KUidSystemCategory, KTestKey2,RProperty::EInt);
    80 	LaunchHelperL(KUidSystemCategory, KTestKey3,RProperty::EInt);	
    81 	}	
    82 
    83 static void ResetVariablesL(TInt aKey1Val,
    84 							TInt aKey2Val,
    85 							TInt aKey3Val)
    86 	{
    87 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,aKey1Val));
    88 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey2,aKey2Val));
    89 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey3,aKey3Val));		
    90 	}
    91 	
    92 static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
    93 						TInt aScheduleId)
    94 	// Extract schedule references from the schedule server based on a filter. If
    95 	{
    96 	aTaskInfoArray.Reset();
    97 	TTime defaultTime;
    98 	TScheduleState state;
    99 	CSchConditionArray* conditionList 
   100 		= new (ELeave) CSchConditionArray(3);
   101 	CleanupStack::PushL(conditionList);
   102 	TInt res = TheScheduler.GetScheduleL(aScheduleId, 
   103 										state, 
   104 										*conditionList, 
   105 										defaultTime,
   106 										aTaskInfoArray);
   107 	TEST2(res, KErrNone);
   108 	CleanupStack::PopAndDestroy(conditionList);
   109 	}
   110 
   111 static TInt CountTasksL(TInt aScheduleId)	
   112 	{
   113 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
   114 	CleanupStack::PushL(tasks);
   115 	GetTaskInfoL(*tasks, aScheduleId);
   116 	TInt ret = tasks->Count();
   117 	CleanupStack::PopAndDestroy(tasks);
   118 	return ret;
   119 	}
   120 	
   121 
   122 static TInt CountScheduledItemsL(TScheduleFilter aFilter, 
   123 								RScheduler& aScheduler)
   124 	// Extract schedule references from the schedule server based on a filter. If
   125 	{
   126 	CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
   127 	CleanupStack::PushL(refs);
   128 
   129 	TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
   130 	TEST2(res, KErrNone);
   131 
   132 	TInt count = refs->Count();
   133 	CleanupStack::PopAndDestroy(); // refs
   134 	return count;
   135 	}
   136 
   137 static CSchConditionArray* CreateSingleConditionLC(const TUid& aConditionUID,
   138 									TUint aConditionUInt)
   139 	{
   140 	CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
   141 	CleanupStack::PushL(conditionList);
   142 
   143 	{
   144 	TTaskSchedulerCondition condition1;
   145 	condition1.iCategory = aConditionUID;
   146 	condition1.iKey		= aConditionUInt;
   147 	condition1.iState	= 10;
   148 	condition1.iType	= TTaskSchedulerCondition::EEquals;
   149 	conditionList->AppendL(condition1);
   150 	}
   151 	return conditionList;
   152 	}
   153 	
   154 static CSchConditionArray* CreateMultipleConditionsLC()
   155 	{
   156 	CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
   157 	CleanupStack::PushL(conditionList);
   158 	{
   159 	TTaskSchedulerCondition condition1;
   160 	condition1.iCategory = KUidSystemCategory;
   161 	condition1.iKey		= KTestKey1;
   162 	condition1.iState	= 10;
   163 	condition1.iType	= TTaskSchedulerCondition::EEquals;
   164 	conditionList->AppendL(condition1);
   165 	}
   166 	{
   167 	TTaskSchedulerCondition condition2;
   168 	condition2.iCategory = KUidSystemCategory;
   169 	condition2.iKey		= KTestKey2;
   170 	condition2.iState	= 10;
   171 	condition2.iType	= TTaskSchedulerCondition::ENotEquals;
   172 	conditionList->AppendL(condition2);
   173 	}
   174 	{
   175 	TTaskSchedulerCondition condition3;
   176 	condition3.iCategory = KUidSystemCategory;
   177 	condition3.iKey		= KTestKey3;
   178 	condition3.iState	= 10;
   179 	condition3.iType	= TTaskSchedulerCondition::ELessThan;
   180 	conditionList->AppendL(condition3);
   181 	}
   182 	return conditionList;		
   183 	}
   184 	
   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)
   192 	{
   193 	aRef.iName = _L("Schedule created using CreateScheduleSingle");
   194 
   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
   200 	return res;
   201 	}
   202 
   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)
   208 	{
   209 	aRef.iName = _L("Schedule created using CreateScheduleSingle");
   210 
   211 	CSchConditionArray* conditionList 
   212 		= CreateSingleConditionLC(aConditionUID, aConditionUInt);
   213 	TTime time = Time::MaxTTime();
   214 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
   215 	CleanupStack::PopAndDestroy(); // conditionList
   216 	return res;
   217 	}
   218 
   219 // An empty schedule list.
   220 static TInt CreateScheduleEmpty3L(TSchedulerItemRef& aRef, 
   221 									RScheduler& aScheduler,
   222 									const TUid&,
   223 									TUint )
   224 	{
   225 	aRef.iName = _L("Empty Schedule list created");
   226 
   227 	CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
   228 	CleanupStack::PushL(conditionList);
   229 
   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
   233 	
   234 	return res;
   235 	}
   236 
   237 // A null schedule.
   238 static TInt CreateScheduleSingleNull4L(TSchedulerItemRef& aRef, 
   239 									RScheduler& aScheduler,
   240 									const TUid&,
   241 									TUint )
   242 	{
   243 	aRef.iName = _L("One schedule in the list with a NULL uid");
   244 	
   245 	CSchConditionArray* conditionList 
   246 		= CreateSingleConditionLC(KNullUid, 0);
   247 		
   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
   251 
   252 	return res;
   253 	}
   254 
   255 static TInt CreateScheduleMultipleL(TSchedulerItemRef& aRef, RScheduler& aScheduler)
   256 	{
   257 	aRef.iName = _L("Schedule created using CreateScheduleMultiple");
   258 	
   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
   263 	return res;
   264 	}
   265 
   266 static TInt SchedulePersistentTaskL(const TDesC& aName, 
   267 									TInt& aNewId, 
   268 									TInt aScheduleId, 
   269 									RScheduler& aScheduler)
   270 	{
   271 	TTaskInfo taskInfo;
   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;
   279 
   280 	CleanupStack::PopAndDestroy(); // data
   281 	return res;
   282 	}
   283 
   284 /**
   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
   292 @SYMREQ                 REQ0000
   293 */
   294 static void DoTest1L()
   295 	{
   296 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1024 "));
   297 	// create a simple condition based schedule, and see if it runs.
   298 	TInt res = KErrNone;
   299 	TheTest.Next(_L("single condition based test"));
   300 	__UHEAP_MARK;
   301 	
   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);
   308 	
   309 	TInt task1 = 0;
   310 	_LIT(KName1, "toothpaste");
   311 	TheTest.Printf(_L("Schedule some tasks\n"));
   312 
   313 	res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
   314 	TEST2(res, KErrNone);
   315 	
   316 	res = TheScheduler.__DbgMarkHeap();
   317 	User::LeaveIfError(res); //#1
   318 	TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   319 	TEST(scheduleCount == 1); 
   320 	
   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 );	
   326 
   327 	res = TheScheduler.__DbgMarkEnd(0);
   328 	User::LeaveIfError(res); //#1
   329 	
   330 	TheScheduler.Close();
   331 	SchSvrHelpers::Pause(TheTest);
   332 
   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);
   338 	
   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);
   345 
   346 	// can't check scheduler to see if any tasks left because it's been
   347 	// deleted as last task has completed
   348 	
   349 	//Check that persistent schedule has auto-deleted
   350 
   351 	TheTest.Printf(_L("DEF46200 - Check schedule has auto deleted\n"));
   352 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   353 	TEST(scheduleCount == 0);
   354 	
   355 	// Attempt to delete auto-deleted schedule, should fail
   356 	 
   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);
   360 	
   361 	// now repeat process with singleschedule2
   362 	ResetVariablesL(0,0,0);
   363 
   364 	TheTest.Printf(_L("Create another schedule\n"));
   365 	res = CreateScheduleSingle2L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   366 	TEST2(res, KErrNone);	
   367 
   368 	TheTest.Printf(_L("Create an empty schedule list\n"));
   369 	res = CreateScheduleEmpty3L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   370 	TEST2(res, KErrArgument);	
   371 
   372 	TheTest.Printf(_L("Create an empty schedule in a list\n"));
   373 	res = CreateScheduleSingleNull4L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   374 	TEST2(res, KErrArgument);	
   375 
   376 	res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
   377 	TEST2(res, KErrNone);
   378 	SchSvrHelpers::Pause(TheTest);
   379 
   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);
   384 
   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);
   391 	
   392 	// can't check scheduler to see if any tasks left because it's been
   393 	// deleted as last task has completed
   394 
   395 	TheTest.Printf(_L("DEF46200 - Check schedule has auto deleted\n"));
   396 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   397 	TEST(scheduleCount == 0); 
   398 	
   399 	// Attempt to delete auto-deleted schedule, should fail
   400 	 
   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);
   404 		
   405 	SchSvrHelpers::Pause(TheTest);
   406 	__UHEAP_MARKEND;
   407 	}
   408 
   409 /**
   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
   416 @SYMREQ                 REQ0000
   417 */
   418 static void DoTest2L()
   419 	{
   420 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1025 "));
   421 	// create a condition based schedule with multiple entries, and see if it runs.
   422 	TInt res = KErrNone;
   423 	TheTest.Next(_L("multiple condition based test"));
   424 	__UHEAP_MARK;
   425 	
   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);	
   432 
   433 	TInt task1 = 0;
   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);
   438 
   439 	TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   440 	TEST(scheduleCount == 1); 
   441 
   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);
   447 	tasks->Reset();
   448 	
   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);
   455 
   456 	// Can't check schedule for task info because it's gone
   457 
   458 	
   459 	// we should have no schedule, it has auto-deleted
   460 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   461 	TEST(scheduleCount == 0); 
   462 			
   463 	// Reset variables
   464 	TheTest.Printf(_L("Reseting variables"));
   465 	ResetVariablesL(0,10,20);
   466 
   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); 
   472 	
   473 	CleanupStack::PopAndDestroy(tasks);
   474 	
   475 	SchSvrHelpers::Pause(TheTest);
   476 	__UHEAP_MARKEND;
   477 	}
   478 
   479 /**
   480 Test 3 does a lot of error checking	
   481 
   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
   487 @SYMREQ                 REQ0000
   488 */	
   489 static void DoTest3L()	
   490 	{
   491 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1026 "));
   492 	// create a simple condition based schedule, and see if it runs.
   493 	TInt res = KErrNone;
   494 	TheTest.Next(_L("error checking test"));
   495 	__UHEAP_MARK;
   496 	
   497 	//reset the p&s variables before creating the schedule
   498 	ResetVariablesL(0,0,0);
   499 	TSchedulerItemRef ref1;
   500 	_LIT(KName1, "toothpaste");
   501 
   502 	TheTest.Printf(_L("Create a schedule with a P&S variables that doesnt exist\n"));
   503 	{
   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"));
   508 
   509 	TTaskInfo taskInfo;
   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
   519 	
   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); 
   525 	}
   526 	
   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"));
   531 	{
   532 	TTaskInfo taskInfo;
   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
   543 	}
   544 	
   545 	SchSvrHelpers::Pause(TheTest);
   546 	__UHEAP_MARKEND;
   547 	}
   548 	
   549 
   550 /**
   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
   559 @SYMREQ                 REQ0000
   560 */
   561 static void DoTest4L()
   562 	{
   563 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3227 "));
   564 	// create a persistent condition based schedule
   565 	TInt res = KErrNone;
   566 	TheTest.Next(_L("single condition based test"));
   567 	__UHEAP_MARK;
   568 	
   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);
   575 	
   576 	TInt task1 = 0;
   577 	_LIT(KName1, "shutdown task 1");
   578 	TheTest.Printf(_L("Schedule a persistant task\n"));
   579 
   580 	res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
   581 	TEST2(res, KErrNone);
   582 
   583 	//Fault the server to force it to shut down
   584  	TheScheduler.__FaultServer();
   585 
   586 	//close the Scheduler handle
   587 	TheScheduler.Close();
   588 	
   589 	SchSvrHelpers::Pause(TheTest);
   590 		
   591 	// set condition
   592 	res = RProperty::Set(KUidSystemCategory, KTestKey1,10);
   593 	TEST2(res, KErrNone);
   594 	
   595 	//Restart the scheduler with the condition for this persistant task
   596 	//satisfied
   597 	res = TheScheduler.Connect();
   598 	TEST2(res, KErrNone);	
   599 	
   600 	//wait for task to be executed
   601 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone); 
   602 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   603 	
   604 	SchSvrHelpers::Pause(TheTest);
   605 
   606 	__UHEAP_MARKEND;
   607 	
   608 	}
   609 
   610 static TInt RunTestsL()
   611 	{
   612 	TheTest.Next(_L("Delete old files"));
   613 	SchSvrHelpers::DeleteScheduleFilesL();
   614 
   615 	TheTest.Next(_L("Create Task notification semaphore"));
   616 	//initialise task notification semaphore
   617 	STaskSemaphore sem;
   618 	sem.CreateL();
   619 
   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);
   628 	
   629 	// Launch helper process to create P&S variables
   630 	CreateTestVariables();
   631 	
   632 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
   633 	CleanupStack::PushL(scheduler);
   634 	CActiveScheduler::Install(scheduler);
   635 	
   636 	TheTest.Next(_L("Start tests"));
   637  	DoTest1L();
   638 	DoTest2L();
   639 	DoTest3L();
   640 	DoTest4L();
   641 
   642 	TheTest.Next(_L("Tidying up"));
   643 	CleanupStack::PopAndDestroy(scheduler);
   644 	//close handle to semaphore
   645 	sem.Close();
   646 	
   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();
   653 
   654 	TheScheduler.Close();
   655 	return KErrNone;
   656 	}
   657 
   658 GLDEF_C TInt E32Main()
   659     {
   660 	__UHEAP_MARK;
   661 	TheTest.Start(_L("TC_TSCH_CONDITION"));
   662 	TheTest.Title();
   663 	TheCleanup = CTrapCleanup::New();
   664 
   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);
   673 	delete TheCleanup;	
   674 	
   675 	TheFsSession.Close();
   676 	TheTest.End();
   677 	TheTest.Close();
   678 	__UHEAP_MARKEND;
   679 
   680 	return KErrNone;
   681 	}