First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // These macros and templates extend the tef testsuite support.
21 @internalComponent - Internal Symbian test code
24 #ifndef __EXTENDTEF_H__
25 #define __EXTENDTEF_H__
27 #include <test/tefunit.h>
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));
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);
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));
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
48 #define SUB_SUITE_OPT(ClassName,aTestSuite) \
49 CTestSuite* lTestSuite = aTestSuite? (CTestSuite*)aTestSuite: CTestSuite::NewL(aName); \
50 typedef ClassName ThisThis;
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);
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);
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);
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);
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);
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);
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);
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);
98 /*********************************************************************************
99 * Base class for derived doers that use the Setup/Shutdown
100 *********************************************************************************/
101 template <class Fixture>
102 class CTestCaseDoTemplate : public CTestCase
107 CTestCaseDoTemplate(const TDesC &aName);
108 virtual ~CTestCaseDoTemplate();
110 virtual void RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger);
111 virtual void DoRunL(Fixture*aFixture)=0;
116 /*********************************************************************************
117 * Derived dorun to run a test with a parameter
118 *********************************************************************************/
119 template <class Fixture,class Param>
120 class CTestCaseParamTemplate : public CTestCaseDoTemplate<Fixture>
123 typedef void (Fixture::*TestMethod)(Param aParam);
126 CTestCaseParamTemplate(const TDesC &aName, TestMethod aTestMethod, Param aParam)
127 : CTestCaseDoTemplate<Fixture>(aName),
128 iTest(aTestMethod),iParam(aParam)
130 virtual void DoRunL(Fixture*aFixture)
132 (aFixture->*iTest)(iParam);
140 /***************************************
142 * Implementation of template <class Fixture,class Param> class CTestCaseTemplate
144 ***************************************/
145 template <class Fixture>
146 CTestCaseDoTemplate<Fixture>::CTestCaseDoTemplate(const TDesC &aName)
150 template <class Fixture>
151 CTestCaseDoTemplate<Fixture>::~CTestCaseDoTemplate()
154 template <class Fixture>
155 void CTestCaseDoTemplate<Fixture>::RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger)
157 * Creates and destroys the test fixture, but runs the DoRunL in the derived class, which in turn runs the test.
159 * @param aConfig - Test configuration
160 * @param aLogger - Test logger
163 Fixture* iFixture = new (ELeave) Fixture();
164 CleanupStack::PushL(iFixture);
166 // Must set the Logger and Config now
167 iFixture->SetLoggerL( aLogger );
168 iFixture->SetConfigL( aConfig );
174 TRAPD( err, DoRunL(iFixture) );
177 CTEFLogger::LogResult( iName, err, aLogger );
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 )
187 iFixture->TearDownL();
189 CleanupStack::PopAndDestroy();
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
195 template <class Param,class Fixture>
196 inline void AddTestCaseParam(CTestSuite* aTestSuite, const TDesC& aName, void (Fixture::*aTestMethod)(Param aParam),Param aParam)
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);
204 * Adds a test with a range of parameter values. Usually launched from ADD_TEST_STEP_PARAM_RANGE
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)
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)
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);
223 aTestSuite->AddL(lTestSuite);
224 CleanupStack::Pop(lTestSuite);
228 * Adds a test with a range of parameter values. Usually launched from ADD_TEST_STEP_PARAM_BOOL
231 template <class Param,class Fixture>
232 inline void AddTestCaseParamBoolL(CTestSuite* aTestSuite, const TDesC& aName, void (Fixture::*aTestMethod)(Param aParam))
234 class CTestSuiteBool: public CTestSuite
237 static CTestSuite* NewL(const TTestName& aName)
239 CTestSuite* lTestSuite = new (ELeave) CTestSuiteBool(aName);
240 CleanupStack::PushL(lTestSuite);
241 lTestSuite->ConstructL();
246 CTestSuiteBool(const TTestName& aName): CTestSuite(aName)
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++)
255 TRAPD(err,TestL(lIndex)->RunL(aConfig, aLogger));
256 if(err != KErrTEFUnitPass)
261 if(lError != KErrTEFUnitPass)
268 typedef Fixture ThisThis;
269 CTestSuite* lTestSuite = CTestSuiteBool::NewL(aName);
270 CleanupStack::PushL(lTestSuite);
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);
297 ADD_TEST_STEP_PARAM_1(aTestMethod,1);
298 ADD_TEST_STEP_PARAM_1(aTestMethod,0);
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);
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);
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);
344 aTestSuite->AddL(lTestSuite);
345 CleanupStack::Pop(lTestSuite);
348 void TefUnitFailLeaveL();
350 #undef __ASSERT_SHARED
351 #define __MAKE_L_STRING(S) L##S
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.
356 #define __ASSERT_SHARED(aFunction, aMessage) \
360 { /*This wrapper is provided purely to hide the temp string from the debugger.*/ \
361 static const TLitC<sizeof("ASSERT FAILED: " #aFunction)>& Str()\
363 _LIT(aMessage, "ASSERT FAILED: " __MAKE_L_STRING(#aFunction) ); \
367 Logger().LogExtra(((TText8*)__FILE__), __LINE__, ESevrAll, Log::Str() );\
368 TefUnitFailLeaveL();\
370 #define ASSERT_NOT_EQUALS(aExpected, aActual) \
371 __ASSERT_SHARED(!AssertEquals(aExpected, aActual) , KAssertFailedEquals);
373 template <class Pointer>
374 inline TBool AssertTrue(Pointer* aCondition)
376 return AssertTrue((TBool)aCondition);
380 #define LOG_AND_RETURN_IF_GOT_GCE \
382 if (GCEIsSupported()) \
384 INFO_PRINTF1(_L("Test skipped: GCE support is loaded")); \
385 User::Panic(_L("GCE.Wrong.Mode"),1); \
390 #define LOG_AND_PANIC_IF_NOT_GCE \
392 if (!GCEIsSupported()) \
394 INFO_PRINTF1(_L("Test skipped: GCE support is not loaded")); \
395 User::Panic(_L("GCE.Wrong.Mode"),1); \
401 #define _LIT2(NAME,STRING) \
405 static const TLitC<sizeof(L##STRING)/2>& S() \
407 _LIT(singleton,STRING); \
413 #endif //__EXTENDTEF_H__