Update contrib.
1 // Copyright (c) 1997-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 "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 // Implementation of the CUnitTest base class.
18 #include <ecom/test_bed/unittest.h>
19 #include <ecom/test_bed/transition.h>
20 #include <ecom/test_bed/datalogger.h>
21 #include <ecom/test_bed/testbeddefinitions.h>
24 EXPORT_C CUnitTest::~CUnitTest()
28 // Delete any outstanding asynchronous transitions
29 if(iOutstandingTransitions)
31 iOutstandingTransitions->Reset();
32 delete iOutstandingTransitions;
37 iTransitions->ResetAndDestroy();
41 iLeaveErrorArray.Reset();
47 EXPORT_C void CUnitTest::UnitTestConstructL()
49 User::LeaveIfError(iFs.Connect());
50 iFileMan = CFileMan::NewL(iFs);
52 iTransitions = new(ELeave) RPointerArray<CTransition>;
53 iOutstandingTransitions = new(ELeave) RPointerArray<CTransition>;
54 _LIT(KConstructingUnitTestMsg, "Constructed Unit Test named %S");
55 iDataLogger.LogInformationWithParameters(KConstructingUnitTestMsg, &iUnitTestName);
59 CUnitTestInfo* CUnitTest::TransitionSetL() const
61 CUnitTestInfo* transitionSet = CUnitTestInfo::NewL(iUnitTestName);
66 EXPORT_C void CUnitTest::RunTest(TTimeIntervalMicroSeconds32 aTimeAfter /*= 0*/)
69 _LIT(KTxtSeparator, "-----------------------------------------------------------------------------------");
70 _LIT(KStartingUnitTest, "Beginning Unit Test named %S");
71 iDataLogger.LogInformation(KTxtSeparator);
72 iDataLogger.LogInformationWithParameters(KStartingUnitTest, &iUnitTestName);
73 iDataLogger.ReportInformationWithParameters(KStartingUnitTest, &iUnitTestName);
76 EXPORT_C void CUnitTest::RunL()
78 _LIT(KUnitTestRunLPanic, "CUnitTest::RunL");
80 TInt status = iStatus.Int();
83 case (KTestBedRepeatTest): /* A stub has requested repeat of the last test */
84 // Go back one so that we repeat the last test
85 --iNextTransitionIndex;
88 case (KTestBedTestLeft): /* The last transition's RunL left */
89 case (KTestBedTestCancel): /* The last transition was cancelled */
90 case (KTestBedLeakTestLoopDetected): /* A leak test detected an infinite loop */
91 case (KTestBedFailedPreConditions): /* The last transition failed it's pre conditions */
92 case (KTestBedFailedPostConditions): /* The last transition failed it's post conditions */
93 // Go to the end of the test so that it finishes
94 iNextTransitionIndex = iTransitions->Count();
97 case (KTestBedAsynchronousTransition): /* The last transition started an async request */
98 // Remember that we have an outstanding request and then carry on
99 iOutstandingTransitions->Append((*iTransitions)[iNextTransitionIndex - 1]);
106 User::Panic(KUnitTestRunLPanic, KTestBedInvalidStatus);
109 // If we still have more transitions to run
110 if(iNextTransitionIndex < iTransitions->Count())
112 iStatus = KRequestPending;
115 // If the next transition is a blocking one then wait for all outstanding async
116 // requests to complete. Otherwise just run the next transition
117 if(((*iTransitions)[iNextTransitionIndex]->IsBlockingTransition()) &&
118 (iOutstandingTransitions->Count() > 0))
120 iWaitingForCompletion = ETrue;
124 (*iTransitions)[iNextTransitionIndex]->RunTransition(&iStatus);
125 ++iNextTransitionIndex;
130 // If we still have outstanding async requests then wait for these to complete
131 // otherwise we have finished this test
132 if(iOutstandingTransitions->Count() > 0)
134 iWaitingForCompletion = ETrue;
135 iStatus = KRequestPending;
140 iUnitTestObserver.Complete(this);
142 _LIT(KInfoPrintFailed, "Failed: Unit Test");
143 _LIT(KTestLeft, "Failed: Unit Test %S left");
144 _LIT(KTestLeftWithUnexpectedError, "Failed: Test %S left with unexpected error");
145 _LIT(KTestFailed, "Failed: Unit Test %S failed a pre/post condition validation check");
146 _LIT(KTestLeftWithExpectedError, "Test %S left with an anticipated error");
147 _LIT(KTestCancelled, "Cancelled: Unit Test Transition %S was cancelled");
148 _LIT(KTestEnteredInfiniteLoop, "Unit Test Transition %S aborted (infinitely looping)");
149 _LIT(KEndingUnitTest, "Successfully completed Unit Test %S");
150 // We use RTest if it is present in the framework to validate
151 // status codes for errors. Note: not all non KErrNone code mean
152 // there was an error and so we need to selective over which
153 // cases below we use RTest().
156 case (KTestBedTestLeft):
158 TInt leaveCode = iCurrentlyExecutingTransition->GetErrorCode();
159 //Check to see if the leave code is NOT on the list of known leaving codes
160 if(iLeaveErrorArray.Find(leaveCode) == KErrNotFound)
162 iDataLogger.LogInformationWithParameters(KTestLeft, &iUnitTestName);
163 iDataLogger.ReportInformationWithParameters(KTestLeft, &iUnitTestName);
166 (*iRTest)(status==KErrNone);
169 else //Leave code is on the list
171 TInt count = iTransitions->Count();
172 //Check transition number and if it is the last transition then this is an expected error
173 CTransition* lastTransition = (*iTransitions)[count-1];
174 if(iCurrentlyExecutingTransition == lastTransition)
176 iDataLogger.LogInformationWithParameters(KTestLeftWithExpectedError, &iUnitTestName);
177 iDataLogger.ReportInformationWithParameters(KEndingUnitTest, &iUnitTestName);
179 else //Otherwise, if not the last transition, the test failed with an unexpected error
181 User::InfoPrint(KInfoPrintFailed);
182 User::InfoPrint(iUnitTestName);
183 iDataLogger.LogInformationWithParameters(KTestLeftWithUnexpectedError, &iUnitTestName);
184 iDataLogger.ReportInformationWithParameters(KTestLeftWithUnexpectedError, &iUnitTestName);
187 (*iRTest)(status==KErrNone);
194 case (KTestBedFailedPreConditions):
195 case (KTestBedFailedPostConditions):
197 User::InfoPrint(KInfoPrintFailed);
198 User::InfoPrint(iUnitTestName);
199 iDataLogger.LogInformationWithParameters(KTestFailed, &iUnitTestName);
200 iDataLogger.ReportInformationWithParameters(KTestFailed, &iUnitTestName);
203 (*iRTest)(status==KErrNone);
208 case (KTestBedTestCancel):
209 iDataLogger.LogInformationWithParameters(KTestCancelled, &iUnitTestName);
210 iDataLogger.ReportInformationWithParameters(KTestCancelled, &iUnitTestName);
213 (*iRTest)(status==KErrNone);
217 case (KTestBedLeakTestLoopDetected):
218 iDataLogger.LogInformationWithParameters(KTestEnteredInfiniteLoop, &iUnitTestName);
219 iDataLogger.ReportInformationWithParameters(KTestEnteredInfiniteLoop, &iUnitTestName);
222 (*iRTest)(status==KErrNone);
227 iDataLogger.LogInformationWithParameters(KEndingUnitTest, &iUnitTestName);
228 iDataLogger.ReportInformationWithParameters(KEndingUnitTest, &iUnitTestName);
232 User::Panic(KUnitTestRunLPanic, KTestBedInvalidStatus);
238 EXPORT_C void CUnitTest::AddTransitionL(CTransition* aTransition)
240 __ASSERT_DEBUG(aTransition, User::Panic(_L("CUnitTest"), KErrTestBedInvalidTransition));
241 CleanupStack::PushL(aTransition);
242 User::LeaveIfError(iTransitions->Append(aTransition));
243 CleanupStack::Pop(aTransition);
246 EXPORT_C void CUnitTest::AddBlockingTransitionL(CTransition* aTransition)
248 __ASSERT_DEBUG(aTransition, User::Panic(_L("CUnitTest"), KErrTestBedInvalidTransition));
249 aTransition->SetBlockingTransition(ETrue);
250 CleanupStack::PushL(aTransition);
251 User::LeaveIfError(iTransitions->Append(aTransition));
252 CleanupStack::Pop(aTransition);
255 EXPORT_C void CUnitTest::AddLeaveErrorCodeL(TInt aLeaveErrorCode)
257 User::LeaveIfError(iLeaveErrorArray.Append(aLeaveErrorCode));
260 EXPORT_C CTransition& CUnitTest::GetCurrentTransition() const
262 // Check fror a stray stub call
263 // We will always have a valid pointer here IF called
264 // from a stub in response to that transition's call
265 // on the stub's methods.
266 __ASSERT_DEBUG(iCurrentlyExecutingTransition, User::Invariant());
267 return *iCurrentlyExecutingTransition;
270 EXPORT_C void CUnitTest::SetCurrentTransition(CTransition& aTransition)
272 iCurrentlyExecutingTransition = &aTransition;
275 EXPORT_C void CUnitTest::Complete(CTransition& aTransition, TInt aAsyncPostCheckError)
277 // Should never be NULL at this point
278 __ASSERT_DEBUG(iCurrentlyExecutingTransition, User::Invariant());
279 if(iCurrentlyExecutingTransition == &aTransition)
280 iCurrentlyExecutingTransition = NULL; // Clear the current transition
282 // Oops the code will crash if this is ever false
283 __ASSERT_DEBUG(iOutstandingTransitions, User::Invariant());
285 // Look-up the transition passed in...
286 TInt index = iOutstandingTransitions->Find(&aTransition);
287 if(index != KErrNotFound)
289 // ... and remove from the set of outstanding ones.
290 iOutstandingTransitions->Remove(index);
292 // Did we fail a second-phase post-condition validation on an asynchronous transition?
293 // Or was it a normal transition completion? Regardless, we go for another iteration
294 // of the AO, passing through the error code.
295 TBool completeIt = (aAsyncPostCheckError != KErrNone);
296 if(iWaitingForCompletion && (iOutstandingTransitions->Count() == 0))
301 TRequestStatus* status = &iStatus;
302 User::RequestComplete(status, aAsyncPostCheckError);
307 __ASSERT_DEBUG(ETrue,
308 User::Panic(_L("CUnitTest"), KErrTestBedInvalidTransition));
312 EXPORT_C void CUnitTest::SetParametersL(TAny* /*aParams*/)
317 EXPORT_C void CUnitTest::DoCancel()
321 if(iCurrentlyExecutingTransition)
322 iCurrentlyExecutingTransition->Cancel();
324 // Cancel any outstanding asynchronous transitions
325 if(iOutstandingTransitions)
327 TInt count = iOutstandingTransitions->Count();
328 for(TInt index = 0; index < count; ++index)
330 (*iOutstandingTransitions)[index]->Cancel();
334 iUnitTestObserver.Complete(this);