Update contrib.
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.
19 #include <e32std_private.h>
20 #include <u32std.h> // unicode builds
22 #include <e32base_private.h>
24 #include <e32Test.h> // RTest headder
26 #include <e32def_private.h>
27 #include "debugmacros.h"
28 #include "TestEngine.h"
29 #include "TestCaseController.h"
30 #include "TestCaseFactory.h"
31 #include "TestCaseRoot.h"
33 // Console application parameter options
34 _LIT(KArgAllTestCases,"/ALL"); // see default test-list below
35 _LIT(KArgGoTestCase, "/G:");
36 _LIT(KArgAutomatedTest, "/AUTO"); // removes "press any key to continue" prompts
37 _LIT(KArgVerboseOutput, "/VERBOSE"); // also turns on RDebug logging of all test output (to serial)
38 _LIT(KArgSetOpenIterations, "/LOOPO:"); // Open/Close test loop count
39 _LIT(KArgSetOOMIterations, "/LOOPM:"); // OOM test set #allocs
40 _LIT(KArgSetRoleMaster, "/MASTER"); // this is the default
41 _LIT(KArgSetRoleSlave, "/SLAVE"); // slave - Runs a dual-role test's Slave steps instead of Master
42 _LIT(KArgOverrideVidPid, "/PID:"); // vendor, product ID XXXX 4 hex digits /PID:0670
44 _LIT(KidFormatter,"PBASE-USB_OTGDI-%04d");
45 _LIT(KidFormatterS,"PBASE-USB_OTGDI-%S");
47 // '/ALL' tests grouping
48 const TInt KAllDefaultTestIDs[6] =
50 456, // (a) PBASE-USB_OTG-0456 open/close 'A'
51 457, // (a) PBASE-USB_OTG-0457 open/close disconnected
52 459, // (m) PBASE-USB_OTG-0459 detect 'A'
53 460, // (m) PBASE-USB_OTG-0460 detect 'A' removal
54 464, // (m) PBASE-USB_OTG-0464 raise
55 465 // (m) PBASE-USB_OTG-0465 lower
61 // CTestEngine implementation
63 CTestEngine* CTestEngine::NewL()
65 CTestEngine* self = new (ELeave) CTestEngine;
66 CleanupStack::PushL(self);
68 CleanupStack::Pop(self);
73 CTestEngine::CTestEngine():
75 iHelpRequested(EFalse)
81 CTestEngine::~CTestEngine()
84 // Destroy the test case controller
85 if (iTestCaseController)
87 delete iTestCaseController;
89 // Destroy the test identity array and its contents
90 iTestCasesIdentities.ResetAndDestroy();
95 void CTestEngine::ConstructL()
98 TInt menuSelection(0);
100 // Display information
102 test.Start(_L("Test Engine Initiation v2.00 "));
103 test.Printf(_L(">>\n"));
104 test.Printf(_L(">> T E S T R U N \n"));
105 test.Printf(_L(">>\n"));
108 // Process the command line parameters for batch/etc
109 TRAPD(err, ProcessCommandLineL());
112 User::Panic(_L("Test F/W Err"), KErrNoMemory);
118 User::Leave(-2); // nothing to do!
121 // if no command-line, we use a menu UI
122 if (!iTestCasesIdentities.Count())
124 RPointerArray<HBufC> testCaseNames;
125 // no tests added, select ONE to run from the menu
127 // list test cases (PRINT MENU) - in numeric order
128 RTestFactory::ListRegisteredTestCases(testCaseNames);
130 iTestCaseIndex = 0; // be sure we go back to beginning of the collection!
131 iTestCasesIdentities.ResetAndDestroy();
133 test.Printf(_L("Please select 0 to %d\n"), RTestFactory::TestCaseCount()-1);
134 test.Printf(_L("or 99<ENTER> to exit\n"));
135 GetNumericInput(menuSelection);
136 if ((menuSelection >=0) &&(menuSelection < RTestFactory::TestCaseCount()))
138 // add it to the list,and we can go
139 TBuf<KTestCaseIdLength> aSelectionID;
140 HBufC* tc = HBufC::NewLC(KTestCaseIdLength);
142 // get name from index
144 *tc = *testCaseNames[menuSelection];
146 iTestCasesIdentities.Append(tc);
147 CleanupStack::Pop(tc);
149 testCaseNames.ResetAndDestroy();
152 if ((menuSelection < RTestFactory::TestCaseCount()) && (menuSelection>=0))
154 // Create the test case controller
155 test.Printf(_L("Creating the test controller\n"));
156 iTestCaseController = CTestCaseController::NewL(*this, ETrue);
158 // Test-engine is non CActive class
162 // nothing to do, exit. USER aborted
163 test.Printf(_L("Test run stopped by user, nothing to do.\n"));
169 /* Displayed if used supplied no parameters, garbage, or a ? in the parameters
171 void CTestEngine::PrintUsage()
173 test.Printf(_L("OTGDI Unit Test Suite.\n"));
174 test.Printf(_L("Usage : t_otgdi.exe [/option] /G:<TESTNUM1>\n"));
175 test.Printf(_L(" /ALL = add default test subset to List\n"));
176 test.Printf(_L(" /G:<TESTNUM> where <testname> is the test# to add \n"));
177 test.Printf(_L(" /AUTO = largely unattended operation\n"));
178 test.Printf(_L(" /VERBOSE = test debugging info\n"));
179 test.Printf(_L(" /LOOPO:<n> = Open/close repeat counter<n>\n"));
180 test.Printf(_L(" /LOOPM:<n> = OOM HEAP_ALLOCS counter<n>\n"));
181 test.Printf(_L(" /SLAVE = Test-peer server mode\n"));
182 test.Printf(_L(" /PID:<n> = USB VID/PID in hex eg 2670\n"));
183 test.Printf(_L("Valid test ID range 0456...0469\n"));
184 test.Printf(_L("and 0675...0684 .\n"));
185 test.Printf(_L("\n"));
188 /** process the command-line, ; arguments appear in any order
189 IN : User::CommandLine()
190 OUT : iTestCasesIdentities
199 void CTestEngine::ProcessCommandLineL()
201 // example t_otgdi.exe /ALL /G:0468 /VERBOSE
202 TInt cmdLineLength(User::CommandLineLength());
203 HBufC* cmdLine = HBufC::NewMaxLC(cmdLineLength);
204 TPtr cmdLinePtr = cmdLine->Des();
205 User::CommandLine(cmdLinePtr);
206 TBool tokenParsed(EFalse);
209 args.SkipSpace(); // args are separated by spaces
211 // first arg is the exe name, skip it
212 TPtrC cmdToken = args.NextToken();
213 HBufC* tc = HBufC::NewLC(KParameterTextLenMax);
217 tokenParsed = EFalse;
219 // '/?' help wanted flag '?' or /? parameter
221 if ((0== tc->FindF(_L("?"))) || (0==tc->FindF(_L("/?"))))
223 iHelpRequested = ETrue;
228 pos = tc->FindF(KArgAllTestCases);
229 if (pos != KErrNotFound)
231 AddAllDefaultTests();
236 pos = tc->FindF(KArgAutomatedTest);
237 if (pos != KErrNotFound)
239 // skip some of the press-any key things
240 test.Printf(_L("Test semi-automated mode.\n"));
241 gSemiAutomated = ETrue;
246 pos = tc->FindF(KArgGoTestCase);
247 if (pos != KErrNotFound)
249 HBufC* tcPart = HBufC::NewLC(KTestCaseIdLength);
250 TPtrC testID = tc->Right(tc->Length() - pos - KArgGoTestCase().Length());
252 LOG_VERBOSE2(_L("Parameter found:'%S'\n"), &testID);
254 // Check if it is a test we know of in our suite, users may provide the full
255 // name "PBASE-USB_OTGDI-0466", or just the last 4 digits "0466", in such cases, fetch the full name
256 if (!RTestFactory::TestCaseExists(testID))
257 { // try use just the test#part
258 TPtr tcDes = tcPart->Des();
260 // build and add the full name
261 tcDes.Format(KidFormatterS, &testID);
262 if (!RTestFactory::TestCaseExists(tcDes))
265 test.Printf(_L("Test case does NOT Exist: '%lS'\n"), &testID);
268 { // only the number was supplied, copy the full name
272 // check that it's valid before adding it to the run-list
273 if (RTestFactory::TestCaseExists(testID))
275 HBufC* testIdentity = HBufC::NewLC(KTestCaseIdLength);
276 *testIdentity = testID;
277 test.Printf(_L("Test case specified: %lS\n"), testIdentity);
279 iTestCasesIdentities.Append(testIdentity);
280 CleanupStack::Pop(testIdentity);
282 CleanupStack::PopAndDestroy(tcPart);
287 pos = tc->FindF(KArgVerboseOutput);
288 if (pos != KErrNotFound)
290 gVerboseOutput = ETrue;
293 // turn on logging of test Printf() output to serial debug/log at the same time
294 test.SetLogged(ETrue);
298 // '/LOOPO:n' option (Set #times to run open/close tests amongst others)
299 pos = tc->FindF(KArgSetOpenIterations);
300 if (pos != KErrNotFound)
302 TPtrC iterationStr = tc->Right(tc->Length() - pos - KArgSetOpenIterations().Length());
303 TLex lex(iterationStr);
304 lex.Val(gOpenIterations);
305 MINMAX_CLAMPVALUE(gOpenIterations, OPEN_MINREPEATS, OPEN_MAXREPEATS);
309 // '/LOOPM:n' option (Set # of allocs to start at for OOM test)
310 pos = tc->FindF(KArgSetOOMIterations);
311 if (pos != KErrNotFound)
313 TPtrC iterationStr = tc->Right(tc->Length() - pos - KArgSetOOMIterations().Length());
314 TLex lex(iterationStr);
315 lex.Val(gOOMIterations);
316 MINMAX_CLAMPVALUE(gOOMIterations, OOM_MINREPEATS, OOM_MAXREPEATS);
321 // '/VID:nnnn' option (Set Symbian or other VID-Pid example /VID:0670)
322 pos = tc->FindF(KArgOverrideVidPid);
323 if (pos != KErrNotFound)
325 TPtrC vidpidStr = tc->Right(tc->Length() - pos - KArgOverrideVidPid().Length());
329 if (KErrNone == lex.Val(prodID, EHex))
334 LOG_VERBOSE2(_L(" accept param %04X \n\n"), prodID);
335 gUSBVidPid = prodID; // replace the vid-pid with the user-supplied one
340 test.Printf(_L("Warning: VID+PID '%lS' not parsed .\n"), tc);
345 pos = tc->FindF(KArgSetRoleSlave);
346 if (pos != KErrNotFound)
348 gTestRoleMaster = EFalse;
351 // '/MASTER' - default role
352 pos = tc->FindF(KArgSetRoleMaster); // note that master is the default role, so this parameter is optional
353 if (pos != KErrNotFound)
355 gTestRoleMaster = ETrue;
361 // warn about unparsed parameter
362 test.Printf(_L("Warning: '%lS'??? not parsed\n"), tc);
363 iHelpRequested = ETrue;
367 *tc = args.NextToken();
369 CleanupStack::PopAndDestroy(tc);
370 CleanupStack::PopAndDestroy(cmdLine);
374 /** Add all default tests to the front of the test-list so we run them all in sequence
376 void CTestEngine::AddAllDefaultTests()
378 test.Printf(_L("Adding default set test cases\n"));
381 while (index < sizeof(KAllDefaultTestIDs)/sizeof(KAllDefaultTestIDs[0]))
383 // allocate heap string
385 TRAPD(err, tc = HBufC::NewL(KTestCaseIdLength))
388 User::Panic(_L("Test F/W Err"), KErrNoMemory);
390 TPtr tcDes = tc->Des();
393 tcDes.Format(KidFormatter, KAllDefaultTestIDs[index]);
394 iTestCasesIdentities.Append(tc);
400 /* Return subsequent test case IDs from the test run-list KerrNotFound = end of list.
402 TInt CTestEngine::NextTestCaseId(TDes& aTestCaseId)
404 if (iTestCaseIndex < iTestCasesIdentities.Count())
406 aTestCaseId = *iTestCasesIdentities[iTestCaseIndex++];
415 /////////////////////////////////////////////////////////////////////////////
419 void CTestEngine::GetNumericInput(TInt &aNumber)
423 TKeyCode key = (TKeyCode) 0;
426 while ( key != EKeyEnter )
430 if ( ( key >= '0' ) && ( key <= '9' ) )
432 test.Printf(_L("%c"),key);
434 value = ( 10 * value ) + ( key - '0' );
437 { // very basic keyboard processing, backspace
438 if (key == EKeyBackspace)
442 test.Printf(_L("\r \r%d"), value);
451 test.Printf(_L("\n"));
455 /** Print a report at the end of a test run of all PASSED tests, Note: If a
456 test fails, the framework gets Panic'd */
457 void CTestEngine::Report()
459 TBuf<KTestCaseIdLength> aTestCaseId;
460 test.Printf(_L("============================\n"));
461 test.Printf(_L("PASSED TESTS:\n"));
462 // itterate our list of tests to perform
463 ResetTestCaseIndex();
464 while (KErrNone == NextTestCaseId(aTestCaseId))
466 test.Printf(_L("%S\n"), &aTestCaseId);
471 void CTestEngine::DoCancel()
474 test.Console()->ReadCancel();
478 TInt CTestEngine::RunError(TInt aError)