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