|
1 // Copyright (c) 2000-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 // |
|
15 |
|
16 #include <e32std.h> |
|
17 #include <e32test.h> |
|
18 #include <cntdb.h> |
|
19 #include <cntitem.h> |
|
20 #include <cntfldst.h> |
|
21 #include <f32file.h> |
|
22 #include "t_utils2.h" |
|
23 #include "t_filterbroadcast.h" |
|
24 |
|
25 // |
|
26 // Constants. |
|
27 // |
|
28 |
|
29 _LIT(KTestName,"t_filterbroadcast"); |
|
30 |
|
31 _LIT(KDbFileName,"c:t_filterbroadcast.cdb"); |
|
32 |
|
33 _LIT(KViewName, "viewname"); |
|
34 const TInt KNumSmsContacts = 111; |
|
35 // Time to wait for Views to be updated - 5s is very generous for any platform |
|
36 const TInt KViewWaitTimeout = 5000000; |
|
37 |
|
38 |
|
39 LOCAL_C void CleanupFilesL() |
|
40 { |
|
41 // delete the database file |
|
42 if (CContactDatabase::ContactDatabaseExistsL(KDbFileName) ) |
|
43 { |
|
44 CContactDatabase::DeleteDatabaseL(KDbFileName); |
|
45 } |
|
46 } |
|
47 |
|
48 |
|
49 |
|
50 // |
|
51 // CTestConductor. |
|
52 // |
|
53 |
|
54 CTestConductor* CTestConductor::NewL(RTest& aTest) |
|
55 { |
|
56 CTestConductor* self=new(ELeave) CTestConductor(aTest); |
|
57 CleanupStack::PushL(self); |
|
58 self->ConstructL(); |
|
59 self->RunTestsL(); |
|
60 CleanupStack::Pop(); |
|
61 return self; |
|
62 } |
|
63 |
|
64 CTestConductor::~CTestConductor() |
|
65 { |
|
66 delete iDb; |
|
67 delete iRandomGenerator; |
|
68 } |
|
69 |
|
70 CTestConductor::CTestConductor(RTest& aTest) : iTest(aTest) |
|
71 { |
|
72 } |
|
73 |
|
74 void CTestConductor::ConstructL() |
|
75 { |
|
76 iDb=CContactDatabase::ReplaceL(KDbFileName); |
|
77 iRandomGenerator=CRandomContactGenerator::NewL(); |
|
78 iRandomGenerator->SetDbL(*iDb); |
|
79 AddContactsL(); |
|
80 } |
|
81 |
|
82 void CTestConductor::AddContactL(TInt aBitwiseFilterType) |
|
83 { |
|
84 iTest.Printf(_L("Adding Contact")); |
|
85 iRandomGenerator->AddTypicalContactForFilterL(aBitwiseFilterType); |
|
86 } |
|
87 |
|
88 void CTestConductor::AddContactsThatMatchFilterL(TInt aNumberOfContacts, TInt aFilter) |
|
89 { |
|
90 TInt counter = 0; |
|
91 for (counter = 0; counter < aNumberOfContacts; counter++) |
|
92 { |
|
93 iRandomGenerator->AddTypicalContactForFilterL(aFilter); |
|
94 } |
|
95 } |
|
96 void CTestConductor::AddContactsL() |
|
97 { |
|
98 TInt bit=0; |
|
99 iTest.Printf(_L("Adding ")); |
|
100 |
|
101 bit = CContactDatabase::ESmsable; |
|
102 AddContactsThatMatchFilterL(KNumSmsContacts, bit); |
|
103 } |
|
104 |
|
105 void CTestConductor::RunTestsL() |
|
106 { |
|
107 CViewTester* tester = CViewTester::NewL(iTest, *iDb, *this); |
|
108 CleanupStack::PushL(tester); |
|
109 tester->StartTest(); |
|
110 CActiveScheduler::Start(); |
|
111 CleanupStack::PopAndDestroy(tester); |
|
112 |
|
113 // error from CViewTester ? |
|
114 User::LeaveIfError(iTestError); |
|
115 } |
|
116 |
|
117 |
|
118 void CTestConductor::SetTestError(TInt aTestError) |
|
119 { |
|
120 iTestError = aTestError; |
|
121 } |
|
122 |
|
123 |
|
124 // |
|
125 // CViewTester. |
|
126 // |
|
127 |
|
128 CViewTester* CViewTester::NewL(RTest& aTest,CContactDatabase& aDb, CTestConductor& aTestConductor) |
|
129 { |
|
130 CViewTester* self=new(ELeave) CViewTester(aTest,aDb, aTestConductor); |
|
131 CleanupStack::PushL(self); |
|
132 self->ConstructL(); |
|
133 CleanupStack::Pop(); |
|
134 return self; |
|
135 } |
|
136 |
|
137 CViewTester::~CViewTester() |
|
138 { |
|
139 iTimer.Cancel(); |
|
140 iFilteredView->Close(*this); |
|
141 iNamedRemoteView->Close(*this); |
|
142 iSortOrder[0].Close(); |
|
143 iSortOrder[1].Close(); |
|
144 } |
|
145 |
|
146 CViewTester::CViewTester(RTest& aTest,CContactDatabase& aDb,CTestConductor& aTestConductor) |
|
147 : CActive(EPriorityStandard),iTest(aTest),iDb(aDb),iTestConductor(aTestConductor) |
|
148 { |
|
149 CActiveScheduler::Add(this); |
|
150 } |
|
151 |
|
152 void CViewTester::ConstructL() |
|
153 { |
|
154 iSortOrder[0].AppendL(KUidContactFieldGivenName); |
|
155 iSortOrder[0].AppendL(KUidContactFieldFamilyName); |
|
156 iSortOrder[0].AppendL(KUidContactFieldCompanyName); |
|
157 |
|
158 iSortOrder[1].AppendL(KUidContactFieldCompanyName); |
|
159 iSortOrder[1].AppendL(KUidContactFieldFamilyName); |
|
160 iSortOrder[1].AppendL(KUidContactFieldGivenName); |
|
161 |
|
162 iTimer.CreateLocal(); |
|
163 } |
|
164 |
|
165 |
|
166 |
|
167 void CViewTester::StartTest() |
|
168 { |
|
169 iState = ECreate_RemoteView; |
|
170 iRemoteViewEvent = TContactViewEvent::EUnavailable; |
|
171 iFilteredViewEvent = TContactViewEvent::EUnavailable; |
|
172 CallRunLAgain(); |
|
173 } |
|
174 |
|
175 void CViewTester::RunL() |
|
176 { |
|
177 switch (iState) |
|
178 { |
|
179 case ECreate_RemoteView: |
|
180 iTest.Next(_L("Named Remote View creation View Ready event")); |
|
181 |
|
182 iNamedRemoteView = CContactNamedRemoteView::NewL( *this, KViewName, iDb, iSortOrder[0], EContactsOnly ); |
|
183 // Wait for view to be created |
|
184 iTimer.After( iStatus, KViewWaitTimeout ); |
|
185 SetActive(); |
|
186 NextState(); |
|
187 break; |
|
188 |
|
189 |
|
190 // It is an error for the RTimer to trigger before the state has changed. |
|
191 case EWait_RemoteView: |
|
192 iTest.Printf(_L("Timeout waiting for Remote View to be created")); |
|
193 TRAP_IGNORE(CleanupFilesL() ); |
|
194 iTest( EFalse); // failed |
|
195 break; |
|
196 |
|
197 case ECreate_FilteredView: |
|
198 iTest.Next(_L("Filtered View creation View Ready event")); |
|
199 |
|
200 iFilteredView = CContactFilteredView::NewL( *this, iDb, *iNamedRemoteView, CContactDatabase::ESmsable ); |
|
201 // Wait for view to be created |
|
202 iTimer.After( iStatus, KViewWaitTimeout ); |
|
203 SetActive(); |
|
204 NextState(); |
|
205 break; |
|
206 |
|
207 |
|
208 // It is an error for the RTimer to trigger before the state has changed. |
|
209 case EWait_FilteredView: |
|
210 iTest.Printf(_L("Timeout waiting for Filtered View to be created")); |
|
211 TRAP_IGNORE(CleanupFilesL() ); |
|
212 iTest( EFalse ); // failed |
|
213 break; |
|
214 |
|
215 case EChangeSortOrder: |
|
216 iTest.Next(_L("Re-sort Remote View and Filtered View, wait for view events")); |
|
217 |
|
218 iNamedRemoteView->ChangeSortOrderL( iSortOrder[1] ); |
|
219 // Wait for views to be resorted |
|
220 iTimer.After( iStatus, KViewWaitTimeout ); |
|
221 SetActive(); |
|
222 NextState(); |
|
223 break; |
|
224 |
|
225 |
|
226 // It is an error for the RTimer to trigger before the state has changed. |
|
227 case EWait_SortViewEvents: |
|
228 // Give information on what went wrong: |
|
229 iTest.Printf(_L("Test Timeout waiting for Filtered and Remote View to be resorted:\n")); |
|
230 switch(iRemoteViewEvent) |
|
231 { |
|
232 case TContactViewEvent::EReady: |
|
233 iTest.Printf(_L(" Named Remote View; sort not started\n")); |
|
234 break; |
|
235 |
|
236 case TContactViewEvent::EUnavailable: |
|
237 iTest.Printf(_L(" Named Remote View; sort not finished\n")); |
|
238 break; |
|
239 |
|
240 case TContactViewEvent::ESortOrderChanged: |
|
241 // Remote View Sorted & Ready |
|
242 break; |
|
243 |
|
244 default: |
|
245 // error in test |
|
246 User::Invariant(); |
|
247 } |
|
248 |
|
249 switch(iFilteredViewEvent) |
|
250 { |
|
251 case TContactViewEvent::EReady: |
|
252 iTest.Printf(_L(" Filtered View; sort not started\n")); |
|
253 break; |
|
254 |
|
255 case TContactViewEvent::EUnavailable: |
|
256 iTest.Printf(_L(" Filtered View; sort not finished\n")); |
|
257 break; |
|
258 |
|
259 case TContactViewEvent::ESortOrderChanged: |
|
260 // Filtered View Sorted & Ready |
|
261 break; |
|
262 |
|
263 default: |
|
264 // error in test |
|
265 User::Invariant(); |
|
266 } |
|
267 TRAP_IGNORE(CleanupFilesL() ); |
|
268 iTest( EFalse ); // failed |
|
269 break; |
|
270 |
|
271 case EFinished: |
|
272 CActiveScheduler::Stop(); // we're done. |
|
273 break; |
|
274 |
|
275 default: |
|
276 ASSERT(EFalse); |
|
277 break; |
|
278 } |
|
279 } |
|
280 |
|
281 |
|
282 TInt CViewTester::RunError(TInt aError) |
|
283 { |
|
284 iTestConductor.SetTestError(aError); |
|
285 |
|
286 switch (iState) |
|
287 { |
|
288 case ECreate_RemoteView: iTest.Printf(_L("Test failed at step Create_RemoteView (%i) with error %i"), iState, aError); break; |
|
289 case EWait_RemoteView: iTest.Printf(_L("Test failed at step Wait_RemoteView (%i) with error %i"), iState, aError); break; |
|
290 case ECreate_FilteredView: iTest.Printf(_L("Test failed at step Create_FilteredView (%i) with error %i"), iState, aError); break; |
|
291 case EWait_FilteredView: iTest.Printf(_L("Test failed at step Wait_FilteredView (%i) with error %i"), iState, aError); break; |
|
292 case EChangeSortOrder: iTest.Printf(_L("Test failed at step ChangeSortOrder (%i) with error %i"), iState, aError); break; |
|
293 case EWait_SortViewEvents: iTest.Printf(_L("Test failed at step Wait_SortViewEvents (%i) with error %i"), iState, aError); |
|
294 // Give information on what went wrong: |
|
295 iTest.Printf(_L("Test Timeout waiting for Filtered and Remote View to be resorted:\n")); |
|
296 switch(iRemoteViewEvent) |
|
297 { |
|
298 case TContactViewEvent::EReady: |
|
299 iTest.Printf(_L(" Named Remote View; sort not started\n")); |
|
300 break; |
|
301 |
|
302 case TContactViewEvent::EUnavailable: |
|
303 iTest.Printf(_L(" Named Remote View; sort not finished\n")); |
|
304 break; |
|
305 |
|
306 case TContactViewEvent::ESortOrderChanged: |
|
307 // Remote View Sorted & Ready |
|
308 break; |
|
309 |
|
310 default: |
|
311 break; |
|
312 } |
|
313 |
|
314 switch(iFilteredViewEvent) |
|
315 { |
|
316 case TContactViewEvent::EReady: |
|
317 iTest.Printf(_L(" Filtered View; sort not started\n")); |
|
318 break; |
|
319 |
|
320 case TContactViewEvent::EUnavailable: |
|
321 iTest.Printf(_L(" Filtered View; sort not finished\n")); |
|
322 break; |
|
323 |
|
324 case TContactViewEvent::ESortOrderChanged: |
|
325 // Filtered View Sorted & Ready |
|
326 break; |
|
327 |
|
328 default: |
|
329 break; |
|
330 } |
|
331 break; |
|
332 |
|
333 case EFinished: iTest.Printf(_L("Test failed at step Finished (%i) with error %i"), iState, aError); break; |
|
334 |
|
335 default: iTest.Printf(_L("Test failed at step %i with error %i"), iState, aError); |
|
336 } |
|
337 |
|
338 // stop test |
|
339 CActiveScheduler::Stop(); |
|
340 return KErrNone; |
|
341 } |
|
342 |
|
343 |
|
344 |
|
345 void CViewTester::HandleContactViewEvent(const CContactViewBase& aView,const TContactViewEvent& aEvent) |
|
346 /** |
|
347 * for each of the Remote and Filtered Views we wait for certain events |
|
348 * iState Event |
|
349 * EWait_RemoteView Named Remote View to become EReady |
|
350 * EWait_FilteredView Filtered View to become EReady |
|
351 * EWait_SortViewEvents Named Remote View to become EUnavailable, and then ESortOrderChanged |
|
352 * Filtered View to become EUnavailable, and then ESortOrderChanged |
|
353 * The last expected event in each state calls NextState() for the test state machine. |
|
354 * Any other event, or any event in another test state, is an error. |
|
355 */ |
|
356 { |
|
357 // view reference is correct? |
|
358 if(&aView != iNamedRemoteView && &aView != iFilteredView) |
|
359 { |
|
360 iTest.Printf(_L("test error: event for unknown View\n")); |
|
361 User::Invariant(); |
|
362 } |
|
363 |
|
364 TBool expectedEvent = EFalse; // assume this is not the event we are looking for |
|
365 |
|
366 // is this the/an event we are expecting now? |
|
367 switch(aEvent.iEventType) |
|
368 { |
|
369 case TContactViewEvent::EReady: |
|
370 if(iState == EWait_RemoteView && &aView == iNamedRemoteView) |
|
371 { |
|
372 // Remote View is now ready, go to next test state |
|
373 iRemoteViewEvent = aEvent.iEventType; |
|
374 iTest.Printf(_L("event: Remote View ready\n")); |
|
375 expectedEvent = ETrue; |
|
376 NextState(); |
|
377 } |
|
378 else if(iState == EWait_FilteredView && &aView == iFilteredView) |
|
379 { |
|
380 // Filter View is now ready, go to next test state |
|
381 iFilteredViewEvent = aEvent.iEventType; |
|
382 iTest.Printf(_L("event: Filtered View ready\n")); |
|
383 expectedEvent = ETrue; |
|
384 NextState(); |
|
385 } |
|
386 break; |
|
387 |
|
388 case TContactViewEvent::EUnavailable: |
|
389 if(iState == EWait_SortViewEvents && &aView == iNamedRemoteView) |
|
390 { |
|
391 // Remote View, but correct event? |
|
392 if(iRemoteViewEvent == TContactViewEvent::EReady) |
|
393 { |
|
394 iRemoteViewEvent = aEvent.iEventType; |
|
395 iTest.Printf(_L("event: Remote View unavailable\n")); |
|
396 expectedEvent = ETrue; |
|
397 } |
|
398 } |
|
399 else if(iState == EWait_SortViewEvents && &aView == iFilteredView) |
|
400 { |
|
401 if(iFilteredViewEvent == TContactViewEvent::EReady) |
|
402 { |
|
403 iFilteredViewEvent = aEvent.iEventType; |
|
404 iTest.Printf(_L("event: Filtered View unavailable\n")); |
|
405 expectedEvent = ETrue; |
|
406 } |
|
407 } |
|
408 break; |
|
409 |
|
410 case TContactViewEvent::ESortOrderChanged: |
|
411 if(iState == EWait_SortViewEvents) |
|
412 { |
|
413 if(&aView == iNamedRemoteView && iRemoteViewEvent == TContactViewEvent::EUnavailable) |
|
414 { |
|
415 iRemoteViewEvent = aEvent.iEventType; |
|
416 iTest.Printf(_L("event: Remote View sort order changed\n")); |
|
417 expectedEvent = ETrue; |
|
418 } |
|
419 else if(&aView == iFilteredView && iFilteredViewEvent == TContactViewEvent::EUnavailable) |
|
420 { |
|
421 iFilteredViewEvent = aEvent.iEventType; |
|
422 iTest.Printf(_L("event: Filtered View sort order changed\n")); |
|
423 expectedEvent = ETrue; |
|
424 } |
|
425 |
|
426 // both views now Re-Sorted ? |
|
427 if(iRemoteViewEvent == TContactViewEvent::ESortOrderChanged && |
|
428 iFilteredViewEvent == TContactViewEvent::ESortOrderChanged) |
|
429 { |
|
430 NextState(); |
|
431 } |
|
432 } |
|
433 break; |
|
434 |
|
435 default: |
|
436 // unexpected event |
|
437 break; |
|
438 } |
|
439 |
|
440 |
|
441 if(!expectedEvent) |
|
442 { |
|
443 // valid View, but unexpected event |
|
444 iTest.Printf(_L("test error: unexpected View event\n")); |
|
445 TRAP_IGNORE(CleanupFilesL() ); |
|
446 iTest( EFalse ); |
|
447 } |
|
448 |
|
449 return; |
|
450 } |
|
451 |
|
452 |
|
453 |
|
454 void CViewTester::NextState() |
|
455 { |
|
456 TState oldState(iState); |
|
457 |
|
458 iState = static_cast<TState>( static_cast<TInt>(iState) + 1 ); |
|
459 |
|
460 // moving from an event with timeout? |
|
461 if(oldState == EWait_RemoteView || oldState == EWait_FilteredView || oldState == EWait_SortViewEvents) |
|
462 { |
|
463 // Cancel view wait timer, causes RunL to run |
|
464 iTimer.Cancel(); |
|
465 } |
|
466 } |
|
467 |
|
468 void CViewTester::CallRunLAgain() |
|
469 { |
|
470 TRequestStatus *pS=&iStatus; |
|
471 User::RequestComplete(pS,KErrNone); |
|
472 SetActive(); |
|
473 } |
|
474 |
|
475 void CViewTester::DoCancel() |
|
476 { |
|
477 } |
|
478 |
|
479 /** |
|
480 |
|
481 @SYMTestCaseID PIM-T-FILTERBROADCAST-0001 |
|
482 |
|
483 */ |
|
484 |
|
485 void doMainL() |
|
486 { |
|
487 RTest test( KTestName ); |
|
488 CTestConductor* testConductor=NULL; |
|
489 RFs fs; |
|
490 User::LeaveIfError(fs.Connect()); |
|
491 CleanupClosePushL(fs); |
|
492 |
|
493 CTestRegister* TempFiles = CTestRegister::NewLC(); |
|
494 TempFiles->RegisterL(KDbFileName, EFileTypeCnt); |
|
495 |
|
496 test.Start(_L("@SYMTESTCaseID:PIM-T-FILTERBROADCAST-0001 ")); |
|
497 |
|
498 |
|
499 __UHEAP_MARK; |
|
500 testConductor = CTestConductor::NewL(test); |
|
501 |
|
502 delete testConductor; |
|
503 __UHEAP_MARKEND; |
|
504 |
|
505 CleanupStack::PopAndDestroy(2); |
|
506 |
|
507 test.Printf(_L("\n")); |
|
508 test.End(); |
|
509 test.Close(); |
|
510 } |
|
511 // |
|
512 // Main. |
|
513 // |
|
514 |
|
515 GLDEF_C TInt E32Main() |
|
516 { |
|
517 __UHEAP_MARK; |
|
518 // The dummy is just to get rid of linker warning LNK4089 when building for WINS |
|
519 TVolumeInfo Dummy; |
|
520 Dummy.iSize = 1; |
|
521 CActiveScheduler* scheduler=new CActiveScheduler; |
|
522 if (scheduler) |
|
523 { |
|
524 CActiveScheduler::Install(scheduler); |
|
525 CTrapCleanup* cleanup=CTrapCleanup::New(); |
|
526 if (cleanup) |
|
527 { |
|
528 TRAP_IGNORE(doMainL() ); |
|
529 // cleanup files even if doMainL() leaves |
|
530 TRAP_IGNORE(CleanupFilesL() ); |
|
531 delete cleanup; |
|
532 } |
|
533 delete scheduler; |
|
534 } |
|
535 __UHEAP_MARKEND; |
|
536 return KErrNone; |
|
537 } |