phonebookengines_old/contactsmodel/tsrc/T_PreFixFullSearch.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <cntdef.h>
       
    18 #include <cntdb.h>
       
    19 #include <cntitem.h>
       
    20 #include <cntfield.h>
       
    21 #include <cntfldst.h>
       
    22 #include <cntviewbase.h>
       
    23 #include <cntview.h>
       
    24 #include "CContactViewEventQueue.h"
       
    25 #include "cfindtext.h"
       
    26 #include "t_utils2.h"
       
    27 #include "T_UTILS.H"
       
    28 
       
    29 _LIT(KTestName, "T_PreFixFullSearch");
       
    30 
       
    31 _LIT(KTestDbName, "c:T_PreFixFullSearch.cdb");
       
    32 
       
    33 const TInt KMaxNumContacts=1000;
       
    34 
       
    35 LOCAL_D RTest test(KTestName);
       
    36 
       
    37 class CTestResources : public CBase
       
    38     {
       
    39     public:
       
    40         static CTestResources* NewLC();
       
    41         void ConstructL();
       
    42         void CreateTestContactsL();
       
    43 		void CreateMoreTestContactsL();
       
    44         ~CTestResources();
       
    45 
       
    46         CContactDatabase* iDb;
       
    47         CContactViewEventQueue* iViewEventQueue;
       
    48         RContactViewSortOrder iViewSortOrder;
       
    49         CContactLocalView* iLocalView;
       
    50         CContactViewEventQueue* iFindViewEventQueue;
       
    51         CContactFindView* iFindView;
       
    52         CFindText* iFindText;
       
    53     };
       
    54 
       
    55 CTestResources* CTestResources::NewLC()
       
    56     {
       
    57     CTestResources* self = new(ELeave) CTestResources;
       
    58     CleanupStack::PushL(self);
       
    59     self->ConstructL();
       
    60     return self;
       
    61     }
       
    62 
       
    63 void CTestResources::ConstructL()
       
    64     {
       
    65     iDb = CContactDatabase::ReplaceL(KTestDbName);
       
    66 
       
    67     CreateTestContactsL();
       
    68 
       
    69     iViewEventQueue = CContactViewEventQueue::NewL();
       
    70 
       
    71     iViewSortOrder.AppendL(KUidContactFieldFamilyName);
       
    72     iViewSortOrder.AppendL(KUidContactFieldGivenName);
       
    73     iViewSortOrder.AppendL(KUidContactFieldCompanyName);
       
    74 
       
    75     const TContactViewPreferences prefs =
       
    76         static_cast<TContactViewPreferences>(EContactsOnly);
       
    77     iLocalView = CContactLocalView::NewL
       
    78         (*iViewEventQueue, *iDb, iViewSortOrder, prefs);
       
    79 
       
    80     // Wait for view to get ready
       
    81     TContactViewEvent event;
       
    82     __ASSERT_ALWAYS(iViewEventQueue->ListenForEvent(10,event),User::Invariant());
       
    83     __ASSERT_ALWAYS(event.iEventType == TContactViewEvent::EReady ,User::Invariant());
       
    84   
       
    85     }
       
    86 
       
    87 void CTestResources::CreateTestContactsL()
       
    88     {
       
    89 
       
    90     // Create a few test contacts in the db
       
    91 	// - Creating 'prefix' results for 'an'
       
    92     CContactCard* card;
       
    93     CContactItemField* field;
       
    94     card = CContactCard::NewLC();
       
    95     field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
    96     field->TextStorage()->SetTextL(_L("Annie"));
       
    97     card->AddFieldL(*field);
       
    98     CleanupStack::Pop(field);
       
    99     iDb->AddNewContactL(*card);
       
   100     CleanupStack::PopAndDestroy(card);
       
   101 
       
   102     card = CContactCard::NewLC();
       
   103     field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
   104     field->TextStorage()->SetTextL(_L("Anne"));
       
   105     card->AddFieldL(*field);
       
   106     CleanupStack::Pop(field);
       
   107     iDb->AddNewContactL(*card);
       
   108     CleanupStack::PopAndDestroy(card);
       
   109 
       
   110     card = CContactCard::NewLC();
       
   111     field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
   112     field->TextStorage()->SetTextL(_L("Andrew"));
       
   113     card->AddFieldL(*field);
       
   114     CleanupStack::Pop(field);
       
   115     iDb->AddNewContactL(*card);
       
   116     CleanupStack::PopAndDestroy(card);
       
   117 
       
   118 	// - Creating 'fullsearch' results for 'an'
       
   119     card = CContactCard::NewLC();
       
   120     field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
   121     field->TextStorage()->SetTextL(_L("Christan"));
       
   122     card->AddFieldL(*field);
       
   123     CleanupStack::Pop(field);
       
   124     iDb->AddNewContactL(*card);
       
   125     CleanupStack::PopAndDestroy(card);
       
   126 
       
   127     card = CContactCard::NewLC();
       
   128     field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
   129     field->TextStorage()->SetTextL(_L("Adan"));
       
   130     card->AddFieldL(*field);
       
   131     CleanupStack::Pop(field);
       
   132     iDb->AddNewContactL(*card);
       
   133     CleanupStack::PopAndDestroy(card);
       
   134 
       
   135     }
       
   136 
       
   137 
       
   138 void CTestResources::CreateMoreTestContactsL()
       
   139 	{
       
   140 	CRandomContactGenerator* generator = CRandomContactGenerator::NewL();
       
   141 	CleanupStack::PushL(generator);
       
   142 	generator->SetDbL(*iDb);
       
   143 	for (TInt i=0;i<KMaxNumContacts;i++)
       
   144 		{
       
   145 		generator->AddTypicalRandomContactL();
       
   146 		if (i%100==0)
       
   147 			{
       
   148 			iDb->CompactL();
       
   149 			test.Printf(_L("Added %d contacts\n"),i);
       
   150 			}
       
   151 		}
       
   152 	CleanupStack::PopAndDestroy(generator);
       
   153 
       
   154 	}
       
   155 
       
   156 CTestResources::~CTestResources()
       
   157     {
       
   158     if (iFindView) iFindView->Close(*iFindViewEventQueue);
       
   159     delete iFindViewEventQueue;
       
   160     delete iFindText;
       
   161     if (iLocalView) iLocalView->Close(*iViewEventQueue);
       
   162     iViewSortOrder.Close();
       
   163     delete iViewEventQueue;
       
   164     delete iDb;
       
   165     TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
       
   166     }
       
   167 
       
   168 /**
       
   169  * Simulates using CContactFindView to filter contacts as the user types
       
   170  * characters to an UI "find box".
       
   171  */
       
   172 
       
   173 
       
   174 LOCAL_C TInt FullSearchL(const TDesC& aDes, CTestResources* aTestResources)
       
   175     {
       
   176 	CTestResources* res = NULL;
       
   177 	HBufC* dummy = NULL;
       
   178 	CFindText* newFindText = NULL;
       
   179 	TContactViewEvent event;
       
   180 	TInt retval = 0;
       
   181 
       
   182 	test.Next(_L("Preform a full search only"));
       
   183 
       
   184 	if (aTestResources == NULL)
       
   185 		{
       
   186 		res = CTestResources::NewLC();
       
   187 		}
       
   188 	else
       
   189 		{
       
   190 		res = aTestResources;
       
   191 		}
       
   192 
       
   193     dummy = HBufC::NewLC(aDes.Length() * aDes.Length());
       
   194     newFindText = CFindText::NewLC(aDes);
       
   195     res->iFindViewEventQueue = CContactViewEventQueue::NewL();
       
   196     res->iFindView = CContactFindView::NewL(*res->iDb, *res->iLocalView, *res->iFindViewEventQueue, newFindText);
       
   197 
       
   198 	do
       
   199 		{
       
   200 		__ASSERT_ALWAYS(res->iFindViewEventQueue->ListenForEvent(10,event),User::Invariant());
       
   201 		}
       
   202 	while (event.iEventType != TContactViewEvent::EReady);
       
   203 	__ASSERT_ALWAYS(event.iEventType == TContactViewEvent::EReady,User::Invariant());
       
   204 
       
   205     retval = res->iFindView->CountL();
       
   206 
       
   207 
       
   208     CleanupStack::PopAndDestroy(newFindText);
       
   209 	CleanupStack::PopAndDestroy(dummy);
       
   210 	if (aTestResources == NULL)
       
   211 		{
       
   212 		CleanupStack::PopAndDestroy(res);
       
   213 		}
       
   214 	return retval;
       
   215     }
       
   216 
       
   217 LOCAL_C TInt PreFixSearchL(const TDesC& aDes, CTestResources* aTestResources)
       
   218     {
       
   219 	CTestResources* res = NULL;
       
   220 	HBufC* dummy = NULL;
       
   221 	CFindText* newFindText = NULL;
       
   222 	TContactViewEvent event;
       
   223 	TInt retval = 0;
       
   224 
       
   225 	test.Next(_L("Preform a prefix search only"));
       
   226 
       
   227 	if (aTestResources == NULL)
       
   228 		{
       
   229     	res = CTestResources::NewLC();
       
   230 		}
       
   231 	else
       
   232 		{
       
   233 		res = aTestResources;
       
   234 		}
       
   235     dummy = HBufC::NewLC(aDes.Length() * aDes.Length());
       
   236     newFindText = CFindText::NewLC(aDes);
       
   237     res->iFindViewEventQueue = CContactViewEventQueue::NewL();
       
   238     res->iFindView = CContactFindView::NewL(*res->iDb, *res->iLocalView, *res->iFindViewEventQueue, newFindText, CContactViewBase::EPrefixSearch);
       
   239 
       
   240 	do
       
   241 		{
       
   242 		__ASSERT_ALWAYS(res->iFindViewEventQueue->ListenForEvent(10,event),User::Invariant());
       
   243 		}
       
   244 	while (event.iEventType != TContactViewEvent::EReady);
       
   245 	__ASSERT_ALWAYS(event.iEventType == TContactViewEvent::EReady,User::Invariant());
       
   246 
       
   247     //__ASSERT_ALWAYS(res->iFindViewEventQueue->ListenForEvent(10,event),User::Invariant());
       
   248 	//__ASSERT_ALWAYS(event.iEventType == TContactViewEvent::EReady,User::Invariant());
       
   249 
       
   250     retval = res->iFindView->CountL();
       
   251 
       
   252 
       
   253     CleanupStack::PopAndDestroy(newFindText);
       
   254 	CleanupStack::PopAndDestroy(dummy);
       
   255 
       
   256 	if (aTestResources == NULL)
       
   257 		{
       
   258 		CleanupStack::PopAndDestroy(res);
       
   259 		}
       
   260 	return retval;
       
   261     }
       
   262 
       
   263 /** Profile the speed of CContactLocalView::ContactsMatchingCriteriaL */
       
   264 LOCAL_C void AddNewRandomContactsL(CTestResources* res)
       
   265 	{
       
   266 	//CTestResources* res = CTestResources::NewLC();
       
   267 	res->CreateMoreTestContactsL();
       
   268 
       
   269 	while (res->iLocalView->CountL()<KMaxNumContacts)
       
   270 		{
       
   271 		TContactViewEvent event;
       
   272 		__ASSERT_ALWAYS(res->iViewEventQueue->ListenForEvent(10,event), User::Invariant());
       
   273 
       
   274 		if (event.iEventType == TContactViewEvent::EUnavailable)
       
   275 			{
       
   276 			// In the event of queue over flow, the view becomes unavailable
       
   277 			// this is valid behaviour, as view will be refreshed completely
       
   278 			break;
       
   279 			}
       
   280 		
       
   281 		__ASSERT_ALWAYS(event.iEventType == TContactViewEvent::EItemAdded , User::Invariant());
       
   282 		}
       
   283 	}
       
   284 
       
   285 LOCAL_C void TimeSearchesL(const TDesC& aDes)
       
   286 	{
       
   287 	CTestResources* res = CTestResources::NewLC();
       
   288 
       
   289 	AddNewRandomContactsL(res);
       
   290 
       
   291 
       
   292 	CCntTest::ProfileReset(0,2);
       
   293 	CCntTest::ProfileStart(0);
       
   294 	FullSearchL(aDes, res);
       
   295 	CCntTest::ProfileEnd(0);
       
   296 
       
   297 	if (res->iFindView != NULL)
       
   298 		{
       
   299 		delete res->iFindView;
       
   300 		res->iFindView = NULL;
       
   301 		}
       
   302 	if (res->iFindViewEventQueue != NULL)
       
   303 		{
       
   304 		delete res->iFindViewEventQueue;
       
   305 		res->iFindViewEventQueue = NULL;
       
   306 		}
       
   307 
       
   308 	CCntTest::ProfileStart(1);
       
   309 	PreFixSearchL(aDes, res);
       
   310 	CCntTest::ProfileEnd(1);
       
   311 
       
   312 
       
   313 	TCntProfile profile[2];
       
   314 	CCntTest::ProfileResult(profile,0,2);
       
   315 	_LIT(KFullSearch,"FullSearch %d.%03d sec (%d hits)\n");
       
   316 	_LIT(KPrefixSearch,"PrefixSearch %d.%03d sec (%d hits)\n");
       
   317 	test.Printf(KFullSearch,profile[0].iTime/1000000, profile[0].iTime%1000000, profile[0].iCount);
       
   318 	RDebug::Print(KFullSearch,profile[0].iTime/1000000, profile[0].iTime%1000000, profile[0].iCount);
       
   319 	test.Printf(KPrefixSearch,profile[1].iTime/1000000, profile[1].iTime%1000000, profile[1].iCount);
       
   320 	RDebug::Print(KPrefixSearch,profile[1].iTime/1000000, profile[1].iTime%1000000, profile[1].iCount);
       
   321 	CleanupStack::PopAndDestroy(res);
       
   322 	}
       
   323 
       
   324 /**
       
   325 
       
   326 @SYMTestCaseID     PIM-T-PREFIXFULLSEARCH-0001
       
   327 
       
   328 */
       
   329 
       
   330 void DoTestsL()
       
   331     {
       
   332 	_LIT(KSearchString, "an");
       
   333 	test.Start(_L("@SYMTESTCaseID:PIM-T-PREFIXFULLSEARCH-0001 T_PreFixFullSearch"));
       
   334 
       
   335 
       
   336 	__UHEAP_MARK;
       
   337     FullSearchL(KSearchString, NULL);
       
   338     __UHEAP_MARKEND;
       
   339 
       
   340     __UHEAP_MARK;
       
   341     PreFixSearchL(KSearchString, NULL);
       
   342     __UHEAP_MARKEND;
       
   343 
       
   344 	TimeSearchesL(KSearchString);
       
   345 
       
   346     test.End();
       
   347     }
       
   348 
       
   349 GLDEF_C TInt E32Main()
       
   350 	{
       
   351     // Init
       
   352 	__UHEAP_MARK;
       
   353 	RProcess().SetPriority(EPriorityBackground);
       
   354     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   355     if (!cleanupStack)
       
   356         {
       
   357         return KErrNoMemory;
       
   358         }
       
   359 
       
   360     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   361     if (!activeScheduler)
       
   362         {
       
   363         return KErrNoMemory;
       
   364         }
       
   365     CActiveScheduler::Install(activeScheduler);
       
   366 
       
   367 	//Get rid of the warnings.
       
   368 	//T_utils2 uses efsrv.dll, but we don't use this functions in this test code.
       
   369 	//So we pretend to use efsrv to keep T_utils2.obj and linker happy
       
   370 	RFs fs;
       
   371 	fs.Connect();
       
   372 	fs.Close();
       
   373 
       
   374     // Run the tests
       
   375     TRAPD(err, DoTestsL());
       
   376 
       
   377     // Cleanup
       
   378     delete activeScheduler;
       
   379     delete cleanupStack;
       
   380 	test.Close();
       
   381 	__UHEAP_MARKEND;
       
   382 	return err;
       
   383     }