|
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: STIFUnit module declaration |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef StifUnitGeneric_H |
|
19 #define StifUnitGeneric_H |
|
20 |
|
21 // INCLUDES |
|
22 #include <e32svr.h> |
|
23 //#include "StifTestModule.h" |
|
24 #include <StifTestInterface.h> |
|
25 #include <StifLogger.h> |
|
26 #include <StifUnitMacros.h> |
|
27 |
|
28 // INCLUDES FROM CASES FILE |
|
29 #define STIF_UNIT_INCLUDE_SECTION |
|
30 #include STIF_UNIT_TEST_CASES |
|
31 #undef STIF_UNIT_INCLUDE_SECTION |
|
32 |
|
33 // FORWARD DECLARATIONS |
|
34 class STIF_UNIT_MODULE_CLASS_NAME; |
|
35 |
|
36 // DATA TYPES |
|
37 // A typedef for function that does the actual testing, |
|
38 // function is a type |
|
39 // TInt STIF_UNIT_MODULE_CLASS_NAME::<NameOfFunction> ( TTestResult& aResult ) |
|
40 typedef TInt (STIF_UNIT_MODULE_CLASS_NAME::* TestFunction)(TTestResult&); |
|
41 |
|
42 // CLASS DECLARATION |
|
43 /** |
|
44 * An internal structure containing a test case name and |
|
45 * the pointer to function doing the test |
|
46 * |
|
47 * @lib ?library |
|
48 * @since ?Series60_version |
|
49 */ |
|
50 class TCaseInfoInternal |
|
51 { |
|
52 public: |
|
53 const TText* iCaseName; |
|
54 TestFunction iMethod; |
|
55 TBool iIsOOMTest; |
|
56 TInt iFirstMemoryAllocation; |
|
57 TInt iLastMemoryAllocation; |
|
58 }; |
|
59 |
|
60 // CLASS DECLARATION |
|
61 /** |
|
62 * A structure containing a test case name and |
|
63 * the pointer to function doing the test |
|
64 * |
|
65 * @lib ?library |
|
66 * @since ?Series60_version |
|
67 */ |
|
68 class TCaseInfo |
|
69 { |
|
70 public: |
|
71 TPtrC iCaseName; |
|
72 TestFunction iMethod; |
|
73 TBool iIsOOMTest; |
|
74 TInt iFirstMemoryAllocation; |
|
75 TInt iLastMemoryAllocation; |
|
76 |
|
77 TCaseInfo( const TText* a ) : iCaseName( (TText*) a ) |
|
78 { |
|
79 }; |
|
80 |
|
81 }; |
|
82 |
|
83 // CLASS DECLARATION |
|
84 /** |
|
85 * STIFUnit class (name of the class is changed by real module name). |
|
86 */ |
|
87 NONSHARABLE_CLASS(STIF_UNIT_MODULE_CLASS_NAME) : public CTestModuleBase |
|
88 { |
|
89 public: //Enums |
|
90 |
|
91 public: // Constructors and destructor |
|
92 |
|
93 |
|
94 /** |
|
95 * Two-phased constructor. |
|
96 */ |
|
97 static STIF_UNIT_MODULE_CLASS_NAME* NewL() |
|
98 { |
|
99 STIF_UNIT_MODULE_CLASS_NAME* self = new (ELeave) STIF_UNIT_MODULE_CLASS_NAME; |
|
100 |
|
101 CleanupStack::PushL( self ); |
|
102 self->ConstructL(); |
|
103 CleanupStack::Pop(); |
|
104 |
|
105 return self; |
|
106 } |
|
107 |
|
108 /** |
|
109 * Destructor. |
|
110 */ |
|
111 virtual ~STIF_UNIT_MODULE_CLASS_NAME() |
|
112 { |
|
113 delete iLog; |
|
114 } |
|
115 |
|
116 |
|
117 public: // New functions |
|
118 |
|
119 /** |
|
120 * ?member_description. |
|
121 * @since ?Series60_version |
|
122 * @param ?arg1 ?description |
|
123 * @return ?description |
|
124 */ |
|
125 //?type ?member_function( ?type ?arg1 ); |
|
126 |
|
127 public: // Functions from base classes |
|
128 |
|
129 /** |
|
130 * From CTestModuleBase InitL is used to initialize the |
|
131 * STIFUnit module. It is called once for every instance of |
|
132 * TestModuleUnit after its creation. |
|
133 * @param aIniFile Initialization file for the test module (optional) |
|
134 * @param aFirstTime Flag is true when InitL is executed for first |
|
135 * created instance of STIFUnit module. |
|
136 * @return Symbian OS error code |
|
137 */ |
|
138 TInt InitL( TFileName& /*aIniFile*/, TBool /*aFirstTime*/ ) |
|
139 { |
|
140 return KErrNone; |
|
141 } |
|
142 |
|
143 /** |
|
144 * From CTestModuleBase GetTestCasesL is used to inquiry test cases |
|
145 * from STIFUnit module. |
|
146 * @since ?Series60_version |
|
147 * @param aTestCaseFile Test case file (optional) |
|
148 * @param aTestCases Array of TestCases returned to test framework |
|
149 * @return Symbian OS error code |
|
150 */ |
|
151 TInt GetTestCasesL( const TFileName& /*aTestCaseFile*/, |
|
152 RPointerArray<TTestCaseInfo>& aTestCases ) |
|
153 { |
|
154 TTestResult dummyResult; |
|
155 return MainTestL(EEnumerateTestCases, -1, aTestCases, dummyResult); |
|
156 } |
|
157 |
|
158 /** |
|
159 * From CTestModuleBase RunTestCaseL is used to run an individual |
|
160 * test case. |
|
161 * @since ?Series60_version |
|
162 * @param aCaseNumber Test case number |
|
163 * @param aTestCaseFile Test case file (optional) |
|
164 * @param aResult Test case result returned to test framework (PASS/FAIL) |
|
165 * @return Symbian OS error code (test case execution error, which is |
|
166 * not reported in aResult parameter as test case failure). |
|
167 */ |
|
168 TInt RunTestCaseL( const TInt aCaseNumber, |
|
169 const TFileName& /*aTestCaseFile*/, |
|
170 TTestResult& aResult ) |
|
171 { |
|
172 if(!iVersionLogged) |
|
173 { |
|
174 SendTestModuleVersion(); |
|
175 iVersionLogged = ETrue; |
|
176 } |
|
177 |
|
178 RPointerArray<TTestCaseInfo> aTestCases; //temporary |
|
179 |
|
180 // iNumberOfTestCases should contain valid number of test cases present in test module. This info |
|
181 // is gathered in the enumeration procedure. |
|
182 // However if test case crashes, test module is loaded again and enumeration is not performed. |
|
183 // In such case counting procedure must be done. |
|
184 if(iNumberOfTestCases == 0) |
|
185 { |
|
186 TRAPD(errCount, MainTestL(ECountTestCases, aCaseNumber, aTestCases, aResult)); |
|
187 if(errCount != KErrNone) |
|
188 { |
|
189 iLog->Log(_L("Error during counting test cases [%d]"), errCount); |
|
190 return errCount; |
|
191 } |
|
192 } |
|
193 |
|
194 /** |
|
195 * SetupL is responsible for inicialization of all fields (etc.) common for all testcases |
|
196 * MainTestL starts required testcase |
|
197 * TeardownL destroys all data that was created by SetupL |
|
198 */ |
|
199 TInt errSetup = KErrNone; |
|
200 TInt errTestcase = KErrNone; |
|
201 |
|
202 if(aCaseNumber > iNumberOfTestCases) |
|
203 return KErrNotFound; |
|
204 |
|
205 __UHEAP_MARK; |
|
206 |
|
207 TRAP(errSetup, MainTestL(ERunTestCase, STIF_RUN_SETUP, aTestCases, aResult); TRAP(errTestcase, MainTestL(ERunTestCase, aCaseNumber, aTestCases, aResult))\ |
|
208 MainTestL(ERunTestCase, STIF_RUN_TEARDOWN, aTestCases, aResult)); |
|
209 |
|
210 __UHEAP_MARKEND; |
|
211 |
|
212 if(errTestcase != KErrNone) |
|
213 { |
|
214 aResult.SetResult(errTestcase, _L("Testcase failed")); |
|
215 } |
|
216 if(errSetup != KErrNone) |
|
217 { |
|
218 aResult.SetResult(errSetup, _L("Setup or teardown failed")); |
|
219 } |
|
220 |
|
221 return KErrNone; |
|
222 } |
|
223 |
|
224 /** |
|
225 * From CTestModuleBase; OOMTestQueryL is used to specify is particular |
|
226 * test case going to be executed using OOM conditions |
|
227 * @param aTestCaseFile Test case file (optional) |
|
228 * @param aCaseNumber Test case number (optional) |
|
229 * @param aFailureType OOM failure type (optional) |
|
230 * @param aFirstMemFailure The first heap memory allocation failure value (optional) |
|
231 * @param aLastMemFailure The last heap memory allocation failure value (optional) |
|
232 * @return TBool |
|
233 */ |
|
234 #ifdef STIFUNIT_OOMTESTQUERYL |
|
235 virtual TBool OOMTestQueryL( const TFileName& aTestCaseFile, |
|
236 const TInt aCaseNumber, |
|
237 TOOMFailureType& aFailureType, |
|
238 TInt& aFirstMemFailure, |
|
239 TInt& aLastMemFailure); |
|
240 #else |
|
241 virtual TBool OOMTestQueryL( const TFileName& /* aTestCaseFile */, |
|
242 const TInt /* aCaseNumber */, |
|
243 TOOMFailureType& /* aFailureType */, |
|
244 TInt& /* aFirstMemFailure */, |
|
245 TInt& /* aLastMemFailure */ ) |
|
246 { |
|
247 return EFalse; |
|
248 } |
|
249 #endif |
|
250 |
|
251 /** |
|
252 * From CTestModuleBase; OOMTestInitializeL may be used to initialize OOM |
|
253 * test environment |
|
254 * @param aTestCaseFile Test case file (optional) |
|
255 * @param aCaseNumber Test case number (optional) |
|
256 * @return None |
|
257 */ |
|
258 #ifdef STIFUNIT_OOMTESTINITIALIZEL |
|
259 virtual void OOMTestInitializeL( const TFileName& aTestCaseFile, |
|
260 const TInt aCaseNumber); |
|
261 #else |
|
262 virtual void OOMTestInitializeL( const TFileName& /* aTestCaseFile */, |
|
263 const TInt /* aCaseNumber */ ) |
|
264 { |
|
265 } |
|
266 #endif |
|
267 |
|
268 /** |
|
269 * From CTestModuleBase; OOMHandleWarningL |
|
270 * @param aTestCaseFile Test case file (optional) |
|
271 * @param aCaseNumber Test case number (optional) |
|
272 * @param aFailNextValue FailNextValue for OOM test execution (optional) |
|
273 * @return None |
|
274 * |
|
275 * User may add implementation for OOM test warning handling. Usually no |
|
276 * implementation is required. |
|
277 */ |
|
278 #ifdef STIFUNIT_OOMHANDLEWARNINGL |
|
279 virtual void OOMHandleWarningL( const TFileName& aTestCaseFile, |
|
280 const TInt aCaseNumber, |
|
281 TInt& aFailNextValue); |
|
282 #else |
|
283 virtual void OOMHandleWarningL( const TFileName& /* aTestCaseFile */, |
|
284 const TInt /* aCaseNumber */, |
|
285 TInt& /* aFailNextValue */) |
|
286 { |
|
287 } |
|
288 #endif |
|
289 |
|
290 /** |
|
291 * From CTestModuleBase; OOMTestFinalizeL may be used to finalize OOM |
|
292 * test environment |
|
293 * @param aTestCaseFile Test case file (optional) |
|
294 * @param aCaseNumber Test case number (optional) |
|
295 * @return None |
|
296 * |
|
297 */ |
|
298 #ifdef STIFUNIT_OOMTESTFINALIZEL |
|
299 virtual void OOMTestFinalizeL( const TFileName& aTestCaseFile, |
|
300 const TInt aCaseNumber); |
|
301 #else |
|
302 virtual void OOMTestFinalizeL( const TFileName& /* aTestCaseFile */, |
|
303 const TInt /* aCaseNumber */ ) |
|
304 { |
|
305 } |
|
306 #endif |
|
307 |
|
308 /** |
|
309 * Method used to log version of test module |
|
310 */ |
|
311 void SendTestModuleVersion() |
|
312 { |
|
313 TVersion moduleVersion; |
|
314 moduleVersion.iMajor = TEST_MODULE_VERSION_MAJOR; |
|
315 moduleVersion.iMinor = TEST_MODULE_VERSION_MINOR; |
|
316 moduleVersion.iBuild = TEST_MODULE_VERSION_BUILD; |
|
317 |
|
318 TFileName moduleName; |
|
319 moduleName = STIF_UNIT_MODULE_NAME; |
|
320 |
|
321 TBool newVersionOfMethod = ETrue; |
|
322 TestModuleIf().SendTestModuleVersion(moduleVersion, moduleName, newVersionOfMethod); |
|
323 } |
|
324 |
|
325 protected: // New functions |
|
326 |
|
327 /** |
|
328 * ?member_description. |
|
329 * @since ?Series60_version |
|
330 * @param ?arg1 ?description |
|
331 * @return ?description |
|
332 */ |
|
333 //?type ?member_function( ?type ?arg1 ); |
|
334 |
|
335 protected: // Functions from base classes |
|
336 |
|
337 /** |
|
338 * From ?base_class ?member_description |
|
339 */ |
|
340 //?type ?member_function(); |
|
341 |
|
342 private: |
|
343 |
|
344 /** |
|
345 * C++ default constructor. |
|
346 */ |
|
347 STIF_UNIT_MODULE_CLASS_NAME() |
|
348 { |
|
349 } |
|
350 |
|
351 /** |
|
352 * By default Symbian 2nd phase constructor is private. |
|
353 */ |
|
354 void ConstructL() |
|
355 { |
|
356 iLog = CStifLogger::NewL( KUnitLogPath, KUnitLogFile); |
|
357 |
|
358 // Sample how to use logging |
|
359 iLog->Log( KLogStart ); |
|
360 |
|
361 iVersionLogged = EFalse; |
|
362 iNumberOfTestCases = 0; |
|
363 } |
|
364 |
|
365 /** |
|
366 * Method containing all test cases, setup and teardown sections. |
|
367 */ |
|
368 TInt MainTestL(TCallReason aRunReason, TInt aTestToRun, RPointerArray<TTestCaseInfo>& aTestCases, TTestResult& aResult) |
|
369 { |
|
370 aResult.iResult = aResult.iResult; //for avoiding compiler warning when unit test does not use any assert macro |
|
371 if(aRunReason == ERunTestCase) |
|
372 { |
|
373 if(aTestToRun < 0) |
|
374 { |
|
375 iLog->Log(_L("Running setup or teardown")); |
|
376 } |
|
377 else |
|
378 { |
|
379 iLog->Log(_L("Running test case #%d"), aTestToRun); |
|
380 } |
|
381 } |
|
382 else if(aRunReason == EEnumerateTestCases) |
|
383 { |
|
384 iLog->Log(_L("Enumerating test cases.")); |
|
385 } |
|
386 else if(aRunReason == ECountTestCases) |
|
387 { |
|
388 iLog->Log(_L("Counting test cases.")); |
|
389 } |
|
390 else |
|
391 { |
|
392 iLog->Log(_L("Unknown run reason [%d]."), aRunReason); |
|
393 return KErrNotSupported; |
|
394 } |
|
395 |
|
396 TInt _test_case_no = -1; |
|
397 |
|
398 //test cases, setup and teardown include |
|
399 #define TEST_CASES |
|
400 #include STIF_UNIT_TEST_CASES |
|
401 #undef TEST_CASES |
|
402 |
|
403 if(aRunReason == EEnumerateTestCases) |
|
404 { |
|
405 iNumberOfTestCases = _test_case_no; |
|
406 iLog->Log(_L("Enumeration completed.")); |
|
407 } |
|
408 else if(aRunReason == ECountTestCases) |
|
409 { |
|
410 iNumberOfTestCases = _test_case_no; |
|
411 iLog->Log(_L("Counting completed.")); |
|
412 } |
|
413 |
|
414 // Test case was executed |
|
415 return KErrNone; |
|
416 } |
|
417 |
|
418 public: // Data |
|
419 // ?one_line_short_description_of_data |
|
420 //?data_declaration; |
|
421 |
|
422 protected: // Data |
|
423 // ?one_line_short_description_of_data |
|
424 //?data_declaration; |
|
425 |
|
426 private: // Data |
|
427 // Pointer to test (function) to be executed |
|
428 TestFunction iMethod; |
|
429 |
|
430 // Pointer to logger |
|
431 CStifLogger * iLog; |
|
432 |
|
433 // Flag saying if version of test module was already sent |
|
434 TBool iVersionLogged; |
|
435 // Total number of test cases |
|
436 TInt iNumberOfTestCases; |
|
437 |
|
438 // ?one_line_short_description_of_data |
|
439 //?data_declaration; |
|
440 |
|
441 // Reserved pointer for future extension |
|
442 //TAny* iReserved; |
|
443 |
|
444 #define TEST_VAR_DECLARATIONS |
|
445 /** |
|
446 * all testmodule-global variables declarations are inserted here |
|
447 */ |
|
448 #include STIF_UNIT_TEST_CASES |
|
449 #undef TEST_VAR_DECLARATIONS |
|
450 |
|
451 public: // Friend classes |
|
452 //?friend_class_declaration; |
|
453 protected: // Friend classes |
|
454 //?friend_class_declaration; |
|
455 private: // Friend classes |
|
456 //?friend_class_declaration; |
|
457 |
|
458 }; |
|
459 |
|
460 EXPORT_C CTestModuleBase* LibEntryL() |
|
461 { |
|
462 return STIF_UNIT_MODULE_CLASS_NAME::NewL(); |
|
463 } |
|
464 |
|
465 EXPORT_C TInt SetRequirements( CTestModuleParam*& /*aTestModuleParam*/, |
|
466 TUint32& /*aParameterValid*/ ) |
|
467 { |
|
468 return KErrNone; |
|
469 } |
|
470 |
|
471 #endif // StifUnitGeneric_H |
|
472 |
|
473 // End of File |