epoc32/include/test/tefunit.inl
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.inl
       
    21 */
       
    22 
       
    23 
       
    24 #if !(defined TEF_LITE)
       
    25 #include <basched.h>
       
    26 #endif
       
    27 
       
    28 template <class T>
       
    29 inline TBool AssertEquals(const T& aExpected, const T& aActual)
       
    30 /**
       
    31  * AssertEquals
       
    32  *
       
    33  * @publishedPartner
       
    34  * @test
       
    35  *
       
    36  * @param aExpected - Expected result
       
    37  * @param aActual - Actual result
       
    38  * @return - True if equal
       
    39  */
       
    40 	{
       
    41 	if( aExpected==aActual )
       
    42 		{
       
    43 		return ETrue;
       
    44 		}
       
    45 	return EFalse;
       
    46 	}
       
    47 
       
    48 template <class T>
       
    49 inline TBool AssertNull(const T* aPtr)
       
    50 /**
       
    51  * AssertNull
       
    52  *
       
    53  * @publishedPartner
       
    54  * @test
       
    55  *
       
    56  * @param aPtr - Pointer
       
    57  * @return - True if NULL
       
    58  */
       
    59 	{
       
    60 	if( aPtr==NULL )
       
    61 		{
       
    62 		return ETrue;
       
    63 		}
       
    64 	return EFalse;
       
    65 	}
       
    66 
       
    67 template <class T>
       
    68 inline TBool AssertSame(const T* aExpectedPtr, const T* aActualPtr)
       
    69 /**
       
    70  * AssertSame
       
    71  *
       
    72  * @publishedPartner
       
    73  * @test
       
    74  *
       
    75  * @param aExpectedPtr - Expected pointer
       
    76  * @param aActualPtr - Actual pointer
       
    77  * @return - True if equal
       
    78  */
       
    79 	{
       
    80 	if( aExpectedPtr==aActualPtr )
       
    81 		{
       
    82 		return ETrue;
       
    83 		}
       
    84 	return EFalse;
       
    85 	}
       
    86 
       
    87 inline TBool AssertTrue(const TBool& aCondition)
       
    88 /**
       
    89  * AssertTrue
       
    90  *
       
    91  * @publishedPartner
       
    92  * @test
       
    93  *
       
    94  * @param aCondition - Condition
       
    95  * @return - True if aCondition is true
       
    96  */
       
    97 	{
       
    98 	if( !aCondition )
       
    99 		{
       
   100 		return EFalse;
       
   101 		}
       
   102 	return ETrue;
       
   103 	}
       
   104 	
       
   105 	
       
   106 template<class Fixture>
       
   107 inline void AddTestCase(CTestSuite* aTestSuite, const TDesC& aName, void (Fixture::*aTestMethod)())
       
   108 /**
       
   109  * AddTestCase
       
   110  *
       
   111  * @param aTestSuite - CTestSuite
       
   112  * @param aName - Test name
       
   113  * @param aTestMethod - Test method within the fixture
       
   114  */
       
   115 	{
       
   116 	CTestCaseTemplate<Fixture> *testCaseTemplate = new (ELeave) CTestCaseTemplate<Fixture>(aName, aTestMethod);
       
   117 	CleanupStack::PushL(testCaseTemplate);
       
   118 	aTestSuite->AddL( testCaseTemplate );
       
   119 	CleanupStack::Pop(testCaseTemplate);
       
   120 	}
       
   121 
       
   122 template<class Fixture>
       
   123 inline void AddAsyncTestCase(CTestSuite* aTestSuite, const TDesC& aName, void (Fixture::*aTestMethod)())
       
   124 /**
       
   125  * AddAsyncTestCase
       
   126  *
       
   127  * @param aTestSuite - CTestSuite
       
   128  * @param aName - Test name
       
   129  * @param aTestMethod - Test method within the active fixture
       
   130  */
       
   131 	{
       
   132 	CAsyncTestCase<Fixture> *asyncTestCase = new (ELeave) CAsyncTestCase<Fixture>(aName, aTestMethod);
       
   133 	CleanupStack::PushL(asyncTestCase);
       
   134 	aTestSuite->AddL( asyncTestCase );
       
   135 	CleanupStack::Pop(asyncTestCase);
       
   136 	}
       
   137 
       
   138 template <class Fixture>
       
   139 inline CTestCaseTemplate<Fixture>::CTestCaseTemplate(const TDesC &aName, TestMethod aTestMethod )
       
   140 /**
       
   141  * Constructor
       
   142  *
       
   143  * @param aName - Test name
       
   144  * @param aTestMethod - Test method
       
   145  */
       
   146 	: CTestCase(aName), iTest(aTestMethod)
       
   147 	{
       
   148 	}
       
   149 
       
   150 template <class Fixture>
       
   151 inline CTestCaseTemplate<Fixture>::~CTestCaseTemplate()
       
   152 /**
       
   153  * Destructor
       
   154  */
       
   155 	{
       
   156 	}
       
   157 
       
   158 template <class Fixture>
       
   159 inline void CTestCaseTemplate<Fixture>::RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger)
       
   160 /**
       
   161  * RunL
       
   162  *
       
   163  * @param aConfig - Test configuration
       
   164  * @param aLogger - Test logger
       
   165  */
       
   166 	{
       
   167 	Fixture* iFixture = new (ELeave) Fixture();
       
   168 	CleanupStack::PushL(iFixture);
       
   169 	
       
   170 	// Must set the Logger and Config now
       
   171 	iFixture->SetLoggerL( aLogger );
       
   172 	iFixture->SetConfigL( aConfig );
       
   173 	
       
   174 	// Setup code
       
   175 	iFixture->SetupL();
       
   176 	
       
   177 	// Run the test
       
   178 	TRAPD( err, (iFixture->*iTest)() );
       
   179 
       
   180 	// Log the result
       
   181 	CTEFLogger::LogResult( iName, err, aLogger );
       
   182 
       
   183 	// Now the test case has been logged we need to leave
       
   184 	// again if that is what the test did
       
   185 	if( err != KErrTEFUnitPass )
       
   186 		{
       
   187 		User::Leave(err);
       
   188 		}
       
   189 
       
   190 	// Teardown code
       
   191 	iFixture->TearDownL();
       
   192 
       
   193 	CleanupStack::PopAndDestroy();
       
   194 	}
       
   195 
       
   196 template <class Fixture>
       
   197 inline CActiveTester<Fixture>::CActiveTester(Fixture* aFixture, TestMethod aTestMethod, TTimeIntervalMicroSeconds32 /*aDelays[KNumberOfDelays]*/ )
       
   198 /**
       
   199  * Constructor
       
   200  *
       
   201  * @param aFixture - Test fixture
       
   202  * @param aTestMethod - Test method within the fixture
       
   203  */
       
   204 	:	CTimer(EPriorityStandard), iState(EConstruct), iFixture(aFixture),
       
   205 		iTest(aTestMethod), iError(KErrTEFUnitPass)
       
   206 	{
       
   207 	for( TInt i=0 ; i<KNumberOfDelays ; i++ )
       
   208 		{
       
   209 		iDelays[i] = 1;
       
   210 		}
       
   211 	}
       
   212 
       
   213 template <class Fixture>
       
   214 inline CActiveTester<Fixture>::~CActiveTester()
       
   215 /**
       
   216  * Destructor
       
   217  */
       
   218 	{
       
   219 	}
       
   220 
       
   221 template <class Fixture>
       
   222 inline void CActiveTester<Fixture>::RunL()
       
   223 /**
       
   224  * RunL
       
   225  */
       
   226 	{
       
   227 	switch( iState )
       
   228 		{
       
   229 		case EConstruct:
       
   230 			// Run generic startup code - delegate to the fixture
       
   231 			iFixture->SetupL();
       
   232 			iState = ERunTest;
       
   233 			After( iDelays[0] );
       
   234 			break;
       
   235 		case ERunTest:
       
   236 			// Run the test - delegate to the fixture
       
   237 			(iFixture->*iTest)();
       
   238 			iState = EDestroy;
       
   239 			After( iDelays[1] );
       
   240 			break;
       
   241 		case EDestroy:
       
   242 			// Run generic teardown code - delegate to the fixture
       
   243 			iFixture->TearDownL();
       
   244 			iState = EExit;
       
   245 			After( iDelays[2] );
       
   246 			break;
       
   247 		case EExit:
       
   248 			// Stop the Active Scheduler, test complete - delegate to the fixture
       
   249 			iFixture->StopEnvL();
       
   250 			return;
       
   251 		default:
       
   252 			break;
       
   253 		}
       
   254 	}
       
   255 
       
   256 template <class Fixture>
       
   257 inline TInt CActiveTester<Fixture>::RunError(TInt aError)
       
   258 /**
       
   259  * RunError
       
   260  *
       
   261  * @return - Error
       
   262  */
       
   263 	{
       
   264 	if (aError != KErrNone)
       
   265 		{
       
   266 		#if !(defined TEF_LITE)	
       
   267 		if(aError != KLeaveExit)
       
   268 			{
       
   269 		#endif
       
   270 			iError = KErrTEFUnitFail;
       
   271 			// Run generic teardown code - delegate to the fixture
       
   272 			iFixture->TearDownL();
       
   273 			// Stop the ActiveScheduler - delegate to the fixture
       
   274 			iFixture->StopEnvL();
       
   275 			return KErrNone;
       
   276 		#if !(defined TEF_LITE)
       
   277 			}
       
   278 		else
       
   279 			{
       
   280 			return aError;
       
   281 			}
       
   282 		#endif
       
   283 		}
       
   284 	else
       
   285 		{
       
   286 		return aError;
       
   287 		}
       
   288 	}
       
   289 
       
   290 template <class Fixture>
       
   291 inline TInt CActiveTester<Fixture>::Error()
       
   292 /**
       
   293  * Error
       
   294  *
       
   295  * @return - Error
       
   296  */
       
   297 	{
       
   298 	return iError;
       
   299 	}
       
   300 
       
   301 template <class Fixture>
       
   302 inline void CActiveTester<Fixture>::ConstructEnvL()
       
   303 /**
       
   304  * ConstructEnvL
       
   305  */
       
   306 	{
       
   307 	CTimer::ConstructL();
       
   308 	// Delegate to the fixture
       
   309 	iFixture->ConstructEnvL();
       
   310 	}
       
   311 
       
   312 template <class Fixture>
       
   313 inline void CActiveTester<Fixture>::DestroyEnvL()
       
   314 /**
       
   315  * DestroyEnvL
       
   316  */
       
   317 	{
       
   318 	// Delgate to the fixture
       
   319 	iFixture->DestroyEnvL();
       
   320 	}
       
   321 
       
   322 template <class Fixture>
       
   323 inline void CActiveTester<Fixture>::StartEnvL()
       
   324 /**
       
   325  * StartEnvL
       
   326  */
       
   327 	{
       
   328 	After( iDelays[0] );
       
   329 	// Delegate to the fixture
       
   330 	iFixture->StartEnvL();
       
   331 	}
       
   332 
       
   333 template <class Fixture>
       
   334 inline void CActiveTester<Fixture>::SetLoggerL(CTestExecuteLogger& aLogger)
       
   335 /**
       
   336  * SetLoggerL
       
   337  *
       
   338  * @param aLogger - Test logger
       
   339  */
       
   340 	{
       
   341 	// Delegate to the fixture
       
   342 	iFixture->SetLoggerL(aLogger);
       
   343 	}
       
   344 
       
   345 template <class Fixture>
       
   346 inline void CActiveTester<Fixture>::SetConfigL(CTestConfig& aConfig)
       
   347 /**
       
   348  * SetConfigL
       
   349  *
       
   350  * @param aConfig - test configuration
       
   351  */
       
   352 	{
       
   353 	// Delegate to the fixture
       
   354 	iFixture->SetConfigL(aConfig);
       
   355 	}
       
   356 
       
   357 
       
   358 template <class Fixture>
       
   359 inline CAsyncTestCase<Fixture>::CAsyncTestCase(const TDesC &aName, TestMethod aTestMethod )
       
   360 /**
       
   361  * Constructor
       
   362  *
       
   363  * @param aName - Test name
       
   364  * @param aTestMethod - Test method within the fixture
       
   365  */
       
   366 	: CTestCase(aName), iTest(aTestMethod)
       
   367 	{
       
   368 	}
       
   369 
       
   370 template <class Fixture>
       
   371 inline CAsyncTestCase<Fixture>::~CAsyncTestCase()
       
   372 /**
       
   373  * Destructor
       
   374  */
       
   375 	{
       
   376 	}
       
   377 
       
   378 template <class Fixture>
       
   379 inline void CAsyncTestCase<Fixture>::RunL(CTestConfig& aConfig, CTestExecuteLogger& aLogger)
       
   380 /**
       
   381  * RunL
       
   382  *
       
   383  * @param aConfig - Test configuration
       
   384  * @param aLogger - Test logger
       
   385  */
       
   386 	{
       
   387 	// Create the active Object
       
   388 	Fixture *fixture = new (ELeave) Fixture();
       
   389 	CleanupStack::PushL(fixture);
       
   390 	CActiveTester<Fixture>* tester = new (ELeave) CActiveTester<Fixture>(fixture, iTest, 0);
       
   391 	CleanupStack::PushL(tester);
       
   392 
       
   393 	// Set the Logger and Config now
       
   394 	tester->SetLoggerL( aLogger );
       
   395 	tester->SetConfigL( aConfig );
       
   396 
       
   397 	// Create the Active Scheduler Environment
       
   398 	tester->ConstructEnvL();
       
   399 
       
   400 	// Add the ActiveObject which has the test fixture
       
   401 	CActiveScheduler::Add( tester );
       
   402 
       
   403 	// Start the ActiveSheduler
       
   404 	tester->StartEnvL();
       
   405 
       
   406 	// When complete Teardown
       
   407 	tester->DestroyEnvL();
       
   408 
       
   409 	// Log the result
       
   410 	CTEFLogger::LogResult( iName, tester->Error(), aLogger );
       
   411 
       
   412 	// If an error was returned from the ActiveTester Leave here
       
   413 	if( tester->Error() != KErrTEFUnitPass )
       
   414 		{
       
   415 		User::Leave( tester->Error() );
       
   416 		}
       
   417 
       
   418 	CleanupStack::PopAndDestroy(2, fixture);
       
   419 	}