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