summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_filterviewspeed.cpp
blob: 1e2dce6da8d8b15cf252c9465f4ac7c35709bec2 (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
/*
* 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: 
* This test the speed of how fast it takes to filter a view, with various numbers of contacts. 
* This can be automatic when run as a part of the test suite on the emulator, but 
* on some hardware this can blow up since it can take up more memory that the device has.
* This does not mean the test fails (it's just profiling speed), it just mean the full set of tests can't be run.
* This for for testing defect INC038634 - Contact View filtered view takes too long to open.
*
*/

#include <cntdb.h>
#include <cntdef.h>
#include <cntitem.h>
#include <cntfield.h>
#include <cntfldst.h>
#include <cntview.h>

// System includes
#include <e32std.h>
#include <e32twin.h>
#include <e32test.h>
#include <cntdbobs.h>

// User includes
#include "t_utils2.h"

// optional clean up (might want to turn off if you want to keep the db for further tests)
#define DELETE_FILES_WHEN_DONE

//
// Constants.
//

_LIT(KTestName,"t_filterviewspeed");

const TInt KNewContactsPerTest[] = {50, 100, 200, 400, 800, 500, 500, 1000};
const TInt KNumberOfTests=8;
const TInt  KMaxNumberOfFilters = 6;
const TInt  KMaxFilterBits = 10;
const TInt KPrintFreq=50;

// Globals

/** This is used to keep track of the expected numbers to match each filter*/
class TFilterCounter
	{
	public: 
		TInt AddBits(TInt aBits);
		void Reset();
		void Print();
		TFilterCounter();
		TInt Count(CContactDatabase::TContactViewFilter aFilter);

	private: 
		TInt iCounter[KMaxFilterBits];
		void PrintFilterName(CContactDatabase::TContactViewFilter aFilter);
	};




/** Wait for the filter to finish and state results*/
class CWatcher : public CActive, MContactViewObserver
	{
	public:
		static CWatcher* NewL(CContactDatabase::TContactViewFilter aFilter);
		void HandleContactViewEvent(const CContactViewBase& aView,const TContactViewEvent& aEvent);
		~CWatcher();

	private:
		CWatcher(CContactDatabase::TContactViewFilter aFilter);
		void ConstructL();
		void DoCancel();
		void RunL();
		TInt RunError(TInt aTest);
		void CreateFilterL();
		void DoneFilteringL();

		TTime iInitTime;
		TTime iStartFilterTime;
		CContactDatabase* iDatabase;
		RContactViewSortOrder iViewSortOrder;
		CContactRemoteView* iView;
		CContactFilteredView* iFilterer;
		CContactDatabase::TContactViewFilter iFilter;
		TBool iStarted;
		TBool iTestNumber;

	};


CContactIdArray* PopulateTestDatabaseLC(CContactDatabase& aDatabase, TInt aCount );

LOCAL_D RTest test(KTestName);
LOCAL_D TFilterCounter gCounter;



CWatcher::~CWatcher()
	{
	if(iView)
		iView->Close(*this);
	iViewSortOrder.Close();
	if(iFilterer)
		iFilterer->Close(*this);		
	delete iDatabase;
	}

CWatcher::CWatcher(CContactDatabase::TContactViewFilter aFilter) :CActive(0), iFilter(aFilter)	{}

CWatcher* CWatcher::NewL(CContactDatabase::TContactViewFilter aFilter)
	{
	CWatcher* self = new(ELeave) CWatcher(aFilter);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return(self);
	}


void CWatcher::ConstructL()
	{
	iInitTime.UniversalTime();
	// open new db
	iDatabase = CContactDatabase::ReplaceL();

	// create contacts
	CContactIdArray& contacts = *PopulateTestDatabaseLC(*iDatabase, KNewContactsPerTest[iTestNumber] );
	gCounter.Print();

	// create view
	iViewSortOrder.AppendL(KUidContactFieldFamilyName);
   	iViewSortOrder.AppendL(KUidContactFieldGivenName);
    iViewSortOrder.AppendL(KUidContactFieldCompanyName);
	iView = CContactRemoteView::NewL(*this, *iDatabase, iViewSortOrder, EContactsOnly);
	CleanupStack::PopAndDestroy( &contacts );
	CActiveScheduler::Add(this);
	}


void CWatcher::DoCancel() {}

/** Try to start the view anyway, even if we can't get the desired number of contacts */
TInt CWatcher::RunError(TInt /*aTest*/)
	{
	if(!iView)
		{
		TRAPD(err,
			iView = CContactRemoteView::NewL(*this, *iDatabase, iViewSortOrder, EContactsOnly)
		);
		return err;
		}
	return KErrNone;
	}

/**  add more contacts and make a new remote view*/
void CWatcher::RunL()
	{
	iView->Close(*this);		// delete it
	iView = NULL;
	CContactIdArray& contacts = *PopulateTestDatabaseLC(*iDatabase, KNewContactsPerTest[iTestNumber] );
	gCounter.Print();
	CleanupStack::PopAndDestroy( &contacts );
	iView = CContactRemoteView::NewL(*this, *iDatabase, iViewSortOrder, EContactsOnly);
	}

/** when the remote view is ready start the filter. When the filter is ready end the test (or go to next test)*/


void CWatcher::HandleContactViewEvent(const CContactViewBase& aView,const TContactViewEvent& aEvent)
	{
	TInt err1(KErrNone);
	TInt err2(KErrNone);
	if(!iStarted && &aView == iView && TContactViewEvent::EReady==aEvent.iEventType) 
		{
		iStarted = ETrue;
		test.Next(_L("Filter time test"));

		TRAP(err1, CreateFilterL() );
		}

	if(iStarted && &aView == iFilterer && TContactViewEvent::EReady==aEvent.iEventType) 
		{
		TRAP(err2, DoneFilteringL() );
		iStarted = EFalse;
		}

	if (err1 || err2)
		{
		_LIT(KErrMsg, "Contact view error in CWatcher::HandleContactViewEvent()\n");
		test.Printf(KErrMsg);
		User::Invariant();
		}
	}

/** start timing and make a new filter */
void CWatcher::CreateFilterL()
	{
	iStartFilterTime.UniversalTime();
	iFilterer = CContactFilteredView::NewL(*this, *iDatabase, *iView, iFilter);
	}

/** report the time and either end the tests or go to the next one */
void CWatcher::DoneFilteringL()
	{
	TTime stopTime;
	stopTime.UniversalTime();
	TTimeIntervalMicroSeconds elapsedTime = stopTime.MicroSecondsFrom(iStartFilterTime);
	test.Printf(_L("Number of Contacts: %i\t\t Test time: %ims\n"), iView->CountL(), elapsedTime.Int64()/1000);
	test(gCounter.Count(iFilter) ==	iFilterer->CountL());
	iFilterer->Close(*this);		// this deletes iFilterer
	iFilterer=NULL;
	iTestNumber++;
	if(iTestNumber==KNumberOfTests)
		{
		CActiveScheduler::Stop();
		}
	else 
		{
		TRequestStatus* stat = &iStatus;
		User::RequestComplete(stat,KErrNone);
		SetActive();
		}
	}



TFilterCounter::TFilterCounter()
	{
	Reset();
	}

void TFilterCounter::Reset()
	{
	for(TInt i=0;i<KMaxFilterBits;i++)
		iCounter[i]=0;
	}


TInt TFilterCounter::AddBits(TInt aBits)
	{
	for(TInt i=0;i<KMaxFilterBits;i++)
		{
		if ((1<<i) & aBits) 
			iCounter[i]++;
		}
	return aBits;
	}

void TFilterCounter::PrintFilterName(CContactDatabase::TContactViewFilter aFilter)
	{
	switch(aFilter)
		{
		case CContactDatabase::EUnfiltered: test.Printf(_L("Unfiltered"));break;
		case CContactDatabase::EMailable: test.Printf(_L("Mailable"));break;
		case CContactDatabase::ESmsable: test.Printf(_L("Smsable"));break;
		case CContactDatabase::ELandLine: test.Printf(_L("LandLine"));break;
		case CContactDatabase::EFaxable: test.Printf(_L("Faxable"));break;
		case CContactDatabase::EPhonable: test.Printf(_L("Phonable"));break;
		case CContactDatabase::EWork: test.Printf(_L("Work"));break;
		case CContactDatabase::EHome: test.Printf(_L("Home"));break;
		case CContactDatabase::ERingTone: test.Printf(_L("RingTone"));break;
		case CContactDatabase::EVoiceDial: test.Printf(_L("VoiceDial"));break;
		case CContactDatabase::EIMAddress: test.Printf(_L("IMAddress"));break;
		case CContactDatabase::EWirelessVillage: test.Printf(_L("WirelessVillage"));break;
		case CContactDatabase::ECustomFilter1: test.Printf(_L("CustomFilter1"));break;
		case CContactDatabase::ECustomFilter2: test.Printf(_L("CustomFilter2"));break;
		case CContactDatabase::ECustomFilter3: test.Printf(_L("CustomFilter3"));break;
		case CContactDatabase::ECustomFilter4: test.Printf(_L("CustomFilter4"));break;
		}
	}

void TFilterCounter::Print()
	{
	for(TInt i=0;i<KMaxNumberOfFilters;i++)
		{
		PrintFilterName((CContactDatabase::TContactViewFilter) (1<<i));
		test.Printf(_L(": %i\t"), iCounter[i]);
		}
	test.Printf(_L("\n"));;
	}

TInt TFilterCounter::Count(CContactDatabase::TContactViewFilter aFilter)
	{
	for(TInt i=0;i<KMaxFilterBits;i++)
		{
		if((1<<i) == aFilter)
			return iCounter[i];
		}
	return 0;
	}


/** Ranomly generate up to KMaxNumberOfFilters (6)  filters for a contact*/
TInt RandomFilterBits(TInt64& aSeed)
	{
	TInt bits = 0;
	TInt i;
	for(i=Math::Rand(aSeed)%KMaxNumberOfFilters;i>=0;i--)
		{
		bits |= 1 << (Math::Rand(aSeed)%(KMaxFilterBits+1));		// random TContactViewFilter
		}
	return bits;
	}

/** add aCount random contacts to the open database. The 1st item will always be the own card */
CContactIdArray* PopulateTestDatabaseLC(CContactDatabase& aDatabase, TInt aCount )
	{
	__ASSERT_ALWAYS(aCount>0, User::Invariant());
	CContactIdArray* contacts = CContactIdArray::NewLC();
	CRandomContactGenerator* generator = CRandomContactGenerator::NewL();
	CleanupStack::PushL( generator );
	generator->SetDbL(aDatabase);

	TInt64 seed=47;
	TContactItemId itemId;
	for(TInt counter = 0;counter<aCount;counter++)
		{
 		itemId = generator->AddTypicalContactForFilterL(gCounter.AddBits(RandomFilterBits(seed)));
		if(counter%KPrintFreq==0)
			{
			aDatabase.CompactL();
			test.Printf(_L("Added contact %d: %i\n"), counter, itemId);
			}
		contacts->AddL(itemId);
		}
	CleanupStack::PopAndDestroy( generator );
	return contacts;
	}

/** Delete default contacts DB ignoring any errors
	@return KErrNone if the DB is deleted */
TInt DeleteDbFile()
	{		
	TRAPD(err,CContactDatabase::DeleteDefaultFileL())
	return err;
	}

void TestL(CContactDatabase::TContactViewFilter aFilter)
	{

	CActiveScheduler* scheduler=new (ELeave) CActiveScheduler;
	CActiveScheduler::Install(scheduler);
	CleanupStack::PushL( scheduler );


	CWatcher* watch = CWatcher::NewL(aFilter);
	
	CActiveScheduler::Start();
	delete watch;

#ifdef DELETE_FILES_WHEN_DONE
	DeleteDbFile(); //cntmodel need CActiveScheduler to run this function
#endif

	CleanupStack::PopAndDestroy(scheduler );
	}



void doMainL()
	{
	__UHEAP_MARK;
	TestL(CContactDatabase::EMailable);
	__UHEAP_MARKEND;
	}



/**

@SYMTestCaseID     PIM-T-FILTERVIEWSPEED-0001

*/

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	test.Start(_L("@SYMTESTCaseID:PIM-T-FILTERVIEWSPEED-0001 "));

	CTrapCleanup* cleanup=CTrapCleanup::New();
	if (cleanup)
		{
		//Get rid of the warnings.
		//T_utils2 uses efsrv.dll, but we don't use this functions in this test code.
		//So we pretend to use efsrv to keep T_utils2.obj and linker happy
		RFs fs;
		fs.Connect();
		fs.Close();

		TRAP_IGNORE(doMainL());	
		/* The error does not matter. It could be out of memory or disk full or others, 
		but the only errors that matter are the ones tested for already.
		This tests what it can and if there's not enough room to test more, it stops*/
		delete cleanup;
		}
	test.End();
	test.Close();
	
	__UHEAP_MARKEND;
	return KErrNone;
    }