mpx/tsrc/public/basic/common/basetest/src/basetest.cpp
changeset 62 b276843a15ba
equal deleted inserted replaced
58:c76ea6caa649 62:b276843a15ba
       
     1 /*
       
     2 * Copyright (c) 2002 - 2007 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:  Base Class Scripted STIF cases
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <e32svr.h>
       
    20 #include <StifParser.h>
       
    21 #include <Stiftestinterface.h>
       
    22 #include "basetest.h"
       
    23 
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CBaseTest::CCollectionHelperTest
       
    27 // C++ default constructor can NOT contain any code, that
       
    28 // might leave.
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 EXPORT_C CBaseTest::CBaseTest( CTestModuleIf& aTestModuleIf ):
       
    32         CScriptBase( aTestModuleIf ), 
       
    33         iTestModuleIf(aTestModuleIf)
       
    34     {   
       
    35     }
       
    36     
       
    37 // Destructor
       
    38 EXPORT_C CBaseTest::~CBaseTest()
       
    39     { 
       
    40     // Delete logger
       
    41     delete iLog; 
       
    42     delete iTimeoutController;
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CBaseTest::ConstructL
       
    47 // Symbian 2nd phase constructor can leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 EXPORT_C void CBaseTest::ConstructL()
       
    51 {
       
    52     iTimeoutController = CSimpleTimeout::NewL (this, iLog);
       
    53 }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CBaseTest::EventName
       
    57 // Return descriptor with the notification description
       
    58 // -----------------------------------------------------------------------------
       
    59 EXPORT_C TPtrC CBaseTest::EventName( TInt aKey )
       
    60 {
       
    61     static TText* const badKeyword = (TText*)L"BadKeyword";
       
    62     static TText* const keywords[] = {
       
    63         (TText*)L"EColHandleCollectionMessage",
       
    64         (TText*)L"EColHandleOpen",
       
    65         (TText*)L"EColHandleAddFileComplete",
       
    66         (TText*)L"EColHandleRemove",
       
    67         (TText*)L"EColHandleFindAllComplete",
       
    68         (TText*)L"EPlaHandlePlaybackMessage1",
       
    69         (TText*)L"EPlaHandlePlaybackMessage2"
       
    70     };
       
    71 
       
    72     if( (TUint)aKey >= (sizeof( keywords )/sizeof(TText*)) )
       
    73     {
       
    74         iLog->Log(_L("Keyword out of bounds"));
       
    75         TPtrC keyword( badKeyword );
       
    76         return keyword;
       
    77     }
       
    78     else
       
    79     {
       
    80         TPtrC keyword( keywords[aKey] );
       
    81         return keyword;
       
    82     }
       
    83 }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CBaseTest::AddExpectedEvent
       
    87 // Add an event to the expected events' list
       
    88 // -----------------------------------------------------------------------------
       
    89 EXPORT_C void CBaseTest::AddExpectedEvent(TExpectedEvent event, TInt ms)
       
    90 {
       
    91     FTRACE(FPrint(_L("CBaseTest::AddExpectedEvent")));
       
    92     iExpectedEvents.Append(event);
       
    93     TPtrC eventName = EventName(event);
       
    94     iLog->Log(_L("Adding expected event:(0x%02x)%S Total=%d"), event, 
       
    95             &eventName, iExpectedEvents.Count() );
       
    96 
       
    97     if ( iTimeoutController && !iTimeoutController->IsActive() )
       
    98     {
       
    99         if (ms > 0)
       
   100         {
       
   101             iTimeoutController->Start( TTimeIntervalMicroSeconds(ms * 1000) );
       
   102         }
       
   103         else
       
   104         {
       
   105             iLog->Log(_L("Timeout with default value (1s)"));
       
   106             iTimeoutController->Start( TTimeIntervalMicroSeconds(1000000) );
       
   107         }
       
   108     }
       
   109 }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CBaseTest::RemoveExpectedEvent
       
   113 // Remove the indicated event from the expected events' list
       
   114 // Returns: ETrue: Event found.
       
   115 //          EFalse: Event not found.
       
   116 // -----------------------------------------------------------------------------
       
   117 EXPORT_C TBool CBaseTest::RemoveExpectedEvent(TExpectedEvent aEvent)
       
   118 {
       
   119     FTRACE(FPrint(_L("CBaseTest::RemoveExpectedEvent")));
       
   120     TBool match = EFalse;
       
   121     for (TUint i=0; i < iExpectedEvents.Count() ; i++)
       
   122     {
       
   123         if (iExpectedEvents[i] == aEvent)
       
   124         {
       
   125             iExpectedEvents.Remove(i);
       
   126             match = ETrue;
       
   127             break;
       
   128         }
       
   129     }
       
   130 
       
   131     return match;
       
   132 }
       
   133 
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CBaseTest::RemoveAllExpectedEvents
       
   137 // Remove the indicated event from the expected events' list
       
   138 // -----------------------------------------------------------------------------
       
   139 EXPORT_C void CBaseTest::RemoveAllExpectedEvents()
       
   140 {
       
   141     FTRACE(FPrint(_L("CBaseTest::RemoveAllExpectedEvents")));
       
   142     iLog->Log(_L("Removing all expected events"));
       
   143 
       
   144     iExpectedEvents.Reset();
       
   145     iOcurredEvents.Reset();
       
   146 }
       
   147 
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CBaseTest::ProcessEvent
       
   151 // Process events.
       
   152 // -----------------------------------------------------------------------------
       
   153 EXPORT_C void CBaseTest::ProcessEvent(TExpectedEvent aEvent, TInt aError)
       
   154 {
       
   155     FTRACE(FPrint(_L("CBaseTest::ProcessExpectedEvent")));
       
   156     TPtrC nameEvent = EventName(aEvent);
       
   157 
       
   158     // Check for error
       
   159     if (aError == KErrNone)
       
   160     {
       
   161         // Remove the event
       
   162         if (RemoveExpectedEvent(aEvent))
       
   163         {
       
   164             iLog->Log(_L("Expected Event: (0x%02x)%S has ocurred Total=%d"), 
       
   165                     aEvent, &nameEvent,iExpectedEvents.Count());
       
   166         }
       
   167         else
       
   168         {
       
   169             iLog->Log(_L("Event: (0x%02x)%S has ocurred"), aEvent, &nameEvent);
       
   170             return;
       
   171         }
       
   172 
       
   173         // All expected events have ocurred
       
   174         if (iExpectedEvents.Count() == 0 )
       
   175         {
       
   176             Signal();
       
   177             iTimeoutController->Cancel();
       
   178         }
       
   179     }
       
   180     else
       
   181     {
       
   182         iLog->Log(_L("[Error] Event: (0x%02x)%S return with error code=%d"), 
       
   183                 aEvent, &nameEvent, aError);
       
   184         
       
   185         if (iExpectedEvents.Count() != 0 )
       
   186         {
       
   187             RemoveExpectedEvent(aEvent);
       
   188         }
       
   189         
       
   190         iTimeoutController->Cancel();
       
   191         Signal(KErrCallbackErrorCode);
       
   192     }
       
   193 }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CBaseTest::HandleTimeout
       
   197 // Review if all the expected events have ocurred once the time is over
       
   198 // -----------------------------------------------------------------------------
       
   199 EXPORT_C void CBaseTest::HandleTimeout(TInt aError)
       
   200 {
       
   201     FTRACE(FPrint(_L("CBaseTest::HandleTimeout")));
       
   202     // All expected events have ocurred
       
   203     if (aError != KErrNone)
       
   204     {
       
   205         if (iExpectedEvents.Count() == 0 )
       
   206         {
       
   207             iLog->Log(_L("Timing out but events have ocurred"));
       
   208             Signal();
       
   209         }
       
   210         else
       
   211         {
       
   212             RemoveAllExpectedEvents();
       
   213             iLog->Log(_L("Timing out and events still pending"));
       
   214             Signal(KErrEventPending);
       
   215         }
       
   216     }
       
   217     else
       
   218     {
       
   219         iLog->Log(_L("Timing out return a error %d"), aError);
       
   220         Signal(aError);
       
   221     }
       
   222 }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CBaseTest::SetTimeout
       
   226 // Create a timer and set a timeout
       
   227 // When the timeout is reached the test case is marked as failed
       
   228 // It's used rather than the "timeout" keyword in the configuration file
       
   229 // because in this way the log continues
       
   230 // -----------------------------------------------------------------------------
       
   231 EXPORT_C TInt CBaseTest::SetTimeout( CStifItemParser& aItem )
       
   232 {
       
   233     FTRACE(FPrint(_L("CBaseTest::SetTimeout")));
       
   234     TInt timeout=0;
       
   235     TInt error = aItem.GetNextInt(timeout) ;
       
   236     if ( iTimeoutController )
       
   237     {
       
   238         if ( timeout > 0 )
       
   239         {
       
   240             iTimeoutController->Start( TTimeIntervalMicroSeconds(timeout*1000) );
       
   241         }
       
   242         else
       
   243         {
       
   244             iTimeoutController->Start( TTimeIntervalMicroSeconds(1000000) );
       
   245         }
       
   246     }
       
   247     else
       
   248     {
       
   249         iLog->Log(_L("Timeout Controller doesn't exist"));
       
   250         error = KErrTimeoutController;
       
   251     }
       
   252     return error;
       
   253 }
       
   254 
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // Uses the TestModuleBase API to allow a panic as exit reason for a test case
       
   258 // Returns: Symbian OS errors.
       
   259 // -----------------------------------------------------------------------------
       
   260 EXPORT_C TInt CBaseTest::SetAllowedPanic( CStifItemParser& aItem )
       
   261 {
       
   262     FTRACE(FPrint(_L("CBaseTest::SetAllowedPanic")));
       
   263     TInt error = KErrNone;
       
   264     TInt panicCode;
       
   265     TPtrC panicType;
       
   266     if (  ( KErrNone == aItem.GetNextString(panicType) ) &&
       
   267     ( KErrNone == aItem.GetNextInt(panicCode) )  )
       
   268     {
       
   269         iLog->Log(_L("Allowing panic: %S %d"), &panicType, panicCode);
       
   270         iTestModuleIf.SetExitReason( CTestModuleIf::EPanic, panicCode );
       
   271         iNormalExitReason = EFalse;
       
   272     }
       
   273     else
       
   274     {
       
   275         iLog->Log(KMsgBadTestParameters);
       
   276         error = KErrBadTestParameter;
       
   277     }
       
   278     return error;
       
   279 }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CBaseTest::SetExpectedEvents()
       
   283 // Returns: Symbian OS errors.
       
   284 // -----------------------------------------------------------------------------
       
   285 EXPORT_C TInt CBaseTest::SetExpectedEvents( CStifItemParser& aItem )
       
   286 {
       
   287     FTRACE(FPrint(_L("CBaseTest::SetExpectedEvents")));
       
   288     TInt error = KErrNone;
       
   289     TInt event=0;
       
   290     while ( KErrNone == aItem.GetNextInt(event))
       
   291     {
       
   292         AddExpectedEvent(static_cast<TExpectedEvent>(event), 0); // Default timeout value
       
   293     }
       
   294     return error;
       
   295 }