|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "symbianunittestresult.h" |
|
19 #include "symbianunittestfailure.h" |
|
20 #include "sutlogger.h" |
|
21 |
|
22 _LIT8( KSymbianUnitTestLeaveCodeMessageFormat, "Leave code: %d" ); |
|
23 _LIT8( KSymbianUnitTestMemoryLeakPhrase, "Leaked %d bytes of memory" ); |
|
24 _LIT8( KSymbianUnitTestPanicPhrase, "Panic: " ); |
|
25 _LIT8( KSymbianUnitTestPanicReasonFormat, ", reason: %d" ); |
|
26 _LIT8( KSymbianUnitTestAllocFailureRateFormat, ", alloc failure rate: %d" ); |
|
27 _LIT8( KSymbianUnitTestSetupFailed, "Leave from the setup function: %d" ); |
|
28 _LIT8( KSymbianUnitTestErrorFormat, "Test Failed File[%S], Line[%d], Msg[%S]"); |
|
29 _LIT8( KSymbianUnitTestFailed, "Test Failed Reason[%S]"); |
|
30 _LIT( KSymbianUnitTestPass, "pass" ); |
|
31 _LIT( KSymbianUnitTestFail, "fail" ); |
|
32 _LIT8( KSymbianUnitTestTimedOut, "Timed out: %d s" ); |
|
33 |
|
34 const TInt KMax64BitUintLengthAsDescriptor = 20; |
|
35 |
|
36 // ----------------------------------------------------------------------------- |
|
37 // |
|
38 // ----------------------------------------------------------------------------- |
|
39 // |
|
40 CSymbianUnitTestResult* CSymbianUnitTestResult::NewLC() |
|
41 { |
|
42 CSymbianUnitTestResult* self = new( ELeave )CSymbianUnitTestResult; |
|
43 CleanupStack::PushL( self ); |
|
44 self->ConstructL(); |
|
45 return self; |
|
46 } |
|
47 |
|
48 // ----------------------------------------------------------------------------- |
|
49 // |
|
50 // ----------------------------------------------------------------------------- |
|
51 // |
|
52 CSymbianUnitTestResult* CSymbianUnitTestResult::NewL() |
|
53 { |
|
54 CSymbianUnitTestResult* self = CSymbianUnitTestResult::NewLC(); |
|
55 CleanupStack::Pop( self ); |
|
56 return self; |
|
57 } |
|
58 |
|
59 // ----------------------------------------------------------------------------- |
|
60 // |
|
61 // ----------------------------------------------------------------------------- |
|
62 // |
|
63 CSymbianUnitTestResult::CSymbianUnitTestResult() |
|
64 : iOriginalHeap( User::Heap() ) |
|
65 { |
|
66 } |
|
67 |
|
68 // ----------------------------------------------------------------------------- |
|
69 // |
|
70 // ----------------------------------------------------------------------------- |
|
71 // |
|
72 void CSymbianUnitTestResult::ConstructL() |
|
73 { |
|
74 iCurrentTestName = KNullDesC().AllocL(); |
|
75 } |
|
76 |
|
77 // ----------------------------------------------------------------------------- |
|
78 // |
|
79 // ----------------------------------------------------------------------------- |
|
80 // |
|
81 CSymbianUnitTestResult::~CSymbianUnitTestResult() |
|
82 { |
|
83 delete iCurrentTestName; |
|
84 iFailures.ResetAndDestroy(); |
|
85 } |
|
86 |
|
87 // ----------------------------------------------------------------------------- |
|
88 // |
|
89 // ----------------------------------------------------------------------------- |
|
90 // |
|
91 TInt CSymbianUnitTestResult::TestCount() const |
|
92 { |
|
93 return iTestCount; |
|
94 } |
|
95 |
|
96 // ----------------------------------------------------------------------------- |
|
97 // |
|
98 // ----------------------------------------------------------------------------- |
|
99 // |
|
100 TInt CSymbianUnitTestResult::PassedTestCount() const |
|
101 { |
|
102 return ( iTestCount - iFailures.Count() ); |
|
103 } |
|
104 |
|
105 // ----------------------------------------------------------------------------- |
|
106 // |
|
107 // ----------------------------------------------------------------------------- |
|
108 // |
|
109 RPointerArray< CSymbianUnitTestFailure >& CSymbianUnitTestResult::Failures() |
|
110 { |
|
111 return iFailures; |
|
112 } |
|
113 |
|
114 // ----------------------------------------------------------------------------- |
|
115 // |
|
116 // ----------------------------------------------------------------------------- |
|
117 // |
|
118 void CSymbianUnitTestResult::StartTestL( const TDesC& aTestName ) |
|
119 { |
|
120 SUT_LOG_FORMAT(_L("StartCase[%S]"), &aTestName); |
|
121 HBufC* tmp = aTestName.AllocL(); |
|
122 delete iCurrentTestName; |
|
123 iCurrentTestName = tmp; |
|
124 iTestCount++; |
|
125 iCurrentResult = ETrue; |
|
126 iTime.UniversalTime(); |
|
127 } |
|
128 |
|
129 |
|
130 // ----------------------------------------------------------------------------- |
|
131 // |
|
132 // ----------------------------------------------------------------------------- |
|
133 // |
|
134 void CSymbianUnitTestResult::EndTestL() |
|
135 { |
|
136 TTime now; |
|
137 now.UniversalTime(); |
|
138 TTimeIntervalMicroSeconds microSeconds = now.MicroSecondsFrom(iTime); |
|
139 TReal64 ms= microSeconds.Int64(); |
|
140 //log the test time in milliseconds |
|
141 SUT_LOG_FORMAT2(_L("EndCase Result[%S] Time[%d]ms"), |
|
142 iCurrentResult?&KSymbianUnitTestPass:&KSymbianUnitTestFail, |
|
143 microSeconds.Int64()/1000); |
|
144 //SUT_LOG_FORMAT2(_L("EndCase Result[%S] Time[%d]ms"), &KSymbianUnitTestPass, microSeconds.Int64()/1000); |
|
145 } |
|
146 |
|
147 |
|
148 |
|
149 // ----------------------------------------------------------------------------- |
|
150 // |
|
151 // ----------------------------------------------------------------------------- |
|
152 // |
|
153 void CSymbianUnitTestResult::AddSetupErrorL( TInt aError ) |
|
154 { |
|
155 const TInt KLength = |
|
156 KSymbianUnitTestSetupFailed().Length() + |
|
157 KMax64BitUintLengthAsDescriptor; |
|
158 HBufC8* errorAsText = HBufC8::NewLC( KLength ); |
|
159 errorAsText->Des().Format( KSymbianUnitTestSetupFailed, aError ); |
|
160 User::LeaveIfError( AddFailure( *errorAsText, 0 ) ); |
|
161 CleanupStack::PopAndDestroy( errorAsText ); |
|
162 } |
|
163 |
|
164 // ----------------------------------------------------------------------------- |
|
165 // |
|
166 // ----------------------------------------------------------------------------- |
|
167 // |
|
168 void CSymbianUnitTestResult::AddLeaveFromTestL( |
|
169 TInt aLeaveCode, |
|
170 TInt aAllocFailureRate ) |
|
171 { |
|
172 const TInt KLength = |
|
173 KSymbianUnitTestLeaveCodeMessageFormat().Length() + |
|
174 KMax64BitUintLengthAsDescriptor; |
|
175 HBufC8* failureMsg = HBufC8::NewLC( KLength ); |
|
176 failureMsg->Des().Format( |
|
177 KSymbianUnitTestLeaveCodeMessageFormat, aLeaveCode ); |
|
178 User::LeaveIfError( AddFailure( *failureMsg, aAllocFailureRate ) ); |
|
179 CleanupStack::PopAndDestroy( failureMsg ); |
|
180 } |
|
181 |
|
182 // ----------------------------------------------------------------------------- |
|
183 // |
|
184 // ----------------------------------------------------------------------------- |
|
185 // |
|
186 void CSymbianUnitTestResult::AddMemoryLeakInfoL( |
|
187 TInt aLeakedMemory, |
|
188 TInt aAllocFailureRate ) |
|
189 { |
|
190 const TInt KLength = |
|
191 KSymbianUnitTestMemoryLeakPhrase().Length() + |
|
192 KMax64BitUintLengthAsDescriptor; |
|
193 HBufC8* failureMsg = HBufC8::NewLC( KLength ); |
|
194 failureMsg->Des().Format( |
|
195 KSymbianUnitTestMemoryLeakPhrase, aLeakedMemory ); |
|
196 User::LeaveIfError( AddFailure( *failureMsg, aAllocFailureRate ) ); |
|
197 CleanupStack::PopAndDestroy( failureMsg ); |
|
198 } |
|
199 |
|
200 // ----------------------------------------------------------------------------- |
|
201 // |
|
202 // ----------------------------------------------------------------------------- |
|
203 // |
|
204 void CSymbianUnitTestResult::AddPanicInfoL( |
|
205 const TExitCategoryName& aPanicCategory, |
|
206 TInt aPanicReason, |
|
207 TInt aAllocFailureRate ) |
|
208 { |
|
209 const TInt KLength = |
|
210 KSymbianUnitTestPanicPhrase().Length() + aPanicCategory.Length() + |
|
211 KSymbianUnitTestPanicReasonFormat().Length() + |
|
212 KMax64BitUintLengthAsDescriptor; |
|
213 HBufC8* panicAsText = HBufC8::NewLC( KLength ); |
|
214 TPtr8 panicAsTextPtr( panicAsText->Des() ); |
|
215 panicAsTextPtr.Append( KSymbianUnitTestPanicPhrase ); |
|
216 panicAsTextPtr.Append( aPanicCategory ); |
|
217 panicAsTextPtr.AppendFormat( |
|
218 KSymbianUnitTestPanicReasonFormat, aPanicReason ); |
|
219 User::LeaveIfError( AddFailure( panicAsTextPtr, aAllocFailureRate ) ); |
|
220 CleanupStack::PopAndDestroy( panicAsText ); |
|
221 } |
|
222 |
|
223 // ----------------------------------------------------------------------------- |
|
224 // |
|
225 // ----------------------------------------------------------------------------- |
|
226 // |
|
227 TInt CSymbianUnitTestResult::AddAssertFailure( |
|
228 const TDesC8& aFailureMessage, |
|
229 TInt aLineNumber, |
|
230 const TDesC8& aFileName ) |
|
231 { |
|
232 RHeap* currentHeap = User::SwitchHeap( &iOriginalHeap ); |
|
233 TRAPD( err, AddAssertFailureL( aFailureMessage, aLineNumber, aFileName ) ); |
|
234 User::SwitchHeap( currentHeap ); |
|
235 return err; |
|
236 } |
|
237 |
|
238 // ----------------------------------------------------------------------------- |
|
239 // |
|
240 // ----------------------------------------------------------------------------- |
|
241 // |
|
242 void CSymbianUnitTestResult::AddAssertFailureL( |
|
243 const TDesC8& aFailureMessage, |
|
244 TInt aLineNumber, |
|
245 const TDesC8& aFileName ) |
|
246 { |
|
247 CSymbianUnitTestFailure* failure = |
|
248 CSymbianUnitTestFailure::NewLC( |
|
249 *iCurrentTestName, aFailureMessage, aLineNumber, aFileName ); |
|
250 SUT_LOG_FORMAT3(KSymbianUnitTestErrorFormat, &aFileName, aLineNumber, &aFailureMessage); |
|
251 iCurrentResult = EFalse; |
|
252 iFailures.AppendL( failure ); |
|
253 CleanupStack::Pop( failure ); |
|
254 } |
|
255 |
|
256 // ----------------------------------------------------------------------------- |
|
257 // |
|
258 // ----------------------------------------------------------------------------- |
|
259 // |
|
260 void CSymbianUnitTestResult::AddTimeOutErrorL( TInt aTimeOut ) |
|
261 { |
|
262 const TInt KLength = |
|
263 KSymbianUnitTestTimedOut().Length() + |
|
264 KMax64BitUintLengthAsDescriptor; |
|
265 HBufC8* errorAsText = HBufC8::NewLC( KLength ); |
|
266 errorAsText->Des().Format( KSymbianUnitTestTimedOut, aTimeOut ); |
|
267 User::LeaveIfError( AddFailure( *errorAsText, 0 ) ); |
|
268 CleanupStack::PopAndDestroy( errorAsText ); |
|
269 } |
|
270 |
|
271 |
|
272 // ----------------------------------------------------------------------------- |
|
273 // |
|
274 // ----------------------------------------------------------------------------- |
|
275 // |
|
276 TInt CSymbianUnitTestResult::AddFailure( |
|
277 const TDesC8& aFailureText, |
|
278 TInt aAllocFailureRate ) |
|
279 { |
|
280 RHeap* currentHeap = User::SwitchHeap( &iOriginalHeap ); |
|
281 TRAPD( err, AddFailureL( aFailureText, aAllocFailureRate ) ) |
|
282 User::SwitchHeap( currentHeap ); |
|
283 return err; |
|
284 } |
|
285 |
|
286 // ----------------------------------------------------------------------------- |
|
287 // |
|
288 // ----------------------------------------------------------------------------- |
|
289 // |
|
290 void CSymbianUnitTestResult::AddFailureL( |
|
291 const TDesC8& aFailureText, |
|
292 TInt aAllocFailureRate ) |
|
293 { |
|
294 CSymbianUnitTestFailure* failure = NULL; |
|
295 if ( aAllocFailureRate > 0 ) |
|
296 { |
|
297 const TInt KLength = |
|
298 aFailureText.Length() + |
|
299 KSymbianUnitTestAllocFailureRateFormat().Length() + |
|
300 KMax64BitUintLengthAsDescriptor; |
|
301 HBufC8* failureMsg = HBufC8::NewLC( KLength ); |
|
302 failureMsg->Des() = aFailureText; |
|
303 failureMsg->Des().AppendFormat( |
|
304 KSymbianUnitTestAllocFailureRateFormat, aAllocFailureRate ); |
|
305 failure = CSymbianUnitTestFailure::NewL( |
|
306 *iCurrentTestName, *failureMsg, KErrNotFound, KNullDesC8 ); |
|
307 CleanupStack::PopAndDestroy( failureMsg ); |
|
308 CleanupStack::PushL( failure ); |
|
309 } |
|
310 else |
|
311 { |
|
312 failure = CSymbianUnitTestFailure::NewLC( |
|
313 *iCurrentTestName, aFailureText, KErrNotFound, KNullDesC8 ); |
|
314 } |
|
315 SUT_LOG_FORMAT(KSymbianUnitTestFailed, &aFailureText); |
|
316 iCurrentResult = EFalse; |
|
317 iFailures.AppendL( failure ); |
|
318 CleanupStack::Pop( failure ); |
|
319 } |