summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_contactsramtest.cpp
blob: 3ddc529da9675de30134de9820ce4ac5c630c525 (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
/*
* 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 <e32test.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntviewbase.h>
#include <cntfldst.h>
#include <e32panic.h>

_LIT(KTestName, "Ram consumption Test");

LOCAL_D RTest test(KTestName);

#define DELETE_SET_NULL(x) { delete x; x = NULL; }

static const TInt KMaxContacts = 2000;
static const TInt KManyFields = 8;
static const TInt KAgentId = 2;
_LIT(KTime,"22000101:235959.999999");// time to use for datetime storage
_LIT(KFieldValue,"Standard length field value");
TInt manycontacts = 0;

_LIT(KContactsIniFileFullPath, "c:\\private\\10003A73\\Cntmodel.ini");
_LIT(KContactsDbFileFullPath, "c:\\private\\10003A73\\Contacts.cdb");
_LIT(KContactsBackupRegistrationIniFileFullPath, "c:\\private\\10003A73\\backup_registration.xml");

static RFs TheFsSession;

class CContactsRamTest : public CActive, public MContactViewObserver
	{
public:
	static CContactsRamTest* NewLC();
	~CContactsRamTest();
	void SetView(const CContactViewBase *aLocalView);
	void RamTestL();
	void RamTest2L();
	void Activate();
	void CloseView();
	
private:
	CContactsRamTest();
	void ConstructL();
	void AddContactsL(const TInt aNumber);
	void SetFieldL(const CContactItemField &aField, const TDesC& aText);
	void SetManyFieldsL(const TDesC& aText, const TInt aMany, CContactItemFieldSet &fields);
	void PrintHeap();
	
private: // From CActive.
	void RunL();
	void DoCancel();
	TInt RunError(TInt aError);
private: // From MContactViewObserver.
	virtual void HandleContactViewEvent(const CContactViewBase& aView, 
										const TContactViewEvent& aEvent);

private:
	CContactDatabase* iContactsDatabase;
	CContactRemoteView* iRemoteView;
	RContactViewSortOrder iViewSortOrder;
	TBool iEnd;
	};


CContactsRamTest* CContactsRamTest::NewLC()
	{
	CContactsRamTest* self = new(ELeave) CContactsRamTest();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}
	

void CContactsRamTest::ConstructL()
	{
	CActiveScheduler::Add(this);
	iViewSortOrder.AppendL(KUidContactFieldGivenName);	
	iViewSortOrder.AppendL(KUidContactFieldFamilyName);
	}
	

CContactsRamTest::~CContactsRamTest()
	{
	CloseView();
	iViewSortOrder.Close();
	delete iContactsDatabase;
	}

CContactsRamTest::CContactsRamTest() : CActive(EPriorityStandard)
	{
	}
	
	
void CContactsRamTest::RunL()
	{
	if( iEnd )
		{
		iEnd = EFalse;
		RamTest2L();
		CActiveScheduler::Stop();
		}
	else
		{
		RamTestL();
		}
	}
	
void CContactsRamTest::DoCancel()
	{
	}

TInt CContactsRamTest::RunError(TInt aError)
	{
	test.Printf( _L("ViewObserver:: Error in doTest runL: %d"), aError );
	return aError;
	}

void CContactsRamTest::Activate()
	{
	TRequestStatus *pS=&iStatus;
	User::RequestComplete(pS,KErrNone);
	SetActive();
	}

void CContactsRamTest::RamTestL()
	{
	test.Next(_L("Initial ram test"));

	test.Printf( _L("Ram test for %d contacts\n"), manycontacts);
	DELETE_SET_NULL( iContactsDatabase );
	test.Printf(_L("Empty Database is opened\n"));
	iContactsDatabase = CContactDatabase::ReplaceL();
	PrintHeap();
	AddContactsL( manycontacts );
	test.Printf(_L("Database is opened and contacts added\n"));
	PrintHeap();
	test.Printf(_L("RemoteView with first/surname order is created\n"));
	iRemoteView = CContactRemoteView::NewL(	*this, *iContactsDatabase, iViewSortOrder, EContactsOnly);
	PrintHeap();
	
	}	

void CContactsRamTest::RamTest2L()
	{
	test.Printf(_L("RemoteView is populated\n"));
	PrintHeap();
	CloseView();
	DELETE_SET_NULL( iContactsDatabase );
	test.Printf(_L("Full Database is re-opened\n"));
	iContactsDatabase = CContactDatabase::OpenL();
	PrintHeap();
	DELETE_SET_NULL( iContactsDatabase );
	}	
	
void CContactsRamTest::HandleContactViewEvent(	const CContactViewBase& aView, 
											const TContactViewEvent& aEvent)
	{
	if (&aView==iRemoteView)
		{
		switch(aEvent.iEventType)
			{
			case TContactViewEvent::EReady:
				{				
				iEnd = ETrue;
				Activate();
				break;
				}
			}
		}
	}
	
void CContactsRamTest::AddContactsL(const TInt aNumber)
/**
Add number of contacts to db, using the system template.
@return None
@param aNumber number of contacts to be added
@pre None
@post database now contains aNumber of empty contacts
*/
	{
	

	//viewdef that loads all fields 	
	CContactItemViewDef *viewAll = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EIncludeHiddenFields);
	viewAll->AddL(KUidContactFieldMatchAll);
	CContactTemplate *ttemplate = static_cast< CContactTemplate* >(
	iContactsDatabase->ReadContactLC( iContactsDatabase->TemplateId(), *viewAll ) );

	CContactCard* contact = CContactCard::NewLC(ttemplate);
	SetManyFieldsL(KFieldValue, KManyFields, contact->CardFields());
	for(TInt k = 0; k<aNumber; k++)
		{		
		iContactsDatabase->AddNewContactL(*contact);
		}	
	CleanupStack::PopAndDestroy(contact);
	CleanupStack::PopAndDestroy(ttemplate);
	CleanupStack::PopAndDestroy(viewAll);
	}

/*sets a specific contact field to a value as determined by its type.
field at aPos in aFields is set to aText*/
void CContactsRamTest::SetFieldL(const CContactItemField &aField, const TDesC& aText)
	{
	if( aField.IsTemplateLabelField() )
		{
		return;
		}
		
	if( 0 == aText.Size() )
		{
		return;
		}
	
	switch(aField.StorageType())
		{
		case KStorageTypeText:
			aField.TextStorage()->SetTextL(aText);
		break;
		case KStorageTypeStore:
			{
			HBufC8 *buf = HBufC8::NewLC(aText.Size());
			TPtr8 tptr8 = buf->Des();
			tptr8.Copy(aText);
			aField.StoreStorage()->SetThingL(*buf);
			CleanupStack::PopAndDestroy(buf);
			}
		break;
		case KStorageTypeContactItemId:
			{
			aField.AgentStorage()->SetAgentId( KAgentId );
			}
		break;
		case KStorageTypeDateTime:
			{
			if( aText.Size() > 0 ) 
				{
				aField.DateTimeStorage()->SetTime( TTime(KTime) );
				}
			else
				{
				aField.DateTimeStorage()->SetTime( TTime(0) );
				}
			}
		break;
		default:
			User::Panic(aText,EInvariantFalse);
		break;
		}
	}

/**
sets aMany fields of a currently read / open item to aText
*/
void CContactsRamTest::SetManyFieldsL(const TDesC& aText, const TInt aMany, CContactItemFieldSet &fields)
	{
	TInt count = fields.Count();
	if( count > aMany )
		{
		count = aMany;
		}
	TInt i = 0;
	for(; i < count; ++i)
		{
		SetFieldL(fields[i],aText);
		}
	}
	
void CContactsRamTest::PrintHeap()
	{
	TInt totalAllocSize(0);
	User::Heap().AllocSize(totalAllocSize);
	
	test.Printf(_L("Client heap size: %d bytes\n"), totalAllocSize);
	iContactsDatabase->CntServerResourceCount();
	}

void CContactsRamTest::CloseView()
	{
	if(iRemoteView)
		{
		iRemoteView->Close(*this);
		iRemoteView = NULL;
		}
	}

void CleanupFiles()
	{
	TheFsSession.Delete(KContactsIniFileFullPath);
	TheFsSession.Delete(KContactsDbFileFullPath);
	TheFsSession.Delete(KContactsBackupRegistrationIniFileFullPath);
	}

/**

@SYMTestCaseID     PIM-T-CONTACTSRAMTEST-0001

*/
	
void DoTestsL()
    {
    test.SetLogged( ETrue );
	test.Start(_L("@SYMTESTCaseID:PIM-T-CONTACTSRAMTEST-0001 Test Ram usage for contacts model"));

	
	test.Printf(_L("Uninitilized Client Heap count: %d bytes, Heap Size: %d bytes\n"), User::Heap().Count(), User::Heap().Size());

	TInt error =TheFsSession.Connect();
	User::LeaveIfError(error);

	CleanupFiles();
	for ( manycontacts = 500; manycontacts <= KMaxContacts; manycontacts+=500 )
		{
		CContactsRamTest* res = CContactsRamTest::NewLC();	
		res->Activate();
		CActiveScheduler::Start();
		CleanupStack::PopAndDestroy(res);
		}
    test.End();
	test.Close();
	CleanupFiles();
	//cleanup
	CContactDatabase::DeleteDefaultFileL();
    }

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    if (!cleanupStack)
        {
        return KErrNoMemory;
        }

    CActiveScheduler* activeScheduler = new CActiveScheduler;
    if (!activeScheduler)
        {
        return KErrNoMemory;
        }
    CActiveScheduler::Install(activeScheduler);

    // Run the tests
    TRAPD(err, DoTestsL());

    // Cleanup
    delete activeScheduler;
    delete cleanupStack;
	__UHEAP_MARKEND;
	return err;
    }