landmarks/locationlandmarks/tsrc/LandmarkTestModule/src/FT_CPosTp130.cpp
branchRCL_3
changeset 44 2b4ea9893b66
equal deleted inserted replaced
42:02ba3f1733c6 44:2b4ea9893b66
       
     1 /*
       
     2 * Copyright (c) 2005 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 *   ?description_line
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //  INCLUDES
       
    21 #include "FT_CPosTp130.h"
       
    22 #include "FT_LandmarkConstants.h"
       
    23 #include <EPos_CPosLmMultiDbSearch.h>
       
    24 #include <EPos_CPosLmTextCriteria.h>
       
    25 #include <EPos_CPosLmDatabaseManager.h>
       
    26 #include <EPos_CPosLmItemIterator.h>
       
    27 #include <EPos_TPosLmSortPref.h>
       
    28 #include <EPos_CPosLmCatNameCriteria.h>
       
    29 #include "badesca.h"
       
    30 
       
    31 //  CONSTANTS
       
    32 const TInt KNrOfDatabases = 5;
       
    33 _LIT(KSearchPattern, "*e*");  
       
    34 
       
    35 class CNamedLmItem : public CBase
       
    36 	{
       
    37 	public:
       
    38 		~CNamedLmItem() { delete iName; };
       
    39 		TPosLmItemId iId;
       
    40 		HBufC* iName;
       
    41 		
       
    42 		static TInt CompareByName( const CNamedLmItem& aLeft, const CNamedLmItem& aRight )
       
    43 			{
       
    44 			return aLeft.iName->CompareC( *aRight.iName );
       
    45 			};
       
    46 	};
       
    47 
       
    48 // ================= LOCAL FUNCTIONS =======================
       
    49 
       
    50 void ResetAndDestroy(TAny* aAny)
       
    51     {
       
    52     RPointerArray<CPosLmItemIterator>* pointerArray = 
       
    53         reinterpret_cast <RPointerArray<CPosLmItemIterator>*> (aAny);
       
    54     pointerArray->ResetAndDestroy();
       
    55     }
       
    56 
       
    57 void ResetAndDestroyNamedLmItem(TAny* aAny)
       
    58     {
       
    59     RPointerArray<CNamedLmItem>* pointerArray = 
       
    60         reinterpret_cast <RPointerArray<CNamedLmItem>*> (aAny);
       
    61     pointerArray->ResetAndDestroy();
       
    62     }
       
    63 
       
    64 // ================= MEMBER FUNCTIONS =======================
       
    65 // ---------------------------------------------------------
       
    66 // CPosTp130::InitTestL
       
    67 //
       
    68 // (other items were commented in a header).
       
    69 // ---------------------------------------------------------
       
    70 //
       
    71 void CPosTp130::InitTestL()
       
    72     {
       
    73     CLandmarkTestProcedureBase::InitTestL();
       
    74     
       
    75     // PrepareDatabases
       
    76     const TInt KNrOfDatabases = 5;
       
    77     RemoveAllLmDatabasesL();
       
    78     // Get the list of Dbs remaining after removing the possible ones.
       
    79     CPosLmDatabaseManager* dbMan = CPosLmDatabaseManager::NewL();
       
    80         CleanupStack::PushL(dbMan);
       
    81         iDbUris = dbMan->ListDatabasesLC();
       
    82         iInitialDbCount = iDbUris->Count();
       
    83         CleanupStack::PopAndDestroy(iDbUris);
       
    84     CopyTestDbFileL(KDb20);
       
    85     CopyTestDbFileL(KDb40);
       
    86     CopyTestDbFileL(KDb60);
       
    87     CopyTestDbFileL(KDb80);
       
    88     CopyTestDbFileL(KDb105);
       
    89     
       
    90     // List databases
       
    91     
       
    92     iDbUris = dbMan->ListDatabasesLC();
       
    93     CleanupStack::Pop(iDbUris);
       
    94     AssertTrueSecL(iDbUris->Count() == iInitialDbCount+KNrOfDatabases, _L("Wrong number of test databases!"));
       
    95     CleanupStack::PopAndDestroy(dbMan);
       
    96     
       
    97     // Get only the list of dburi in which this test case operations need to be performed
       
    98         TInt dbUriCount = iDbUris->Count();
       
    99         
       
   100         for ( TInt i= 0;i < dbUriCount;i++)
       
   101             {
       
   102         iLog->Log((*iDbUris)[i]);
       
   103         // Action to be performed only on the newly added dbs in this test case, therefore remove any other 
       
   104         // db apart from the ones added in this test case from the iDbUris list
       
   105             TPtrC dbUri((*iDbUris)[i]);
       
   106                 if ( (dbUri != KDb20Uri) && (dbUri != KDb40Uri) && (dbUri != KDb60Uri) &&
       
   107                        ( dbUri != KDb80Uri) && (dbUri != KDb105Uri) )
       
   108                     {
       
   109                 iDbUris->Delete(i);
       
   110                     }
       
   111 
       
   112             }
       
   113     
       
   114     iDbSearcher = CPosLmMultiDbSearch::NewL(*iDbUris);
       
   115     
       
   116     // Initialize expected landmark search result
       
   117     
       
   118     for ( TInt i = 0; i < iDbUris->Count(); i++ )
       
   119     	{
       
   120         RIdArray* lmArray = new (ELeave) RIdArray;
       
   121         InitExpectedResultsL( (*iDbUris)[i], *lmArray, ETrue );
       
   122         iExpectedLmResult.AppendL( lmArray );
       
   123 
       
   124         RIdArray* catArray = new (ELeave) RIdArray;
       
   125         InitExpectedResultsL( (*iDbUris)[i], *catArray, EFalse );
       
   126         iExpectedCatResult.AppendL( catArray );
       
   127     	}
       
   128 
       
   129     }
       
   130     
       
   131 // ---------------------------------------------------------
       
   132 // CPosTp130::CloseTest
       
   133 //
       
   134 // (other items were commented in a header).
       
   135 // ---------------------------------------------------------
       
   136 //
       
   137 void CPosTp130::CloseTest()
       
   138     {
       
   139     delete iDbUris;
       
   140     delete iDbSearcher;
       
   141     for (TInt i = 0; i < iExpectedLmResult.Count(); i++)
       
   142         {
       
   143         iExpectedLmResult[i]->Close();
       
   144         iExpectedCatResult[i]->Close();
       
   145         }
       
   146     iExpectedLmResult.ResetAndDestroy();
       
   147     iExpectedCatResult.ResetAndDestroy();
       
   148     }
       
   149     
       
   150 // ---------------------------------------------------------
       
   151 // CPosTp130::StartL
       
   152 //
       
   153 // (other items were commented in a header).
       
   154 // ---------------------------------------------------------
       
   155 //
       
   156 void CPosTp130::StartL()
       
   157     {
       
   158     // nrOfExpectedMatches contains the number of matches for each db when search pattern is *e*
       
   159     RArray<TInt> nrOfExpectedMatches;
       
   160     CleanupClosePushL(nrOfExpectedMatches);
       
   161     for (TInt i = 0; i < KNrOfDatabases; i++)
       
   162         {
       
   163         nrOfExpectedMatches.AppendL((*iExpectedLmResult[i]).Count());
       
   164         }
       
   165 
       
   166 // 1. Search for landmarks
       
   167     iLog->Log(_L("1"));
       
   168     SearchForLandmarksL();    
       
   169     
       
   170 // 2-9. Verify result
       
   171     iLog->Log(_L("2-9"));
       
   172     VerifyIteratorsL(nrOfExpectedMatches, ETrue);
       
   173     
       
   174     iLog->Log(_L("10.true"));
       
   175 // 10. Test iterators dynamically
       
   176     TestMatchIteratorL(ETrue);
       
   177     iLog->Log(_L("10.false"));
       
   178     TestMatchIteratorL(EFalse);
       
   179     
       
   180     iLog->Log(_L("11"));
       
   181 // 11. Test error codes
       
   182     TestErrorCodesL();
       
   183     
       
   184     iLog->Log(_L("12"));
       
   185 // 12. Check iterators from matching categories
       
   186     nrOfExpectedMatches.Reset();
       
   187     for (TInt i = 0; i < KNrOfDatabases; i++)
       
   188         {
       
   189         nrOfExpectedMatches.AppendL((*iExpectedCatResult[i]).Count());
       
   190         }
       
   191     SearchForCategoriesL();
       
   192     VerifyIteratorsL(nrOfExpectedMatches, EFalse);
       
   193     CleanupStack::PopAndDestroy(&nrOfExpectedMatches);
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------
       
   197 // CPosTp130::SearchForLandmarksL
       
   198 //
       
   199 // (other items were commented in a header).
       
   200 // ---------------------------------------------------------
       
   201 //
       
   202 void CPosTp130::SearchForLandmarksL()
       
   203     {
       
   204     // Search for landmarks containing a specific string in name
       
   205     CPosLmTextCriteria* criteria = CPosLmTextCriteria::NewLC();
       
   206     criteria->SetTextL(KSearchPattern);
       
   207     criteria->SetAttributesToSearch(CPosLandmark::ELandmarkName);
       
   208 
       
   209     // Do the search    
       
   210     ExecuteAndDeleteLD(iDbSearcher->StartLandmarkSearchL(*criteria));
       
   211     
       
   212     // Check errors
       
   213     TUint nrOfSearchErrors = iDbSearcher->NumOfSearchErrors();
       
   214     AssertTrueSecL(nrOfSearchErrors == 0, _L("Found errors during search"));
       
   215     
       
   216     CleanupStack::PopAndDestroy(criteria);
       
   217     }
       
   218     
       
   219 // ---------------------------------------------------------
       
   220 // CPosTp130::SearchForCategoriesL
       
   221 //
       
   222 // (other items were commented in a header).
       
   223 // ---------------------------------------------------------
       
   224 //
       
   225 void CPosTp130::SearchForCategoriesL()
       
   226     {
       
   227     // Search for categories containing a specific string in name
       
   228     CPosLmCatNameCriteria* criteria = CPosLmCatNameCriteria::NewLC();
       
   229     criteria->SetSearchPatternL(KSearchPattern);
       
   230 
       
   231     // Do the search    
       
   232     ExecuteAndDeleteLD(iDbSearcher->StartCategorySearchL(*criteria, CPosLmCategoryManager::ECategorySortOrderNone));
       
   233     
       
   234     // Check errors
       
   235     TUint nrOfSearchErrors = iDbSearcher->NumOfSearchErrors();
       
   236     AssertTrueSecL(nrOfSearchErrors == 0, _L("Found errors during search"));
       
   237     
       
   238     CleanupStack::PopAndDestroy(criteria);
       
   239     }
       
   240     
       
   241 // ---------------------------------------------------------
       
   242 // CPosTp130::VerifyIteratorsL
       
   243 //
       
   244 // (other items were commented in a header).
       
   245 // ---------------------------------------------------------
       
   246 //
       
   247 void CPosTp130::VerifyIteratorsL(RArray<TInt>& aNrOfExpectedItems, TBool aLm)
       
   248     {    
       
   249     RPointerArray<CPosLmItemIterator> iterators;
       
   250     CleanupClosePushL(iterators);
       
   251     CleanupStack::PushL(TCleanupItem(ResetAndDestroy, &iterators));
       
   252     
       
   253     TInt totalNrOfMatches(0), totalNrOfExpectedMatches(0);
       
   254     for (TInt i = 0; i < iDbUris->Count(); i++)
       
   255         {
       
   256         // 2. Ask for the iterator for each database
       
   257         iterators.AppendL(iDbSearcher->MatchIteratorL(i));
       
   258         
       
   259         // 3. Verify that the number of each iterator equals to the expected result
       
   260         AssertTrueSecL(aNrOfExpectedItems[i] == iterators[i]->NumOfItemsL(), _L("aNrOfExpectedItems[i] != iterators[i]->NumOfItemsL()"));
       
   261         AssertTrueSecL(aNrOfExpectedItems[i] == iDbSearcher->NumOfMatches(i), _L("aNrOfExpectedItems[i] != iDbSearcher->NumOfMatches(i)"));
       
   262         
       
   263         totalNrOfExpectedMatches += aNrOfExpectedItems[i];
       
   264         totalNrOfMatches += iterators[i]->NumOfItemsL();
       
   265         }
       
   266         
       
   267     // 4. Verify that the total number of matches equals the sum of expected
       
   268     AssertTrueSecL(totalNrOfMatches == totalNrOfExpectedMatches, _L("totalNrOfMatches != totalNrOfExpectedMatches"));
       
   269     AssertTrueSecL(totalNrOfMatches == iDbSearcher->TotalNumOfMatches(), _L("totalNrOfMatches != iDbSearcher->TotalNumOfMatches()"));
       
   270     
       
   271     for (TInt dbIndex = 0; dbIndex < iDbUris->Count(); dbIndex++)
       
   272         {
       
   273         // 5. Ask for the first item in each iterator
       
   274         TPosLmItemId id1 = iterators[dbIndex]->NextL();
       
   275         
       
   276         // 6. Perform an operation on the first item, e.g. read it from db
       
   277         CPosLandmarkDatabase* db = CPosLandmarkDatabase::OpenL((*iDbUris)[dbIndex]);
       
   278         CleanupStack::PushL(db);
       
   279         if (aLm)
       
   280             {            
       
   281             CPosLandmark* lm = db->ReadLandmarkLC(id1);
       
   282             TPtrC name;
       
   283             lm->GetLandmarkName(name);
       
   284             AssertTrueSecL(name.MatchC(KSearchPattern) != KErrNotFound, _L("First lm in iterator does not contain \"e\""));
       
   285             CleanupStack::PopAndDestroy(lm);
       
   286             }
       
   287         else
       
   288             {
       
   289             CPosLmCategoryManager* catMan = CPosLmCategoryManager::NewL(*db);
       
   290             CleanupStack::PushL(catMan);
       
   291             CPosLandmarkCategory* cat = catMan->ReadCategoryLC(id1);
       
   292             TPtrC name;
       
   293             cat->GetCategoryName(name);
       
   294             AssertTrueSecL(name.MatchC(KSearchPattern) != KErrNotFound, _L("First category in iterator does not contain \"e\""));            
       
   295             CleanupStack::PopAndDestroy(2, catMan);
       
   296             }
       
   297         CleanupStack::PopAndDestroy(db);
       
   298         
       
   299         RIdArray allIds;
       
   300         CleanupClosePushL(allIds);        
       
   301         
       
   302         // 7. Reset iterator and ask for next item
       
   303         iterators[dbIndex]->Reset();
       
   304         TPosLmItemId id2 = iterators[dbIndex]->NextL();
       
   305         AssertTrueSecL(id1 == id2, _L("id1 != id2"));
       
   306         allIds.AppendL(id2);
       
   307         
       
   308         // 8. Iterate until there are no more items to iterate
       
   309         while (id2 != KPosLmNullItemId)
       
   310             {
       
   311             id2 = iterators[dbIndex]->NextL();
       
   312             allIds.AppendL(id2);
       
   313             }
       
   314         AssertTrueSecL(id2 == KPosLmNullItemId, _L("id2 != KPosLmNullItemId"));
       
   315         
       
   316         // 9. Ask for sequences of iterator
       
   317         RIdArray idArray;
       
   318         CleanupClosePushL(idArray);
       
   319         TInt nrOfItems = iterators[dbIndex]->NumOfItemsL();
       
   320         for (TInt iterIndex = 0; iterIndex < nrOfItems; iterIndex++)
       
   321             {
       
   322             // Get sequence of the tail of the iterator
       
   323             iterators[dbIndex]->GetItemIdsL(idArray, iterIndex, nrOfItems - iterIndex);
       
   324             AssertTrueSecL(idArray.Count() == nrOfItems - iterIndex, _L("A: Wrong number of items in id array"));
       
   325             for (TInt i = iterIndex; i < nrOfItems - iterIndex; i++)
       
   326                 {
       
   327                 id1 = idArray[i - iterIndex];
       
   328                 id2 = allIds[i];
       
   329                 AssertTrueSecL(id1 == id2, _L("A: Unexpected id in part of all ids array"));
       
   330                 }
       
   331             
       
   332             // Get sequence of the nose of the iterator
       
   333             iterators[dbIndex]->GetItemIdsL(idArray, 0, iterIndex);
       
   334             AssertTrueSecL(idArray.Count() == iterIndex, _L("B: Wrong number of items in id array"));
       
   335             for (TInt j = 0; j < iterIndex; j++)
       
   336                 {
       
   337                 id1 = idArray[j];
       
   338                 id2 = allIds[j];
       
   339                 AssertTrueSecL(id1 == id2, _L("B: Unexpected id in part of all ids array"));
       
   340                 }
       
   341             }
       
   342         CleanupStack::PopAndDestroy(2, &allIds);
       
   343         }
       
   344         
       
   345     CleanupStack::PopAndDestroy(2, &iterators);
       
   346     }
       
   347        
       
   348 // ---------------------------------------------------------
       
   349 // CPosTp130::TestErrorCodesL
       
   350 //
       
   351 // (other items were commented in a header).
       
   352 // ---------------------------------------------------------
       
   353 //
       
   354 void CPosTp130::TestErrorCodesL()
       
   355     {
       
   356     // No error codes to test
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------
       
   360 // CPosTp130::TestMatchIteratorL
       
   361 //
       
   362 // (other items were commented in a header).
       
   363 // ---------------------------------------------------------
       
   364 //
       
   365 void CPosTp130::TestMatchIteratorL(TBool aSearchLm)
       
   366     {    
       
   367     CMatchIteratorTester* matchIterTester = CMatchIteratorTester::NewLC(iDbUris, iLog);
       
   368     CPosLmOperation* operation = NULL;
       
   369     
       
   370     if (aSearchLm)
       
   371         {        
       
   372         CPosLmTextCriteria* textCriteria = CPosLmTextCriteria::NewLC();
       
   373         textCriteria->SetTextL(KSearchPattern);
       
   374         textCriteria->SetAttributesToSearch(CPosLandmark::ELandmarkName);
       
   375         TPosLmSortPref sortPref(CPosLandmark::ELandmarkName, TPosLmSortPref::EAscending);
       
   376         operation = iDbSearcher->StartLandmarkSearchL(*textCriteria, sortPref);
       
   377         CleanupStack::PopAndDestroy(textCriteria);
       
   378         
       
   379         CleanupStack::PushL(operation);
       
   380         matchIterTester->RunTestL(iDbSearcher, operation, iExpectedLmResult);
       
   381         CleanupStack::Pop(operation); // ownership transferred
       
   382         }
       
   383     else
       
   384         {
       
   385         CPosLmCatNameCriteria* criteria = CPosLmCatNameCriteria::NewLC();
       
   386         criteria->SetSearchPatternL(KSearchPattern);
       
   387         operation = iDbSearcher->StartCategorySearchL(*criteria, CPosLmCategoryManager::ECategorySortOrderNameAscending);
       
   388         CleanupStack::PopAndDestroy(criteria);
       
   389         
       
   390         CleanupStack::PushL(operation);
       
   391         matchIterTester->RunTestL(iDbSearcher, operation, iExpectedCatResult);
       
   392         CleanupStack::Pop(operation); // ownership transferred
       
   393         }        
       
   394     
       
   395     TInt error = matchIterTester->Result();
       
   396     if (error != KErrNone)
       
   397         {
       
   398         TPtrC msg;
       
   399         matchIterTester->GetErrorMsg(msg);
       
   400         
       
   401         iLog->Log(msg);
       
   402         User::Leave(error);
       
   403         }
       
   404         
       
   405     CleanupStack::PopAndDestroy(matchIterTester);
       
   406     }
       
   407 
       
   408 // ---------------------------------------------------------
       
   409 // ---------------------------------------------------------
       
   410 //
       
   411 void CPosTp130::InitExpectedResultsL( const TDesC& aDbName, RIdArray& aArray, TBool aSearchLm )
       
   412     {    
       
   413     iLog->Log( aDbName );
       
   414     CPosLandmarkDatabase* db = CPosLandmarkDatabase::OpenL( aDbName );
       
   415     CleanupStack::PushL( db );
       
   416     
       
   417     if ( db->IsInitializingNeeded() )
       
   418     	{
       
   419     	ExecuteAndDeleteLD( db->InitializeL() );
       
   420     	}
       
   421     
       
   422 	CPosLmCategoryManager* catman = CPosLmCategoryManager::NewL( *db );
       
   423     CleanupStack::PushL( catman );
       
   424 
       
   425     RPointerArray<CNamedLmItem> items;
       
   426     CleanupStack::PushL( TCleanupItem( ResetAndDestroyNamedLmItem, &items ) );
       
   427     
       
   428     CPosLmItemIterator* iter = NULL;
       
   429     if ( aSearchLm )
       
   430     	{
       
   431     	iter = db->LandmarkIteratorL();
       
   432     	}
       
   433     else
       
   434     	{
       
   435         iter = catman->CategoryIteratorL();
       
   436     	}
       
   437     CleanupStack::PushL( iter );
       
   438     
       
   439     TPosLmItemId id = iter->NextL();
       
   440     while ( id != KPosLmNullItemId )
       
   441     	{
       
   442     	CNamedLmItem* item = new (ELeave) CNamedLmItem;
       
   443         CleanupStack::PushL( item );
       
   444         
       
   445         item->iId = id;
       
   446         
       
   447     	TPtrC name;
       
   448         if ( aSearchLm )
       
   449         	{
       
   450         	CPosLandmark* lm = db->ReadLandmarkLC( id );
       
   451         	lm->GetLandmarkName( name );
       
   452         	item->iName = name.AllocL();
       
   453         	CleanupStack::PopAndDestroy( lm );
       
   454         	}
       
   455         else
       
   456         	{
       
   457         	CPosLandmarkCategory* cat = catman->ReadCategoryLC( id );
       
   458         	cat->GetCategoryName( name );
       
   459         	item->iName = name.AllocL();
       
   460         	CleanupStack::PopAndDestroy( cat );
       
   461         	}
       
   462         
       
   463         if ( item->iName->MatchC( KSearchPattern ) != KErrNotFound ) 
       
   464         	{
       
   465 	        items.AppendL( item );
       
   466 	        CleanupStack::Pop( item );
       
   467         	}
       
   468         else
       
   469         	{
       
   470         	CleanupStack::PopAndDestroy( item );
       
   471         	}
       
   472         id = iter->NextL();
       
   473     	}
       
   474     CleanupStack::PopAndDestroy( iter );
       
   475     
       
   476     TLinearOrder<CNamedLmItem> order( CNamedLmItem::CompareByName );
       
   477     items.Sort( order );
       
   478     
       
   479     aArray.Reset();
       
   480     for( TInt i = 0; i < items.Count(); i++ )
       
   481     	{
       
   482     	aArray.AppendL( items[i]->iId );
       
   483         iLog->Log( _L("id: %d, name = '%S'"), items[i]->iId, items[i]->iName );
       
   484     	}
       
   485     
       
   486     CleanupStack::PopAndDestroy(); // items
       
   487     CleanupStack::PopAndDestroy( catman );
       
   488     CleanupStack::PopAndDestroy( db );
       
   489     }
       
   490 
       
   491 // ================= CMatchIteratorTester MEMBER FUNCTIONS =======================
       
   492 
       
   493 // Constructor
       
   494 CMatchIteratorTester::CMatchIteratorTester(
       
   495 		CDesCArray* aDbUris, CStifLogger* aLog )
       
   496 : CActive(CActive::EPriorityStandard), 
       
   497 iDbUris(aDbUris), iLog( aLog )
       
   498     {
       
   499     CActiveScheduler::Add(this);
       
   500     }
       
   501 
       
   502 // Destructor
       
   503 CMatchIteratorTester::~CMatchIteratorTester() 
       
   504     {
       
   505     Cancel();
       
   506     delete iErrorMsg;
       
   507     delete iOperation;
       
   508     iIterators.ResetAndDestroy();
       
   509     }
       
   510 
       
   511 // ---------------------------------------------------------
       
   512 // CMatchIteratorTester::NewLC
       
   513 //
       
   514 // (other items were commented in a header).
       
   515 // ---------------------------------------------------------
       
   516 //
       
   517 CMatchIteratorTester* CMatchIteratorTester::NewLC(
       
   518 	CDesCArray* aDbUris, CStifLogger* aLog)
       
   519     {
       
   520     CMatchIteratorTester* self = 
       
   521     	new (ELeave) CMatchIteratorTester(aDbUris, aLog);
       
   522     CleanupStack::PushL(self);
       
   523     return self;
       
   524     }
       
   525 
       
   526 // ---------------------------------------------------------
       
   527 // CMatchIteratorTester::RunL
       
   528 //
       
   529 // (other items were commented in a header).
       
   530 // ---------------------------------------------------------
       
   531 //
       
   532 void CMatchIteratorTester::RunL()
       
   533     {       
       
   534     iResult = iStatus.Int();    
       
   535     VerifyIteratorsL(iResult);
       
   536     
       
   537     if (iStatus.Int() == KPosLmOperationNotComplete)
       
   538         {        
       
   539         iOperation->NextStep(iStatus, iProgress);
       
   540         SetActive();
       
   541         }
       
   542     else
       
   543         {
       
   544         CActiveScheduler::Stop();
       
   545         }
       
   546     }
       
   547     
       
   548 // ---------------------------------------------------------
       
   549 // CMatchIteratorTester::DoCancel
       
   550 //
       
   551 // (other items were commented in a header).
       
   552 // ---------------------------------------------------------
       
   553 //
       
   554 void CMatchIteratorTester::DoCancel()
       
   555     {
       
   556     delete iOperation;
       
   557     iOperation = NULL;
       
   558     }
       
   559     
       
   560 // ---------------------------------------------------------
       
   561 // CMatchIteratorTester::RunError
       
   562 //
       
   563 // (other items were commented in a header).
       
   564 // ---------------------------------------------------------
       
   565 //
       
   566 TInt CMatchIteratorTester::RunError(TInt aError)
       
   567     {
       
   568     iResult = aError;
       
   569     CActiveScheduler::Stop();
       
   570     return KErrNone;
       
   571     }
       
   572     
       
   573 // ---------------------------------------------------------
       
   574 // CMatchIteratorTester::RunTestL
       
   575 //
       
   576 // (other items were commented in a header).
       
   577 // ---------------------------------------------------------
       
   578 //
       
   579 void CMatchIteratorTester::RunTestL(
       
   580     CPosLmMultiDbSearch* aMultiSearcher,
       
   581     CPosLmOperation* aSearchOperation,
       
   582     RPointerArray<RIdArray>& aExpectedResult)
       
   583     {
       
   584     iMultiSearcher = aMultiSearcher;
       
   585     iExpectedResult = &aExpectedResult;
       
   586     iOperation = aSearchOperation;
       
   587     
       
   588     // Initialize iIterators
       
   589     for (TInt i = 0; i < iMultiSearcher->NumOfDatabasesToSearch(); i++)
       
   590         {
       
   591         CPosLmItemIterator* iterator = iMultiSearcher->MatchIteratorL(i);
       
   592         CleanupStack::PushL(iterator);
       
   593         User::LeaveIfError(iIterators.Append(iterator));
       
   594         CleanupStack::Pop(iterator);
       
   595         }
       
   596     
       
   597     iOperation->NextStep(iStatus, iProgress);
       
   598     SetActive();
       
   599     CActiveScheduler::Start();
       
   600     }
       
   601     
       
   602 // ---------------------------------------------------------
       
   603 // CMatchIteratorTester::Result
       
   604 //
       
   605 // (other items were commented in a header).
       
   606 // ---------------------------------------------------------
       
   607 //
       
   608 TInt CMatchIteratorTester::Result() const 
       
   609     {
       
   610     return iResult;
       
   611     }        
       
   612 
       
   613 // ---------------------------------------------------------
       
   614 // CMatchIteratorTester::GetErrorMsg
       
   615 //
       
   616 // (other items were commented in a header).
       
   617 // ---------------------------------------------------------
       
   618 //
       
   619 void CMatchIteratorTester::GetErrorMsg(TPtrC& aMsg) const
       
   620     {
       
   621     aMsg.Set(*iErrorMsg);
       
   622     }
       
   623     
       
   624 // ---------------------------------------------------------
       
   625 // CMatchIteratorTester::VerifyIteratorsL
       
   626 //
       
   627 // (other items were commented in a header).
       
   628 // ---------------------------------------------------------
       
   629 //
       
   630 void CMatchIteratorTester::VerifyIteratorsL(TInt aResult)
       
   631     {
       
   632     for (TInt dbIndex = 0; dbIndex < iMultiSearcher->NumOfDatabasesToSearch(); dbIndex++)
       
   633         {
       
   634         CPosLmItemIterator* iterator = iMultiSearcher->MatchIteratorL(dbIndex);
       
   635         CleanupStack::PushL(iterator);
       
   636         CompareIteratorsL(*iterator, *iIterators[dbIndex]);
       
   637         delete iIterators[dbIndex];
       
   638         iIterators[dbIndex] = iterator;
       
   639         CleanupStack::Pop(iterator);
       
   640         
       
   641         // Verify matches are correct
       
   642         TBuf<50> buf;
       
   643         iterator->Reset();
       
   644         TPosLmItemId id = iterator->NextL();
       
   645         while (id != KPosLmNullItemId)
       
   646             {
       
   647             if ((*(*iExpectedResult)[dbIndex]).Find(id) == KErrNotFound)
       
   648                 {
       
   649                 buf.Format(_L("Cannot find match %d in expected result"), id );
       
   650                 SetErrorAndLeaveL(buf);
       
   651                 }
       
   652             id = iterator->NextL();
       
   653             }
       
   654             
       
   655         if (aResult == KErrNone) // Search is completed - result should be sorted ascending
       
   656             {
       
   657             RIdArray idArray;
       
   658             CleanupClosePushL(idArray);
       
   659             TInt nrOfItems = iterator->NumOfItemsL();
       
   660             iterator->GetItemIdsL(idArray, 0, nrOfItems);
       
   661             for (TInt i = 0; i < nrOfItems; i++)
       
   662                 {
       
   663                 TPosLmItemId id1 = idArray[i];
       
   664                 TPosLmItemId id2 = (*(*iExpectedResult)[dbIndex])[i];
       
   665                 if (id1 != id2)
       
   666                     {
       
   667                     buf.Format(_L("Sorted result differs, %d != %d"), id1, id2);
       
   668                     SetErrorAndLeaveL(buf);
       
   669                     }
       
   670                 }
       
   671                 
       
   672             TInt expectedLength = (*(*iExpectedResult)[dbIndex]).Count();
       
   673             if (nrOfItems != expectedLength)
       
   674                 {
       
   675                 buf.Format(_L("Length of arrays doesnät match, %d != %d"), nrOfItems, expectedLength);
       
   676                 SetErrorAndLeaveL(buf);
       
   677                 }
       
   678             CleanupStack::PopAndDestroy(&idArray);
       
   679             }
       
   680         }
       
   681     }
       
   682     
       
   683 // ---------------------------------------------------------
       
   684 // CMatchIteratorTester::CompareIteratorsL
       
   685 //
       
   686 // (other items were commented in a header).
       
   687 // ---------------------------------------------------------
       
   688 //
       
   689 void CMatchIteratorTester::CompareIteratorsL(
       
   690     CPosLmItemIterator& aNewIter, 
       
   691     CPosLmItemIterator& aOldIter)
       
   692     {
       
   693     TInt oldIterLength = aOldIter.NumOfItemsL();
       
   694     TInt newIterLength = aNewIter.NumOfItemsL();
       
   695     
       
   696     if (newIterLength < oldIterLength)
       
   697         {
       
   698         SetErrorAndLeaveL(_L("aNewIter is shorter than aOldIter"));
       
   699         }
       
   700         
       
   701     // Verify that all the items in the old iterator can be found in the new one
       
   702     if (newIterLength > 0)
       
   703         {
       
   704         RArray<TPosLmItemId> newItemIds;
       
   705         CleanupClosePushL(newItemIds);
       
   706         aNewIter.GetItemIdsL(newItemIds, 0, newIterLength);
       
   707         
       
   708         aOldIter.Reset();
       
   709         TPosLmItemId id = aOldIter.NextL();
       
   710         while (id != KPosLmNullItemId)
       
   711             {
       
   712             TInt err = newItemIds.Find(id);
       
   713             if (err == KErrNotFound)
       
   714                 {
       
   715                 SetErrorAndLeaveL(_L("aNewIter doesn't match aOldIter"));
       
   716                 }
       
   717             id = aOldIter.NextL();
       
   718             }
       
   719             
       
   720         CleanupStack::PopAndDestroy(&newItemIds);
       
   721         }
       
   722     }
       
   723 
       
   724 // ---------------------------------------------------------
       
   725 // CMatchIteratorTester::SetErrorAndLeaveL
       
   726 //
       
   727 // (other items were commented in a header).
       
   728 // ---------------------------------------------------------
       
   729 //
       
   730 void CMatchIteratorTester::SetErrorAndLeaveL(const TDesC& aErrMsg)
       
   731     {    
       
   732     delete iErrorMsg; 
       
   733     iErrorMsg = NULL;
       
   734     iErrorMsg = aErrMsg.AllocL();
       
   735     User::Leave(KErrGeneral);
       
   736     }
       
   737     
       
   738 //  End of File