|
1 // Copyright (c) 2004-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 file contains test classes and their implementations |
|
15 // to test production class CBackupNotifier. Where necessary stubs |
|
16 // are implemented to help in writing test harness using RTest. |
|
17 // |
|
18 // |
|
19 |
|
20 #include <e32test.h> |
|
21 #include <s32file.h> |
|
22 #include <bautils.h> |
|
23 |
|
24 #include "BackupNotifier.h" |
|
25 #include "BackupNotifierObserver.h" |
|
26 |
|
27 // Used for suppressing warning in OOM tests |
|
28 #define __UNUSED_VAR(var) var = var |
|
29 |
|
30 LOCAL_D CTrapCleanup* TheTrapCleanup =NULL; |
|
31 |
|
32 LOCAL_D CActiveScheduler* TheActiveScheduler =NULL; |
|
33 |
|
34 LOCAL_D RTest test(_L("t_backupnotifier.exe")); |
|
35 |
|
36 //It is used by some test methods which are called two times: |
|
37 //from normal test and from OOM test. |
|
38 static void LeaveIfErrNoMemory(TInt aError) |
|
39 { |
|
40 if(aError == KErrNoMemory) |
|
41 { |
|
42 User::Leave(aError); |
|
43 } |
|
44 } |
|
45 |
|
46 /** |
|
47 Stub classes are provided to act as replacement members for the class |
|
48 under test. This ensures the test class is constructed correctly and its |
|
49 behaviour is also correct within the context of the test. It allows the |
|
50 class to be tested in isolation. Other tests are available that test the |
|
51 interoperability of the class |
|
52 */ |
|
53 class CBackupNotifierObserverStub : public CBase, public MBackupNotifierObserver |
|
54 { |
|
55 public: |
|
56 |
|
57 CBackupNotifierObserverStub(); |
|
58 ~CBackupNotifierObserverStub(); |
|
59 TInt Suspend(); |
|
60 TInt Resume(); |
|
61 |
|
62 /** Flag to check for BackupNotifierObserver directory scanning status*/ |
|
63 TBool iSuspended; |
|
64 }; |
|
65 |
|
66 CBackupNotifierObserverStub::CBackupNotifierObserverStub() |
|
67 : CBase() |
|
68 { |
|
69 iSuspended = EFalse; |
|
70 } |
|
71 |
|
72 CBackupNotifierObserverStub::~CBackupNotifierObserverStub() |
|
73 { |
|
74 // Do nothing here |
|
75 } |
|
76 |
|
77 /** |
|
78 Overload of the MBackupNotifierObserver method. |
|
79 Called to stop the directory scanning as the backup session is started. |
|
80 */ |
|
81 TInt CBackupNotifierObserverStub::Suspend() |
|
82 { |
|
83 iSuspended = ETrue; |
|
84 return KErrNone; |
|
85 } |
|
86 |
|
87 |
|
88 /** |
|
89 Overload of the MBackupNotifierObserver method. |
|
90 Called to resume the directory scanning as the backup session is over. |
|
91 */ |
|
92 TInt CBackupNotifierObserverStub::Resume() |
|
93 { |
|
94 iSuspended = EFalse; |
|
95 return KErrNone; |
|
96 } |
|
97 |
|
98 /** |
|
99 This friend class allows us to access private and protected members of production |
|
100 code class BackupNotifier. |
|
101 */ |
|
102 class TBackupNotifier_StateAccessor |
|
103 { |
|
104 public: |
|
105 void HandleBackupOperationEventL(CBackupNotifier& , const TBackupOperationAttributes&); |
|
106 void RegisterForNotificationsL(CBackupNotifier& aNotifier); |
|
107 void DeregisterForNotifications(CBackupNotifier& aNotifier); |
|
108 TBool IsRegisteredForNotifications(CBackupNotifier& aNotifier); |
|
109 }; |
|
110 |
|
111 /** |
|
112 Handles the Backup operation event. |
|
113 |
|
114 @param aNotifier The CBackupNotifier class object under test |
|
115 @param aAttributes Attribute specifying the Backup event(Start/Stop) |
|
116 */ |
|
117 void TBackupNotifier_StateAccessor::HandleBackupOperationEventL(CBackupNotifier& aNotifier, |
|
118 const TBackupOperationAttributes& aAttributes) |
|
119 { |
|
120 aNotifier.HandleBackupOperationEventL(aAttributes); |
|
121 } |
|
122 |
|
123 /** |
|
124 Creates the Backup session if already not created and Registers BackupNotifier for |
|
125 notifications from the backup server. |
|
126 |
|
127 @param aNotifier The CBackupNotifier class object under test |
|
128 */ |
|
129 void TBackupNotifier_StateAccessor::RegisterForNotificationsL(CBackupNotifier& aNotifier) |
|
130 { |
|
131 aNotifier.RegisterForNotificationsL(); |
|
132 } |
|
133 |
|
134 /** |
|
135 Deregisters the BackupNotifier for notifications from the backup server and deletes |
|
136 the Backup session object. |
|
137 |
|
138 @param aNotifier The CBackupNotifier class object under test |
|
139 */ |
|
140 void TBackupNotifier_StateAccessor::DeregisterForNotifications(CBackupNotifier& aNotifier) |
|
141 { |
|
142 if(aNotifier.iBackupSession) |
|
143 { |
|
144 if(aNotifier.iIsRegistered) |
|
145 { |
|
146 aNotifier.iBackupSession->DeRegisterBackupOperationObserver(aNotifier); |
|
147 } |
|
148 delete aNotifier.iBackupSession; |
|
149 aNotifier.iBackupSession = NULL; |
|
150 } |
|
151 aNotifier.iIsRegistered = EFalse; |
|
152 } |
|
153 |
|
154 /** |
|
155 Checks whether the notifier object is registered for backup notifications. |
|
156 |
|
157 @param aNotifier The CBackupNotifier class object under test |
|
158 */ |
|
159 TBool TBackupNotifier_StateAccessor::IsRegisteredForNotifications(CBackupNotifier& aNotifier) |
|
160 { |
|
161 return aNotifier.iIsRegistered; |
|
162 } |
|
163 |
|
164 |
|
165 /** |
|
166 Test class encloses necessary members that aid to test CBackupNotifier |
|
167 */ |
|
168 class CBackUpNotifierTest: public CBase |
|
169 { |
|
170 public: |
|
171 static CBackUpNotifierTest* NewL(); |
|
172 |
|
173 ~CBackUpNotifierTest(); |
|
174 void HandleBackupOperationEventTestL(); |
|
175 void RegisterForNotificationsTestL(); |
|
176 |
|
177 private: |
|
178 CBackUpNotifierTest(); |
|
179 void ConstructL(); |
|
180 |
|
181 public: |
|
182 /** The instance of the class under test */ |
|
183 CBackupNotifier* iBackupNotifier; |
|
184 |
|
185 /** The instance of the stubbed observer of the class under test */ |
|
186 CBackupNotifierObserverStub* iObserverStub; |
|
187 |
|
188 /** Friend class pointer used for accessing private members */ |
|
189 TBackupNotifier_StateAccessor* iStateAccessor; |
|
190 }; |
|
191 |
|
192 /** |
|
193 Create a new CBackUpNotifierTest object |
|
194 @return A pointer to the newly created class. |
|
195 */ |
|
196 CBackUpNotifierTest* CBackUpNotifierTest::NewL() |
|
197 { |
|
198 CBackUpNotifierTest* self = new (ELeave) CBackUpNotifierTest(); |
|
199 CleanupStack::PushL(self); |
|
200 self->ConstructL(); |
|
201 CleanupStack::Pop(); |
|
202 return self; |
|
203 } |
|
204 |
|
205 /** |
|
206 Standardized default constructor |
|
207 @post CBackUpNotifierTest is fully constructed |
|
208 */ |
|
209 |
|
210 CBackUpNotifierTest::CBackUpNotifierTest() |
|
211 { |
|
212 // Do nothing here |
|
213 } |
|
214 |
|
215 /** |
|
216 Standardized 2nd(Initialization) phase of two phase construction. |
|
217 Completes the safe construction of the CBackUpNotifierTest object |
|
218 @post CBackUpNotifierTest is fully constructed. |
|
219 @leave KErrNoMemory. |
|
220 */ |
|
221 void CBackUpNotifierTest::ConstructL() |
|
222 { |
|
223 iStateAccessor = new(ELeave) TBackupNotifier_StateAccessor(); |
|
224 iObserverStub = new(ELeave) CBackupNotifierObserverStub(); |
|
225 iBackupNotifier = CBackupNotifier::NewL(*iObserverStub); |
|
226 } |
|
227 |
|
228 /** |
|
229 Standard destructor |
|
230 */ |
|
231 CBackUpNotifierTest::~CBackUpNotifierTest() |
|
232 { |
|
233 delete iBackupNotifier; |
|
234 delete iObserverStub; |
|
235 delete iStateAccessor; |
|
236 } |
|
237 |
|
238 /** |
|
239 The test executes by first starting the backup session and then by ending it. |
|
240 Backup operation event notifications are passed to the observer(i.e CRegistrar) for |
|
241 notifing CDiscoverer to start/ stop the discoveries process depending on the |
|
242 event(Backup start/end). The observer stub class is being used for the |
|
243 verification of the results. |
|
244 |
|
245 @SYMTestCaseID SYSLIB-ECOM-CT-0753 |
|
246 @SYMTestCaseDesc Tests for CBackupNotifier::HandleBackupOperationEventL function |
|
247 @SYMTestPriority High |
|
248 @SYMTestActions Setting the backup operation attributes before starting the backup operation. |
|
249 Suspend BackupNotifier observer. |
|
250 Reset backup operation attributes and stop the backup operation. |
|
251 Resume BackupNotifier observer. |
|
252 @SYMTestExpectedResults The test must not fail. |
|
253 @SYMREQ REQ0000 |
|
254 */ |
|
255 void CBackUpNotifierTest::HandleBackupOperationEventTestL() |
|
256 { |
|
257 test.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-0753 ")); |
|
258 TBackupOperationAttributes attribs; |
|
259 // Setting the backup operation attributes before making the event. |
|
260 attribs.iFileFlag=MBackupObserver::EReleaseLockNoAccess; |
|
261 attribs.iOperation=MBackupOperationObserver::EStart; |
|
262 |
|
263 // Start the backup operation |
|
264 TRAPD(err, iStateAccessor->HandleBackupOperationEventL(*iBackupNotifier, attribs)); |
|
265 ::LeaveIfErrNoMemory(err); |
|
266 test(err == KErrNone); |
|
267 // BackupNotifier observer should get suspended. The directory scanning process(in stub class) |
|
268 // should get stopped as backup is started. |
|
269 test(iObserverStub->iSuspended); |
|
270 |
|
271 // Resetting the backup operation attributes before making another event. |
|
272 attribs.iFileFlag=MBackupObserver::EReleaseLockNoAccess; |
|
273 attribs.iOperation=MBackupOperationObserver::EEnd; |
|
274 |
|
275 // Stop the backup operation |
|
276 TRAP(err, iStateAccessor->HandleBackupOperationEventL(*iBackupNotifier, attribs)); |
|
277 ::LeaveIfErrNoMemory(err); |
|
278 test(err == KErrNone); |
|
279 // BackupNotifier observer should get resumed. The directory scanning process(in stub class) |
|
280 // should get resumed as backup is over. |
|
281 test(!iObserverStub->iSuspended); |
|
282 } |
|
283 |
|
284 /** |
|
285 @SYMTestCaseID SYSLIB-ECOM-CT-0754 |
|
286 @SYMTestCaseDesc Register for notification test |
|
287 @SYMTestPriority High |
|
288 @SYMTestActions Tests by first unregistring(if already registerd) the Backup notifier for notifications |
|
289 and then by registering for notifications from the backup server. |
|
290 @SYMTestExpectedResults The test must not fail. |
|
291 @SYMREQ REQ0000 |
|
292 */ |
|
293 void CBackUpNotifierTest::RegisterForNotificationsTestL() |
|
294 { |
|
295 test.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-0754 ")); |
|
296 TBool isRegistered = iStateAccessor->IsRegisteredForNotifications(*iBackupNotifier); |
|
297 |
|
298 // Idealy it should got registered when the CBackupNotifier object is created. |
|
299 if(isRegistered) |
|
300 { |
|
301 TRAPD(err, iStateAccessor->DeregisterForNotifications(*iBackupNotifier)); |
|
302 ::LeaveIfErrNoMemory(err); |
|
303 test(err == KErrNone); |
|
304 } |
|
305 |
|
306 // Backup notifier should not be registerd for backup operation events. |
|
307 isRegistered = iStateAccessor->IsRegisteredForNotifications(*iBackupNotifier); |
|
308 test(!isRegistered); |
|
309 |
|
310 // Register the Backup Notifier for the backup operation events. |
|
311 TRAPD(err, iStateAccessor->RegisterForNotificationsL(*iBackupNotifier)); |
|
312 ::LeaveIfErrNoMemory(err); |
|
313 test(err == KErrNone); |
|
314 |
|
315 // Backup notifier should be registerd for backup operation events. |
|
316 isRegistered = iStateAccessor->IsRegisteredForNotifications(*iBackupNotifier); |
|
317 test(isRegistered); |
|
318 } |
|
319 |
|
320 /** |
|
321 @SYMTestCaseID SYSLIB-ECOM-CT-0755 |
|
322 @SYMTestCaseDesc Create and delete test of CBackUpNotifier |
|
323 @SYMTestPriority High |
|
324 @SYMTestActions Check for handle leak after deletion of object. |
|
325 @SYMTestExpectedResults The test must not fail. |
|
326 @SYMREQ REQ0000 |
|
327 */ |
|
328 LOCAL_C void CreateDeleteTestL() |
|
329 { |
|
330 test.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-0755 CreateDeleteTestL ")); |
|
331 // Set up for heap leak checking |
|
332 __UHEAP_MARK; |
|
333 |
|
334 // and leaking thread handles |
|
335 TInt startProcessHandleCount; |
|
336 TInt startThreadHandleCount; |
|
337 TInt endProcessHandleCount; |
|
338 TInt endThreadHandleCount; |
|
339 |
|
340 // Test Starts... |
|
341 RThread thisThread; |
|
342 thisThread.HandleCount(startProcessHandleCount, startThreadHandleCount); |
|
343 |
|
344 CBackUpNotifierTest* notifierTest = CBackUpNotifierTest::NewL(); |
|
345 |
|
346 test(notifierTest!=NULL); |
|
347 |
|
348 delete notifierTest; |
|
349 |
|
350 // Check for open handles |
|
351 thisThread.HandleCount(endProcessHandleCount, endThreadHandleCount); |
|
352 test(startThreadHandleCount == endThreadHandleCount); |
|
353 |
|
354 __UHEAP_MARKEND; |
|
355 } |
|
356 |
|
357 /** |
|
358 @SYMTestCaseID SYSLIB-ECOM-CT-0756 |
|
359 @SYMTestCaseDesc OOM Test for create and delete of CBackUpNotifier |
|
360 @SYMTestPriority High |
|
361 @SYMTestActions Check for handle leak after deletion of object. |
|
362 @SYMTestExpectedResults The test must not fail. |
|
363 @SYMREQ REQ0000 |
|
364 */ |
|
365 LOCAL_C void OOMCreateDeleteTest() |
|
366 { |
|
367 test.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-0756 OOM CreateDeleteTest ")); |
|
368 TInt err; |
|
369 TInt failAt = 1; |
|
370 __UNUSED_VAR(failAt); |
|
371 |
|
372 CBackUpNotifierTest* theTest = NULL; |
|
373 |
|
374 do |
|
375 { |
|
376 __UHEAP_MARK; |
|
377 // find out the number of open handles |
|
378 TInt startProcessHandleCount; |
|
379 TInt startThreadHandleCount; |
|
380 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount); |
|
381 |
|
382 __UHEAP_SETFAIL(RHeap::EDeterministic, failAt++); |
|
383 |
|
384 TRAP(err, theTest = CBackUpNotifierTest::NewL()); |
|
385 |
|
386 __UHEAP_SETFAIL(RHeap::ENone, 0); |
|
387 |
|
388 delete theTest; |
|
389 theTest = NULL; |
|
390 |
|
391 // check that no handles have leaked |
|
392 TInt endProcessHandleCount; |
|
393 TInt endThreadHandleCount; |
|
394 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount); |
|
395 |
|
396 test(startProcessHandleCount == endProcessHandleCount); |
|
397 test(startThreadHandleCount == endThreadHandleCount); |
|
398 |
|
399 __UHEAP_MARKEND; |
|
400 } |
|
401 while(err == KErrNoMemory); |
|
402 |
|
403 test.Printf(_L("- Succeeded at heap failure rate of %i\n"), failAt); |
|
404 test(err == KErrNone); |
|
405 } |
|
406 |
|
407 // Type definition for pointer to member function. |
|
408 // Used in calling the CBackUpNotifierTest member function for testing. |
|
409 typedef void (CBackUpNotifierTest::*ClassFuncPtrL) (void); |
|
410 |
|
411 /** |
|
412 @SYMTestCaseID SYSLIB-ECOM-CT-0757 |
|
413 @SYMTestCaseDesc Function to call all test functions |
|
414 @SYMTestPriority High |
|
415 @SYMTestActions Calls up test functions of CBackupNotifier,check that no handles have leaked |
|
416 @SYMTestExpectedResults The test must not fail. |
|
417 @SYMREQ REQ0000 |
|
418 */ |
|
419 /** |
|
420 Wrapper function to call all test functions |
|
421 @param testFunc pointer to test function |
|
422 @param aTestDesc test function name |
|
423 */ |
|
424 LOCAL_C void DoBasicTestL(ClassFuncPtrL testFuncL, const TDesC& aTestDesc) |
|
425 { |
|
426 test.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-0757 ")); |
|
427 test.Next(aTestDesc); |
|
428 |
|
429 __UHEAP_MARK; |
|
430 // find out the number of open handles |
|
431 TInt startProcessHandleCount; |
|
432 TInt startThreadHandleCount; |
|
433 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount); |
|
434 |
|
435 CBackUpNotifierTest* theTest = CBackUpNotifierTest::NewL(); |
|
436 CleanupStack::PushL(theTest); |
|
437 |
|
438 (theTest->*testFuncL)(); |
|
439 |
|
440 CleanupStack::PopAndDestroy(theTest); |
|
441 |
|
442 // check that no handles have leaked |
|
443 TInt endProcessHandleCount; |
|
444 TInt endThreadHandleCount; |
|
445 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount); |
|
446 |
|
447 test(startProcessHandleCount == endProcessHandleCount); |
|
448 test(startThreadHandleCount == endThreadHandleCount); |
|
449 |
|
450 __UHEAP_MARKEND; |
|
451 } |
|
452 |
|
453 /** |
|
454 @SYMTestCaseID SYSLIB-ECOM-CT-0758 |
|
455 @SYMTestCaseDesc Function to call all OOM test functions |
|
456 @SYMTestPriority High |
|
457 @SYMTestActions Calls up all OOM test function related to CBackupNotifier |
|
458 @SYMTestExpectedResults The test must not fail. |
|
459 @SYMREQ REQ0000 |
|
460 */ |
|
461 /** |
|
462 Wrapper function to call all OOM test functions |
|
463 @param testFuncL pointer to OOM test function |
|
464 @param aTestDesc test function name |
|
465 */ |
|
466 LOCAL_C void DoOOMTestL(ClassFuncPtrL testFuncL, const TDesC& aTestDesc) |
|
467 { |
|
468 test.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-0758 ")); |
|
469 test.Next(aTestDesc); |
|
470 |
|
471 TInt err; |
|
472 TInt tryCount = 0; |
|
473 do |
|
474 { |
|
475 __UHEAP_MARK; |
|
476 // find out the number of open handles |
|
477 TInt startProcessHandleCount; |
|
478 TInt startThreadHandleCount; |
|
479 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount); |
|
480 |
|
481 CBackUpNotifierTest* theTest = CBackUpNotifierTest::NewL(); |
|
482 CleanupStack::PushL(theTest); |
|
483 |
|
484 __UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount); |
|
485 |
|
486 TRAP(err, (theTest->*testFuncL)()); |
|
487 |
|
488 __UHEAP_SETFAIL(RHeap::ENone, 0); |
|
489 |
|
490 CleanupStack::PopAndDestroy(theTest); |
|
491 theTest = NULL; |
|
492 // check that no handles have leaked |
|
493 TInt endProcessHandleCount; |
|
494 TInt endThreadHandleCount; |
|
495 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount); |
|
496 |
|
497 test(startProcessHandleCount == endProcessHandleCount); |
|
498 test(startThreadHandleCount == endThreadHandleCount); |
|
499 |
|
500 __UHEAP_MARKEND; |
|
501 } while(err == KErrNoMemory); |
|
502 |
|
503 test(err == KErrNone); |
|
504 test.Printf(_L("- server succeeded at heap failure rate of %i\n"), tryCount); |
|
505 } |
|
506 |
|
507 LOCAL_C void DoTestsL() |
|
508 { |
|
509 __UHEAP_MARK; |
|
510 |
|
511 CreateDeleteTestL(); |
|
512 DoBasicTestL(&CBackUpNotifierTest::HandleBackupOperationEventTestL, _L("HandleBackupOperationEventL Test")); |
|
513 DoBasicTestL(&CBackUpNotifierTest::RegisterForNotificationsTestL, _L("RegisterForNotificationsL Test")); |
|
514 |
|
515 OOMCreateDeleteTest(); |
|
516 DoOOMTestL(&CBackUpNotifierTest::HandleBackupOperationEventTestL, _L("HandleBackupOperationEventL Test")); |
|
517 DoOOMTestL(&CBackUpNotifierTest::RegisterForNotificationsTestL, _L("RegisterForNotificationsL Test")); |
|
518 |
|
519 __UHEAP_MARKEND; |
|
520 } |
|
521 |
|
522 //Initialise the Active Scheduler |
|
523 LOCAL_C void SetupL() |
|
524 { |
|
525 // Construct and install the active scheduler |
|
526 TheActiveScheduler = new(ELeave)CActiveScheduler; |
|
527 CActiveScheduler::Install(TheActiveScheduler); |
|
528 } |
|
529 |
|
530 |
|
531 GLDEF_C TInt E32Main() |
|
532 { |
|
533 __UHEAP_MARK; |
|
534 |
|
535 test.Printf(_L("\n")); |
|
536 test.Title(); |
|
537 test.Start(_L("BackUp Notifier Tests")); |
|
538 |
|
539 TheTrapCleanup = CTrapCleanup::New(); |
|
540 |
|
541 TRAPD(err, SetupL()); |
|
542 test(err == KErrNone); |
|
543 |
|
544 TRAP(err, DoTestsL()); |
|
545 test(err == KErrNone); |
|
546 |
|
547 delete TheTrapCleanup; |
|
548 delete TheActiveScheduler; |
|
549 test.End(); |
|
550 test.Close(); |
|
551 |
|
552 __UHEAP_MARKEND; |
|
553 return(KErrNone); |
|
554 } |