os/ossrv/genericservices/taskscheduler/Test/Scheduling/TC_TSCH_SCHEDULING1_HOMETIME.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <e32base.h>
    17 #include <e32test.h>
    18 #include <csch_cli.h>
    19 #include <f32file.h>
    20 #include "Thelpers.h"
    21 
    22 #include "TestUtils.h"
    23 
    24 // Globals
    25 RTest					TheTest(_L("TC_TSCH_SCHEDULING1 - Hometime"));
    26 static RScheduler		TheScheduler;
    27 static RFs				TheFsSession;
    28 
    29 typedef CArrayFixFlat<TScheduleEntryInfo2>	CScheduleEntryInfoArray;
    30 typedef CArrayFixFlat<TTaskInfo>			CTaskInfoArray;
    31 typedef CArrayFixFlat<TSchedulerItemRef>    CSchItemRefArray;
    32 
    33 _LIT(KMinimalTaskHandler, "MinimalTaskHandler");
    34 
    35 _LIT(KTimeFormatString,						"%-B%:0%J%:1%T%:2%S%.%*C4%:3%+B");
    36 _LIT(KCurrentDateTimeChanged,				"Date & Time changed to: [%S]\n");
    37 
    38 //***********************************************************************************
    39 
    40 // Sets time to before specified time by aTimeBeforeInSeconds
    41 static void SetTimeBeforeL(RTest& aTest, TTsTime& aTime, TInt aTimeBeforeInSeconds)
    42 	{
    43 	TTimeIntervalSeconds secs(aTimeBeforeInSeconds);
    44 	TTime time = aTime.GetLocalTime()-secs;	
    45 	SchSvrHelpers::SetHomeTimeL(time);
    46 	TBuf<30> dateString;
    47 	time.FormatL(dateString, KTimeFormatString);
    48 	aTest.Printf(KCurrentDateTimeChanged, &dateString);
    49 	}
    50 
    51 // gets the due time for this schedule
    52 static TTsTime GetDueTimeL(TInt aScheduleId)
    53 	{
    54 	TTsTime nextTimeScheduleIsDue;
    55 	TScheduleState2 state;
    56 	CScheduleEntryInfoArray* entries 
    57 		= new (ELeave) CScheduleEntryInfoArray(3);
    58 	CleanupStack::PushL(entries);
    59 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
    60 	CleanupStack::PushL(tasks);
    61 
    62 	TInt res = TheScheduler.GetScheduleL(aScheduleId, state, *entries, *tasks, nextTimeScheduleIsDue);
    63 	TEST2(res, KErrNone);
    64 
    65 	CleanupStack::PopAndDestroy(2); // entries, tasks
    66 	
    67 	return state.DueTime();
    68 	}
    69 	
    70 // Forces the task to be exectued aCount times.
    71 static void ForceTaskExecutionForSpecifiedIdL(TInt aId, TInt aCount)
    72 	{
    73 	TheTest.Printf(_L("Executing %d times\n"), aCount);
    74 	TTsTime time;
    75 	for (TInt i=0; i<aCount; ++i)
    76 		{
    77 		TheTest.Printf(_L("Execution %d\n"), i+1);
    78 		time = GetDueTimeL(aId);		
    79 		
    80 		SetTimeBeforeL(TheTest, time, 5 /*seconds*/);		
    81 		
    82 		// Wait for notification that schedule has executed.
    83 		TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
    84 		CleanupHelpers::KillProcess(KMinimalTaskHandler);
    85 		}
    86 	}
    87 
    88 //creates a daily schedule with StartTime of aStartTime
    89 static TInt CreateScheduleL(TSchedulerItemRef& aRef, 
    90 							RScheduler& aScheduler, 
    91 							const TTsTime& aStartTime)
    92 	{
    93 	CScheduleEntryInfoArray* entryList 
    94 		= new (ELeave) CScheduleEntryInfoArray(1);
    95 	CleanupStack::PushL(entryList);
    96 	TScheduleEntryInfo2 entry1 (aStartTime, EDaily, 1, 30);
    97 	entryList->AppendL(entry1);
    98 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
    99 	CleanupStack::PopAndDestroy(); // entryList
   100 	return res;
   101 	}
   102 
   103 // counts the number of scheduled items based on the supplied filter.
   104 static TInt CountScheduledItemsL(TScheduleFilter aFilter, 
   105 								RScheduler& aScheduler)
   106 	// Extract schedule references from the schedule server based on a filter. If
   107 	{
   108 	CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
   109 	CleanupStack::PushL(refs);
   110 
   111 	TInt res = aScheduler.GetScheduleRefsL(*refs, aFilter);
   112 	TEST2(res, KErrNone);
   113 
   114 	TInt count = refs->Count();
   115 	CleanupStack::PopAndDestroy(); // refs
   116 	return count;
   117 	}	
   118 
   119 // Extract task references from the schedule server based on a ID
   120 static void GetTaskInfoL(CTaskInfoArray& aTaskInfoArray,
   121 						TInt aScheduleId)
   122 	{
   123 	aTaskInfoArray.Reset();
   124 	TTsTime nextTimeScheduleIsDue;
   125 	TScheduleState2 state;
   126 	CScheduleEntryInfoArray* entries 
   127 		= new (ELeave) CScheduleEntryInfoArray(3);
   128 	CleanupStack::PushL(entries);
   129 	TInt res = TheScheduler.GetScheduleL(aScheduleId, 
   130 										state, 
   131 										*entries, 
   132 										aTaskInfoArray, 
   133 										nextTimeScheduleIsDue);
   134 	TEST2(res, KErrNone);
   135 	CleanupStack::PopAndDestroy(entries);
   136 	}
   137 
   138 // schedules a transient task	
   139 static TInt ScheduleTransientTaskL(TInt& aTaskId, 
   140 								TSchedulerItemRef& aRef, 
   141 								TInt aRepeat, 
   142 								RScheduler& aScheduler)
   143 	{
   144 	CScheduleEntryInfoArray* entryList = new(ELeave) CScheduleEntryInfoArray(3);
   145 	CleanupStack::PushL(entryList);
   146 
   147 	aRef.iName = _L("transient one");
   148 
   149 	// SCHEDULES
   150 	TTime ttime1(SchSvrHelpers::TimeBasedOnOffset(0, 1));
   151 	TTsTime startTime1 (ttime1,EFalse); // 1 min in the future
   152 	TScheduleEntryInfo2 entry1 (startTime1, EDaily, 1, 20);
   153 	entryList->AppendL(entry1);
   154 	TTime ttime2(SchSvrHelpers::TimeBasedOnOffset(0, 2));
   155 	TTsTime startTime2 (ttime2,EFalse); // 2 min in the future
   156 	TScheduleEntryInfo2 entry2 (startTime2, EDaily, 1, 500);
   157 	entryList->AppendL(entry2);
   158 
   159 	TTime ttime3(SchSvrHelpers::TimeBasedOnOffset(0, 3));
   160 	TTsTime startTime3 (ttime3,EFalse); // 3 min in the future
   161 	TScheduleEntryInfo2 entry3 (startTime3, EDaily, 1, 5);
   162 	entryList->AppendL(entry3);
   163 
   164 	// TASK
   165 	TTaskInfo taskInfo;
   166 	taskInfo.iName = _L("mail");
   167 	taskInfo.iTaskId = aTaskId;
   168 	taskInfo.iRepeat = aRepeat;
   169 	taskInfo.iPriority = 2;
   170 	HBufC* data = _L("the data, some strange new name ").AllocLC();
   171 
   172 	// Schedule the item
   173 	TInt res = aScheduler.ScheduleTask(taskInfo, *data, aRef, *entryList);
   174 	CleanupStack::PopAndDestroy(2); // data, entryList
   175 
   176 	aTaskId = taskInfo.iTaskId;
   177 	return res;
   178 	}
   179 
   180 
   181 //***********************************************************************************
   182 
   183 /**
   184 @file
   185 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0251
   186 @SYMTestCaseDesc 			Replicated test for for defect (EDNEMHE-4Q69BG) - Hometime
   187 @SYMTestPriority 			High
   188 @SYMTestActions  			Create time based schedules and then jump to a time after the due time but within the validity period and check it schedule still fires
   189 @SYMTestExpectedResults		The test must not fail.
   190 @SYMPREQ					PREQ234
   191 */
   192 static void Test1L()
   193 	{
   194 	
   195 	_LIT(KTaskData1, "This is some really exciting task data (number 1)");
   196 	_LIT(KTaskData2, "This is some really exciting task data (number 2)");
   197 	_LIT(KTestName,	"SmsTest");
   198 
   199 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0251 TheTest3: SMS:Sending to multiple recipients "));
   200 	
   201 	TheTest.Next(_L("Connect to Scheduler"));
   202 	TInt res = TheScheduler.Connect();
   203 	TEST2(res, KErrNone);
   204 
   205 	TheTest.Next(_L("Registering Client"));
   206 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   207 
   208 	// Set the time to a known value, since this makes testing much easier (and more
   209 	// repeatable).	
   210 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
   211 
   212 	// This is the time when we want the schedule to fire
   213 	TDateTime datetime(2000, EJanuary, 1, 10, 0, 0, 0);
   214 	TTsTime startTimeForSchedule(datetime, EFalse); // 10:00 am
   215 
   216 
   217 	// Prepare a schedule describing when we want the tasks to run (10:00 am)
   218 	TSchedulerItemRef ref;
   219 	User::LeaveIfError(CreateScheduleL(ref, TheScheduler, startTimeForSchedule));
   220 
   221 	// Disable the schedule whilst we set it up
   222 	User::LeaveIfError(TheScheduler.DisableSchedule(ref.iHandle));
   223 
   224 	// Associate a task with the schedule
   225 	TTaskInfo taskInfo1;
   226 	taskInfo1.iRepeat = 0;
   227 	taskInfo1.iName = KTestName;
   228 	taskInfo1.iPriority = 2;
   229 
   230 	// Create some data associated with this task
   231 	HBufC* taskData1 = KTaskData1().AllocLC();
   232 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo1, *taskData1, ref.iHandle));
   233 	CleanupStack::PopAndDestroy(); // taskData1
   234 
   235 	// Associate a task (2) with the schedule
   236 	TTaskInfo taskInfo2;
   237 	taskInfo2.iRepeat = 0;
   238 	taskInfo2.iName = KTestName;
   239 	taskInfo2.iPriority = 2;
   240 
   241 	// Create some data associated with this task
   242 	HBufC* taskData2 = KTaskData2().AllocLC();
   243 	User::LeaveIfError(TheScheduler.ScheduleTask(taskInfo2, *taskData2, ref.iHandle));
   244 	CleanupStack::PopAndDestroy(); // taskData2
   245 
   246 	// We should now have two tasks scheduled at exactly the same time...
   247 	User::LeaveIfError(TheScheduler.EnableSchedule(ref.iHandle));
   248 
   249 	// Set the time to 5 minutes *AFTER* the schedule was due to run (10:05am). In this instance,
   250 	// based on the new fixed Schedule Server, the schedule should still execute since 
   251 	// it falls within the validity period (30 mins), however, in the old scheme of things,
   252 	// the schedule would not be valid again until tomorrow (2/1/2000) at 10:00am and hence
   253 	// would not execute until then.	
   254 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 10, 5, 0, 0))); // 10:05 am
   255 
   256 	// Now wait for something to happen...
   257 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
   258 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   259 	}
   260 
   261 //***********************************************************************************
   262 
   263 /**
   264 @file
   265 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0252
   266 @SYMTestCaseDesc 			Replicated test for for defect (EDNEMHE-4Q69BG) - Hometime
   267 @SYMTestPriority 			High
   268 @SYMTestActions  			Create time based schedules and then jump to a time after the due time but within the validity period and check it schedule still fires
   269 @SYMTestExpectedResults		The test must not fail.
   270 @SYMPREQ					PREQ234
   271 */
   272 static void Test2L()
   273 	{	
   274 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0252 Testing creation of transient schedule with task repeating 5 times "));
   275 
   276 	TInt tTask = 0;
   277 	TSchedulerItemRef ref;
   278 	// schedule has 3 entries - +1min, +2min and +3min
   279 	TInt res = ScheduleTransientTaskL(tTask, ref, 5, TheScheduler);//5 repeats
   280 	TEST2(res, KErrNone);
   281 	
   282 	TheTest.Printf(_L("Get Task count and repeat count\n"));
   283 
   284 	CTaskInfoArray* tasks = new (ELeave) CTaskInfoArray(3);
   285 	CleanupStack::PushL(tasks);
   286 	GetTaskInfoL(*tasks, ref.iHandle);
   287 	TEST(tasks->Count() == 1);
   288 	TTaskInfo info = tasks->At(0);
   289 	TEST(info.iRepeat == 5);
   290 	ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 3);
   291 	GetTaskInfoL(*tasks, ref.iHandle);
   292 	TEST(tasks->Count() == 1);
   293 	info = tasks->At(0);
   294 	TEST(info.iRepeat == 2); // still 2 repeats to go.
   295 	ForceTaskExecutionForSpecifiedIdL(ref.iHandle, 2);
   296 
   297 	CleanupStack::PopAndDestroy(tasks);
   298 	
   299 	TInt scheduleCount = CountScheduledItemsL(EPendingSchedules, TheScheduler);
   300 	// There should be no schedules as its a transient one and last schedule
   301 	// should have deleted itself.
   302 	TEST(scheduleCount == 0); 
   303 	SchSvrHelpers::Pause(TheTest);
   304 	}
   305 
   306 //**********************************************************************************
   307 
   308 /**
   309 @file
   310 @SYMTestCaseID				SYSLIB-SCHSVR-CT-0253
   311 @SYMTestCaseDesc 			Replicated test for for defect (DEF055586L) - Hometime
   312 @SYMTestPriority 			High
   313 @SYMTestActions  			Create a time-based schedule check that there are 0 entries in the scehule after it fires
   314 @SYMTestExpectedResults		The test must not fail.
   315 @SYMPREQ					PREQ234
   316 */
   317 static void DEF055586L()
   318 	{
   319 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0253 DEF055586 - Last element in array missed by loop "));
   320 		
   321 	TheTest.Next(_L("Connect to Scheduler"));
   322 	TInt res = TheScheduler.Connect();
   323 	TEST2(res, KErrNone);
   324 
   325 	TheTest.Next(_L("Registering Client"));
   326 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   327 
   328 	// Set the time to a known value, since this makes testing much easier (and more
   329 	// repeatable).	
   330 	SchSvrHelpers::SetHomeTimeL(TTime(TDateTime(2000, EJanuary, 1, 9, 55, 0, 0))); // 9:55 am
   331 
   332 	// This is the time when we want the schedule to fire
   333 	TDateTime datetime(2000, EJanuary, 1, 10, 0, 0, 0);
   334 	TTsTime startTimeForSchedule(datetime, EFalse); // 10:00 am
   335 
   336 	// Prepare a schedule describing when we want the tasks to run (10:00 am)
   337 	TSchedulerItemRef ref;
   338 
   339 	CScheduleEntryInfoArray* entryList = new (ELeave) CScheduleEntryInfoArray(1);
   340 	CleanupStack::PushL(entryList);
   341 
   342 	TScheduleEntryInfo2 entry1 (startTimeForSchedule, EDaily, 0, 30);
   343 	entryList->AppendL(entry1);
   344 	TInt err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
   345 
   346 	TEST2(err, KErrArgument);
   347 
   348 	TheTest.Next(_L("DEF055586 - Now checking 0 entries in schedule"));
   349 	entryList->Reset();
   350 	
   351 	err = TheScheduler.CreatePersistentSchedule(ref, *entryList);
   352 	
   353 	TEST2(err, KErrArgument);
   354 	
   355 	CleanupStack::PopAndDestroy(); // entryList
   356 	}
   357 	
   358 /**
   359 @SYMTestCaseID          SYSLIB-SCHSVR-CT-3361
   360 @SYMTestCaseDesc	    Replicated test for for defect (INC098909) - Hometime  
   361 @SYMTestPriority 	    High
   362 @SYMTestActions  	    Mark heap of Scheduler then create a schedule & task wait for its 
   363 						execution then check heap again for memory leaks
   364 @SYMTestExpectedResults Test must not fail (i.e. No memory leaks)
   365 @SYMDEF                INC098909: Process !TaskScheluder leaks memory in mail polls.
   366 */
   367 static void INC098909()
   368 	{
   369 	const TInt KTimeToWait = 10*1000*1000; 
   370 	SchSvrHelpers::DeleteScheduleFilesL();
   371 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3361 INC098909: Process !TaskScheluder leaks memory in mail polls "));
   372 	TheTest.Next(_L("Connect to Scheduler"));
   373 	TEST2(TheScheduler.Connect(),KErrNone);
   374 
   375 	TheTest.Next(_L("Registering Client"));
   376 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   377 	
   378 	User::LeaveIfError(TheScheduler.__DbgMarkHeap());
   379 
   380 	//Create Schedule
   381 	CArrayFixFlat<TScheduleEntryInfo2>* entryList = new(ELeave) CArrayFixFlat<TScheduleEntryInfo2>(1);
   382 	CleanupStack::PushL(entryList);
   383 	TSchedulerItemRef ref;
   384 	
   385 	TTime ttime(SchSvrHelpers::TimeBasedOnOffset(0));
   386 	TTsTime startTime1(ttime, EFalse); 
   387 
   388 	TScheduleEntryInfo2 entry1 (startTime1, EDaily, 1, 20);
   389 	entryList->AppendL(entry1);
   390 	
   391 	// Create the schedule for the task...
   392 	TEST2(TheScheduler.CreatePersistentSchedule(ref, *entryList),KErrNone);
   393 	
   394 	//Create Task
   395 	TTaskInfo task;
   396 	task.iRepeat	= 1; // repeat once
   397 	task.iName		= _L("Test ");
   398 	task.iPriority	= 100;
   399 	
   400 	HBufC* taskData = HBufC::NewLC(1);
   401 	TEST2(TheScheduler.ScheduleTask(task, *taskData,ref.iHandle), KErrNone);
   402 	
   403 	CleanupStack::PopAndDestroy(taskData);
   404 	CleanupStack::PopAndDestroy(entryList);	
   405 	
   406 	//Wait schedule to complete
   407 	User::After(KTimeToWait);
   408 
   409 	User::LeaveIfError(TheScheduler.__DbgMarkEnd(0));
   410 	
   411 	// really clean out the scheduler (get rid of all the files and process)
   412 	SchSvrHelpers::DeleteScheduleFilesL();
   413 	CleanupHelpers::KillProcess(KMinimalTaskHandler);
   414 	// Now wait for something to happen...
   415 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);	
   416 	}
   417 
   418 
   419 GLDEF_C TInt DoTheTestsL()
   420 	{
   421 	//Delete old files.
   422 	SchSvrHelpers::DeleteScheduleFilesL();
   423 	
   424 	STaskSemaphore sem;
   425 	sem.CreateL();
   426 	
   427 	TheTest.Next(_L("Start tests"));
   428 	// Add tests here:-
   429 	Test1L();
   430 	Test2L();
   431 	DEF055586L();
   432 	INC098909();
   433 	
   434 	sem.Close();
   435 	
   436 	//Tidying up so next test will be clear.
   437 	TheTest.Next(_L("Delete all schedules"));
   438 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
   439 	SchSvrHelpers::Pause(TheTest, 2);
   440 	TheTest.Next(_L("Delete old files\n"));
   441 	SchSvrHelpers::DeleteScheduleFilesL();
   442 
   443 	TheScheduler.Close();
   444 
   445 	return KErrNone;
   446 	}
   447 
   448 //***********************************************************************************
   449 GLDEF_C TInt E32Main()
   450     {
   451 	__UHEAP_MARK;
   452 	TheTest.Title();
   453 	TheTest.Start(_L("TC_TSCH_SCHEDULING1 - Hometime"));
   454 
   455 	TInt error = KErrNone;
   456 	CTrapCleanup* cleanup = CTrapCleanup::New();
   457 	if	(!cleanup)
   458 		return KErrNoMemory;
   459 	//If the previous test fails, SCHSVR.exe may stay in memory.
   460 	TRAP(error,CleanupHelpers::TestCleanupL());
   461 	TEST2(error, KErrNone);
   462 
   463 	TEST2(TheFsSession.Connect(), KErrNone);
   464 	TheTest.Next(_L("Do the tests"));
   465 	TRAP(error, DoTheTestsL());
   466 	TEST2(error,KErrNone);
   467 	TheFsSession.Close();
   468 	TRAP(error,CleanupHelpers::TestCleanupL());
   469 	TEST2(error, KErrNone);
   470 	delete cleanup;	
   471 
   472 	TheTest.End();
   473 	TheTest.Close();
   474 	__UHEAP_MARKEND;
   475 	return KErrNone;
   476 	}