summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_eventorder.cpp
blob: a52befeb0c2e263a73c819c314a095cef5b5625d (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
/*
* 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_eventorder.h"

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

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

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

_LIT(KRemoteViewName,"RemoteView");


LOCAL_D RTest test(KTestName);

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

CTestConductor::~CTestConductor()
	{
	delete iLog;
	const TInt KCount(contacts->Count() );

	TRAP_IGNORE(
		for(TInt i = 0; i < KCount; ++i)
			{
			iDb->DeleteContactL((*contacts)[i]);
			}
	);
	delete iDb;
	delete iRandomGenerator;
	delete contacts;
    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()
	{
	contacts = CContactIdArray::NewLC();
	iTotalContacts=0;
	test.Printf(_L("Adding Contact for Phone number test "));
	TInt ii;
	for (ii=0;ii<3;ii++)
		{
		TInt bit=0;
		bit |= CContactDatabase::ESmsable;
		TContactItemId itemId =iRandomGenerator->AddTypicalContactForFilterL(bit);
		contacts->AddL(itemId);
		iTotalContacts++;
		}
	CleanupStack::Pop(contacts); // tester.
	}

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();
   
	iViewSortOrder.Close();
	iLocalViewResults.Close();
	iReceivedResults.Close();
	iAddNumberRemoteViewResults1.Close();
	iAddNumberRemoteViewResults2.Close();
	iAddNumberRemoteViewResults3.Close();
	iAddNumberRemoteViewResults4.Close();	
	iAddNumberReceivedResults.Close();
	}

void CViewTester::ConstructL()
	{
	iViewSortOrder.AppendL(KUidContactFieldGivenName);
	iViewSortOrder.AppendL(KUidContactFieldFamilyName);
	iViewSortOrder.AppendL(KUidContactFieldCompanyName);


	iAddNumberRemoteViewResults1.Append( TContactViewEvent::EItemRemoved);
	iAddNumberRemoteViewResults1.Append( 2 );
	iAddNumberRemoteViewResults1.Append( TContactViewEvent::EItemRemoved);
	iAddNumberRemoteViewResults1.Append( 0 );
	iAddNumberRemoteViewResults1.Append( TContactViewEvent::EItemAdded);
	iAddNumberRemoteViewResults1.Append( 0 );
	iAddNumberRemoteViewResults1.Append( TContactViewEvent::EItemAdded);
	iAddNumberRemoteViewResults1.Append( 2 );
	
	iAddNumberRemoteViewResults2.Append( TContactViewEvent::EItemRemoved);
	iAddNumberRemoteViewResults2.Append( 1 );
	iAddNumberRemoteViewResults2.Append( TContactViewEvent::EItemRemoved);
	iAddNumberRemoteViewResults2.Append( 0 );
	iAddNumberRemoteViewResults2.Append( TContactViewEvent::EItemAdded);
	iAddNumberRemoteViewResults2.Append( 0 );
	iAddNumberRemoteViewResults2.Append( TContactViewEvent::EItemAdded);
	iAddNumberRemoteViewResults2.Append( 1 );
	
	iAddNumberRemoteViewResults3.Append( TContactViewEvent::EItemRemoved);
	iAddNumberRemoteViewResults3.Append( 0 );
	iAddNumberRemoteViewResults3.Append( TContactViewEvent::EItemAdded);
	iAddNumberRemoteViewResults3.Append( 0 );
	iAddNumberRemoteViewResults3.Append( TContactViewEvent::EItemRemoved);
	iAddNumberRemoteViewResults3.Append( 2 );
	iAddNumberRemoteViewResults3.Append( TContactViewEvent::EItemAdded);
	iAddNumberRemoteViewResults3.Append( 2 );
	
	iAddNumberRemoteViewResults4.Append( TContactViewEvent::EItemRemoved);
	iAddNumberRemoteViewResults4.Append( 0 );
	iAddNumberRemoteViewResults4.Append( TContactViewEvent::EItemAdded);
	iAddNumberRemoteViewResults4.Append( 0 );
	iAddNumberRemoteViewResults4.Append( TContactViewEvent::EItemRemoved);
	iAddNumberRemoteViewResults4.Append( 1 );
	iAddNumberRemoteViewResults4.Append( TContactViewEvent::EItemAdded);
	iAddNumberRemoteViewResults4.Append( 1 );			
	iTestState = EStartingTests;
	NextTest();
	}

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


void CViewTester::AddNumberCompareRecordedResultWithExpected(const RArray<TInt>& aAddNumberRemoteViewResults)
	{
	TInt counter;
	TInt max;
	TContactViewEvent::TEventType expectedEvent;
	TContactViewEvent::TEventType actualEvent;
	TInt expectedInt;
	TInt actualInt;

	test( aAddNumberRemoteViewResults.Count() == iAddNumberReceivedResults.Count() );
	max = aAddNumberRemoteViewResults.Count();

	for (counter = 0; counter < max; counter++)
		{
		expectedEvent = static_cast<TContactViewEvent::TEventType>(aAddNumberRemoteViewResults[counter]);
		actualEvent = static_cast<TContactViewEvent::TEventType>(iAddNumberReceivedResults[counter]);
		test( expectedEvent == actualEvent );
		counter++;
		expectedInt = static_cast<TInt>(aAddNumberRemoteViewResults[counter]);
		actualInt = static_cast<TInt>(iAddNumberReceivedResults[counter]);
		test( expectedInt == actualInt );
		}
		
	iAddNumberReceivedResults.Reset();

	}
	
void CViewTester::AddPhoneNumberL(const TDesC& aWorkPhone,TContactItemId& ItemId)
	{
	CContactItem *contactItem=iDb.OpenContactL(ItemId);
	CleanupStack::PushL(contactItem);
	CContactItemFieldSet& fields=contactItem->CardFields();
	TInt fieldIndex = fields.Find(KUidContactFieldPhoneNumber);

	if ( fieldIndex > KErrNotFound )
		{
		CContactItemField& field = fields[ fieldIndex ];
		ASSERT(field.StorageType()==KStorageTypeText);
		STATIC_CAST(CContactTextField*,field.Storage())->SetText(aWorkPhone.AllocL());
		}
	
	iDb.CommitContactL(*contactItem); 
	CleanupStack::PopAndDestroy(contactItem);
	}

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;

		case ERemoteViewPlugin2:
		case ERemoteViewPlugin:
			iLog.LogLine ( _L("=== Creating remote view plugin"));
			ConstructRemoteViewL(KSortPlugin);
			break;			
			
		//Add Phone Numbers	
		case EAddPhoneNumber1:
			iLog.LogLine ( _L("=== Add Phone Number to contact item 2 and 1"));
			AddPhoneNumberL(_L("222"), (*(iTestConductor->contacts))[1]);
			AddPhoneNumberL(_L("111"), (*(iTestConductor->contacts))[0]);
			iTestState = static_cast<TTest>( static_cast<TInt>(iTestState) + 1 );
			break;
		case EAddPhoneNumber2:
			iLog.LogLine ( _L("=== Add Phone Number to contact item 3 and 1"));
			AddPhoneNumberL(_L("333"), (*(iTestConductor->contacts))[2]);
			AddPhoneNumberL(_L("111"), (*(iTestConductor->contacts))[0]);
			iTestState = static_cast<TTest>( static_cast<TInt>(iTestState) + 1 );
			break;
		case EAddPhoneNumber3:
			iLog.LogLine ( _L("=== Add Phone Number to contact item 1 and 2"));
			AddPhoneNumberL(_L("111"), (*(iTestConductor->contacts))[0]);
			AddPhoneNumberL(_L("222"), (*(iTestConductor->contacts))[1]);
			iTestState = static_cast<TTest>( static_cast<TInt>(iTestState) + 1 );
			break;
		case EAddPhoneNumber4:
			iLog.LogLine ( _L("=== Add Phone Number to contact item 1 and 3"));
			AddPhoneNumberL(_L("111"), (*(iTestConductor->contacts))[0]);
			AddPhoneNumberL(_L("333"), (*(iTestConductor->contacts))[2]);
			iTestState = static_cast<TTest>( static_cast<TInt>(iTestState) + 1 );
			break;
		
		case EPhoneNumberChangedEvent1:
		case EPhoneNumberChangedEvent2:
		case EPhoneNumberChangedEvent3:
		case EPhoneNumberChangedEvent4:
			iLog.LogLine ( _L("=== Capture Phone Number Changed Event"));
			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 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)
	{
	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;


        //
        // 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 wihout alot 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) );
				if (err)
					{
					_LIT(KErrMsg, "Contact view error in CViewTester::HandleContactViewEvent() assigning ids\n");
					test.Printf(KErrMsg);
					User::Invariant();
					}
				TRAP(err, ids2 = OrderOfContactsLC(*iRemoteView3) );
				if (err)
					{
					_LIT(KErrMsg, "Contact view error in CViewTester::HandleContactViewEvent() assigning ids2\n");
					test.Printf(KErrMsg);
					User::Invariant();
					}
				
				TInt size = ids->Count();
				test(ids2->Count()==size);
				for (TInt i =0;i<size;i++) 
					{
					test(ids->At(i)==ids2->At(i));
					}

				CleanupStack::PopAndDestroy(2,ids); // +ids2
	
				NextTest();

				}
			break;
					
		//Phone Number Changed events	
		case EPhoneNumberChangedEvent1:	//Pass through
		case EPhoneNumberChangedEvent2:	//Pass through
		case EPhoneNumberChangedEvent3:	//Pass through
		case EPhoneNumberChangedEvent4:	
		 	if ( &aView == iRemoteView ) 
		 	{
			if (aEvent.iEventType == TContactViewEvent::EItemRemoved )
				{
				iLog.LogLine ( _L("=== Capature EItemRemoved"));
				}
			else if ( (&aView == iRemoteView) && (aEvent.iEventType == TContactViewEvent::EItemAdded) )
				{
				iLog.LogLine ( _L("=== Capature EItemAdded"));
				}
			iAddNumberReceivedResults.Append( aEvent.iEventType );
			iAddNumberReceivedResults.Append( aEvent.iInt );
			}
			
			if (iAddNumberReceivedResults.Count() == 8 )
				{
				if (iTestState == EPhoneNumberChangedEvent1)
					{
					AddNumberCompareRecordedResultWithExpected(iAddNumberRemoteViewResults1);
					iTestState = static_cast<TTest>( static_cast<TInt>(iTestState) + 1 );
					}
				else if (iTestState == EPhoneNumberChangedEvent2)
					{
					AddNumberCompareRecordedResultWithExpected(iAddNumberRemoteViewResults2);
					iTestState = static_cast<TTest>( static_cast<TInt>(iTestState) + 1 );
					}
				else if (iTestState == EPhoneNumberChangedEvent3)
					{
					AddNumberCompareRecordedResultWithExpected(iAddNumberRemoteViewResults3);
					iTestState = static_cast<TTest>( static_cast<TInt>(iTestState) + 1 );
					}
				else
					{
					AddNumberCompareRecordedResultWithExpected(iAddNumberRemoteViewResults4);
					iTestState = EEndTest;
					}	
				}
		 	
			CompleteRequest();
			break;
					
		//
		// End Tests
		//
		case EEndTest:
		default:
			test(EFalse);
			break;
		}
	}


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;

	}



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-EVENTORDER-0001

*/

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

	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;
    }