1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/TestEngine.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,288 @@
1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// @file testengine.cpp
1.18 +// @internalComponent
1.19 +//
1.20 +//
1.21 +
1.22 +#include "TestEngine.h"
1.23 +#include "testdebug.h"
1.24 +#include "TestCaseController.h"
1.25 +#include "TestCaseFactory.h"
1.26 +
1.27 +// Console application options
1.28 +
1.29 +_LIT(KArgRole,"-role=");
1.30 +_LIT(KArgTestCases,"-cases=");
1.31 +_LIT(KArgTestRepeats,"-repeats=");
1.32 +
1.33 +// Role values
1.34 +
1.35 +_LIT(KArgRoleHost,"host");
1.36 +_LIT(KArgRoleClient,"client");
1.37 +
1.38 +
1.39 +extern RTest gtest;
1.40 +
1.41 +namespace NUnitTesting_USBDI
1.42 + {
1.43 +const TUint KDefaultNumRepeats = 1;
1.44 +const TUint KTestIdSize = 4;
1.45 +_LIT(KTestStringPreamble,"PBASE-T_USBDI-");
1.46 +
1.47 +CTestEngine* CTestEngine::NewL()
1.48 + {
1.49 + CTestEngine* self = new (ELeave) CTestEngine;
1.50 + CleanupStack::PushL(self);
1.51 + self->ConstructL();
1.52 + CleanupStack::Pop(self);
1.53 + return self;
1.54 + }
1.55 +
1.56 +
1.57 +
1.58 +CTestEngine::CTestEngine()
1.59 +: CActive(EPriorityUserInput),
1.60 + iTestCaseIndex(0), iRepeat(0), iNumRepeats(KDefaultNumRepeats)
1.61 + {
1.62 + }
1.63 +
1.64 +
1.65 +
1.66 +CTestEngine::~CTestEngine()
1.67 + {
1.68 + // Cancel reading user console input
1.69 + Cancel();
1.70 +
1.71 + // Destroy the test case controller
1.72 + delete iTestCaseController;
1.73 +
1.74 + // Destroy the identity array and its contents
1.75 + iTestCasesIdentities.ResetAndDestroy();
1.76 +
1.77 + // Finish test and release resources
1.78 + gtest.End();
1.79 + gtest.Close();
1.80 + }
1.81 +
1.82 +
1.83 +
1.84 +void CTestEngine::ConstructL()
1.85 + {
1.86 + LOG_FUNC
1.87 + CActiveScheduler::Add(this);
1.88 +
1.89 + // Display information (construction text and OS build version number
1.90 + gtest.Title();
1.91 + gtest.Start(_L("Test Engine Initiation"));
1.92 + gtest.Printf(_L(">>\n"));
1.93 + gtest.Printf(_L(">> T E S T R U N \n"));
1.94 + gtest.Printf(_L(">>\n"));
1.95 +
1.96 + // Process the command line option for role
1.97 + TInt cmdLineLength(User::CommandLineLength());
1.98 + HBufC* cmdLine = HBufC::NewMax(cmdLineLength);
1.99 + CleanupStack::PushL(cmdLine);
1.100 + TPtr cmdLinePtr = cmdLine->Des();
1.101 + User::CommandLine(cmdLinePtr);
1.102 +
1.103 + // be careful, command line length is limited(248 characters)
1.104 + gtest.Printf(_L("***cmdLine = %lS\n"), cmdLine);
1.105 +
1.106 + TLex args(*cmdLine);
1.107 + args.SkipSpace();
1.108 +
1.109 + // Obtain the role of this test module
1.110 + TPtrC roleToken = args.NextToken(); // e.g. -role=host
1.111 + TBool hostFlag(ETrue);
1.112 +
1.113 + TInt pos(roleToken.FindF(KArgRole));
1.114 + if(pos != KErrNotFound)
1.115 + {
1.116 + pos = roleToken.FindF(_L("="));
1.117 + TPtrC role = roleToken.Right(roleToken.Length()-pos-1);
1.118 + if(role.Compare(KArgRoleHost) == 0)
1.119 + {
1.120 + hostFlag = ETrue;
1.121 + }
1.122 + else if(role.Compare(KArgRoleClient) == 0)
1.123 + {
1.124 + hostFlag = EFalse;
1.125 + }
1.126 + else
1.127 + {
1.128 + gtest.Printf(_L("Test configuration: could not find option -role\n"));
1.129 + gtest(EFalse);
1.130 + }
1.131 + }
1.132 + else
1.133 + {
1.134 + gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgRole);
1.135 + gtest(EFalse);
1.136 + }
1.137 +
1.138 + // Obtain the test cases to be run
1.139 + TPtrC casesToken = args.NextToken();
1.140 +
1.141 + pos = casesToken.FindF(KArgTestCases);
1.142 + if(pos != KErrNotFound)
1.143 + {
1.144 + pos = casesToken.FindF(_L("="));
1.145 + TPtrC testCases = casesToken.Right(casesToken.Length()-pos-1);
1.146 +
1.147 + // Remaining test cases
1.148 + TPtrC remCases(testCases);
1.149 + while(pos != KErrNotFound)
1.150 + {
1.151 + pos = remCases.FindF(_L(","));
1.152 + HBufC* tc = HBufC::NewLC(KTestCaseIdLength);
1.153 + TPtr tcPtr = tc->Des();
1.154 + tcPtr.Append(KTestStringPreamble);
1.155 +
1.156 + if(pos == KErrNotFound)
1.157 + {
1.158 + // This is the last test case identity
1.159 + tcPtr.Append(remCases);
1.160 + }
1.161 + else
1.162 + {
1.163 + tcPtr.Append(remCases.Left(KTestIdSize));
1.164 + }
1.165 +
1.166 + gtest.Printf(_L("Test case specified: %S\n"),tc);
1.167 +
1.168 +
1.169 + iTestCasesIdentities.Append(tc);
1.170 + CleanupStack::Pop(tc);
1.171 + remCases.Set(testCases.Right(remCases.Length()-pos-1));
1.172 + }
1.173 + }
1.174 + else
1.175 + {
1.176 + gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestCases);
1.177 + gtest(EFalse);
1.178 + }
1.179 +
1.180 + // Obtain the role of this test module
1.181 + TPtrC repeatsToken = args.NextToken(); // e.g. -repeats=4
1.182 +
1.183 + pos = repeatsToken.FindF(KArgTestRepeats);
1.184 + if(pos != KErrNotFound)
1.185 + {
1.186 + pos = repeatsToken.FindF(_L("="));
1.187 + TPtrC repeats = repeatsToken.Right(repeatsToken.Length()-pos-1);
1.188 + TLex lex(repeats);
1.189 + TInt ret = lex.Val(iNumRepeats, EDecimal);
1.190 + if(ret)
1.191 + {
1.192 + gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestRepeats);
1.193 + gtest.Printf(_L("DEFAULT to number of repeats = %d\n"),KDefaultNumRepeats);
1.194 + iNumRepeats = KDefaultNumRepeats;
1.195 + }
1.196 + gtest.Printf(_L("Test repeats specified: %d cycles\n"),iNumRepeats);
1.197 + }
1.198 + else
1.199 + {
1.200 + gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestRepeats);
1.201 + gtest.Printf(_L("DEFAULT to number of repeats = %d\n"),KDefaultNumRepeats);
1.202 + iNumRepeats = KDefaultNumRepeats;
1.203 + }
1.204 +
1.205 + // Create the test case controller
1.206 + gtest.Printf(_L("Creating the test controller\n"));
1.207 + iTestCaseController = CTestCaseController::NewL(*this,hostFlag);
1.208 +
1.209 + CleanupStack::PopAndDestroy(cmdLine);
1.210 +
1.211 + gtest.Console()->Read(iStatus);
1.212 + SetActive();
1.213 + }
1.214 +
1.215 +
1.216 +TInt CTestEngine::NextTestCaseId(TDes& aTestCaseId)
1.217 + {
1.218 + LOG_FUNC
1.219 + if(iTestCaseIndex < iTestCasesIdentities.Count())
1.220 + {
1.221 + aTestCaseId = *iTestCasesIdentities[iTestCaseIndex++];
1.222 + if(iTestCaseIndex==iTestCasesIdentities.Count())
1.223 + {
1.224 + iRepeat++;
1.225 + if(iRepeat < iNumRepeats)
1.226 + {
1.227 + iTestCaseIndex = 0; //prepare to start again
1.228 + }
1.229 + }
1.230 + return KErrNone;
1.231 + }
1.232 + else
1.233 + {
1.234 + return KErrNotFound;
1.235 + }
1.236 + }
1.237 +
1.238 +RPointerArray<HBufC>& CTestEngine::TestCasesIdentities()
1.239 + {
1.240 + return iTestCasesIdentities;
1.241 + }
1.242 +
1.243 +TUint CTestEngine::NumRepeats()
1.244 + {
1.245 + return iNumRepeats;
1.246 + }
1.247 +
1.248 +void CTestEngine::DoCancel()
1.249 + {
1.250 + LOG_FUNC
1.251 + gtest.Console()->ReadCancel();
1.252 + }
1.253 +
1.254 +
1.255 +void CTestEngine::RunL()
1.256 + {
1.257 + LOG_FUNC
1.258 + TInt completionCode(iStatus.Int());
1.259 +
1.260 + if(completionCode == KErrNone)
1.261 + {
1.262 + // Possibility of displaying a range of key commands
1.263 + // then gtest.Console()->Getch()
1.264 +
1.265 + TKeyCode keyCode(gtest.Console()->KeyCode());
1.266 + if(keyCode == EKeySpace)
1.267 + {
1.268 + iTestCaseController->Cancel();
1.269 + gtest.Printf(_L("Test module terminating\n"));
1.270 + RDebug::Printf("CActiveScheduler::Stop CTestEngine::RunL");
1.271 + CActiveScheduler::Stop();
1.272 + }
1.273 + else
1.274 + {
1.275 + gtest.Printf(_L("%d key pressed"),keyCode);
1.276 + }
1.277 + }
1.278 + else
1.279 + {
1.280 + gtest.Printf(_L("Manual key error %d\n"),completionCode);
1.281 + SetActive();
1.282 + }
1.283 + }
1.284 +
1.285 +
1.286 +TInt CTestEngine::RunError(TInt aError)
1.287 + {
1.288 + return KErrNone;
1.289 + }
1.290 +
1.291 + }