landmarks/locationlandmarks/tsrc/LandmarkTestModule/src/FT_CPosTp58.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_CPosTp58.h"
       
    22 #include <EPos_CPosLandmarkDatabase.h>
       
    23 #include <EPos_CPosLandmarkParser.h>
       
    24 
       
    25 // CONSTANTS
       
    26 #ifdef __WINS__
       
    27 _LIT(KXMLFile, "z:\\system\\test\\testdata\\Tp49ImportInput.xml"); // re-use
       
    28 #else
       
    29 _LIT(KXMLFile, "c:\\system\\test\\testdata\\Tp49ImportInput.xml"); // re-use
       
    30 #endif
       
    31 _LIT(KTp58Panic, "Timeout Panic");
       
    32 const TInt KTp58PanicNumber = 58;
       
    33 
       
    34 // ================= LOCAL FUNCTIONS =======================
       
    35 
       
    36 // ---------------------------------------------------------
       
    37 // LOCAL_C ImportThreadFunction
       
    38 //
       
    39 // (other items were commented in a header).
       
    40 // ---------------------------------------------------------
       
    41 //
       
    42 LOCAL_C TInt ImportThreadFunction(TAny* aData)
       
    43     {
       
    44     CTrapCleanup* cleanup = CTrapCleanup::New(); 
       
    45 
       
    46     CActiveScheduler* actSch = new (ELeave) CActiveScheduler;
       
    47     CActiveScheduler::Install(actSch);
       
    48 
       
    49     TRAPD(err, CPosTp58::RunImportTestL(aData));
       
    50        
       
    51     delete actSch;
       
    52     delete cleanup;
       
    53     return err;
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------
       
    57 // LOCAL_C KillerThreadFunction
       
    58 //
       
    59 // (other items were commented in a header).
       
    60 // ---------------------------------------------------------
       
    61 //
       
    62 LOCAL_C TInt KillerThreadFunction(TAny* aData)
       
    63     {
       
    64     CTrapCleanup* cleanup = CTrapCleanup::New(); 
       
    65 
       
    66     CActiveScheduler* actSch = new (ELeave) CActiveScheduler;
       
    67     CActiveScheduler::Install(actSch);
       
    68 
       
    69     TRAPD(err, CPosTp58::RunThreadKillerL(reinterpret_cast <RThread*> (aData)));
       
    70        
       
    71     delete actSch;
       
    72     delete cleanup;
       
    73     return err;
       
    74     }
       
    75 
       
    76 // ================= MEMBER FUNCTIONS =======================
       
    77 
       
    78 // ---------------------------------------------------------
       
    79 // CPosTp58::GetName
       
    80 //
       
    81 // (other items were commented in a header).
       
    82 // ---------------------------------------------------------
       
    83 //
       
    84 void CPosTp58::GetName(TDes& aName) const
       
    85     {
       
    86     _LIT(KTestName, "TP58 - Import landmarks using User::WaitForRequest()");
       
    87     aName = KTestName;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------
       
    91 // CPosTp58::InitTestL
       
    92 //
       
    93 // (other items were commented in a header).
       
    94 // ---------------------------------------------------------
       
    95 //
       
    96 void CPosTp58::InitTestL()
       
    97     {
       
    98     MakeSurePanicDebugFileExistsL();
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------
       
   102 // CPosTp58::CloseTest
       
   103 //
       
   104 // (other items were commented in a header).
       
   105 // ---------------------------------------------------------
       
   106 //
       
   107 void CPosTp58::CloseTest()
       
   108     {
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------
       
   112 // CPosTp58::StartL
       
   113 //
       
   114 // (other items were commented in a header).
       
   115 // ---------------------------------------------------------
       
   116 //
       
   117 void CPosTp58::StartL()
       
   118     {
       
   119 // Test Import all landmarks
       
   120     iLog->Put(_L("Importing all landmarks"));
       
   121     iTestCase = ETestImportAllLandmarks;
       
   122     iExpectedErrorCode = KErrGeneral;
       
   123     ResumeThreadsAndVerifyExitL();
       
   124 
       
   125 // Test Import subset of landmarks
       
   126     iLog->Put(_L("Importing subset of all landmarks"));
       
   127     iTestCase = ETestImportSubsetOfLandmarks;
       
   128     iExpectedErrorCode = KErrGeneral;
       
   129     ResumeThreadsAndVerifyExitL();
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------
       
   133 // CPosTp58::InitDbLC
       
   134 //
       
   135 // (other items were commented in a header).
       
   136 // ---------------------------------------------------------
       
   137 //
       
   138 CPosLandmarkDatabase* CPosTp58::InitDbLC()
       
   139     {
       
   140     RemoveDefaultDbL();
       
   141     CPosLandmarkDatabase* database = CPosLandmarkDatabase::OpenL();
       
   142     CleanupStack::PushL(database);
       
   143     
       
   144     if (database->IsInitializingNeeded())
       
   145         {
       
   146         ExecuteAndDeleteLD(database->InitializeL());
       
   147         }
       
   148 
       
   149     return database;
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------
       
   153 // CPosTp58::RunImportTestL
       
   154 //
       
   155 // (other items were commented in a header).
       
   156 // ---------------------------------------------------------
       
   157 //
       
   158 void CPosTp58::RunImportTestL(TAny* aData)
       
   159     {
       
   160     CPosTp58* tp58 = reinterpret_cast <CPosTp58*> (aData);
       
   161 
       
   162     switch (tp58->iTestCase)
       
   163         {
       
   164         case ETestImportAllLandmarks:
       
   165             tp58->ImportLandmarksL(NULL);
       
   166             break;
       
   167         case ETestImportSubsetOfLandmarks:
       
   168             {
       
   169             RArray<TUint> subset;
       
   170             CleanupClosePushL(subset);    
       
   171             subset.Append(5);
       
   172             subset.Append(1);
       
   173             subset.Append(9);
       
   174             subset.Append(3);
       
   175 
       
   176             tp58->ImportLandmarksL(&subset);
       
   177 
       
   178             CleanupStack::PopAndDestroy(&subset);
       
   179             }
       
   180             break;
       
   181         };
       
   182     }
       
   183 
       
   184 // ---------------------------------------------------------
       
   185 // CPosTp58::RunThreadKillerL
       
   186 //
       
   187 // (other items were commented in a header).
       
   188 // ---------------------------------------------------------
       
   189 //
       
   190 void CPosTp58::RunThreadKillerL(RThread* aThread)
       
   191     {
       
   192     TTimeIntervalMicroSeconds32 oneMinute = 60 * 1000000;
       
   193 
       
   194     // Enter a infinite loop. This reason to this is that I want to be sure 
       
   195     // that it is killed by the TP58-thread.
       
   196     TBool forever = ETrue;
       
   197     while (forever)
       
   198         {
       
   199         User::After(oneMinute);
       
   200 
       
   201         // Panic importThread if it is still alive
       
   202         if (aThread->ExitType() == EExitPending)
       
   203             {
       
   204             TRequestStatus status;
       
   205             aThread->Logon(status);
       
   206             aThread->Panic(KTp58Panic, KTp58PanicNumber);
       
   207             User::WaitForRequest(status);
       
   208             }
       
   209         }
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------
       
   213 // CPosTp58::ImportLandmarksL
       
   214 //
       
   215 // (other items were commented in a header).
       
   216 // ---------------------------------------------------------
       
   217 //
       
   218 void CPosTp58::ImportLandmarksL(const RArray<TUint>* aLandmarkSubSet)
       
   219     {
       
   220     CPosLandmarkDatabase* db = InitDbLC();
       
   221 
       
   222     // Recreate parser
       
   223     CPosLandmarkParser* landmarkParser = CPosLandmarkParser::NewL(KMimeType);
       
   224     CleanupStack::PushL(landmarkParser);
       
   225     landmarkParser->SetInputFileL(KXMLFile);
       
   226 
       
   227     CPosLmOperation* operation = NULL;
       
   228     if (aLandmarkSubSet)
       
   229         {
       
   230         // Import only a subset of the landmarks in parser
       
   231         operation = db->ImportLandmarksL(
       
   232             *landmarkParser, 
       
   233             *aLandmarkSubSet,  
       
   234             CPosLandmarkDatabase::EDefaultOptions);
       
   235         }
       
   236     else
       
   237         {
       
   238         operation = db->ImportLandmarksL(
       
   239             *landmarkParser, 
       
   240             CPosLandmarkDatabase::EDefaultOptions);
       
   241         }
       
   242     TRequestStatus status(KPosLmOperationNotComplete);
       
   243     TReal32 progress(0);
       
   244     operation->NextStep(status, progress);
       
   245 
       
   246     // Wait for NextStep to complete - should hang
       
   247     User::WaitForRequest(status);
       
   248 
       
   249     // Cancel the import operation
       
   250     delete operation;
       
   251 
       
   252     CleanupStack::PopAndDestroy(2, db);
       
   253     }
       
   254 
       
   255 // ---------------------------------------------------------
       
   256 // CPosTp58::ResumeThreadsAndVerifyExitL
       
   257 //
       
   258 // (other items were commented in a header).
       
   259 // ---------------------------------------------------------
       
   260 //
       
   261 void CPosTp58::ResumeThreadsAndVerifyExitL()
       
   262     {
       
   263     iLog->Put(_L("ResumeThreadAndVerifyExitL"));
       
   264     _LIT(KCreateThreadErr, "Creating thread failed with %d");
       
   265 
       
   266 	TBuf<100> threadName;
       
   267     // Create import thread
       
   268     RThread importThread;
       
   269     CleanupClosePushL(importThread);
       
   270     _LIT(KImportThreadName, "TP58 - Import thread, part %d");
       
   271     threadName.Format(KImportThreadName, iTestCase);
       
   272     iLog->Put(_L("Creating Import thread"));
       
   273     //TInt err = importThread.Create(KImportThreadName, ImportThreadFunction, 
       
   274     TInt err = importThread.Create(threadName, ImportThreadFunction, 
       
   275         KDefaultStackSize, KMinHeapSize, KMaxHeapSize, this);
       
   276 
       
   277     AssertTrueSecL(err == KErrNone, KCreateThreadErr, err);
       
   278 
       
   279     // Create killer thread
       
   280     RThread killerThread;
       
   281     CleanupClosePushL(killerThread);
       
   282     _LIT(KKillerThreadName, "TP58 - Killer thread, part %d");
       
   283     threadName.Format(KKillerThreadName, iTestCase);
       
   284     iLog->Put(_L("Creating Killer thread"));
       
   285     err = killerThread.Create(threadName, KillerThreadFunction, 
       
   286         KDefaultStackSize, KMinHeapSize, KMaxHeapSize, &importThread);
       
   287     AssertTrueSecL(err == KErrNone, KCreateThreadErr, err);
       
   288 
       
   289     // Start threads and wait until either of them exits.
       
   290     TRequestStatus importThreadStatus, killerThreadStatus;
       
   291     importThread.Logon(importThreadStatus);
       
   292     importThread.Resume();
       
   293     killerThread.Logon(killerThreadStatus);
       
   294     killerThread.Resume();
       
   295     User::WaitForRequest(importThreadStatus, killerThreadStatus);
       
   296 
       
   297     // Kill the thread that might still be alive
       
   298     TExitType importThreadExitType = importThread.ExitType();
       
   299     TExitCategoryName importThreadExitCategory = importThread.ExitCategory();
       
   300     TInt importThreadExitReason = importThread.ExitReason();
       
   301     if (importThreadExitType == EExitPending)
       
   302         {
       
   303         importThread.Kill(KErrCancel);
       
   304         User::WaitForRequest(importThreadStatus);
       
   305         }
       
   306     TExitType killerThreadExitType = killerThread.ExitType();
       
   307     TExitCategoryName killerThreadExitCategory = killerThread.ExitCategory();
       
   308     TInt killerThreadExitReason = killerThread.ExitReason();
       
   309     if (killerThreadExitType == EExitPending)
       
   310         {
       
   311         killerThread.Kill(KErrCancel);
       
   312         User::WaitForRequest(killerThreadStatus);
       
   313         }
       
   314     
       
   315     // Log threads' exit results
       
   316     TBuf<100> buf;
       
   317     buf.Format(
       
   318         _L("ImportThread ended with exit type: %d, exit reason: %d, exit category: %S"), 
       
   319         importThreadExitType, importThreadExitReason, &importThreadExitCategory);
       
   320     iLog->Put(buf);
       
   321     buf.Format(
       
   322         _L("KillerThread ended with exit type: %d, exit reason: %d, exit category: %S"), 
       
   323         killerThreadExitType, killerThreadExitReason, &killerThreadExitCategory);
       
   324     iLog->Put(buf);
       
   325 
       
   326     // Verify thread exit results
       
   327     TBool error = VerifyThreadExitResults(killerThreadExitType, importThreadExitType, 
       
   328         importThreadExitCategory, importThreadExitReason);
       
   329     if (error)
       
   330         {
       
   331         User::Leave(KErrGeneral);
       
   332         }
       
   333 
       
   334     CleanupStack::PopAndDestroy(2, &importThread);
       
   335     }
       
   336 
       
   337 // ---------------------------------------------------------
       
   338 // CPosTp58::VerifyThreadExitResults
       
   339 //
       
   340 // (other items were commented in a header).
       
   341 // ---------------------------------------------------------
       
   342 //
       
   343 TBool CPosTp58::VerifyThreadExitResults(
       
   344     TExitType aKillerThreadExitType,
       
   345     TExitType aImportThreadExitType,
       
   346     const TExitCategoryName& aImportThreadExitCategory,
       
   347     TInt aImportThreadExitReason)
       
   348     {
       
   349     TBool error = EFalse;
       
   350 
       
   351     if (aImportThreadExitType != EExitPanic)
       
   352         {
       
   353         _LIT(KExitTypeErr, "Import thread was not panicked as expected.");
       
   354         iLog->PutError(KExitTypeErr);
       
   355         error = ETrue;
       
   356         }
       
   357     if (aImportThreadExitCategory != KTp58Panic)
       
   358         {
       
   359         _LIT(KPanicErr, "Import thread had unexpected panic category");
       
   360         iLog->PutError(KPanicErr);
       
   361         error = ETrue;
       
   362         }
       
   363     if (aImportThreadExitReason != KTp58PanicNumber)
       
   364         {
       
   365         _LIT(KPanicCodeErr, "Import thread had unexpected panic number");
       
   366         iLog->PutError(KPanicCodeErr);
       
   367         error = ETrue;
       
   368         }
       
   369     if (aKillerThreadExitType != EExitPending)
       
   370         {
       
   371         _LIT(KExitTypeErr, "Killer thread was not alive as expected.");
       
   372         iLog->PutError(KExitTypeErr);
       
   373         error = ETrue;
       
   374         }
       
   375 
       
   376     return error;
       
   377     }
       
   378 
       
   379 //  End of File