os/mm/mmtestenv/mmtestfw/Source/TestFrameworkClient/TestStep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/mmtestenv/mmtestfw/Source/TestFrameworkClient/TestStep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1164 @@
     1.4 +// Copyright (c) 2002-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 +// EPOC includes
    1.20 +#include <e32base.h>
    1.21 +
    1.22 +// Test system includes
    1.23 +#include <testframework.h>
    1.24 +#include "TestIniData.h"
    1.25 +#include "asyncscheduler.h"
    1.26 +
    1.27 +// do not export if Unit Testing
    1.28 +#if defined (__TSU_TESTFRAMEWORK__)
    1.29 +#undef EXPORT_C
    1.30 +#define EXPORT_C
    1.31 +#endif
    1.32 +
    1.33 +static const TUint16*const KVerdictString[] = // must match up with TVerdict
    1.34 +	{
    1.35 +	_S("EPass"),
    1.36 +	_S("EFail"),
    1.37 +	_S("EInconclusive"),
    1.38 +	_S("ETestSuiteError"),
    1.39 +	_S("EAbort"),
    1.40 +	_S("EKnownFailure")
    1.41 +	};
    1.42 +
    1.43 +/**
    1.44 + *
    1.45 + * Test step constructor.
    1.46 + *
    1.47 + * @xxxx
    1.48 + *
    1.49 + */
    1.50 +EXPORT_C RTestStep::RTestStep()
    1.51 +	{
    1.52 +	iTestStepResult = EPass;
    1.53 +	iTestStepName.Zero();
    1.54 +	iSuite = NULL;
    1.55 +	iConfigData = NULL;
    1.56 +	iConfigDataAvailable = EFalse;
    1.57 +	iStackSize = KTestStackSize;
    1.58 +	iHeapSize = KMaxTestThreadHeapSize;
    1.59 +
    1.60 +	// NB : RTestStep has no destructor
    1.61 +	}
    1.62 +
    1.63 +/**
    1.64 + *
    1.65 + * CBase compatibility functionality.
    1.66 + *
    1.67 + * @xxxx
    1.68 + *
    1.69 + */
    1.70 +
    1.71 +
    1.72 +EXPORT_C TAny* RTestStep::operator new(TUint aSize, TAny* aBase) __NO_THROW
    1.73 +	{
    1.74 +	Mem::FillZ(aBase,aSize);
    1.75 +	return(aBase);
    1.76 +	}
    1.77 +
    1.78 +
    1.79 +EXPORT_C TAny* RTestStep::operator new(TUint aSize, TLeave)
    1.80 +	{
    1.81 +	return newL(aSize);		// will leave on alloc failure
    1.82 +	}
    1.83 +
    1.84 +EXPORT_C TAny* RTestStep::operator new(TUint aSize) __NO_THROW
    1.85 +	{
    1.86 +	TAny* pM=User::Alloc(aSize);
    1.87 +	if (pM)
    1.88 +		Mem::FillZ(pM,aSize);
    1.89 +	return(pM);
    1.90 +	}
    1.91 +
    1.92 +EXPORT_C TAny* RTestStep::newL(TUint aSize)
    1.93 +	{
    1.94 +	TAny* pM=User::AllocL(aSize);
    1.95 +	Mem::FillZ(pM,aSize);
    1.96 +	return pM;
    1.97 +	}
    1.98 +
    1.99 +EXPORT_C TAny* RTestStep::operator new(TUint aSize,TUint anExtraSize) __NO_THROW
   1.100 +	{
   1.101 +	aSize+=anExtraSize;
   1.102 +	TAny *pM=User::Alloc(aSize);
   1.103 +	if (pM)
   1.104 +		Mem::FillZ(pM,aSize);
   1.105 +	return(pM);
   1.106 +	}
   1.107 +
   1.108 +/**
   1.109 + *
   1.110 + * Pre-preamble for all test steps. This grows the cleanup stack to
   1.111 + * allow for allocation errors.
   1.112 + *
   1.113 + * @xxxx
   1.114 + *
   1.115 + */
   1.116 +EXPORT_C void RTestStep::PreOpenL()
   1.117 +	{
   1.118 +	TAny* dummyPtr = NULL;
   1.119 +	for(TInt i = 0 ; i < 20 ; i++)
   1.120 +		CleanupStack::PushL(dummyPtr); // Grow the cleanup stack.
   1.121 +	CleanupStack::PopAndDestroy(20);
   1.122 +	}
   1.123 +
   1.124 +/**
   1.125 + *
   1.126 + * Default empty implementation of OpenL (preamble).
   1.127 + * Test steps can override this to implement required code.
   1.128 + *
   1.129 + * @return	"TVerdict"
   1.130 + *			Result of preamble (should only be EPass or EInconclusive)
   1.131 + *
   1.132 + * @xxxx
   1.133 + *
   1.134 + */
   1.135 +EXPORT_C TVerdict RTestStep::OpenL()
   1.136 +	{
   1.137 +	// for backward compatibility with CTestStep
   1.138 +	return DoTestStepPreambleL();
   1.139 +	}
   1.140 +
   1.141 +/**
   1.142 + *
   1.143 + * Default empty implementation of CleanupAfterOpenFail (preamble cleanup).
   1.144 + * Test steps can override this to implement required code.
   1.145 + *
   1.146 + * @xxxx
   1.147 + *
   1.148 + */
   1.149 +EXPORT_C void RTestStep::CleanupAfterOpenFail()
   1.150 +	{
   1.151 +	// default empty implementation
   1.152 +	// a step should implement its own method if required
   1.153 +	}
   1.154 +
   1.155 +/**
   1.156 + *
   1.157 + * Default empty implementation of Close (postamble)
   1.158 + * Test steps can override this to implement required code.
   1.159 + *
   1.160 + * NB this does NOT leave - any leaves should be trapped and panicked.
   1.161 + *
   1.162 + * @xxxx
   1.163 + *
   1.164 + */
   1.165 +EXPORT_C void RTestStep::Close()
   1.166 +	{
   1.167 +	// for backward compatibility with CTestStep
   1.168 +	_LIT(KPanicStr, "RTestStep::Close");
   1.169 +	TVerdict ver = EPass;
   1.170 +	TRAPD(err, ver = DoTestStepPostambleL());
   1.171 +	if(err != KErrNone)
   1.172 +		User::Panic(KPanicStr, 0);
   1.173 +	// any DoTestStepPostambleL() which returns EInconclusive should be panicked
   1.174 + 	if(ver != EPass)
   1.175 +		User::Panic(KPanicStr, 1);
   1.176 +
   1.177 +	}
   1.178 +
   1.179 +/**
   1.180 + *
   1.181 + * Set the test suite
   1.182 + *
   1.183 + * @param	"CTestSuite*"
   1.184 + *			The test suite
   1.185 + *
   1.186 + * @xxxx
   1.187 + *
   1.188 + */
   1.189 +EXPORT_C void RTestStep::SetSuite(CTestSuite* aSuite)
   1.190 +	{
   1.191 +	iSuite = aSuite;
   1.192 +	}
   1.193 +
   1.194 +/**
   1.195 + *
   1.196 + * Set the test result
   1.197 + *
   1.198 + * @param	"TVerdict"
   1.199 + *			The test result
   1.200 + *
   1.201 + * @xxxx
   1.202 + *
   1.203 + */
   1.204 +EXPORT_C void RTestStep::SetResult(TVerdict aResult)
   1.205 +	{
   1.206 +	iTestStepResult = aResult;
   1.207 +	}
   1.208 +
   1.209 +/**
   1.210 + *
   1.211 + * Get the step name
   1.212 + *
   1.213 + * @return	"TPtrC"
   1.214 + *			The step name
   1.215 + *
   1.216 + * @xxxx
   1.217 + *
   1.218 + */
   1.219 +EXPORT_C TPtrC RTestStep::StepName() const
   1.220 +	{
   1.221 +	return iTestStepName;
   1.222 +	}
   1.223 +
   1.224 +/**
   1.225 + *
   1.226 + * General logging function for test steps.
   1.227 + *
   1.228 + * @param	"TRefByValue<const TDesC16> aFmt"
   1.229 + *			Printf-style aFmt.
   1.230 + *
   1.231 + * @param	"..."
   1.232 + *			Variable print parameters
   1.233 + *
   1.234 + * @xxxx
   1.235 + *
   1.236 + */
   1.237 +EXPORT_C void RTestStep::Log(TRefByValue<const TDesC16> aFmt, ...)
   1.238 +	{
   1.239 +    
   1.240 +	VA_LIST aList;
   1.241 +	VA_START(aList, aFmt);
   1.242 +
   1.243 +	TIntegrationTestLog16Overflow iOverflow16;
   1.244 +
   1.245 +	// decode formatted data for display on console
   1.246 +	TBuf<KMaxLogLineLength> lineBuf;
   1.247 +	lineBuf.AppendFormatList(aFmt, aList, &iOverflow16);
   1.248 +
   1.249 +	// send the data to the log system via the suite
   1.250 +	iSuite->Log(_L("%S"),&lineBuf);
   1.251 +
   1.252 +	VA_END(aList); 
   1.253 +
   1.254 +	}
   1.255 +
   1.256 +/**
   1.257 + *
   1.258 + * General logging function for test steps, with severity.
   1.259 + *
   1.260 + * @param	"TInt aSeverity"
   1.261 + *			Severity level required to log
   1.262 + *
   1.263 + * @param	"TRefByValue<const TDesC16> aFmt"
   1.264 + *			Printf-style aFmt.
   1.265 + *
   1.266 + * @param	"..."
   1.267 + *			Variable print parameters
   1.268 + *
   1.269 + * @xxxx
   1.270 + *
   1.271 + */
   1.272 +EXPORT_C void RTestStep::Log( TInt aSeverity, TRefByValue<const TDesC16> aFmt, ... )
   1.273 +{
   1.274 +	VA_LIST aList;
   1.275 +	VA_START(aList, aFmt);
   1.276 +
   1.277 +	TIntegrationTestLog16Overflow iOverflow16;
   1.278 +
   1.279 +	// decode formatted data for display on console
   1.280 +	TBuf<KMaxLogLineLength> lineBuf;
   1.281 +	lineBuf.AppendFormatList(aFmt, aList, &iOverflow16);
   1.282 +
   1.283 +	// send the data to the log system via the suite
   1.284 +	if(LogSeverity::IsActive(aSeverity, iSuite->Severity()))
   1.285 +		iSuite->Log(aSeverity, _L("%S"),&lineBuf);
   1.286 +
   1.287 +	VA_END(aList); 
   1.288 +}
   1.289 +
   1.290 +/**
   1.291 + *
   1.292 + * Traceable logging function for test steps.
   1.293 + *
   1.294 + * @param	"const TText8* aFile"
   1.295 + *			Source code file name
   1.296 + *
   1.297 + * @param	"TInt aLine"
   1.298 + *			Source code line
   1.299 + *
   1.300 + * @param	"TInt aSeverity"
   1.301 + *			Severity level required to log
   1.302 + *
   1.303 + * @param	"TRefByValue<const TDesC16> aFmt"
   1.304 + *			Printf-style format.
   1.305 + *
   1.306 + * @param	"..."
   1.307 + *			Variable print parameters
   1.308 + *
   1.309 + * @xxxx
   1.310 + *
   1.311 + */
   1.312 +EXPORT_C void RTestStep::LogExtra(const TText8* aFile, TInt aLine, TInt aSeverity,
   1.313 +		TRefByValue<const TDesC16> aFmt,...)
   1.314 +	{
   1.315 +	VA_LIST aList;
   1.316 +	VA_START(aList, aFmt);
   1.317 +
   1.318 +	TIntegrationTestLog16Overflow iOverflow16;
   1.319 +
   1.320 +	// decode formatted data for display on console
   1.321 +	TBuf<KMaxLogLineLength> lineBuf;
   1.322 +	lineBuf.AppendFormatList(aFmt, aList, &iOverflow16);
   1.323 +
   1.324 +	// send the data to the log system via the suite
   1.325 +	if(LogSeverity::IsActive(aSeverity, iSuite->Severity()))
   1.326 +		iSuite->LogExtra(aFile, aLine, aSeverity, lineBuf);
   1.327 +
   1.328 +	VA_END(aList); 
   1.329 +	}
   1.330 +	
   1.331 +/**
   1.332 +Set default paramSet
   1.333 +Test steps can use this when looking up configs, to provide a level of script control
   1.334 +*/
   1.335 +void RTestStep::SetDefaultParamSet(const TDesC& aParamSet)
   1.336 +	{
   1.337 +	iDefaultParamSet.Set(aParamSet);
   1.338 +	}
   1.339 +
   1.340 +/**
   1.341 + *
   1.342 + * Load a configuration file.
   1.343 + * If successful, data member iConfigDataAvailable is set.
   1.344 + *
   1.345 + * @param	"TPtrC aConfig"
   1.346 + *			The configuration file name.
   1.347 + *
   1.348 + * @xxxx
   1.349 + *
   1.350 + */
   1.351 +EXPORT_C void RTestStep::LoadConfig(const TDesC& aConfig)
   1.352 +	{
   1.353 +
   1.354 +	// if a config file supplied then use
   1.355 +	if (aConfig.Length() != 0)
   1.356 +		{
   1.357 +
   1.358 +		// get the full pathname default drive name and extension
   1.359 +		_LIT(KRelated,"C:\\config.ini"); 
   1.360 +		TParse configFileName;
   1.361 +		TInt returnCode = configFileName.Set(aConfig, &KRelated, NULL);
   1.362 +
   1.363 +		if (returnCode != KErrNone)
   1.364 +			{
   1.365 +			// error opening FileManager
   1.366 +			ERR_PRINTF2(_L("Error opening config file %S"), &(configFileName.FullName())); 
   1.367 +			}
   1.368 +
   1.369 +		// create and load the CTestIniData object
   1.370 +		TRAPD(r, iConfigData = CTestIniData::NewL(configFileName.FullName()));
   1.371 +		
   1.372 +		// check if loaded ok
   1.373 +		if (r == KErrNone)
   1.374 +			{
   1.375 +			// loaded ok
   1.376 +			iConfigDataAvailable = ETrue;
   1.377 +			}
   1.378 +		else
   1.379 +			{
   1.380 +			// failed to load
   1.381 +			iConfigDataAvailable = EFalse;
   1.382 +			iConfigData = NULL;
   1.383 +
   1.384 +			// report error 
   1.385 +			TPtrC errortxt = CLog::EpocErrorToText(r);
   1.386 +			ERR_PRINTF2(_L("Failed to load config data file - error %S"), &errortxt);
   1.387 +			}
   1.388 +		}
   1.389 +	}
   1.390 +
   1.391 +/**
   1.392 + *
   1.393 + * Unload any existing configuration data.
   1.394 + *
   1.395 + * @xxxx
   1.396 + *
   1.397 + */
   1.398 +EXPORT_C void RTestStep::UnloadConfig()
   1.399 +	{
   1.400 +	iConfigDataAvailable = EFalse;
   1.401 +
   1.402 +	// clean up Config data object
   1.403 +	delete iConfigData;
   1.404 +	iConfigData = NULL;
   1.405 +
   1.406 +	}
   1.407 +
   1.408 +/**
   1.409 + *
   1.410 + * Check the config file for a boolean value
   1.411 + *
   1.412 + * @param	"const TDesC &aSectName"
   1.413 + *			Section name to check in
   1.414 + *
   1.415 + * @param	"const TDesC &aKeyName"
   1.416 + *			Key name to check for
   1.417 + *
   1.418 + * @param	"Bool &aResult"
   1.419 + *			TBool returned from config file
   1.420 + *
   1.421 + * @return	"TBool"
   1.422 + *			Result (ETrue if found)
   1.423 + *
   1.424 + * @xxxx
   1.425 + *
   1.426 + */
   1.427 +EXPORT_C TBool RTestStep::GetBoolFromConfig(const TDesC &aSectName, const TDesC &aKeyName, TBool &aResult)
   1.428 +	{
   1.429 +	// check file available
   1.430 +	if (!iConfigDataAvailable)
   1.431 +		{
   1.432 +		ERR_PRINTF1(_L("No config file available"));
   1.433 +		return EFalse;
   1.434 +		}
   1.435 +
   1.436 +	TBool ret = EFalse;
   1.437 +	TPtrC result;
   1.438 +
   1.439 +	// get the value 
   1.440 +	ret = iConfigData->FindVar(aSectName, aKeyName, result);
   1.441 +
   1.442 +	// if failed to decode display error
   1.443 +	if (!ret) 
   1.444 +		{
   1.445 +		// display error message
   1.446 +		ERR_PRINTF3(_L("Failed to read section:%S key:%S "),
   1.447 +				&aSectName, &aKeyName );
   1.448 +
   1.449 +		// return fail
   1.450 +		return EFalse;
   1.451 +		}
   1.452 +
   1.453 +	// set result as a TBool
   1.454 +	if (result.FindF(_L("true")) == KErrNotFound)
   1.455 +		aResult = EFalse;
   1.456 +	else
   1.457 +		aResult = ETrue;
   1.458 +	// return success
   1.459 +	return ETrue;
   1.460 +	}
   1.461 +
   1.462 +/**
   1.463 + *
   1.464 + * Check the config file for a TInt value
   1.465 + *
   1.466 + * @param	"const TDesC &aSectName"
   1.467 + *			Section name to check in
   1.468 + *
   1.469 + * @param	"const TDesC &aKeyName"
   1.470 + *			Key name to check for
   1.471 + *
   1.472 + * @param	"TInt &aResult"
   1.473 + *			TInt returned from config file
   1.474 + *
   1.475 + * @return	"TBool"
   1.476 + *			Result (ETrue if found)
   1.477 + *
   1.478 + * @xxxx
   1.479 + *
   1.480 + */
   1.481 +EXPORT_C TBool RTestStep::GetIntFromConfig(const TDesC &aSectName, const TDesC &aKeyName, TInt &aResult)
   1.482 +	{
   1.483 +	// check file available
   1.484 +	if ( !iConfigDataAvailable )
   1.485 +		{
   1.486 +		ERR_PRINTF1(_L("No config file available"));
   1.487 +		return EFalse;
   1.488 +		}	
   1.489 +
   1.490 +	TBool ret = EFalse;
   1.491 +	TPtrC result;
   1.492 +
   1.493 +	// get the value 
   1.494 +	ret = iConfigData->FindVar(aSectName, aKeyName, result);
   1.495 +
   1.496 +	// if failed to decode display error
   1.497 +	if (!ret) 
   1.498 +		{
   1.499 +		// display error message
   1.500 +		ERR_PRINTF3(_L("Failed to read section:%S key:%S "),
   1.501 +				&aSectName, &aKeyName );
   1.502 +
   1.503 +		// return fail
   1.504 +		return EFalse;
   1.505 +		}
   1.506 +
   1.507 +	// use TLex to convert to a TInt
   1.508 +	TLex lex(result);
   1.509 +	if (lex.Val(aResult) == KErrNone)
   1.510 +		return ETrue;
   1.511 +	else
   1.512 +		return EFalse;
   1.513 +}
   1.514 +
   1.515 +/**
   1.516 + *
   1.517 + * Check the config file for a string TPtr value
   1.518 + *
   1.519 + * @param	"const TDesC &aSectName"
   1.520 + *			Section name to check in
   1.521 + *
   1.522 + * @param	"const TDesC &aKeyName"
   1.523 + *			Key name to check for
   1.524 + *
   1.525 + * @param	"TPtrC &aResult"
   1.526 + *			String returned from config file
   1.527 + *
   1.528 + * @return	"TBool"
   1.529 + *			Result (ETrue if found)
   1.530 + *
   1.531 + * @xxxx
   1.532 + *
   1.533 + */
   1.534 +EXPORT_C TBool RTestStep::GetStringFromConfig(const TDesC &aSectName, const TDesC &aKeyName, TPtrC &aResult)
   1.535 +	{
   1.536 +	// check file available
   1.537 +	if (!iConfigDataAvailable)
   1.538 +		{
   1.539 +		ERR_PRINTF1(_L("No config file available"));
   1.540 +		return EFalse;
   1.541 +		}	
   1.542 +
   1.543 +	// get the value 
   1.544 +	TBool ret = iConfigData->FindVar(aSectName, aKeyName, aResult);
   1.545 +
   1.546 +	// if failed to decode display error
   1.547 +	if (ret == EFalse) 
   1.548 +		{
   1.549 +		ERR_PRINTF3(_L("Failed to read section:%S key:%S "),
   1.550 +				&aSectName, &aKeyName );
   1.551 +		}
   1.552 +
   1.553 +	return ret;
   1.554 +}
   1.555 +
   1.556 +/**
   1.557 + * Reads the value present from the test steps ini file within the mentioned section name and key name
   1.558 + * Copies the value to the TInt reference passed in
   1.559 + * @param aSectName - Section within the test steps ini file
   1.560 + * @param aKeyName - Name of a key within a section
   1.561 + * @return aResult - The integer value of the Hex input
   1.562 + * @return TBool - ETrue for found, EFalse for not found 
   1.563 + */	
   1.564 +EXPORT_C TBool RTestStep::GetHexFromConfig(const TDesC &aSectName,const TDesC &aKeyName,TInt &aResult)
   1.565 +	{
   1.566 +	TPtrC result;
   1.567 +	if(!iConfigData)
   1.568 +		return EFalse;
   1.569 +	if(!iConfigData->FindVar(aSectName, aKeyName, result))
   1.570 +		return EFalse;
   1.571 +	TLex lex(result);
   1.572 +	TInt err = lex.Val((TUint &)aResult, EHex);
   1.573 +	if(err)
   1.574 +		return EFalse;
   1.575 +	
   1.576 +	return(ETrue);
   1.577 +	}
   1.578 +	
   1.579 +/**
   1.580 + *
   1.581 + * Default empty implementation of DoTestStepPreambleL.
   1.582 + * Test steps can override this to implement required code.
   1.583 + *
   1.584 + * @return	"TVerdict"
   1.585 + *			Result of preamble (should only be EPass or EInconclusive)
   1.586 + *
   1.587 + * @xxxx
   1.588 + *
   1.589 + */
   1.590 +// for backward compatibility with CTestStep
   1.591 +EXPORT_C TVerdict RTestStep::DoTestStepPreambleL()
   1.592 +	{
   1.593 +	return EPass;
   1.594 +	}
   1.595 +
   1.596 +/**
   1.597 + *
   1.598 + * Default empty implementation of DoTestStepPostambleL.
   1.599 + * Test steps can override this to implement required code.
   1.600 + *
   1.601 + * @return	"TVerdict"
   1.602 + *			Result of postamble (should only be EPass or EInconclusive)
   1.603 + *
   1.604 + * @xxxx
   1.605 + *
   1.606 + */
   1.607 +// for backward compatibility with CTestStep
   1.608 +EXPORT_C TVerdict RTestStep::DoTestStepPostambleL()
   1.609 +	{
   1.610 +	return EPass;
   1.611 +	}
   1.612 +
   1.613 +/**
   1.614 + *
   1.615 + * Traceable Boolean condition tester.
   1.616 + * If the condition is not true, record an error.
   1.617 + *
   1.618 + * @param	"TBool aCondition"
   1.619 + *			Condition to be checked
   1.620 + *
   1.621 + * @param	"const TText8* aFile"
   1.622 + *			Source code file name
   1.623 + *
   1.624 + * @param	"TInt aLine"
   1.625 + *			Source code line
   1.626 + *
   1.627 + * @xxxx
   1.628 + *
   1.629 + */
   1.630 +EXPORT_C void RTestStep::TestBooleanTrue(TBool aCondition, const TText8* aFile, TInt aLine)
   1.631 +	{
   1.632 +
   1.633 +	// check condition
   1.634 +	if (aCondition)
   1.635 +		return;
   1.636 +
   1.637 +	// this is only relevant if the current result is pass
   1.638 +	if (iTestStepResult == EPass)
   1.639 +		iTestStepResult = EFail;
   1.640 +
   1.641 +	// convert filename for log
   1.642 +	TBuf<KMaxLogFilenameLength> fileName;
   1.643 +	TPtrC8 fileName8(aFile);
   1.644 +	fileName.Copy(fileName8);  // TText8->TBuf16
   1.645 +
   1.646 +	// display a log message
   1.647 + 	ERR_PRINTF3(_L("Test Failed in file:%S line:%d"), &fileName, aLine);
   1.648 +
   1.649 +	}
   1.650 +
   1.651 +/**
   1.652 + *
   1.653 + * Traceable Boolean condition tester.
   1.654 + * If the condition is not true, record an error and leave.
   1.655 + *
   1.656 + * @param	"TBool aCondition"
   1.657 + *			Condition to be checked
   1.658 + *
   1.659 + * @param	"const TText8* aFile"
   1.660 + *			Source code file name
   1.661 + *
   1.662 + * @param	"TInt aLine"
   1.663 + *			Source code line
   1.664 + *
   1.665 + * @xxxx
   1.666 + *
   1.667 + */
   1.668 +EXPORT_C void RTestStep::TestBooleanTrueL(TBool aCondition, const TText8* aFile, TInt aLine)
   1.669 +	{
   1.670 +
   1.671 +	// check condition
   1.672 +	if (aCondition)
   1.673 +		return;
   1.674 +
   1.675 +	// this is only relevant if the current result is pass
   1.676 +	if (iTestStepResult == EPass)
   1.677 +		iTestStepResult = EFail;
   1.678 +
   1.679 +	// convert filename for log
   1.680 +	TBuf<KMaxLogFilenameLength> fileName;
   1.681 +	TPtrC8 fileName8(aFile);
   1.682 +	fileName.Copy(fileName8);  // TText8->TBuf16
   1.683 +
   1.684 +	// display a log message
   1.685 + 	ERR_PRINTF3(_L("Test Failed in file:%S line:%d"), &fileName, aLine);
   1.686 +
   1.687 +	// leave with error code
   1.688 +	User::Leave(KTestErrorCode);
   1.689 +
   1.690 +	}
   1.691 +
   1.692 +/**
   1.693 + *
   1.694 + * Traceable Boolean condition tester.
   1.695 + * If the condition is not true, record an error with the supplied 
   1.696 + * error code, and leave.
   1.697 + *
   1.698 + * @param	"TBool aCondition"
   1.699 + *			Condition to be checked
   1.700 + *
   1.701 + * @param	"TInt aErrorCode"
   1.702 + *			Supplied error code
   1.703 + *
   1.704 + * @param	"const TText8* aFile"
   1.705 + *			Source code file name
   1.706 + *
   1.707 + * @param	"TInt aLine"
   1.708 + *			Source code line
   1.709 + *
   1.710 + * @xxxx
   1.711 + *
   1.712 + */
   1.713 +EXPORT_C void RTestStep::TestBooleanTrueWithErrorCodeL(TBool aCondition, TInt aErrorCode, const TText8* aFile, TInt aLine)
   1.714 +	{
   1.715 +	// check condition
   1.716 +	if (aCondition)
   1.717 +		return;
   1.718 +
   1.719 +	// this is only relevant if the current result is pass
   1.720 +	if (iTestStepResult == EPass)
   1.721 +		iTestStepResult = EFail;
   1.722 +
   1.723 +	// convert filename for log
   1.724 +	TBuf<KMaxLogFilenameLength> fileName;
   1.725 +	TPtrC8 fileName8(aFile);
   1.726 +	fileName.Copy(fileName8);  // TText8->TBuf16
   1.727 +
   1.728 +	// get the error text
   1.729 +	TPtrC errortxt = CLog::EpocErrorToText(aErrorCode);
   1.730 +
   1.731 +	// display a log message
   1.732 +	ERR_PRINTF4(_L("Test Failed with error:%S in file:%S line:%d"),
   1.733 +			&errortxt, &fileName, aLine);
   1.734 +
   1.735 +	// leave with error code
   1.736 +	User::Leave(aErrorCode);
   1.737 +	
   1.738 +	}
   1.739 +
   1.740 +/**
   1.741 + *
   1.742 + * Traceable Boolean condition tester.
   1.743 + * If the condition is not true, record an error with the supplied 
   1.744 + * error code.
   1.745 + *
   1.746 + * @param	"TBool aCondition"
   1.747 + *			Condition to be checked
   1.748 + *
   1.749 + * @param	"TInt aErrorCode"
   1.750 + *			Supplied error code
   1.751 + *
   1.752 + * @param	"const TText8* aFile"
   1.753 + *			Source code file name
   1.754 + *
   1.755 + * @param	"TInt aLine"
   1.756 + *			Source code line
   1.757 + *
   1.758 + * @xxxx
   1.759 + *
   1.760 + */
   1.761 +EXPORT_C void RTestStep::TestBooleanTrueWithErrorCode(TBool aCondition, TInt aErrorCode, const TText8* aFile, TInt aLine)
   1.762 +	{
   1.763 +	// check condition
   1.764 +	if (aCondition)
   1.765 +		return;
   1.766 +
   1.767 +	// this is only relevant if the current result is pass
   1.768 +	if (iTestStepResult == EPass)
   1.769 +		iTestStepResult = EFail;
   1.770 +
   1.771 +	// convert filename for log
   1.772 +	TBuf<KMaxLogFilenameLength> fileName;
   1.773 +	TPtrC8 fileName8(aFile);
   1.774 +	fileName.Copy(fileName8);  // TText8->TBuf16
   1.775 +
   1.776 +	// get the error text
   1.777 +	TPtrC errortxt = CLog::EpocErrorToText(aErrorCode);
   1.778 +
   1.779 +	// display a log message
   1.780 +	ERR_PRINTF4(_L("Test Failed with error:%S in file:%S line:%d"),
   1.781 +			&errortxt, &fileName, aLine);
   1.782 +	}
   1.783 +
   1.784 +/**
   1.785 + *
   1.786 + * Traceable checkpoint tester.
   1.787 + * If the value does not match expected, record an error with supplied
   1.788 + * text string, and leave.
   1.789 + *
   1.790 + * @param	"TInt aVal"
   1.791 + *			Value to be checked
   1.792 + *
   1.793 + * @param	"TInt aExpectedVal"
   1.794 + *			Value expected
   1.795 + *
   1.796 + * @param	"const TDesC& aText"
   1.797 + *			Supplied text string
   1.798 + *
   1.799 + * @param	"const TText8* aFile"
   1.800 + *			Source code file name
   1.801 + *
   1.802 + * @param	"TInt aLine"
   1.803 + *			Source code line
   1.804 + *
   1.805 + * @xxxx
   1.806 + *
   1.807 + */
   1.808 +EXPORT_C void RTestStep::TestCheckPointCompareL(TInt aVal, TInt aExpectedVal, 
   1.809 +												  const TDesC& aText, const TText8* aFile, TInt aLine)
   1.810 +	{
   1.811 +	if(aVal != aExpectedVal)
   1.812 +		{
   1.813 +		// this is only relevant if the current result is pass
   1.814 +		if (iTestStepResult == EPass)
   1.815 +			iTestStepResult = EFail;
   1.816 +
   1.817 +		// convert filename for log
   1.818 +		TBuf<KMaxLogFilenameLength> fileName;
   1.819 +		TPtrC8 fileName8(aFile);
   1.820 +		fileName.Copy(fileName8);  // TText8->TBuf16
   1.821 +
   1.822 +		ERR_PRINTF6(_L("FAILED test:  Val = %d Exp Val = %d %S in file:%S line:%d"), 
   1.823 +			aVal, aExpectedVal, &aText, &fileName, aLine);
   1.824 +
   1.825 +		User::Leave(aVal);
   1.826 +		}
   1.827 +	}
   1.828 +
   1.829 +/** 
   1.830 + *
   1.831 + * Accessors for stack / heap size
   1.832 + *
   1.833 + * NB - These can only be set from within the RTestStep derived constructor itself - there
   1.834 + * are no setter accessors.
   1.835 + *
   1.836 + * @xxxx
   1.837 + *
   1.838 + */
   1.839 +EXPORT_C TInt RTestStep::StackSize() const
   1.840 +{
   1.841 +	return iStackSize;
   1.842 +}
   1.843 +
   1.844 +EXPORT_C TInt RTestStep::HeapSize() const
   1.845 +{
   1.846 +	return iHeapSize;
   1.847 +}
   1.848 +
   1.849 +
   1.850 +/** 
   1.851 + *
   1.852 + * Virtual destructor for CTestStep
   1.853 + * Provided for backward compatibility ONLY
   1.854 + *
   1.855 + * @xxxx
   1.856 + *
   1.857 + */
   1.858 +EXPORT_C CTestStep::~CTestStep()
   1.859 +{
   1.860 +}
   1.861 +
   1.862 +//
   1.863 +// RAsyncTestStep
   1.864 +//
   1.865 +
   1.866 +EXPORT_C RAsyncTestStep::RAsyncTestStep():
   1.867 +	iReason (KErrNone),
   1.868 +	iResult (EPass),
   1.869 +	iStartAO (NULL),
   1.870 +	iActiveSchedulerWait (NULL),
   1.871 +	iStarted (EFalse)
   1.872 +	{
   1.873 +	}
   1.874 +
   1.875 +EXPORT_C TVerdict RAsyncTestStep::DoTestStepL()
   1.876 +	{
   1.877 +	// allow recalls to same test step (?), so re-initialise the basic variables. Can't delete
   1.878 +	// as if in that case, old heap would be dead
   1.879 +	iReason = KErrNone;
   1.880 +	iResult = EPass;
   1.881 +	iScheduler = NULL;
   1.882 +	iActiveSchedulerWait = NULL;
   1.883 +	
   1.884 +	iScheduler = new (ELeave) CAsyncTestActiveScheduler(*this);	
   1.885 +	CActiveScheduler::Install(iScheduler);
   1.886 +	iActiveSchedulerWait = new (ELeave) CActiveSchedulerWait();
   1.887 +	
   1.888 +	// arrange for DoCallBack() to be called as the first thing. Use low priority to reduce
   1.889 +	// the overhead of an extra AO on everything else
   1.890 +	TCallBack callback (CallBack, this);
   1.891 +	iStartAO = NULL;
   1.892 +	iStartAO = new (ELeave) CAsyncCallBack (callback, CActive::EPriorityIdle); 
   1.893 +	iStartAO->Call();
   1.894 +	
   1.895 +	iStarted = ETrue; // obviously do this before we start, as can't once it is
   1.896 +	iActiveSchedulerWait->Start();
   1.897 +	return CheckTestResult();
   1.898 +	}
   1.899 +
   1.900 +EXPORT_C TVerdict RAsyncTestStep::DoTestStepPostambleL()
   1.901 +	{
   1.902 +	CloseTest();
   1.903 +	delete iStartAO; // no need to Cancel 
   1.904 +	iStartAO = NULL;
   1.905 +	delete iActiveSchedulerWait;
   1.906 +	iActiveSchedulerWait = NULL;
   1.907 +	delete iScheduler; 
   1.908 +	iScheduler = NULL;
   1.909 +	return EPass;
   1.910 +	}
   1.911 +	
   1.912 +EXPORT_C void RAsyncTestStep::StopTest()
   1.913 +	{
   1.914 +	StopTest(KErrNone);
   1.915 +	}
   1.916 +	
   1.917 +EXPORT_C void RAsyncTestStep::StopTest(TInt aReason)
   1.918 +	{
   1.919 +	TVerdict resultToUse = (aReason==KErrNone) ? EPass : EFail;
   1.920 +	StopTest (aReason, resultToUse);
   1.921 +	}
   1.922 +
   1.923 +EXPORT_C void RAsyncTestStep::StopTest(TInt aReason, TVerdict aResult)
   1.924 +	{
   1.925 +	// note if stop is called multiple times, we record last
   1.926 +	// non-KErrNone reason and last non-Pass result, but only actually stop once
   1.927 +	if (aReason!=KErrNone)
   1.928 +		{
   1.929 +		iReason = aReason;		
   1.930 +		}
   1.931 +	SetResult(aResult);
   1.932 +	if (iStarted)
   1.933 +		{
   1.934 +		iStarted = EFalse;
   1.935 +		iActiveSchedulerWait->AsyncStop();
   1.936 +		}
   1.937 +	}
   1.938 +	
   1.939 +EXPORT_C TInt RAsyncTestStep::Reason() const
   1.940 +	{
   1.941 +	return iReason;
   1.942 +	}
   1.943 +	
   1.944 +EXPORT_C TVerdict RAsyncTestStep::Result() const
   1.945 +	{
   1.946 +	return iResult;
   1.947 +	}
   1.948 +	
   1.949 +void RAsyncTestStep::SetResult(TVerdict aResult)
   1.950 +	{
   1.951 +	// remember the last non-Pass result
   1.952 +	if (aResult!=EPass)
   1.953 +		{
   1.954 +		iResult = aResult;
   1.955 +		}	
   1.956 +	}
   1.957 +	
   1.958 +EXPORT_C TVerdict RAsyncTestStep::CheckTestResult()
   1.959 +	{
   1.960 +	TVerdict result = Result();
   1.961 +	
   1.962 +	if (result!=EPass) // if the result is a Pass, even if error is too we treat as test pass
   1.963 +		{
   1.964 +		INFO_PRINTF3(_L("Failed test with error %d, result %s"), Reason(), KVerdictString[result]);
   1.965 +		}
   1.966 +	return result;
   1.967 +	}
   1.968 +
   1.969 +TInt RAsyncTestStep::CallBack(TAny* aPtr)
   1.970 +	{
   1.971 +	RAsyncTestStep* self = static_cast<RAsyncTestStep*> (aPtr);
   1.972 +	self->DoCallBack(); // if this fails, it will stop itself
   1.973 +	return KErrNone;
   1.974 +	}
   1.975 +	
   1.976 +void RAsyncTestStep::DoCallBack()
   1.977 +	{
   1.978 +	TRAPD(error, KickoffTestL()); 
   1.979 +	if (error!=KErrNone)
   1.980 +		{
   1.981 +		StopTest(error); // if kickoff fails, we stop here
   1.982 +		}	
   1.983 +	}
   1.984 +
   1.985 +void RAsyncTestStep::HandleError(TInt aError)
   1.986 +	{
   1.987 +	INFO_PRINTF2(_L("ActiveScheduler::Error(%d)"), aError);
   1.988 +	StopTest(aError);
   1.989 +	}
   1.990 +	
   1.991 +//
   1.992 +// CBusyTestUnit
   1.993 +//
   1.994 +
   1.995 +CBusyTestUnit::CBusyTestUnit():
   1.996 +		iPercentBusy(0), iThreadPriority(EPriorityNormal)
   1.997 +	{
   1.998 +	// non exported default constructor to enforce non-derivation
   1.999 +	}
  1.1000 +
  1.1001 +CBusyTestUnit::CBusyTestUnit(TInt aPercentBusy, TThreadPriority aThreadPriority):
  1.1002 +		iPercentBusy(aPercentBusy), iThreadPriority(aThreadPriority)
  1.1003 +	{
  1.1004 +	ASSERT(aPercentBusy>=0 && aPercentBusy<=100); // assume this
  1.1005 +	}
  1.1006 +	
  1.1007 +void CBusyTestUnit::ConstructL()
  1.1008 +	{
  1.1009 +	}
  1.1010 +	
  1.1011 +EXPORT_C CBusyTestUnit* CBusyTestUnit::NewLC(TInt aPercentBusy, TThreadPriority aThreadPriority)
  1.1012 +	{
  1.1013 +	CBusyTestUnit* self = new (ELeave) CBusyTestUnit(aPercentBusy, aThreadPriority);
  1.1014 +	CleanupStack::PushL(self);
  1.1015 +	self->ConstructL();
  1.1016 +	return self;
  1.1017 +	}
  1.1018 +	
  1.1019 +EXPORT_C CBusyTestUnit* CBusyTestUnit::NewL(TInt aPercentBusy, TThreadPriority aThreadPriority)
  1.1020 +	{
  1.1021 +	CBusyTestUnit* self = NewLC(aPercentBusy, aThreadPriority);
  1.1022 +	CleanupStack::Pop(self);
  1.1023 +	return self;
  1.1024 +	}
  1.1025 +	
  1.1026 +CBusyTestUnit::~CBusyTestUnit()
  1.1027 +	{
  1.1028 +	Stop();
  1.1029 +	}
  1.1030 +	
  1.1031 +EXPORT_C void CBusyTestUnit::Stop()
  1.1032 +	{
  1.1033 +	if (iTimer)
  1.1034 +		{
  1.1035 +		iTimer->Cancel();
  1.1036 +		delete iTimer;
  1.1037 +		iTimer=NULL;
  1.1038 +		}
  1.1039 +	if (iChildThread.Handle())
  1.1040 +		{
  1.1041 +		// child thread created, so kill
  1.1042 +		iChildThread.Kill(0);
  1.1043 +		iChildThread.Close();
  1.1044 +		}
  1.1045 +	}
  1.1046 +	
  1.1047 +EXPORT_C TInt CBusyTestUnit::Start()
  1.1048 +	{
  1.1049 +	return Start(0);	
  1.1050 +	}
  1.1051 +	
  1.1052 +EXPORT_C TInt CBusyTestUnit::Start(TTimeIntervalMicroSeconds aRunFor)
  1.1053 +	{
  1.1054 +	return Start(0, aRunFor);
  1.1055 +	}
  1.1056 +	
  1.1057 +EXPORT_C TInt CBusyTestUnit::Start(TTimeIntervalMicroSeconds32 aDelayFor, TTimeIntervalMicroSeconds aRunFor)
  1.1058 +	{
  1.1059 +	iRunFor = aRunFor;
  1.1060 +	if (!aDelayFor.Int())
  1.1061 +		{
  1.1062 +		// run immediately
  1.1063 +		return RunThread();
  1.1064 +		}
  1.1065 +	else
  1.1066 +		{
  1.1067 +		iTimer = CPeriodic::NewL(CActive::EPriorityHigh);
  1.1068 +		TCallBack callback(StaticTimerCallback, this);
  1.1069 +		iTimer->Start(aDelayFor, 0, callback);
  1.1070 +		return KErrNone;
  1.1071 +		}
  1.1072 +	}
  1.1073 +	
  1.1074 +TInt CBusyTestUnit::StaticTimerCallback(TAny* aPtr)
  1.1075 +	{
  1.1076 +	CBusyTestUnit* self = static_cast<CBusyTestUnit*>(aPtr);
  1.1077 +	return self->TimerCallback();
  1.1078 +	}
  1.1079 +	
  1.1080 +TInt CBusyTestUnit::TimerCallback()
  1.1081 +	{
  1.1082 +	// called on timer callback, so assume iTimer!
  1.1083 +	ASSERT(iTimer);
  1.1084 +	// first stop and delete timer - don't need again this run
  1.1085 +	iTimer->Cancel();
  1.1086 +	// then kick off the thread
  1.1087 +	TInt error = RunThread(); 
  1.1088 +	// now delete the timer - do now as we've been called back by it!
  1.1089 +	delete iTimer;
  1.1090 +	iTimer = NULL;
  1.1091 +	return error; // any error will stop the test, in theory
  1.1092 +	}
  1.1093 +	
  1.1094 +TInt CBusyTestUnit::RunThread()
  1.1095 +	{
  1.1096 +	TAny* paramPtr = this;
  1.1097 +	TInt error = iChildThread.Create(KNullDesC, StartThread, 
  1.1098 +					KDefaultStackSize, NULL, paramPtr, EOwnerThread);
  1.1099 +	if (!error)
  1.1100 +		{
  1.1101 +		iChildThread.SetPriority(iThreadPriority);
  1.1102 +		iChildThread.Resume();
  1.1103 +		}
  1.1104 +	return error;
  1.1105 +	}
  1.1106 +	
  1.1107 +TInt CBusyTestUnit::StartThread(TAny* aPtr)
  1.1108 +	{
  1.1109 +	CBusyTestUnit* self = static_cast<CBusyTestUnit*>(aPtr);
  1.1110 +	if (self)
  1.1111 +		{
  1.1112 +		self->ThreadFunction();
  1.1113 +		}
  1.1114 +	return KErrNone;
  1.1115 +	}
  1.1116 +	
  1.1117 +void CBusyTestUnit::ThreadFunction()
  1.1118 +	{
  1.1119 +	// this runs in a separate thread and tries to use lots of CPU time up to percentage
  1.1120 +	// nominally we run busy for loops for iPercentBusy/100ms, and then wait for rest of the
  1.1121 +	// 100ms using User::After(). We keep doing this until we reach the target time, if there is
  1.1122 +	// one
  1.1123 +	
  1.1124 +	const TInt KDefaultLoop = 10000;
  1.1125 +	TTime timeAtStart; 
  1.1126 +	timeAtStart.UniversalTime(); // time of start
  1.1127 +	
  1.1128 +	const TInt KLoopInterval =100000; // 100ms - loop time
  1.1129 +	TTimeIntervalMicroSeconds32 busyInterval(KLoopInterval*iPercentBusy/100); // how much of loop time to be busy
  1.1130 +	TTimeIntervalMicroSeconds32 quietInterval(KLoopInterval-busyInterval.Int()); // how much of loop time to be quiet
  1.1131 +	
  1.1132 +	while (ETrue)
  1.1133 +		{
  1.1134 +		// the interval, for loops for the busy bit and then a User::After()
  1.1135 +		TTime startOfInterval;
  1.1136 +		startOfInterval.UniversalTime();
  1.1137 +		while (ETrue)
  1.1138 +			{
  1.1139 +			// the busy bit - iBusyVariable is volatile so should never be optimised out
  1.1140 +			for (TInt i=0; i<KDefaultLoop; i++)
  1.1141 +				{
  1.1142 +				iBusyVariable = i;
  1.1143 +				}
  1.1144 +			TTime now;
  1.1145 +			now.UniversalTime();
  1.1146 +			if (startOfInterval + busyInterval < now)
  1.1147 +				{
  1.1148 +				// we're passed the time
  1.1149 +				break;
  1.1150 +				}
  1.1151 +			}
  1.1152 +		User::After(quietInterval);		
  1.1153 +			
  1.1154 +		if (iRunFor.Int64())
  1.1155 +			{
  1.1156 +			// check to see if we are passed the interval given at Start()
  1.1157 +			TTime now;
  1.1158 +			now.UniversalTime();
  1.1159 +			if (timeAtStart + iRunFor < now)
  1.1160 +				{
  1.1161 +				// we're passed the time
  1.1162 +				break;
  1.1163 +				}
  1.1164 +			}
  1.1165 +		}
  1.1166 +	}
  1.1167 +