summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_filterfindcontacts.cpp
blob: e57351c5d984b8ded78acc45c7287fbd16906fca (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
/*
* 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: 
* Tests the following:
* 1: Addition and removal of contacts from a CContactFindView
* 2: Database update when editing an unsortable contact ie making it sortable
*
*/


#include <e32test.h>	
#include <f32file.h>	
#include <cntdb.h>				
#include <cntviewbase.h>
#include <cntitem.h>

#include "t_filterfindcontacts.h"
#include "t_utils.h"	
#include "t_utils2.h"	

_LIT(KTestName,			"T_FilterFindContacts Test");

_LIT(KTestDbName,		"C:T_FilterFindContacts.cdb");

RTest test(KTestName);

CFilterFindContactsTester* CFilterFindContactsTester::NewL(CContactDatabase& aDb)
	{
	CFilterFindContactsTester* self = new (ELeave) CFilterFindContactsTester (aDb);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
	}

void CFilterFindContactsTester::ConstructL()
	{
	iAddedFindContacts = EFalse;
	iSortOrder.AppendL(KUidContactFieldGivenName);
	iSortOrder.AppendL(KUidContactFieldFamilyName);
	iSortOrder.AppendL(KUidContactFieldCompanyName);
	NextTest();
	}

CFilterFindContactsTester::CFilterFindContactsTester(CContactDatabase& aDb)
	: CActive(EPriorityStandard), iDb(aDb)
	{
	CActiveScheduler::Add(this);
	}

CFilterFindContactsTester::~CFilterFindContactsTester()
	{
	iSortOrder.Close();
	delete iFindDesArray;
	iFindView->Close(*this);
	iFindView = NULL;
	iLocalView->Close(*this);
	iLocalView = NULL;
	}

void CFilterFindContactsTester::NextTest()
	{
	++iCurrentTest;
	TRequestStatus *pS=&iStatus;
	User::RequestComplete(pS,KErrNone);
	SetActive();
	}

//============================================================================
// CFilterFindContactsTester::HandleContactViewEvent
// From MContactViewObserver
// Allows this class to recieve notification of any view events
//============================================================================
void CFilterFindContactsTester::HandleContactViewEvent(const CContactViewBase& /*aView*/,const TContactViewEvent& aEvent)
	{

	switch (iCurrentTest)
		{
		case ECreateLocalView:
		case ECheckAddContactEntry:
		case ECreateFindView:
			{
			NextTest();
			}
		break;
		case EFindAddContacts:
			{
			if (aEvent.iEventType == TContactViewEvent::EItemAdded && iAddedFindContacts == EFalse)
				{
				iAddedFindContacts = ETrue;
				NextTest();
				}
			}
		break;
		}
	}

//============================================================================
// CFilterFindContactsTester::RunL
// From CActive
// Performs the following actions:
// 1: Creates a CContactLocalView and CContactFindView
// 2: Creates 2 contacts, one of which will not be visible in either view
//    as it has no sortable text fields 
// 3: Modifies and updates the unsortable contact to be sortable
// 4: Deletes the contact
//============================================================================

void CFilterFindContactsTester::RunL()
	{
	switch (iCurrentTest)
		{
		case ECreateLocalView:
			{
			test.Next(_L("Creating local view"));
			iLocalView = CContactLocalView::NewL(*this,iDb,iSortOrder,EIgnoreUnSorted);
			}
		break;

		case ECheckAddContactEntry:
			{
			// test step for for DEF065179- bug in contact views AllFieldsLC() 
			test.Next(_L("Adding contact entry: John,Smith,Symbian"));

			AddEntryL();
			}
		break;

		case ECreateFindView:
			{
			test.Next(_L("Creating Find View, seachstring abc"));

			//Create a find view based on the local view
			iFindDesArray =new (ELeave) CPtrCArray(1);
			iFindDesArray->AppendL(_L("abc"));
			iFindView = CContactFindView::NewL(iDb,*iLocalView,*this,iFindDesArray);
			}
		break;
		case EFindAddContacts:
			{
			DisplayL();

			test.Next(_L("Adding two contacts in find view"));

			CContactTemplate*  iTemplate=STATIC_CAST(CContactTemplate*,iDb.ReadContactL(iDb.TemplateId()));
			CTestContact* contact = CTestContact::NewLC(*iTemplate);
			contact->SetFirstNameL(_L("ABC"));
			
			iFindAdd1 = iDb.AddNewContactL(contact->ContactItem());
			
			CTestContact* unsortableContact = CTestContact::NewLC(*iTemplate);
			iFindAdd2 = iDb.AddNewContactL(unsortableContact->ContactItem());

			delete iTemplate;
			iTemplate = NULL;

			CleanupStack::PopAndDestroy(2); //contact , unsortableContact
			
			}
		break;
		case EModifyUnsortableContact:
			{
			DisplayL();
			test.Next(_L("Modifying Unsortable contact"));

			CContactItem* item=iDb.OpenContactL(iFindAdd2);
			CleanupStack::PushL(item);
			TBuf<16> name;
			name.Format(_L("ABC 123456789"));
			SetNameL(*item,KUidContactFieldGivenName,KUidContactFieldVCardMapUnusedN,name,EFalse);
			iDb.CommitContactL(*item);
			CleanupStack::PopAndDestroy(); // item;
			Cancel();
			NextTest();
			}
		break;
		case EFindRemoveContacts:
			{
			DisplayL();
			test.Next(_L("Removing contact from find view"));

			iDb.DeleteContactL(iFindAdd2);
			NextTest();
			}
		break;
		
		case ECheckCorrectSeparators:
			// test step for for DEF065179 - bug in contact views AllFieldsLC() 
			{
			test.Next(_L("Get fields using AllFieldsLC and check commas"));

			
			_LIT(KFieldSeparator,",");
			TInt index;
			index  = iLocalView->FindL(iContactId);
			HBufC* text= iLocalView->AllFieldsLC(index,KFieldSeparator);

			_LIT(KExpectedText,"John,Smith,Symbian");
			test (text->Des() == KExpectedText);

			CleanupStack::PopAndDestroy(); // text
			NextTest();
			}
		break;
		
		case ETestsComplete:
			{
			DisplayL();
			test.Next(_L("Tests complete"));

			Cancel();
			CActiveScheduler::Stop();
			delete this;
			}
		break;
		default:
			{
			}
		break;
		}
	}

TInt CFilterFindContactsTester::RunError(TInt aError)
	{
	switch (iCurrentTest)
		{
		case ECreateLocalView: test.Printf(_L("test failed at step CreateLocalView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateFindView: test.Printf(_L("test failed at step  CreateFindView (%i) with error %i"), iCurrentTest, aError); break;
		case EFindAddContacts: test.Printf(_L("test failed at step FindAddContacts (%i) with error %i"), iCurrentTest, aError); break;
		case EModifyUnsortableContact: test.Printf(_L("test failed at step ModifyUnsortableContacts (%i) with error %i"), iCurrentTest, aError); break;
		case EFindRemoveContacts: test.Printf(_L("test failed at step FindRemoveContacts (%i) with error %i"), iCurrentTest, aError); break;
		case ETestsComplete: test.Printf(_L("test failed at step TestsComplete (%i) with error %i"), iCurrentTest, aError); break;

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

	// stop the test
	test(aError != KErrNone);
	return KErrNone;
	}

//============================================================================
// CFilterFindContactsTester::DisplayL()
// Prints side by side comparison of the CContactFindView and the 
// CContactLocalView. 
//============================================================================
void CFilterFindContactsTester::DisplayL()
	{
	test.Printf(_L("Database Count: \t%i\n"),	iDb.CountL());
	test.Printf(_L("LocalView Count:\t%i\n"),	iLocalView->CountL());
	test.Printf(_L("Findview Count: \t%i\n"),	iFindView->CountL());

	for (TInt x = 0; x < iLocalView->CountL(); x++)
		{
		HBufC* text1 = iLocalView->AllFieldsLC(x,_L(","));
		test.Printf(_L("LocalView: %d:\t"),x+1);

		TPtrC16 firstName = text1->Des();
		TInt length = firstName.Locate(',');
		test.Printf(firstName.Left(length));
		test.Printf(_L("\t"));
		CleanupStack::PopAndDestroy();

		if (x < iFindView->CountL())
			{
			HBufC* text2 = iFindView->AllFieldsLC(x,_L(","));
			test.Printf(text2->Des() );
			test.Printf(_L("FindView: %d:\t"),x+1);
			TPtrC16 firstName = text2->Des();
			TInt length = firstName.Locate(',');
			test.Printf(firstName.Left(length));
			CleanupStack::PopAndDestroy();
			}
		test.Printf(_L("\n"));
		}
	test.Printf(_L("\n"));
	}

void CFilterFindContactsTester::DoCancel()
	{
	}

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

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

CFilterFindContactsTestConductor::CFilterFindContactsTestConductor() 
	{
	}

void CFilterFindContactsTestConductor::ConstructL()
	{
	iDb=CContactDatabase::ReplaceL(KTestDbName);
	iRandomGenerator=CRandomContactGenerator::NewL();
	iRandomGenerator->SetDbL(*iDb);
	AddSomeContactsL(10);
	}

void CFilterFindContactsTestConductor::RunTestsL()
	{
	CFilterFindContactsTester* tester = CFilterFindContactsTester::NewL(*iDb);
	CleanupStack::PushL(tester);
	CActiveScheduler::Start();
	CleanupStack::Pop(tester);
	}
	
//============================================================================
// CFilterFindContactsTester::AddEntryL
// Adds a contact item to the contact database.
//============================================================================ 
void CFilterFindContactsTester::AddEntryL()
	{
	_LIT(KForename,"John"); 
	_LIT(KSurname,"Smith"); 
	_LIT(KCompanyName,"Symbian"); 

	// Create a  contact card to contain the data
	CContactCard* newCard = CContactCard::NewLC();
    
	// Create the firstName field and add the data to it
	CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
	firstName->TextStorage()->SetTextL(KForename);
	newCard->AddFieldL(*firstName);
  	CleanupStack::Pop(firstName);
  	
	// Create the lastName field and add the data to it
   	CContactItemField* lastName= CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
  	lastName ->TextStorage()->SetTextL(KSurname);
  	newCard->AddFieldL(*lastName);
   	CleanupStack::Pop(lastName);
   		    
	// Create the company name and add the data to it
  	CContactItemField* companyName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldCompanyName);
	companyName->SetMapping(KUidContactFieldCompanyName);
	companyName ->TextStorage()->SetTextL(KCompanyName);
	newCard->AddFieldL(*companyName);
	CleanupStack::Pop(companyName);
	
	// Add newCard to the database
	iContactId = iDb.AddNewContactL(*newCard);
	CleanupStack::PopAndDestroy(newCard);
	}

//============================================================================
// CFilterFindContactsTestConductor::AddSomeContactsL
// Creates a number of contacts and adds them to the database
//============================================================================
void CFilterFindContactsTestConductor::AddSomeContactsL(TInt aCount)
	{
	test.Printf(_L("Adding %d contacts directly to database\n"),aCount);

	CContactIdArray* contacts = CContactIdArray::NewLC();
	CRandomContactGenerator* generator = CRandomContactGenerator::NewL();
	CleanupStack::PushL( generator );
	generator->SetDbL(*iDb);

	TInt bit = CContactDatabase::ESmsable;
	
	TContactItemId itemId = 0;
	
		for(TInt i = 1;i <= aCount;i++)
		{
		itemId = generator->AddTypicalContactForFilterL(bit);
		contacts->AddL(itemId);
		}

	CleanupStack::PopAndDestroy( generator );
	CleanupStack::PopAndDestroy( contacts );

	}

//============================================================================
// doMainL
// Creates a CFilterFindContactsTestConducter, which creates a database
// instance and populates it with a few random contacts
//============================================================================
LOCAL_C void doMainL()
	{
	__UHEAP_MARK;
	CFilterFindContactsTestConductor* testConductor = CFilterFindContactsTestConductor::NewL();
	CleanupStack::PushL( testConductor );
	testConductor->RunTestsL();
	CleanupStack::PopAndDestroy( testConductor );
	__UHEAP_MARKEND;
	}

//============================================================================
// E32Main
// Main function called by E32
//============================================================================
/**

@SYMTestCaseID     PIM-T-FILTERFINDCONTACTS-0001

*/

GLDEF_C TInt E32Main()
    {
	__UHEAP_MARK;											// Mark heap state
	test.Start(_L("@SYMTESTCaseID:PIM-T-FILTERFINDCONTACTS-0001 T_FilterFindContacts Test"));

	CTrapCleanup* cleanup	= CTrapCleanup::New();			// Get cleanup stack
	CActiveScheduler* as	= new (ELeave) CActiveScheduler;// Get Active Scheduler
	CActiveScheduler::Install(as);							// Install Active Scheduler
	TRAPD(ret,doMainL());									// Run the test
	test(ret == KErrNone);
	delete as;												// Destroy the active scheduler
	delete cleanup;											// destroy clean-up stack
	test.End();
 	test.Close();
	__UHEAP_MARKEND;										// check no memory leak
	return (KErrNone);										// and return
    }