Update contrib.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "t_testsetup.h"
20 #include "t_testactionspec.h"
22 #include "t_certstoreactionmemfail.h"
24 #include "t_message.h"
25 #include "tScriptSetup.h"
26 #include "Thardcodedsetup.h"
27 #include "t_testhandler.h"
29 #include "tTestSpec.h"
30 #include "ttesthandlersettings.h"
31 #include "testexecuteinterface.h"
33 #include "t_testrunner.h"
34 #include "t_dummyconsole.h"
36 EXPORT_C HBufC* CTestSetup::GetArgument(TInt nPos)
39 HBufC *argv = HBufC::NewLC(User::CommandLineLength());
40 TPtr cmd(argv->Des());
41 User::CommandLine(cmd);
47 // finds nth parameter that doesnt have a -
48 while(nPos >= 0 && !arguments.Eos())
50 token.Set(arguments.NextToken());
51 if(token.Length() > 0 && token[0] != '-')
55 HBufC* result = token.AllocL();
57 CleanupStack::PopAndDestroy(argv);
62 void CTestSetup::InitFileserverSessionLC(RFs& aFs)
64 User::LeaveIfError(aFs.Connect());
65 CleanupClosePushL(aFs);
67 // enable tests to pass file handles between processes
70 // Create the private directory, to fix failures in tests that write to
71 // relative paths that were broken when the default path was changed to the
72 // private path in build 03429
73 TFileName privatePath;
74 User::LeaveIfError(aFs.PrivatePath(privatePath));
75 TInt err = aFs.MkDir(privatePath);
76 if (err != KErrNone && err != KErrAlreadyExists)
82 EXPORT_C void CTestSetup::CreateAndRunTestsL(TScriptTests theTestTypes[],
83 const TDesC& aScript, const TDesC& aLogFile, TBool aUseCommandLine,
84 CConsoleBase* aConsole, TBool* aResult)
86 LOG(_L("-- Test handler starting"));
90 InitFileserverSessionLC(fs);
92 CScriptSetup* testSetup = CScriptSetup::NewLC(aConsole);
93 CTestHandlerSettings* commandLineArgs = CTestHandlerSettings::NewLC();
94 CTestSpec* testSpec = CTestSpec::NewL();
95 CleanupStack::PushL(testSpec);
98 if(testSetup->InitialiseL(fs, aScript, aLogFile, aUseCommandLine))
101 // Store the state of the heap and RFs resource count before the tests
103 TInt initAllocCount = User::CountAllocCells();
104 TInt initRFsCount = fs.ResourceCount();
106 // Store the initial count of process and thread handles
107 TInt initThreadHandleCount;
108 TInt initProcessHandleCount;
109 RThread().HandleCount(initProcessHandleCount, initThreadHandleCount);
111 testSetup->LogFile().write(_L("<html><body><pre>\r\n"));
112 testSetup->LogFile().write(_L("State of the system before the tests:\n"));
113 testSetup->LogFile().write(_L("\tRFs resource count: %d\n"), initRFsCount);
114 testSetup->LogFile().write(_L("\tProcess handle count: %d\n"), initProcessHandleCount);
115 testSetup->LogFile().write(_L("\tThread handle count: %d\n"), initThreadHandleCount);
117 TRAPD(error, testSetup->SetupTestsL(fs, *testSpec, theTestTypes, *commandLineArgs))
121 CTestHandler* handler = CTestHandler::NewLC(fs, *testSpec, *commandLineArgs,
122 &testSetup->Console(),
123 &testSetup->LogFile());
124 handler->RunTestsL();
125 summary = handler->Summary();
126 testSpec->FreeAllTests();
127 CleanupStack::PopAndDestroy(handler);
129 // Do heap, RFs resource, process and thread handle balance checks
131 TInt finalRFsCount = fs.ResourceCount();
133 testSetup->LogFile().write(_L("State of the system after the tests:\n"));
134 testSetup->LogFile().write(_L("\tRFs resource count: %d\n"), finalRFsCount);
136 if (initRFsCount == finalRFsCount)
138 testSetup->LogFile().write(_L("\nRFs resource count ok: %d final\n\n"),
143 testSetup->LogFile().write(_L("\nRFs resource count inbalance: %d final\n\n"),
145 ++summary.iTestsFailed;
149 TInt finalAllocCount = User::CountAllocCells();
150 if (initAllocCount == finalAllocCount)
152 testSetup->LogFile().write(_L("\nHeap alloc count ok: %d final vs %d initial\n\n"),
153 finalAllocCount, initAllocCount);
157 testSetup->LogFile().write(_L("\nHeap alloc count inbalance: %d final vs %d initial\n\n"),
158 finalAllocCount, initAllocCount);
159 ++summary.iTestsFailed;
162 TInt finalThreadHandleCount;
163 TInt finalProcessHandleCount;
164 RThread().HandleCount(finalProcessHandleCount, finalThreadHandleCount);
166 if (initProcessHandleCount == finalProcessHandleCount)
168 testSetup->LogFile().write(_L("\nProcess handle count ok: %d final vs %d initial\n\n"),
169 finalProcessHandleCount, initProcessHandleCount);
173 testSetup->LogFile().write(_L("\nProcess handle count imbalance: %d final vs %d initial\n\n"),
174 finalProcessHandleCount, initProcessHandleCount);
175 ++summary.iTestsFailed;
178 if (initThreadHandleCount == finalThreadHandleCount)
180 testSetup->LogFile().write(_L("\nThread handle count ok: %d final vs %d initial\n\n"),
181 finalThreadHandleCount, initThreadHandleCount);
185 testSetup->LogFile().write(_L("\nThread handle count imbalance: %d final vs %d initial\n\n"),
186 finalThreadHandleCount, initThreadHandleCount);
187 ++summary.iTestsFailed;
192 // Set the result if required by caller
195 *aResult = summary.AllTestsPassed();
198 summary.PrintL(testSetup->LogFile());
200 // pauses runtime if command line requests it i.e. -w
201 if(commandLineArgs->iWaitForKeyPressAtEnd)
203 testSetup->Console().Printf(_L("\nPress a key to quit"));
204 testSetup->Console().Getch();
209 CleanupStack::PopAndDestroy(4, &fs); // fs, testsetup, commandLineArgs and testspec
210 // this MUST be the last thing to do incase objects being destructed on cleanup
213 // RDebug::RawPrint(_L("RTEST: SUCCESS : testhandler\n"));
216 EXPORT_C void CTestSetup::CreateAndRunTestsL(THardcodedTests theTestTypes[], const TDesC& aDefaultLog)
218 LOG(_L("-- Test handler starting"));
222 InitFileserverSessionLC(fs);
224 InitFileserverSessionLC(fs);
226 CHardcodedSetup* testSetup = CHardcodedSetup::NewLC();
227 CTestHandlerSettings* commandLineArgs = CTestHandlerSettings::NewLC();
228 CTestSpec* testSpec = CTestSpec::NewL();
229 CleanupStack::PushL(testSpec);
231 TTestSummary summary;
232 if(testSetup->InitialiseL(fs, aDefaultLog))
235 // Store the state of the heap and RFs resource count before the tests
237 TInt initAllocCount = User::CountAllocCells();
238 TInt initRFsCount = fs.ResourceCount();
240 TRAPD(error, testSetup->SetupTestsL(fs, *testSpec, theTestTypes, *commandLineArgs));
244 CTestHandler* handler = CTestHandler::NewLC(fs, *testSpec, *commandLineArgs,
245 &testSetup->Console(),
246 &testSetup->LogFile());
247 handler->RunTestsL();
248 summary = handler->Summary();
249 testSpec->FreeAllTests();
250 CleanupStack::PopAndDestroy(handler);
252 // Do heap and RFs resource balance checks
254 TInt finalRFsCount = fs.ResourceCount();
256 testSetup->LogFile().write(_L("State of the system after the tests:\n"));
257 testSetup->LogFile().write(_L("\tRFs resource count: %d\n"), finalRFsCount);
259 if (initRFsCount == finalRFsCount)
261 testSetup->LogFile().write(_L("\nRFs resource count ok: %d final\n\n"),
266 testSetup->LogFile().write(_L("\nRFs resource count inbalance: %d final\n\n"),
268 ++summary.iTestsFailed;
271 TInt finalAllocCount = User::CountAllocCells();
272 if (initAllocCount == finalAllocCount)
274 testSetup->LogFile().write(_L("\nHeap alloc count ok: %d final vs %d initial\n\n"),
275 finalAllocCount, initAllocCount);
279 testSetup->LogFile().write(_L("\nHeap alloc count inbalance: %d final vs %d initial\n\n"),
280 finalAllocCount, initAllocCount);
281 ++summary.iTestsFailed;
286 summary.PrintL(testSetup->LogFile());
288 // pauses runtime if command line requests it i.e. -w
289 if(commandLineArgs->iWaitForKeyPressAtEnd)
291 testSetup->Console().Printf(_L("\nPress a key to quit"));
292 testSetup->Console().Getch();
296 CleanupStack::PopAndDestroy(4, &fs); // fs, testsetup, commandLineArgs and testspec
299 // RDebug::RawPrint(_L("RTEST: SUCCESS : testhandler\n"));
302 //Check all flags is provided for understanability - Each grouping function called calls the next function
303 // if it has succeeded
304 TBool CTestSetup::CheckAllFlags(const CTestHandlerSettings& aCommandLineSettings, TInt& aScriptGroupings)
306 return CheckExhaustiveandSmoke(aCommandLineSettings,aScriptGroupings);
309 TBool CTestSetup::CheckExhaustiveandSmoke(const CTestHandlerSettings& aCommandLineSettings, TInt& aScriptGroupings)
312 if (aCommandLineSettings.iExhaust || (aScriptGroupings & SMOKE))
315 return CheckSkipped(aCommandLineSettings, aScriptGroupings);
323 TBool CTestSetup::CheckSkipped(const CTestHandlerSettings& aCommandLineSettings,
324 TInt& aScriptGroupings)
326 if (aCommandLineSettings.iSkip || !(aScriptGroupings & SKIP))
329 return CheckInteractive(aCommandLineSettings, aScriptGroupings);
335 TBool CTestSetup::CheckInteractive(const CTestHandlerSettings& aCommandLineSettings,
336 TInt& aScriptGroupings)
338 if (aCommandLineSettings.iInt || !(aScriptGroupings & INTER))
341 return CheckOOMandCancel(aCommandLineSettings, aScriptGroupings);
347 TBool CTestSetup::CheckOOMandCancel(const CTestHandlerSettings& aCommandLineSettings,
348 TInt& aScriptGroupings)
351 if ((aScriptGroupings & (EXOOM | INOOM)) == (EXOOM | INOOM))
352 User::Panic(_L("Test is both Included and Excluded from OOM"), 1);
354 if ((aScriptGroupings & (EXCANCEL | INCANCEL)) == (EXCANCEL | INCANCEL))
355 User::Panic(_L("Test is both Included and Excluded from Cancel"), 1);
357 if ((aScriptGroupings & (INOOM | INCANCEL)) == (INOOM | INCANCEL))
358 User::Panic(_L("Test is in both OOM and Cancel groups"), 1);
360 // Check to see whether -o set
361 if (aCommandLineSettings.iOOM)
363 //Is the test in OOM group?
364 if (aScriptGroupings & INOOM)
367 //Is the test excluded from OOM?
368 if (aScriptGroupings & EXOOM)
372 if (aScriptGroupings & INOOM)
375 // Check to see whether -c set
376 if (aCommandLineSettings.iCancel)
378 //Is the test in CANCEL group?
379 if (aScriptGroupings & INCANCEL)
382 //Is the test excluded from CANCEL?
383 if (aScriptGroupings & EXCANCEL)
387 if (aScriptGroupings & INCANCEL)
395 EXPORT_C CTestSetup::~CTestSetup()
401 // Note that the heap count when this is deleted must be the same as
402 // when the console was allocated, otherwise a panic will occur. This
403 // is due to the techview implementation of the console doing a heap
404 // mark in its destructor.
409 CTestSetup::CTestSetup(CConsoleBase* aConsole)
410 : iConsole(aConsole), iConsoleOwned(aConsole == NULL)
414 void CTestSetup::ConstructL()
418 iConsole = Console::NewL(_L("Test code"), TSize(KDefaultConsWidth, KDefaultConsHeight));
421 // Currently the console passed to the test actions discards all output -
422 // this is an attempt to make hardware tests run faster. All information
423 // should be written to the log file anyway, and I'd like to remove use of a
424 // console as well as the log file. -- jc
425 iTestConsole = new (ELeave) CDummyConsole();
428 void CTestSetup::OpenLogFileL(RFs &aFs, TInt nPos, const TDesC &aLogFile, TBool aUseCommandline)
430 HBufC* logFileName = NULL;
434 logFileName = GetArgument(nPos);
435 CleanupStack::PushL(logFileName);
437 // check if logfile was specified on command line
438 if(logFileName->Length()==0)
440 // empty so remove it
441 CleanupStack::PopAndDestroy(logFileName);
446 if(logFileName == NULL)
448 if(aLogFile.Length()==0)
450 PRINTANDLOG(_L("No log file specified on command line and no default given"));
451 User::Leave(KErrArgument);
455 // there is a default copy that
456 logFileName = aLogFile.AllocL();
457 CleanupStack::PushL(logFileName);
461 PRINTANDLOG1(_L("Log file: %S"), logFileName);
465 // attempts to create directories incase they dont exist
466 aFs.MkDirAll(*logFileName); // ignore errors
468 // write over any existing log
469 User::LeaveIfError(logFile.Replace(aFs, *logFileName, EFileWrite));
471 // output goes only to the log file
472 iLogFile = new(ELeave) FileOutput(logFile);
473 CleanupStack::PopAndDestroy(logFileName);