summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/integration/cntperftest/src/accesscounttests.cpp
blob: c169d073079632f9f63ac0f00d5cb31b2e5f8618 (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
/*
* Copyright (c) 2006-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: 
*
*/


/**
 @file
 @publishedAll
 @released
*/

#include <test/testexecutelog.h>
#include <cntfldst.h>
#include "clientserver.h"
#include "accesscounttests.h"

_LIT(KRun1,"DeleteAccessed");
_LIT(KRun2,"ForwardDeletion");
_LIT(KRun3,"ReverseDeletion");
_LIT(KRun4,"AgentGetSet");
_LIT(KRun5,"AgentAccess");
_LIT(KRun6,"AgentImportExport");

_LIT(KTest1,"Testing deletion of contacts with access count > 0...");
_LIT(KTest2,"Testing agent fields and forward deletion of items with those fields...");
_LIT(KTest3,"Testing agent fields and reverse deletion of items with those fields...");
_LIT(KTest4,"Agent retrieve and set test...");
_LIT(KTest5,"Agent access count test...");
_LIT(KTest6,"Agent import/export access count test...");

_LIT(KVcardFile,"c:\\ExportedContacts.vcf");
_LIT(KAgentValue,"Agent value: %d");

//Number of contacts
CAccessCountTests::CAccessCountTests(CCntPerfServer &aParent):CCntBaseStep(30, aParent)
	{
	// Call base class method to set up the human readable name for logging
	SetTestStepName(SharedConstants::KAccessCountTests);
	}

void CAccessCountTests::PreTestL()
	{
	User::LeaveIfError( iParent->Fs().Connect() );
	}

TVerdict CAccessCountTests::doTestStepL()
	{
	
	__UHEAP_MARK;

	InitializeL();
	iIterate->Reset();
	
	const TDesC &run = ConfigSection();
	
	if( run == KRun1 )
		{
		INFO_PRINTF1(KTest1);
		AccessCountTestL( iIterate->NextL() );
		}
	else if( run == KRun2 )
		{
		INFO_PRINTF1(KTest2);
		ForwardAgentDeletionTestL( 10 );
		}
	else if( run == KRun3 )
		{
		INFO_PRINTF1(KTest3);
		ReverseAgentDeletionTestL( 10 );
		}
	else if( run == KRun4 )
		{
		INFO_PRINTF1(KTest4);
		AddGetAgentTestL(iIterate->NextL());
		}
	else if( run == KRun5 )
		{
		INFO_PRINTF1(KTest5);
		AgentAccessCountTestL();
		}
	else if( run == KRun6 )
		{
		INFO_PRINTF1(KTest6);
		AgentExportImportAccessTestL();
		}
	else
		{
		MissngTestPanic();
		}
	Cleanup();
	__UHEAP_MARKEND;

	
	return TestStepResult();
	}

/**
open acid, increase access count to 1, attempt to delete contact, check that it has
not been deleted, set access count to 0, delete, and check that it has been deleted.
*/
void CAccessCountTests::AccessCountTestL(const TContactItemId aCid)
	{
	TInt err = KErrNone;
	static const TInt KAccessCount = 1;//increase access count of contact by 1, 
										
	TInt i = 0;
	
	OpenL(aCid,*iViewAll);
	SetContactFieldsL(*iContactItem);
	for(i = 0; i < KAccessCount; ++i )
		{
		iContactItem->IncAccessCount();
		}
	CommitL();
	
	ReadL(aCid, *iViewAll);
	const TInt KInitialAccessCount = iContactItem->AccessCount();//facount
	const TInt KInitialFieldCount = iFields->Count(); //fcount
	const TInt KInitialEmptyFields = CountEmpty(*iFields);//fhcount
	Close();
	
	INTCOMPARE(  KInitialEmptyFields , ==,  0 , 0, 0 );
	INTCOMPARE(  KInitialFieldCount , >,  0 , 0, 0 );
	
	for(i = 0; i < KAccessCount; ++i )
		{
		iContactsDatabase->DeleteContactL(aCid);
		}
		
	ReadL(aCid,*iViewAll);
	const TInt KModifiedAccessCount = iContactItem->AccessCount();//dacount
	const TInt KModifiedFieldCount 	= iFields->Count();//dcount
	const TInt KModifiedEmptyFields = CountEmpty(*iFields);//dHcount
	Close();
	
	/*
	check that deleting a contact doesnt modify its access count
	check that deleting a contact with access count > 1, 
	deletes all fields associated with contact but not the contact item itself
	*/
	INTCOMPARE(  KModifiedAccessCount , ==,  KInitialAccessCount , 0, 0 ); 
	INTCOMPARE(  KInitialFieldCount , ==,  KModifiedFieldCount , 0, 0 );
	INTCOMPARE(  KModifiedEmptyFields , ==,  KInitialFieldCount , 0, 0 ); 
	INTCOMPARE(  KModifiedFieldCount , ==,  KModifiedEmptyFields , 0, 0 );
	
	OpenL(aCid,*iViewAll);
	for(i = 0; i < KAccessCount; ++i )
		{
		iContactItem->DecAccessCount();
		}
	const TInt lAcount = iContactItem->AccessCount();
	CommitL();
	
	INTCOMPARE( lAcount , ==,  0, 0, 0 );
	
	TRAP( err, ReadL(aCid, *iViewAll) );
	Close();
	
	INTCOMPARE(  err , ==,  KErrNotFound , 0, 0 );
	}

/*
test that contactitems can be delete successfull irregardless of the contents of their agent value
*/
void CAccessCountTests::ReverseAgentDeletionTestL(const TInt aNumContacts)
	{
	TContactItemId PreviousContactId = -1;
	TContactItemId CurrentContactId = iIterate->NextL();
	TInt i = 0;
	TInt err = KErrNone;
	
	for(; i < aNumContacts; ++i)
		{
		PreviousContactId = CurrentContactId;
		CurrentContactId = iIterate->NextL();
		//open contact item
		OpenL(CurrentContactId, *iViewAll);
		//add agent field and set to previous contact id
		AddAgentFieldL(PreviousContactId);
		CommitL();
		//print agent values useful for debugging
		INFO_PRINTF2(KAgentValue, GetAgentFieldL(CurrentContactId) );
		}

	//attempt to delete contacts starting from last contact to frist contact
	TBool DeleteSuccess = EFalse;
	for(i = aNumContacts ; i > 0 ; --i)
		{
		//attempt to delete contact
		iContactsDatabase->DeleteContactL(CurrentContactId);
		//remove contact id from iterator
		iIterate->RemoveL(CurrentContactId);
		//attempt to read deleted contact
		TRAP( err, ReadL(CurrentContactId, *iViewAll) );
		Close();
		//fail test if contact is still present
		DeleteSuccess = INTCOMPARE( err, ==, KErrNotFound, i, 0 );
		CurrentContactId = iIterate->PreviousL();
		if( ! DeleteSuccess )
			{
			break;
			}
		}
	//if test fails
	if( ! DeleteSuccess )
		{
		//remove rest of contacts from iterator
		for(; i > 0 ; --i)
			{
			iIterate->RemoveL(CurrentContactId);
			CurrentContactId = iIterate->PreviousL();
			}
		}
	}

/*
test that contactitems can be deleted successfully irregardless of the contents of their agent value
*/
void CAccessCountTests::ForwardAgentDeletionTestL(const TInt aNumContacts)
	{
	TBool DeleteSuccess = EFalse;
	TContactItemId PreviousContactId = -1;
	TContactItemId CurrentContactId = iIterate->NextL();
	TInt err = KErrNone;
	TInt i = 0;
	
	for(; i < aNumContacts; ++i)
		{
		PreviousContactId = CurrentContactId;
		CurrentContactId = iIterate->NextL();
		//open contact item
		OpenL(CurrentContactId, *iViewAll);
		//add agent field and set to previous contact id
		AddAgentFieldL(PreviousContactId);
		CommitL();
		//print agent values useful for debugging
		INFO_PRINTF2(KAgentValue, GetAgentFieldL(CurrentContactId) );
		}
	
	//move iterator to start first contact in list
	for( i = 0 ; i < aNumContacts; ++i )
		{
		CurrentContactId = iIterate->PreviousL();
		}
	
	//attempt to delete contacts starting from first contact to last contact
	for( i = 0 ; i < aNumContacts ; ++i )
		{
		//attempt to delete contact
		iContactsDatabase->DeleteContactL(CurrentContactId);
		//attempt to read deleted contact
		TRAP( err, ReadL(CurrentContactId, *iViewAll) );
		Close();
		//fail test if contact is still present
		DeleteSuccess = INTCOMPARE( err, ==, KErrNotFound , i, 0 );
		CurrentContactId = iIterate->NextL();
		if( ! DeleteSuccess )
			{
			break;
			}
		}
	//if test fails
	if(!DeleteSuccess)
		{
		//remove rest of contacts from iterator
		const TInt end = i + 1;
		for(i = 0 ; i < end ; ++i)
			{
			iIterate->RemoveL(CurrentContactId);
			CurrentContactId = iIterate->PreviousL();
			}
		CurrentContactId = iIterate->NextL();
		for(i = end ; i < aNumContacts ; ++i)
			{
			iIterate->RemoveL(CurrentContactId);
			CurrentContactId = iIterate->CurrentL();
			}
		}
	}

/**
* Open aCid, add agent field, set value of agent field, and get value of agent.
* Check this is correct, repeat this for minint, 0, 1, Maxint. Setting the 
* agent id should only accept values > 0, all other values are ignored.
*/
void CAccessCountTests::AddGetAgentTestL(const TContactItemId aCid)
	{
	TContactItemId retrievedAgentId = -1;
	const TInt KInitialAgentId = 5;
	
	OpenL(aCid,*iViewAll);
	AddAgentFieldL( KInitialAgentId );
	CommitL();
	
	TRAPD( error, SetAgentFieldL( aCid, KMinTInt ) );
	INTCOMPARE(  error , ==,  KErrNone , 0, 0 );
	retrievedAgentId = GetAgentFieldL( aCid );
	INTCOMPARE(  retrievedAgentId , ==,  KInitialAgentId , 0, 0 );
	
	TRAP( error, SetAgentFieldL( aCid, -1 ) );
	INTCOMPARE(  error , ==,  KErrNone , 0, 0 );
	retrievedAgentId = GetAgentFieldL( aCid );
	INTCOMPARE(  retrievedAgentId , ==,  KInitialAgentId , 0, 0 );
	
	TRAP( error, SetAgentFieldL( aCid, 0 ) );
	INTCOMPARE(  error , ==,  KErrNone , 0, 0 );
	retrievedAgentId = GetAgentFieldL( aCid );
	INTCOMPARE(  retrievedAgentId , ==,  KInitialAgentId , 0, 0 );

	TRAP( error, SetAgentFieldL( aCid, 1 ) );
	INTCOMPARE(  error , ==,  KErrNone , 0, 0 );
	retrievedAgentId = GetAgentFieldL( aCid );
	INTCOMPARE(  retrievedAgentId , ==,  1 , 0, 0 );
	
	TRAP( error, SetAgentFieldL( aCid, KMaxTInt ) );
	INTCOMPARE(  error , ==,  KErrNone , 0, 0 );
	retrievedAgentId = GetAgentFieldL( aCid );
	INTCOMPARE(  retrievedAgentId , ==,  KMaxTInt , 0, 0 );
	}

/**
add agent field to contact b, set value of agent in b to id of contact a.
check that agent field is retrieved/stored correctly, check that access count is
not modified for either contact
*/	
void CAccessCountTests::AgentAccessCountTestL()
	{
	TContactItemId RetrievedAgentId = -1;
	TContactItemId PreviousContactId = iIterate->NextL();//contact a
	TContactItemId CurrentContactId = iIterate->NextL();//contact b
	
	//open current contact
	OpenL(CurrentContactId,*iViewAll);
	//add new agent field, set to previous contact id
	AddAgentFieldL(PreviousContactId);
	CommitL();
	
	//get agent id store for current contact
	RetrievedAgentId = GetAgentFieldL(CurrentContactId);
	
	//get access count of current contact
	ReadL(CurrentContactId, *iViewAll);
	const TInt KCurrentContactAccessCount = iContactItem->AccessCount();
	Close();
	
	//get access count of previous contact
	ReadL(PreviousContactId, *iViewAll);
	const TInt KPreviousContactAccessCount = iContactItem->AccessCount();
	Close();
	
	//check that retrieved agent id equals previous contact id
	INTCOMPARE( RetrievedAgentId , ==,  PreviousContactId, 0, 0 );
	//check that changing the agent field values doesnt not affect the access count
	//of involved contacts
	INTCOMPARE( KCurrentContactAccessCount , ==,  0, 0, 0 ); 
	INTCOMPARE( KPreviousContactAccessCount , ==,  0, 0, 0 );
	}

/**
add agent field to contact b. set value of agent to contact a.
export contacts a & b. check access count for a & b(should be 0), 
check value of agent(should equal b) open contact b, delete all fields.
commit. import contacts a and b. check that agent field is not imported.
*/
void CAccessCountTests::AgentExportImportAccessTestL()
	{
	const TContactItemId KFirstContactId = iIterate->NextL();//contact a
	const TContactItemId KSecondContactId = iIterate->NextL();//contact b
	
	//open contact a and populate all fields
	OpenL(KFirstContactId, *iViewAll);
	SetContactFieldsL(*iContactItem);
	CommitL();
	
	//open contact b
	OpenL(KSecondContactId,*iViewAll);
	//populate all fields
	SetContactFieldsL(*iContactItem);
	//add contact a id as an agent field value
	AddAgentFieldL(KFirstContactId);
	CommitL();
	
	CContactIdArray *ArrayOfExportedContactIds = CContactIdArray::NewLC();
	//set contact a and b to be export
	ArrayOfExportedContactIds->AddL(KFirstContactId);
	ArrayOfExportedContactIds->AddL(KSecondContactId);
	
	//export contact a & b
	ExportVcardL(*ArrayOfExportedContactIds);
	
	const TInt KFirstContactAccessCountAfterExport = GetAccessCountL(KFirstContactId);
	const TInt KSecondContactAccessCountAfterExport = GetAccessCountL(KSecondContactId);
	const TInt KSecondContactAgentFieldAfterExport = GetAgentFieldL(KSecondContactId);
	CleanupStack::PopAndDestroy(ArrayOfExportedContactIds);
	
	//delete all fields from contact b
	OpenL(KSecondContactId,*iViewAll);
	iFields->Reset();
	CommitL();
	
	//import exported contacts
	ImportVcardL();
	
	const TInt KFirstContactAccessCountAfterImport = GetAccessCountL(KFirstContactId);
	const TInt KSecondContactAccessCountAfterImport = GetAccessCountL(KSecondContactId);
	const TContactItemId KSecondContactAgentFieldAfterImport = GetAgentFieldL(KSecondContactId);
	
	//importing & exporting contacts should not affect the access count
	INTCOMPARE( KFirstContactAccessCountAfterExport , ==,  0, 0, 0 ); 
	INTCOMPARE( KSecondContactAccessCountAfterExport , ==,  0, 0, 0 ); 
	INTCOMPARE( KFirstContactAccessCountAfterExport , ==,  KFirstContactAccessCountAfterImport, 0, 0 );
	INTCOMPARE( KSecondContactAccessCountAfterExport , ==,  KSecondContactAccessCountAfterImport, 0, 0 );
	
	//agent field values should not be exported/imported
	INTCOMPARE( KFirstContactId , ==,  KSecondContactAgentFieldAfterImport, 0, 0 ); 
	//exporting contacts should not affect the internal state of the contacts agent field
	INTCOMPARE( KSecondContactAgentFieldAfterExport , ==,  KFirstContactId, 0, 0 );
	
	iParent->Fs().Delete(KVcardFile);//cleanup of exported vcard file
	}
	
void CAccessCountTests::AddAgentFieldL(const TContactItemId aCid)
	{
	_LIT(KAgentName, "ImAnAgent");
	CContactItemField * field = CContactItemField::NewLC(KStorageTypeContactItemId, KUidContactFieldVCardMapAGENT );
	field->SetMapping(KUidContactFieldVCardMapAGENT);
	field->SetLabelL(KAgentName);
	field->AgentStorage()->SetAgentId(aCid);
	iFields->AddL(*field);
	CleanupStack::Pop(field);
	field = NULL;
	}

TContactItemId CAccessCountTests::GetAgentFieldL(const TContactItemId aCid)
	{
	ReadL(aCid, *iViewAll);
	const TInt count = iFields->Count();
	TContactItemId ret = KErrNotFound;
	TInt i = 0;
	for(; i < count; ++i)
		{
		if( (*iFields)[i].StorageType() == KStorageTypeContactItemId )
			{
			ret = (*iFields)[i].AgentStorage()->Value();
			break;
			}
		}
	Close();
	return ret;
	}

void CAccessCountTests::SetAgentFieldL(const TContactItemId aCid, const TContactItemId aVal)
	{
	
	TBool FoundAgentField = EFalse;
	OpenL(aCid, *iViewAll);
	const TInt count = iFields->Count();
	TInt i = 0;
	
	for(; i < count; ++i)
		{
		if( (*iFields)[i].StorageType() == KStorageTypeContactItemId )
			{
			(*iFields)[i].AgentStorage()->SetAgentId(aVal);
			FoundAgentField = ETrue;
			break;
			}
		}
		
	if( !FoundAgentField )
		{
		AddAgentFieldL(aVal);
		}
		
	CommitL();
	}

void CAccessCountTests::ExportVcardL(const CContactIdArray &aConids)
	{	
	RFileWriteStream rfws;
	CleanupClosePushL(rfws);
	User::LeaveIfError( rfws.Replace( iParent->Fs(), KVcardFile, EFileWrite | EFileShareAny ) );
	
	iContactsDatabase->ExportSelectedContactsL(	TUid::Uid(KUidVCardConvDefaultImpl), aConids, rfws,
										CContactDatabase::EIncludeX | CContactDatabase::ETTFormat );
	
	CleanupStack::PopAndDestroy(&rfws);		
	}	
	
void CAccessCountTests::ImportVcardL()
	{
	TBool sucess = EFalse;
	RFileReadStream rfrs;
	CleanupClosePushL( rfrs );
	User::LeaveIfError( rfrs.Open( iParent->Fs(), KVcardFile, EFileRead | EFileShareAny ) );
	
	FOREVER
		{
		CArrayPtr<CContactItem>* imported= 	iContactsDatabase->ImportContactsL( 
											TUid::Uid( KUidVCardConvDefaultImpl ), rfrs, sucess, 
											CContactDatabase::EIncludeX | CContactDatabase::ETTFormat |
											CContactDatabase::EImportSingleContact
											);	
		if(sucess)
			{
			imported->ResetAndDestroy();
			CLEAR(imported);	
			}
		else
			{
			imported->ResetAndDestroy();
			CLEAR(imported);
			break;	
			}
		sucess = EFalse;
		}

	CleanupStack::PopAndDestroy(&rfrs);
	}

TInt CAccessCountTests::GetAccessCountL(const TContactItemId aCid)
	{
	ReadL( aCid, *iViewAll );
	TInt ret = iContactItem->AccessCount();
	Close();
	return ret;
	}