epoc32/include/test/tefunit.h
branchSymbian3
changeset 4 837f303aceeb
equal deleted inserted replaced
3:e1b950c65cb4 4:837f303aceeb
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file TEFUnit.h
       
    21 */
       
    22 
       
    23 #ifndef __TEF_UNIT__
       
    24 #define __TEF_UNIT__
       
    25 
       
    26 #include <e32std.h>
       
    27 #include <test/testexecutestepbase.h>
       
    28 #include <test/cteflogger.h>
       
    29 
       
    30 /*********************************************************************************
       
    31  * Assert Macros
       
    32  *********************************************************************************/
       
    33 #define __ASSERT_SHARED(aFunction, aMessage) \
       
    34 	if(!aFunction) \
       
    35 		{ \
       
    36 		Logger().LogExtra(((TText8*)__FILE__), __LINE__, ESevrAll, aMessage );\
       
    37 		User::Leave(KErrTEFUnitFail);\
       
    38 		}
       
    39 
       
    40 #define ASSERT_EQUALS(aExpected, aActual) \
       
    41 	__ASSERT_SHARED(AssertEquals(aExpected, aActual) , KAssertFailedEquals);
       
    42 
       
    43 #define ASSERT_NULL(aPtr) \
       
    44 	__ASSERT_SHARED(AssertNull(aPtr), KAssertFailedNull);
       
    45 
       
    46 #define ASSERT_NOT_NULL(aPtr) \
       
    47 	__ASSERT_SHARED(!AssertNull(aPtr), KAssertFailedNotNull);
       
    48 
       
    49 #define ASSERT_SAME(aExpectedPtr, aActualPtr) \
       
    50 	__ASSERT_SHARED(AssertSame(aExpectedPtr, aActualPtr), KAssertFailedSame);
       
    51 
       
    52 #define ASSERT_NOT_SAME(aExpectedPtr, aActualPtr) \
       
    53 	__ASSERT_SHARED(!AssertSame(aExpectedPtr, aActualPtr), KAssertFailedNotSame);
       
    54 
       
    55 #define ASSERT_TRUE(aCondition) \
       
    56 	__ASSERT_SHARED(AssertTrue(aCondition), KAssertFailedTrue);
       
    57 
       
    58 #define ASSERT_FALSE(aCondition) \
       
    59 	__ASSERT_SHARED(!AssertTrue(aCondition), KAssertFailedFalse);
       
    60 
       
    61 /*********************************************************************************
       
    62  * CTestConfig class
       
    63  *********************************************************************************/
       
    64 
       
    65 class CTestConfig
       
    66 /**
       
    67 @publishedPartner
       
    68 @test
       
    69 */
       
    70 	{
       
    71 public:
       
    72 	CTestConfig();
       
    73 	CTestConfig(const CTestConfig& aTestConfig );
       
    74 	CTestConfig( CTestStep* aTestStep );
       
    75 	~CTestConfig();
       
    76 
       
    77 	TBool GetBool(const TDesC& aSectionName, const TDesC& aKey, TBool& aResult) const;
       
    78 	TBool GetInt(const TDesC& aSectionName, const TDesC& aKey, TInt& aResult) const;
       
    79 	TBool GetHex(const TDesC& aSectionName, const TDesC& aKey, TInt& aResult) const;
       
    80 	TBool GetString(const TDesC& aSectionName, const TDesC& aKey, TPtrC& aResult) const;
       
    81 
       
    82 	// Use the section name passed in via the script
       
    83 	TBool GetBool(const TDesC& aKey, TBool& aResult) const;
       
    84 	TBool GetInt(const TDesC& aKey, TInt& aResult) const;
       
    85 	TBool GetHex(const TDesC& aKey, TInt& aResult) const;
       
    86 	TBool GetString(const TDesC& aKey, TPtrC& aResult) const;
       
    87 
       
    88 	TBool WriteBool(const TDesC& aSectionName, const TDesC& aKey, TBool& aValue) const;
       
    89 	TBool WriteInt(const TDesC& aSectionName, const TDesC& aKey, TInt& aValue) const;
       
    90 	TBool WriteHex(const TDesC& aSectionName, const TDesC& aKey, TInt& aValue) const;
       
    91 	TBool WriteString(const TDesC& aSectionName, const TDesC& aKey, TPtrC& aValue) const;
       
    92 
       
    93 	// Use the section name passed in via the script
       
    94 	TBool WriteBool(const TDesC& aKey, TBool& aValue) const;
       
    95 	TBool WriteInt(const TDesC& aKey, TInt& aValue) const;
       
    96 	TBool WriteHex(const TDesC& aKey, TInt& aValue) const;
       
    97 	TBool WriteString(const TDesC& aKey, TPtrC& aValue) const;
       
    98 	
       
    99 	TBool GetInt(const TDesC& aSectionName, const TDesC& aKey, TInt64& aResult) const;
       
   100 	TBool GetInt(const TDesC& aKey, TInt64& aResult) const;
       
   101 	TBool WriteInt(const TDesC& aSectionName, const TDesC& aKey, TInt64& aValue) const;
       
   102 	TBool WriteInt(const TDesC& aKey, TInt64& aValue) const;
       
   103 		
       
   104 private:
       
   105 	CTestStep*	iTestStep;
       
   106 	};
       
   107 
       
   108 
       
   109 /*********************************************************************************
       
   110  * CTestFixture class
       
   111  *********************************************************************************/
       
   112 	
       
   113 class CTestFixture : public CBase
       
   114 /**
       
   115 @publishedPartner
       
   116 @test
       
   117 */
       
   118 	{
       
   119 public:
       
   120 	virtual	~CTestFixture();
       
   121 
       
   122 	virtual void SetupL();
       
   123 	virtual void TearDownL();
       
   124 	
       
   125 	void SetConfigL(CTestConfig& aConfig);
       
   126 	void SetLoggerL( CTestExecuteLogger& aLogger );
       
   127 	inline CTestExecuteLogger& Logger() { return iLogger; }
       
   128 
       
   129 protected:
       
   130 	CTestConfig			iConfig;
       
   131 	CTestExecuteLogger	iLogger;
       
   132 	};
       
   133 
       
   134 /*********************************************************************************
       
   135  * CActiveTestFixture class
       
   136  *********************************************************************************/
       
   137  
       
   138 class CActiveTestFixture : public CTestFixture
       
   139 /**
       
   140 @publishedPartner
       
   141 @test
       
   142 */
       
   143 	{
       
   144 public:
       
   145 	virtual	~CActiveTestFixture();
       
   146 	virtual void SetupL();
       
   147 	virtual void TearDownL();
       
   148  	// Construct the active environment
       
   149  	virtual void ConstructEnvL();
       
   150  	// Destroy the active environment
       
   151  	virtual void DestroyEnvL();
       
   152  	// Start the active environnment
       
   153    	virtual void StartEnvL();
       
   154  	// Stop the active environnment
       
   155    	virtual void StopEnvL();
       
   156 	CActiveScheduler* iSched;
       
   157 	};
       
   158 
       
   159 class CTestCase;
       
   160 class CTestSuite;
       
   161 
       
   162 /*********************************************************************************
       
   163  * MVisitor class
       
   164  *********************************************************************************/
       
   165 class MVisitor
       
   166 	{
       
   167 public: 
       
   168 	virtual void VisitL(CTestCase* aTestCase) = 0;
       
   169 	virtual void VisitL(CTestSuite* aTestSuite) = 0;
       
   170 	};
       
   171 
       
   172 
       
   173 const TInt KMaxTestName = 256;
       
   174 typedef TBuf<KMaxTestName> TTestName;
       
   175 
       
   176 const TInt KMaxPathSize = KMaxTestName*10;
       
   177 typedef TBuf<KMaxPathSize> TTestPath;
       
   178 
       
   179 /*********************************************************************************
       
   180  * CTestBase class
       
   181  *********************************************************************************/
       
   182 class CTestBase : public CBase
       
   183 	{
       
   184 public:
       
   185 	CTestBase(const TTestName &aName); 
       
   186 	virtual ~CTestBase();
       
   187 		
       
   188 	const TTestName& Name() const; 
       
   189 	virtual void RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger) = 0;
       
   190 	virtual void AcceptL(MVisitor &aVisitor) = 0;
       
   191 	
       
   192 protected: 
       
   193 	TTestName iName; 
       
   194 	};
       
   195 
       
   196 
       
   197 /*********************************************************************************
       
   198  * CTestCase class
       
   199  *********************************************************************************/
       
   200 class CTestCase : public CTestBase
       
   201 	{
       
   202 public:
       
   203 	CTestCase(const TTestName& aName);
       
   204 	virtual ~CTestCase(); 
       
   205 
       
   206 	virtual void RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger) = 0;
       
   207 	virtual void AcceptL(MVisitor &aVisitor);
       
   208 	};
       
   209 
       
   210 /*********************************************************************************
       
   211  * Test Suite Macros
       
   212  *********************************************************************************/
       
   213 #define START_SUITE \
       
   214 	CTestSuite* lTestSuite = CTestSuite::NewL();
       
   215 
       
   216 #define SUB_SUITE \
       
   217 	CTestSuite* lTestSuite = CTestSuite::NewL(aName);
       
   218 
       
   219 #define END_SUITE \
       
   220 	return lTestSuite;
       
   221 //PDEF115446 start
       
   222 // By adding a condition marco to reconginize different compiler
       
   223 #ifndef __GCCXML__
       
   224 	// this means using gcc or armcc or mwccsym2 
       
   225 	#define ADD_TEST_STEP(name) \
       
   226 		_LIT( KTest ## name, #name); \
       
   227 		AddTestCase(lTestSuite, KTest ## name, &name);
       
   228 	
       
   229 	#define ADD_ASYNC_TEST_STEP(name) \
       
   230 		_LIT( KTest ## name, #name); \
       
   231 		AddAsyncTestCase(lTestSuite, KTest ## name, &name);
       
   232 #endif
       
   233 #ifdef __GCCXML__
       
   234 	// this means using gccxml
       
   235 	// GCCXML compiler NOT use ADD_TEST_STEP 
       
   236 	// Please use 2 parameter Macro ADD_(ASYNC)_TEST_STEP_CLASS	 
       
   237 #endif
       
   238 	
       
   239 //PDEF115446
       
   240 //new version marco
       
   241 //For both GCCXML and Non-GCCXML compiler
       
   242 #define ADD_TEST_STEP_CLASS(classname, callback) \
       
   243     _LIT( KTest ## callback, #callback);\
       
   244     AddTestCase(lTestSuite, KTest ## callback, & classname::callback);
       
   245 
       
   246 #define ADD_ASYNC_TEST_STEP_CLASS(classname, callback) \
       
   247     _LIT( KTest ## callback, #callback);\
       
   248     AddAsyncTestCase (lTestSuite, KTest ## callback, & classname::callback);
       
   249 //PDEF115446 end
       
   250 	
       
   251 #define ADD_TEST_SUITE(name) \
       
   252 	_LIT( KTest ## name, #name); \
       
   253 	lTestSuite->AddL( name::CreateSuiteL( KTest##name));
       
   254 
       
   255 
       
   256 /*********************************************************************************
       
   257  * CTestSuite class
       
   258  *********************************************************************************/
       
   259 
       
   260 class CTestSuite : public CTestBase
       
   261 /**
       
   262 @publishedPartner
       
   263 @test
       
   264 */
       
   265 	{
       
   266 public: 
       
   267 	CTestSuite(const TTestName& aName);
       
   268 	CTestSuite(CTestSuite &rhs);
       
   269 	void ConstructL();
       
   270 	static CTestSuite* NewL();
       
   271 	static CTestSuite* NewL(const TTestName& aName);
       
   272 	virtual ~CTestSuite();
       
   273 		
       
   274 	// Add a test to the suite
       
   275 	virtual void AddL(CTestBase* aTest);
       
   276 
       
   277 	// Run all the tests in the suite
       
   278 	virtual void RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger);
       
   279 
       
   280 	virtual void AcceptL(MVisitor &aVisitor);
       
   281 
       
   282 	TInt Count();
       
   283 	TTestName NameL( TInt anIndex );
       
   284 	CTestBase*	TestL( TInt anIndex );
       
   285 
       
   286 private:
       
   287 	RPointerArray<CTestBase>	iTests;
       
   288 	TInt iError;
       
   289 	};
       
   290 
       
   291 
       
   292 /*********************************************************************************
       
   293  * CTestCaseTemplate class
       
   294  *********************************************************************************/
       
   295 template <class Fixture>
       
   296 class CTestCaseTemplate : public CTestCase
       
   297 	{
       
   298 private:
       
   299 	typedef void (Fixture::*TestMethod)();
       
   300 	
       
   301 public:
       
   302 	CTestCaseTemplate(const TDesC &aName, TestMethod aTestMethod );
       
   303 	virtual ~CTestCaseTemplate();
       
   304 
       
   305 	virtual void RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger);
       
   306 
       
   307 protected:
       
   308 	TestMethod	iTest;
       
   309 	};
       
   310 
       
   311 /*********************************************************************************
       
   312  * CActiveTester class
       
   313  *********************************************************************************/
       
   314 const TInt KNumberOfDelays = 3;
       
   315 
       
   316 template <class Fixture>
       
   317 class CActiveTester : public CTimer
       
   318 	{
       
   319 private:
       
   320 	typedef void (Fixture::*TestMethod)();
       
   321 
       
   322 public:
       
   323 	CActiveTester(Fixture* aFixture, TestMethod aTestMethod, TTimeIntervalMicroSeconds32 /*aDelays[KNumberOfDelays]*/ );
       
   324 	virtual ~CActiveTester();
       
   325 
       
   326 	virtual void RunL();
       
   327 	virtual TInt RunError(TInt /*aError*/);
       
   328 	TInt Error();
       
   329 	virtual void ConstructEnvL();
       
   330 	void DestroyEnvL();
       
   331 	void StartEnvL();
       
   332 	void SetLoggerL(CTestExecuteLogger& aLogger);
       
   333 	void SetConfigL(CTestConfig& aConfig);
       
   334 
       
   335 protected:
       
   336 	enum TTestState
       
   337 		{
       
   338 		EConstruct = 0,
       
   339 		ERunTest,
       
   340 		EDestroy,
       
   341 		EExit
       
   342 		};
       
   343 
       
   344 	TTestState					iState;
       
   345 	Fixture*					iFixture;
       
   346 	TestMethod					iTest;
       
   347 	TTimeIntervalMicroSeconds32	iDelays[KNumberOfDelays];
       
   348 	TInt						iError;
       
   349 	};
       
   350 
       
   351 
       
   352 /*********************************************************************************
       
   353  * CAsyncTestCase class
       
   354  *********************************************************************************/
       
   355 template <class Fixture>
       
   356 class CAsyncTestCase : public CTestCase
       
   357 	{
       
   358 private:
       
   359 	typedef void (Fixture::*TestMethod)();
       
   360 
       
   361 public:
       
   362 	CAsyncTestCase(const TDesC &aName, TestMethod aTestMethod );
       
   363 	virtual ~CAsyncTestCase();
       
   364 
       
   365 	virtual void RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger);
       
   366 
       
   367 protected:
       
   368 	TestMethod iTest;
       
   369 	};
       
   370 
       
   371 #include <test/tefunit.inl>
       
   372 
       
   373 #endif // __TEF_UNIT__