os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/TestEngine.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // @file testengine.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "TestEngine.h"
    20 #include "testdebug.h"
    21 #include "TestCaseController.h"
    22 #include "TestCaseFactory.h"
    23 
    24 // Console application options
    25 
    26 _LIT(KArgRole,"-role=");
    27 _LIT(KArgTestCases,"-cases=");
    28 _LIT(KArgTestRepeats,"-repeats=");
    29 
    30 // Role values 
    31 
    32 _LIT(KArgRoleHost,"host");
    33 _LIT(KArgRoleClient,"client");
    34 
    35 
    36 extern RTest gtest;
    37 
    38 namespace NUnitTesting_USBDI
    39 	{
    40 const TUint KDefaultNumRepeats = 1;
    41 const TUint KTestIdSize = 4;
    42 _LIT(KTestStringPreamble,"PBASE-T_USBDI-");
    43 	
    44 CTestEngine* CTestEngine::NewL()
    45 	{
    46 	CTestEngine* self = new (ELeave) CTestEngine;
    47 	CleanupStack::PushL(self);
    48 	self->ConstructL();
    49 	CleanupStack::Pop(self);
    50 	return self;
    51 	}
    52 
    53 
    54 
    55 CTestEngine::CTestEngine()
    56 :	CActive(EPriorityUserInput),
    57 	iTestCaseIndex(0), iRepeat(0), iNumRepeats(KDefaultNumRepeats)
    58 	{
    59 	}
    60 
    61 
    62 
    63 CTestEngine::~CTestEngine()
    64 	{
    65 	// Cancel reading user console input
    66 	Cancel();
    67 	
    68 	// Destroy the test case controller
    69 	delete iTestCaseController;
    70 	
    71 	// Destroy the identity array and its contents
    72 	iTestCasesIdentities.ResetAndDestroy();
    73 	
    74 	// Finish test and release resources
    75 	gtest.End();
    76 	gtest.Close();
    77 	}
    78 	
    79 	
    80 	
    81 void CTestEngine::ConstructL()
    82 	{
    83 	LOG_FUNC
    84 	CActiveScheduler::Add(this);
    85 
    86 	// Display information (construction text and OS build version number
    87 	gtest.Title();
    88 	gtest.Start(_L("Test Engine Initiation"));
    89 	gtest.Printf(_L(">>\n"));
    90 	gtest.Printf(_L(">>   T E S T   R U N \n"));
    91 	gtest.Printf(_L(">>\n"));
    92 
    93 	// Process the command line option for role
    94 	TInt cmdLineLength(User::CommandLineLength());
    95 	HBufC* cmdLine = HBufC::NewMax(cmdLineLength);
    96 	CleanupStack::PushL(cmdLine);
    97 	TPtr cmdLinePtr = cmdLine->Des();
    98 	User::CommandLine(cmdLinePtr);
    99 	
   100 	// be careful, command line length is limited(248 characters)	
   101 	gtest.Printf(_L("***cmdLine = %lS\n"), cmdLine);
   102 		
   103 	TLex args(*cmdLine);
   104 	args.SkipSpace();
   105 	
   106 	// Obtain the role of this test module
   107 	TPtrC roleToken = args.NextToken(); // e.g. -role=host
   108 	TBool hostFlag(ETrue);
   109 	
   110 	TInt pos(roleToken.FindF(KArgRole));
   111 	if(pos != KErrNotFound)
   112 		{
   113 		pos = roleToken.FindF(_L("="));
   114 		TPtrC role = roleToken.Right(roleToken.Length()-pos-1);
   115 		if(role.Compare(KArgRoleHost) == 0)
   116 			{
   117 			hostFlag = ETrue;
   118 			}
   119 		else if(role.Compare(KArgRoleClient) == 0)
   120 			{
   121 			hostFlag = EFalse;
   122 			}
   123 		else
   124 			{
   125 			gtest.Printf(_L("Test configuration: could not find option -role\n"));
   126 			gtest(EFalse);
   127 			}
   128 		}
   129 	else
   130 		{
   131 		gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgRole);
   132 		gtest(EFalse);
   133 		}
   134 		
   135 	// Obtain the test cases to be run
   136 	TPtrC casesToken = args.NextToken();
   137 	
   138 	pos = casesToken.FindF(KArgTestCases);
   139 	if(pos != KErrNotFound)
   140 		{
   141 		pos = casesToken.FindF(_L("="));
   142 		TPtrC testCases = casesToken.Right(casesToken.Length()-pos-1);
   143 	
   144 		// Remaining test cases
   145 		TPtrC remCases(testCases);
   146 		while(pos != KErrNotFound)
   147 			{
   148 			pos = remCases.FindF(_L(","));
   149 			HBufC* tc = HBufC::NewLC(KTestCaseIdLength);
   150 			TPtr tcPtr = tc->Des();
   151 			tcPtr.Append(KTestStringPreamble);	
   152 			
   153 			if(pos == KErrNotFound)
   154 				{
   155 				// This is the last test case identity			
   156 				tcPtr.Append(remCases);
   157 				}
   158 			else
   159 				{ 			
   160 				tcPtr.Append(remCases.Left(KTestIdSize));
   161 				}									
   162 							
   163 			gtest.Printf(_L("Test case specified: %S\n"),tc);
   164 			
   165 						
   166 			iTestCasesIdentities.Append(tc);
   167 			CleanupStack::Pop(tc);
   168 			remCases.Set(testCases.Right(remCases.Length()-pos-1));
   169 			}
   170 		}
   171 	else
   172 		{
   173 		gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestCases);
   174 		gtest(EFalse);		
   175 		}
   176 				
   177 	// Obtain the role of this test module
   178 	TPtrC repeatsToken = args.NextToken(); // e.g. -repeats=4
   179 	
   180 	pos = repeatsToken.FindF(KArgTestRepeats);
   181 	if(pos != KErrNotFound)
   182 		{
   183 		pos = repeatsToken.FindF(_L("="));
   184 		TPtrC repeats = repeatsToken.Right(repeatsToken.Length()-pos-1);
   185 		TLex lex(repeats);
   186 		TInt ret = lex.Val(iNumRepeats, EDecimal);
   187 		if(ret)
   188 			{
   189 			gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestRepeats);
   190 			gtest.Printf(_L("DEFAULT to number of repeats = %d\n"),KDefaultNumRepeats);
   191 			iNumRepeats = KDefaultNumRepeats;
   192 			}
   193 		gtest.Printf(_L("Test repeats specified: %d cycles\n"),iNumRepeats);
   194 		}
   195 	else
   196 		{
   197 		gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestRepeats);
   198 		gtest.Printf(_L("DEFAULT to number of repeats = %d\n"),KDefaultNumRepeats);
   199 		iNumRepeats = KDefaultNumRepeats;
   200 		}
   201 		
   202 	// Create the test case controller
   203 	gtest.Printf(_L("Creating the test controller\n"));
   204 	iTestCaseController = CTestCaseController::NewL(*this,hostFlag);
   205 
   206 	CleanupStack::PopAndDestroy(cmdLine);
   207 
   208 	gtest.Console()->Read(iStatus);
   209 	SetActive();	
   210 	}
   211 	
   212 
   213 TInt CTestEngine::NextTestCaseId(TDes& aTestCaseId)
   214 	{
   215 	LOG_FUNC
   216 	if(iTestCaseIndex < iTestCasesIdentities.Count())
   217 		{
   218 		aTestCaseId = *iTestCasesIdentities[iTestCaseIndex++];
   219 		if(iTestCaseIndex==iTestCasesIdentities.Count())
   220 			{
   221 			iRepeat++;
   222 			if(iRepeat < iNumRepeats)
   223 				{
   224 				iTestCaseIndex = 0; //prepare to start again
   225 				}
   226 			}
   227 		return KErrNone;
   228 		}
   229 	else
   230 		{
   231 		return KErrNotFound;
   232 		}
   233 	}
   234 
   235 RPointerArray<HBufC>& CTestEngine::TestCasesIdentities()
   236 	{
   237 	return iTestCasesIdentities;
   238 	}
   239 
   240 TUint CTestEngine::NumRepeats()
   241 	{
   242 	return iNumRepeats;
   243 	}
   244 
   245 void CTestEngine::DoCancel()
   246 	{
   247 	LOG_FUNC
   248 	gtest.Console()->ReadCancel();	
   249 	}
   250 	
   251 
   252 void CTestEngine::RunL()
   253 	{
   254 	LOG_FUNC
   255 	TInt completionCode(iStatus.Int());
   256 	
   257 	if(completionCode == KErrNone)
   258 		{
   259 		// Possibility of displaying a range of key commands
   260 		// then gtest.Console()->Getch()
   261 		
   262 		TKeyCode keyCode(gtest.Console()->KeyCode());
   263 		if(keyCode == EKeySpace)
   264 			{
   265 			iTestCaseController->Cancel();
   266 			gtest.Printf(_L("Test module terminating\n"));
   267 			RDebug::Printf("CActiveScheduler::Stop CTestEngine::RunL");
   268 			CActiveScheduler::Stop();
   269 			}
   270 		else
   271 			{
   272 			gtest.Printf(_L("%d key pressed"),keyCode);
   273 			}
   274 		}
   275 	else
   276 		{
   277 		gtest.Printf(_L("Manual key error %d\n"),completionCode);
   278 		SetActive();
   279 		}
   280 	}
   281 	
   282 	
   283 TInt CTestEngine::RunError(TInt aError)
   284 	{
   285 	return KErrNone;
   286 	}
   287 
   288 	}