landmarks/locationlandmarks/tsrc/LandmarkTestModule/src/FT_CPosTp49.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_CPosTp49.h"
       
    22 #include <EPos_CPosLandmarkDatabase.h>
       
    23 #include <EPos_CPosLandmarkParser.h>
       
    24 #include <Xml/XmlParserErrors.h>
       
    25 
       
    26 // CONSTANTS
       
    27 #ifdef __WINS__
       
    28 _LIT(KFileWithCorrectSyntax, "z:\\system\\test\\testdata\\Tp49ImportInput.xml");
       
    29 _LIT(KFileWithSyntaxError, "z:\\system\\test\\testdata\\Tp49ImportInputSyntaxError.xml");
       
    30 #else
       
    31 _LIT(KFileWithCorrectSyntax, "c:\\system\\test\\testdata\\Tp49ImportInput.xml");
       
    32 _LIT(KFileWithSyntaxError, "c:\\system\\test\\testdata\\Tp49ImportInputSyntaxError.xml");
       
    33 #endif
       
    34 const TInt KNrOfImportedLandmarks = 7;
       
    35 
       
    36 #ifdef __WINS__
       
    37 const TInt KNrOfCorrectLandmarks = 3;
       
    38 #else
       
    39 const TInt KNrOfCorrectLandmarks = 4;
       
    40 #endif
       
    41 
       
    42 // ================= MEMBER FUNCTIONS =======================
       
    43 
       
    44 // ---------------------------------------------------------
       
    45 // CPosTp49::GetName
       
    46 //
       
    47 // (other items were commented in a header).
       
    48 // ---------------------------------------------------------
       
    49 //
       
    50 void CPosTp49::GetName(TDes& aName) const
       
    51     {
       
    52     _LIT(KTestName, "TP49 - Iterate imported landmarks");
       
    53     aName = KTestName;
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------
       
    57 // CPosTp49::InitTestL
       
    58 //
       
    59 // (other items were commented in a header).
       
    60 // ---------------------------------------------------------
       
    61 //
       
    62 void CPosTp49::InitTestL()
       
    63     {
       
    64     RemoveDefaultDbL();
       
    65     RemoveAllLmDatabasesL();
       
    66 
       
    67     SetupPreConditionsL();
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------
       
    71 // CPosTp49::CloseTest
       
    72 //
       
    73 // (other items were commented in a header).
       
    74 // ---------------------------------------------------------
       
    75 //
       
    76 void CPosTp49::CloseTest()
       
    77     {
       
    78     // Delete and set base class members to NULL
       
    79     delete iOperation;
       
    80     iOperation = NULL;
       
    81     delete iIterator;
       
    82     iIterator = NULL;
       
    83     delete iDatabase;
       
    84     iDatabase = NULL;
       
    85 
       
    86     delete iLandmarkParser;
       
    87     iImportedLandmarkIds.Close();
       
    88     iRemovedLandmarkIds.Close();
       
    89     
       
    90     delete iLandmarkNames;
       
    91     iLandmarkNames = NULL;
       
    92 
       
    93     }
       
    94 
       
    95 // ---------------------------------------------------------
       
    96 // CPosTp49::NotifyProgress
       
    97 //
       
    98 // (other items were commented in a header).
       
    99 // ---------------------------------------------------------
       
   100 //
       
   101 void CPosTp49::NotifyProgress(TReal aProgress)
       
   102     {
       
   103     if (aProgress > iProgressInterruptLevel)
       
   104         {
       
   105         CActiveScheduler::Stop();
       
   106         }
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------
       
   110 // CPosTp49::StartL
       
   111 //
       
   112 // (other items were commented in a header).
       
   113 // ---------------------------------------------------------
       
   114 //
       
   115 void CPosTp49::StartL()
       
   116     {
       
   117     // <<<<<<<<<<<<<< 1-2 Import unparsed landmarks >>>>>>>>>>>>>>>>>>>
       
   118     RecreateParserL(KFileWithCorrectSyntax);
       
   119     ImportAllLandmarksL();
       
   120     FetchIteratorL();
       
   121     VerifyImportedLandmarksL(KNrOfImportedLandmarks);
       
   122 
       
   123     // <<<<<<<<<<<<<< 3-5 Import parsed landmarks >>>>>>>>>>>>>>>>>>>
       
   124     RecreateParserL(KFileWithCorrectSyntax);
       
   125     ParseLandmarksContentL();
       
   126     ImportAllLandmarksL();
       
   127     FetchIteratorL();
       
   128     VerifyImportedLandmarksL(KNrOfImportedLandmarks);
       
   129 
       
   130     // <<<<<<<<<<<<<< 6-8 Remove some imported landmarks >>>>>>>>>>>>>>>>>>>
       
   131     RecreateParserL(KFileWithCorrectSyntax);
       
   132     ImportAllLandmarksL();
       
   133     FetchIteratorL();
       
   134     RemoveThreeLandmarksL();
       
   135     FetchIteratorL();
       
   136     VerifyImportedLandmarksL(KNrOfImportedLandmarks);
       
   137 
       
   138     // <<<<<<<<<<<<<< 9-10 Fetch iterator before import is ready >>>>>>>>>>>>>>>>>>>
       
   139     iProgressInterruptLevel = 0.5; // Cancel the import operation when half ready.
       
   140     TInt nrOfLandmarks = ImportSomeLandmarksL();
       
   141     // Cancel import operation, otherwise db will be locked when verifying
       
   142     delete iOperation; iOperation = NULL;
       
   143     VerifyImportedLandmarksL(nrOfLandmarks);
       
   144 
       
   145     // <<<<<<<<<<<<<< 11-12 Fetch iterator before import is started >>>>>>>>>>>>>>>>>>>
       
   146     iProgressInterruptLevel = 0; // Cancel the import operation as fast as possible.
       
   147     nrOfLandmarks = ImportSomeLandmarksL();
       
   148     // Cancel import operation, otherwise db will be locked when verifying
       
   149     delete iOperation; iOperation = NULL;
       
   150     VerifyImportedLandmarksL(nrOfLandmarks);
       
   151 
       
   152     // <<<<<<<<<<<<<< 13-14 Fetch iterator if import fails >>>>>>>>>>>>>>>>>>>
       
   153     RecreateParserL(KFileWithSyntaxError);
       
   154     StartImportLandmarksL();
       
   155     ExecuteLmOpL(EXmlTagMismatch);
       
   156     FetchIteratorL();
       
   157     CreateNameArrayL(KNrOfCorrectLandmarks);
       
   158     // We don't need to cancel operation since import operation failed. Hence 
       
   159     // it should release the database lock itself.
       
   160     // delete iOperation; iOperation = NULL;
       
   161     // Note: It seems that there is a difference between target and emulator in the number of imported 
       
   162     // landmarks in each next step, thus it differs in the number of imported lms before the incorrect xml-landmark
       
   163     // is found
       
   164     VerifyImportedLandmarksL(KNrOfCorrectLandmarks);
       
   165 
       
   166     // <<<<<<<<<<<<<< 15 Test leave codes - nothing to do >>>>>>>>>>>>>>>>>>>
       
   167     iLog->Put(_L("TP49 Done"));
       
   168 	}
       
   169 
       
   170 // ---------------------------------------------------------
       
   171 // CPosTp49::ImportAllLandmarksL
       
   172 //
       
   173 // (other items were commented in a header).
       
   174 // ---------------------------------------------------------
       
   175 //
       
   176 void CPosTp49::ImportAllLandmarksL()
       
   177     {
       
   178     // Fetch nr of existing items before import
       
   179     CPosLmItemIterator* iterator = iDatabase->LandmarkIteratorL();
       
   180     CleanupStack::PushL(iterator);
       
   181     TInt nrOfItems = iterator->NumOfItemsL();
       
   182     CleanupStack::PopAndDestroy(iterator);
       
   183 
       
   184     StartImportLandmarksL();
       
   185     ExecuteLmOpL(KErrNone);
       
   186     CreateNameArrayL(KNrOfImportedLandmarks);
       
   187 
       
   188     // Check nr of landmarks in db after import
       
   189     iterator = iDatabase->LandmarkIteratorL();
       
   190     CleanupStack::PushL(iterator);
       
   191     _LIT(KErrMsg, "Not all landmarks were imported to database!");
       
   192     TInt nrOfImportedItems = iterator->NumOfItemsL() - nrOfItems;
       
   193     AssertTrueSecL(
       
   194         nrOfImportedItems == KNrOfImportedLandmarks, KErrMsg, KErrGeneral);
       
   195     CleanupStack::PopAndDestroy(iterator);
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------
       
   199 // CPosTp49::ImportSomeLandmarksL
       
   200 //
       
   201 // (other items were commented in a header).
       
   202 // ---------------------------------------------------------
       
   203 //
       
   204 TInt CPosTp49::ImportSomeLandmarksL()
       
   205     {
       
   206     RecreateParserL(KFileWithCorrectSyntax);
       
   207     StartImportLandmarksL();
       
   208     ExecuteLmOpL(KPosLmOperationNotComplete, ETrue);
       
   209     FetchIteratorL();
       
   210     CreateNameArrayL(iIterator->NumOfItemsL());
       
   211     return iIterator->NumOfItemsL();
       
   212     }
       
   213 
       
   214 // ---------------------------------------------------------
       
   215 // CPosTp49::ParseLandmarksContentL
       
   216 //
       
   217 // (other items were commented in a header).
       
   218 // ---------------------------------------------------------
       
   219 //
       
   220 void CPosTp49::ParseLandmarksContentL()
       
   221     {
       
   222     delete iOperation;
       
   223     iOperation = NULL;
       
   224     iOperation = iLandmarkParser->ParseContentL();
       
   225     ExecuteLmOpL(KErrNone);
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------
       
   229 // CPosTp49::RemoveThreeLandmarksL
       
   230 //
       
   231 // (other items were commented in a header).
       
   232 // ---------------------------------------------------------
       
   233 //
       
   234 void CPosTp49::RemoveThreeLandmarksL()
       
   235     {
       
   236     // It is important that the highest index is removed first.
       
   237     TInt indexToRemove[] = {6, 5, 2};
       
   238 
       
   239     const TInt KNrOfItemsToRemove = 3;
       
   240     _LIT(KErrMsg, "Removed landmark exists in database!");
       
   241 
       
   242     for (TInt i = 0; i < KNrOfItemsToRemove; i++)
       
   243         {
       
   244         User::LeaveIfError(
       
   245             iRemovedLandmarkIds.Append(iImportedLandmarkIds[indexToRemove[i]]));
       
   246         iLandmarkNames->Delete(indexToRemove[i]);
       
   247         iDatabase->RemoveLandmarkL(iImportedLandmarkIds[indexToRemove[i]]);
       
   248 
       
   249         // Verify landmark is removed
       
   250         CPosLandmark* lm = NULL;
       
   251         TRAPD(err, lm = iDatabase->ReadLandmarkLC(iImportedLandmarkIds[indexToRemove[i]]));
       
   252         delete lm;
       
   253         lm = NULL;
       
   254         AssertTrueSecL(err == KErrNotFound, KErrMsg, KErrGeneral);
       
   255         }
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------
       
   259 // CPosTp49::VerifyImportedLandmarksL
       
   260 //
       
   261 // (other items were commented in a header).
       
   262 // ---------------------------------------------------------
       
   263 //
       
   264 void CPosTp49::VerifyImportedLandmarksL(TInt aNrOfExpectedItems)
       
   265     {
       
   266     iLog->Put(_L("VerifyImportedLandmarksL"));
       
   267     // Iterate through all imported landmarks
       
   268     TInt i = 0;
       
   269     TPosLmItemId id = iIterator->NextL();
       
   270     while (id != KPosLmNullItemId)
       
   271         {
       
   272         CPosLandmark* lm = NULL;
       
   273         if (iRemovedLandmarkIds.Find(id) == KErrNotFound)
       
   274             {
       
   275             // The landmark has not been removed from database.
       
   276             // Try to read the imported landmark.
       
   277             lm = iDatabase->ReadLandmarkLC(id);
       
   278 
       
   279             // Verify that the landmark name exists in the name array.
       
   280             _LIT(KErrMsg, "Unexpected landmark name");
       
   281             _LIT(KErrMsg1, "GetLandmarkName returned KErrNotFound");
       
   282             TPtrC name;
       
   283             TInt res = lm->GetLandmarkName(name);
       
   284             if (res == KErrNotFound)
       
   285                 {
       
   286                 // No name found at all
       
   287                 LogErrorAndLeave(KErrMsg1, KErrGeneral);
       
   288                 }
       
   289             else
       
   290                 {
       
   291                 TInt index;
       
   292                 iLog->Put(name);
       
   293                 res = iLandmarkNames->Find(name, index);
       
   294                 if (res != KErrNone)
       
   295                     {
       
   296                     // Found name does not match any name in the array.
       
   297                     LogErrorAndLeave(KErrMsg, KErrGeneral);
       
   298                     }
       
   299                 }
       
   300             CleanupStack::PopAndDestroy(lm);
       
   301             }
       
   302         else
       
   303             {
       
   304             // The landmark has been removed from db. 
       
   305             // Verify that landmark does not exist in database.
       
   306             _LIT(KErrMsg, "Removed landmark exists in database!");
       
   307             TRAPD(err, lm = iDatabase->ReadLandmarkLC(id));
       
   308             delete lm;
       
   309             lm = NULL;
       
   310             AssertTrueSecL(err == KErrNotFound, KErrMsg, KErrGeneral);
       
   311             }
       
   312 
       
   313         i++;
       
   314         id = iIterator->NextL();
       
   315         }
       
   316 
       
   317     // Verify that length of iterator is equal to the nr of imported landmarks.
       
   318     _LIT(KErrMsg, "Nr of iterated landmarks did not match the nr of imported.");
       
   319     AssertTrueSecL(i == aNrOfExpectedItems, KErrMsg, KErrGeneral);
       
   320     }
       
   321 
       
   322 // ---------------------------------------------------------
       
   323 // CPosTp49::SetupPreConditionsL
       
   324 //
       
   325 // (other items were commented in a header).
       
   326 // ---------------------------------------------------------
       
   327 //
       
   328 void CPosTp49::SetupPreConditionsL()
       
   329     {
       
   330     // Precondition: A non-empty database exists
       
   331     CPosLandmarkDatabase* db = OpenDatabaseLC();
       
   332     CleanupStack::Pop(db);
       
   333     iDatabase = db;
       
   334     
       
   335     ExecuteAndDeleteLD(iDatabase->CompactL());
       
   336 
       
   337     // Add one item to make it non-empty
       
   338     CPosLandmark* dummy = CPosLandmark::NewLC();
       
   339     iDatabase->AddLandmarkL(*dummy);
       
   340     CleanupStack::PopAndDestroy(dummy);
       
   341     }
       
   342 
       
   343 // ---------------------------------------------------------
       
   344 // CPosTp49::ExecuteLmOpL
       
   345 //
       
   346 // (other items were commented in a header).
       
   347 // ---------------------------------------------------------
       
   348 //
       
   349 void CPosTp49::ExecuteLmOpL(TInt aExpectedResult, TBool aObserveProgress)
       
   350     {
       
   351     CActiveLmOperation* operationWrapper = 
       
   352         new (ELeave) CActiveLmOperation(iLog);
       
   353     CleanupStack::PushL(operationWrapper);
       
   354 
       
   355     if (aObserveProgress)
       
   356         {
       
   357         operationWrapper->Start(iOperation, this);
       
   358         }
       
   359     else 
       
   360         {
       
   361         operationWrapper->Start(iOperation);
       
   362         }
       
   363 
       
   364     CActiveScheduler::Start();
       
   365     if (aExpectedResult == KErrNone)
       
   366         {
       
   367         operationWrapper->CheckOperationL();
       
   368         }
       
   369     else
       
   370         {
       
   371         if (aExpectedResult != operationWrapper->Status())
       
   372             {
       
   373             _LIT(KErrMsg, "Expected result %d but got %d");
       
   374             TBuf<50> msg;
       
   375             msg.Format(KErrMsg, aExpectedResult, operationWrapper->Status());
       
   376             LogErrorAndLeave(msg, KErrGeneral);
       
   377             }
       
   378         }
       
   379 
       
   380     CleanupStack::PopAndDestroy(operationWrapper);
       
   381     }
       
   382 
       
   383 // ---------------------------------------------------------
       
   384 // CPosTp49::FetchIteratorL
       
   385 //
       
   386 // (other items were commented in a header).
       
   387 // ---------------------------------------------------------
       
   388 //
       
   389 void CPosTp49::FetchIteratorL()
       
   390     {
       
   391     delete iIterator;
       
   392     iIterator = NULL;
       
   393     iIterator = iDatabase->ImportedLandmarksIteratorL(iOperation);
       
   394     TBuf<100> buf;
       
   395     iImportedLandmarkIds.Close();
       
   396     TInt nrOfItems = (TInt) iIterator->NumOfItemsL();
       
   397     buf.Format(_L("Retreving %d nr of ids from iterator"), nrOfItems);
       
   398     iLog->Put(buf);
       
   399     if (nrOfItems > 0)
       
   400         {
       
   401         iIterator->GetItemIdsL(iImportedLandmarkIds, 0, nrOfItems);
       
   402         }
       
   403     buf.Zero();    
       
   404     for (TInt i=0;i<iImportedLandmarkIds.Count();i++)
       
   405         {
       
   406         TBuf<15> idde;
       
   407         idde.Format(_L(" Id:%d "), iImportedLandmarkIds[i]);
       
   408         buf.Append(idde);
       
   409         }
       
   410     iLog->Put(buf);
       
   411     }
       
   412 
       
   413 // ---------------------------------------------------------
       
   414 // CPosTp49::RecreateParserL
       
   415 //
       
   416 // (other items were commented in a header).
       
   417 // ---------------------------------------------------------
       
   418 //
       
   419 void CPosTp49::RecreateParserL(const TDesC& aFileName)
       
   420     {
       
   421     delete iLandmarkParser;
       
   422     iLandmarkParser = NULL;
       
   423     iLandmarkParser = CPosLandmarkParser::NewL(KMimeType);
       
   424     iLandmarkParser->SetInputFileL(aFileName);
       
   425     }
       
   426 
       
   427 // ---------------------------------------------------------
       
   428 // CPosTp49::StartImportLandmarksL
       
   429 //
       
   430 // (other items were commented in a header).
       
   431 // ---------------------------------------------------------
       
   432 //
       
   433 void CPosTp49::StartImportLandmarksL()
       
   434     {
       
   435     delete iOperation;
       
   436     iOperation = NULL;
       
   437     iLog->Put(_L("Innan ImportLandmarksL"));
       
   438     iOperation = iDatabase->ImportLandmarksL(
       
   439         *iLandmarkParser, CPosLandmarkDatabase::EDefaultOptions);
       
   440     iLog->Put(_L("EfterImportLandmarksL"));
       
   441     }
       
   442 
       
   443 // ---------------------------------------------------------
       
   444 // CPosTp49::CreateNameArrayL
       
   445 //
       
   446 // (other items were commented in a header).
       
   447 // ---------------------------------------------------------
       
   448 //
       
   449 void CPosTp49::CreateNameArrayL(TInt aNrOfItems)
       
   450     {
       
   451     _LIT(KName0, "Landmark 0");
       
   452     _LIT(KName1, "Landmark 1");
       
   453     _LIT(KName2, "Landmark 2");
       
   454     _LIT(KName3, "Landmark 3");
       
   455     _LIT(KName4, "Landmark 4");
       
   456     _LIT(KName5, "Landmark 5");
       
   457     _LIT(KName6, "Landmark 6");
       
   458 
       
   459     const TDesC* names[] = {&KName0, &KName1, &KName2, &KName3, &KName4, 
       
   460         &KName5, &KName6};
       
   461 
       
   462     // Create array of landmark names. Names match those specified in the 
       
   463     // import file.
       
   464     delete iLandmarkNames;
       
   465     iLandmarkNames = new (ELeave) CDesCArrayFlat(8);
       
   466     for (TInt i = 0; i < aNrOfItems; i++)
       
   467         {
       
   468         iLandmarkNames->AppendL(*names[i]);
       
   469         }
       
   470     }
       
   471 
       
   472 //  End of File