summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/integration/cntperftest/src/testcontactviewdatabaseutilitiesstep.cpp
blob: 91f4535d37cbb93590f6794e32ca6192c2d548d0 (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
/*
* 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 "testcontactviewdatabaseutilitiesstep.h"

/**
 * Test Step Destructor
 */
CTestContactViewDatabaseUtilitiesStep::~CTestContactViewDatabaseUtilitiesStep()
	{
	if(iContactUtility)
		{
		delete iContactUtility;
		}
	if(iContactViewCollection)
		{
		delete iContactViewCollection;
		}
	if(iDb)
		{
		delete iDb;
		}
	if(iScheduler)
		{
		delete iScheduler;
		}
	}

/**
 * Test Step preamble function, setups the environment required for the test step
 */
TVerdict CTestContactViewDatabaseUtilitiesStep::doTestStepPreambleL()
	{
	iPimTestServer.Connect();
	CopyDatabaseL();
	InstallActiveSchedularL();
	OpenDataBaseL();
	SetupUtilityL();
	return TestStepResult();
	}

/**
 * Test Step postamble function, part of cleanup activity for the test step
 */
TVerdict CTestContactViewDatabaseUtilitiesStep::doTestStepPostambleL()
	{
	iPimTestServer.Close();
	CloseDatabaseL();
	UnInstallActiveSchedularL();
	return TestStepResult();
	}


/**
 * Installs active schedular in the current thread
 */
void CTestContactViewDatabaseUtilitiesStep::InstallActiveSchedularL()
	{
	iScheduler = new (ELeave) CActiveScheduler;
	CActiveScheduler::Install(iScheduler);
	}

/**
 * Allocates necessary utility objects
 */
void CTestContactViewDatabaseUtilitiesStep::SetupUtilityL()
	{
	iContactViewCollection = CContactViewCollection::NewL();
	CTestStep* self = static_cast<CTestStep*>(this);
	iContactUtility = CContactUtilitiesCollection::NewL(*self, DatabaseReference(), ViewCollectionReference());
	}

/**
 * Opens the desired database file based on ini data mentioned in the ini file
 */
void CTestContactViewDatabaseUtilitiesStep::OpenDataBaseL()
	{
	TBool	createDataBase = EFalse;
	TPtrC dbName;
	GetBoolFromConfig(ConfigSection(),   SharedConstants::KCreateDataBase, createDataBase);
	GetStringFromConfig(ConfigSection(), SharedConstants::KDbName, dbName);
	iDb = CreateAndOpenDataBaseL(dbName, createDataBase);
	}

/**
 * Create or open the contact database
 * @param aDbName database to be created/opened
 * @return CContactDatabase*
 */
CContactDatabase* CTestContactViewDatabaseUtilitiesStep::CreateAndOpenDataBaseL(const TPtrC& aDbName, TBool aCreateDataBase)
	{
	HBufC*	dbName = HBufC::NewLC(aDbName.Length());
	dbName->Des().Copy(aDbName);
	CContactDatabase*	cntDb = NULL;
	if( aCreateDataBase )
		{
		TInt	err = 0;
		// Replace the existing database and opens it
		if( aDbName != KNullDesC() )
			{
			TRAP(err, cntDb = CContactDatabase::ReplaceL(dbName->Des()));
			}
		else //if database name is not given then create and open the default DB
			{
			TRAP(err, cntDb = CContactDatabase::ReplaceL());
			}

		if( err != KErrNone )
			{
			ERR_PRINTF2(KErrInCreateDataBase, err);
			SetTestStepResult(EFail);
			SetTestStepError(err);
			}
		}
	else
		{
		TRAPD(err, cntDb = CContactDatabase::OpenL(dbName->Des()));
		if( err != KErrNone )
			{
			ERR_PRINTF2(KErrInOpen, err);
			SetTestStepResult(EFail);
			SetTestStepError(err);
			}
		}
	CleanupStack::PopAndDestroy(dbName);
	return	cntDb;
	}

/**
 * Constructs the desired contact views based on the data specified in the ini file
 */
void CTestContactViewDatabaseUtilitiesStep::ConstructViewsL()
	{
	TPtrC	listOfViews;
	GetStringFromConfig(ConfigSection(), SharedConstants::KListOfViews, listOfViews);
	RArray<TPtrC>	sections;
	CleanupClosePushL(sections);
	iContactUtility->TokenizeStringL(listOfViews, sections);
	iContactUtility->ConstructViewsL(sections);
	CleanupStack::PopAndDestroy(&sections);
	}

/**
 * Retrieves the desired contact view observer and listens for contact view notifications
 * @param aEvent - A TContactViewEvent object that will be updated with the notification received
 * @return TBool - An indication whether notification was received or not
 */
TBool CTestContactViewDatabaseUtilitiesStep::ListenForViewEventsL(TContactViewEvent& aEvent)
	{
	const TInt KNotificationTimeout = 10; // ms
	TInt desiredViewIndex = 0;
	_LIT(KDesiredViewIndex, "DesiredViewIndex");
	GetIntFromConfig(ConfigSection(), KDesiredViewIndex, desiredViewIndex);

	TPtrC desiredViewType;
	_LIT(KDesiredViewType, "DesiredViewType");
	GetStringFromConfig(ConfigSection(), KDesiredViewType, desiredViewType);

	CContactViewEventQueue&	contactViewObserver =
		ViewCollectionReference().GetDesiredViewObserver(desiredViewType, desiredViewIndex);

    return (contactViewObserver.ListenForEvent(KNotificationTimeout,aEvent));
	}

/* Here we are trying add blank groups in the database
 * Say some 1000 thousand groups are added to the database
 */
void CTestContactViewDatabaseUtilitiesStep::AddGroupsInDatabaseL()
	{
	_LIT(KNumOfGroups, "numofgroups");
	TInt numOfGroups = 0;
	GetIntFromConfig(ConfigSection(), KNumOfGroups, numOfGroups);

	for(TInt i = 0; i < numOfGroups; ++i)
		{
		CContactItem* contactGroup = DatabaseReference().CreateContactGroupL();
		delete contactGroup;
		}
	}


/* In this section, we edit the desired number of groups in the database
 * Set label for them and associate them with desired number of groups
 * With this database updation, group views can be constructed and accessed
 */
void CTestContactViewDatabaseUtilitiesStep::IterateThroAllGroupSectionsAndUpdateL()
	{
	_LIT(KListOfGroupsSectionsString, "listofgroupsections");
	TPtrC listOfGroupsSectionsString;
	GetStringFromConfig(ConfigSection(), KListOfGroupsSectionsString, listOfGroupsSectionsString);

	RArray<TPtrC>	listOfGroupsSections;
	CleanupClosePushL(listOfGroupsSections);
	ViewUtilityReference().TokenizeStringL(listOfGroupsSectionsString, listOfGroupsSections);

	CCntFilter* filter = CCntFilter::NewL();
	CleanupStack::PushL(filter);
	filter->SetContactFilterTypeGroup(ETrue);
	DatabaseReference().FilterDatabaseL(*filter);

	for ( TInt i = 0; i < listOfGroupsSections.Count(); ++i )
		{
		CContactIdArray* idArray = filter->iIds;
		TContactItemId groupId = (*idArray)[i];
		CContactItem* group = DatabaseReference().OpenContactL(groupId);
		CleanupStack::PushL(group);
		CContactGroup* newGroup = static_cast<CContactGroup*>(group);
		UpdateGroupsL(listOfGroupsSections[i], *newGroup);
		DatabaseReference().CommitContactL(*group);
		DatabaseReference().CloseContactL(groupId);
		CleanupStack::PopAndDestroy(group);
		}

	CleanupStack::PopAndDestroy(2, &listOfGroupsSections);
	}


/**
 * Updates the specified group with details like group name, number of contacts in the group
 * @param aGroupSection - section in the ini file contains necessary details for group update
 * @param aGroup - Group to be updated
 */
void CTestContactViewDatabaseUtilitiesStep::UpdateGroupsL(const TPtrC& aGroupSection, CContactGroup& aGroup)
	{
	_LIT(KGroupName, "groupname");
	TPtrC groupName;
	GetStringFromConfig(aGroupSection, KGroupName, groupName);

	aGroup.SetGroupLabelL(groupName);

	_LIT(KNumOfContacts, "numofcontacts");
	TInt numOfContacts;
	GetIntFromConfig(aGroupSection, KNumOfContacts, numOfContacts);

	CCntFilter* filter = CCntFilter::NewL();
	CleanupStack::PushL(filter);
	filter->SetContactFilterTypeCard(ETrue);
	DatabaseReference().FilterDatabaseL(*filter);

	for(TInt i = 0; i < numOfContacts; ++i)
		{
		CContactIdArray* idArray = filter->iIds;
		TContactItemId contactItemId = (*idArray)[i];
		CContactItem* contactItem = DatabaseReference().OpenContactL(contactItemId);
		CleanupStack::PushL(contactItem);
		TRAP_IGNORE(DatabaseReference().RemoveContactFromGroupL(*contactItem, aGroup));
		TRAP_IGNORE(DatabaseReference().AddContactToGroupL(*contactItem, aGroup));
		CleanupStack::PopAndDestroy(contactItem);
		DatabaseReference().CloseContactL(contactItemId);
		}

	CleanupStack::PopAndDestroy(filter);
	}


/**
 * Closes the relevant contacts database file
 */
void CTestContactViewDatabaseUtilitiesStep::CloseDatabaseL()
	{
	if(iContactUtility)
		{
		delete iContactUtility;
		iContactUtility = NULL;
		}
	if(iContactViewCollection)
		{
		delete iContactViewCollection;
		iContactViewCollection = NULL;
		}
	if(iDb)
		{
		delete iDb;
		iDb=NULL;
		}
	}

/**
 * Uninstall scheduler in the current thread
 */
void CTestContactViewDatabaseUtilitiesStep::UnInstallActiveSchedularL()
	{
	if(iScheduler)
		{
		delete iScheduler;
		iScheduler = NULL;
		}
	}

/**
 * Handle to the current database in operation
 * @return CContactDatabase& - Reference to current database in operation
 */
CContactDatabase& CTestContactViewDatabaseUtilitiesStep::DatabaseReference()
	{
	return *iDb;
	}

/**
 * Handle to the contact view collection
 * @return CContactViewCollection& - Reference to current contact view collection
 */
CContactViewCollection& CTestContactViewDatabaseUtilitiesStep::ViewCollectionReference()
	{
	return *iContactViewCollection;
	}

/**
 * Handle to the contact view utilities
 * @return CContactUtilitiesCollection& - Reference to current contact view utilities collection
 */
CContactUtilitiesCollection& CTestContactViewDatabaseUtilitiesStep::ViewUtilityReference()
	{
	return *iContactUtility;
	}

/**
 * Handle to the Pim Test Server
 * @return RPIMTestServer& - Reference to Pim Test Server
 */
RPIMTestServer& CTestContactViewDatabaseUtilitiesStep::PimTestServerReference()
	{
	return iPimTestServer;
	}

/**
 * Copies database files across folders
 */
void CTestContactViewDatabaseUtilitiesStep::CopyDatabaseL()
	{
	_LIT(KSourcePath, "sourcepath");
	TPtrC sourcePathString;
	GetStringFromConfig(ConfigSection(), KSourcePath, sourcePathString);

	_LIT(KDestinationPath, "destinationpath");
	TPtrC destinationPathString;
	GetStringFromConfig(ConfigSection(), KDestinationPath, destinationPathString);

	if(sourcePathString != KNullDesC && destinationPathString != KNullDesC)
		{
   		TRAP_IGNORE(PimTestServerReference().CopyFileL(sourcePathString, destinationPathString));
		}
	}