symbianunittestfw/symbianunittestfw_pub/symbian_unit_test_api/inc/symbianunittest.h
branchRCL_3
changeset 3 9397a16b6eb8
parent 1 6edeef394eb7
equal deleted inserted replaced
1:6edeef394eb7 3:9397a16b6eb8
     1 /*
       
     2 * Copyright (c) 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 #ifndef SYMBIANUNITTEST_H
       
    18 #define SYMBIANUNITTEST_H
       
    19 
       
    20 // INCLUDES
       
    21 #include <symbianunittestinterface.h>
       
    22 #include <symbianunittestmacros.h>
       
    23 #include <e32base.h>
       
    24 
       
    25 // FORWARD DECLARATIONS
       
    26 class CSymbianUnitTestResult;
       
    27 
       
    28 // CLASS DECLARATION
       
    29 /**
       
    30 * The base class for unit test classes.
       
    31 * The classes inheriting from this class should call 
       
    32 * CSymbianUnitTest::ConstructL from their ConstructL.
       
    33 * Although the functions implemented in this class 
       
    34 * for unit test assertions and adding test cases 
       
    35 * can be called directly from the subclasses,
       
    36 * the macros defined in symbianunittestmacros.h should be used instead.
       
    37 *
       
    38 * @lib symbianunittestfw.lib
       
    39 */
       
    40 class CSymbianUnitTest : public CBase, public MSymbianUnitTestInterface
       
    41     {
       
    42     public: // Typedefs
       
    43     
       
    44         typedef void ( CSymbianUnitTest::*FunctionPtr )();      
       
    45     
       
    46     public: // Destructor
       
    47 
       
    48         /**
       
    49         * Destructor
       
    50         */        
       
    51         IMPORT_C ~CSymbianUnitTest();
       
    52     
       
    53     public: // From MSymbianUnitTestInterface
       
    54 
       
    55         /**
       
    56         * Function called by the framework to execute 
       
    57         * the unit tests in the subclass.
       
    58         * There is no need to call this function directly 
       
    59         * from the unit test classes.
       
    60         * 
       
    61         * @param aObserver an observer for the test progress 
       
    62         * @param aResult the test result
       
    63         * @param aFailureSimulation the type of failure simulation to be used
       
    64 	* @param aTestCaseNames the test cases to run
       
    65 	* @param aTimeout the time out value for test execution
       
    66         */        
       
    67         IMPORT_C void ExecuteL( 
       
    68             MSymbianUnitTestObserver& aObserver,
       
    69             CSymbianUnitTestResult& aResult,
       
    70             MSymbianUnitTestInterface::TFailureSimulation aFailureSimulation,
       
    71 	    const CDesCArray& aTestCaseNamesi,
       
    72 	    TInt aTimeout );
       
    73 
       
    74         /**
       
    75         * Returns the number of unit test cases contained in this object. 
       
    76         * @return The number of unit test cases in this object
       
    77         */        
       
    78         IMPORT_C TInt TestCaseCount();
       
    79 
       
    80         /**
       
    81         * @return The name of this unit test class
       
    82         */        
       
    83         IMPORT_C const TDesC& Name() const;
       
    84 
       
    85         /**
       
    86          * @return the array of test case names
       
    87          */
       
    88         IMPORT_C void TestCaseNamesL(CDesCArray& aTestCaseNames);
       
    89         
       
    90     protected: // Constructors
       
    91 
       
    92         /**
       
    93         * Constructor
       
    94         */        
       
    95         IMPORT_C CSymbianUnitTest();
       
    96         
       
    97         /**
       
    98         * This function should be called 
       
    99         * from the ConstructL of the subclass
       
   100         * 
       
   101         * @param aName the name of the unit test class
       
   102         */        
       
   103         IMPORT_C void ConstructL( const TDesC8& aName );
       
   104     
       
   105     public: // New functions    
       
   106 
       
   107         /**
       
   108         * This function should be overidden in the actual unit test classes.
       
   109         * It is the default setup function that gets executed 
       
   110         * by the framework before each unit test case.
       
   111         */        
       
   112         IMPORT_C virtual void SetupL();
       
   113         
       
   114         /**
       
   115         * This function should be overidden in the actual unit test classes.
       
   116         * It is the default teardown function that gets executed 
       
   117         * by the framework after each unit test case.
       
   118         */        
       
   119         IMPORT_C virtual void Teardown();
       
   120 
       
   121     protected:    // New functions    
       
   122         /**
       
   123         * Checks whteher the test case is being run using memory 
       
   124         * allocation failure simulation.
       
   125         * @return ETrue if the test case is being run using memory 
       
   126         *         allocation failure simulation, EFalse if not
       
   127         */        
       
   128         IMPORT_C TBool IsMemoryAllocationFailureSimulationUsed() const;
       
   129         
       
   130         /**
       
   131         * Adds a new unit test case to this unit test.
       
   132         * Use macro ADD_SYMBIAN_UT in symbianunittestmacros.h 
       
   133         * instead to avoid the need to pass 
       
   134         * the name of the unit test case as a parameter.
       
   135         * 
       
   136         * @param aName the name of the unit test case
       
   137         * @param aSetupFunction a function pointer to the setup function 
       
   138         *        that will be executed before the actual unit test case
       
   139         * @param aTestFunction a function pointer to the unit test case
       
   140         * @param aTeardownFunction a function pointer to the teardown function
       
   141         *        that will be executed after the actual unit test case
       
   142         */        
       
   143         IMPORT_C void AddTestCaseL( 
       
   144             const TDesC& aName,
       
   145             CSymbianUnitTest::FunctionPtr aSetupFunction,
       
   146             CSymbianUnitTest::FunctionPtr aTestFunction,
       
   147             CSymbianUnitTest::FunctionPtr aTeardownFunction );
       
   148 
       
   149         /**
       
   150         * Asserts that two TInt values are equal.
       
   151         * Leaves with a Symbian unit test framework specific error code
       
   152         * if the values are not equal.
       
   153         * In case of a failed assertion, the framework records 
       
   154         * the failure reason, line number and file name to the test results.
       
   155         * Use macro SYMBIAN_UT_ASSERT_EQUALS in symbianunittestmacros.h 
       
   156         * instead to avoid the need to pass the other parameters 
       
   157         * than the actual asserted values.  
       
   158         * 
       
   159         * @param aExpectedValue the expected value
       
   160         * @param aActualValue the actual value
       
   161         * @param aLineNumber the line number of the assertion
       
   162         * @param aFileName the name of the file where the assertion is located
       
   163         */        
       
   164         IMPORT_C void AssertEqualsL( 
       
   165             TInt aExpectedValue,
       
   166             TInt aActualValue,
       
   167             TInt aLineNumber,
       
   168             const TDesC8& aFileName );        
       
   169 
       
   170         /**
       
   171         * Asserts that two TDesC8 values are equal.
       
   172         * Leaves with a Symbian unit test framework specific error code
       
   173         * if the values are not equal.
       
   174         * In case of a failed assertion, the framework records 
       
   175         * the failure reason, line number and file name to the test results.
       
   176         * Use macro SYMBIAN_UT_ASSERT_EQUALS in symbianunittestmacros.h 
       
   177         * instead to avoid the need to pass the other parameters 
       
   178         * than the actual asserted values.  
       
   179         * 
       
   180         * @param aExpectedValue the expected value
       
   181         * @param aActualValue the actual value
       
   182         * @param aLineNumber the line number of the assertion
       
   183         * @param aFileName the name of the file where the assertion is located
       
   184         */ 
       
   185         IMPORT_C void AssertEqualsL(
       
   186             const TDesC8& aExpectedValue,
       
   187             const TDesC8& aActualValue, 
       
   188             TInt aLineNumber, 
       
   189             const TDesC8& aFileName );
       
   190 
       
   191         /**
       
   192         * Asserts that two TDesC16 values are equal.
       
   193         * Leaves with a Symbian unit test framework specific error code
       
   194         * if the values are not equal.
       
   195         * In case of a failed assertion, the framework records 
       
   196         * the failure reason, line number and file name to the test results.
       
   197         * Use macro SYMBIAN_UT_ASSERT_EQUALS in symbianunittestmacros.h 
       
   198         * instead to avoid the need to pass the other parameters 
       
   199         * than the actual asserted values.  
       
   200         * 
       
   201         * @param aExpectedValue the expected value
       
   202         * @param aActualValue the actual value
       
   203         * @param aLineNumber the line number of the assertion
       
   204         * @param aFileName the name of the file where the assertion is located
       
   205         */        
       
   206         IMPORT_C void AssertEqualsL(
       
   207             const TDesC16& aExpectedValue,
       
   208             const TDesC16& aActualValue,
       
   209             TInt aLineNumber,
       
   210             const TDesC8& aFileName );       
       
   211 
       
   212         /**
       
   213         * Asserts that a statement leaves an expected value.
       
   214         * Leaves with a Symbian unit test framework specific error code
       
   215         * if the leave code is not the expected one.
       
   216         * In case of a failed assertion, the framework records 
       
   217         * the failure reason, line number and file name to the test results.
       
   218         * Use macro SYMBIAN_UT_ASSERT_LEAVE in symbianunittestmacros.h 
       
   219         * instead to avoid the need to pass the other parameters 
       
   220         * than the actual statement that is checked for the leave.  
       
   221         * 
       
   222         * @param aStatement textual presentation of the statement
       
   223         * @param aActualLeaveCode the actual leave code from the statement
       
   224         * @param aExpectedLeaveCode the expected leave code
       
   225         * @param aLineNumber the line number of the assertion
       
   226         * @param aFileName the name of the file where the assertion is located
       
   227         */        
       
   228         IMPORT_C void AssertLeaveL(
       
   229             const TDesC8& aStatement,
       
   230             TInt aActualLeaveCode,
       
   231             TInt aExpectedLeaveCode,
       
   232             TInt aLineNumber,
       
   233             const TDesC8& aFileName );        
       
   234        
       
   235         /**
       
   236         * Records a failed assertion to the test results and
       
   237         * leaves with a Symbian unit test framework specific error code
       
   238         * to quit the execution of the test function.
       
   239         * Use macro SYMBIAN_UT_ASSERT in symbianunittestmacros.h 
       
   240         * to perform the actual assertion.  
       
   241         * 
       
   242         * @param aFailureMessage the failed assertion as text
       
   243         * @param aLineNumber the line number of the assertion
       
   244         * @param aFileName the name of the file where the assertion is located
       
   245         */        
       
   246         IMPORT_C void AssertionFailedL( 
       
   247             const TDesC8& aFailureMessage,
       
   248             TInt aLineNumber,
       
   249             const TDesC8& aFileName );        
       
   250 
       
   251         /**
       
   252         * Records a non-leaving statement to the test results and
       
   253         * leaves with a Symbian unit test framework specific error code
       
   254         * to quit the execution of the test function.
       
   255         * This function is called from macro SYMBIAN_UT_ASSERT_LEAVE 
       
   256         * in symbianunittestmacros.h to record the result of a failed assertion.
       
   257         * There is usually no need to call this function directly.
       
   258         * 
       
   259         * @param aStatement textual presentation of the statement
       
   260         * @param aLineNumber the line number of the assertion
       
   261         * @param aFileName the name of the file where the assertion is located
       
   262         */        
       
   263         IMPORT_C void RecordNoLeaveFromStatementL(
       
   264             const TDesC8& aStatement,
       
   265             TInt aLineNumber,
       
   266             const TDesC8& aFileName ); 
       
   267 
       
   268 
       
   269 
       
   270 	 /**
       
   271         * Asserts that two TInt values are equal.
       
   272         * Leaves with a Symbian unit test framework specific error code
       
   273         * if the values are not equal.
       
   274         * In case of a failed assertion, the framework records 
       
   275         * the failure reason, line number and file name to the test results.
       
   276         * Use macro SYMBIAN_UT_ASSERT_EQUALS in symbianunittestmacros.h 
       
   277         * instead to avoid the need to pass the other parameters 
       
   278         * than the actual asserted values.  
       
   279         * 
       
   280         * @param aExpectedValue the expected value
       
   281         * @param aActualValue the actual value
       
   282         * @param aLineNumber the line number of the assertion
       
   283         * @param aFileName the name of the file where the assertion is located
       
   284         * @param aFailureMessage the error message
       
   285         */        
       
   286         IMPORT_C void AssertEqualsL( 
       
   287             TInt aExpectedValue,
       
   288             TInt aActualValue,
       
   289             TInt aLineNumber,
       
   290             const TDesC8& aFileName,
       
   291 	    const TDesC8& aFailureMessage );        
       
   292 
       
   293         /**
       
   294         * Asserts that two TDesC8 values are equal.
       
   295         * Leaves with a Symbian unit test framework specific error code
       
   296         * if the values are not equal.
       
   297         * In case of a failed assertion, the framework records 
       
   298         * the failure reason, line number and file name to the test results.
       
   299         * Use macro SYMBIAN_UT_ASSERT_EQUALS in symbianunittestmacros.h 
       
   300         * instead to avoid the need to pass the other parameters 
       
   301         * than the actual asserted values.  
       
   302         * 
       
   303         * @param aExpectedValue the expected value
       
   304         * @param aActualValue the actual value
       
   305         * @param aLineNumber the line number of the assertion
       
   306         * @param aFileName the name of the file where the assertion is located
       
   307         * @param aFailureMessage the error message
       
   308         */ 
       
   309         IMPORT_C void AssertEqualsL(
       
   310             const TDesC8& aExpectedValue,
       
   311             const TDesC8& aActualValue, 
       
   312             TInt aLineNumber, 
       
   313             const TDesC8& aFileName,
       
   314 	    const TDesC8& aFailureMessage );
       
   315 
       
   316         /**
       
   317         * Asserts that two TDesC16 values are equal.
       
   318         * Leaves with a Symbian unit test framework specific error code
       
   319         * if the values are not equal.
       
   320         * In case of a failed assertion, the framework records 
       
   321         * the failure reason, line number and file name to the test results.
       
   322         * Use macro SYMBIAN_UT_ASSERT_EQUALS in symbianunittestmacros.h 
       
   323         * instead to avoid the need to pass the other parameters 
       
   324         * than the actual asserted values.  
       
   325         * 
       
   326         * @param aExpectedValue the expected value
       
   327         * @param aActualValue the actual value
       
   328         * @param aLineNumber the line number of the assertion
       
   329         * @param aFileName the name of the file where the assertion is located
       
   330         * @param aFailureMessage the error message
       
   331         */        
       
   332         IMPORT_C void AssertEqualsL(
       
   333             const TDesC16& aExpectedValue,
       
   334             const TDesC16& aActualValue,
       
   335             TInt aLineNumber,
       
   336             const TDesC8& aFileName,
       
   337 	    const TDesC8& aFailureMessage );      
       
   338         
       
   339     private: // Internal class for a unit test function's data
       
   340         
       
   341         class CSymbianUnitTestCase : public CBase
       
   342             {            
       
   343             public: // Constructors and destructor
       
   344                 
       
   345                 static CSymbianUnitTestCase* NewL( 
       
   346                     const TDesC& aName,
       
   347                     CSymbianUnitTest::FunctionPtr aSetupFunction,
       
   348                     CSymbianUnitTest::FunctionPtr aTestFunction,
       
   349                     CSymbianUnitTest::FunctionPtr aTeardownFunction );
       
   350                 
       
   351                 ~CSymbianUnitTestCase();
       
   352             
       
   353             public: // New functions
       
   354                 
       
   355                 const TDesC& Name() const;
       
   356                 
       
   357             public: // Data
       
   358                 
       
   359                 CSymbianUnitTest::FunctionPtr iSetupFunction;
       
   360                 CSymbianUnitTest::FunctionPtr iTestFunction;
       
   361                 CSymbianUnitTest::FunctionPtr iTeardownFunction;
       
   362             
       
   363             private: // Constructors
       
   364                 
       
   365                 CSymbianUnitTestCase( 
       
   366                     CSymbianUnitTest::FunctionPtr aSetupFunction,
       
   367                     CSymbianUnitTest::FunctionPtr aTestFunction,
       
   368                     CSymbianUnitTest::FunctionPtr aTeardownFunction );
       
   369                 void ConstructL( const TDesC& aName );
       
   370                 
       
   371             private: // Data
       
   372                 
       
   373                 HBufC* iName;
       
   374             };
       
   375         
       
   376         private: // New functions
       
   377         
       
   378             HBufC8* NotEqualsMessageLC( const TDesC16& aExpected,
       
   379                                         const TDesC16& aActual );
       
   380         
       
   381             void ExecuteTestCaseInThreadL( 
       
   382                 CSymbianUnitTestCase& aTestCase,
       
   383                 CSymbianUnitTestResult& aResult,
       
   384 	        TInt aTimeout);
       
   385             
       
   386             static TInt TestThreadEntryFunction( TAny* aPtr );
       
   387             
       
   388             void ExecuteTestCaseL();
       
   389             
       
   390             void DoExecuteTestCaseL( TInt& aLeaveCodeFromTest );
       
   391             
       
   392             void StartAllocFailureSimulation();
       
   393             
       
   394             void StopAllocFailureSimulation();
       
   395 
       
   396 	    static TInt TimerThreadEntryFunction( TAny* aPtr);
       
   397         
       
   398     private: // Data
       
   399     
       
   400         HBufC* iName;
       
   401         RHeap::TAllocFail iAllocFailureType;
       
   402         TUint iAllocFailureRate;
       
   403         TInt iLeakedMemory;
       
   404         TInt iLeakedResource;
       
   405         TInt iLeakedRequest;
       
   406         TInt iHeapCellsReservedByAssertFailure;
       
   407         RPointerArray< CSymbianUnitTestCase > iTestCases;
       
   408         CSymbianUnitTestResult* iTestResult; // Not owned
       
   409         CSymbianUnitTestCase* iCurrentTestCase; // Not owned
       
   410 
       
   411     private: // Test
       
   412         
       
   413         SYMBIAN_UNIT_TEST_CLASS( UT_CSymbianUnitTest )
       
   414     };
       
   415 
       
   416 #endif // SYMBIANUNITTEST_H