Update contrib.
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
22 _LIT(KPauseMessage, "=== Pausing for %d seconds ===\n");
25 // Helper class for Scheduler Server test code
28 EXPORT_C TBool SchSvrHelpers::IsTimeTheSame(const TTime& aTime1, const TTime& aTime2)
30 // Just compares the time, not the date
31 TDateTime time1 = aTime1.DateTime();
32 TDateTime time2 = aTime2.DateTime();
33 return ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
36 EXPORT_C TBool SchSvrHelpers::IsDateTheSame(const TTime& aTime1, const TTime& aTime2)
38 // Just compares the date not the time
39 TDateTime time1 = aTime1.DateTime();
40 TDateTime time2 = aTime2.DateTime();
41 return ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
44 EXPORT_C TBool SchSvrHelpers::IsTaskInfoTheSame(const TTaskInfo& aTaskInfo1, const TTaskInfo& aTaskInfo2)
46 if ( aTaskInfo1.iRepeat == aTaskInfo2.iRepeat && aTaskInfo1.iTaskId == aTaskInfo2.iTaskId
47 && aTaskInfo1.iName == aTaskInfo2.iName && aTaskInfo1.iPriority == aTaskInfo2.iPriority)
52 EXPORT_C TBool SchSvrHelpers::IsItemRefTheSame(const TSchedulerItemRef& aItemRef1, const TSchedulerItemRef& aItemRef2)
54 if (aItemRef1.iHandle == aItemRef2.iHandle && aItemRef1.iName == aItemRef2.iName)
59 EXPORT_C TBool SchSvrHelpers::IsScheduleStateTheSame(const TScheduleState& aScheduleState1, const TScheduleState& aScheduleState2)
61 if ( aScheduleState1.iDueTime == aScheduleState2.iDueTime && aScheduleState1.iPersists == aScheduleState2.iPersists
62 && aScheduleState1.iName == aScheduleState2.iName && aScheduleState1.iEnabled == aScheduleState2.iEnabled)
67 EXPORT_C TScheduleEntryInfo SchSvrHelpers::RandomScheduleEntryInfo(TInt64& aSeed)
69 TScheduleEntryInfo entryInfo;
70 entryInfo.iIntervalType = STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed));
71 entryInfo.iStartTime = TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed));
72 entryInfo.iInterval = Rand(1, 100, aSeed);
73 entryInfo.iValidityPeriod = Rand(1, 100, aSeed);
77 EXPORT_C TScheduleEntryInfo SchSvrHelpers::ScheduleEntryInfo(TIntervalType aType, const TTime& aStartTime, TInt aInterval, TTimeIntervalMinutes aValidPeriod)
79 TScheduleEntryInfo info;
80 info.iIntervalType = aType;
81 info.iStartTime = aStartTime;
82 info.iInterval = aInterval;
83 info.iValidityPeriod = aValidPeriod;
87 EXPORT_C TTaskInfo SchSvrHelpers::TaskInfo(const TDesC& aName, TInt aPriority, TInt aRepeat)
90 return TTaskInfo(0, name, aPriority, aRepeat); // zero the id for now (returned by server)
93 // Generate a random number based upon a seed and a range
94 EXPORT_C TInt SchSvrHelpers::Rand(const TInt aLow, const TInt aHigh, TInt64& aSeed)
96 TReal initialRand = (Math::FRand(aSeed) * (aHigh - aLow));
99 // Round to 0 decimal places, ie. the nearest whole numer
100 Math::Round(initialRand, initialRand, 0);
101 Math::Int(rand, initialRand);
104 return (aLow + rand);
107 // Pause execution for the specified number of seconds
108 EXPORT_C void SchSvrHelpers::Pause(RTest& aTest, TInt aPauseAmount)
110 aTest.Printf(KPauseMessage, aPauseAmount);
111 User::After(KOneSecond * aPauseAmount); // default is 2 seconds
114 // To be used by Hometime based schedules
115 EXPORT_C TDateTime SchSvrHelpers::TimeBasedOnOffset(TInt aSeconds, TInt aMinutes, TInt aHours, TInt aDays, TInt aMonths, TInt aYears)
119 now += TTimeIntervalSeconds(aSeconds);
120 now += TTimeIntervalMinutes(aMinutes);
121 now += TTimeIntervalHours(aHours);
122 now += TTimeIntervalDays(aDays);
123 now += TTimeIntervalMonths(aMonths);
124 now += TTimeIntervalYears(aYears);
125 return now.DateTime();
128 // To be used by UTC time based schedules
129 EXPORT_C TDateTime SchSvrHelpers::UtcTimeBasedOnOffset(TInt aSeconds, TInt aMinutes, TInt aHours, TInt aDays, TInt aMonths, TInt aYears)
133 now += TTimeIntervalSeconds(aSeconds);
134 now += TTimeIntervalMinutes(aMinutes);
135 now += TTimeIntervalHours(aHours);
136 now += TTimeIntervalDays(aDays);
137 now += TTimeIntervalMonths(aMonths);
138 now += TTimeIntervalYears(aYears);
139 return now.DateTime();
142 // Check task file left
143 // It creates a separate process to do this as file to access in in private
144 // data cage which requires all files capability
145 EXPORT_C TInt SchSvrHelpers::CheckTaskFilesL()
147 _LIT(KTaskFileChecker, "TTaskFileChecker");
150 User::LeaveIfError(p.Create(KTaskFileChecker, KNullDesC));
151 // Asynchronous logon: completes when process terminates with process exit code
155 User::WaitForRequest(stat);
156 TInt exitReason = p.ExitReason();
159 // return exit reasons as checking result
164 // Create task files for testing
165 // It creates a separate process to do this as file to access in in private
166 // data cage which requires all files capability
167 EXPORT_C TInt SchSvrHelpers::CreateTaskFilesL()
169 _LIT(KTaskFileCreator, "TTaskFileCreator");
172 User::LeaveIfError(p.Create(KTaskFileCreator, KNullDesC));
173 // Asynchronous logon: completes when process terminates with process exit code
177 User::WaitForRequest(stat);
178 TInt exitReason = p.ExitReason();
181 // return exit reasons as checking result
185 // Start Task Scheduler
186 // It creates a separate process to do this as need to set appropriate SID
187 // to enable task scheduler to be started
188 EXPORT_C TInt SchSvrHelpers::LaunchTaskSchedulerL()
190 _LIT(KTaskSchedulerLauncher, "TTaskSchedulerLauncher");
193 User::LeaveIfError(p.Create(KTaskSchedulerLauncher, KNullDesC));
194 // Asynchronous logon: completes when process terminates with process exit code
198 User::WaitForRequest(stat);
199 TInt exitReason = p.ExitReason();
202 // return exit reasons as checking result
208 // PREQ234 support //
210 EXPORT_C TBool SchSvrHelpers::IsTimeTheSame(const TTsTime& aTime1, const TTsTime& aTime2)
212 // Just compares the time, not the date
215 if (aTime1.IsUtc() && aTime2.IsUtc())
217 TDateTime time1 = aTime1.GetUtcTime().DateTime();
218 TDateTime time2 = aTime2.GetUtcTime().DateTime();
219 ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
221 if (!aTime1.IsUtc() && !aTime2.IsUtc())
223 TDateTime time1 = aTime1.GetLocalTime().DateTime();
224 TDateTime time2 = aTime2.GetLocalTime().DateTime();
225 ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
231 EXPORT_C TBool SchSvrHelpers::IsTimeTheSameNoSeconds(const TTsTime& aTime1, const TTsTime& aTime2)
233 // Just compares the time up to hours and minutes
236 if (aTime1.IsUtc() && aTime2.IsUtc())
238 TDateTime time1 = aTime1.GetUtcTime().DateTime();
239 TDateTime time2 = aTime2.GetUtcTime().DateTime();
240 ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()));
242 if (!aTime1.IsUtc() && !aTime2.IsUtc())
244 TDateTime time1 = aTime1.GetLocalTime().DateTime();
245 TDateTime time2 = aTime2.GetLocalTime().DateTime();
246 ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()));
251 EXPORT_C TBool SchSvrHelpers::IsDateTheSame(const TTsTime& aTime1, const TTsTime& aTime2)
253 // Just compares the date not the time
256 if (aTime1.IsUtc() && aTime2.IsUtc())
258 TDateTime time1 = aTime1.GetUtcTime().DateTime();
259 TDateTime time2 = aTime2.GetUtcTime().DateTime();
260 ret = ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
262 if (!aTime1.IsUtc() && !aTime2.IsUtc())
264 TDateTime time1 = aTime1.GetLocalTime().DateTime();
265 TDateTime time2 = aTime2.GetLocalTime().DateTime();
266 ret = ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
272 EXPORT_C TBool SchSvrHelpers::IsScheduleStateTheSame(const TScheduleState2& aScheduleState1, const TScheduleState2& aScheduleState2)
274 if (aScheduleState1.DueTime().IsUtc() && aScheduleState2.DueTime().IsUtc())
276 TTime dueTime1 = aScheduleState1.DueTime().GetUtcTime();
277 TTime dueTime2 = aScheduleState2.DueTime().GetUtcTime();
279 if (aScheduleState1.Persists() == aScheduleState2.Persists() && aScheduleState1.Name() == aScheduleState2.Name()
280 && aScheduleState1.Enabled() == aScheduleState2.Enabled() && dueTime1 == dueTime2)
284 if (!aScheduleState1.DueTime().IsUtc() && !aScheduleState2.DueTime().IsUtc())
286 TTime dueTime1 = aScheduleState1.DueTime().GetLocalTime();
287 TTime dueTime2 = aScheduleState2.DueTime().GetLocalTime();
289 if (aScheduleState1.Persists() == aScheduleState2.Persists() && aScheduleState1.Name() == aScheduleState2.Name()
290 && aScheduleState1.Enabled() == aScheduleState2.Enabled() && dueTime1 == dueTime2)
297 EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::RandomScheduleEntryInfoHometime(TInt64& aSeed)
299 TScheduleEntryInfo2 entryInfo;
300 entryInfo.SetIntervalType(STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed)));
301 entryInfo.SetStartTime(TTsTime (TTime(TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed))), ETrue));
302 entryInfo.SetInterval(Rand(1, 100, aSeed));
303 entryInfo.SetValidityPeriod(Rand(1, 100, aSeed));
307 EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::RandomScheduleEntryInfoUtc(TInt64& aSeed)
309 TScheduleEntryInfo2 entryInfo;
310 entryInfo.SetIntervalType(STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed)));
311 entryInfo.SetStartTime(TTsTime(TTime(TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed))), ETrue));
312 entryInfo.SetInterval(Rand(1, 100, aSeed));
313 entryInfo.SetValidityPeriod(Rand(1, 100, aSeed));
317 EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::ScheduleEntryInfo(TIntervalType aType, const TTsTime& aStartTime, TInt aInterval, TTimeIntervalMinutes aValidPeriod)
319 TScheduleEntryInfo2 info (aStartTime, aType, aInterval, aValidPeriod);
323 // Pause execution for the specified number of seconds
324 EXPORT_C void SchSvrHelpers::Pause(TInt aPauseAmount)
326 //aTest.Printf(KPauseMessage, aPauseAmount);
327 User::After(KOneSecond * aPauseAmount); // default is 2 seconds
330 // PREQ234 support //
333 EXPORT_C void SchSvrHelpers::DeleteAllSchedulesL(RScheduler& aScheduler)
335 // First fetch task references so that we can delete all the tasks.
336 CArrayFixFlat<TSchedulerItemRef>* refs = new (ELeave) CArrayFixFlat<TSchedulerItemRef>(3);
337 CleanupStack::PushL(refs);
340 User::LeaveIfError(aScheduler.GetTaskRefsL(*refs, EAllSchedules, EAllTasks));
341 const TInt count = refs->Count();
343 for(TInt i=0; i<count; ++i)
345 const TSchedulerItemRef& ref = refs->At(i);
346 User::LeaveIfError(aScheduler.DeleteTask(ref.iHandle));
351 User::LeaveIfError(aScheduler.GetScheduleRefsL(*refs, EAllSchedules));
352 // Delete all schedules
353 const TInt count = refs->Count();
354 for(TInt i=0; i<count; ++i)
356 const TSchedulerItemRef& ref = refs->At(i);
357 User::LeaveIfError(aScheduler.DeleteSchedule(ref.iHandle));
360 CleanupStack::PopAndDestroy(refs);
363 // Deletes the task scheduler persisted files.
364 // It creates a separate process to do this as file to delete in in private
365 // data cage which requires all files capability which we don't want to
366 // grant to all test exes.
367 EXPORT_C void SchSvrHelpers::DeleteScheduleFilesL()
369 _LIT(KScheduleDeleter, "TScheduleDeleter");
372 User::LeaveIfError(p.Create(KScheduleDeleter, KNullDesC));
373 // Asynchronous logon: completes when process terminates with process exit code
377 User::WaitForRequest(stat);
378 TInt exitReason = p.ExitReason();
380 User::LeaveIfError(exitReason);
383 // registers the client exe, leaving if it is not found
384 EXPORT_C TInt SchSvrHelpers::RegisterClientL(RScheduler& aScheduler)
387 filename = _L("MinimalTaskHandler");
389 return aScheduler.Register(filename, 27);
392 // registers the panicing client exe
393 EXPORT_C TInt SchSvrHelpers::RegisterPanicingClient(RScheduler& aScheduler)
396 filename = _L("FaultyMinimalTaskHandler");
398 return aScheduler.Register(filename, 27);
401 // registers the a non existent exe
402 EXPORT_C TInt SchSvrHelpers::RegisterNonExistentClient(RScheduler& aScheduler)
405 filename = _L("NonExistentClient");
407 return aScheduler.Register(filename, 27);
410 // Sets the system time to the given local time
411 // It creates a separate process to do this,
412 // as this requires WDD capability which we don't want to grant to all test exes.
413 EXPORT_C TInt SchSvrHelpers::SetHomeTimeL(const TTime& aLocalTime)
416 _LIT(KSetHomeTime, "TSetHomeTime");
417 _LIT(KTimeFormat, "%F%Y%M%D:%H%T%S.%*C6");
421 TBuf<128> bufLocalTime;
423 aLocalTime.FormatL(bufLocalTime, KTimeFormat);
425 User::LeaveIfError(p.Create(KSetHomeTime, bufLocalTime));
427 // Asynchronous logon: completes when process terminates with process exit code
431 User::WaitForRequest(stat);
432 TInt exitReason = p.ExitReason();
438 // Sets the UTC time to a specified time value
439 // It creates a separate process to do this,
440 // as this requires WDD capability which we don't want to grant to all test exes.
441 EXPORT_C TInt SchSvrHelpers::SetUTCTimeL(const TTime& aUTCTime)
443 _LIT(KSetUTCTime, "TSetUTCTime");
444 _LIT(KTimeFormat, "%F%Y%M%D:%H%T%S.%*C6");
448 TBuf<128> bufUTCTime;
450 aUTCTime.FormatL(bufUTCTime, KTimeFormat);
452 User::LeaveIfError(p.Create(KSetUTCTime, bufUTCTime));
454 // Asynchronous logon: completes when process terminates with process exit code
458 User::WaitForRequest(stat);
459 TInt exitReason = p.ExitReason();
464 //This function is used in the test code to kill SCHSVR or MinimalTaskHandler
465 //processes (or some other) when they leftover and may cause OOM conditions.
466 // It creates a separate process to do this as killing a process requires
467 // PwrMgmt capability which we don't want to grant to all test exes.
468 EXPORT_C TInt CleanupHelpers::KillProcess(const TDesC& aProcessName)
470 _LIT(KProcessKiller, "TProcessKiller");
473 TInt result = p.Create(KProcessKiller, aProcessName);
475 if(result == KErrNone)
477 // Asynchronous logon: completes when process terminates with process exit code
481 User::WaitForRequest(stat);
482 result = p.ExitReason();
489 //Call this method before "TheTest(error == KErrNone);" statement.
490 //Otherwise if the error code is not KErrNone and the check fails, your
491 //cleanup code never gets called - probably you will have low memory condition on the
492 //test hardware and the rest of SCHSVR unit tests will fail sometimes and it will be very
493 //hard to find out what is going on there.
494 EXPORT_C void CleanupHelpers::TestCleanupL()
496 // 5 second delay to clean up any launched processes
497 // left over by the test
498 RDebug::Print(KPauseMessage, 5);
499 User::After(5 * KOneSecond);
501 _LIT(KLogServerName, "SCHEXE");
503 TInt err = CleanupHelpers::KillProcess(KLogServerName);
504 //dont leave if the process was not found, or if it is already dead
505 //but do leave if some other error occured
506 if((err != KErrNotFound)&&(err != KErrDied))
508 User::LeaveIfError(err);
514 //class STaskSemaphore
516 _LIT(KSchSemaphoreName, "SCHMinimalTaskHandler");
518 EXPORT_C void STaskSemaphore::CreateL()
520 //create semaphore and set it to 0
521 User::LeaveIfError(iSemaphore.CreateGlobal(KSchSemaphoreName,0));
524 EXPORT_C void STaskSemaphore::WaitL()
527 User::LeaveIfError(sem.OpenGlobal(KSchSemaphoreName));
532 EXPORT_C TInt STaskSemaphore::WaitL(TInt aTimeout)
535 User::LeaveIfError(sem.OpenGlobal(KSchSemaphoreName));
536 TInt r = sem.Wait(aTimeout);
541 EXPORT_C void STaskSemaphore::Close()