sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // @file testengine.cpp sl@0: // @internalComponent sl@0: // sl@0: // sl@0: sl@0: #include "TestEngine.h" sl@0: #include "testdebug.h" sl@0: #include "TestCaseController.h" sl@0: #include "TestCaseFactory.h" sl@0: sl@0: // Console application options sl@0: sl@0: _LIT(KArgRole,"-role="); sl@0: _LIT(KArgTestCases,"-cases="); sl@0: _LIT(KArgTestRepeats,"-repeats="); sl@0: sl@0: // Role values sl@0: sl@0: _LIT(KArgRoleHost,"host"); sl@0: _LIT(KArgRoleClient,"client"); sl@0: sl@0: sl@0: extern RTest gtest; sl@0: sl@0: namespace NUnitTesting_USBDI sl@0: { sl@0: const TUint KDefaultNumRepeats = 1; sl@0: const TUint KTestIdSize = 4; sl@0: _LIT(KTestStringPreamble,"PBASE-T_USBDI-"); sl@0: sl@0: CTestEngine* CTestEngine::NewL() sl@0: { sl@0: CTestEngine* self = new (ELeave) CTestEngine; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: sl@0: CTestEngine::CTestEngine() sl@0: : CActive(EPriorityUserInput), sl@0: iTestCaseIndex(0), iRepeat(0), iNumRepeats(KDefaultNumRepeats) sl@0: { sl@0: } sl@0: sl@0: sl@0: sl@0: CTestEngine::~CTestEngine() sl@0: { sl@0: // Cancel reading user console input sl@0: Cancel(); sl@0: sl@0: // Destroy the test case controller sl@0: delete iTestCaseController; sl@0: sl@0: // Destroy the identity array and its contents sl@0: iTestCasesIdentities.ResetAndDestroy(); sl@0: sl@0: // Finish test and release resources sl@0: gtest.End(); sl@0: gtest.Close(); sl@0: } sl@0: sl@0: sl@0: sl@0: void CTestEngine::ConstructL() sl@0: { sl@0: LOG_FUNC sl@0: CActiveScheduler::Add(this); sl@0: sl@0: // Display information (construction text and OS build version number sl@0: gtest.Title(); sl@0: gtest.Start(_L("Test Engine Initiation")); sl@0: gtest.Printf(_L(">>\n")); sl@0: gtest.Printf(_L(">> T E S T R U N \n")); sl@0: gtest.Printf(_L(">>\n")); sl@0: sl@0: // Process the command line option for role sl@0: TInt cmdLineLength(User::CommandLineLength()); sl@0: HBufC* cmdLine = HBufC::NewMax(cmdLineLength); sl@0: CleanupStack::PushL(cmdLine); sl@0: TPtr cmdLinePtr = cmdLine->Des(); sl@0: User::CommandLine(cmdLinePtr); sl@0: sl@0: // be careful, command line length is limited(248 characters) sl@0: gtest.Printf(_L("***cmdLine = %lS\n"), cmdLine); sl@0: sl@0: TLex args(*cmdLine); sl@0: args.SkipSpace(); sl@0: sl@0: // Obtain the role of this test module sl@0: TPtrC roleToken = args.NextToken(); // e.g. -role=host sl@0: TBool hostFlag(ETrue); sl@0: sl@0: TInt pos(roleToken.FindF(KArgRole)); sl@0: if(pos != KErrNotFound) sl@0: { sl@0: pos = roleToken.FindF(_L("=")); sl@0: TPtrC role = roleToken.Right(roleToken.Length()-pos-1); sl@0: if(role.Compare(KArgRoleHost) == 0) sl@0: { sl@0: hostFlag = ETrue; sl@0: } sl@0: else if(role.Compare(KArgRoleClient) == 0) sl@0: { sl@0: hostFlag = EFalse; sl@0: } sl@0: else sl@0: { sl@0: gtest.Printf(_L("Test configuration: could not find option -role\n")); sl@0: gtest(EFalse); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgRole); sl@0: gtest(EFalse); sl@0: } sl@0: sl@0: // Obtain the test cases to be run sl@0: TPtrC casesToken = args.NextToken(); sl@0: sl@0: pos = casesToken.FindF(KArgTestCases); sl@0: if(pos != KErrNotFound) sl@0: { sl@0: pos = casesToken.FindF(_L("=")); sl@0: TPtrC testCases = casesToken.Right(casesToken.Length()-pos-1); sl@0: sl@0: // Remaining test cases sl@0: TPtrC remCases(testCases); sl@0: while(pos != KErrNotFound) sl@0: { sl@0: pos = remCases.FindF(_L(",")); sl@0: HBufC* tc = HBufC::NewLC(KTestCaseIdLength); sl@0: TPtr tcPtr = tc->Des(); sl@0: tcPtr.Append(KTestStringPreamble); sl@0: sl@0: if(pos == KErrNotFound) sl@0: { sl@0: // This is the last test case identity sl@0: tcPtr.Append(remCases); sl@0: } sl@0: else sl@0: { sl@0: tcPtr.Append(remCases.Left(KTestIdSize)); sl@0: } sl@0: sl@0: gtest.Printf(_L("Test case specified: %S\n"),tc); sl@0: sl@0: sl@0: iTestCasesIdentities.Append(tc); sl@0: CleanupStack::Pop(tc); sl@0: remCases.Set(testCases.Right(remCases.Length()-pos-1)); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestCases); sl@0: gtest(EFalse); sl@0: } sl@0: sl@0: // Obtain the role of this test module sl@0: TPtrC repeatsToken = args.NextToken(); // e.g. -repeats=4 sl@0: sl@0: pos = repeatsToken.FindF(KArgTestRepeats); sl@0: if(pos != KErrNotFound) sl@0: { sl@0: pos = repeatsToken.FindF(_L("=")); sl@0: TPtrC repeats = repeatsToken.Right(repeatsToken.Length()-pos-1); sl@0: TLex lex(repeats); sl@0: TInt ret = lex.Val(iNumRepeats, EDecimal); sl@0: if(ret) sl@0: { sl@0: gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestRepeats); sl@0: gtest.Printf(_L("DEFAULT to number of repeats = %d\n"),KDefaultNumRepeats); sl@0: iNumRepeats = KDefaultNumRepeats; sl@0: } sl@0: gtest.Printf(_L("Test repeats specified: %d cycles\n"),iNumRepeats); sl@0: } sl@0: else sl@0: { sl@0: gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestRepeats); sl@0: gtest.Printf(_L("DEFAULT to number of repeats = %d\n"),KDefaultNumRepeats); sl@0: iNumRepeats = KDefaultNumRepeats; sl@0: } sl@0: sl@0: // Create the test case controller sl@0: gtest.Printf(_L("Creating the test controller\n")); sl@0: iTestCaseController = CTestCaseController::NewL(*this,hostFlag); sl@0: sl@0: CleanupStack::PopAndDestroy(cmdLine); sl@0: sl@0: gtest.Console()->Read(iStatus); sl@0: SetActive(); sl@0: } sl@0: sl@0: sl@0: TInt CTestEngine::NextTestCaseId(TDes& aTestCaseId) sl@0: { sl@0: LOG_FUNC sl@0: if(iTestCaseIndex < iTestCasesIdentities.Count()) sl@0: { sl@0: aTestCaseId = *iTestCasesIdentities[iTestCaseIndex++]; sl@0: if(iTestCaseIndex==iTestCasesIdentities.Count()) sl@0: { sl@0: iRepeat++; sl@0: if(iRepeat < iNumRepeats) sl@0: { sl@0: iTestCaseIndex = 0; //prepare to start again sl@0: } sl@0: } sl@0: return KErrNone; sl@0: } sl@0: else sl@0: { sl@0: return KErrNotFound; sl@0: } sl@0: } sl@0: sl@0: RPointerArray& CTestEngine::TestCasesIdentities() sl@0: { sl@0: return iTestCasesIdentities; sl@0: } sl@0: sl@0: TUint CTestEngine::NumRepeats() sl@0: { sl@0: return iNumRepeats; sl@0: } sl@0: sl@0: void CTestEngine::DoCancel() sl@0: { sl@0: LOG_FUNC sl@0: gtest.Console()->ReadCancel(); sl@0: } sl@0: sl@0: sl@0: void CTestEngine::RunL() sl@0: { sl@0: LOG_FUNC sl@0: TInt completionCode(iStatus.Int()); sl@0: sl@0: if(completionCode == KErrNone) sl@0: { sl@0: // Possibility of displaying a range of key commands sl@0: // then gtest.Console()->Getch() sl@0: sl@0: TKeyCode keyCode(gtest.Console()->KeyCode()); sl@0: if(keyCode == EKeySpace) sl@0: { sl@0: iTestCaseController->Cancel(); sl@0: gtest.Printf(_L("Test module terminating\n")); sl@0: RDebug::Printf("CActiveScheduler::Stop CTestEngine::RunL"); sl@0: CActiveScheduler::Stop(); sl@0: } sl@0: else sl@0: { sl@0: gtest.Printf(_L("%d key pressed"),keyCode); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: gtest.Printf(_L("Manual key error %d\n"),completionCode); sl@0: SetActive(); sl@0: } sl@0: } sl@0: sl@0: sl@0: TInt CTestEngine::RunError(TInt aError) sl@0: { sl@0: return KErrNone; sl@0: } sl@0: sl@0: }