contacts_plat/predictivesearch_client_api/tsrc/PsClientTestSuite/src/pstestqueryhandler.cpp
changeset 0 e686773b3f54
child 6 e8e3147d53eb
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //SYSTEM INCLUDES
       
    20 #include <vpbkeng.rsg>
       
    21 #include <collate.h>
       
    22 
       
    23 //USER INCLUDES
       
    24 #include "pstestqueryhandler.h"
       
    25 #include "CPsQuery.h"
       
    26 #include "CPsQueryItem.h"
       
    27 #include "CPsSettings.h"
       
    28 
       
    29 // ---------------------------------------------------------------------------------
       
    30 // 1st phase constructor
       
    31 // ---------------------------------------------------------------------------------
       
    32 CPsTestQueryHandler* CPsTestQueryHandler::NewL()
       
    33 {
       
    34 	CPsTestQueryHandler* self = new ( ELeave ) CPsTestQueryHandler();
       
    35 	CleanupStack::PushL( self );
       
    36 	self->ConstructL();
       
    37 	CleanupStack::Pop( self );
       
    38 	
       
    39 	return self;
       
    40 }
       
    41 
       
    42 // ---------------------------------------------------------------------------------
       
    43 // Constructor
       
    44 // ---------------------------------------------------------------------------------
       
    45 //
       
    46 CPsTestQueryHandler::CPsTestQueryHandler() 
       
    47 {
       
    48 }
       
    49 
       
    50 // ---------------------------------------------------------------------------------
       
    51 // CPsTestQueryHandler::ConstructL() 
       
    52 // 2nd phase constructor
       
    53 // ---------------------------------------------------------------------------------
       
    54 void CPsTestQueryHandler::ConstructL()
       
    55 {
       
    56   	iAsyncHandler = CPSRequestHandler::NewL();
       
    57     iAsyncHandler->AddObserverL(this); 
       
    58   	iSettings = CPsSettings::NewL();   
       
    59   	iPsQuery = CPsQuery::NewL();
       
    60   	iCurrentTestCase = EDefaultCase;
       
    61   	iCurrentErrorCode = KErrNone;
       
    62 }
       
    63 
       
    64 // ---------------------------------------------------------------------------------
       
    65 // Destructor
       
    66 // ---------------------------------------------------------------------------------
       
    67 CPsTestQueryHandler::~CPsTestQueryHandler() 
       
    68 {
       
    69 	if(iAsyncHandler)
       
    70 	{
       
    71 		delete iAsyncHandler;
       
    72 		iAsyncHandler = NULL;
       
    73 	}
       
    74 	if(iSettings)
       
    75 	{
       
    76 		delete iSettings;
       
    77 		iSettings = NULL;
       
    78 	}
       
    79 	if(iPsQuery)
       
    80 	{
       
    81 		delete iPsQuery;
       
    82 		iPsQuery = NULL;
       
    83 	} 
       
    84 	iContactsList.Reset();
       
    85 }
       
    86 
       
    87 // ---------------------------------------------------------------------------------
       
    88 // CPsTestQueryHandler::SetCurrentTestCase() 
       
    89 // 
       
    90 // ---------------------------------------------------------------------------------
       
    91 void CPsTestQueryHandler :: SetCurrentTestCase(TTestCase aCurrentTestCase)
       
    92 {
       
    93 	iCurrentTestCase = aCurrentTestCase;
       
    94 }
       
    95 
       
    96 // ---------------------------------------------------------------------------------
       
    97 // CPsTestQueryHandler::SetCurrentErrorCode() 
       
    98 // 
       
    99 // ---------------------------------------------------------------------------------
       
   100 void CPsTestQueryHandler :: SetCurrentErrorCode(TInt aCurrentErrorCode)
       
   101 {
       
   102 	iCurrentErrorCode = aCurrentErrorCode;
       
   103 }
       
   104 
       
   105 // ---------------------------------------------------------------------------------
       
   106 // CPsTestQueryHandler::CurrentErrorCode() 
       
   107 // 
       
   108 // ---------------------------------------------------------------------------------
       
   109 TInt CPsTestQueryHandler :: CurrentErrorCode()
       
   110 {
       
   111 	return(iCurrentErrorCode);
       
   112 }
       
   113 
       
   114 // ---------------------------------------------------------------------------------
       
   115 // CPsTestQueryHandler::GetAllContentsL() 
       
   116 // 
       
   117 // ---------------------------------------------------------------------------------
       
   118 void CPsTestQueryHandler::GetAllContentsL()
       
   119 {	
       
   120 	iAsyncHandler->GetAllContentsL();
       
   121 }
       
   122 
       
   123 // ---------------------------------------------------------------------------------
       
   124 // CPsTestQueryHandler::StartSearchL() 
       
   125 // 
       
   126 // ---------------------------------------------------------------------------------
       
   127 void CPsTestQueryHandler :: StartSearchL(const CPsQuery& aSearchQuery)
       
   128 {
       
   129     // Send a search request
       
   130 	iAsyncHandler->SearchL(aSearchQuery);
       
   131 }
       
   132 
       
   133 // ---------------------------------------------------------------------------------
       
   134 // CPsTestQueryHandler::TestSearchL() 
       
   135 // 
       
   136 // ---------------------------------------------------------------------------------
       
   137 void CPsTestQueryHandler :: TestSearchL(const TDesC& aBuf, TKeyboardModes aMode)
       
   138 {
       
   139 	FormTestSearchQueryL(aBuf, aMode);
       
   140 
       
   141     // Send a search request
       
   142 	iAsyncHandler->SearchL(*iPsQuery);
       
   143 }
       
   144 
       
   145 // ---------------------------------------------------------------------------------
       
   146 // CPsTestQueryHandler::InputSearchL() 
       
   147 // 
       
   148 // ---------------------------------------------------------------------------------
       
   149 void CPsTestQueryHandler :: InputSearchL(const CPsQuery& aSearchQuery, const TDesC& aInput, RPointerArray<TDesC>& aResultSet)
       
   150 {
       
   151 	SetCurrentErrorCode(KErrNotFound);
       
   152 	
       
   153 	//to obtain the result;
       
   154 	RPointerArray<TDesC> matchSet;
       
   155 	RArray<TPsMatchLocation> matchLocation;
       
   156 	
       
   157 	// send a search request
       
   158 	iAsyncHandler->SearchL(aSearchQuery, aInput, matchSet, matchLocation);
       
   159 	
       
   160 	// compare the obtained result with desired result set
       
   161 	if(matchSet.Count() == aResultSet.Count())
       
   162 	{
       
   163 		TInt count = matchSet.Count();
       
   164 		if(count == 0)
       
   165 		{
       
   166 			SetCurrentErrorCode(KErrNone);
       
   167 		}
       
   168 		for(TInt i = 0; i < count; i++)	
       
   169 		{
       
   170 			TBufC<20> buf1(*(aResultSet[i]));
       
   171 			for(TInt j = 0; j < count; j++)
       
   172 			{
       
   173 				TBufC<20> buf2(*(matchSet[j]));
       
   174 				if(buf1.Compare(buf2) == 0)
       
   175 				{
       
   176 					SetCurrentErrorCode(KErrNone);
       
   177 					break;
       
   178 				}
       
   179 			}
       
   180 		}
       
   181 	}
       
   182 }
       
   183 
       
   184 // ---------------------------------------------------------------------------------
       
   185 // CPsTestQueryHandler::SetSearchSettingsL() 
       
   186 // 
       
   187 // ---------------------------------------------------------------------------------
       
   188 void CPsTestQueryHandler :: SetSearchSettingsL(RPointerArray<TDesC>& aSearchUri, RArray<TInt>& aDisplayFields, TInt aMaxResults)
       
   189 {
       
   190     iSettings->SetSearchUrisL(aSearchUri);
       
   191     iSettings->SetDisplayFieldsL(aDisplayFields);
       
   192     iSettings->SetMaxResults(aMaxResults);
       
   193 
       
   194     iAsyncHandler->SetSearchSettingsL(*iSettings);
       
   195 }
       
   196 
       
   197 // ---------------------------------------------------------------------------------
       
   198 // CPsTestQueryHandler::FormTestSearchQueryL() 
       
   199 // 
       
   200 // ---------------------------------------------------------------------------------
       
   201 void CPsTestQueryHandler :: FormTestSearchQueryL(const TDesC& aBuf, TKeyboardModes aMode)
       
   202 {
       
   203 	for ( int i = 0; i < aBuf.Length(); i++ )
       
   204 	{
       
   205 	    CPsQueryItem* item = CPsQueryItem::NewL();
       
   206 	    item->SetCharacter(aBuf[i]);
       
   207 	 	if(aMode != EModeUndefined)
       
   208 	 	{
       
   209 	 		item->SetMode(aMode);	 		
       
   210 	 	}
       
   211 	 	/*
       
   212 	 	else 
       
   213 	 	{
       
   214 	 		_LIT(KCode,"i");
       
   215  			if( (iKeyboardModeString.Mid(i,1)).Compare(KCode) == 0)
       
   216  			{
       
   217  				item->SetMode(EItut);
       
   218  			}
       
   219  			else 
       
   220  			{
       
   221  				item->SetMode(EQwerty);
       
   222  			}
       
   223 	 	}
       
   224 	 	*/
       
   225 		iPsQuery->AppendL(*item);			
       
   226 	}
       
   227 	
       
   228 	CPsQueryItem* item1 = CPsQueryItem::NewL();
       
   229 	item1->SetCharacter(aBuf[0]);
       
   230 	item1->SetMode(EItut);
       
   231 	
       
   232 	iPsQuery->InsertL(*item1,0);
       
   233 	
       
   234 	iPsQuery->Remove(0);
       
   235 	
       
   236 	iPsQuery->InsertL(*item1,aBuf.Length()+2);
       
   237 	
       
   238 	iPsQuery->Remove(-1);
       
   239 }
       
   240 
       
   241 // ---------------------------------------------------------------------------------
       
   242 // CPsTestQueryHandler::HandlePsResultsUpdate() 
       
   243 // 
       
   244 // ---------------------------------------------------------------------------------
       
   245 void CPsTestQueryHandler :: HandlePsResultsUpdate(RPointerArray<CPsClientData>& searchResults, RPointerArray<TDesC>& /*searchSeqs*/)
       
   246 {
       
   247 	// search results should always be a subset of iContactslist
       
   248 	TInt itemstoshow = searchResults.Count();	
       
   249     switch(iCurrentTestCase)
       
   250     {
       
   251     	case EAddContact:
       
   252     	{
       
   253 			VerifyResult(searchResults, ETrue);
       
   254     		break;
       
   255     	}
       
   256     	case EDeleteContact:
       
   257     	case EAddContactNeg:
       
   258     	case EFilter:
       
   259     	{
       
   260     		VerifyResult(searchResults, EFalse);
       
   261     		break;
       
   262     	}
       
   263     	/*
       
   264     	case EPsData:
       
   265     	{
       
   266     		SwitchEPsData(searchResults);
       
   267     		break;
       
   268     	}
       
   269     	*/
       
   270     	case ETruncateResult:
       
   271     	{
       
   272     		SetCurrentErrorCode(KErrGeneral);
       
   273     		if(itemstoshow == (iContactsList.Count() - 2))
       
   274     		{
       
   275     		//	SetCurrentErrorCode(KErrNone);
       
   276     			VerifyResult(searchResults, ETrue);
       
   277     		}
       
   278     		break;
       
   279     	}
       
   280     	case ESort:
       
   281     	{
       
   282     		SetCurrentErrorCode(KErrNotFound);
       
   283 
       
   284     		// first verify if the contact items created are present in result set
       
   285     		VerifyResult(searchResults, ETrue);
       
   286     		
       
   287     		if(CurrentErrorCode() == KErrNone)
       
   288     		{
       
   289     			// then verify if the result is sorted or not
       
   290 	    		if( !VerifySortResult(searchResults) )
       
   291 	    		{
       
   292 	    			SetCurrentErrorCode(KErrGeneral);
       
   293 	    		}
       
   294     		}
       
   295     		
       
   296     		break;
       
   297     	}
       
   298     	default:
       
   299     	{
       
   300     		SetCurrentErrorCode(KErrNone);
       
   301     		break;
       
   302     	}
       
   303     }
       
   304 	CActiveScheduler::Stop();
       
   305 }
       
   306 
       
   307 // ---------------------------------------------------------------------------------
       
   308 // CPsTestQueryHandler::ShutDownL() 
       
   309 // 
       
   310 // ---------------------------------------------------------------------------------
       
   311 void CPsTestQueryHandler :: ShutDownL()
       
   312 {
       
   313 	iAsyncHandler->ShutdownServerL();
       
   314 }
       
   315 
       
   316 // ---------------------------------------------------------------------------------
       
   317 // CPsTestQueryHandler::Cancel() 
       
   318 // 
       
   319 // ---------------------------------------------------------------------------------
       
   320 void CPsTestQueryHandler :: Cancel()
       
   321 {
       
   322 	iAsyncHandler->CancelSearch();
       
   323 }
       
   324 
       
   325 // ---------------------------------------------------------------------------------
       
   326 // CPsTestQueryHandler::Version() 
       
   327 // 
       
   328 // ---------------------------------------------------------------------------------
       
   329 TVersion CPsTestQueryHandler :: Version()
       
   330 {
       
   331 	return (iAsyncHandler->Version());
       
   332 }
       
   333 
       
   334 // ---------------------------------------------------------------------------------
       
   335 // CPsTestQueryHandler::IsLanguageSupportedL() 
       
   336 // 
       
   337 // ---------------------------------------------------------------------------------
       
   338 TBool CPsTestQueryHandler::IsLanguageSupportedL(TLanguage aLang)
       
   339 {
       
   340 	return(iAsyncHandler->IsLanguageSupportedL(aLang));
       
   341 }
       
   342 
       
   343 // ---------------------------------------------------------------------------------
       
   344 // CPsTestQueryHandler::HandlePsErrorL() 
       
   345 // 
       
   346 // ---------------------------------------------------------------------------------
       
   347 void CPsTestQueryHandler :: HandlePsError(TInt /*aErrorCode*/)
       
   348 {
       
   349 }
       
   350 
       
   351 // ---------------------------------------------------------------------------------
       
   352 // CPsTestQueryHandler::SetContactsDetails() 
       
   353 // 
       
   354 // ---------------------------------------------------------------------------------
       
   355 void CPsTestQueryHandler :: SetContactsDetails(RPointerArray<CPsClientData>& aContactsList)
       
   356 {
       
   357 	for(TInt i =0 ; i < aContactsList.Count();i++)
       
   358 	{
       
   359 		iContactsList.Append(aContactsList[i]);
       
   360 	}
       
   361 }
       
   362 
       
   363 // ---------------------------------------------------------------------------------
       
   364 // CPsTestQueryHandler::VerifyResult() 
       
   365 // 
       
   366 // This function verifies 
       
   367 // 	for positive test case: if the contacts/ groups created are present in result set 
       
   368 //  for negative test case: contacts/ groups created are not be present in result set
       
   369 // ---------------------------------------------------------------------------------
       
   370 void CPsTestQueryHandler :: VerifyResult(RPointerArray<CPsClientData>& aSearchResults, TBool aResultType)
       
   371 {
       
   372 	SetCurrentErrorCode(KErrGeneral);
       
   373 	if(aSearchResults.Count() == 0 && !aResultType)
       
   374 	{
       
   375 		// result match is 0 AND is negative test case 
       
   376 		SetCurrentErrorCode(KErrNone);
       
   377 		return;
       
   378 	}
       
   379 	
       
   380 	for(TInt i = 0; i < aSearchResults.Count(); i++)
       
   381 	{
       
   382 		for(TInt j = 0; j < iContactsList.Count(); j++)
       
   383 		{
       
   384 			if(CompareByData(*(aSearchResults[i]), *(iContactsList[j])) == 0)
       
   385 			{
       
   386 				iContactsList[j]->SetId(aSearchResults[i]->Id());
       
   387 				if(aResultType)
       
   388 				{
       
   389 					SetCurrentErrorCode(KErrNone);
       
   390 					break;
       
   391 				}
       
   392 				else
       
   393 				{
       
   394 					SetCurrentErrorCode(KErrGeneral);
       
   395 					break;
       
   396 				}
       
   397 			}
       
   398 			else
       
   399 			{
       
   400 				if(aResultType)
       
   401 				{
       
   402 					SetCurrentErrorCode(KErrGeneral);
       
   403 				}
       
   404 				else
       
   405 				{
       
   406 					SetCurrentErrorCode(KErrNone);
       
   407 				}
       
   408 			}
       
   409 		}
       
   410 	}
       
   411 }
       
   412 
       
   413 // ---------------------------------------------------------------------------------
       
   414 // CPsTestQueryHandler::IndexForGroupDataL() 
       
   415 // 
       
   416 // 
       
   417 // ---------------------------------------------------------------------------------
       
   418 TInt CPsTestQueryHandler::IndexForGroupDataL()
       
   419 {
       
   420 	TInt grpIndex;
       
   421 	RArray<TInt> dataOrder;
       
   422 	TBufC<30> groupUri(KGroupUri);
       
   423 	iAsyncHandler->GetDataOrderL(groupUri, dataOrder);
       
   424 	
       
   425 	for(TInt i=0; i < dataOrder.Count(); i++)
       
   426 	{
       
   427 		if(dataOrder[i] == R_VPBK_FIELD_TYPE_LASTNAME)
       
   428 		{
       
   429 			grpIndex = i;
       
   430 		}
       
   431 	}
       
   432 	
       
   433 	return grpIndex;
       
   434 }
       
   435 
       
   436 // ---------------------------------------------------------------------------------
       
   437 // CPsTestQueryHandler::VerifySortResult() 
       
   438 // 
       
   439 // This function verifies if the result set is sorted or not
       
   440 // ---------------------------------------------------------------------------------
       
   441 TBool CPsTestQueryHandler::VerifySortResult(RPointerArray<CPsClientData>& aSearchResults)
       
   442 {
       
   443 	TBool isSorted = EFalse;
       
   444 	TInt i = 0;
       
   445 	
       
   446 	for(i = 0; i < aSearchResults.Count() - 1 ; i++)
       
   447 	{
       
   448 		if(CompareByData(*(aSearchResults[i]), *(aSearchResults[i+1])) > 0)	
       
   449 		{
       
   450 			break;
       
   451 		}
       
   452 	}
       
   453 	
       
   454 	if( i == aSearchResults.Count() - 1)
       
   455 	{
       
   456 		isSorted = ETrue;
       
   457 	}
       
   458 	return isSorted;
       
   459 }
       
   460 
       
   461 void CPsTestQueryHandler::SwitchEPsData(RPointerArray<CPsClientData>& searchResults)
       
   462 {
       
   463 	TInt itemstoshow = searchResults.Count();		
       
   464 	SetCurrentErrorCode(KErrNotFound);
       
   465 	for(TInt i = 0; i < itemstoshow; i++)
       
   466 	{
       
   467         /*
       
   468         InsertContactId(searchResults[i]->Id(),i);
       
   469         if((iFirstName.Compare(searchResults[i]->Data(0)->Des()) == 0) && (iLastName.Compare(searchResults[i]->Data(1)->Des())==0) && (iCompanyName.Compare(searchResults[i]->Data(2)->Des())==0))
       
   470         {
       
   471             if(searchResults[i]->IsDataMatch(0) == EFalse && searchResults[i]->IsDataMatch(1) == EFalse && searchResults[i]->IsDataMatch(2))
       
   472             {
       
   473                 SetCurrentErrorCode(KErrNone);
       
   474             }
       
   475 
       
   476             TBool fl = searchResults[i]->IsWordMatch(2,0);
       
   477         }
       
   478         */
       
   479 		for(TInt j = 0; j < iContactsList.Count(); j++)
       
   480 		{
       
   481 			// compare searchResult[i] with iContactsList[j]
       
   482             /*
       
   483 		 	if((CompareByData(*(searchResults[i]), *(iContactsList[j])) == 0) &&
       
   484 			(searchResults[i]->IsDataMatch(0) == EFalse && searchResults[i]->IsDataMatch(1) == EFalse && searchResults[i]->IsDataMatch(2)))
       
   485 			{
       
   486 				SetCurrentErrorCode(KErrNone);
       
   487 				iContactsList[j]->SetId(searchResults[i]->Id());
       
   488 				break;
       
   489 			}
       
   490 			else
       
   491 			{
       
   492 				SetCurrentErrorCode(KErrNotFound);
       
   493 			}
       
   494             */
       
   495 		}
       
   496 	}
       
   497 }
       
   498 
       
   499 void CPsTestQueryHandler::CachingStatus(TCachingStatus& /*aStatus*/, TInt& /*aError*/)
       
   500 {
       
   501 	// implement later
       
   502 }
       
   503 
       
   504 TInt CPsTestQueryHandler::CompareByData ( const CPsClientData& aObject1, const CPsClientData& aObject2 )
       
   505 {
       
   506 	TBuf<KBufferMaxLen> data1;
       
   507      TBuf<255> data2;
       
   508      
       
   509      _LIT(KSpace, " ");
       
   510      
       
   511      data1 = KNullDesC;
       
   512      for ( int i = 0; i < aObject1.DataElementCount(); i++ )
       
   513      {
       
   514      	 if ( aObject1.Data(i) )
       
   515 	     {
       
   516 	     	data1 += aObject1.Data(i)->Des();
       
   517 	     	data1 += KSpace;
       
   518 	     }	     
       
   519      }
       
   520      
       
   521      // Trim the unnecessary white spaces before we compare
       
   522      data1.TrimAll();
       
   523      
       
   524      data2 = KNullDesC;
       
   525      for ( int i = 0; i < aObject2.DataElementCount(); i++ )
       
   526      {
       
   527      	 if ( aObject2.Data(i) )
       
   528 	     {
       
   529 	     	data2 += aObject2.Data(i)->Des();
       
   530 	     	data2 += KSpace;
       
   531 	     }	     
       
   532      }   
       
   533      
       
   534      // Trim the unnecessary white spaces before we compare 
       
   535      data2.TrimAll();   
       
   536      
       
   537 	 // Get the standard method
       
   538 	 TCollationMethod meth = *Mem::CollationMethodByIndex( 0 );
       
   539   	 meth.iFlags |= TCollationMethod::EIgnoreNone;
       
   540   	 meth.iFlags |= TCollationMethod::EFoldCase;
       
   541 
       
   542 	 // Magic: Collation level 3 is used
       
   543 	 TInt comparison(data1.CompareC( data2, 3, &meth ));
       
   544 	 return comparison;
       
   545 }
       
   546 // End of file