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_testhandler.h"
20 #include "ttesthandlersettings.h"
22 #include "t_testaction.h"
23 #include "t_testsetup.h"
24 #include "tTestSpec.h"
25 #include "t_testrunner.h"
28 TTestSummary::TTestSummary() :
29 iTestsRun(0), iTestsFailed(0)
33 void TTestSummary::PrintL(Output& aOut)
35 aOut.write(_L("%d tests failed out of %d\n"), iTestsFailed, iTestsRun);
36 aOut.write(_L("\r\n</pre></body></html>\r\n"));
39 TBool TTestSummary::AllTestsPassed()
41 return iTestsRun > 0 && iTestsFailed == 0;
44 EXPORT_C CTestHandler* CTestHandler::NewLC(RFs& aFs,
46 CTestHandlerSettings& aSettings,
47 CConsoleBase* aConsole,
50 CTestHandler* self = new(ELeave) CTestHandler(aFs, aTestSpec, aConsole, aOut);
51 CleanupStack::PushL(self);
52 self->ConstructL(aSettings);
56 CTestHandler::~CTestHandler()
62 delete iNextTestRunner;
65 iFailedTestNames.ResetAndDestroy();
66 iKnownDefects.ResetAndDestroy();
69 CTestHandler::CTestHandler(RFs& aFs,
71 CConsoleBase* aConsole,
73 iFs(aFs), iConsole(aConsole), iOut(aOut), iTestSpec(aTestSpec)
77 void CTestHandler::ConstructL(CTestHandlerSettings& aSettings)
79 iSettings = CTestHandlerSettings::NewL(aSettings);
80 iScheduler = new(ELeave) CActiveScheduler;
81 CActiveScheduler::Install(iScheduler);
83 // Set up test handler based on command line argument
86 SetTestRunnerL(new (ELeave) COOMTestRunner(*iOut));
88 else if (aSettings.iCancel)
90 SetTestRunnerL(new (ELeave) CCancelTestRunner(*iOut));
98 TTestSummary CTestHandler::Summary()
101 result.iTestsRun = iActionCount;
102 result.iTestsFailed = iFailedTests.Count();
106 EXPORT_C void CTestHandler::RunTestsL()
111 time.UniversalTime();
112 TDateTime dateTime = time.DateTime();
113 _LIT8(KDateFormat,"%02d:%02d:%02d:%03d ");
114 timeBuf.AppendFormat(KDateFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),(dateTime.MicroSecond()/1000));
118 HBufC8* currentID=NULL;
119 HBufC8* previousID=NULL;
120 TBool testCaseResult=ETrue;
121 TBool isTefScript = EFalse;
122 while(iTestSpec.GetNextTest(action))
126 isTefScript = action->iTefScript;
127 // action->iTefScript = EFalse;
129 previousID = currentID;
130 currentID = action->iNameInfo->AllocLC();
132 if ( (previousID == NULL) || (*currentID != *previousID) )
134 if (previousID != NULL)
138 if(action->iTefScript)
140 iOut->writeString(timeBuf);
141 iOut->writeString(_L("Command = END_TESTCASE "));
143 iOut->writeString(*previousID);
144 if(action->iTefScript)
146 iOut->writeString(_L(" ***TestCaseResult = "));
151 iOut->writeString(_L("PASS"));
155 iOut->writeString(_L("FAIL"));
157 iOut->writeString(_L("\r\n"));
158 iOut->writeString(_L("\r\n"));
162 if(action->iTefScript)
164 iOut->writeString(timeBuf);
165 iOut->writeString(_L("Command = START_TESTCASE "));
167 iOut->writeString(*(currentID));
168 iOut->writeString(_L("\r\n"));
169 testCaseResult = ETrue;
172 TRAPD(err, RunTestL(action));
173 if (!action->iResult)
175 testCaseResult = EFalse;
179 FailTestL(action, EFailInTestHandler, err);
181 CleanupStack::Pop(currentID);
184 if (currentID != NULL)
190 iOut->writeString(timeBuf);
191 iOut->writeString(_L("Command = END_TESTCASE "));
193 iOut->writeString(*currentID);
196 iOut->writeString(_L(" ***TestCaseResult = "));
201 iOut->writeString(_L("PASS"));
205 iOut->writeString(_L("FAIL"));
207 iOut->writeString(_L("\r\n"));
208 iOut->writeString(_L("\r\n"));
218 void CTestHandler::RunTestL(CTestAction* aAction)
220 // If the last test set a new test runner, install it here
224 iTestRunner = iNextTestRunner;
225 iNextTestRunner = NULL;
228 iOut->writeString(_L("Test "));
229 iOut->writeNum(iActionNumber);
230 iOut->writeNewLine();
232 if (iActionNumber > 1)
234 iConsole->Printf(_L(", "));
236 iConsole->Printf(_L("%d: "), iActionNumber);
238 aAction->ReportAction();
239 aAction->SetTestHandler(*this);
241 TInt preErr = iTestRunner->PerformPrerequisiteL(aAction);
242 if ((preErr != KErrNone) && !aAction->iResult)
244 FailTestL(aAction, EFailInPrerequisite, preErr);
248 if (preErr == KErrNone)
250 actionErr = iTestRunner->PerformActionL(aAction);
252 TInt postErr = iTestRunner->PerformPostrequisiteL(aAction, actionErr);
253 // investiage if passing actionErr would make more sense here
254 aAction->CheckResult(postErr);
255 if (!aAction->iResult)
257 FailTestL(aAction, EFailInAction, actionErr);
261 iConsole->Printf(_L("passed"));
264 const TDesC& CTestHandler::GetFailLocationName(TTestFailLocation aLoc)
266 _LIT(KTestHandler, "test handler");
267 _LIT(KPrerequisite, "prerequisite");
268 _LIT(KTestAction, "test action");
270 const TDesC* result = NULL;
274 case EFailInTestHandler:
275 result = &KTestHandler;
277 case EFailInPrerequisite:
278 result = &KPrerequisite;
281 result = &KTestAction;
289 void CTestHandler::FailTestL(CTestAction* aAction, TTestFailLocation aLoc, TInt aErr)
291 iOut->writeString(_L("Test failed in "));
292 iOut->writeString(GetFailLocationName(aLoc));
293 iOut->writeString(_L(": "));
294 iOut->writeNum(aErr);
295 iOut->writeNewLine();
297 iConsole->Printf(_L("failed"));
299 AppendFailedTestL(iActionNumber, *aAction->iNameInfo);
300 if(aAction->ScriptError() == CTestAction::EFileNotFound)
302 TBuf<KMaxErrorSize> scriptError;
303 aAction->ScriptError(scriptError);
304 iOut->writeString(_L("File Not Found \""));
305 iOut->writeString(scriptError);
306 iOut->writeString(_L("\" in test "));
307 iOut->writeNum(iActionNumber);
308 iOut->writeNewLine();
311 if (aAction->iKnownFailure)
312 {// Expecting a failure because of known defect - record details
313 HBufC* name=HBufC::NewLC(aAction->iNameInfo->Length());
314 name->Des().Copy(*(aAction->iNameInfo));
315 User::LeaveIfError(iKnownDefects.Append(name));
316 CleanupStack::Pop(name);
320 void CTestHandler::AppendFailedTestL(TInt aIndex, const TDesC8& aName)
322 iFailedTests.Append(aIndex);
323 HBufC* name=HBufC::NewLC(aName.Length());
324 name->Des().Copy(aName);
325 User::LeaveIfError(iFailedTestNames.Append(name));
326 CleanupStack::Pop(name);
329 void CTestHandler::DisplaySummary()
331 //Display the RTest information
332 iConsole->Printf(_L("\n"));
334 // Log the details of tests that failed because of known (deferred) defects
335 TInt theFailCount = iKnownDefects.Count();
336 if (theFailCount > 0)
338 iOut->writeNewLine();
339 iOut->writeString(_L("----------------------------------------------------------------------------------"));
340 iOut->writeNewLine();
341 iOut->writeString(_L("These tests may not pass because of known defects details of which are given below:"));
342 iOut->writeNewLine();
343 iOut->writeNewLine();
346 for (; index<theFailCount; index++)
348 iOut->writeString(*iKnownDefects[index]);
349 iOut->writeNewLine();
351 iOut->writeString(_L("----------------------------------------------------------------------------------"));
352 iOut->writeNewLine();
353 iOut->writeNewLine();
356 // Display the failed tests
357 TInt iEnd = iFailedTests.Count();
360 iOut->writeString(_L(" Failed tests:-"));
361 iOut->writeNewLine();
362 for (TInt i = 0; i < iEnd; i++)
364 iOut->writeNum(iFailedTests[i]);
365 iOut->writeSpaces(2);
366 iOut->writeString(*iFailedTestNames[i]);
367 iOut->writeNewLine();
369 iOut->writeNewLine();
372 // Summary line is now printed in CTestSetup
375 CBase* CTestHandler::SharedData() const
380 void CTestHandler::SetSharedData(CBase* aData)
385 EXPORT_C void CTestHandler::SetTestRunnerL(CTestRunner *aTestRunner)
387 /* We can't set iTestRunner directly, as we may be called while we're inside
388 * one of its methods. */
390 // This method can be called twice in a row, eg if the -o option is used
393 delete iNextTestRunner;
394 iNextTestRunner = NULL;
397 iNextTestRunner = aTestRunner ? aTestRunner : new (ELeave) CTestRunner(*iOut);
400 void CTestHandler::SetHeapMark(TInt aAllocCount)
402 iHeapMark = aAllocCount;
405 TInt CTestHandler::HeapMark()