stif/DemoModule/src/DemoModule.cpp
branchRCL_3
changeset 59 8ad140f3dd41
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     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: This file (DemoModule.cpp) contains all test 
       
    15 * framework related parts of this test module. Actual test cases 
       
    16 * are implemented in file DemoModuleCases.cpp.
       
    17 *
       
    18 * CTestModuleDemo is an example of test module implementation. This example
       
    19 * uses hard coded test cases (i.e it does not have any test case
       
    20 * configuration file).
       
    21 *
       
    22 * Example uses function pointers to call test cases. This provides an easy
       
    23 * method to add new test cases.
       
    24 *
       
    25 * See function Cases in file DemoModuleCases.cpp for instructions how to
       
    26 * add new test cases. It is not necessary to modify this file when adding
       
    27 * new test cases.
       
    28 *
       
    29 * To take this module into use, add following lines to test framework
       
    30 * initialisation file:
       
    31 *
       
    32 * # Demo module
       
    33 * [New_Module]
       
    34 * ModuleName= DemoModule
       
    35 * [End_Module]
       
    36 *
       
    37 */
       
    38 
       
    39 // INCLUDE FILES
       
    40 #include <StifTestInterface.h>
       
    41 #include "DemoModule.h"
       
    42 #include <e32math.h>
       
    43 #include "SettingServerClient.h"
       
    44 
       
    45 // EXTERNAL DATA STRUCTURES
       
    46 // None
       
    47 
       
    48 // EXTERNAL FUNCTION PROTOTYPES  
       
    49 // None
       
    50 
       
    51 // CONSTANTS
       
    52 // None
       
    53 
       
    54 // MACROS
       
    55 // None
       
    56 
       
    57 // LOCAL CONSTANTS AND MACROS
       
    58 // None
       
    59 
       
    60 // MODULE DATA STRUCTURES
       
    61 // None
       
    62 
       
    63 // LOCAL FUNCTION PROTOTYPES
       
    64 // None
       
    65 
       
    66 // FORWARD DECLARATIONS
       
    67 // None
       
    68 
       
    69 // ==================== LOCAL FUNCTIONS =======================================
       
    70 
       
    71 
       
    72 // ================= MEMBER FUNCTIONS =========================================
       
    73 
       
    74 /*
       
    75 -------------------------------------------------------------------------------
       
    76 
       
    77     Class: CDemoModule
       
    78 
       
    79     Method: CDemoModule
       
    80 
       
    81     Description: C++ default constructor can NOT contain any code, that
       
    82     might leave.
       
    83 
       
    84     Parameters: None
       
    85 
       
    86     Return Values: None
       
    87 
       
    88     Errors/Exceptions: None
       
    89 
       
    90     Status: Approved
       
    91 
       
    92 -------------------------------------------------------------------------------
       
    93 */
       
    94 CDemoModule::CDemoModule()
       
    95     {
       
    96     }
       
    97 
       
    98 /*
       
    99 -------------------------------------------------------------------------------
       
   100 
       
   101     Class: CDemoModule
       
   102 
       
   103     Method: ConstructL
       
   104 
       
   105     Description: Symbian 2nd phase constructor that can leave.
       
   106 
       
   107     Note: If OOM test case uses STIF Logger, then STIF Logger must be created
       
   108     with static buffer size parameter (aStaticBufferSize). Otherwise Logger 
       
   109     allocates memory from heap and therefore causes error situations with OOM 
       
   110     testing. For more information about STIF Logger construction, see STIF 
       
   111     Users Guide.
       
   112 
       
   113     Parameters: None
       
   114 
       
   115     Return Values: None
       
   116 
       
   117     Errors/Exceptions: None
       
   118 
       
   119     Status: Approved
       
   120 
       
   121 -------------------------------------------------------------------------------
       
   122 */
       
   123 void CDemoModule::ConstructL()
       
   124     {
       
   125     //Read logger settings to check whether test case name is to be
       
   126     //appended to log file name.
       
   127     RSettingServer settingServer;
       
   128     TInt ret = settingServer.Connect();
       
   129     if(ret != KErrNone)
       
   130         {
       
   131         User::Leave(ret);
       
   132         }
       
   133     // Struct to StifLogger settigs.
       
   134     TLoggerSettings loggerSettings; 
       
   135     // Parse StifLogger defaults from STIF initialization file.
       
   136     ret = settingServer.GetLoggerSettings(loggerSettings);
       
   137     if(ret != KErrNone)
       
   138         {
       
   139         User::Leave(ret);
       
   140         } 
       
   141     // Close Setting server session
       
   142     settingServer.Close();
       
   143     iAddTestCaseTitleToLogName = loggerSettings.iAddTestCaseTitle;
       
   144     
       
   145     // Constructing static buffer size logger, needed with OOM testing because
       
   146     // normally logger allocates memory from heap!
       
   147     iStdLog = CStifLogger::NewL( KDemoModuleLogPath, 
       
   148                               KDemoModuleLogFile,
       
   149                               CStifLogger::ETxt,
       
   150                               CStifLogger::EFile,
       
   151                               ETrue,
       
   152                               ETrue,
       
   153                               ETrue,
       
   154                               EFalse,
       
   155                               ETrue,
       
   156                               EFalse,
       
   157                               100 );
       
   158     iLog = iStdLog;
       
   159     
       
   160     // Sample how to use logging
       
   161     _LIT( KLogInfo, "DemoModule logging starts!" );
       
   162     iLog->Log( KLogInfo );
       
   163     }
       
   164 
       
   165 /*
       
   166 -------------------------------------------------------------------------------
       
   167 
       
   168     Class: CTestModuleDemo
       
   169 
       
   170     Method: NewL
       
   171 
       
   172     Description: Two-phased constructor. Constructs new CTestModuleDemo
       
   173     instance and returns pointer to it.
       
   174 
       
   175     Parameters:    None
       
   176 
       
   177     Return Values: CTestModuleDemo*: new object.
       
   178 
       
   179     Errors/Exceptions: Leaves if memory allocation fails or ConstructL leaves.
       
   180 
       
   181     Status: Approved
       
   182 
       
   183 -------------------------------------------------------------------------------
       
   184 */
       
   185 CDemoModule* CDemoModule::NewL()
       
   186     {
       
   187     CDemoModule* self = new (ELeave) CDemoModule;
       
   188 
       
   189     CleanupStack::PushL( self );
       
   190     self->ConstructL();
       
   191     CleanupStack::Pop();
       
   192 
       
   193     return self;
       
   194     }
       
   195 
       
   196 /*
       
   197 -------------------------------------------------------------------------------
       
   198 
       
   199     Class: CTestModuleDemo
       
   200 
       
   201     Method: ~CTestModuleDemo
       
   202 
       
   203     Description: Destructor.
       
   204 
       
   205     Parameters:    None
       
   206 
       
   207     Return Values: None
       
   208 
       
   209     Errors/Exceptions: None
       
   210 
       
   211     Status: Approved
       
   212 
       
   213 -------------------------------------------------------------------------------
       
   214 */
       
   215 CDemoModule::~CDemoModule()
       
   216     { 
       
   217     iLog = NULL;
       
   218     delete iStdLog;
       
   219     iStdLog = NULL;
       
   220     delete iTCLog;
       
   221     iTCLog = NULL;
       
   222     }
       
   223 
       
   224 /*
       
   225 -------------------------------------------------------------------------------
       
   226     Class: CTestModuleDemo
       
   227 
       
   228     Method: InitL
       
   229 
       
   230     Description: Method for test case initialization
       
   231     
       
   232     Parameters: None
       
   233 
       
   234     Return Values: None
       
   235 
       
   236     Errors/Exceptions: None
       
   237 
       
   238     Status: Approved
       
   239 -------------------------------------------------------------------------------
       
   240 */
       
   241 TInt CDemoModule::InitL( TFileName& /*aIniFile*/, 
       
   242                          TBool /*aFirstTime*/ )
       
   243     {
       
   244     return KErrNone;
       
   245 
       
   246     }
       
   247 
       
   248 /*
       
   249 -------------------------------------------------------------------------------
       
   250 
       
   251     Class: CTestModuleDemo
       
   252 
       
   253     Method: GetTestCases
       
   254 
       
   255     Description: GetTestCases is used to inquire test cases 
       
   256     from the test module. Because this test module has hard coded test cases
       
   257     (i.e cases are not read from file), paramter aConfigFile is not used.
       
   258 
       
   259     This function loops through all cases defined in Cases() function and 
       
   260     adds corresponding items to aTestCases array.
       
   261 
       
   262     Parameters: const TFileName&  : in: Configuration file name. Not used                                                       
       
   263                 RPointerArray<TTestCaseInfo>& aTestCases: out: 
       
   264                       Array of TestCases.
       
   265     
       
   266     Return Values: KErrNone: No error
       
   267 
       
   268     Errors/Exceptions: Function leaves if any memory allocation operation fails
       
   269 
       
   270     Status: Proposal
       
   271     
       
   272 -------------------------------------------------------------------------------
       
   273 */      
       
   274 TInt CDemoModule::GetTestCasesL( const TFileName& /*aConfig*/, 
       
   275                                  RPointerArray<TTestCaseInfo>& aTestCases )
       
   276     {
       
   277     // Loop through all test cases and create new
       
   278     // TTestCaseInfo items and append items to aTestCase array    
       
   279     for( TInt i = 0; Case(i).iMethod != NULL; i++ )
       
   280         {
       
   281         // Allocate new TTestCaseInfo from heap for a testcase definition.
       
   282         TTestCaseInfo* newCase = new( ELeave ) TTestCaseInfo();
       
   283     
       
   284         // PushL TTestCaseInfo to CleanupStack.    
       
   285         CleanupStack::PushL( newCase );
       
   286 
       
   287         // Set number for the testcase.
       
   288         // When the testcase is run, this comes as a parameter to RunTestCaseL.
       
   289         newCase->iCaseNumber = i;
       
   290 
       
   291         // Set title for the test case. This is shown in UI to user.
       
   292         newCase->iTitle.Copy( Case(i).iCaseName );
       
   293 
       
   294         // Append TTestCaseInfo to the testcase array. After appended 
       
   295         // successfully the TTestCaseInfo object is owned (and freed) 
       
   296         // by the TestServer. 
       
   297         User::LeaveIfError(aTestCases.Append ( newCase ) );
       
   298         
       
   299         // Pop TTestCaseInfo from the CleanupStack.
       
   300         CleanupStack::Pop( newCase );
       
   301         }
       
   302 
       
   303     return KErrNone;
       
   304 
       
   305     }
       
   306 
       
   307 /*
       
   308 -------------------------------------------------------------------------------
       
   309 
       
   310     Class: CTestModuleDemo
       
   311 
       
   312     Method: RunTestCase
       
   313 
       
   314     Description: Run a specified testcase.
       
   315 
       
   316     Function runs a test case specified by test case number. Test case file
       
   317     parameter is not used.
       
   318 
       
   319     If case number is valid, this function runs a test case returned by
       
   320     function Cases(). 
       
   321 
       
   322     Parameters: const TInt aCaseNumber: in: Testcase number 
       
   323                 const TFileName& : in: Configuration file name. Not used
       
   324                 TTestResult& aResult: out: Testcase result
       
   325 
       
   326     Return Values: KErrNone: Testcase ran.
       
   327                    KErrNotFound: Unknown testcase
       
   328 
       
   329     Errors/Exceptions: None
       
   330     
       
   331     Status: Proposal
       
   332 
       
   333 -------------------------------------------------------------------------------
       
   334 */
       
   335 TInt CDemoModule::RunTestCaseL( const TInt aCaseNumber,
       
   336                                 const TFileName& /* aConfig */,
       
   337                                 TTestResult& aResult )
       
   338     {
       
   339     // Return value
       
   340     TInt execStatus = KErrNone;
       
   341 
       
   342     // Get the pointer to test case function
       
   343     TCaseInfo tmp = Case ( aCaseNumber );
       
   344 
       
   345     _LIT( KLogInfo, "Starting testcase [%S]" );
       
   346     iLog->Log( KLogInfo, &tmp.iCaseName);
       
   347 
       
   348     // Check that case number was valid
       
   349     if ( tmp.iMethod != NULL )
       
   350         {
       
   351         //Open new log file with test case title in file name
       
   352         if(iAddTestCaseTitleToLogName)
       
   353             {
       
   354             //delete iLog; //Close currently opened log
       
   355             //iLog = NULL;
       
   356             //Delete test case logger if exists
       
   357             if(iTCLog)
       
   358                 {
       
   359                 delete iTCLog;
       
   360                 iTCLog = NULL;
       
   361                 }
       
   362                 
       
   363             TFileName logFileName;
       
   364             TName title;
       
   365             TestModuleIf().GetTestCaseTitleL(title);
       
   366         
       
   367             logFileName.Format(KDemoModuleLogFileWithTitle, &title);
       
   368 
       
   369             iTCLog = CStifLogger::NewL(KDemoModuleLogPath, 
       
   370                                      logFileName,
       
   371                                      CStifLogger::ETxt,
       
   372                                      CStifLogger::EFile,
       
   373                                      ETrue,
       
   374                                      ETrue,
       
   375                                      ETrue,
       
   376                                      EFalse,
       
   377                                      ETrue,
       
   378                                      EFalse,
       
   379                                      100);
       
   380             iLog = iTCLog;
       
   381             }
       
   382 
       
   383         // Valid case was found, call it via function pointer
       
   384         iMethod = tmp.iMethod;        
       
   385         //execStatus  = ( this->*iMethod )( aResult );
       
   386         TRAPD(err, execStatus  = ( this->*iMethod )( aResult ));
       
   387         if(iAddTestCaseTitleToLogName)
       
   388             {
       
   389             //Restore standard log and destroy test case logger
       
   390             iLog = iStdLog;
       
   391             delete iTCLog; //Close currently opened log
       
   392             iTCLog = NULL;
       
   393             }
       
   394         User::LeaveIfError(err);
       
   395         
       
   396         }
       
   397     else
       
   398         {
       
   399         // Valid case was not found, return error.
       
   400         execStatus = KErrNotFound;
       
   401         }
       
   402 
       
   403     // Return case execution status (not the result of the case execution)
       
   404     return execStatus;
       
   405 
       
   406     }
       
   407 
       
   408 /*
       
   409 -------------------------------------------------------------------------------
       
   410 
       
   411     Class: CDemoModule
       
   412 
       
   413     Method: OOMTestQueryL
       
   414 
       
   415     Description: Checks test case information for OOM execution. 
       
   416 
       
   417     Return Values: TBool
       
   418 
       
   419     Errors/Exceptions: None
       
   420 
       
   421     Status: Proposal
       
   422 
       
   423 -------------------------------------------------------------------------------
       
   424 */
       
   425 TBool CDemoModule::OOMTestQueryL( const TFileName& /* aTestCaseFile */, 
       
   426                                   const TInt aCaseNumber, 
       
   427                                   TOOMFailureType& /* aFailureType */, 
       
   428                                   TInt& aFirstMemFailure, 
       
   429                                   TInt& aLastMemFailure ) 
       
   430     {
       
   431     _LIT( KLogInfo, "CDemoModule::OOMTestQueryL" );
       
   432     iLog->Log( KLogInfo ); 
       
   433 
       
   434     aFirstMemFailure = Case( aCaseNumber ).iFirstMemoryAllocation;
       
   435     aLastMemFailure = Case( aCaseNumber ).iLastMemoryAllocation;
       
   436 
       
   437     return Case( aCaseNumber ).iIsOOMTest;
       
   438     }
       
   439 
       
   440 /*
       
   441 -------------------------------------------------------------------------------
       
   442 
       
   443     Class: CDemoModule
       
   444 
       
   445     Method: OOMTestInitializeL
       
   446 
       
   447     Description: Used to perform the test environment setup for a particular
       
   448     OOM test case. Test Modules may use the initialization file to read 
       
   449     parameters for Test Module initialization but they can also have their own
       
   450     configure file or some other routine to initialize themselves. 
       
   451 
       
   452     NOTE: User may add implementation for OOM test environment initialization.
       
   453     Usually no implementation is required.
       
   454 
       
   455     Return Values: None
       
   456 
       
   457     Errors/Exceptions: None
       
   458 
       
   459     Status: Proposal
       
   460 
       
   461 -------------------------------------------------------------------------------
       
   462 */
       
   463 void CDemoModule::OOMTestInitializeL( const TFileName& /* aTestCaseFile */, 
       
   464                                       const TInt /* aCaseNumber */ )
       
   465     {
       
   466     _LIT( KLogInfo, "CDemoModule::OOMTestInitializeL" );
       
   467     iLog->Log( KLogInfo ); 
       
   468 
       
   469     }
       
   470 
       
   471 /*
       
   472 -------------------------------------------------------------------------------
       
   473 
       
   474     Class: CDemoModule
       
   475 
       
   476     Method: OOMHandleWarningL
       
   477 
       
   478     Description: Used in OOM testing to provide a way to the derived TestModule 
       
   479     to handle warnings related to non-leaving or TRAPped allocations.
       
   480 
       
   481     In some cases the allocation should be skipped, either due to problems in 
       
   482     the OS code or components used by the code being tested, or even inside the
       
   483     tested components which are implemented this way on purpose (by design), so
       
   484     it is important to give the tester a way to bypass allocation failures.
       
   485 
       
   486     NOTE: User may add implementation for OOM test warning handling. Usually no
       
   487     implementation is required.    
       
   488 
       
   489     Return Values: None
       
   490 
       
   491     Errors/Exceptions: None
       
   492 
       
   493     Status: Proposal
       
   494 
       
   495 -------------------------------------------------------------------------------
       
   496 */
       
   497 void CDemoModule::OOMHandleWarningL( const TFileName& /* aTestCaseFile */,
       
   498                                      const TInt /* aCaseNumber */, 
       
   499                                      TInt& /* aFailNextValue */ )
       
   500     {
       
   501     _LIT( KLogInfo, "CDemoModule::OOMHandleWarningL" );
       
   502     iLog->Log( KLogInfo );
       
   503 
       
   504     }
       
   505 
       
   506 /*
       
   507 -------------------------------------------------------------------------------
       
   508 
       
   509     Class: CDemoModule
       
   510 
       
   511     Method: OOMTestFinalizeL
       
   512 
       
   513     Description: Used to perform the test environment cleanup for a particular OOM 
       
   514     test case.
       
   515 
       
   516     NOTE: User may add implementation for OOM test environment finalization.
       
   517     Usually no implementation is required.   
       
   518 
       
   519     Return Values: None
       
   520 
       
   521     Errors/Exceptions: None
       
   522 
       
   523     Status: Proposal
       
   524 
       
   525 -------------------------------------------------------------------------------
       
   526 */
       
   527 void CDemoModule::OOMTestFinalizeL( const TFileName& /* aTestCaseFile */, 
       
   528                                     const TInt /* aCaseNumber */ )
       
   529     {
       
   530     _LIT( KLogInfo, "CDemoModule::OOMTestFinalizeL" );
       
   531     iLog->Log( KLogInfo );
       
   532 
       
   533     }
       
   534 
       
   535 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   536 
       
   537 // -----------------------------------------------------------------------------
       
   538 // LibEntryL is a polymorphic Dll entry point
       
   539 // Returns: CTestModuleBase*: Pointer to Test Module object
       
   540 // -----------------------------------------------------------------------------
       
   541 //
       
   542 EXPORT_C CTestModuleBase* LibEntryL()
       
   543     {
       
   544     return CDemoModule::NewL();
       
   545 
       
   546     }
       
   547 
       
   548 // -----------------------------------------------------------------------------
       
   549 // SetRequirements handles test module parameters(implements evolution
       
   550 // version 1 for test module's heap and stack sizes configuring).
       
   551 // Returns: TInt: Symbian error code.
       
   552 // -----------------------------------------------------------------------------
       
   553 //
       
   554 EXPORT_C TInt SetRequirements( CTestModuleParam*& /*aTestModuleParam*/, 
       
   555                                 TUint32& /*aParameterValid*/ )
       
   556     {
       
   557 
       
   558     /* --------------------------------- NOTE ---------------------------------
       
   559     USER PANICS occurs in test thread creation when:
       
   560     1) "The panic occurs when the value of the stack size is negative."
       
   561     2) "The panic occurs if the minimum heap size specified is less
       
   562        than KMinHeapSize".
       
   563        KMinHeapSize: "Functions that require a new heap to be allocated will
       
   564        either panic, or will reset the required heap size to this value if
       
   565        a smaller heap size is specified".
       
   566     3) "The panic occurs if the minimum heap size specified is greater than
       
   567        the maximum size to which the heap can grow".
       
   568     Other:
       
   569     1) Make sure that your hardware or Symbian OS is supporting given sizes.
       
   570        e.g. Hardware might support only sizes that are divisible by four.
       
   571     ------------------------------- NOTE end ------------------------------- */
       
   572 
       
   573     // Normally STIF uses default heap and stack sizes for test thread, see:
       
   574     // KTestThreadMinHeap, KTestThreadMinHeap and KStackSize.
       
   575     // If needed heap and stack sizes can be configured here by user. Remove
       
   576     // comments and define sizes.
       
   577 
       
   578 /*
       
   579     aParameterValid = KStifTestModuleParameterChanged;
       
   580 
       
   581     CTestModuleParamVer01* param = CTestModuleParamVer01::NewL();
       
   582     // Stack size
       
   583     param->iTestThreadStackSize= 16384; // 16K stack
       
   584     // Heap sizes
       
   585     param->iTestThreadMinHeap = 4096;   // 4K heap min
       
   586     param->iTestThreadMaxHeap = 1048576;// 1M heap max
       
   587 
       
   588     aTestModuleParam = param;
       
   589 */
       
   590     return KErrNone;
       
   591 
       
   592     }
       
   593 
       
   594 // End of File