contacts_plat/predictivesearch_utils_api/tsrc/PsUtilsTestSuite/src/TestSuiteInputData.cpp
changeset 0 e686773b3f54
child 39 a6539d1e8e43
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 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 // INCLUDE FILES
       
    20 #include <StifParser.h>
       
    21 #include <CPsQueryItem.h>
       
    22 #include <collate.h>
       
    23 #include "TestSuiteInputData.h"
       
    24 
       
    25 // ============================== MEMBER FUNCTIONS ============================
       
    26 
       
    27 // ----------------------------------------------------------------------------
       
    28 // Two Phase constructor
       
    29 // ----------------------------------------------------------------------------
       
    30 CTestSuiteInputData* CTestSuiteInputData::NewL(CStifItemParser& aItem)
       
    31 {
       
    32 	CTestSuiteInputData* self = new (ELeave) CTestSuiteInputData();	
       
    33     CleanupStack::PushL(self);
       
    34     self->ConstructL(aItem);
       
    35     CleanupStack::Pop();
       
    36     return self;
       
    37 }
       
    38 
       
    39 // ----------------------------------------------------------------------------
       
    40 // Default constructor
       
    41 // ----------------------------------------------------------------------------
       
    42 CTestSuiteInputData::CTestSuiteInputData()
       
    43 {
       
    44 }
       
    45 
       
    46 
       
    47 // ----------------------------------------------------------------------------
       
    48 // Destructor
       
    49 // ----------------------------------------------------------------------------
       
    50 CTestSuiteInputData::~CTestSuiteInputData()
       
    51 {    	
       
    52     	
       
    53     if(iQueryBuf)
       
    54     {
       
    55     	delete iQueryBuf;
       
    56 		iQueryBuf = NULL;    
       
    57     }	
       
    58     if(iKeyboardModeBuf)
       
    59     {
       
    60     	delete iKeyboardModeBuf;
       
    61     	iKeyboardModeBuf = NULL;
       
    62     }
       
    63 
       
    64 	
       
    65 	iCacheUris.ResetAndDestroy();
       
    66 	
       
    67 
       
    68 	iDisplayFields.Close();
       
    69 
       
    70 }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // Destructor
       
    74 // ----------------------------------------------------------------------------
       
    75 TDesC& CTestSuiteInputData::KeyboardMode() const
       
    76 {
       
    77 	return *iKeyboardModeBuf;
       
    78 }
       
    79 
       
    80 // ----------------------------------------------------------------------------
       
    81 // Returns the Id
       
    82 // ----------------------------------------------------------------------------
       
    83 TInt CTestSuiteInputData::Id()
       
    84 {
       
    85 	return iId;
       
    86 }
       
    87 
       
    88 void CTestSuiteInputData::Data(RPointerArray<TDesC> & aDataArray)
       
    89 {
       
    90 	for(TInt i = 0 ; i < iDataArray.Count(); i++)
       
    91 	{
       
    92 		aDataArray.Append(iDataArray[i]->AllocL());
       
    93 	}
       
    94 
       
    95 }
       
    96 // ----------------------------------------------------------------------------
       
    97 // Sets the keyboard mode
       
    98 // ----------------------------------------------------------------------------
       
    99 void CTestSuiteInputData::SetKeyboardModeL(const TDesC& aKeyboardMode)
       
   100 {
       
   101 	iKeyboardModeBuf = aKeyboardMode.AllocL();
       
   102 }
       
   103 
       
   104 TSortType CTestSuiteInputData::SortType() const
       
   105 {
       
   106 	return iSortType;
       
   107 }
       
   108 		
       
   109 void CTestSuiteInputData::SetSortType(TSortType aSortType)
       
   110 {
       
   111 	iSortType = aSortType;
       
   112 }
       
   113 
       
   114 // ----------------------------------------------------------------------------
       
   115 // Returns the search query (as specified in the config file)
       
   116 // ----------------------------------------------------------------------------
       
   117 TDesC& CTestSuiteInputData::Query() const
       
   118 {
       
   119 	return *iQueryBuf;
       
   120 }
       
   121 
       
   122 // ----------------------------------------------------------------------------
       
   123 // Sets the search query
       
   124 // 
       
   125 // ----------------------------------------------------------------------------
       
   126 void CTestSuiteInputData::SetQueryL(const TDesC& aQuery)
       
   127 {
       
   128 	iQueryBuf = aQuery.AllocL();
       
   129 }
       
   130 
       
   131 
       
   132 // ----------------------------------------------------------------------------
       
   133 // Return the cache uri at given index 
       
   134 // ----------------------------------------------------------------------------
       
   135 TDesC& CTestSuiteInputData::CacheUriAt(TInt aIndex) const
       
   136 {
       
   137 	if ( aIndex >= iCacheUris.Count() ) 
       
   138 	{
       
   139 		HBufC* temp = HBufC::NewL(2);
       
   140 		temp->Des().Copy(KNullDesC);
       
   141 		return *temp;
       
   142 	
       
   143 	}
       
   144          
       
   145     return *(iCacheUris[aIndex]);
       
   146 }
       
   147 
       
   148 
       
   149 // ----------------------------------------------------------------------------
       
   150 // Return the cache uri 
       
   151 // ----------------------------------------------------------------------------
       
   152 void CTestSuiteInputData::CacheUris(RPointerArray<TDesC> & aUriArray)
       
   153 {
       
   154 	for(TInt i = 0 ; i < iCacheUris.Count(); i++)
       
   155 	{
       
   156 		aUriArray.Append(iCacheUris[i]->AllocL());
       
   157 	}
       
   158 }
       
   159 
       
   160 // ----------------------------------------------------------------------------
       
   161 // Sets the cache uris
       
   162 // ----------------------------------------------------------------------------
       
   163 void CTestSuiteInputData::ApendCacheUrisL(RPointerArray<TDesC>& aCacheUriArray)
       
   164 {   iCacheUris.ResetAndDestroy();
       
   165 	for(TInt i =0; i <aCacheUriArray.Count(); i++ ) 
       
   166     	iCacheUris.Append(aCacheUriArray[i]);
       
   167 }
       
   168 
       
   169 
       
   170 // ----------------------------------------------------------------------------
       
   171 // Return the display fields
       
   172 // ----------------------------------------------------------------------------
       
   173 void CTestSuiteInputData::DisplayFields(RArray<TInt>& aDisplayFields)
       
   174 {
       
   175 	for(TInt i = 0 ; i < iDisplayFields.Count(); i++)
       
   176 	{
       
   177 		aDisplayFields.Append(iDisplayFields[i]);
       
   178 	}
       
   179 }
       
   180 
       
   181 // ----------------------------------------------------------------------------
       
   182 // Return the group ids
       
   183 // ----------------------------------------------------------------------------
       
   184 void CTestSuiteInputData::GroupIds(RArray<TInt>& aGroupId)
       
   185 {
       
   186 	for(TInt i = 0 ; i < iGroupIds.Count(); i++)
       
   187 	{
       
   188 		aGroupId.Append(iGroupIds[i]);
       
   189 	}
       
   190 }
       
   191 // ----------------------------------------------------------------------------
       
   192 // Second phase construction
       
   193 // ----------------------------------------------------------------------------
       
   194 void CTestSuiteInputData::ConstructL(CStifItemParser& /*aItem*/)
       
   195 {
       
   196 
       
   197 }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // This function parses the input parameters
       
   201 // -----------------------------------------------------------------------------
       
   202 void CTestSuiteInputData:: ParseInputL(CStifItemParser& aItem)
       
   203 {
       
   204 	TPtrC string;
       
   205 
       
   206    	// parsing type to be set before parsing a string 
       
   207 	// EQuoteStyleParsing: information between quotes is handled as a one string. 
       
   208 	aItem.SetParsingType( CStifItemParser::EQuoteStyleParsing );
       
   209 	
       
   210 	// loop till there is a token in i/p string
       
   211 	while ( aItem.GetNextString ( string ) == KErrNone )
       
   212 	{
       
   213 		HBufC *item = string.AllocL();
       
   214 		TChar sqBracketOpen = '[';
       
   215 		TChar sqBracketClose = ']';
       
   216 		
       
   217 		TInt sqBracketStartPos = string.Locate(sqBracketOpen);
       
   218 		TInt sqBracketEndPos = string.Locate(sqBracketClose);
       
   219 		
       
   220 		if(  (sqBracketStartPos != KErrNotFound) && ( sqBracketEndPos!=KErrNotFound))
       
   221 		{
       
   222 			TPtrC16 optionPtr = string.Left(sqBracketStartPos);	
       
   223 				
       
   224 			TPtrC16 dataPtr = string.Mid(sqBracketStartPos + 1,sqBracketEndPos - sqBracketStartPos - 1 );	
       
   225 			
       
   226 			
       
   227 			// Check the type of tag, then do the corresponding parsing
       
   228 		   if(optionPtr.Compare(KSearchQueryTag) == 0)
       
   229 		    {
       
   230 		    	// Search query
       
   231 		    	ParseInputForSearchQueryL(dataPtr);
       
   232 		    
       
   233 		    }
       
   234 		    else if(optionPtr.Compare(KUriTag) == 0)
       
   235 		    {
       
   236 		    	// Data store uris
       
   237 		    	ParseInputForDataStoresUriL(dataPtr);
       
   238 		    }
       
   239 		
       
   240 		    else if(optionPtr.Compare(KIdTag) == 0)
       
   241 		    {
       
   242 		    	// Id
       
   243 		    	ParseInputForIdL(dataPtr);
       
   244 		    }
       
   245 		    else if(optionPtr.Compare(KDataTag) == 0)
       
   246 		    {
       
   247 		    	// data
       
   248 		    	ParseInputForData(dataPtr);
       
   249 		    }
       
   250 		    
       
   251 		    else if(optionPtr.Compare(KSetDisplayFields) == 0)
       
   252 		    {
       
   253 		    	// Display fields
       
   254 		    	ParseInputForDisplayFieldsL(dataPtr);
       
   255 		    }
       
   256 		    else if(optionPtr.Compare(KSortTag) == 0)
       
   257 		    {
       
   258 		    	// Sorting
       
   259 		    	ParseInputForSortTypeL(dataPtr);
       
   260 		    }
       
   261 		    else if(optionPtr.Compare(KGroupIdTag) == 0)
       
   262 		    {
       
   263 		    	// Sorting
       
   264 		    	ParseInputForGroupIdsL(dataPtr);
       
   265 		    }
       
   266 		}
       
   267 		else
       
   268 		{
       
   269 			// Wrong Input..
       
   270 			User::Leave(KErrGeneral);
       
   271 		
       
   272 		}
       
   273 		
       
   274 	}
       
   275 }
       
   276 
       
   277 
       
   278 // -----------------------------------------------------------------------------
       
   279 // This function parses input for number id
       
   280 // -----------------------------------------------------------------------------
       
   281 void CTestSuiteInputData::ParseInputForIdL(TDesC& aInput)
       
   282 {
       
   283 
       
   284 		TInt fieldId = 0;
       
   285 		TLex num(aInput);
       
   286 		
       
   287 		TInt err = num.Val(fieldId);
       
   288 		if(err == KErrNone)
       
   289 		{
       
   290 			iId = fieldId;
       
   291 		}
       
   292 		else
       
   293 		{
       
   294 			User::Leave(KErrArgument);
       
   295 		}
       
   296 
       
   297 }
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 // This function parses input for sort type
       
   301 // -----------------------------------------------------------------------------
       
   302 void CTestSuiteInputData::ParseInputForSortTypeL(TDesC& aInput)
       
   303 {
       
   304 //	TSortType sorttype;
       
   305 	
       
   306 	// set the sort type
       
   307 	if(aInput.Compare(KAlphabetic) == 0)
       
   308 	{
       
   309 	    iSortType = EAlphabetical;
       
   310 	}
       
   311 	else if (aInput.Compare(KPattern) == 0)
       
   312 	{
       
   313 	    iSortType = EPatternBased;
       
   314 	}
       
   315 	else
       
   316 	{
       
   317 		User::Leave(KErrArgument);
       
   318 	}
       
   319 }
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // This function parses input for cache uris
       
   323 // -----------------------------------------------------------------------------
       
   324 void CTestSuiteInputData::ParseInputForData(TDesC& aInput)
       
   325 {
       
   326 	RPointerArray<TDesC> data;
       
   327 	TChar separator = ',';
       
   328 	
       
   329 	// Get the uris which are separated by commas
       
   330 	GetCharacterSeparatedDataL( aInput,separator, data);
       
   331 	for(TInt i = 0; i< data.Count(); i++)
       
   332 	{
       
   333 	        HBufC *temp =  data[i]->AllocL();
       
   334 			iDataArray.Append(data[i]);
       
   335 	
       
   336 	}
       
   337 }
       
   338 
       
   339 // -----------------------------------------------------------------------------
       
   340 // This function parses input for cache uris
       
   341 // -----------------------------------------------------------------------------
       
   342 void CTestSuiteInputData::ParseInputForDataStoresUriL(TDesC& aInput)
       
   343 {
       
   344 	RPointerArray<TDesC> Uris;
       
   345 	TChar separator = ',';
       
   346 	
       
   347 	// Get the uris which are separated by commas
       
   348 	GetCharacterSeparatedDataL( aInput,separator, Uris);
       
   349 	for(TInt i = 0; i< Uris.Count(); i++)
       
   350 	{
       
   351 	        HBufC *temp =  Uris[i]->AllocL();
       
   352 			iCacheUris.Append(Uris[i]);
       
   353 	
       
   354 	}
       
   355 }
       
   356 
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // This function parses the input for display fields
       
   360 // -----------------------------------------------------------------------------
       
   361 void CTestSuiteInputData::ParseInputForDisplayFieldsL(TDesC& aInput)
       
   362 {
       
   363 	RPointerArray<TDesC> dispFieldsBuf;
       
   364 	
       
   365 	TChar separator = ',';
       
   366 	GetCharacterSeparatedDataL( aInput,separator, dispFieldsBuf);
       
   367 	for(TInt i = 0; i< dispFieldsBuf.Count(); i++)
       
   368 	{
       
   369 		TInt fieldId = 0;
       
   370 		TLex num(*(dispFieldsBuf[i]));
       
   371 		
       
   372 		    TInt err = num.Val(fieldId);  // parse the string to extract integer value
       
   373 			if(err != KErrNone)
       
   374 			{
       
   375 				User::Leave(KErrGeneral);
       
   376 			}
       
   377 			else
       
   378 			{
       
   379 			    iDisplayFields.Append(fieldId);
       
   380 			}
       
   381 	}
       
   382 }
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // This function parses the input for expected group ids
       
   386 // -----------------------------------------------------------------------------
       
   387 void CTestSuiteInputData::ParseInputForGroupIdsL(TDesC& aInput)
       
   388 {
       
   389 	RPointerArray<TDesC> grpIdsBuf;
       
   390 	
       
   391 	TChar separator = ',';
       
   392 	GetCharacterSeparatedDataL( aInput,separator, grpIdsBuf);
       
   393 	for(TInt i = 0; i< grpIdsBuf.Count(); i++)
       
   394 	{
       
   395 		TInt groupId = 0;
       
   396 		TLex num(*(grpIdsBuf[i]));
       
   397 		
       
   398 		    TInt err = num.Val(groupId);  // parse the string to extract integer value
       
   399 			if(err != KErrNone)
       
   400 			{
       
   401 				User::Leave(KErrGeneral);
       
   402 			}
       
   403 			else
       
   404 			{
       
   405 			    iGroupIds.Append(groupId);
       
   406 			}
       
   407 	}
       
   408 }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // This function splits the the input separated by particular character
       
   412 // -----------------------------------------------------------------------------
       
   413 void CTestSuiteInputData::GetCharacterSeparatedDataL(TDesC& aInput,TChar aSeparator, RPointerArray<TDesC> &aSeparatedData)
       
   414 {
       
   415 	
       
   416 	TInt SeparatorPos = -1 ;
       
   417     for (TInt j = 0 ; j< aInput.Length();)
       
   418 	{
       
   419 			
       
   420 	        TInt Startpos = SeparatorPos + 1;
       
   421 		// Get the data for each contact
       
   422 			while((aInput[j] != aSeparator) && (j < ( aInput.Length()-1) ) )
       
   423 			     j++;
       
   424 			if(j == (aInput.Length() - 1))
       
   425 			 j++; 
       
   426 			      
       
   427 			TPtrC16 dataPtr = aInput.Mid(Startpos ,j - Startpos);	
       
   428 			
       
   429 			
       
   430 			// Add data to the output
       
   431 			HBufC* tempData = dataPtr.AllocL();
       
   432 			aSeparatedData.Append(tempData);
       
   433 			SeparatorPos = j ;  
       
   434 			j++;   
       
   435 			
       
   436 		}
       
   437 
       
   438 }
       
   439 
       
   440 // -----------------------------------------------------------------------------
       
   441 // This function parses the input for search query
       
   442 // -----------------------------------------------------------------------------
       
   443 void CTestSuiteInputData::ParseInputForSearchQueryL(TDesC& aInput)
       
   444 {
       
   445 
       
   446 	RPointerArray<TDesC> queryData;
       
   447 	TChar separator = ',';
       
   448 	GetCharacterSeparatedDataL( aInput,separator, queryData);
       
   449 
       
   450     // If there are more than 2 field, then it is an error
       
   451     if(queryData.Count() == 2)
       
   452     {
       
   453     	iQueryBuf = queryData[0]->AllocL();
       
   454     	iKeyboardModeBuf = queryData[1]->AllocL();
       
   455     	queryData.ResetAndDestroy();
       
   456     }
       
   457     else
       
   458     {
       
   459     	queryData.ResetAndDestroy();
       
   460      	User::Leave(KErrArgument);
       
   461     }
       
   462 	
       
   463 
       
   464 }
       
   465 
       
   466 
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // This function creates the search query from the input buffer
       
   470 // -----------------------------------------------------------------------------
       
   471 void CTestSuiteInputData::CreateSearchQueryL(CPsQuery& aPsQuery,TDesC& aQueryBuf)
       
   472 {
       
   473 	TKeyboardModes keyboardMode = EModeUndefined;
       
   474 
       
   475     // Check the keyboard mode
       
   476 	if(iKeyboardModeBuf->Des().Compare(KItut) == 0)
       
   477 	{
       
   478 	    keyboardMode = EItut;
       
   479 	}
       
   480 	else if (iKeyboardModeBuf->Des().Compare(KQwerty) == 0)
       
   481 	{
       
   482 	    keyboardMode = EQwerty;
       
   483 	}
       
   484 
       
   485 	else // mixed mode
       
   486 	{
       
   487 		// convert the search query to the required format
       
   488 		_LIT(KCode,"i");
       
   489 		for(TInt i = 0; i < aQueryBuf.Length(); i++)
       
   490 		{
       
   491 			CPsQueryItem* item = CPsQueryItem::NewL();
       
   492 	    	item->SetCharacter(aQueryBuf[i]); // set search character
       
   493 	    	
       
   494 	    	// set the keyboard mode
       
   495 	    	if( (iKeyboardModeBuf->Des().Mid(i,1)).Compare(KCode) == 0)
       
   496  			{
       
   497  				item->SetMode(EItut);
       
   498  			}
       
   499  			else 
       
   500  			{
       
   501  				item->SetMode(EQwerty);
       
   502  			}
       
   503  			
       
   504  			aPsQuery.AppendL(*item);
       
   505 		}
       
   506 		
       
   507 		return ;
       
   508 	}
       
   509 	
       
   510 	// create the query as CPsQuery object
       
   511 	// for EItut and EQwerty mode
       
   512 	for(TInt j = 0; j < aQueryBuf.Length(); j++)
       
   513 	{
       
   514 		CPsQueryItem* item1 = CPsQueryItem::NewL();
       
   515 	    item1->SetCharacter(aQueryBuf[j]);
       
   516 	    item1->SetMode(keyboardMode);
       
   517 	    aPsQuery.AppendL(*item1);
       
   518 	}
       
   519 }
       
   520 
       
   521 
       
   522 // -----------------------------------------------------------------------------
       
   523 // Perform the search settings
       
   524 // -----------------------------------------------------------------------------
       
   525 
       
   526 void CTestSuiteInputData::PerformSearchSettingsL(CPsSettings& aSettings)
       
   527 {
       
   528 
       
   529 	RPointerArray<TDesC> databases; // list of databases
       
   530 	RArray<TInt> displayFields;     // list of display fields
       
   531 	TInt maxResults = 100;
       
   532 
       
   533 	// add the display fields
       
   534     if(iDisplayFields.Count())
       
   535     {
       
   536 		for(TInt i =0; i<iDisplayFields.Count();i++)
       
   537 		    displayFields.Append(iDisplayFields[i]);
       
   538     
       
   539     }
       
   540     else
       
   541     {
       
   542 	    displayFields.Append(6); // Firstname
       
   543 	    displayFields.Append(4); // Lastname
       
   544 	    displayFields.Append(27); // Company name
       
   545     }
       
   546     
       
   547      
       
   548     // Set the databases to be searched 
       
   549 	for(TInt i =0; i <iCacheUris.Count(); i++ )
       
   550 	{
       
   551 		databases.Append(iCacheUris[i]->AllocL());
       
   552 	
       
   553 	}
       
   554 	
       
   555 	// Perform the settings
       
   556 	aSettings.SetSearchUrisL(databases);
       
   557     aSettings.SetDisplayFieldsL(displayFields);
       
   558     aSettings.SetMaxResults(maxResults);
       
   559 	
       
   560 
       
   561 	// cleanup
       
   562 	displayFields.Close();
       
   563 	databases.Reset(); 
       
   564 	databases.Close(); 
       
   565 
       
   566 
       
   567 }
       
   568 
       
   569 TInt CTestSuiteInputData::CompareByDataL ( const CPsClientData& aObject1, const CPsClientData& aObject2 )
       
   570 {
       
   571 	TBuf<KBufferMaxLen> data1;
       
   572      TBuf<255> data2;
       
   573      
       
   574      _LIT(KSpace, " ");
       
   575      
       
   576      data1 = KNullDesC;
       
   577      for ( int i = 0; i < aObject1.DataElementCount(); i++ )
       
   578      {
       
   579      	 if ( aObject1.Data(i) )
       
   580 	     {
       
   581 	     	data1 += aObject1.Data(i)->Des();
       
   582 	     	data1 += KSpace;
       
   583 	     }	     
       
   584      }
       
   585      
       
   586      // Trim the unnecessary white spaces before we compare
       
   587      data1.TrimAll();
       
   588      
       
   589      data2 = KNullDesC;
       
   590      for ( int i = 0; i < aObject2.DataElementCount(); i++ )
       
   591      {
       
   592      	 if ( aObject2.Data(i) )
       
   593 	     {
       
   594 	     	data2 += aObject2.Data(i)->Des();
       
   595 	     	data2 += KSpace;
       
   596 	     }	     
       
   597      }   
       
   598      
       
   599      // Trim the unnecessary white spaces before we compare 
       
   600      data2.TrimAll();   
       
   601      
       
   602 	 // Get the standard method
       
   603 	 TCollationMethod meth = *Mem::CollationMethodByIndex( 0 );
       
   604   	 meth.iFlags |= TCollationMethod::EIgnoreNone;
       
   605   	 meth.iFlags |= TCollationMethod::EFoldCase;
       
   606 
       
   607 	 // Magic: Collation level 3 is used
       
   608 	 TInt comparison(data1.CompareC( data2, 3, &meth ));
       
   609 	 return comparison;
       
   610 }
       
   611 // End of file