os/ossrv/genericservices/taskscheduler/Test/TSUtils/thelpers.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/taskscheduler/Test/TSUtils/thelpers.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,547 @@
     1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "Thelpers.h"
    1.20 +
    1.21 +// System includes
    1.22 +#include <e32math.h>
    1.23 +#include <f32file.h>
    1.24 +
    1.25 +_LIT(KPauseMessage,							"=== Pausing for %d seconds ===\n");
    1.26 +
    1.27 +//
    1.28 +// Helper class for Scheduler Server test code
    1.29 +//
    1.30 +
    1.31 +EXPORT_C TBool SchSvrHelpers::IsTimeTheSame(const TTime& aTime1, const TTime& aTime2)
    1.32 +	{
    1.33 +	// Just compares the time, not the date
    1.34 +	TDateTime time1 = aTime1.DateTime();
    1.35 +	TDateTime time2 = aTime2.DateTime();
    1.36 +	return ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
    1.37 +	}
    1.38 +
    1.39 +EXPORT_C TBool SchSvrHelpers::IsDateTheSame(const TTime& aTime1, const TTime& aTime2)
    1.40 +	{
    1.41 +	// Just compares the date not the time
    1.42 +	TDateTime time1 = aTime1.DateTime();
    1.43 +	TDateTime time2 = aTime2.DateTime();
    1.44 +	return ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
    1.45 +	}
    1.46 +
    1.47 +EXPORT_C TBool SchSvrHelpers::IsTaskInfoTheSame(const TTaskInfo& aTaskInfo1, const TTaskInfo& aTaskInfo2)
    1.48 +	{
    1.49 +	if	(	aTaskInfo1.iRepeat == aTaskInfo2.iRepeat	&& aTaskInfo1.iTaskId == aTaskInfo2.iTaskId
    1.50 +		 &&	aTaskInfo1.iName == aTaskInfo2.iName		&& aTaskInfo1.iPriority == aTaskInfo2.iPriority)
    1.51 +		 return ETrue;
    1.52 +	return EFalse;
    1.53 +	}
    1.54 +
    1.55 +EXPORT_C TBool SchSvrHelpers::IsItemRefTheSame(const TSchedulerItemRef& aItemRef1, const TSchedulerItemRef& aItemRef2)
    1.56 +	{
    1.57 +	if	(aItemRef1.iHandle == aItemRef2.iHandle && aItemRef1.iName == aItemRef2.iName)
    1.58 +		return ETrue;
    1.59 +	return EFalse;
    1.60 +	}
    1.61 +
    1.62 +EXPORT_C TBool SchSvrHelpers::IsScheduleStateTheSame(const TScheduleState& aScheduleState1, const TScheduleState& aScheduleState2)
    1.63 +	{
    1.64 +	if	(	aScheduleState1.iDueTime == aScheduleState2.iDueTime	&& aScheduleState1.iPersists == aScheduleState2.iPersists
    1.65 +		 &&	aScheduleState1.iName == aScheduleState2.iName			&& aScheduleState1.iEnabled == aScheduleState2.iEnabled)
    1.66 +		 return ETrue;
    1.67 +	return EFalse;
    1.68 +	}
    1.69 +
    1.70 +EXPORT_C TScheduleEntryInfo SchSvrHelpers::RandomScheduleEntryInfo(TInt64& aSeed)
    1.71 +	{
    1.72 +	TScheduleEntryInfo entryInfo;
    1.73 +	entryInfo.iIntervalType		= STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed));
    1.74 +	entryInfo.iStartTime		= TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed));
    1.75 +	entryInfo.iInterval			= Rand(1, 100, aSeed);
    1.76 +	entryInfo.iValidityPeriod	= Rand(1, 100, aSeed);
    1.77 +	return entryInfo;
    1.78 +	}
    1.79 +
    1.80 +EXPORT_C TScheduleEntryInfo SchSvrHelpers::ScheduleEntryInfo(TIntervalType aType, const TTime& aStartTime, TInt aInterval, TTimeIntervalMinutes aValidPeriod)
    1.81 +	{
    1.82 +	TScheduleEntryInfo info;
    1.83 +	info.iIntervalType		= aType;
    1.84 +	info.iStartTime			= aStartTime;
    1.85 +	info.iInterval			= aInterval;
    1.86 +	info.iValidityPeriod	= aValidPeriod;
    1.87 +	return info;
    1.88 +	}
    1.89 +
    1.90 +EXPORT_C TTaskInfo SchSvrHelpers::TaskInfo(const TDesC& aName, TInt aPriority, TInt aRepeat)
    1.91 +	{
    1.92 +	TName name(aName);
    1.93 +	return TTaskInfo(0, name, aPriority, aRepeat); // zero the id for now (returned by server)
    1.94 +	}
    1.95 +
    1.96 +// Generate a random number based upon a seed and a range
    1.97 +EXPORT_C TInt SchSvrHelpers::Rand(const TInt aLow, const TInt aHigh, TInt64& aSeed)
    1.98 +	{
    1.99 +	TReal initialRand = (Math::FRand(aSeed) * (aHigh - aLow));
   1.100 +	TInt32 rand;
   1.101 +
   1.102 +	// Round to 0 decimal places, ie. the nearest whole numer
   1.103 +	Math::Round(initialRand, initialRand, 0);
   1.104 +	Math::Int(rand, initialRand);
   1.105 +
   1.106 +	//aSeed = seed;
   1.107 +	return (aLow + rand);
   1.108 +	}
   1.109 +
   1.110 +// Pause execution for the specified number of seconds
   1.111 +EXPORT_C void SchSvrHelpers::Pause(RTest& aTest, TInt aPauseAmount)
   1.112 +	{
   1.113 +	aTest.Printf(KPauseMessage, aPauseAmount);
   1.114 +	User::After(KOneSecond * aPauseAmount); // default is 2 seconds
   1.115 +	}
   1.116 +
   1.117 +// To be used by Hometime based schedules
   1.118 +EXPORT_C TDateTime SchSvrHelpers::TimeBasedOnOffset(TInt aSeconds, TInt aMinutes, TInt aHours, TInt aDays, TInt aMonths, TInt aYears)
   1.119 +	{
   1.120 +	TTime now;
   1.121 +	now.HomeTime();
   1.122 +	now += TTimeIntervalSeconds(aSeconds);
   1.123 +	now += TTimeIntervalMinutes(aMinutes);
   1.124 +	now += TTimeIntervalHours(aHours);
   1.125 +	now += TTimeIntervalDays(aDays);
   1.126 +	now += TTimeIntervalMonths(aMonths);
   1.127 +	now += TTimeIntervalYears(aYears);
   1.128 +	return now.DateTime();
   1.129 +	}
   1.130 +
   1.131 +// To be used by UTC time based schedules
   1.132 +EXPORT_C TDateTime SchSvrHelpers::UtcTimeBasedOnOffset(TInt aSeconds, TInt aMinutes, TInt aHours, TInt aDays, TInt aMonths, TInt aYears)
   1.133 +	{
   1.134 +	TTime now;
   1.135 +	now.UniversalTime();
   1.136 +	now += TTimeIntervalSeconds(aSeconds);
   1.137 +	now += TTimeIntervalMinutes(aMinutes);
   1.138 +	now += TTimeIntervalHours(aHours);
   1.139 +	now += TTimeIntervalDays(aDays);
   1.140 +	now += TTimeIntervalMonths(aMonths);
   1.141 +	now += TTimeIntervalYears(aYears);
   1.142 +	return now.DateTime();
   1.143 +	}
   1.144 +	
   1.145 +// Check task file left
   1.146 +// It creates a separate process to do this as file to access in in private
   1.147 +// data cage which requires all files capability 
   1.148 +EXPORT_C TInt SchSvrHelpers::CheckTaskFilesL()
   1.149 +	{
   1.150 +	_LIT(KTaskFileChecker, "TTaskFileChecker");
   1.151 +	TRequestStatus stat;
   1.152 +	RProcess p;
   1.153 +	User::LeaveIfError(p.Create(KTaskFileChecker, KNullDesC));
   1.154 +	// Asynchronous logon: completes when process terminates with process exit code
   1.155 +	p.Logon(stat);
   1.156 +	p.Resume();
   1.157 +
   1.158 +	User::WaitForRequest(stat);
   1.159 +	TInt exitReason = p.ExitReason();
   1.160 +	p.Close();
   1.161 +	
   1.162 +	// return exit reasons as checking result
   1.163 +	return exitReason;
   1.164 +	}	
   1.165 +	
   1.166 +	
   1.167 +// Create task files for testing
   1.168 +// It creates a separate process to do this as file to access in in private
   1.169 +// data cage which requires all files capability 
   1.170 +EXPORT_C TInt SchSvrHelpers::CreateTaskFilesL()
   1.171 +	{
   1.172 +	_LIT(KTaskFileCreator, "TTaskFileCreator");
   1.173 +	TRequestStatus stat;
   1.174 +	RProcess p;
   1.175 +	User::LeaveIfError(p.Create(KTaskFileCreator, KNullDesC));
   1.176 +	// Asynchronous logon: completes when process terminates with process exit code
   1.177 +	p.Logon(stat);
   1.178 +	p.Resume();
   1.179 +
   1.180 +	User::WaitForRequest(stat);
   1.181 +	TInt exitReason = p.ExitReason();
   1.182 +	p.Close();
   1.183 +	
   1.184 +	// return exit reasons as checking result
   1.185 +	return exitReason;
   1.186 +	}	
   1.187 +	
   1.188 +// Start Task Scheduler
   1.189 +// It creates a separate process to do this as need to set appropriate SID
   1.190 +// to enable task scheduler to be started 
   1.191 +EXPORT_C TInt SchSvrHelpers::LaunchTaskSchedulerL()
   1.192 +	{
   1.193 +	_LIT(KTaskSchedulerLauncher, "TTaskSchedulerLauncher");
   1.194 +	TRequestStatus stat;
   1.195 +	RProcess p;
   1.196 +	User::LeaveIfError(p.Create(KTaskSchedulerLauncher, KNullDesC));
   1.197 +	// Asynchronous logon: completes when process terminates with process exit code
   1.198 +	p.Logon(stat);
   1.199 +	p.Resume();
   1.200 +
   1.201 +	User::WaitForRequest(stat);
   1.202 +	TInt exitReason = p.ExitReason();
   1.203 +	p.Close();
   1.204 +	
   1.205 +	// return exit reasons as checking result
   1.206 +	return exitReason;
   1.207 +	}	
   1.208 +
   1.209 +
   1.210 +//
   1.211 +// PREQ234 support //
   1.212 +
   1.213 +EXPORT_C TBool SchSvrHelpers::IsTimeTheSame(const TTsTime& aTime1, const TTsTime& aTime2)
   1.214 +	{
   1.215 +	// Just compares the time, not the date
   1.216 +	TBool ret = EFalse;
   1.217 +	
   1.218 +	if (aTime1.IsUtc() && aTime2.IsUtc())
   1.219 +		{
   1.220 +		TDateTime time1 = aTime1.GetUtcTime().DateTime();
   1.221 +		TDateTime time2 = aTime2.GetUtcTime().DateTime();
   1.222 +		ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
   1.223 +		}
   1.224 +	if (!aTime1.IsUtc() && !aTime2.IsUtc())
   1.225 +		{
   1.226 +		TDateTime time1 = aTime1.GetLocalTime().DateTime();
   1.227 +		TDateTime time2 = aTime2.GetLocalTime().DateTime();
   1.228 +		ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
   1.229 +		}
   1.230 +
   1.231 +	return ret;
   1.232 +	}
   1.233 +
   1.234 +EXPORT_C TBool SchSvrHelpers::IsTimeTheSameNoSeconds(const TTsTime& aTime1, const TTsTime& aTime2)
   1.235 +	{
   1.236 +	// Just compares the time up to hours and minutes
   1.237 +	TBool ret = EFalse;
   1.238 +	
   1.239 +	if (aTime1.IsUtc() && aTime2.IsUtc())
   1.240 +		{
   1.241 +		TDateTime time1 = aTime1.GetUtcTime().DateTime();
   1.242 +		TDateTime time2 = aTime2.GetUtcTime().DateTime();
   1.243 +		ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()));
   1.244 +		}
   1.245 +	if (!aTime1.IsUtc() && !aTime2.IsUtc())
   1.246 +		{
   1.247 +		TDateTime time1 = aTime1.GetLocalTime().DateTime();
   1.248 +		TDateTime time2 = aTime2.GetLocalTime().DateTime();
   1.249 +		ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()));
   1.250 +		}
   1.251 +	return ret;
   1.252 +	}
   1.253 +
   1.254 +EXPORT_C TBool SchSvrHelpers::IsDateTheSame(const TTsTime& aTime1, const TTsTime& aTime2)
   1.255 +	{
   1.256 +	// Just compares the date not the time
   1.257 +	TBool ret = EFalse;
   1.258 +	
   1.259 +	if (aTime1.IsUtc() && aTime2.IsUtc())
   1.260 +		{
   1.261 +		TDateTime time1 = aTime1.GetUtcTime().DateTime();
   1.262 +		TDateTime time2 = aTime2.GetUtcTime().DateTime();
   1.263 +		ret = ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
   1.264 +		}
   1.265 +	if (!aTime1.IsUtc() && !aTime2.IsUtc())
   1.266 +		{
   1.267 +		TDateTime time1 = aTime1.GetLocalTime().DateTime();
   1.268 +		TDateTime time2 = aTime2.GetLocalTime().DateTime();
   1.269 +		ret = ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
   1.270 +		}
   1.271 +
   1.272 +	return ret;
   1.273 +	}
   1.274 +
   1.275 +EXPORT_C TBool SchSvrHelpers::IsScheduleStateTheSame(const TScheduleState2& aScheduleState1, const TScheduleState2& aScheduleState2)
   1.276 +	{
   1.277 +	if (aScheduleState1.DueTime().IsUtc() && aScheduleState2.DueTime().IsUtc())
   1.278 +		{
   1.279 +		TTime dueTime1 = aScheduleState1.DueTime().GetUtcTime();
   1.280 +		TTime dueTime2 = aScheduleState2.DueTime().GetUtcTime();
   1.281 +		
   1.282 +		if (aScheduleState1.Persists() == aScheduleState2.Persists()	&&	aScheduleState1.Name() == aScheduleState2.Name()
   1.283 +		&& 	aScheduleState1.Enabled() == aScheduleState2.Enabled()		&&	dueTime1 == dueTime2)
   1.284 +			return ETrue;
   1.285 +		}
   1.286 +
   1.287 +	if (!aScheduleState1.DueTime().IsUtc() && !aScheduleState2.DueTime().IsUtc())
   1.288 +		{
   1.289 +		TTime dueTime1 = aScheduleState1.DueTime().GetLocalTime();
   1.290 +		TTime dueTime2 = aScheduleState2.DueTime().GetLocalTime();
   1.291 +		
   1.292 +		if (aScheduleState1.Persists() == aScheduleState2.Persists()	&&	aScheduleState1.Name() == aScheduleState2.Name()
   1.293 +		&& 	aScheduleState1.Enabled() == aScheduleState2.Enabled()		&&	dueTime1 == dueTime2)
   1.294 +			return ETrue;
   1.295 +		}	
   1.296 +	
   1.297 +	return EFalse;
   1.298 +	}
   1.299 +
   1.300 +EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::RandomScheduleEntryInfoHometime(TInt64& aSeed)
   1.301 +	{
   1.302 +	TScheduleEntryInfo2 entryInfo;
   1.303 +	entryInfo.SetIntervalType(STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed)));
   1.304 +	entryInfo.SetStartTime(TTsTime (TTime(TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed))), ETrue));
   1.305 +	entryInfo.SetInterval(Rand(1, 100, aSeed));
   1.306 +	entryInfo.SetValidityPeriod(Rand(1, 100, aSeed));
   1.307 +	return entryInfo;
   1.308 +	}
   1.309 +
   1.310 +EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::RandomScheduleEntryInfoUtc(TInt64& aSeed)
   1.311 +	{
   1.312 +	TScheduleEntryInfo2 entryInfo;
   1.313 +	entryInfo.SetIntervalType(STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed)));
   1.314 +	entryInfo.SetStartTime(TTsTime(TTime(TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed))), ETrue));
   1.315 +	entryInfo.SetInterval(Rand(1, 100, aSeed));
   1.316 +	entryInfo.SetValidityPeriod(Rand(1, 100, aSeed));
   1.317 +	return entryInfo;
   1.318 +	}
   1.319 +
   1.320 +EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::ScheduleEntryInfo(TIntervalType aType, const TTsTime& aStartTime, TInt aInterval, TTimeIntervalMinutes aValidPeriod)
   1.321 +	{
   1.322 +	TScheduleEntryInfo2 info (aStartTime, aType, aInterval, aValidPeriod);
   1.323 +	return info;
   1.324 +	}
   1.325 +
   1.326 +// Pause execution for the specified number of seconds
   1.327 +EXPORT_C void SchSvrHelpers::Pause(TInt aPauseAmount)
   1.328 +	{
   1.329 +	//aTest.Printf(KPauseMessage, aPauseAmount);
   1.330 +	User::After(KOneSecond * aPauseAmount); // default is 2 seconds
   1.331 +	}
   1.332 +
   1.333 +// PREQ234 support //
   1.334 +//
   1.335 +
   1.336 +EXPORT_C void SchSvrHelpers::DeleteAllSchedulesL(RScheduler& aScheduler)
   1.337 +	{
   1.338 +	// First fetch task references so that we can delete all the tasks.
   1.339 +	CArrayFixFlat<TSchedulerItemRef>* refs = new (ELeave) CArrayFixFlat<TSchedulerItemRef>(3);
   1.340 +	CleanupStack::PushL(refs);
   1.341 +
   1.342 +	{
   1.343 +	User::LeaveIfError(aScheduler.GetTaskRefsL(*refs, EAllSchedules, EAllTasks));
   1.344 +	const TInt count = refs->Count();
   1.345 +	// Delete all tasks
   1.346 +	for(TInt i=0; i<count; ++i)
   1.347 +		{
   1.348 +		const TSchedulerItemRef& ref = refs->At(i);
   1.349 +		User::LeaveIfError(aScheduler.DeleteTask(ref.iHandle));
   1.350 +		}
   1.351 +	}
   1.352 +	refs->Reset();
   1.353 +	{
   1.354 +	User::LeaveIfError(aScheduler.GetScheduleRefsL(*refs, EAllSchedules));
   1.355 +	// Delete all schedules
   1.356 +	const TInt count = refs->Count();
   1.357 +	for(TInt i=0; i<count; ++i)
   1.358 +		{
   1.359 +		const TSchedulerItemRef& ref = refs->At(i);
   1.360 +		User::LeaveIfError(aScheduler.DeleteSchedule(ref.iHandle));
   1.361 +		}
   1.362 +	}
   1.363 +	CleanupStack::PopAndDestroy(refs);
   1.364 +	}
   1.365 +
   1.366 +// Deletes the task scheduler persisted files.
   1.367 +// It creates a separate process to do this as file to delete in in private
   1.368 +// data cage which requires all files capability which we don't want to 
   1.369 +// grant to all test exes.
   1.370 +EXPORT_C void SchSvrHelpers::DeleteScheduleFilesL()
   1.371 +	{
   1.372 +	_LIT(KScheduleDeleter, "TScheduleDeleter");
   1.373 +	TRequestStatus stat;
   1.374 +	RProcess p;
   1.375 +	User::LeaveIfError(p.Create(KScheduleDeleter, KNullDesC));
   1.376 +	// Asynchronous logon: completes when process terminates with process exit code
   1.377 +	p.Logon(stat);
   1.378 +	p.Resume();
   1.379 +
   1.380 +	User::WaitForRequest(stat);
   1.381 +	TInt exitReason = p.ExitReason();
   1.382 +	p.Close();
   1.383 +	User::LeaveIfError(exitReason);
   1.384 +	}
   1.385 +	
   1.386 +// registers the client exe, leaving if it is not found 
   1.387 +EXPORT_C TInt SchSvrHelpers::RegisterClientL(RScheduler& aScheduler)
   1.388 +	{
   1.389 +	TFileName filename;
   1.390 +	filename = _L("MinimalTaskHandler");
   1.391 +
   1.392 +	return aScheduler.Register(filename, 27);
   1.393 +	}
   1.394 +	
   1.395 +// registers the panicing client exe
   1.396 +EXPORT_C TInt SchSvrHelpers::RegisterPanicingClient(RScheduler& aScheduler)
   1.397 +	{
   1.398 +	TFileName filename;
   1.399 +	filename = _L("FaultyMinimalTaskHandler");
   1.400 +
   1.401 +	return aScheduler.Register(filename, 27);
   1.402 +	}
   1.403 +	
   1.404 +// registers the a non existent exe
   1.405 +EXPORT_C TInt SchSvrHelpers::RegisterNonExistentClient(RScheduler& aScheduler)
   1.406 +	{
   1.407 +	TFileName filename;
   1.408 +	filename = _L("NonExistentClient");
   1.409 +
   1.410 +	return aScheduler.Register(filename, 27);
   1.411 +	}
   1.412 +	
   1.413 +// Sets the system time to the given local time
   1.414 +// It creates a separate process to do this, 
   1.415 +// as this requires WDD capability which we don't want to grant to all test exes.
   1.416 +EXPORT_C TInt SchSvrHelpers::SetHomeTimeL(const TTime& aLocalTime)
   1.417 +	{
   1.418 +	
   1.419 +	_LIT(KSetHomeTime, "TSetHomeTime");
   1.420 +	_LIT(KTimeFormat, "%F%Y%M%D:%H%T%S.%*C6");
   1.421 +	
   1.422 +	RProcess p;	
   1.423 +	TRequestStatus stat;
   1.424 +	TBuf<128> bufLocalTime;	
   1.425 +	
   1.426 +	aLocalTime.FormatL(bufLocalTime, KTimeFormat);
   1.427 +	
   1.428 +	User::LeaveIfError(p.Create(KSetHomeTime, bufLocalTime));
   1.429 +	
   1.430 +	// Asynchronous logon: completes when process terminates with process exit code
   1.431 +	p.Logon(stat);
   1.432 +	p.Resume();
   1.433 +
   1.434 +	User::WaitForRequest(stat);
   1.435 +	TInt exitReason = p.ExitReason();
   1.436 +	p.Close();
   1.437 +	return (exitReason);
   1.438 +	}
   1.439 +
   1.440 +	
   1.441 +// Sets the UTC time to a specified time value
   1.442 +// It creates a separate process to do this, 
   1.443 +// as this requires WDD capability which we don't want to grant to all test exes.
   1.444 +EXPORT_C TInt SchSvrHelpers::SetUTCTimeL(const TTime& aUTCTime)
   1.445 +	{
   1.446 +	_LIT(KSetUTCTime, "TSetUTCTime");
   1.447 +	_LIT(KTimeFormat, "%F%Y%M%D:%H%T%S.%*C6");
   1.448 +	
   1.449 +	RProcess p;	
   1.450 +	TRequestStatus stat;
   1.451 +	TBuf<128> bufUTCTime;	
   1.452 +	
   1.453 +	aUTCTime.FormatL(bufUTCTime, KTimeFormat);	
   1.454 +
   1.455 +	User::LeaveIfError(p.Create(KSetUTCTime, bufUTCTime));
   1.456 +		
   1.457 +	// Asynchronous logon: completes when process terminates with process exit code
   1.458 +	p.Logon(stat);
   1.459 +	p.Resume();
   1.460 +
   1.461 +	User::WaitForRequest(stat);
   1.462 +	TInt exitReason = p.ExitReason();	
   1.463 +	p.Close();	
   1.464 +	return (exitReason);
   1.465 +	}
   1.466 +
   1.467 +//This function is used in the test code to kill SCHSVR or MinimalTaskHandler
   1.468 +//processes (or some other) when they leftover and may cause OOM conditions.
   1.469 +// It creates a separate process to do this as killing a process requires
   1.470 +// PwrMgmt capability which we don't want to grant to all test exes.
   1.471 +EXPORT_C TInt CleanupHelpers::KillProcess(const TDesC& aProcessName)
   1.472 +	{
   1.473 +	_LIT(KProcessKiller, "TProcessKiller");
   1.474 +	TRequestStatus stat;
   1.475 +	RProcess p;
   1.476 +	TInt result = p.Create(KProcessKiller, aProcessName);
   1.477 +	
   1.478 +	if(result == KErrNone)
   1.479 +		{
   1.480 +		// Asynchronous logon: completes when process terminates with process exit code
   1.481 +		p.Logon(stat);
   1.482 +		p.Resume();
   1.483 +
   1.484 +		User::WaitForRequest(stat);
   1.485 +		result = p.ExitReason();
   1.486 +		p.Close();			
   1.487 +		}
   1.488 +
   1.489 +	return result;
   1.490 +	}
   1.491 +
   1.492 +//Call this method before "TheTest(error == KErrNone);" statement.
   1.493 +//Otherwise if the error code is not KErrNone and the check fails, your 
   1.494 +//cleanup code never gets called - probably you will have low memory condition on the 
   1.495 +//test hardware and the rest of SCHSVR unit tests will fail sometimes and it will be very 
   1.496 +//hard to find out what is going on there.
   1.497 +EXPORT_C void CleanupHelpers::TestCleanupL()
   1.498 +	{
   1.499 +	// 5 second delay to clean up any launched processes 
   1.500 +	// left over by the test
   1.501 +	RDebug::Print(KPauseMessage, 5);
   1.502 +	User::After(5 * KOneSecond); 
   1.503 +
   1.504 +	_LIT(KLogServerName, "SCHEXE");
   1.505 +	
   1.506 +	TInt err = CleanupHelpers::KillProcess(KLogServerName);
   1.507 +	//dont leave if the process was not found, or if it is already dead
   1.508 +	//but do leave if some other error occured
   1.509 +	if((err != KErrNotFound)&&(err != KErrDied))
   1.510 +		{
   1.511 +		User::LeaveIfError(err);	
   1.512 +		}
   1.513 +	}
   1.514 +	
   1.515 +
   1.516 +//
   1.517 +//class STaskSemaphore
   1.518 +
   1.519 +_LIT(KSchSemaphoreName, "SCHMinimalTaskHandler");
   1.520 +
   1.521 +EXPORT_C void STaskSemaphore::CreateL()
   1.522 +	{
   1.523 +	//create semaphore and set it to 0
   1.524 +	User::LeaveIfError(iSemaphore.CreateGlobal(KSchSemaphoreName,0));
   1.525 +	}
   1.526 +
   1.527 +EXPORT_C void STaskSemaphore::WaitL()
   1.528 +	{
   1.529 +	RSemaphore sem;
   1.530 +	User::LeaveIfError(sem.OpenGlobal(KSchSemaphoreName));
   1.531 +	sem.Wait();
   1.532 +	sem.Close();		
   1.533 +	}
   1.534 +
   1.535 +EXPORT_C TInt STaskSemaphore::WaitL(TInt aTimeout)
   1.536 +	{
   1.537 +	RSemaphore sem;
   1.538 +	User::LeaveIfError(sem.OpenGlobal(KSchSemaphoreName));
   1.539 +	TInt r = sem.Wait(aTimeout);
   1.540 +	sem.Close();		
   1.541 +	return r;
   1.542 +	}
   1.543 +
   1.544 +EXPORT_C void STaskSemaphore::Close()
   1.545 +	{
   1.546 +	iSemaphore.Close();
   1.547 +	}
   1.548 +	
   1.549 +	
   1.550 +