graphicstest/graphicstestharness/inc/extendtef.h
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // These macros and templates extend the tef testsuite support.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @test
       
    21  @internalComponent - Internal Symbian test code
       
    22 */
       
    23 
       
    24 #ifndef __EXTENDTEF_H__
       
    25 #define __EXTENDTEF_H__
       
    26 
       
    27 #include <test/tefunit.h>
       
    28 
       
    29 // Adds a test suite that is not called CreateSuiteL so that one class can describe multiple subsets of tests.
       
    30 #define ADD_TEST_SUITE_SUB(subname) \
       
    31 	_LIT( KTest ## subname, #subname); \
       
    32 	lTestSuite->AddL( CreateSuiteSub##subname##L( KTest##subname));
       
    33 
       
    34 // Calls a subfunction to add more members to the current test suite at the current level. Can be the same function as used to create nested tests.
       
    35 #define ADD_TEST_SUITE_SUB_FLAT(subname) \
       
    36 	_LIT( KTest ## subname, #subname); \
       
    37 	CreateSuiteSub##subname##L( KTest##subname,lTestSuite);
       
    38 
       
    39 // Allows the class suite to be added with a name different to the class name, so class names can be computational
       
    40 #define ADD_TEST_SUITE_MODULE_SUB_AS(module,subname,as) \
       
    41 	_LIT( KTest ## as, #as); \
       
    42 	lTestSuite->AddL( module::CreateSuiteSub##subname##L( KTest##as));
       
    43 
       
    44 /** Use in a subsuite to distinguish between ADD_TEST_SUITE_SUB and ADD_TEST_SUITE_SUB_FLAT caller
       
    45  *	ClassName is required - it defines the name of the class containing the referenced methods. 
       
    46  *	aTestSuite is optional - it should be the function parameter aTestSuite or the keyword NULL 
       
    47  **/
       
    48 #define SUB_SUITE_OPT(ClassName,aTestSuite) \
       
    49 	CTestSuite* lTestSuite = aTestSuite? (CTestSuite*)aTestSuite: CTestSuite::NewL(aName); \
       
    50 	typedef ClassName	ThisThis;
       
    51 
       
    52 // Create a dotted name subtest in the script that represents a parameter to the named test
       
    53 // This should be called from a ADD_TEST_SUITE_SUB with the same name as the test method,
       
    54 // so that entries can be inserted of the form CMyTestClass.MyTest.EColor256
       
    55 #define ADD_TEST_STEP_PARAM_1(method,param)	\
       
    56 	_LIT( KTest ## method ## param, #param); \
       
    57 	AddTestCaseParam(lTestSuite, KTest ## method ## param, method, param);
       
    58 
       
    59 // Insert a test that takes a parameter and a range of dotted subtests that set that parameter
       
    60 // This allows tests of the form CMyTestClass.MyLoopTest.8
       
    61 #define ADD_TEST_STEP_PARAM_RANGE(method,minparam,maxparam)	\
       
    62 	_LIT(KTest ## method,#method); \
       
    63 	AddTestCaseParamRangeL(lTestSuite,KTest ## method,&ThisThis::method,minparam,maxparam);
       
    64 
       
    65 // Insert a test that takes a parameter and a range of dotted subtests that set that parameter
       
    66 // This allows tests of the form CMyTestClass.MyLoopTest.8
       
    67 #define ADD_TEST_STEP_PARAM_RANGE_STEP(method,minparam,maxparam,step)		 \
       
    68 	_LIT( KTest ## method , #method); \
       
    69 	AddTestCaseParamRangeL(lTestSuite, KTest ## method, &ThisThis::method, minparam,maxparam,step);
       
    70 
       
    71 // Insert a test that takes a boolean parameter and a range of dotted subtests that set that parameter 
       
    72 // Names for dotted tests include "true", "yes", "on", "1"
       
    73 // This allows tests of the form CMyTestClass.EnableFeature.on
       
    74 #define ADD_TEST_STEP_PARAM_BOOL(method)	\
       
    75 	_LIT(KTest ## method,#method); \
       
    76 	AddTestCaseParamBoolL(lTestSuite,KTest ## method,&ThisThis::method);
       
    77 
       
    78 // Insert a test that that just requires calling a function
       
    79 #define ADD_WSGCE_TEST_STEP(classname,method) \
       
    80 	_LIT(KTest ## method,#method); \
       
    81 	AddTestCase(lTestSuite,KTest ## method,&classname::method);
       
    82 
       
    83 // Insert an asyncronus test that that just requires calling a function
       
    84 #define ADD_WSGCE_ASYNC_TEST_STEP(classname,method) \
       
    85 	_LIT(KTest ## method,#method); \
       
    86 	AddAsyncTestCase(lTestSuite,KTest ## method,&classname::method);
       
    87 
       
    88 // Insert a test that that just requires calling a function
       
    89 #define ADD_THIS_TEST_STEP(method) \
       
    90 	_LIT(KTest ## method,#method); \
       
    91 	AddTestCase(lTestSuite,KTest ## method,&ThisThis::method);
       
    92 
       
    93 // Insert an asyncronus test that that just requires calling a function
       
    94 #define ADD_THIS_ASYNC_TEST_STEP(method) \
       
    95 	_LIT(KTest ## method,#method); \
       
    96 	AddAsyncTestCase(lTestSuite,KTest ## method,&ThisThis::method);
       
    97 
       
    98 /*********************************************************************************
       
    99  * Base class for derived doers that use the Setup/Shutdown
       
   100  *********************************************************************************/
       
   101 template <class Fixture>
       
   102 class CTestCaseDoTemplate : public CTestCase
       
   103 	{
       
   104 private:
       
   105 	
       
   106 public:
       
   107 	CTestCaseDoTemplate(const TDesC &aName);
       
   108 	virtual ~CTestCaseDoTemplate();
       
   109 
       
   110 	virtual void RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger);
       
   111 	virtual void DoRunL(Fixture*aFixture)=0;
       
   112 
       
   113 protected:
       
   114 	};
       
   115 
       
   116 /*********************************************************************************
       
   117  * Derived dorun to run a test with a parameter
       
   118  *********************************************************************************/
       
   119 template <class Fixture,class Param>
       
   120 class CTestCaseParamTemplate : public CTestCaseDoTemplate<Fixture>
       
   121 	{
       
   122 private:
       
   123 	typedef void (Fixture::*TestMethod)(Param aParam);
       
   124 	
       
   125 public:
       
   126 	CTestCaseParamTemplate(const TDesC &aName, TestMethod aTestMethod, Param aParam)
       
   127 	:	CTestCaseDoTemplate<Fixture>(aName),
       
   128 		iTest(aTestMethod),iParam(aParam)
       
   129 	{}
       
   130 	virtual void DoRunL(Fixture*aFixture)
       
   131 		{
       
   132 		(aFixture->*iTest)(iParam);
       
   133 		}
       
   134 
       
   135 protected:
       
   136 	TestMethod	iTest;
       
   137 	Param iParam;
       
   138 	};
       
   139 
       
   140 /***************************************
       
   141  * 
       
   142  * Implementation of template <class Fixture,class Param> class CTestCaseTemplate
       
   143  *
       
   144  ***************************************/
       
   145 template <class Fixture>
       
   146 CTestCaseDoTemplate<Fixture>::CTestCaseDoTemplate(const TDesC &aName)
       
   147 : CTestCase(aName)
       
   148 	{
       
   149 	}
       
   150 template <class Fixture>
       
   151 CTestCaseDoTemplate<Fixture>::~CTestCaseDoTemplate()
       
   152 	{
       
   153 	}
       
   154 template <class Fixture>
       
   155 void CTestCaseDoTemplate<Fixture>::RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger)
       
   156 /**
       
   157  * Creates and destroys the test fixture, but runs the DoRunL in the derived class, which in turn runs the test. 
       
   158  *
       
   159  * @param aConfig - Test configuration
       
   160  * @param aLogger - Test logger
       
   161  */
       
   162 	{
       
   163 	Fixture* iFixture = new (ELeave) Fixture();
       
   164 	CleanupStack::PushL(iFixture);
       
   165 	
       
   166 	// Must set the Logger and Config now
       
   167 	iFixture->SetLoggerL( aLogger );
       
   168 	iFixture->SetConfigL( aConfig );
       
   169 	
       
   170 	// Setup code
       
   171 	iFixture->SetupL();
       
   172 	
       
   173 	// Run the test
       
   174 	TRAPD( err, DoRunL(iFixture) );
       
   175 
       
   176 	// Log the result
       
   177 	CTEFLogger::LogResult( iName, err, aLogger );
       
   178 
       
   179 	// Now the test case has been logged we need to leave
       
   180 	// again if that is what the test did
       
   181 	if( err != KErrTEFUnitPass )
       
   182 		{
       
   183 		User::Leave(err);
       
   184 		}
       
   185 
       
   186 	// Teardown code
       
   187 	iFixture->TearDownL();
       
   188 
       
   189 	CleanupStack::PopAndDestroy();
       
   190 	}
       
   191 /**
       
   192  * Support method to add a test that takes a specific value as its parameter.
       
   193  * Can be used from ADD_TEST_STEP_PARAM_1, or from the methods below
       
   194  **/
       
   195 template <class Param,class Fixture>
       
   196 inline void AddTestCaseParam(CTestSuite* aTestSuite, const TDesC& aName, void (Fixture::*aTestMethod)(Param aParam),Param aParam)
       
   197 	{
       
   198 	CTestCaseParamTemplate<Fixture,Param> *testCaseTemplate = new (ELeave) CTestCaseParamTemplate<Fixture,Param>(aName, aTestMethod,aParam);
       
   199 	CleanupStack::PushL(testCaseTemplate);
       
   200 	aTestSuite->AddL( testCaseTemplate );
       
   201 	CleanupStack::Pop(testCaseTemplate);
       
   202 	}
       
   203 /**
       
   204  * Adds a test with a range of parameter values. Usually launched from ADD_TEST_STEP_PARAM_RANGE
       
   205  * 
       
   206  **/
       
   207 template <class Param,class Fixture>
       
   208 inline void AddTestCaseParamRangeL(CTestSuite* aTestSuite, const TDesC& aName, void (Fixture::*aTestMethod)(Param aParam),TInt aParamMin,TInt aParamMax,TInt aDelta=1)
       
   209 	{
       
   210 	CTestSuite* lTestSuite = CTestSuite::NewL(aName);
       
   211 	CleanupStack::PushL(lTestSuite);
       
   212 	__ASSERT_ALWAYS(aDelta!=0 && aParamMin!=aParamMax,User::Invariant());
       
   213 	__ASSERT_ALWAYS((aDelta>0) == (aParamMax>aParamMin),User::Invariant());
       
   214 	for (TInt count=aParamMin;count!=aParamMax;count+=aDelta)
       
   215 		{
       
   216 		TBuf<10> name2;
       
   217 		name2.AppendNum(count);
       
   218 		CTestCaseParamTemplate<Fixture,Param> *testCaseTemplate = new (ELeave) CTestCaseParamTemplate<Fixture,Param>(name2, aTestMethod,Param(count));
       
   219 		CleanupStack::PushL(testCaseTemplate);
       
   220 		lTestSuite->AddL( testCaseTemplate );
       
   221 		CleanupStack::Pop(testCaseTemplate);
       
   222 		}
       
   223 	aTestSuite->AddL(lTestSuite);
       
   224 	CleanupStack::Pop(lTestSuite);
       
   225 	}
       
   226 
       
   227 /**
       
   228  * Adds a test with a range of parameter values. Usually launched from ADD_TEST_STEP_PARAM_BOOL
       
   229  * 
       
   230  **/
       
   231 template <class Param,class Fixture>
       
   232 inline void AddTestCaseParamBoolL(CTestSuite* aTestSuite, const TDesC& aName, void (Fixture::*aTestMethod)(Param aParam))
       
   233 	{
       
   234 	class CTestSuiteBool: public CTestSuite
       
   235 		{
       
   236 		public:
       
   237 		static CTestSuite* NewL(const TTestName& aName)
       
   238 			{
       
   239 			CTestSuite* lTestSuite = new (ELeave) CTestSuiteBool(aName);
       
   240 			CleanupStack::PushL(lTestSuite);
       
   241 			lTestSuite->ConstructL();
       
   242 			CleanupStack::Pop();
       
   243 			return lTestSuite;
       
   244 			}
       
   245 		protected:
       
   246 		CTestSuiteBool(const TTestName& aName):	CTestSuite(aName)
       
   247 			{	}
       
   248 		virtual void RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger)
       
   249 			{	//run "all the tests"
       
   250 			// Log that the runner is currently in this suite
       
   251 			CTEFLogger::LogTraverse( iName, aLogger );
       
   252 			TInt lError=KErrNone;
       
   253 			for(TInt lIndex = 0; lIndex < Count() && lIndex<2; lIndex++)
       
   254 				{
       
   255 				TRAPD(err,TestL(lIndex)->RunL(aConfig, aLogger));
       
   256 				if(err != KErrTEFUnitPass)
       
   257 					{
       
   258 					lError=err;
       
   259 					}
       
   260 				}
       
   261 			if(lError != KErrTEFUnitPass)
       
   262 				{
       
   263 				User::Leave(lError);
       
   264 				}
       
   265 			}
       
   266 		
       
   267 		};
       
   268 	typedef Fixture	ThisThis;
       
   269 	CTestSuite* lTestSuite = CTestSuiteBool::NewL(aName);
       
   270 	CleanupStack::PushL(lTestSuite);
       
   271 #define yes 1
       
   272 #define true 1
       
   273 #define on 1
       
   274 #define off 0
       
   275 #define no 0
       
   276 #define false 0
       
   277 	
       
   278 #define Yes 1
       
   279 #define True 1
       
   280 #define On 1
       
   281 #define Off 0
       
   282 #define No 0
       
   283 #define False 0
       
   284 	
       
   285 #define YES 1
       
   286 #define ON 1
       
   287 #define OFF 0
       
   288 #define NO 0
       
   289 
       
   290 
       
   291 #define ETrue 1
       
   292 #define EFalse 0
       
   293 	//The first two MUST be a true and a false so running "all" will run them both
       
   294 	ADD_TEST_STEP_PARAM_1(aTestMethod,ETrue);
       
   295 	ADD_TEST_STEP_PARAM_1(aTestMethod,EFalse);
       
   296 
       
   297 	ADD_TEST_STEP_PARAM_1(aTestMethod,1);
       
   298 	ADD_TEST_STEP_PARAM_1(aTestMethod,0);
       
   299 	
       
   300 	ADD_TEST_STEP_PARAM_1(aTestMethod,yes);
       
   301 	ADD_TEST_STEP_PARAM_1(aTestMethod,true);
       
   302 	ADD_TEST_STEP_PARAM_1(aTestMethod,on);
       
   303 	ADD_TEST_STEP_PARAM_1(aTestMethod,no);
       
   304 	ADD_TEST_STEP_PARAM_1(aTestMethod,false);
       
   305 	ADD_TEST_STEP_PARAM_1(aTestMethod,off);
       
   306 
       
   307 	ADD_TEST_STEP_PARAM_1(aTestMethod,Yes);
       
   308 	ADD_TEST_STEP_PARAM_1(aTestMethod,True);
       
   309 	ADD_TEST_STEP_PARAM_1(aTestMethod,On);
       
   310 	ADD_TEST_STEP_PARAM_1(aTestMethod,No);
       
   311 	ADD_TEST_STEP_PARAM_1(aTestMethod,False);
       
   312 	ADD_TEST_STEP_PARAM_1(aTestMethod,Off);
       
   313 
       
   314 	ADD_TEST_STEP_PARAM_1(aTestMethod,YES);
       
   315 	ADD_TEST_STEP_PARAM_1(aTestMethod,TRUE);
       
   316 	ADD_TEST_STEP_PARAM_1(aTestMethod,ON);
       
   317 	ADD_TEST_STEP_PARAM_1(aTestMethod,NO);
       
   318 	ADD_TEST_STEP_PARAM_1(aTestMethod,FALSE);
       
   319 	ADD_TEST_STEP_PARAM_1(aTestMethod,OFF);
       
   320 
       
   321 #undef yes 
       
   322 #undef true
       
   323 #undef on 
       
   324 #undef off
       
   325 #undef no 
       
   326 #undef false
       
   327 
       
   328 #undef Yes
       
   329 #undef True
       
   330 #undef On
       
   331 #undef Off
       
   332 #undef No
       
   333 #undef False
       
   334 	
       
   335 #undef YES
       
   336 #undef ON
       
   337 #undef OFF
       
   338 #undef NO
       
   339 
       
   340 
       
   341 #undef ETrue
       
   342 #undef EFalse
       
   343 
       
   344 	aTestSuite->AddL(lTestSuite);
       
   345 	CleanupStack::Pop(lTestSuite);
       
   346 	}
       
   347 
       
   348 void TefUnitFailLeaveL();
       
   349 	
       
   350 #undef 	__ASSERT_SHARED
       
   351 #define __MAKE_L_STRING(S)	L##S
       
   352 /**
       
   353  * Redefining this macro allows the condition text of the assert to be logged when the fail is reported as well as the file and line number
       
   354  * This allows simple issues to be diagnosed without resorting to the source.
       
   355  **/
       
   356 #define __ASSERT_SHARED(aFunction, aMessage) \
       
   357 	if(!aFunction) \
       
   358 		{ \
       
   359 		struct Log	\
       
   360 			{	/*This wrapper is provided purely to hide the temp string from the debugger.*/ \
       
   361 			static const TLitC<sizeof("ASSERT FAILED: " #aFunction)>&	Str()\
       
   362 				{\
       
   363 				_LIT(aMessage, "ASSERT FAILED: " __MAKE_L_STRING(#aFunction) ); \
       
   364 				return aMessage;\
       
   365 				}\
       
   366 			};\
       
   367 		Logger().LogExtra(((TText8*)__FILE__), __LINE__, ESevrAll, Log::Str() );\
       
   368 		TefUnitFailLeaveL();\
       
   369 		}
       
   370 #define ASSERT_NOT_EQUALS(aExpected, aActual) \
       
   371 	__ASSERT_SHARED(!AssertEquals(aExpected, aActual) , KAssertFailedEquals);
       
   372 
       
   373 template <class Pointer>
       
   374 inline TBool AssertTrue(Pointer* aCondition)
       
   375 	{
       
   376 	return AssertTrue((TBool)aCondition);
       
   377 	}
       
   378 
       
   379 
       
   380 #define LOG_AND_RETURN_IF_GOT_GCE										\
       
   381 		{																\
       
   382 		if (GCEIsSupported())											\
       
   383 			{															\
       
   384 			INFO_PRINTF1(_L("Test skipped: GCE support is loaded"));	\
       
   385 			User::Panic(_L("GCE.Wrong.Mode"),1);						\
       
   386 			return;														\
       
   387 			}															\
       
   388 		}
       
   389 		
       
   390 #define LOG_AND_PANIC_IF_NOT_GCE											\
       
   391 		{																	\
       
   392 		if (!GCEIsSupported())												\
       
   393 			{																\
       
   394 			INFO_PRINTF1(_L("Test skipped: GCE support is not loaded"));	\
       
   395 			User::Panic(_L("GCE.Wrong.Mode"),1);							\
       
   396 			return;															\
       
   397 			}																\
       
   398 		}
       
   399 
       
   400 
       
   401 #define _LIT2(NAME,STRING)											\
       
   402     class NAME														\
       
   403     	{															\
       
   404     public:															\
       
   405     	static const TLitC<sizeof(L##STRING)/2>& S()			\
       
   406     		{														\
       
   407     		_LIT(singleton,STRING);								\
       
   408     		return singleton;										\
       
   409     		}														\
       
   410     	}	;
       
   411 
       
   412 
       
   413 #endif	//__EXTENDTEF_H__