os/ossrv/genericservices/taskscheduler/Test/TSUtils/thelpers.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 "Thelpers.h"
sl@0
    17
sl@0
    18
// System includes
sl@0
    19
#include <e32math.h>
sl@0
    20
#include <f32file.h>
sl@0
    21
sl@0
    22
_LIT(KPauseMessage,							"=== Pausing for %d seconds ===\n");
sl@0
    23
sl@0
    24
//
sl@0
    25
// Helper class for Scheduler Server test code
sl@0
    26
//
sl@0
    27
sl@0
    28
EXPORT_C TBool SchSvrHelpers::IsTimeTheSame(const TTime& aTime1, const TTime& aTime2)
sl@0
    29
	{
sl@0
    30
	// Just compares the time, not the date
sl@0
    31
	TDateTime time1 = aTime1.DateTime();
sl@0
    32
	TDateTime time2 = aTime2.DateTime();
sl@0
    33
	return ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
sl@0
    34
	}
sl@0
    35
sl@0
    36
EXPORT_C TBool SchSvrHelpers::IsDateTheSame(const TTime& aTime1, const TTime& aTime2)
sl@0
    37
	{
sl@0
    38
	// Just compares the date not the time
sl@0
    39
	TDateTime time1 = aTime1.DateTime();
sl@0
    40
	TDateTime time2 = aTime2.DateTime();
sl@0
    41
	return ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
sl@0
    42
	}
sl@0
    43
sl@0
    44
EXPORT_C TBool SchSvrHelpers::IsTaskInfoTheSame(const TTaskInfo& aTaskInfo1, const TTaskInfo& aTaskInfo2)
sl@0
    45
	{
sl@0
    46
	if	(	aTaskInfo1.iRepeat == aTaskInfo2.iRepeat	&& aTaskInfo1.iTaskId == aTaskInfo2.iTaskId
sl@0
    47
		 &&	aTaskInfo1.iName == aTaskInfo2.iName		&& aTaskInfo1.iPriority == aTaskInfo2.iPriority)
sl@0
    48
		 return ETrue;
sl@0
    49
	return EFalse;
sl@0
    50
	}
sl@0
    51
sl@0
    52
EXPORT_C TBool SchSvrHelpers::IsItemRefTheSame(const TSchedulerItemRef& aItemRef1, const TSchedulerItemRef& aItemRef2)
sl@0
    53
	{
sl@0
    54
	if	(aItemRef1.iHandle == aItemRef2.iHandle && aItemRef1.iName == aItemRef2.iName)
sl@0
    55
		return ETrue;
sl@0
    56
	return EFalse;
sl@0
    57
	}
sl@0
    58
sl@0
    59
EXPORT_C TBool SchSvrHelpers::IsScheduleStateTheSame(const TScheduleState& aScheduleState1, const TScheduleState& aScheduleState2)
sl@0
    60
	{
sl@0
    61
	if	(	aScheduleState1.iDueTime == aScheduleState2.iDueTime	&& aScheduleState1.iPersists == aScheduleState2.iPersists
sl@0
    62
		 &&	aScheduleState1.iName == aScheduleState2.iName			&& aScheduleState1.iEnabled == aScheduleState2.iEnabled)
sl@0
    63
		 return ETrue;
sl@0
    64
	return EFalse;
sl@0
    65
	}
sl@0
    66
sl@0
    67
EXPORT_C TScheduleEntryInfo SchSvrHelpers::RandomScheduleEntryInfo(TInt64& aSeed)
sl@0
    68
	{
sl@0
    69
	TScheduleEntryInfo entryInfo;
sl@0
    70
	entryInfo.iIntervalType		= STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed));
sl@0
    71
	entryInfo.iStartTime		= TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed));
sl@0
    72
	entryInfo.iInterval			= Rand(1, 100, aSeed);
sl@0
    73
	entryInfo.iValidityPeriod	= Rand(1, 100, aSeed);
sl@0
    74
	return entryInfo;
sl@0
    75
	}
sl@0
    76
sl@0
    77
EXPORT_C TScheduleEntryInfo SchSvrHelpers::ScheduleEntryInfo(TIntervalType aType, const TTime& aStartTime, TInt aInterval, TTimeIntervalMinutes aValidPeriod)
sl@0
    78
	{
sl@0
    79
	TScheduleEntryInfo info;
sl@0
    80
	info.iIntervalType		= aType;
sl@0
    81
	info.iStartTime			= aStartTime;
sl@0
    82
	info.iInterval			= aInterval;
sl@0
    83
	info.iValidityPeriod	= aValidPeriod;
sl@0
    84
	return info;
sl@0
    85
	}
sl@0
    86
sl@0
    87
EXPORT_C TTaskInfo SchSvrHelpers::TaskInfo(const TDesC& aName, TInt aPriority, TInt aRepeat)
sl@0
    88
	{
sl@0
    89
	TName name(aName);
sl@0
    90
	return TTaskInfo(0, name, aPriority, aRepeat); // zero the id for now (returned by server)
sl@0
    91
	}
sl@0
    92
sl@0
    93
// Generate a random number based upon a seed and a range
sl@0
    94
EXPORT_C TInt SchSvrHelpers::Rand(const TInt aLow, const TInt aHigh, TInt64& aSeed)
sl@0
    95
	{
sl@0
    96
	TReal initialRand = (Math::FRand(aSeed) * (aHigh - aLow));
sl@0
    97
	TInt32 rand;
sl@0
    98
sl@0
    99
	// Round to 0 decimal places, ie. the nearest whole numer
sl@0
   100
	Math::Round(initialRand, initialRand, 0);
sl@0
   101
	Math::Int(rand, initialRand);
sl@0
   102
sl@0
   103
	//aSeed = seed;
sl@0
   104
	return (aLow + rand);
sl@0
   105
	}
sl@0
   106
sl@0
   107
// Pause execution for the specified number of seconds
sl@0
   108
EXPORT_C void SchSvrHelpers::Pause(RTest& aTest, TInt aPauseAmount)
sl@0
   109
	{
sl@0
   110
	aTest.Printf(KPauseMessage, aPauseAmount);
sl@0
   111
	User::After(KOneSecond * aPauseAmount); // default is 2 seconds
sl@0
   112
	}
sl@0
   113
sl@0
   114
// To be used by Hometime based schedules
sl@0
   115
EXPORT_C TDateTime SchSvrHelpers::TimeBasedOnOffset(TInt aSeconds, TInt aMinutes, TInt aHours, TInt aDays, TInt aMonths, TInt aYears)
sl@0
   116
	{
sl@0
   117
	TTime now;
sl@0
   118
	now.HomeTime();
sl@0
   119
	now += TTimeIntervalSeconds(aSeconds);
sl@0
   120
	now += TTimeIntervalMinutes(aMinutes);
sl@0
   121
	now += TTimeIntervalHours(aHours);
sl@0
   122
	now += TTimeIntervalDays(aDays);
sl@0
   123
	now += TTimeIntervalMonths(aMonths);
sl@0
   124
	now += TTimeIntervalYears(aYears);
sl@0
   125
	return now.DateTime();
sl@0
   126
	}
sl@0
   127
sl@0
   128
// To be used by UTC time based schedules
sl@0
   129
EXPORT_C TDateTime SchSvrHelpers::UtcTimeBasedOnOffset(TInt aSeconds, TInt aMinutes, TInt aHours, TInt aDays, TInt aMonths, TInt aYears)
sl@0
   130
	{
sl@0
   131
	TTime now;
sl@0
   132
	now.UniversalTime();
sl@0
   133
	now += TTimeIntervalSeconds(aSeconds);
sl@0
   134
	now += TTimeIntervalMinutes(aMinutes);
sl@0
   135
	now += TTimeIntervalHours(aHours);
sl@0
   136
	now += TTimeIntervalDays(aDays);
sl@0
   137
	now += TTimeIntervalMonths(aMonths);
sl@0
   138
	now += TTimeIntervalYears(aYears);
sl@0
   139
	return now.DateTime();
sl@0
   140
	}
sl@0
   141
	
sl@0
   142
// Check task file left
sl@0
   143
// It creates a separate process to do this as file to access in in private
sl@0
   144
// data cage which requires all files capability 
sl@0
   145
EXPORT_C TInt SchSvrHelpers::CheckTaskFilesL()
sl@0
   146
	{
sl@0
   147
	_LIT(KTaskFileChecker, "TTaskFileChecker");
sl@0
   148
	TRequestStatus stat;
sl@0
   149
	RProcess p;
sl@0
   150
	User::LeaveIfError(p.Create(KTaskFileChecker, KNullDesC));
sl@0
   151
	// Asynchronous logon: completes when process terminates with process exit code
sl@0
   152
	p.Logon(stat);
sl@0
   153
	p.Resume();
sl@0
   154
sl@0
   155
	User::WaitForRequest(stat);
sl@0
   156
	TInt exitReason = p.ExitReason();
sl@0
   157
	p.Close();
sl@0
   158
	
sl@0
   159
	// return exit reasons as checking result
sl@0
   160
	return exitReason;
sl@0
   161
	}	
sl@0
   162
	
sl@0
   163
	
sl@0
   164
// Create task files for testing
sl@0
   165
// It creates a separate process to do this as file to access in in private
sl@0
   166
// data cage which requires all files capability 
sl@0
   167
EXPORT_C TInt SchSvrHelpers::CreateTaskFilesL()
sl@0
   168
	{
sl@0
   169
	_LIT(KTaskFileCreator, "TTaskFileCreator");
sl@0
   170
	TRequestStatus stat;
sl@0
   171
	RProcess p;
sl@0
   172
	User::LeaveIfError(p.Create(KTaskFileCreator, KNullDesC));
sl@0
   173
	// Asynchronous logon: completes when process terminates with process exit code
sl@0
   174
	p.Logon(stat);
sl@0
   175
	p.Resume();
sl@0
   176
sl@0
   177
	User::WaitForRequest(stat);
sl@0
   178
	TInt exitReason = p.ExitReason();
sl@0
   179
	p.Close();
sl@0
   180
	
sl@0
   181
	// return exit reasons as checking result
sl@0
   182
	return exitReason;
sl@0
   183
	}	
sl@0
   184
	
sl@0
   185
// Start Task Scheduler
sl@0
   186
// It creates a separate process to do this as need to set appropriate SID
sl@0
   187
// to enable task scheduler to be started 
sl@0
   188
EXPORT_C TInt SchSvrHelpers::LaunchTaskSchedulerL()
sl@0
   189
	{
sl@0
   190
	_LIT(KTaskSchedulerLauncher, "TTaskSchedulerLauncher");
sl@0
   191
	TRequestStatus stat;
sl@0
   192
	RProcess p;
sl@0
   193
	User::LeaveIfError(p.Create(KTaskSchedulerLauncher, KNullDesC));
sl@0
   194
	// Asynchronous logon: completes when process terminates with process exit code
sl@0
   195
	p.Logon(stat);
sl@0
   196
	p.Resume();
sl@0
   197
sl@0
   198
	User::WaitForRequest(stat);
sl@0
   199
	TInt exitReason = p.ExitReason();
sl@0
   200
	p.Close();
sl@0
   201
	
sl@0
   202
	// return exit reasons as checking result
sl@0
   203
	return exitReason;
sl@0
   204
	}	
sl@0
   205
sl@0
   206
sl@0
   207
//
sl@0
   208
// PREQ234 support //
sl@0
   209
sl@0
   210
EXPORT_C TBool SchSvrHelpers::IsTimeTheSame(const TTsTime& aTime1, const TTsTime& aTime2)
sl@0
   211
	{
sl@0
   212
	// Just compares the time, not the date
sl@0
   213
	TBool ret = EFalse;
sl@0
   214
	
sl@0
   215
	if (aTime1.IsUtc() && aTime2.IsUtc())
sl@0
   216
		{
sl@0
   217
		TDateTime time1 = aTime1.GetUtcTime().DateTime();
sl@0
   218
		TDateTime time2 = aTime2.GetUtcTime().DateTime();
sl@0
   219
		ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
sl@0
   220
		}
sl@0
   221
	if (!aTime1.IsUtc() && !aTime2.IsUtc())
sl@0
   222
		{
sl@0
   223
		TDateTime time1 = aTime1.GetLocalTime().DateTime();
sl@0
   224
		TDateTime time2 = aTime2.GetLocalTime().DateTime();
sl@0
   225
		ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()) && (time1.Second() == time2.Second()));
sl@0
   226
		}
sl@0
   227
sl@0
   228
	return ret;
sl@0
   229
	}
sl@0
   230
sl@0
   231
EXPORT_C TBool SchSvrHelpers::IsTimeTheSameNoSeconds(const TTsTime& aTime1, const TTsTime& aTime2)
sl@0
   232
	{
sl@0
   233
	// Just compares the time up to hours and minutes
sl@0
   234
	TBool ret = EFalse;
sl@0
   235
	
sl@0
   236
	if (aTime1.IsUtc() && aTime2.IsUtc())
sl@0
   237
		{
sl@0
   238
		TDateTime time1 = aTime1.GetUtcTime().DateTime();
sl@0
   239
		TDateTime time2 = aTime2.GetUtcTime().DateTime();
sl@0
   240
		ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()));
sl@0
   241
		}
sl@0
   242
	if (!aTime1.IsUtc() && !aTime2.IsUtc())
sl@0
   243
		{
sl@0
   244
		TDateTime time1 = aTime1.GetLocalTime().DateTime();
sl@0
   245
		TDateTime time2 = aTime2.GetLocalTime().DateTime();
sl@0
   246
		ret = ((time1.Hour() == time2.Hour()) && (time1.Minute() == time2.Minute()));
sl@0
   247
		}
sl@0
   248
	return ret;
sl@0
   249
	}
sl@0
   250
sl@0
   251
EXPORT_C TBool SchSvrHelpers::IsDateTheSame(const TTsTime& aTime1, const TTsTime& aTime2)
sl@0
   252
	{
sl@0
   253
	// Just compares the date not the time
sl@0
   254
	TBool ret = EFalse;
sl@0
   255
	
sl@0
   256
	if (aTime1.IsUtc() && aTime2.IsUtc())
sl@0
   257
		{
sl@0
   258
		TDateTime time1 = aTime1.GetUtcTime().DateTime();
sl@0
   259
		TDateTime time2 = aTime2.GetUtcTime().DateTime();
sl@0
   260
		ret = ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
sl@0
   261
		}
sl@0
   262
	if (!aTime1.IsUtc() && !aTime2.IsUtc())
sl@0
   263
		{
sl@0
   264
		TDateTime time1 = aTime1.GetLocalTime().DateTime();
sl@0
   265
		TDateTime time2 = aTime2.GetLocalTime().DateTime();
sl@0
   266
		ret = ((time1.Day() == time2.Day()) && (time1.Month() == time2.Month()) && (time1.Year() == time2.Year()));
sl@0
   267
		}
sl@0
   268
sl@0
   269
	return ret;
sl@0
   270
	}
sl@0
   271
sl@0
   272
EXPORT_C TBool SchSvrHelpers::IsScheduleStateTheSame(const TScheduleState2& aScheduleState1, const TScheduleState2& aScheduleState2)
sl@0
   273
	{
sl@0
   274
	if (aScheduleState1.DueTime().IsUtc() && aScheduleState2.DueTime().IsUtc())
sl@0
   275
		{
sl@0
   276
		TTime dueTime1 = aScheduleState1.DueTime().GetUtcTime();
sl@0
   277
		TTime dueTime2 = aScheduleState2.DueTime().GetUtcTime();
sl@0
   278
		
sl@0
   279
		if (aScheduleState1.Persists() == aScheduleState2.Persists()	&&	aScheduleState1.Name() == aScheduleState2.Name()
sl@0
   280
		&& 	aScheduleState1.Enabled() == aScheduleState2.Enabled()		&&	dueTime1 == dueTime2)
sl@0
   281
			return ETrue;
sl@0
   282
		}
sl@0
   283
sl@0
   284
	if (!aScheduleState1.DueTime().IsUtc() && !aScheduleState2.DueTime().IsUtc())
sl@0
   285
		{
sl@0
   286
		TTime dueTime1 = aScheduleState1.DueTime().GetLocalTime();
sl@0
   287
		TTime dueTime2 = aScheduleState2.DueTime().GetLocalTime();
sl@0
   288
		
sl@0
   289
		if (aScheduleState1.Persists() == aScheduleState2.Persists()	&&	aScheduleState1.Name() == aScheduleState2.Name()
sl@0
   290
		&& 	aScheduleState1.Enabled() == aScheduleState2.Enabled()		&&	dueTime1 == dueTime2)
sl@0
   291
			return ETrue;
sl@0
   292
		}	
sl@0
   293
	
sl@0
   294
	return EFalse;
sl@0
   295
	}
sl@0
   296
sl@0
   297
EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::RandomScheduleEntryInfoHometime(TInt64& aSeed)
sl@0
   298
	{
sl@0
   299
	TScheduleEntryInfo2 entryInfo;
sl@0
   300
	entryInfo.SetIntervalType(STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed)));
sl@0
   301
	entryInfo.SetStartTime(TTsTime (TTime(TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed))), ETrue));
sl@0
   302
	entryInfo.SetInterval(Rand(1, 100, aSeed));
sl@0
   303
	entryInfo.SetValidityPeriod(Rand(1, 100, aSeed));
sl@0
   304
	return entryInfo;
sl@0
   305
	}
sl@0
   306
sl@0
   307
EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::RandomScheduleEntryInfoUtc(TInt64& aSeed)
sl@0
   308
	{
sl@0
   309
	TScheduleEntryInfo2 entryInfo;
sl@0
   310
	entryInfo.SetIntervalType(STATIC_CAST(TIntervalType, Rand(EHourly, EYearly, aSeed)));
sl@0
   311
	entryInfo.SetStartTime(TTsTime(TTime(TimeBasedOnOffset(Rand(0, 59, aSeed), Rand(0, 10, aSeed), Rand(0, 1, aSeed))), ETrue));
sl@0
   312
	entryInfo.SetInterval(Rand(1, 100, aSeed));
sl@0
   313
	entryInfo.SetValidityPeriod(Rand(1, 100, aSeed));
sl@0
   314
	return entryInfo;
sl@0
   315
	}
sl@0
   316
sl@0
   317
EXPORT_C TScheduleEntryInfo2 SchSvrHelpers::ScheduleEntryInfo(TIntervalType aType, const TTsTime& aStartTime, TInt aInterval, TTimeIntervalMinutes aValidPeriod)
sl@0
   318
	{
sl@0
   319
	TScheduleEntryInfo2 info (aStartTime, aType, aInterval, aValidPeriod);
sl@0
   320
	return info;
sl@0
   321
	}
sl@0
   322
sl@0
   323
// Pause execution for the specified number of seconds
sl@0
   324
EXPORT_C void SchSvrHelpers::Pause(TInt aPauseAmount)
sl@0
   325
	{
sl@0
   326
	//aTest.Printf(KPauseMessage, aPauseAmount);
sl@0
   327
	User::After(KOneSecond * aPauseAmount); // default is 2 seconds
sl@0
   328
	}
sl@0
   329
sl@0
   330
// PREQ234 support //
sl@0
   331
//
sl@0
   332
sl@0
   333
EXPORT_C void SchSvrHelpers::DeleteAllSchedulesL(RScheduler& aScheduler)
sl@0
   334
	{
sl@0
   335
	// First fetch task references so that we can delete all the tasks.
sl@0
   336
	CArrayFixFlat<TSchedulerItemRef>* refs = new (ELeave) CArrayFixFlat<TSchedulerItemRef>(3);
sl@0
   337
	CleanupStack::PushL(refs);
sl@0
   338
sl@0
   339
	{
sl@0
   340
	User::LeaveIfError(aScheduler.GetTaskRefsL(*refs, EAllSchedules, EAllTasks));
sl@0
   341
	const TInt count = refs->Count();
sl@0
   342
	// Delete all tasks
sl@0
   343
	for(TInt i=0; i<count; ++i)
sl@0
   344
		{
sl@0
   345
		const TSchedulerItemRef& ref = refs->At(i);
sl@0
   346
		User::LeaveIfError(aScheduler.DeleteTask(ref.iHandle));
sl@0
   347
		}
sl@0
   348
	}
sl@0
   349
	refs->Reset();
sl@0
   350
	{
sl@0
   351
	User::LeaveIfError(aScheduler.GetScheduleRefsL(*refs, EAllSchedules));
sl@0
   352
	// Delete all schedules
sl@0
   353
	const TInt count = refs->Count();
sl@0
   354
	for(TInt i=0; i<count; ++i)
sl@0
   355
		{
sl@0
   356
		const TSchedulerItemRef& ref = refs->At(i);
sl@0
   357
		User::LeaveIfError(aScheduler.DeleteSchedule(ref.iHandle));
sl@0
   358
		}
sl@0
   359
	}
sl@0
   360
	CleanupStack::PopAndDestroy(refs);
sl@0
   361
	}
sl@0
   362
sl@0
   363
// Deletes the task scheduler persisted files.
sl@0
   364
// It creates a separate process to do this as file to delete in in private
sl@0
   365
// data cage which requires all files capability which we don't want to 
sl@0
   366
// grant to all test exes.
sl@0
   367
EXPORT_C void SchSvrHelpers::DeleteScheduleFilesL()
sl@0
   368
	{
sl@0
   369
	_LIT(KScheduleDeleter, "TScheduleDeleter");
sl@0
   370
	TRequestStatus stat;
sl@0
   371
	RProcess p;
sl@0
   372
	User::LeaveIfError(p.Create(KScheduleDeleter, KNullDesC));
sl@0
   373
	// Asynchronous logon: completes when process terminates with process exit code
sl@0
   374
	p.Logon(stat);
sl@0
   375
	p.Resume();
sl@0
   376
sl@0
   377
	User::WaitForRequest(stat);
sl@0
   378
	TInt exitReason = p.ExitReason();
sl@0
   379
	p.Close();
sl@0
   380
	User::LeaveIfError(exitReason);
sl@0
   381
	}
sl@0
   382
	
sl@0
   383
// registers the client exe, leaving if it is not found 
sl@0
   384
EXPORT_C TInt SchSvrHelpers::RegisterClientL(RScheduler& aScheduler)
sl@0
   385
	{
sl@0
   386
	TFileName filename;
sl@0
   387
	filename = _L("MinimalTaskHandler");
sl@0
   388
sl@0
   389
	return aScheduler.Register(filename, 27);
sl@0
   390
	}
sl@0
   391
	
sl@0
   392
// registers the panicing client exe
sl@0
   393
EXPORT_C TInt SchSvrHelpers::RegisterPanicingClient(RScheduler& aScheduler)
sl@0
   394
	{
sl@0
   395
	TFileName filename;
sl@0
   396
	filename = _L("FaultyMinimalTaskHandler");
sl@0
   397
sl@0
   398
	return aScheduler.Register(filename, 27);
sl@0
   399
	}
sl@0
   400
	
sl@0
   401
// registers the a non existent exe
sl@0
   402
EXPORT_C TInt SchSvrHelpers::RegisterNonExistentClient(RScheduler& aScheduler)
sl@0
   403
	{
sl@0
   404
	TFileName filename;
sl@0
   405
	filename = _L("NonExistentClient");
sl@0
   406
sl@0
   407
	return aScheduler.Register(filename, 27);
sl@0
   408
	}
sl@0
   409
	
sl@0
   410
// Sets the system time to the given local time
sl@0
   411
// It creates a separate process to do this, 
sl@0
   412
// as this requires WDD capability which we don't want to grant to all test exes.
sl@0
   413
EXPORT_C TInt SchSvrHelpers::SetHomeTimeL(const TTime& aLocalTime)
sl@0
   414
	{
sl@0
   415
	
sl@0
   416
	_LIT(KSetHomeTime, "TSetHomeTime");
sl@0
   417
	_LIT(KTimeFormat, "%F%Y%M%D:%H%T%S.%*C6");
sl@0
   418
	
sl@0
   419
	RProcess p;	
sl@0
   420
	TRequestStatus stat;
sl@0
   421
	TBuf<128> bufLocalTime;	
sl@0
   422
	
sl@0
   423
	aLocalTime.FormatL(bufLocalTime, KTimeFormat);
sl@0
   424
	
sl@0
   425
	User::LeaveIfError(p.Create(KSetHomeTime, bufLocalTime));
sl@0
   426
	
sl@0
   427
	// Asynchronous logon: completes when process terminates with process exit code
sl@0
   428
	p.Logon(stat);
sl@0
   429
	p.Resume();
sl@0
   430
sl@0
   431
	User::WaitForRequest(stat);
sl@0
   432
	TInt exitReason = p.ExitReason();
sl@0
   433
	p.Close();
sl@0
   434
	return (exitReason);
sl@0
   435
	}
sl@0
   436
sl@0
   437
	
sl@0
   438
// Sets the UTC time to a specified time value
sl@0
   439
// It creates a separate process to do this, 
sl@0
   440
// as this requires WDD capability which we don't want to grant to all test exes.
sl@0
   441
EXPORT_C TInt SchSvrHelpers::SetUTCTimeL(const TTime& aUTCTime)
sl@0
   442
	{
sl@0
   443
	_LIT(KSetUTCTime, "TSetUTCTime");
sl@0
   444
	_LIT(KTimeFormat, "%F%Y%M%D:%H%T%S.%*C6");
sl@0
   445
	
sl@0
   446
	RProcess p;	
sl@0
   447
	TRequestStatus stat;
sl@0
   448
	TBuf<128> bufUTCTime;	
sl@0
   449
	
sl@0
   450
	aUTCTime.FormatL(bufUTCTime, KTimeFormat);	
sl@0
   451
sl@0
   452
	User::LeaveIfError(p.Create(KSetUTCTime, bufUTCTime));
sl@0
   453
		
sl@0
   454
	// Asynchronous logon: completes when process terminates with process exit code
sl@0
   455
	p.Logon(stat);
sl@0
   456
	p.Resume();
sl@0
   457
sl@0
   458
	User::WaitForRequest(stat);
sl@0
   459
	TInt exitReason = p.ExitReason();	
sl@0
   460
	p.Close();	
sl@0
   461
	return (exitReason);
sl@0
   462
	}
sl@0
   463
sl@0
   464
//This function is used in the test code to kill SCHSVR or MinimalTaskHandler
sl@0
   465
//processes (or some other) when they leftover and may cause OOM conditions.
sl@0
   466
// It creates a separate process to do this as killing a process requires
sl@0
   467
// PwrMgmt capability which we don't want to grant to all test exes.
sl@0
   468
EXPORT_C TInt CleanupHelpers::KillProcess(const TDesC& aProcessName)
sl@0
   469
	{
sl@0
   470
	_LIT(KProcessKiller, "TProcessKiller");
sl@0
   471
	TRequestStatus stat;
sl@0
   472
	RProcess p;
sl@0
   473
	TInt result = p.Create(KProcessKiller, aProcessName);
sl@0
   474
	
sl@0
   475
	if(result == KErrNone)
sl@0
   476
		{
sl@0
   477
		// Asynchronous logon: completes when process terminates with process exit code
sl@0
   478
		p.Logon(stat);
sl@0
   479
		p.Resume();
sl@0
   480
sl@0
   481
		User::WaitForRequest(stat);
sl@0
   482
		result = p.ExitReason();
sl@0
   483
		p.Close();			
sl@0
   484
		}
sl@0
   485
sl@0
   486
	return result;
sl@0
   487
	}
sl@0
   488
sl@0
   489
//Call this method before "TheTest(error == KErrNone);" statement.
sl@0
   490
//Otherwise if the error code is not KErrNone and the check fails, your 
sl@0
   491
//cleanup code never gets called - probably you will have low memory condition on the 
sl@0
   492
//test hardware and the rest of SCHSVR unit tests will fail sometimes and it will be very 
sl@0
   493
//hard to find out what is going on there.
sl@0
   494
EXPORT_C void CleanupHelpers::TestCleanupL()
sl@0
   495
	{
sl@0
   496
	// 5 second delay to clean up any launched processes 
sl@0
   497
	// left over by the test
sl@0
   498
	RDebug::Print(KPauseMessage, 5);
sl@0
   499
	User::After(5 * KOneSecond); 
sl@0
   500
sl@0
   501
	_LIT(KLogServerName, "SCHEXE");
sl@0
   502
	
sl@0
   503
	TInt err = CleanupHelpers::KillProcess(KLogServerName);
sl@0
   504
	//dont leave if the process was not found, or if it is already dead
sl@0
   505
	//but do leave if some other error occured
sl@0
   506
	if((err != KErrNotFound)&&(err != KErrDied))
sl@0
   507
		{
sl@0
   508
		User::LeaveIfError(err);	
sl@0
   509
		}
sl@0
   510
	}
sl@0
   511
	
sl@0
   512
sl@0
   513
//
sl@0
   514
//class STaskSemaphore
sl@0
   515
sl@0
   516
_LIT(KSchSemaphoreName, "SCHMinimalTaskHandler");
sl@0
   517
sl@0
   518
EXPORT_C void STaskSemaphore::CreateL()
sl@0
   519
	{
sl@0
   520
	//create semaphore and set it to 0
sl@0
   521
	User::LeaveIfError(iSemaphore.CreateGlobal(KSchSemaphoreName,0));
sl@0
   522
	}
sl@0
   523
sl@0
   524
EXPORT_C void STaskSemaphore::WaitL()
sl@0
   525
	{
sl@0
   526
	RSemaphore sem;
sl@0
   527
	User::LeaveIfError(sem.OpenGlobal(KSchSemaphoreName));
sl@0
   528
	sem.Wait();
sl@0
   529
	sem.Close();		
sl@0
   530
	}
sl@0
   531
sl@0
   532
EXPORT_C TInt STaskSemaphore::WaitL(TInt aTimeout)
sl@0
   533
	{
sl@0
   534
	RSemaphore sem;
sl@0
   535
	User::LeaveIfError(sem.OpenGlobal(KSchSemaphoreName));
sl@0
   536
	TInt r = sem.Wait(aTimeout);
sl@0
   537
	sem.Close();		
sl@0
   538
	return r;
sl@0
   539
	}
sl@0
   540
sl@0
   541
EXPORT_C void STaskSemaphore::Close()
sl@0
   542
	{
sl@0
   543
	iSemaphore.Close();
sl@0
   544
	}
sl@0
   545
	
sl@0
   546
	
sl@0
   547