summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_viewevents.cpp
blob: 86b96228878e429d61896a5bdfe513a9627af0bd (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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* 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 "t_utils2.h"
#include "t_viewevents.h"

//
// Constants.
//
_LIT8(KSortPlugin, "application.vnd.symbian.com/contacts-reverse-sort"); //SimpleSortPlugin DLL Unique name

_LIT(KTestName,"t_viewevents");
_LIT(KLogFileName,"t_viewevents.log");

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

_LIT(KRemoteViewName,"RemoteView");
_LIT(KGroupOneName,"GroupOne");
_LIT(KGroupTwoName,"GroupTwo");

const TInt KContactIdToNotAssignToGroup2 = 5;

LOCAL_D RTest test(KTestName);


//
// CTestConductor.
//

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

CTestConductor::~CTestConductor()
	{
	delete iLog;
	delete iDb;
	delete iRandomGenerator;
    TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KDbFileName));
	iFs.Close();
	}

CTestConductor::CTestConductor() 
	{
	}

void CTestConductor::ConstructL()
	{
	User::LeaveIfError(iFs.Connect());
	iLog=CLog::NewL(test,KLogFileName);
	iDb=CContactDatabase::ReplaceL(KDbFileName);
	iRandomGenerator=CRandomContactGenerator::NewL();
	iRandomGenerator->SetDbL(*iDb);
	AddContactsL();
	}

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

void CTestConductor::AddContactsL()
	{
	iTotalContacts=0;
	test.Printf(_L("Adding "));
	TInt ii;
	for (ii=0;ii<5;ii++)
		{
		TInt bit=0;
		bit |= CContactDatabase::ESmsable;
		iRandomGenerator->AddTypicalContactForFilterL(bit);
		iTotalContacts++;
		}


	for (ii=0;ii<5;ii++)
		{
		TInt bit=0;
		bit |= CContactDatabase::ELandLine;
		iRandomGenerator->AddTypicalContactForFilterL(bit);
		iTotalContacts++;
		}

	}

void CTestConductor::RunTestsL()
	{
	CViewTester* tester=CViewTester::NewL(*iLog,*iDb,this);
	CleanupStack::PushL(tester);
	CActiveScheduler::Start();
	CleanupStack::PopAndDestroy(); // tester.
	}

void CTestConductor::SetTestError(TInt aTestError)
	{
	// error from tester
	iTestError = aTestError;
	}

//
// CViewTester.
//

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



CViewTester::CViewTester(CLog& aLog,CContactDatabase& aDb,CTestConductor* aTestConductor)
	: CActive(EPriorityStandard),iLog(aLog),iDb(aDb),iTestConductor(aTestConductor)
	{
	CActiveScheduler::Add(this);
	}

CViewTester::~CViewTester()
	{
	DestructRemoteView();
	DestructFilterView();
	DestructGroupView();
	DestructGroupView2();
	delete iGroup;
	iGroup = NULL;
    delete iGroup2;
    iGroup2 = NULL;	
	iViewSortOrder.Close();
	iLocalViewResults.Close();
	iReceivedResults.Close();
	}

void CViewTester::ConstructL()
	{
	iViewSortOrder.AppendL(KUidContactFieldGivenName);
	iViewSortOrder.AppendL(KUidContactFieldFamilyName);
	iViewSortOrder.AppendL(KUidContactFieldCompanyName);
	iGroup = static_cast<CContactGroup*>(iDb.CreateContactGroupL(KGroupOneName));
	AssignAllContactsToGroupL();
    iGroup2 = static_cast<CContactGroup*>(iDb.CreateContactGroupL(KGroupTwoName));
   	AssignContactsToGroup2L();

	iTestState = EStartingTests;
	NextTest();
	}


void CViewTester::NextTest()
	{
	iTestState = static_cast<TTest>( static_cast<TInt>(iTestState) + 1 );
	TRequestStatus *pS=&iStatus;
	User::RequestComplete(pS,KErrNone);
	SetActive();
	}


void CViewTester::CompareRecordedResultWithExpected()
	{
	TInt counter;
	TInt max;
	TContactViewEvent::TEventType expected;
	TContactViewEvent::TEventType actual;

	test( iLocalViewResults.Count() == iReceivedResults.Count() );
	max = iLocalViewResults.Count();

	for (counter = 0; counter < max; counter++)
		{
		expected = static_cast<TContactViewEvent::TEventType>(iLocalViewResults[counter]);
		actual = static_cast<TContactViewEvent::TEventType>(iReceivedResults[counter]);
		test( expected == actual );
		}

	iReceivedResults.Reset();
	}




void CViewTester::RunL()
	{
	switch (iTestState)
		{
		//
		// Remote View
		//
		case EConstructRemoteView:
			iLog.LogLine ( _L("=== Creating remote view"));
			ConstructRemoteViewL();
			break;
		case ERemoteViewSortOrderChange:
			iLog.LogLine ( _L("=== Changing sort order and recording application messages"));
			RemoteViewSortOrderChangeL();
			break;

		//
		// Filter View
		//
		case EConstructFilterView:
			iLog.LogLine ( _L("=== Creating Filter View"));
			ConstructFilterViewL();
			break;
		case EFilterViewSortOrderChange:
			iLog.LogLine ( _L("=== Changing sort order and processing application messages"));
			FilterViewSortOrderChangeL();
			break;

		//
		// Group View
		//
		case EConstructGroupView:
			iLog.LogLine ( _L("=== Creating Group View"));
			ConstructGroupViewL();
			break;
		case EGroupViewSortOrderChange:
			iLog.LogLine ( _L("=== Changing sort order and processing application messages"));
			GroupViewSortOrderChangeL();
			break;

        //
        // Group2 View
        //
        case EConstructGroupView2:
            iLog.LogLine(_L("=== Creating Group2 View"));
            ConstructGroupView2L();
            break;
        case EGroupView2DeleteContactEvent:
            iLog.LogLine(_L("=== Deleting the only contact in group 2"));
            GroupView2DeleteOnlyContactL();
            break;

        //
        // Remote View
        //
		case ERemoteViewPlugin2:
		case ERemoteViewPlugin:
			iLog.LogLine ( _L("=== Creating remote view plugin"));
			ConstructRemoteViewL(KSortPlugin);
			break;
			
		//
		// End Tests
		//
		case EEndTest:

			iLog.LogLine(_L("==== Finished Tests"));
			CActiveScheduler::Stop();
			break;

		default:
			ASSERT(EFalse);
			break;
		}
	}

TInt CViewTester::RunError(TInt aError)
	{
	// propagate error
	iTestConductor->SetTestError(aError);

	switch (iTestState)
		{
		case EConstructRemoteView: test.Printf(_L("Test failed at step ConstructRemoteView (%i), with error %i"), iTestState, aError); break;
		case ERemoteViewSortOrderChange: test.Printf(_L("Test failed at step RemoteViewSortOrderChange (%i), with error %i"), iTestState, aError); break;
		case EConstructFilterView: test.Printf(_L("Test failed at step ConstructFilterView (%i), with error %i"), iTestState, aError); break;
		case EFilterViewSortOrderChange: test.Printf(_L("Test failed at step FilterViewSortOrderChange (%i), with error %i"), iTestState, aError); break;
		case EConstructGroupView: test.Printf(_L("Test failed at step ConstructGroupView (%i), with error %i"), iTestState, aError); break;
		case EGroupViewSortOrderChange: test.Printf(_L("Test failed at step GroupViewSortOrderChange (%i), with error %i"), iTestState, aError); break;
        case EConstructGroupView2: test.Printf(_L("Test failed at step ConstructGroupView2 (%i), with error %i"), iTestState, aError); break;
        case EGroupView2DeleteContactEvent: test.Printf(_L("Test failed at step EGroupView2DeleteContactEvent (%i), with error %i"), iTestState, aError); break;
		case ERemoteViewPlugin2: test.Printf(_L("Test failed at step RemoteViewPlugin2 (%i), with error %i"), iTestState, aError); break;
		case ERemoteViewPlugin: test.Printf(_L("Test failed at step RemoteViewPlugin (%i), with error %i"), iTestState, aError); break;
		case EEndTest: test.Printf(_L("Test failed at step EndTest (%i), with error %i"), iTestState, aError); break;

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

	CActiveScheduler::Stop();
	return KErrNone;
	}


void CViewTester::HandleContactViewEvent(const CContactViewBase& aView,const TContactViewEvent& aEvent)
	{
	static TInt NumChanged = 0;
	switch (iTestState)
		{
		//
		// Remote View
		//
		case EConstructRemoteView:
			if ( (&aView == iRemoteView) && (aEvent.iEventType == TContactViewEvent::EReady) )
				{
				NextTest();
				}
			break;

		case ERemoteViewSortOrderChange:
			if ( &aView == iRemoteView )
				{
				iLocalViewResults.Append( aEvent.iEventType );
				if ( aEvent.iEventType == TContactViewEvent::ESortOrderChanged )
					{
					NextTest();
					}
				}
			break;

		//
		// Filter View
		//
		case EConstructFilterView:
			if ( (&aView == iFilteredView) && (aEvent.iEventType == TContactViewEvent::EReady) )
				{
				NextTest();
				}
			break;

		case EFilterViewSortOrderChange:
			if ( &aView == iFilteredView )
				{
				iReceivedResults.Append( aEvent.iEventType );
				if ( aEvent.iEventType == TContactViewEvent::ESortOrderChanged )
					{
					CompareRecordedResultWithExpected();
					NextTest();
					}
				}
			break;

		//
		// Group View
		//
		case EConstructGroupView:
			if ( (&aView == iGroupView) && (aEvent.iEventType == TContactViewEvent::EReady) )
				{
				NumChanged = 0;
				NextTest();
				}
			break;

		case EGroupViewSortOrderChange:
			if ( aEvent.iEventType == TContactViewEvent::ESortOrderChanged)
				{
				++NumChanged;
				}
			if ( &aView == iGroupView )
				{
				iReceivedResults.Append( aEvent.iEventType );
				}
			if ( NumChanged == 3 )
				{
				CompareRecordedResultWithExpected();
				NextTest();
				}
			break;

        //
        // Group2 View
        //
        case EConstructGroupView2:
            if ((&aView == iGroupView2) && (aEvent.iEventType == TContactViewEvent::EReady))
                {
                NextTest();
                }
            break;
       
        case EGroupView2DeleteContactEvent:
            if ((&aView == iGroupView2))
                {
                // When a contact is deleted, we expect a EGroupChanged event
                // followed by a EItemRemoved. The iInt in the EItemRemoved
                // should be the index of the contact in the group view and
                // not its index in the underlying view.
                switch (aEvent.iEventType)
                    {
                    case TContactViewEvent::EGroupChanged:
                        break;
                    case TContactViewEvent::EItemRemoved:
                        {
                        // Check that the index in the event is the index of
                        // the contact in the group view and NOT the index
                        // of the contact in the global database. There is
                        // only one item in the group, so its index is 0.
                        test(aEvent.iInt == iContactToDeleteMapping.iMapping);
                        test(aEvent.iContactId == iContactToDeleteMapping.iId);
                        NextTest();                       
                        }
                        break;
                    default:
                        test(EFalse);
                    }
                }
            break;

        //
        // Remote View Plugin
        //
		case ERemoteViewPlugin:
			if ( aEvent.iEventType == TContactViewEvent::EReady)
				{
				NextTest();
				}
			break;
			
		case ERemoteViewPlugin2:
			if (aEvent.iEventType == TContactViewEvent::EReady)
				{
				/* test to make sure both these two view2 use the same remote view
					We can't *really* test this without a lot of painful effort. So we test what we can:
					that the two plugins are identical. 
					To really confirm this, check the logs to ensure that two example sort
					plugins were created.
					
					There should be two instantiated on the client side and one on the server side.
					Thus a total of three.
				*/
				TUid firstID = iRemoteView2->GetViewSortPluginImplUid();
				TUid secondID = iRemoteView3->GetViewSortPluginImplUid();
				test(firstID == secondID); // make sure both use the same plugin.


				CArrayFix<TContactItemId >* ids(NULL);
				CArrayFix<TContactItemId >* ids2(NULL);
				TRAPD(err, 
					ids = OrderOfContactsLC(*iRemoteView2);
					ids2 = OrderOfContactsLC(*iRemoteView3);
					// Must pop these here because they're on this trap's cleanup stack.
					// Don't need to re-push them as nothing below can leave.
					CleanupStack::Pop(2, ids); // and ids2
					)
				test(err == KErrNone);
				
				TInt size = ids->Count();
				test(ids2->Count() == size);
				for (TInt i =0; i < size; ++i) 
					{
					test(ids->At(i) == ids2->At(i));
					}

				// cleanup id arrays -- don't need Reset() as only holding TClasses
				delete ids;
				delete ids2;
	
				NextTest();

				}
			break;

		//
		// End Tests
		//
		case EEndTest:
		default:
			test(EFalse);
			break;
		}
	}

void CViewTester::AssignAllContactsToGroupL()
	{
	TInt counter = 0;
	for ( counter = 0; counter < 10; counter++)
		{
		iDb.AddContactToGroupL(counter, iGroup->Id());
		}
	}

void CViewTester::AssignContactsToGroup2L()
    {
    TInt counter = 0;
    for ( counter = 0; counter < 10; counter++)
        {
        if (counter != KContactIdToNotAssignToGroup2)
            {
            iDb.AddContactToGroupL(counter, iGroup2->Id());
            }
        }
    }

void CViewTester::ConstructFilterViewL()
	{
	iFilteredView = CContactFilteredView::NewL(*this, iDb, *iRemoteView, EContactsOnly);
	}

void CViewTester::FilterViewSortOrderChangeL()
	{
	iRemoteView->ChangeSortOrderL(iViewSortOrder);
	}

void CViewTester::DestructFilterView()
	{
	if (iFilteredView) iFilteredView->Close( *this );
	// filtered view should delete it's self when it runs out of observers
	iFilteredView = NULL;
	}

void CViewTester::ConstructRemoteViewL(const TDesC8& aSortPluginName)
	{
	if(aSortPluginName.Length() == 0)
		{
		iRemoteView = CContactNamedRemoteView::NewL(*this, KRemoteViewName, iDb, iViewSortOrder, EContactsOnly);
		}
	else 
		{
		if(iRemoteView2) 
			iRemoteView3 = CContactRemoteView::NewL(*this, iDb, iViewSortOrder, EContactsOnly, aSortPluginName);
		else
			iRemoteView2 = CContactRemoteView::NewL(*this, iDb, iViewSortOrder, EContactsOnly, aSortPluginName);
		}
	}

CContactRemoteView* CViewTester::ExtracttRemoteViewLC()
	{
	CContactRemoteView* remoteView = iRemoteView2;
	iRemoteView2 = NULL;
	CleanupStack::PushL(remoteView);
	return remoteView;
	}

void CViewTester::RemoteViewSortOrderChangeL()
	{
	iRemoteView->ChangeSortOrderL(iViewSortOrder);
	}

void CViewTester::DestructRemoteView()
	{
	if (iRemoteView) iRemoteView->Close( *this );
	// remote view should delete it's self when it runs out of observers
	iRemoteView = NULL;
	if (iRemoteView2) iRemoteView2->Close( *this );
	// remote view should delete it's self when it runs out of observers
	iRemoteView2 = NULL;
	if (iRemoteView3) iRemoteView3->Close( *this );
	// remote view should delete it's self when it runs out of observers
	iRemoteView3 = NULL;

	}

void CViewTester::ConstructGroupViewL()
	{
	iGroupView = CContactGroupView::NewL(iDb,*iRemoteView,*this,iGroup->Id(),CContactGroupView::EShowContactsInGroup);
	}

void CViewTester::GroupViewSortOrderChangeL()
	{
	iRemoteView->ChangeSortOrderL(iViewSortOrder);
	}

void CViewTester::DestructGroupView()
	{
	if(iGroupView) iGroupView->Close(*this);
	// group view should delete it's self when it runs out of observers
	iGroupView = NULL;
	}

void CViewTester::ConstructGroupView2L()
    {
    iGroupView2 = CContactGroupView::NewL(iDb,*iRemoteView,*this,iGroup2->Id(),CContactGroupView::EShowContactsNotInGroup);
    }
   
void CViewTester::GroupView2DeleteOnlyContactL()
    {
    // Set up the mapping between the contact Id that
    // will be deleted and its index in the group view.
    iContactToDeleteMapping.iId = KContactIdToNotAssignToGroup2;
    iContactToDeleteMapping.iMapping = iGroupView2->FindL(KContactIdToNotAssignToGroup2);
   
    iDb.DeleteContactL(KContactIdToNotAssignToGroup2);
    }
   
void CViewTester::DestructGroupView2()
    {
    if(iGroupView2) iGroupView2->Close(*this);
    // group view should delete it's self when it runs out of observers
    iGroupView2 = NULL;
    }

CArrayFix<TContactItemId >* CViewTester::OrderOfContactsLC(const CContactViewBase& aView)
	{ // aView must be ready
	CArrayFix<TContactItemId >* ids=new(ELeave) CArrayFixFlat<TContactItemId >(4);
	CleanupStack::PushL(ids);

	TInt size = aView.CountL();
	for(TInt i=0;i<size;i++)
		{
		ids->AppendL(aView.AtL(i));
		}
	return ids;
	}


void CViewTester::DoCancel()
	{
	}

//
// Main.
//

/**

@SYMTestCaseID     PIM-T-VIEWEVENTS-0001

*/

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	CActiveScheduler* scheduler=new CActiveScheduler;
	test.Start(_L("@SYMTESTCaseID:PIM-T-VIEWEVENTS-0001 Testing ViewEvents"));

	if (scheduler)
		{
		CActiveScheduler::Install(scheduler);
		CTrapCleanup* cleanup=CTrapCleanup::New();
		if (cleanup)
			{
			CTestConductor* testConductor=NULL;
			TRAP_IGNORE(testConductor = CTestConductor::NewL());
			delete testConductor;
			delete cleanup;
			}
		delete scheduler;
		}
	test.End();
	test.Close();
	__UHEAP_MARKEND;
	return KErrNone;
    }