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