os/ossrv/genericservices/taskscheduler/Test/Conditions/TC_TSCH_CONDITION_HOMETIME.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 #include "TestUtils.h"
    26 
    27 _LIT(KTestName,	"Task Scheduler Condition Scheduling Test - Hometime");
    28 
    29 RTest	TheTest(KTestName);
    30 
    31 typedef CArrayFixFlat<TTaskInfo>			CTaskInfoArray;
    32 typedef CArrayFixFlat<TSchedulerItemRef>    CSchItemRefArray;
    33 typedef CArrayFixFlat<TTaskSchedulerCondition>	CSchConditionArray;
    34 
    35 static RScheduler		TheScheduler;
    36 static CTrapCleanup*	TheCleanup;
    37 static RFs				TheFsSession;
    38 
    39 const TInt KTestKey1 = 1;
    40 const TInt KTestKey2 = 2;
    41 const TInt KTestKey3 = 3;
    42 
    43 _LIT(KMinimalTaskHandler, "MinimalTaskHandler");
    44 _LIT(KSeparator, "|"); // Invalid filepath char used to separate filenames
    45 
    46 // This function launches the TPropertyDefine process which
    47 //	has WriteDeviceData Capabilities enabling it to create the P&S 
    48 //	variables used by this test.
    49 static void LaunchHelperL(TUid aCategory, TInt aKey, TInt aAttr)
    50 	{
    51 	_LIT(KConditionHelper, "TPropertyDefine");
    52 	TRequestStatus stat;
    53 	RProcess p;
    54 	TBuf<32> args;
    55 	args.AppendNum(aCategory.iUid);
    56 	args.Append(KSeparator);
    57 	args.AppendNum(aKey);
    58 	args.Append(KSeparator);
    59 	args.AppendNum(aAttr);
    60 	User::LeaveIfError(p.Create(KConditionHelper, args,EOwnerProcess));
    61 	
    62 	// Asynchronous logon: completes when process terminates with process exit code
    63 	p.Logon(stat);
    64 	p.Resume();
    65 
    66 	User::WaitForRequest(stat);
    67 	TInt exitReason = p.ExitReason();
    68 	p.Close();
    69 	User::LeaveIfError(exitReason);
    70 	}
    71 static void CreateTestVariables()
    72 	{
    73 	LaunchHelperL(KUidSystemCategory, KTestKey1,RProperty::EInt);
    74 	LaunchHelperL(KUidSystemCategory, KTestKey2,RProperty::EInt);
    75 	LaunchHelperL(KUidSystemCategory, KTestKey3,RProperty::EInt);	
    76 	}
    77 
    78 static void ResetVariablesL(TInt aKey1Val,
    79 							TInt aKey2Val,
    80 							TInt aKey3Val)
    81 	{
    82 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,aKey1Val));
    83 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey2,aKey2Val));
    84 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey3,aKey3Val));		
    85 	}
    86 	
    87 static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
    88 						TInt aScheduleId)
    89 	// Extract schedule references from the schedule server based on a filter. If
    90 	{
    91 	aTaskInfoArray.Reset();
    92 	TTsTime defaultTime;
    93 	TScheduleState2 state;
    94 	CSchConditionArray* conditionList 
    95 		= new (ELeave) CSchConditionArray(3);
    96 	CleanupStack::PushL(conditionList);
    97 	TInt res = TheScheduler.GetScheduleL(aScheduleId, 
    98 										state, 
    99 										*conditionList, 
   100 										defaultTime,
   101 										aTaskInfoArray);
   102 	TEST2(res, KErrNone);
   103 	CleanupStack::PopAndDestroy(conditionList);
   104 	}
   105 
   106 static TInt CountTasksL(TInt aScheduleId)	
   107 	{
   108 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
   109 	CleanupStack::PushL(tasks);
   110 	GetTaskInfoL(*tasks, aScheduleId);
   111 	TInt ret = tasks->Count();
   112 	CleanupStack::PopAndDestroy(tasks);
   113 	return ret;
   114 	}
   115 	
   116 
   117 static TInt CountScheduledItemsL(TScheduleFilter aFilter, 
   118 								RScheduler& aScheduler)
   119 	// Extract schedule references from the schedule server based on a filter. If
   120 	{
   121 	CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
   122 	CleanupStack::PushL(refs);
   123 
   124 	TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
   125 	TEST2(res, KErrNone);
   126 
   127 	TInt count = refs->Count();
   128 	CleanupStack::PopAndDestroy(); // refs
   129 	return count;
   130 	}
   131 
   132 static CSchConditionArray* CreateSingleConditionLC(const TUid& aConditionUID,
   133 									TUint aConditionUInt)
   134 	{
   135 	CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
   136 	CleanupStack::PushL(conditionList);
   137 
   138 	{
   139 	TTaskSchedulerCondition condition1;
   140 	condition1.iCategory = aConditionUID;
   141 	condition1.iKey		= aConditionUInt;
   142 	condition1.iState	= 10;
   143 	condition1.iType	= TTaskSchedulerCondition::EEquals;
   144 	conditionList->AppendL(condition1);
   145 	}
   146 	return conditionList;			
   147 	}
   148 
   149 static CSchConditionArray* CreateMultipleConditionsLC()
   150 	{
   151 	CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
   152 	CleanupStack::PushL(conditionList);
   153 	{
   154 	TTaskSchedulerCondition condition1;
   155 	condition1.iCategory = KUidSystemCategory;
   156 	condition1.iKey		= KTestKey1;
   157 	condition1.iState	= 10;
   158 	condition1.iType	= TTaskSchedulerCondition::EEquals;
   159 	conditionList->AppendL(condition1);
   160 	}
   161 	{
   162 	TTaskSchedulerCondition condition2;
   163 	condition2.iCategory = KUidSystemCategory;
   164 	condition2.iKey		= KTestKey2;
   165 	condition2.iState	= 10;
   166 	condition2.iType	= TTaskSchedulerCondition::ENotEquals;
   167 	conditionList->AppendL(condition2);
   168 	}
   169 	{
   170 	TTaskSchedulerCondition condition3;
   171 	condition3.iCategory = KUidSystemCategory;
   172 	condition3.iKey		= KTestKey3;
   173 	condition3.iState	= 10;
   174 	condition3.iType	= TTaskSchedulerCondition::ELessThan;
   175 	conditionList->AppendL(condition3);
   176 	}
   177 	return conditionList;		
   178 	}
   179 	
   180 // single condition with default time set to 1 year in the future
   181 // As this is a valid time a CTimer object actually gets set unlike 
   182 // if Time::TTimeMax() is used, hence we need to test for both cases.
   183 static TInt CreateScheduleSingle1L(TSchedulerItemRef& aRef, 
   184 									RScheduler& aScheduler,
   185 									const TUid& aConditionUID,
   186 									TUint aConditionUInt)
   187 	{
   188 	aRef.iName = _L("Schedule created using CreateScheduleSingle");
   189 
   190 	CSchConditionArray* conditionList 
   191 		= CreateSingleConditionLC(aConditionUID, aConditionUInt);
   192 	TTime ttime(SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1));
   193 	TTsTime time(ttime, EFalse); //1 year in the future
   194 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
   195 	CleanupStack::PopAndDestroy(); // conditionList
   196 	return res;
   197 	}
   198 
   199 // single condition with default time set to Time::MaxTTime()
   200 static TInt CreateScheduleSingle2L(TSchedulerItemRef& aRef, 
   201 									RScheduler& aScheduler,
   202 									const TUid& aConditionUID,
   203 									TUint aConditionUInt)
   204 	{
   205 	aRef.iName = _L("Schedule created using CreateScheduleSingle");
   206 
   207 	CSchConditionArray* conditionList 
   208 		= CreateSingleConditionLC(aConditionUID, aConditionUInt);
   209 	TTime ttime(Time::MaxTTime());
   210 	TTsTime time(ttime, EFalse);
   211 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
   212 	CleanupStack::PopAndDestroy(); // conditionList
   213 	return res;
   214 	}
   215 
   216 // An empty schedule list.
   217 static TInt CreateScheduleEmpty3L(TSchedulerItemRef& aRef, 
   218 									RScheduler& aScheduler,
   219 									const TUid&,
   220 									TUint )
   221 	{
   222 	aRef.iName = _L("Empty Schedule list created");
   223 
   224 	CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(3);
   225 	CleanupStack::PushL(conditionList);
   226 	TTime ttime(SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1));
   227 	TTsTime time(ttime, EFalse); //1 year in the future
   228 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
   229 	CleanupStack::PopAndDestroy(); // conditionList
   230 	
   231 	return res;
   232 	}
   233 
   234 // A null schedule.
   235 static TInt CreateScheduleSingleNull4L(TSchedulerItemRef& aRef, 
   236 									RScheduler& aScheduler,
   237 									const TUid&,
   238 									TUint )
   239 	{
   240 	aRef.iName = _L("One schedule in the list with a NULL uid");
   241 	
   242 	CSchConditionArray* conditionList 
   243 		= CreateSingleConditionLC(KNullUid, 0);
   244 		
   245 	TTime ttime(SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1));
   246 	TTsTime time(ttime, EFalse); //1 year in the future
   247 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
   248 	CleanupStack::PopAndDestroy(); // conditionList
   249 
   250 	return res;
   251 	}
   252 
   253 static TInt CreateScheduleMultipleL(TSchedulerItemRef& aRef, RScheduler& aScheduler)
   254 	{
   255 	aRef.iName = _L("Schedule created using CreateScheduleMultiple");
   256 	
   257 	CSchConditionArray* conditionList = CreateMultipleConditionsLC();
   258 	TTime ttime(SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1));
   259 	TTsTime time(ttime, EFalse); //1 year in the future
   260 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
   261 	CleanupStack::PopAndDestroy(); // conditionList
   262 	return res;
   263 	}
   264 
   265 static TInt SchedulePersistentTaskL(const TDesC& aName, 
   266 									TInt& aNewId, 
   267 									TInt aScheduleId, 
   268 									RScheduler& aScheduler)
   269 	{
   270 	TTaskInfo taskInfo;
   271 	taskInfo.iTaskId = aNewId;
   272 	taskInfo.iName = aName;
   273 	taskInfo.iPriority = 2;
   274 	taskInfo.iRepeat = 0;
   275 	HBufC* data = _L("the data").AllocLC();
   276 	TInt res = aScheduler.ScheduleTask(taskInfo, *data, aScheduleId);
   277 	aNewId = taskInfo.iTaskId;
   278 
   279 	CleanupStack::PopAndDestroy(); // data
   280 	return res;
   281 	}
   282 
   283 /**
   284 @file
   285 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0241
   286 @SYMTestCaseDesc 			Single condition based test - Hometime
   287 @SYMTestPriority 			High
   288 @SYMTestActions  			Create a condition based schedule & check schedule auto deletes
   289 @SYMTestExpectedResults		The test must not fail.
   290 @SYMPREQ					PREQ234
   291 */
   292 static void DoTest1L()
   293 	{
   294 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0241 "));
   295 	// create a simple condition based schedule, and see if it runs.
   296 	TInt res = KErrNone;
   297 	TheTest.Next(_L("single condition based test"));
   298 	__UHEAP_MARK;
   299 	
   300 	//reset the p&s variables before creating the schedule
   301 	ResetVariablesL(0,0,0);
   302 	TSchedulerItemRef ref1;
   303 	TheTest.Printf(_L("Create a schedule\n"));
   304 	res = CreateScheduleSingle1L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   305 	TEST2(res, KErrNone);
   306 	
   307 	TInt task1 = 0;
   308 	_LIT(KName1, "toothpaste");
   309 	TheTest.Printf(_L("Schedule some tasks\n"));
   310 
   311 	res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
   312 	TEST2(res, KErrNone);
   313 	
   314 	res = TheScheduler.__DbgMarkHeap();
   315 	User::LeaveIfError(res); //#1
   316 	TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   317 	TEST(scheduleCount == 1); 
   318 	
   319 	// Check that schedule is of condition type
   320 	TScheduleType scheduleType;
   321 	res = TheScheduler.GetScheduleTypeL(ref1.iHandle, scheduleType);
   322 	TEST2(res, KErrNone);
   323 	TEST(scheduleType == EConditionSchedule );	
   324 
   325 	res = TheScheduler.__DbgMarkEnd(0);
   326 	User::LeaveIfError(res); //#1
   327 	
   328 	TheScheduler.Close();
   329 	SchSvrHelpers::Pause(TheTest);
   330 
   331 	// wait for condition to be satisfied
   332 	res = RProperty::Set(KUidSystemCategory, KTestKey1,10);
   333 	User::LeaveIfError(res);	
   334 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone); 
   335 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   336 	
   337 	res = TheScheduler.Connect();
   338 	TEST2(res, KErrNone);
   339 	// Register a client with the server
   340 	TheTest.Next(_L("===== Registering Client ====="));
   341 	res = SchSvrHelpers::RegisterClientL(TheScheduler);
   342 	TEST2(res, KErrNone);
   343 
   344 	// can't check scheduler to see if any tasks left because it's been
   345 	// deleted as last task has completed
   346 	
   347 	//Check that persistent schedule has auto-deleted
   348 
   349 	TheTest.Printf(_L("DEF46200 - Check schedule has auto deleted\n"));
   350 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   351 	TEST(scheduleCount == 0);
   352 	
   353 	// Attempt to delete auto-deleted schedule, should fail
   354 	 
   355 	TheTest.Printf(_L("DEF46200 - Attempting to delete schedule with id %d\n"), ref1.iHandle);
   356 	res = TheScheduler.DeleteSchedule(ref1.iHandle);
   357 	TEST2(res, KErrNotFound);
   358 	
   359 	// now repeat process with singleschedule2
   360 	ResetVariablesL(0,0,0);
   361 
   362 	TheTest.Printf(_L("Create another schedule\n"));
   363 	res = CreateScheduleSingle2L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   364 	TEST2(res, KErrNone);	
   365 
   366 	TheTest.Printf(_L("Create an empty schedule list\n"));
   367 	res = CreateScheduleEmpty3L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   368 	TEST2(res, KErrArgument);	
   369 
   370 	TheTest.Printf(_L("Create an empty schedule list\n"));
   371 	res = CreateScheduleSingleNull4L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   372 	TEST2(res, KErrArgument);	
   373 
   374 	res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
   375 	TEST2(res, KErrNone);
   376 	SchSvrHelpers::Pause(TheTest);
   377 
   378 	// we should have one outstanding task (without the check in
   379 	// schtimer.cpp specifically for Time::MaxTTime() the timer would have
   380 	// gone off immediately in the past.)
   381 	TEST(CountTasksL(ref1.iHandle) == 1);
   382 
   383 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   384 	TEST(scheduleCount == 1); 
   385 	// wait for condition to be satisfied
   386 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,10));
   387 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone); 
   388 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   389 	
   390 	// can't check scheduler to see if any tasks left because it's been
   391 	// deleted as last task has completed
   392 
   393 	TheTest.Printf(_L("DEF46200 - Check schedule has auto deleted\n"));
   394 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   395 	TEST(scheduleCount == 0); 
   396 	
   397 	// Attempt to delete auto-deleted schedule, should fail
   398 	 
   399 	TheTest.Printf(_L("DEF46200 - Attempting to delete schedule with id %d\n"), ref1.iHandle);
   400 	res = TheScheduler.DeleteSchedule(ref1.iHandle);
   401 	TEST2(res, KErrNotFound);
   402 		
   403 	SchSvrHelpers::Pause(TheTest);
   404 	__UHEAP_MARKEND;
   405 	}
   406 
   407 /**
   408 @file
   409 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0242
   410 @SYMTestCaseDesc 			Multiple condition based test - Hometime
   411 @SYMTestPriority 			High
   412 @SYMTestActions  	    	Create a condition based schedule with multiple entries, and see if it runs.
   413                         	Try auto deleting the last schedule and test for not found error. 
   414 @SYMTestExpectedResults		The test must not fail.
   415 @SYMPREQ					PREQ234
   416 */
   417 static void DoTest2L()
   418 	{
   419 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0242 "));
   420 	// create a condition based schedule with multiple entries, and see if it runs.
   421 	TInt res = KErrNone;
   422 	TheTest.Next(_L("multiple condition based test"));
   423 	__UHEAP_MARK;
   424 	
   425 	//reset the p&s variables before creating the schedule
   426 	ResetVariablesL(0,10,20);
   427 	TSchedulerItemRef ref1;
   428 	TheTest.Printf(_L("Create a schedule\n"));
   429 	res = CreateScheduleMultipleL(ref1, TheScheduler);
   430 	TEST2(res, KErrNone);	
   431 
   432 	TInt task1 = 0;
   433 	_LIT(KName1, "web subscription");
   434 	TheTest.Printf(_L("Schedule some tasks\n"));
   435 	res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
   436 	TEST2(res, KErrNone);
   437 
   438 	TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   439 	TEST(scheduleCount == 1); 
   440 
   441 	// we should have one task scheduled to run
   442 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
   443 	CleanupStack::PushL(tasks);
   444 	GetTaskInfoL(*tasks, ref1.iHandle);
   445 	TEST(tasks->Count() == 1);
   446 	tasks->Reset();
   447 	
   448 	// wait for conditions to be satisfied
   449 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,10));//"=="
   450 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey2,1234));//"!="
   451 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey3,9));//"<"
   452 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone); 
   453 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   454 
   455 	// Can't check schedule for task info because it's gone
   456 
   457 	
   458 	// we should have no schedule, it has auto-deleted
   459 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   460 	TEST(scheduleCount == 0); 
   461 			
   462 	// Reset variables
   463 	TheTest.Printf(_L("Reseting variables"));
   464 	ResetVariablesL(0,10,20);
   465 
   466 	TheTest.Printf(_L("DEF46200 - Attempting to delete schedule with id %d\n"), ref1.iHandle);
   467 	res = TheScheduler.DeleteSchedule(ref1.iHandle);
   468 	TEST2(res, KErrNotFound);
   469 	scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   470 	TEST(scheduleCount == 0); 
   471 	
   472 	CleanupStack::PopAndDestroy(tasks);
   473 	
   474 	SchSvrHelpers::Pause(TheTest);
   475 	__UHEAP_MARKEND;
   476 	}
   477 	
   478 /**
   479 @file
   480 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0243
   481 @SYMTestCaseDesc 			Error checking test - Hometime
   482 @SYMTestPriority 			High
   483 @SYMTestActions  			Create a schedule with a P&S variables that doesnt exist & create a schedule with a P&S variable that isnt an integer.
   484 @SYMTestExpectedResults		The test must not fail.
   485 @SYMPREQ					PREQ234
   486 */
   487 static void DoTest3L()	
   488 	{
   489 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0243 "));
   490 	// create a simple condition based schedule, and see if it runs.
   491 	TInt res = KErrNone;
   492 	TheTest.Next(_L("error checking test"));
   493 	__UHEAP_MARK;
   494 	
   495 	//reset the p&s variables before creating the schedule
   496 	ResetVariablesL(0,0,0);
   497 	TSchedulerItemRef ref1;
   498 	_LIT(KName1, "toothpaste");
   499 
   500 	TheTest.Printf(_L("Create a schedule with a P&S variables that doesnt exist\n"));
   501 	{
   502 	const TUid KNonexistentUid = TUid::Uid(0x01234566);
   503 	res = CreateScheduleSingle1L(ref1, TheScheduler, KNonexistentUid, KTestKey1);
   504 	TEST2(res, KErrNone);
   505 	TheTest.Printf(_L("Schedule some tasks - error should be returned\n"));
   506 
   507 	TTaskInfo taskInfo;
   508 	taskInfo.iName = KName1;
   509 	taskInfo.iPriority = 2;
   510 	taskInfo.iRepeat = 0;
   511 	HBufC* data = _L("the data").AllocLC();
   512 	res = TheScheduler.ScheduleTask(taskInfo, *data, ref1.iHandle);
   513 	// since we have created the schedule using a UID which doesn't exist
   514 	//we should get an error
   515 	TEST2(res, KErrArgument);
   516 	CleanupStack::PopAndDestroy(); // data
   517 	
   518 	TheTest.Printf(_L("Deleting schedule with id %d\n"), ref1.iHandle);
   519 	res = TheScheduler.DeleteSchedule(ref1.iHandle);
   520 	TEST2(res, KErrNone);
   521 	TInt scheduleCount = CountScheduledItemsL(EAllSchedules, TheScheduler);
   522 	TEST(scheduleCount == 0); 
   523 	}
   524 
   525 	TheTest.Printf(_L("Create a schedule\n"));
   526 	res = CreateScheduleSingle1L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   527 	TEST2(res, KErrNone);	
   528 	TheTest.Printf(_L("Schedule some tasks\n"));
   529 	{
   530 	TTaskInfo taskInfo;
   531 	taskInfo.iName = KName1;
   532 	taskInfo.iPriority = 2;
   533 	taskInfo.iRepeat = 1;
   534 	HBufC* data = _L("the data").AllocLC();
   535 	User::LeaveIfError(TheScheduler.__DbgMarkHeap());
   536 	res = TheScheduler.ScheduleTask(taskInfo, *data, ref1.iHandle);
   537 	// since we have set repeat to something other than 0, we should get an error
   538 	TEST2(res, KErrArgument);
   539 	User::LeaveIfError(TheScheduler.__DbgMarkEnd(0));
   540 	CleanupStack::PopAndDestroy(); // data
   541 	}
   542 	
   543 	SchSvrHelpers::Pause(TheTest);
   544 	__UHEAP_MARKEND;
   545 	}
   546 		
   547 /**
   548 @SYMTestCaseID          SYSLIB-SCHSVR-CT-3246
   549 @SYMTestCaseDesc	    Persistent condition based schedule test
   550 @SYMTestPriority 	    High
   551 @SYMTestActions  	    Create a single persistent condition based schedule and then 
   552 						terminate the task scheduler.
   553 						Set the condition and then launch the task scheduler with the 
   554 						condition satisfied.  Check that the schedule is executed.
   555 @SYMTestExpectedResults Schedule must be executed and test must not panic or fail
   556 @SYMREQ                 REQ0000
   557 */
   558 static void DoTest4L()
   559 	{
   560 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3246 "));
   561 	
   562 	// create a persistent condition based schedule
   563 	TInt res = KErrNone;
   564 	TheTest.Next(_L("single condition based test"));
   565 	__UHEAP_MARK;
   566 	
   567 	//reset the p&s variables before creating the schedule
   568 	ResetVariablesL(0,0,0);
   569 	TSchedulerItemRef ref1;
   570 	TheTest.Printf(_L("Create a test schedule\n"));
   571 	res = CreateScheduleSingle1L(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
   572 	TEST2(res, KErrNone);
   573 	
   574 	TInt task1 = 0;
   575 	_LIT(KName1, "shutdown task 1");
   576 	TheTest.Printf(_L("Schedule a persistant task\n"));
   577 
   578 	res = SchedulePersistentTaskL(KName1, task1, ref1.iHandle, TheScheduler);
   579 	TEST2(res, KErrNone);
   580 
   581 	//Fault the server to force it to shut down
   582  	TheScheduler.__FaultServer();
   583 
   584 	//close the Scheduler handle
   585 	TheScheduler.Close();
   586 	
   587 	SchSvrHelpers::Pause(TheTest);
   588 		
   589 	// set condition
   590 	res = RProperty::Set(KUidSystemCategory, KTestKey1,10);
   591 	TEST2(res, KErrNone);
   592 	
   593 	//Restart the scheduler with the condition for this persistant task
   594 	//satisfied
   595 	res = TheScheduler.Connect();
   596 	TEST2(res, KErrNone);	
   597 	
   598 	//wait for task to be executed
   599 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone); 
   600 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   601 	
   602 	SchSvrHelpers::Pause(TheTest);
   603 
   604 	__UHEAP_MARKEND;
   605 	
   606 	}
   607 
   608 
   609 static TInt RunTestsL()
   610 	{
   611 	TheTest.Next(_L("Delete old files"));
   612 	SchSvrHelpers::DeleteScheduleFilesL();
   613 
   614 	TheTest.Next(_L("Create Task notification semaphore"));
   615 	//initialise task notification semaphore
   616 	STaskSemaphore sem;
   617 	sem.CreateL();
   618 
   619 	// Connect to the server
   620 	TheTest.Next(_L("===== Connect to Scheduler ====="));
   621 	TInt res = TheScheduler.Connect();
   622 	TEST2(res, KErrNone);
   623 	// Register a client with the server
   624 	TheTest.Next(_L("===== Registering Client ====="));
   625 	res = SchSvrHelpers::RegisterClientL(TheScheduler);
   626 	TEST2(res, KErrNone);
   627 	
   628 	// Launch helper process to create P&S variables
   629 	CreateTestVariables();
   630 	
   631 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
   632 	CleanupStack::PushL(scheduler);
   633 	CActiveScheduler::Install(scheduler);
   634 	
   635 	TheTest.Next(_L("Start tests"));
   636  	DoTest1L();
   637 	DoTest2L();
   638 	DoTest3L();
   639 	DoTest4L();	
   640 
   641 	TheTest.Next(_L("Tidying up"));
   642 	CleanupStack::PopAndDestroy(scheduler);
   643 	//close handle to semaphore
   644 	sem.Close();
   645 	
   646 	//Tidying up so next test will be clear.
   647 	TheTest.Next(_L("Delete all schedules"));
   648 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
   649 	SchSvrHelpers::Pause(TheTest, 2);
   650 	TheTest.Next(_L("Delete old files\n"));
   651 	SchSvrHelpers::DeleteScheduleFilesL();
   652 
   653 	TheScheduler.Close();
   654 	return KErrNone;
   655 	}
   656 
   657 GLDEF_C TInt E32Main()
   658     {
   659 	__UHEAP_MARK;
   660 	TheTest.Start(_L("TC_TSCH_CONDITION_HOMETIME"));
   661 	TheTest.Title();
   662 	TheCleanup = CTrapCleanup::New();
   663 
   664 	//If the previous test fails, SCHSVR.exe may stay in memory.
   665 	TRAPD(error,CleanupHelpers::TestCleanupL());
   666 	TEST2(error, KErrNone);
   667 	TheTest(TheFsSession.Connect() == KErrNone);;
   668 	TRAP(error, RunTestsL());
   669 	TEST2(error, KErrNone);	
   670 	TRAP(error,CleanupHelpers::TestCleanupL());
   671 	TEST2(error, KErrNone);
   672 	delete TheCleanup;	
   673 	
   674 	TheFsSession.Close();
   675 	TheTest.End();
   676 	TheTest.Close();
   677 	__UHEAP_MARKEND;
   678 
   679 	return KErrNone;
   680 	}