summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_filterbroadcast.cpp
blob: 3ee63f988f1d7f4e04ecc7e8b65b75c4a0bad670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
* Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


#include <e32std.h>
#include <e32test.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntfldst.h>
#include <f32file.h>
#include "t_utils2.h"
#include "t_filterbroadcast.h"

//
// Constants.
//

_LIT(KTestName,"t_filterbroadcast");

_LIT(KDbFileName,"c:t_filterbroadcast.cdb");

_LIT(KViewName, "viewname");
const TInt KNumSmsContacts = 111;
// Time to wait for Views to be updated - 5s is very generous for any platform
const TInt KViewWaitTimeout = 5000000;


LOCAL_C void CleanupFilesL()
	{
    // delete the database file
	if (CContactDatabase::ContactDatabaseExistsL(KDbFileName) )
		{
		CContactDatabase::DeleteDatabaseL(KDbFileName);
		}
	}



//
// CTestConductor.
//

CTestConductor* CTestConductor::NewL(RTest& aTest)
	{
	CTestConductor* self=new(ELeave) CTestConductor(aTest);
	CleanupStack::PushL(self);
	self->ConstructL();
	self->RunTestsL();
	CleanupStack::Pop();
	return self;
	}

CTestConductor::~CTestConductor()
	{
	delete iDb;
	delete iRandomGenerator;
	}

CTestConductor::CTestConductor(RTest& aTest) : iTest(aTest)
	{
	}

void CTestConductor::ConstructL()
	{
	iDb=CContactDatabase::ReplaceL(KDbFileName);
	iRandomGenerator=CRandomContactGenerator::NewL();
	iRandomGenerator->SetDbL(*iDb);
	AddContactsL();
	}

void CTestConductor::AddContactL(TInt aBitwiseFilterType)
	{
	iTest.Printf(_L("Adding Contact"));
	iRandomGenerator->AddTypicalContactForFilterL(aBitwiseFilterType);
	}

void CTestConductor::AddContactsThatMatchFilterL(TInt aNumberOfContacts, TInt aFilter)
	{
	TInt counter = 0;
	for (counter = 0; counter < aNumberOfContacts; counter++)
		{
		iRandomGenerator->AddTypicalContactForFilterL(aFilter);
		}
	}
void CTestConductor::AddContactsL()
	{
	TInt bit=0;
	iTest.Printf(_L("Adding "));

	bit = CContactDatabase::ESmsable;
	AddContactsThatMatchFilterL(KNumSmsContacts, bit);
	}

void CTestConductor::RunTestsL()
	{
	CViewTester* tester = CViewTester::NewL(iTest, *iDb, *this);
	CleanupStack::PushL(tester);
	tester->StartTest();
	CActiveScheduler::Start();
	CleanupStack::PopAndDestroy(tester);

	// error from CViewTester ?
	User::LeaveIfError(iTestError);
	}


void CTestConductor::SetTestError(TInt aTestError)
	{
	iTestError = aTestError;
	}


//
// CViewTester.
//

CViewTester* CViewTester::NewL(RTest& aTest,CContactDatabase& aDb, CTestConductor& aTestConductor)
	{
	CViewTester* self=new(ELeave) CViewTester(aTest,aDb, aTestConductor);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
	}

CViewTester::~CViewTester()
	{
	iTimer.Cancel();
	iFilteredView->Close(*this);
	iNamedRemoteView->Close(*this);
	iSortOrder[0].Close();
	iSortOrder[1].Close();
	}

CViewTester::CViewTester(RTest& aTest,CContactDatabase& aDb,CTestConductor& aTestConductor)
	: CActive(EPriorityStandard),iTest(aTest),iDb(aDb),iTestConductor(aTestConductor)
	{
	CActiveScheduler::Add(this);
	}

void CViewTester::ConstructL()
	{
	iSortOrder[0].AppendL(KUidContactFieldGivenName);
	iSortOrder[0].AppendL(KUidContactFieldFamilyName);
	iSortOrder[0].AppendL(KUidContactFieldCompanyName);

	iSortOrder[1].AppendL(KUidContactFieldCompanyName);
	iSortOrder[1].AppendL(KUidContactFieldFamilyName);
	iSortOrder[1].AppendL(KUidContactFieldGivenName);

	iTimer.CreateLocal();
	}



void CViewTester::StartTest()
	{
	iState = ECreate_RemoteView;
	iRemoteViewEvent = TContactViewEvent::EUnavailable;
	iFilteredViewEvent = TContactViewEvent::EUnavailable;
	CallRunLAgain();
	}

void CViewTester::RunL()
	{
	switch (iState)
		{
		case ECreate_RemoteView:
			iTest.Next(_L("Named Remote View creation View Ready event"));

			iNamedRemoteView = CContactNamedRemoteView::NewL( *this, KViewName, iDb, iSortOrder[0], EContactsOnly );
			// Wait for view to be created
			iTimer.After( iStatus, KViewWaitTimeout );
			SetActive();
			NextState();
		break;


		// It is an error for the RTimer to trigger before the state has changed.
		case EWait_RemoteView:
			iTest.Printf(_L("Timeout waiting for Remote View to be created"));
			TRAP_IGNORE(CleanupFilesL() );
			iTest( EFalse); // failed
		break;

		case ECreate_FilteredView:
			iTest.Next(_L("Filtered View creation View Ready event"));

			iFilteredView = CContactFilteredView::NewL( *this, iDb, *iNamedRemoteView, CContactDatabase::ESmsable );
			// Wait for view to be created
			iTimer.After( iStatus, KViewWaitTimeout );
			SetActive();
			NextState();
		break;


		// It is an error for the RTimer to trigger before the state has changed.
		case EWait_FilteredView:
			iTest.Printf(_L("Timeout waiting for Filtered View to be created"));
			TRAP_IGNORE(CleanupFilesL() );
			iTest( EFalse ); // failed
		break;

		case EChangeSortOrder:
			iTest.Next(_L("Re-sort Remote View and Filtered View, wait for view events"));

			iNamedRemoteView->ChangeSortOrderL( iSortOrder[1] );
			// Wait for views to be resorted
			iTimer.After( iStatus, KViewWaitTimeout );
			SetActive();
			NextState();
		break;


		// It is an error for the RTimer to trigger before the state has changed.
		case EWait_SortViewEvents:
			// Give information on what went wrong:
			iTest.Printf(_L("Test Timeout waiting for Filtered and Remote View to be resorted:\n"));
			switch(iRemoteViewEvent)
				{
				case TContactViewEvent::EReady:
					iTest.Printf(_L("  Named Remote View; sort not started\n"));
				break;

				case TContactViewEvent::EUnavailable:
					iTest.Printf(_L("  Named Remote View; sort not finished\n"));
				break;

				case TContactViewEvent::ESortOrderChanged:
					// Remote View Sorted & Ready
				break;

				default:
					// error in test
					User::Invariant();
				}

			switch(iFilteredViewEvent)
				{
				case TContactViewEvent::EReady:
					iTest.Printf(_L("  Filtered View; sort not started\n"));
				break;

				case TContactViewEvent::EUnavailable:
					iTest.Printf(_L("  Filtered View; sort not finished\n"));
				break;

				case TContactViewEvent::ESortOrderChanged:
					// Filtered View Sorted & Ready
				break;

				default:
					// error in test
					User::Invariant();
				}
			TRAP_IGNORE(CleanupFilesL() );
			iTest( EFalse ); // failed
		break;

		case EFinished:
			CActiveScheduler::Stop(); // we're done.
		break;

		default:
			ASSERT(EFalse);
		break;
		}
	}


TInt CViewTester::RunError(TInt aError)
	{
	iTestConductor.SetTestError(aError);

	switch (iState)
		{
		case ECreate_RemoteView: iTest.Printf(_L("Test failed at step Create_RemoteView (%i) with error %i"), iState, aError); break;
		case EWait_RemoteView: iTest.Printf(_L("Test failed at step Wait_RemoteView (%i) with error %i"), iState, aError); break;
		case ECreate_FilteredView: iTest.Printf(_L("Test failed at step Create_FilteredView (%i) with error %i"), iState, aError); break;
		case EWait_FilteredView: iTest.Printf(_L("Test failed at step Wait_FilteredView (%i) with error %i"), iState, aError); break;
		case EChangeSortOrder: iTest.Printf(_L("Test failed at step ChangeSortOrder (%i) with error %i"), iState, aError); break;
		case EWait_SortViewEvents: iTest.Printf(_L("Test failed at step Wait_SortViewEvents (%i) with error %i"), iState, aError);
			// Give information on what went wrong:
			iTest.Printf(_L("Test Timeout waiting for Filtered and Remote View to be resorted:\n"));
			switch(iRemoteViewEvent)
				{
				case TContactViewEvent::EReady:
					iTest.Printf(_L("  Named Remote View; sort not started\n"));
					break;

				case TContactViewEvent::EUnavailable:
					iTest.Printf(_L("  Named Remote View; sort not finished\n"));
					break;

				case TContactViewEvent::ESortOrderChanged:
					// Remote View Sorted & Ready
					break;

				default:
					break;
				}

			switch(iFilteredViewEvent)
				{
				case TContactViewEvent::EReady:
					iTest.Printf(_L("  Filtered View; sort not started\n"));
					break;

				case TContactViewEvent::EUnavailable:
					iTest.Printf(_L("  Filtered View; sort not finished\n"));
					break;

				case TContactViewEvent::ESortOrderChanged:
					// Filtered View Sorted & Ready
					break;

				default:
					break;
				}
			break;

		case EFinished: iTest.Printf(_L("Test failed at step Finished (%i) with error %i"), iState, aError); break;

		default: iTest.Printf(_L("Test failed at step %i with error %i"), iState, aError);
		}

	// stop test
	CActiveScheduler::Stop();
	return KErrNone;
	}



void CViewTester::HandleContactViewEvent(const CContactViewBase& aView,const TContactViewEvent& aEvent)
/**
 * for each of the Remote and Filtered Views we wait for certain events
 * iState				  Event
 * EWait_RemoteView		  Named Remote View to become EReady
 * EWait_FilteredView	  Filtered View to become EReady
 * EWait_SortViewEvents   Named Remote View to become EUnavailable, and then ESortOrderChanged
 *                        Filtered View to become EUnavailable, and then ESortOrderChanged
 * The last expected event in each state calls NextState() for the test state machine.
 * Any other event, or any event in another test state, is an error.
 */
	{
	// view reference is correct?
	if(&aView != iNamedRemoteView && &aView != iFilteredView)
		{
		iTest.Printf(_L("test error: event for unknown View\n"));
		User::Invariant();
		}

	TBool expectedEvent = EFalse; // assume this is not the event we are looking for

	// is this the/an event we are expecting now?
	switch(aEvent.iEventType)
		{
		case TContactViewEvent::EReady:
			if(iState == EWait_RemoteView && &aView == iNamedRemoteView)
				{
				// Remote View is now ready, go to next test state
				iRemoteViewEvent = aEvent.iEventType;
				iTest.Printf(_L("event: Remote View ready\n"));
				expectedEvent = ETrue;
				NextState();
				}
			else if(iState == EWait_FilteredView && &aView == iFilteredView)
				{
				// Filter View is now ready, go to next test state
				iFilteredViewEvent = aEvent.iEventType;
				iTest.Printf(_L("event: Filtered View ready\n"));
				expectedEvent = ETrue;
				NextState();
				}
		break;

		case TContactViewEvent::EUnavailable:
			if(iState == EWait_SortViewEvents && &aView == iNamedRemoteView)
				{
				// Remote View, but correct event?
				if(iRemoteViewEvent == TContactViewEvent::EReady)
					{
					iRemoteViewEvent = aEvent.iEventType;
					iTest.Printf(_L("event: Remote View unavailable\n"));
					expectedEvent = ETrue;
					}
				}
			else if(iState == EWait_SortViewEvents && &aView == iFilteredView)
				{
				if(iFilteredViewEvent == TContactViewEvent::EReady)
					{
					iFilteredViewEvent = aEvent.iEventType;
					iTest.Printf(_L("event: Filtered View unavailable\n"));
					expectedEvent = ETrue;
					}
				}
		break;

		case TContactViewEvent::ESortOrderChanged:
			if(iState == EWait_SortViewEvents)
				{
				if(&aView == iNamedRemoteView && iRemoteViewEvent == TContactViewEvent::EUnavailable)
					{
					iRemoteViewEvent = aEvent.iEventType;
					iTest.Printf(_L("event: Remote View sort order changed\n"));
					expectedEvent = ETrue;
					}
				else if(&aView == iFilteredView && iFilteredViewEvent == TContactViewEvent::EUnavailable)
					{
					iFilteredViewEvent = aEvent.iEventType;
					iTest.Printf(_L("event: Filtered View sort order changed\n"));
					expectedEvent = ETrue;
					}

				// both views now Re-Sorted ?
				if(iRemoteViewEvent == TContactViewEvent::ESortOrderChanged &&
						iFilteredViewEvent == TContactViewEvent::ESortOrderChanged)
					{
					NextState();
					}
				}
		break;

		default:
			// unexpected event
		break;
		}


	if(!expectedEvent)
		{
		// valid View, but unexpected event
		iTest.Printf(_L("test error: unexpected View event\n"));
		TRAP_IGNORE(CleanupFilesL() );
		iTest( EFalse );
		}

	return;
	}



void CViewTester::NextState()
	{
	TState oldState(iState);

	iState = static_cast<TState>( static_cast<TInt>(iState) + 1 );

	// moving from an event with timeout?
	if(oldState == EWait_RemoteView || oldState == EWait_FilteredView || oldState == EWait_SortViewEvents)
		{
		// Cancel view wait timer, causes RunL to run
		iTimer.Cancel();
		}
	}

void CViewTester::CallRunLAgain()
	{
	TRequestStatus *pS=&iStatus;
	User::RequestComplete(pS,KErrNone);
	SetActive();
	}

void CViewTester::DoCancel()
	{
	}

/**

@SYMTestCaseID     PIM-T-FILTERBROADCAST-0001

*/

void doMainL()
	{
	RTest test( KTestName );
	CTestConductor* testConductor=NULL;
	RFs fs;
	User::LeaveIfError(fs.Connect());
	CleanupClosePushL(fs);

	CTestRegister* TempFiles = CTestRegister::NewLC();
	TempFiles->RegisterL(KDbFileName, EFileTypeCnt);

	test.Start(_L("@SYMTESTCaseID:PIM-T-FILTERBROADCAST-0001 "));


	__UHEAP_MARK;
	testConductor = CTestConductor::NewL(test);

	delete testConductor;
	__UHEAP_MARKEND;

	CleanupStack::PopAndDestroy(2);

	test.Printf(_L("\n"));
	test.End();
	test.Close();
	}
//
// Main.
//

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	// The dummy is just to get rid of linker warning LNK4089 when building for WINS
	TVolumeInfo Dummy;
	Dummy.iSize = 1;
	CActiveScheduler* scheduler=new CActiveScheduler;
	if (scheduler)
		{
		CActiveScheduler::Install(scheduler);
		CTrapCleanup* cleanup=CTrapCleanup::New();
		if (cleanup)
			{
			TRAP_IGNORE(doMainL() );
			// cleanup files even if doMainL() leaves
			TRAP_IGNORE(CleanupFilesL() );
			delete cleanup;
			}
		delete scheduler;
		}
	__UHEAP_MARKEND;
	return KErrNone;
    }