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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // @file testengine.cpp
19 #include "TestEngine.h"
20 #include "testdebug.h"
21 #include "TestCaseController.h"
22 #include "TestCaseFactory.h"
24 // Console application options
26 _LIT(KArgRole,"-role=");
27 _LIT(KArgTestCases,"-cases=");
28 _LIT(KArgTestRepeats,"-repeats=");
32 _LIT(KArgRoleHost,"host");
33 _LIT(KArgRoleClient,"client");
38 namespace NUnitTesting_USBDI
40 const TUint KDefaultNumRepeats = 1;
41 const TUint KTestIdSize = 4;
42 _LIT(KTestStringPreamble,"PBASE-T_USBDI-");
44 CTestEngine* CTestEngine::NewL()
46 CTestEngine* self = new (ELeave) CTestEngine;
47 CleanupStack::PushL(self);
49 CleanupStack::Pop(self);
55 CTestEngine::CTestEngine()
56 : CActive(EPriorityUserInput),
57 iTestCaseIndex(0), iRepeat(0), iNumRepeats(KDefaultNumRepeats)
63 CTestEngine::~CTestEngine()
65 // Cancel reading user console input
68 // Destroy the test case controller
69 delete iTestCaseController;
71 // Destroy the identity array and its contents
72 iTestCasesIdentities.ResetAndDestroy();
74 // Finish test and release resources
81 void CTestEngine::ConstructL()
84 CActiveScheduler::Add(this);
86 // Display information (construction text and OS build version number
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"));
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);
100 // be careful, command line length is limited(248 characters)
101 gtest.Printf(_L("***cmdLine = %lS\n"), cmdLine);
106 // Obtain the role of this test module
107 TPtrC roleToken = args.NextToken(); // e.g. -role=host
108 TBool hostFlag(ETrue);
110 TInt pos(roleToken.FindF(KArgRole));
111 if(pos != KErrNotFound)
113 pos = roleToken.FindF(_L("="));
114 TPtrC role = roleToken.Right(roleToken.Length()-pos-1);
115 if(role.Compare(KArgRoleHost) == 0)
119 else if(role.Compare(KArgRoleClient) == 0)
125 gtest.Printf(_L("Test configuration: could not find option -role\n"));
131 gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgRole);
135 // Obtain the test cases to be run
136 TPtrC casesToken = args.NextToken();
138 pos = casesToken.FindF(KArgTestCases);
139 if(pos != KErrNotFound)
141 pos = casesToken.FindF(_L("="));
142 TPtrC testCases = casesToken.Right(casesToken.Length()-pos-1);
144 // Remaining test cases
145 TPtrC remCases(testCases);
146 while(pos != KErrNotFound)
148 pos = remCases.FindF(_L(","));
149 HBufC* tc = HBufC::NewLC(KTestCaseIdLength);
150 TPtr tcPtr = tc->Des();
151 tcPtr.Append(KTestStringPreamble);
153 if(pos == KErrNotFound)
155 // This is the last test case identity
156 tcPtr.Append(remCases);
160 tcPtr.Append(remCases.Left(KTestIdSize));
163 gtest.Printf(_L("Test case specified: %S\n"),tc);
166 iTestCasesIdentities.Append(tc);
167 CleanupStack::Pop(tc);
168 remCases.Set(testCases.Right(remCases.Length()-pos-1));
173 gtest.Printf(_L("Test configuration option not found: %S\n"),&KArgTestCases);
177 // Obtain the role of this test module
178 TPtrC repeatsToken = args.NextToken(); // e.g. -repeats=4
180 pos = repeatsToken.FindF(KArgTestRepeats);
181 if(pos != KErrNotFound)
183 pos = repeatsToken.FindF(_L("="));
184 TPtrC repeats = repeatsToken.Right(repeatsToken.Length()-pos-1);
186 TInt ret = lex.Val(iNumRepeats, EDecimal);
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;
193 gtest.Printf(_L("Test repeats specified: %d cycles\n"),iNumRepeats);
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;
202 // Create the test case controller
203 gtest.Printf(_L("Creating the test controller\n"));
204 iTestCaseController = CTestCaseController::NewL(*this,hostFlag);
206 CleanupStack::PopAndDestroy(cmdLine);
208 gtest.Console()->Read(iStatus);
213 TInt CTestEngine::NextTestCaseId(TDes& aTestCaseId)
216 if(iTestCaseIndex < iTestCasesIdentities.Count())
218 aTestCaseId = *iTestCasesIdentities[iTestCaseIndex++];
219 if(iTestCaseIndex==iTestCasesIdentities.Count())
222 if(iRepeat < iNumRepeats)
224 iTestCaseIndex = 0; //prepare to start again
235 RPointerArray<HBufC>& CTestEngine::TestCasesIdentities()
237 return iTestCasesIdentities;
240 TUint CTestEngine::NumRepeats()
245 void CTestEngine::DoCancel()
248 gtest.Console()->ReadCancel();
252 void CTestEngine::RunL()
255 TInt completionCode(iStatus.Int());
257 if(completionCode == KErrNone)
259 // Possibility of displaying a range of key commands
260 // then gtest.Console()->Getch()
262 TKeyCode keyCode(gtest.Console()->KeyCode());
263 if(keyCode == EKeySpace)
265 iTestCaseController->Cancel();
266 gtest.Printf(_L("Test module terminating\n"));
267 RDebug::Printf("CActiveScheduler::Stop CTestEngine::RunL");
268 CActiveScheduler::Stop();
272 gtest.Printf(_L("%d key pressed"),keyCode);
277 gtest.Printf(_L("Manual key error %d\n"),completionCode);
283 TInt CTestEngine::RunError(TInt aError)