landmarks/locationlandmarks/tsrc/LandmarkTestModule/src/FT_CPosTp153.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 //  INCLUDES
       
    20 #include "FT_CPosTp153.h"
       
    21 #include <EPos_CPosLandmarkDatabase.h>
       
    22 #include <EPos_CPosLandmarkParser.h>
       
    23 #include <EPos_CPosLandmarkEncoder.h>
       
    24 #include <LbsPosition.h>
       
    25 #include <e32math.h>
       
    26 #include <bautils.h>
       
    27 #include <utf.h>
       
    28 #include <xml/xmlparsererrors.h>
       
    29 
       
    30 // CONSTANTS
       
    31 
       
    32 _LIT(KFileTrace, "c:\\documents\\LandmarksTP153Trace.txt");
       
    33 
       
    34 // See \epoc32\include\xml\XmlParserErrors.h for error codes
       
    35 
       
    36 
       
    37 
       
    38 
       
    39 // See \epoc32\include\xml\XmlParserErrors.h for error codes
       
    40 
       
    41 
       
    42 _LIT(KCorrectFile, "c:\\system\\test\\TestData\\CorrectFileFor_LandmarksTP153Trace.txt");
       
    43 
       
    44 _LIT(KKMZFile, "c:\\system\\test\\TestData\\Tp153KMZFile.kmz");
       
    45 
       
    46 
       
    47 // Maxmimum size for each line in trace file
       
    48 const TInt KBufSize = 5000;
       
    49 
       
    50 // ================= MEMBER FUNCTIONS =======================
       
    51 // ---------------------------------------------------------
       
    52 // CPosTp153::CloseTest
       
    53 //
       
    54 // (other items were commented in a header).
       
    55 // ---------------------------------------------------------
       
    56 //
       
    57 void CPosTp153::CloseTest()
       
    58     {
       
    59     iLog->Log(_L("CloseTest"));
       
    60 
       
    61     delete iEncoderBuffer;
       
    62     iEncoderBuffer = NULL;
       
    63 
       
    64     iLandmarks.ResetAndDestroy();
       
    65     iLandmarks.Close();
       
    66 
       
    67 
       
    68     delete iLandmarkEncoder;
       
    69     iLandmarkEncoder = NULL;
       
    70 
       
    71     delete iLandmarkParser;
       
    72     iLandmarkParser = NULL;
       
    73 
       
    74     delete iOperation;
       
    75     iOperation = NULL;
       
    76 
       
    77 
       
    78     iLog->Log(_L("CloseTest Done"));
       
    79     }
       
    80     
       
    81 // ---------------------------------------------------------
       
    82 // CPosTp153::StartL
       
    83 //
       
    84 // (other items were commented in a header).
       
    85 // ---------------------------------------------------------
       
    86 //
       
    87 void CPosTp153::StartL()
       
    88     {
       
    89     BaflUtils::EnsurePathExistsL( iFileSession, KKMZFile );
       
    90 
       
    91 
       
    92     // Delete the trace file
       
    93     DeleteFileL(KFileTrace);
       
    94 
       
    95 
       
    96     HBufC8* emptyBuffer=NULL;
       
    97     
       
    98    	iLog->Log(_L("Test with Tp153KMZFile.kmz"));
       
    99 	PrintParsedDataFromEncoderL(KKMZFile, *emptyBuffer, -993, KKmzMimeType ); //EXmlTagMismatch = -993
       
   100 
       
   101 
       
   102     // Now compare tha trace file against the correct one
       
   103     RFile correctFile;
       
   104     RFile file;
       
   105 
       
   106     iLog->Log(_L(">>>>>>>Comparing files<<<<<"));
       
   107     
       
   108     TFileName filename;
       
   109     CreateCorrectXmlFileL( KCorrectFile, filename );
       
   110     
       
   111     User::LeaveIfError(correctFile.Open(iFileSession, filename,  EFileRead));
       
   112     CleanupClosePushL(correctFile);
       
   113     User::LeaveIfError(file.Open(iFileSession, KFileTrace,  EFileRead));
       
   114     CleanupClosePushL(file);
       
   115 
       
   116     HBufC* correctLinebuffer = HBufC::NewLC( KBufSize );
       
   117             TPtr correctLine = correctLinebuffer->Des();    
       
   118             HBufC* linebuffer = HBufC::NewLC( KBufSize );
       
   119             TPtr line = linebuffer->Des();    
       
   120    	
       
   121     TInt err = KErrNone;
       
   122     TBool done = EFalse;
       
   123     TInt lineNr = 0;
       
   124 
       
   125     while (ReadLn(correctFile, correctLine) != KErrEof && !done)
       
   126         {
       
   127         lineNr++;
       
   128         err = ReadLn(file, line);
       
   129         if ( err == KErrEof ) 
       
   130             {
       
   131             done = ETrue;    
       
   132             }
       
   133         correctLine.Trim();
       
   134         line.Trim();
       
   135         if (correctLine.Compare(line) != KErrNone)
       
   136             {
       
   137             HBufC* buf = HBufC::NewLC( 128 );
       
   138             TPtr buffer = buf->Des();
       
   139             
       
   140             buffer.Format(_L("ERROR in TraceFile on line %d: "), lineNr);
       
   141             
       
   142             iLog->Log( buffer );
       
   143             iLog->Log( correctLine );
       
   144             iLog->Log( line );
       
   145             iErrorsFound++;
       
   146             CleanupStack::PopAndDestroy( buf );
       
   147             }
       
   148         }
       
   149 		CleanupStack::PopAndDestroy(linebuffer);
       
   150     CleanupStack::PopAndDestroy(correctLinebuffer);
       
   151     CleanupStack::PopAndDestroy(&file);
       
   152     CleanupStack::PopAndDestroy(&correctFile);
       
   153 
       
   154     // EOF for trace file was reach before EOF in the correct file
       
   155     if (done)
       
   156         {
       
   157         iLog->Log(_L("ERROR: EOF for Trace file was reached to early"));
       
   158         iErrorsFound++;
       
   159         }
       
   160         
       
   161         
       
   162     DoCancelTestL();
       
   163 
       
   164     if (iErrorsFound != KErrNone)
       
   165         {
       
   166     	iLog->Log(_L("Errors found in TP153"));
       
   167     	User::Leave(-1);
       
   168         }
       
   169 	}
       
   170 
       
   171 // ---------------------------------------------------------
       
   172 // CPosTp153::DoCancelTestL
       
   173 //
       
   174 // (other items were commented in a header).
       
   175 // ---------------------------------------------------------
       
   176 //
       
   177 void CPosTp153::DoCancelTestL()
       
   178     {
       
   179     iLog->Log(_L("DoCancelTestL"));
       
   180     iLandmarkParser = CPosLandmarkParser::NewL(KKmzMimeType);
       
   181     iLandmarkParser->SetInputFileL(KKMZFile);
       
   182 
       
   183     iOperation = iLandmarkParser->ParseContentL();
       
   184 
       
   185     TRequestStatus status = KPosLmOperationNotComplete;
       
   186     TReal32 progress;
       
   187 
       
   188     // 1 ) Cancel parse function by deleting iOperation
       
   189     delete iOperation;
       
   190     iOperation = NULL;
       
   191 
       
   192     TUint32 nr = iLandmarkParser->NumOfParsedLandmarks();
       
   193     if ((TInt)nr != 0)
       
   194     {
       
   195     	iLog->Log(_L("No landmarks should be found"));
       
   196     	User::Leave(KErrNone);
       
   197     }
       
   198 
       
   199     // No landmark should be found
       
   200     TRAPD(err, iLandmarkParser->LandmarkLC());
       
   201     if (err != KErrNotFound)
       
   202         {
       
   203         iLog->Log(_L("ERROR: No Landmark should be found"));
       
   204         iErrorsFound++;
       
   205         }
       
   206 
       
   207     // 1 ) Cancel parse function by changing input
       
   208     iOperation = iLandmarkParser->ParseContentL();
       
   209 
       
   210     // Get the first landmark
       
   211     iOperation->NextStep(status, progress);
       
   212     // Wait for NextStep to complete
       
   213     User::WaitForRequest(status);
       
   214     if(status == KErrNone)
       
   215 	{
       
   216 	   	nr = iLandmarkParser->NumOfParsedLandmarks();
       
   217 	    if ((TInt)nr != 1)
       
   218 	    {
       
   219 	    	iLog->Log(_L("One landmark should be found"));
       
   220 	    	iErrorsFound++;
       
   221 	    }
       
   222     }
       
   223 
       
   224     // Change input
       
   225     iLandmarkParser->SetInputFileL(KKMZFile);
       
   226 
       
   227     nr = iLandmarkParser->NumOfParsedLandmarks();
       
   228     if ((TInt)nr != 0)
       
   229     {
       
   230     	iLog->Log(_L("No landmarks should be found"));
       
   231     	User::Leave(KErrNone);
       
   232     }
       
   233 
       
   234     // No landmark should be found
       
   235     TRAP(err, iLandmarkParser->LandmarkLC());
       
   236     if (err != KErrNotFound)
       
   237         {
       
   238         iLog->Log(_L("ERROR: No Landmark should be found"));
       
   239         iErrorsFound++;
       
   240         }
       
   241 
       
   242     delete iOperation;
       
   243     iOperation = NULL;
       
   244     delete iLandmarkParser;
       
   245     iLandmarkParser = NULL;
       
   246     iLog->Log(_L("DoCancelTestL Done"));
       
   247     }
       
   248     
       
   249  // ---------------------------------------------------------
       
   250 // CPosTp153::PrintParsedDataFromEncoderL
       
   251 //
       
   252 // (other items were commented in a header).
       
   253 // ---------------------------------------------------------
       
   254 //
       
   255 void CPosTp153::PrintParsedDataFromEncoderL(
       
   256     const TDesC& aFile, 
       
   257     const TDesC8&  aBuffer, 
       
   258     TInt aExpectedStatusCode,
       
   259     const TDesC8& aMimeType)
       
   260     {
       
   261     iLog->Log(_L("===== PrintParsedDataFromEncoderL ======="));
       
   262     
       
   263     iLandmarkParser = CPosLandmarkParser::NewL(aMimeType);
       
   264 
       
   265     if (aFile != KNullDesC)
       
   266         {
       
   267         TFileName file( aFile );
       
   268         iLog->Log(_L("FILE (%S)"), &file );
       
   269         TraceL(_L("----->Using FILE<-------"));
       
   270         TraceL(file);
       
   271         iLandmarkParser->SetInputFileL(file);
       
   272         }
       
   273     else
       
   274         {
       
   275         iLog->Log(_L("BUFFER (size %d)"), aBuffer.Size() );
       
   276         TraceL(_L("----->Using BUFFER<-------"));
       
   277         iLandmarkParser->SetInputBuffer(aBuffer);
       
   278         }
       
   279 
       
   280     iOperation = iLandmarkParser->ParseContentL();
       
   281     TRequestStatus status = KPosLmOperationNotComplete;
       
   282     TReal32 progress;
       
   283     TInt number = 0;
       
   284 
       
   285     RArray<TPosLmItemId> array;
       
   286     CleanupClosePushL(array);
       
   287 
       
   288     while (status == KPosLmOperationNotComplete)
       
   289         {
       
   290         iLog->Log(_L("--- Parsing ---------------------------"));
       
   291         TraceL(_L("------------------------------"));
       
   292         iOperation->NextStep(status, progress);
       
   293 
       
   294         // Wait for NextStep to complete
       
   295         User::WaitForRequest(status);
       
   296         if (status != KPosLmOperationNotComplete && status != KErrNone)
       
   297             {
       
   298             iLog->Log(_L("Parsing Complete"));
       
   299             
       
   300             HBufC* buffer = HBufC::NewLC( 128);
       
   301             TPtr buf = buffer->Des();
       
   302             buf.Format(_L("\tStatus %d"), status.Int());
       
   303             iLog->Log(buf);
       
   304             TraceL(buf);
       
   305             CleanupStack::PopAndDestroy( buffer );
       
   306             buffer = NULL;              
       
   307             }
       
   308         else
       
   309             {
       
   310             iLog->Log(_L("Parsing element"));
       
   311             if ( iLandmarkParser->NumOfParsedLandmarks() )
       
   312                 {
       
   313                 // Get last parsed landmark
       
   314                 CPosLandmark* lm = iLandmarkParser->LandmarkLC();
       
   315                 TPtrC lmName;
       
   316                 TPtrC catName;
       
   317                 lm->GetLandmarkName(lmName);
       
   318                 lm->GetCategoriesL(array);
       
   319                 //iLog->Log(lmName);
       
   320                 for (TInt i=0;i<array.Count();i++)
       
   321                     {
       
   322                     CPosLandmarkCategory* category = iLandmarkParser->LandmarkCategoryLC(array[i]);
       
   323                     category->GetCategoryName( catName );
       
   324                      
       
   325                     HBufC* buffer = HBufC::NewLC( 128 + catName.Length());
       
   326                     TPtr buf = buffer->Des();
       
   327                                             
       
   328                     if ( category->GlobalCategory()) 
       
   329                         {
       
   330                         buf.Append(_L("\tGlobal category: "));    
       
   331                         }
       
   332                     else 
       
   333                         {
       
   334                         buf.Append(_L("\tLocal category: "));    
       
   335                         }                        
       
   336                                         
       
   337                     buf.Append( catName );
       
   338                     iLog->Log( buf );
       
   339                     TraceL( buf );
       
   340 
       
   341                     CleanupStack::PopAndDestroy( buffer );
       
   342                     buffer = NULL;
       
   343                     CleanupStack::PopAndDestroy(category);
       
   344                     }
       
   345                     
       
   346                	PrintLandmarkFieldsWithDescriptionL(*lm, ETrue);	
       
   347                
       
   348                 
       
   349                 number++;
       
   350                 CleanupStack::PopAndDestroy(lm);
       
   351                 }
       
   352             }
       
   353         }
       
   354 
       
   355     iLog->Log(_L("--- Parsing end ---------------------------"));
       
   356     TraceL(_L("------------------------------"));
       
   357     CleanupStack::PopAndDestroy(&array);
       
   358 
       
   359     if (status.Int() != aExpectedStatusCode)
       
   360         {
       
   361         HBufC* buffer = HBufC::NewLC( 128 );
       
   362         TPtr buf = buffer->Des();
       
   363                     
       
   364         buf.Format(_L("\tERROR: Wrong status returned, was %d, should be %d"), status.Int(), aExpectedStatusCode);
       
   365         iLog->Log( buf );
       
   366         iErrorsFound++;
       
   367         
       
   368         CleanupStack::PopAndDestroy( buffer );
       
   369         buffer = NULL;        
       
   370         }
       
   371 
       
   372     TUint32 nr = iLandmarkParser->NumOfParsedLandmarks();
       
   373     HBufC* buffer = HBufC::NewLC( 256 );
       
   374     TPtr buf = buffer->Des();
       
   375     buf.Format(_L("\tNr of Parsed landmarks %d, should be %d"), nr, number);
       
   376     iLog->Log(buf);
       
   377     TraceL(buf);
       
   378 
       
   379     CleanupStack::PopAndDestroy( buffer );
       
   380     buffer = NULL;
       
   381     if (nr != (TUint32)number)
       
   382         {
       
   383         iLog->Log(_L("\tERROR: Wrong number of landmarks parsed returned!"));
       
   384         iErrorsFound++;
       
   385         }
       
   386 
       
   387     TPosLmCollectionDataId lmId = iLandmarkParser->FirstCollectionDataId();
       
   388 
       
   389     iLog->Log(_L("--- CollectionData ---"));
       
   390     TraceL(_L("--- CollectionData ---"));
       
   391 
       
   392     if (lmId != EPosLmCollDataNone)
       
   393         {
       
   394         TPtrC first = iLandmarkParser->CollectionData(lmId);
       
   395         HBufC* buffer = HBufC::NewLC( first.Length() + 256 );
       
   396         TPtr buf = buffer->Des();
       
   397         buf.Zero();
       
   398         buf.Format(_L("\t(1)Collection Id: %d CollectionData: "), lmId);
       
   399         buf.Append(first);
       
   400         iLog->Log(buf);
       
   401         TraceL(buf);
       
   402         CleanupStack::PopAndDestroy( buffer );
       
   403         buffer = NULL;      
       
   404         }
       
   405     else 
       
   406         {
       
   407         iLog->Log(_L("\tNo collection data found 1"));    
       
   408         }
       
   409 
       
   410     while (lmId != EPosLmCollDataNone)
       
   411         {
       
   412         lmId = iLandmarkParser->NextCollectionDataId(lmId);
       
   413         if (lmId != EPosLmCollDataNone)
       
   414         {
       
   415             TPtrC first = iLandmarkParser->CollectionData(lmId);
       
   416             
       
   417             HBufC* buffer = HBufC::NewLC( first.Length() + 256 );
       
   418             TPtr buf = buffer->Des();         
       
   419             buf.Zero();
       
   420             buf.Format(_L("\t(2)Collection Id: %d CollectionData: "), lmId);
       
   421             buf.Append(first);
       
   422             iLog->Log(buf);
       
   423             TraceL(buf);
       
   424             CleanupStack::PopAndDestroy( buffer );
       
   425             buffer = NULL;              
       
   426             }
       
   427         else 
       
   428             {
       
   429             iLog->Log(_L("\tNo collection data found 2"));   
       
   430             }
       
   431         }
       
   432 
       
   433     iLog->Log(_L("--- CollectionData ---"));
       
   434     TraceL(_L("--- CollectionData ---"));
       
   435 
       
   436     delete iOperation;
       
   437     iOperation = NULL;
       
   438     delete iLandmarkParser;
       
   439     iLandmarkParser = NULL;
       
   440     iLog->Log(_L("====== PrintParsedDataFromEncoderL Done ========\n"));
       
   441     }
       
   442     
       
   443     
       
   444  // ---------------------------------------------------------
       
   445 // CPosTp153::PrintLandmarkFieldsWithDescriptionL
       
   446 //
       
   447 // (other items were commented in a header).
       
   448 // ---------------------------------------------------------
       
   449 //
       
   450 void CPosTp153::PrintLandmarkFieldsWithDescriptionL(const CPosLandmark& aLandmark, TBool aTraceFlag)
       
   451     {
       
   452     iLog->Log(_L("Parsing Description ... \n"));
       
   453     TPositionFieldId sourceFieldId = aLandmark.FirstPositionFieldId();
       
   454     TLocality loc;
       
   455 
       
   456     TInt err;
       
   457     TPtrC landmarkName;
       
   458     TPtrC landmarkDescription;
       
   459     err = aLandmark.GetLandmarkName(landmarkName);
       
   460     if (err == KErrNone)
       
   461         {
       
   462         HBufC* buffer = HBufC::NewLC( landmarkName.Length() + 256 );
       
   463         TPtr buf = buffer->Des();
       
   464                     
       
   465         buf.Append( _L(" \tLandmark Name: "));
       
   466         buf.Append( landmarkName );
       
   467         iLog->Log( buf );
       
   468         if( aTraceFlag ) 
       
   469             {
       
   470             TraceL( buf );   
       
   471             }
       
   472             
       
   473         CleanupStack::PopAndDestroy( buffer );
       
   474         buffer = NULL;  
       
   475         }
       
   476 
       
   477     err = aLandmark.GetPosition(loc);
       
   478     if (err == KErrNone)
       
   479         {
       
   480         HBufC* buffer = HBufC::NewLC( 1024 );
       
   481         TPtr buf = buffer->Des();        
       
   482         TRealFormat format( 12, KRealFormatFixed );
       
   483         format.iPoint = TChar('.');
       
   484         format.iTriLen = KDoNotUseTriads;
       
   485         format.iPlaces = 6;
       
   486         
       
   487         TBuf<20> sLon, sLat, sVacc, sHacc, sAlt, sRad;
       
   488         sLon.Num( loc.Longitude(), format );
       
   489         sLat.Num( loc.Latitude(), format );
       
   490         sAlt.Num( loc.Altitude(), format );
       
   491         sVacc.Num( loc.VerticalAccuracy(), format );
       
   492         sHacc.Num( loc.HorizontalAccuracy(), format );
       
   493         buf.Format(
       
   494             _L("\tLM: Long %S Lat %S vertAcc %S horAcc %S alt %S "), 
       
   495             &sLon, &sLat, &sVacc, &sHacc, &sAlt);
       
   496         
       
   497         TReal32 sourceR;
       
   498         err = aLandmark.GetCoverageRadius(sourceR);
       
   499         if (err == KErrNone )
       
   500             {
       
   501             sRad.Num( sourceR, format );
       
   502             buf.AppendFormat(_L("srcRadius %S"), &sRad);
       
   503             }
       
   504         iLog->Log(buf);
       
   505         if (aTraceFlag) TraceL(buf);
       
   506         CleanupStack::PopAndDestroy( buffer );
       
   507         buffer = NULL;  
       
   508         }
       
   509         
       
   510       //Get landmark description  
       
   511       err = aLandmark.GetLandmarkDescription(landmarkDescription);
       
   512       if (err == KErrNone)
       
   513         {
       
   514         HBufC* buffer = HBufC::NewLC( landmarkDescription.Length() + 256 );
       
   515         TPtr buf = buffer->Des();         
       
   516         buf.Format(_L("\tLandmark Description: "));
       
   517         buf.Append(landmarkDescription);
       
   518         iLog->Log(buf);
       
   519         if ( aTraceFlag ) 
       
   520             {
       
   521             TraceL( buf );    
       
   522             }
       
   523         
       
   524         CleanupStack::PopAndDestroy( buffer );
       
   525         buffer = NULL;         
       
   526         }  
       
   527     
       
   528 
       
   529     while (sourceFieldId != EPositionFieldNone)
       
   530         {
       
   531         TPtrC sourceValue;
       
   532         aLandmark.GetPositionField(sourceFieldId, sourceValue);
       
   533         
       
   534         HBufC* buffer = HBufC::NewLC( sourceValue.Length() + 256 );
       
   535         TPtr buf = buffer->Des();  
       
   536         buf.Format(_L("\tIdField Id: %d Value:"), sourceFieldId);
       
   537         buf.Append(sourceValue);
       
   538         iLog->Log(buf);
       
   539         if ( aTraceFlag ) 
       
   540             {
       
   541             TraceL( buf );
       
   542             }
       
   543         sourceFieldId = aLandmark.NextPositionFieldId(sourceFieldId);
       
   544         
       
   545         CleanupStack::PopAndDestroy( buffer );
       
   546         }
       
   547     }
       
   548     
       
   549     
       
   550 // ---------------------------------------------------------
       
   551 // CPosTp153::TraceL
       
   552 //
       
   553 // (other items were commented in a header).
       
   554 // ---------------------------------------------------------
       
   555 //
       
   556 void CPosTp153::TraceL(const TDesC& msg)
       
   557     {
       
   558     _LIT8( KEnd, "\r\n" );
       
   559 
       
   560     RFile file;
       
   561     TInt err = file.Open(iFileSession, KFileTrace, EFileWrite);
       
   562     if (err == KErrNotFound)
       
   563         {
       
   564         file.Create(iFileSession, KFileTrace, EFileWrite);
       
   565         }
       
   566 
       
   567     HBufC8* line8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L( msg );
       
   568     
       
   569     TInt pos( 0 );
       
   570     file.Seek( ESeekEnd, pos );
       
   571     file.Write( *line8 );
       
   572     file.Write( KEnd );
       
   573     file.Close();
       
   574 
       
   575     delete line8;
       
   576     }
       
   577 
       
   578 //  End of File