landmarks/locationlandmarks/tsrc/LandmarkTestModule/src/FT_CPosTp11.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 
       
    22 #include "FT_CPosTp11.h"
       
    23 #include <EPos_CPosLandmarkDatabase.h> 
       
    24 #include <EPos_CPosLMItemIterator.h> 
       
    25 #include <EPos_CPosLandmark.h>
       
    26 #include <EPos_TPosLMSortPref.h> 
       
    27 
       
    28 //CONSTANTS
       
    29 const TInt KNoCategoriesTp11  = 100;
       
    30 const TInt KNoLandmarksTp11 = 100;     
       
    31 
       
    32 _LIT(KCategoryIteratorNumOfItemsErr, "The iterator returns incorrect number of items");
       
    33 _LIT(KCategoryIteratorIdErr, "A category id not in the database returned from the iterator");         
       
    34 
       
    35 // ================= MEMBER FUNCTIONS =======================
       
    36 
       
    37 // ---------------------------------------------------------
       
    38 // CPosTp11::StartL
       
    39 //
       
    40 // (other items were commented in a header).
       
    41 // ---------------------------------------------------------
       
    42 //
       
    43 void CPosTp11::StartL()
       
    44     {  
       
    45     iLog->Log(_L("***** Testing CPosCategoryManager::CategoryIteratorL() and CPosCategoryManager::ReferencedCategoryIteratorL()***** "));
       
    46     
       
    47     RemoveDefaultDbL();
       
    48     RemoveAllLmDatabasesL();              
       
    49     
       
    50     iDatabase = CPosLandmarkDatabase::OpenL();        
       
    51     if (iDatabase->IsInitializingNeeded())
       
    52        {
       
    53         iLog->Log(_L("ExecuteAndDeleteLD(iDatabase->InitializeL())"));
       
    54        ExecuteAndDeleteLD(iDatabase->InitializeL());
       
    55        } 
       
    56        
       
    57     iLog->Log(_L("Default database opened and initialized "));       
       
    58                
       
    59     iCategoryManager = CPosLmCategoryManager::NewL(*iDatabase);
       
    60     ExecuteAndDeleteLD(iDatabase->CompactL());          
       
    61     
       
    62     MakeEmptyLmDatabaseL( *iDatabase, *iCategoryManager);        
       
    63     
       
    64     iLog->Log(_L("Deleting all landmarks and categories to create empty database Done"));
       
    65     
       
    66     AddCategoriesL();
       
    67     AddLandmarksL();   
       
    68 
       
    69     iLog->Log(_L("** 1: Testing CPosCategoryManager::CategoryIteratorL() (Iteration of categories)"));
       
    70     IterateCategoriesL();
       
    71     
       
    72     iLog->Log(_L("** 2: Testing CPosCategoryManager::ReferencedCategoryIteratorL() (Iteration of referenced categories)"));   
       
    73     
       
    74     IterateReferencedCategoriesL();
       
    75 
       
    76     iLog->Log(_L("** 3: Testing CPosCategoryManager::CategoryIteratorL() (Iteration of categories)"));    
       
    77     IterateSetOfCategoriesL();
       
    78 	
       
    79 	iLog->Log(_L("** 4: Testing CPosCategoryManager::CategoryIteratorL() (Iteration of Sorted categories)"));    
       
    80     IterateSortedCategoriesL();    
       
    81     
       
    82     iLog->Log(_L("** 5: Testing CPosCategoryManager::ReferencedCategoryIteratorL() (Iteration of Sorted referenced categories)"));    
       
    83     IterateReferencedSortedCategoriesL();    
       
    84 
       
    85     delete iDatabase;
       
    86     iDatabase = NULL;
       
    87 
       
    88     // Remove lm db
       
    89     RemoveDefaultDbL();    
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------
       
    93 // CPosTp11::CloseTest
       
    94 //
       
    95 // (other items were commented in a header).
       
    96 // ---------------------------------------------------------
       
    97 //
       
    98 void CPosTp11::CloseTest()
       
    99     {
       
   100     iLog->Log(_L("CloseTest"));
       
   101     iCategories.ResetAndDestroy();
       
   102     iCategories.Close();
       
   103     
       
   104     iReferencedCategories.Close();
       
   105     iLandmarks.ResetAndDestroy();
       
   106     iLandmarks.Close();
       
   107     
       
   108     delete iDatabase;
       
   109     iDatabase = NULL;
       
   110     
       
   111     delete iCategoryManager; 
       
   112     iCategoryManager = NULL;      
       
   113     }
       
   114     
       
   115 
       
   116 // ---------------------------------------------------------
       
   117 // CPosTp11::AddCategoriesL
       
   118 //
       
   119 // (other items were commented in a header).
       
   120 // ---------------------------------------------------------
       
   121 //
       
   122 void CPosTp11::AddCategoriesL()
       
   123     {    
       
   124     _LIT(KName, "CategoryTP11 - %d");
       
   125     TInt err=KErrLocked;
       
   126     for (TInt i=0; i<KNoCategoriesTp11; i++)
       
   127         {
       
   128         TBuf<100> categoryName;
       
   129         categoryName.Format(KName,i);
       
   130        
       
   131         CPosLandmarkCategory* lmc = CPosLandmarkCategory::NewLC();
       
   132         lmc->SetCategoryNameL(categoryName);
       
   133         err = KErrLocked;
       
   134         while (err == KErrLocked)
       
   135             {
       
   136             TRAP(err, iCategoryManager->AddCategoryL(*lmc));
       
   137             }
       
   138     
       
   139         iCategories.Append(lmc);       
       
   140         CleanupStack::Pop(lmc);
       
   141         }    
       
   142     iLog->Log(_L("Adding categories to database done "));
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------
       
   146 // CPosTp11::AddLandmarksL
       
   147 //
       
   148 // (other items were commented in a header).
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 void CPosTp11::AddLandmarksL()
       
   152     {
       
   153     _LIT(KLmName, "LmTP11 - %d");
       
   154     TInt err = KErrLocked;
       
   155     for (TInt i=0; i<KNoLandmarksTp11; i++)
       
   156         {
       
   157         TBuf<100> lmName;
       
   158         lmName.Format(KLmName,i);
       
   159        
       
   160         CPosLandmark* landmark = CPosLandmark::NewLC();
       
   161         landmark->SetLandmarkNameL(lmName); 
       
   162 
       
   163         if (i % 2)
       
   164             {
       
   165             landmark->AddCategoryL(iCategories[i]->CategoryId());
       
   166             iReferencedCategories.Append(*iCategories[i]);
       
   167             }
       
   168 
       
   169         err = KErrLocked;
       
   170         while (err == KErrLocked)
       
   171             {
       
   172             TRAP(err, iDatabase->AddLandmarkL(*landmark));
       
   173             }
       
   174         iLandmarks.Append(landmark);
       
   175         CleanupStack::Pop(landmark);
       
   176         }
       
   177     iLog->Log(_L("Adding categories to landmarks and adding landmarks to database done "));
       
   178     }
       
   179 
       
   180 
       
   181 // ---------------------------------------------------------
       
   182 // CPosTp11::IterateCategoriesL
       
   183 //
       
   184 // (other items were commented in a header).
       
   185 // ---------------------------------------------------------
       
   186 //
       
   187 void CPosTp11::IterateCategoriesL()
       
   188     {
       
   189     CPosLmItemIterator* iter = iCategoryManager->CategoryIteratorL();
       
   190     CleanupStack::PushL(iter);
       
   191 
       
   192     AssertTrueSecL(iter->NumOfItemsL() == (TUint)iCategories.Count(), KCategoryIteratorNumOfItemsErr);
       
   193 
       
   194     CheckIteratorItemsL(*iter);
       
   195  
       
   196     iter->Reset();
       
   197 
       
   198     CheckIteratorItemsL(*iter);
       
   199 
       
   200     CleanupStack::PopAndDestroy(iter);
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------
       
   204 // CPosTp11::IterateReferencedCategoriesL
       
   205 //
       
   206 // (other items were commented in a header).
       
   207 // ---------------------------------------------------------
       
   208 //
       
   209 void CPosTp11::IterateReferencedCategoriesL()
       
   210     {
       
   211     CPosLmItemIterator* iter = iCategoryManager->ReferencedCategoryIteratorL();
       
   212     CleanupStack::PushL(iter);
       
   213 
       
   214     AssertTrueSecL(iter->NumOfItemsL() == (TUint)(iCategories.Count()/2), KCategoryIteratorNumOfItemsErr);
       
   215     
       
   216     CheckIteratorItemsL(*iter);
       
   217  
       
   218     iter->Reset();
       
   219 
       
   220     CheckIteratorItemsL(*iter);
       
   221     
       
   222     CleanupStack::PopAndDestroy(iter);
       
   223     }
       
   224 
       
   225 // ---------------------------------------------------------
       
   226 // CPosTp11::CheckIteratorItemsL
       
   227 //
       
   228 // (other items were commented in a header).
       
   229 // ---------------------------------------------------------
       
   230 //
       
   231 void CPosTp11::CheckIteratorItemsL(CPosLmItemIterator& iter)
       
   232     {
       
   233     TPosLmItemId id = iter.NextL();
       
   234     while (id != KPosLmNullItemId)
       
   235         {
       
   236         CPosLandmarkCategory* source = InternalGet(id);
       
   237         AssertTrueSecL(source != NULL, KCategoryIteratorIdErr);
       
   238         CPosLandmarkCategory* target = iCategoryManager->ReadCategoryLC(id);
       
   239         CompareCategoriesL(*source, *target);
       
   240         CleanupStack::PopAndDestroy(target); 
       
   241         id = iter.NextL();
       
   242         }    
       
   243     }
       
   244 
       
   245 // ---------------------------------------------------------
       
   246 // CPosTp11::IterateSetOfCategoriesL
       
   247 //
       
   248 // (other items were commented in a header).
       
   249 // ---------------------------------------------------------
       
   250 //
       
   251 void CPosTp11::IterateSetOfCategoriesL()
       
   252     {
       
   253     CPosLmItemIterator* iter = iCategoryManager->CategoryIteratorL();
       
   254     CleanupStack::PushL(iter);
       
   255 
       
   256     const TInt startIndex=11;
       
   257     const TInt numOfItems=34;
       
   258 
       
   259     RArray<TPosLmItemId> ids;
       
   260     CleanupClosePushL(ids);
       
   261 
       
   262     iter->GetItemIdsL(ids, startIndex, numOfItems);
       
   263     
       
   264     AssertTrueSecL(ids.Count() == numOfItems, KCategoryIteratorNumOfItemsErr);
       
   265 
       
   266     for (TInt i=0; i<numOfItems; i++)
       
   267         {
       
   268         CPosLandmarkCategory* source = InternalGet(ids[i]);
       
   269         AssertTrueSecL(source != NULL, KCategoryIteratorIdErr);
       
   270         
       
   271         CPosLandmarkCategory* target = iCategoryManager->ReadCategoryLC(ids[i]);
       
   272         CompareCategoriesL(*source, *target);
       
   273         CleanupStack::PopAndDestroy(target); 
       
   274         }
       
   275 
       
   276     CleanupStack::PopAndDestroy(2, iter);
       
   277 
       
   278     }
       
   279 
       
   280 // ---------------------------------------------------------
       
   281 // InternalSort
       
   282 //
       
   283 // (other items were commented in a header).
       
   284 // ---------------------------------------------------------
       
   285 //
       
   286 TInt InternalSortL(const CPosLandmarkCategory& aSource, const CPosLandmarkCategory& aTarget)
       
   287     {
       
   288     TPtrC sourceName, targetName;
       
   289     
       
   290     User::LeaveIfError(aSource.GetCategoryName(sourceName));  
       
   291     User::LeaveIfError(aTarget.GetCategoryName(targetName));
       
   292     
       
   293     return sourceName.Compare(targetName);
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------
       
   297 // CPosTp11::IterateSortedCategoriesL
       
   298 //
       
   299 // (other items were commented in a header).
       
   300 // ---------------------------------------------------------
       
   301 //
       
   302 void CPosTp11::IterateSortedCategoriesL()
       
   303     {
       
   304     
       
   305     TLinearOrder<CPosLandmarkCategory> order(InternalSortL);
       
   306     iCategories.Sort(order);
       
   307 
       
   308     CPosLmItemIterator* iter = iCategoryManager->CategoryIteratorL(
       
   309                                 CPosLmCategoryManager::ECategorySortOrderNameDescending);
       
   310     CleanupStack::PushL(iter);
       
   311 
       
   312     AssertTrueSecL(iter->NumOfItemsL() == (TUint)iCategories.Count(), KCategoryIteratorNumOfItemsErr);
       
   313 
       
   314     TInt lastIndex=iCategories.Count()-1;
       
   315     for (TInt i=lastIndex; i>=0; i--)
       
   316         {
       
   317         CPosLandmarkCategory* target = iCategoryManager->ReadCategoryLC(iter->NextL());
       
   318        
       
   319         CompareCategoriesL(*iCategories[i], *target);
       
   320         CleanupStack::PopAndDestroy(target); 
       
   321         }
       
   322     CleanupStack::PopAndDestroy(iter);
       
   323 
       
   324     iter = iCategoryManager->CategoryIteratorL(
       
   325                                 CPosLmCategoryManager::ECategorySortOrderNameAscending);
       
   326     CleanupStack::PushL(iter);
       
   327 
       
   328     for (TInt j=0; j < iCategories.Count(); j++)
       
   329         {
       
   330         CPosLandmarkCategory* target = iCategoryManager->ReadCategoryLC(iter->NextL());
       
   331        
       
   332         CompareCategoriesL(*iCategories[j], *target);
       
   333         CleanupStack::PopAndDestroy(target); 
       
   334         }
       
   335 
       
   336     CleanupStack::PopAndDestroy(iter);
       
   337     
       
   338     }
       
   339 
       
   340 // ---------------------------------------------------------
       
   341 // CPosTp11::IterateReferencedSortedCategoriesL
       
   342 //
       
   343 // (other items were commented in a header).
       
   344 // ---------------------------------------------------------
       
   345 //
       
   346 void CPosTp11::IterateReferencedSortedCategoriesL()
       
   347     {
       
   348     TLinearOrder<CPosLandmarkCategory> order(InternalSortL);
       
   349 
       
   350     iReferencedCategories.Sort(order);
       
   351 
       
   352     CPosLmItemIterator* iter = iCategoryManager->ReferencedCategoryIteratorL(
       
   353                                 CPosLmCategoryManager::ECategorySortOrderNameDescending);
       
   354     CleanupStack::PushL(iter);
       
   355 
       
   356     AssertTrueSecL(iter->NumOfItemsL() == (TUint)(iCategories.Count()/2), KCategoryIteratorNumOfItemsErr);
       
   357 
       
   358     TInt lastIndex=iReferencedCategories.Count()-1;
       
   359     for (TInt i=lastIndex; i>=0; i--)
       
   360         {
       
   361         CPosLandmarkCategory* target = iCategoryManager->ReadCategoryLC(iter->NextL());
       
   362        
       
   363         CompareCategoriesL(iReferencedCategories[i], *target);
       
   364         CleanupStack::PopAndDestroy(target); 
       
   365         }
       
   366 
       
   367     CleanupStack::PopAndDestroy(iter);
       
   368 
       
   369     iter = iCategoryManager->ReferencedCategoryIteratorL(
       
   370                                 CPosLmCategoryManager::ECategorySortOrderNameAscending);
       
   371     CleanupStack::PushL(iter);
       
   372 
       
   373     for (TInt j=0; j < iReferencedCategories.Count(); j++)
       
   374         {
       
   375         CPosLandmarkCategory* target = iCategoryManager->ReadCategoryLC(iter->NextL());
       
   376        
       
   377         CompareCategoriesL(iReferencedCategories[j], *target);
       
   378         CleanupStack::PopAndDestroy(target); 
       
   379         }
       
   380 
       
   381     CleanupStack::PopAndDestroy(iter);
       
   382     }
       
   383 
       
   384 // ---------------------------------------------------------
       
   385 // CPosT11::InternalGet
       
   386 //
       
   387 // (other items were commented in a header).
       
   388 // ---------------------------------------------------------
       
   389 //
       
   390 CPosLandmarkCategory* CPosTp11::InternalGet(TPosLmItemId id)
       
   391     {
       
   392     for (TInt i=0; i<iCategories.Count(); i++)
       
   393         {   
       
   394         if (iCategories[i]->CategoryId() == id)
       
   395             {
       
   396             return iCategories[i];
       
   397             }
       
   398         }
       
   399   
       
   400     return NULL;
       
   401     }
       
   402                                  
       
   403 // ---------------------------------------------------------
       
   404 // CPosT11::CreateEmptyLmDatabaseL() 
       
   405 // ---------------------------------------------------------
       
   406 //
       
   407 void CPosTp11::MakeEmptyLmDatabaseL( CPosLandmarkDatabase& aLdb,
       
   408                                      CPosLmCategoryManager& aCatMgr )    
       
   409     {
       
   410     
       
   411     CPosLmOperation* operation1 = aLdb.RemoveAllLandmarksL();
       
   412     //Remove all landmarks from default db : Sync call     
       
   413     ExecuteAndDeleteLD( operation1 ); 
       
   414 
       
   415     RArray<TPosLmItemId> catArray;
       
   416     CleanupClosePushL( catArray );
       
   417     
       
   418     // Get all the categories from the database and delete them if necessary 
       
   419     CPosLmItemIterator* iter = aCatMgr.CategoryIteratorL();
       
   420     CleanupStack::PushL( iter );
       
   421 
       
   422     TUint count = iter->NumOfItemsL();
       
   423     if ( count > 0 )
       
   424         { // can only be called if there are some items
       
   425         iter->GetItemIdsL( catArray, 0, count );
       
   426         }
       
   427         
       
   428     // Remove all the categories here Sync call     
       
   429     ExecuteAndDeleteLD(aCatMgr.RemoveCategoriesL( catArray ));    
       
   430     CleanupStack::PopAndDestroy( 2 ); // catArray, iter
       
   431     }
       
   432     
       
   433     
       
   434     
       
   435 //  End of File