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