|
1 // Copyright (c) 2003-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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // This contains CTestSuite which is the base class for all the TestSuite DLLs |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file TestSuite.cpp |
|
20 */ |
|
21 |
|
22 // EPOC includes |
|
23 #include <e32base.h> |
|
24 |
|
25 // Test system includes |
|
26 #include "../inc/Log.h" |
|
27 #include "../inc/TestStep.h" |
|
28 #include "../inc/TestSuite.h" |
|
29 |
|
30 EXPORT_C CTestSuite::~CTestSuite() |
|
31 /** |
|
32 Test suite destructor. |
|
33 This destroys all the test steps contained in this class and |
|
34 in classes drived from it. |
|
35 */ |
|
36 { |
|
37 // free all test steps |
|
38 if (iArrayTestSteps) |
|
39 iArrayTestSteps->ResetAndDestroy(); |
|
40 |
|
41 // free the dynamic array used for test steps |
|
42 delete iArrayTestSteps; |
|
43 |
|
44 } |
|
45 |
|
46 EXPORT_C void CTestSuite::ConstructL( ) |
|
47 /** |
|
48 Construct a test suite object and initialise any drived test suites |
|
49 |
|
50 @note This class is used as a base class for all test suites. |
|
51 It contains all the functionality common to the test suites |
|
52 that is independent of any particular test. |
|
53 */ |
|
54 { |
|
55 // create a new Array to store the test steps in |
|
56 iArrayTestSteps = new(ELeave) CArrayPtrFlat<CTestStep>(1); |
|
57 |
|
58 // default severity |
|
59 SetSeverity(ESevrAll); |
|
60 iLogger = NULL; |
|
61 |
|
62 //-- initialize suite name buffer |
|
63 new((TAny*)&iSuiteName) TBuf<MAX_LEN_TEST_SUITE_NAME>; |
|
64 iSuiteName.Copy(_L("Uninitialized Suite name")); |
|
65 |
|
66 // initialise the derived test suites |
|
67 InitialiseL(); |
|
68 } |
|
69 |
|
70 /** |
|
71 version string constant |
|
72 */ |
|
73 _LIT(KTxtVersion,"?.?"); |
|
74 EXPORT_C TPtrC CTestSuite::GetVersion( void ) |
|
75 /** |
|
76 Make a version string available for test system. |
|
77 |
|
78 @return A string representation of the current version. |
|
79 @note This method should be overridden in the test suites. |
|
80 This is not a pure virtual so as to be backward compatible with some early test suites which did not implement this method. |
|
81 */ |
|
82 { |
|
83 return KTxtVersion(); |
|
84 } |
|
85 |
|
86 EXPORT_C void CTestSuite::AddTestStepL( CTestStep * aTestStep ) |
|
87 /** |
|
88 Add a test step into the suite. |
|
89 |
|
90 @param aTestStep A pointer to a CTestStep which is added into the test suite. |
|
91 */ |
|
92 { |
|
93 __ASSERT_ALWAYS(aTestStep,User::Panic(_L("CTestSuite::AddTestStepL"),KErrArgument)); |
|
94 // test steps contain a pointer back to the suite which owns them |
|
95 aTestStep->iSuite = this; |
|
96 // add the step, order is not important, so at position 1 |
|
97 iArrayTestSteps->AppendL(aTestStep, 1); |
|
98 } |
|
99 |
|
100 EXPORT_C enum TVerdict CTestSuite::DoTestStep( TDesC &step, TDesC &config ) |
|
101 /** |
|
102 Run the test step specified. |
|
103 |
|
104 @param step The name of the test step to be run. |
|
105 @param config Configuration values file name (*.ini), as supplied in the test script. |
|
106 @return The test result. |
|
107 @note This function traps leaves in the test steps, so should not ever leave. |
|
108 */ |
|
109 { |
|
110 // get the number of tests available in this suite |
|
111 TInt NoOfTests = iArrayTestSteps->Count(); |
|
112 |
|
113 // search the available test steps for the required one |
|
114 for ( TInt i=0; i < NoOfTests; i++ ) |
|
115 { |
|
116 if ( iArrayTestSteps->At(i)->iTestStepName.MatchF( step )!= KErrNotFound ) |
|
117 { |
|
118 |
|
119 // reset the log data |
|
120 iLogData.Zero(); |
|
121 |
|
122 // found required test so initialise to PASS |
|
123 iArrayTestSteps->At(i)->iTestStepResult = EPass; |
|
124 |
|
125 // pass the config file info into the test step |
|
126 iArrayTestSteps->At(i)->LoadConfig( config ); |
|
127 |
|
128 // assume it's going to work |
|
129 enum TVerdict result = EPass; |
|
130 |
|
131 // And assume that it is going to leave |
|
132 TBool testLeft = ETrue; |
|
133 // execute test step preamble (if any) inside a trap |
|
134 TRAPD( rPreAmble, result = iArrayTestSteps->At(i)->doTestStepPreambleL( ) ; testLeft = EFalse) |
|
135 |
|
136 if (testLeft) |
|
137 { |
|
138 ERR_PRINTF4(_L("Warning Test step:%S PreAmble in suite:%S left with %d!"), &step, &iSuiteName, testLeft ); |
|
139 result = EFail; |
|
140 } |
|
141 else if (rPreAmble!= KErrNone) |
|
142 { |
|
143 ERR_PRINTF4(_L("Warning Test step:%S PreAmble in suite:%S failed with %d!"), &step, &iSuiteName, rPreAmble ); |
|
144 result = EFail; |
|
145 } |
|
146 |
|
147 // only continue if the preamble passed |
|
148 if (result == EPass) |
|
149 { |
|
150 testLeft = ETrue; |
|
151 // now execute test step inside a trap |
|
152 TRAPD( r, result = iArrayTestSteps->At(i)->doTestStepL( ); testLeft = EFalse) |
|
153 |
|
154 if (testLeft) |
|
155 { |
|
156 ERR_PRINTF4(_L("Warning Test step:%S in suite:%S left with %d!"), &step, &iSuiteName, r ); |
|
157 result = EFail; |
|
158 } |
|
159 |
|
160 else if (r!= KErrNone) |
|
161 { |
|
162 ERR_PRINTF4(_L("Warning Test step:%S in suite:%S failed with %d!"), &step, &iSuiteName, result ); |
|
163 result = EFail; |
|
164 } |
|
165 |
|
166 testLeft = ETrue; |
|
167 // execute test step postamble (if any) inside a trap |
|
168 enum TVerdict PostAmbleResult = EPass; |
|
169 TRAPD( rPostAmble, PostAmbleResult = iArrayTestSteps->At(i)->doTestStepPostambleL( ); testLeft = EFalse) |
|
170 |
|
171 // The postamble result is only significant if the test has passed |
|
172 if ( result == EPass ) result = PostAmbleResult; |
|
173 |
|
174 if (testLeft) |
|
175 { |
|
176 ERR_PRINTF4(_L("Warning Test step:%S PostAmble in suite:%S left with %d!"), &step, &iSuiteName, testLeft ); |
|
177 result = EFail; |
|
178 } |
|
179 else if (rPostAmble!= KErrNone) |
|
180 { |
|
181 ERR_PRINTF4(_L("Warning Test step:%S PostAmble in suite:%S failed with %d!"), &step, &iSuiteName, rPostAmble ); |
|
182 result = EFail; |
|
183 } |
|
184 } |
|
185 |
|
186 // clean up Config data object |
|
187 iArrayTestSteps->At(i)->UnLoadConfig(); |
|
188 |
|
189 return result; |
|
190 |
|
191 } |
|
192 } |
|
193 |
|
194 // suite has been searched but test step not found so |
|
195 ERR_PRINTF3(_L("Failed to find test step:%S in suite:%S"), &step, &iSuiteName ); |
|
196 |
|
197 return ETestSuiteError; |
|
198 |
|
199 } |
|
200 |
|
201 EXPORT_C enum TVerdict CTestSuite::DoTestUnit( TDesC &config ) |
|
202 /** |
|
203 Run the unit test |
|
204 |
|
205 @param config Configuration values file name (*.ini), as supplied in the test script. |
|
206 @return The test result. |
|
207 */ |
|
208 { |
|
209 TPtrC step,verdict; |
|
210 //TIntegrationTestLog16Overflow iOverflow16; |
|
211 TVerdict result = EPass; |
|
212 TInt iPass(1); |
|
213 TInt iFail(0); |
|
214 TInt iInconclusive(0); |
|
215 TInt iTestSuiteError(0); |
|
216 TInt iAbort(0); |
|
217 TInt iTotal(1); |
|
218 |
|
219 // get the number of tests available in this suite |
|
220 TInt NoOfTests = iArrayTestSteps->Count(); |
|
221 |
|
222 // search all available test steps |
|
223 for ( TInt i=0; i < NoOfTests; i++ ) |
|
224 { |
|
225 step.Set(iArrayTestSteps->At(i)->iTestStepName); |
|
226 |
|
227 result = DoTestStep(step, config); |
|
228 |
|
229 verdict.Set(iLogger->TestResultText( result )); |
|
230 if(verdict.Ptr() == _S("FAIL")) |
|
231 { |
|
232 iPass =0; |
|
233 iFail=1; |
|
234 } |
|
235 |
|
236 |
|
237 iLogger->LogResult(result, _L("Test Result for %S is %s"), &step, verdict.Ptr()); |
|
238 iLogger->LogBlankLine(); |
|
239 iLogger->LogBlankLine(); |
|
240 |
|
241 iLogger->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Test Results Summary ") ); |
|
242 iLogger->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("-------------------- ") ); |
|
243 iLogger->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Passed :%d"), iPass); |
|
244 iLogger->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Failed :%d"), iFail); |
|
245 iLogger->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Inconclusive :%d"), iInconclusive); |
|
246 iLogger->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Test suite errors :%d"), iTestSuiteError); |
|
247 iLogger->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Aborted :%d"), iAbort); |
|
248 iLogger->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Total :%d"), iTotal); |
|
249 |
|
250 } |
|
251 |
|
252 return result; |
|
253 } |
|
254 |
|
255 EXPORT_C void CTestSuite::Log( TRefByValue<const TDesC16> format, ... ) |
|
256 /** |
|
257 General purpose formated logging output. |
|
258 |
|
259 @param format Printf style formated output string. |
|
260 */ |
|
261 { |
|
262 |
|
263 VA_LIST aList; |
|
264 VA_START( aList, format ); |
|
265 |
|
266 if(iLogger) iLogger->Log(format, aList); |
|
267 |
|
268 VA_END( aList ); |
|
269 } |
|
270 |
|
271 EXPORT_C void CTestSuite::Log( TInt aSeverity, TRefByValue<const TDesC16> format, ... ) |
|
272 /** |
|
273 General purpose formated logging output. |
|
274 |
|
275 @param aSeverity The current log severity level. |
|
276 @param format Printf style formated output string. |
|
277 */ |
|
278 { |
|
279 VA_LIST aList; |
|
280 VA_START( aList, format ); |
|
281 |
|
282 if( aSeverity & Severity()) |
|
283 { |
|
284 if(iLogger) iLogger->Log(format, aList); |
|
285 } |
|
286 |
|
287 VA_END( aList ); |
|
288 } |
|
289 |
|
290 EXPORT_C void CTestSuite::LogExtra(const TText8* aFile, TInt aLine, TInt aSeverity, |
|
291 TRefByValue<const TDesC> aFmt,...) |
|
292 /** |
|
293 General purpose formated logging output. |
|
294 |
|
295 @param aFile The current file name. |
|
296 @param aLine The current line number. |
|
297 @param aSeverity The current log severity level. |
|
298 @param aFmt Printf style formated output string. |
|
299 */ |
|
300 { |
|
301 VA_LIST aList; |
|
302 VA_START( aList, aFmt ); |
|
303 |
|
304 if( aSeverity & Severity()) |
|
305 { |
|
306 if(iLogger) |
|
307 { |
|
308 iLogger->LogExtra(aFile, aLine, aSeverity, aFmt, aList); |
|
309 } |
|
310 } |
|
311 |
|
312 VA_END( aList ); |
|
313 } |
|
314 |
|
315 |
|
316 EXPORT_C void CTestSuite::testBooleanTrueL( TBool aCondition, char* aFile, TInt aLine ) |
|
317 /** |
|
318 Check the boolean expression is true, |
|
319 if not record error and then leave. |
|
320 |
|
321 @param aCondition The condition to test. |
|
322 @param aFile The current file name. |
|
323 @param aLine The current line number. |
|
324 @note This fuunction can leave. |
|
325 */ |
|
326 { |
|
327 |
|
328 // check condition |
|
329 if (aCondition) |
|
330 return; |
|
331 |
|
332 // convert filename for log |
|
333 TBuf<MAX_LOG_FILENAME_LENGTH> fileName; |
|
334 fileName.Copy(TPtrC8((TText8*)aFile)); |
|
335 |
|
336 // display a log message |
|
337 ERR_PRINTF3(_L("Test Failed in file:%S line:%d"), &fileName, aLine); |
|
338 |
|
339 // leave with error code |
|
340 User::Leave(TEST_ERROR_CODE); |
|
341 |
|
342 } |
|
343 |
|
344 EXPORT_C void CTestSuite::SetSeverity( TInt aSeverity) |
|
345 /** |
|
346 Set the current log severity level. |
|
347 |
|
348 @param aSeverity The new log level severity. |
|
349 */ |
|
350 { |
|
351 iSeverity = aSeverity; |
|
352 } |
|
353 |
|
354 EXPORT_C TInt CTestSuite::Severity() |
|
355 /** |
|
356 Get the current log severity level. |
|
357 */ |
|
358 { |
|
359 return iSeverity; |
|
360 } |
|
361 |
|
362 EXPORT_C void CTestSuite::SetLogSystem(CLog *aLogger) |
|
363 /** |
|
364 Set logging system. |
|
365 |
|
366 @param aLogger Loads the test suite with a pointer to the log system provided by the test framework. |
|
367 */ |
|
368 { |
|
369 iLogger = aLogger; |
|
370 } |
|
371 |
|
372 /** |
|
373 Set a new suite name. |
|
374 Actually, there is no need for test suite derivatives to override existing suite name, because it is |
|
375 set automatically during loading test suite dll. |
|
376 |
|
377 @param aNewName new name for this test suite |
|
378 */ |
|
379 EXPORT_C void CTestSuite::OverrideSuiteName(const TDesC& aNewName) |
|
380 { |
|
381 iSuiteName.Copy(aNewName); |
|
382 } |