searcher/tsrc/cpixsearchertest/src/cpixsearchertester.cpp
changeset 0 671dee74050a
child 1 6f2c1c46032b
equal deleted inserted replaced
-1:000000000000 0:671dee74050a
       
     1 /*
       
     2 * Copyright (c) 2010 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 *
       
    16 */
       
    17 #include "cpixsearchertester.h"
       
    18 //#include "TestDriver.h"
       
    19 //#include "Logger.h"
       
    20 #include <f32file.h>
       
    21 #include "indevicecfg.h"
       
    22 
       
    23 #include "CCPixSearcher.h"
       
    24 #include "CSearchDocument.h"
       
    25 #include "CCPixIndexer.h"
       
    26 #include "common.h"
       
    27 
       
    28 #define TS_ASSERT(a) \
       
    29     if ( !a ) return KErrNotFound 
       
    30 
       
    31 _LIT(KQueryString, "act");
       
    32 
       
    33 _LIT(KCpixRegFile, "c:\\private\\2001f6f7\\cpixreg.txt");
       
    34 
       
    35 _LIT(KTestFileBaseAppClassC, "@c:test root file");
       
    36 
       
    37 _LIT(KAppClassField, CPIX_APPCLASS_FIELD);
       
    38 _LIT(KNonExistentField, "_foo");
       
    39 _LIT(KRootFileUserTxt, TEXTAPPCLASS);
       
    40 
       
    41 _LIT8(KRootMapsGbr8, "@GBR:foo bar root maps");
       
    42 _LIT8(KRootMapsGbrPath8, "c:\\Data\\foo\\bar\\root\\maps\\street\\gbr");
       
    43 _LIT(KRootMapsGbr, "@GBR:foo bar root maps");
       
    44 _LIT(KRootMapsGbrPath, "c:\\Data\\foo\\bar\\root\\maps\\street\\gbr");
       
    45 
       
    46 _LIT(KInvalidQualifiedBaseAppClass1, "@:foo bar root maps");
       
    47 _LIT(KInvalidQualifiedBaseAppClass2, ":foo bar root maps");
       
    48 _LIT(KInvalidQualifiedBaseAppClass3, "foo bar root maps");
       
    49 _LIT(KInvalidQualifiedBaseAppClass4, "root file");
       
    50 
       
    51 
       
    52 const TInt KMaxFileSize = 400;
       
    53 
       
    54 TInt CTestSearcher::SearchForTextL(const TDesC& aQueryString, const TDesC& aDefaultField)
       
    55     {
       
    56     TInt documentCount(KErrNotFound);
       
    57     // Send for indexing
       
    58     if ( iSearcher )
       
    59         {
       
    60         documentCount = iSearcher->SearchL(aQueryString, aDefaultField);
       
    61         }
       
    62     return documentCount;
       
    63     }
       
    64 
       
    65 void CTestSearcher::AddFileToIndexL(const TDesC& aFileName)
       
    66     {
       
    67     CSearchDocument* doc = CSearchDocument::NewLC(aFileName, KNullDesC, KNullDesC, CSearchDocument::EFileParser);   
       
    68     iIndexer->AddL(*doc);
       
    69     CleanupStack::PopAndDestroy(doc);
       
    70     }
       
    71 
       
    72 void CTestSearcher::setUp()
       
    73     {
       
    74     iFs.Connect();
       
    75     User::LeaveIfError(iSession.Connect());
       
    76 
       
    77     
       
    78     iSession.UnDefineVolume(KTestFileBaseAppClassC);
       
    79     // define a test volume
       
    80     iSession.DefineVolume(KTestFileBaseAppClassC, KNullDesC);
       
    81 
       
    82     iIndexer = CCPixIndexer::NewL(iSession);
       
    83     iIndexer->OpenDatabaseL(KTestFileBaseAppClassC);
       
    84     iIndexer->ResetL();
       
    85     
       
    86     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\act0.txt")); 
       
    87     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\act1.txt")); 
       
    88     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\act2.txt")); 
       
    89     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\act3.txt")); 
       
    90     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\act4.txt")); 
       
    91     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\act5.txt")); 
       
    92     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\act6.txt")); 
       
    93     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\complete.txt")); 
       
    94     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\index.txt"));    
       
    95     AddFileToIndexL(_L("c:\\Data\\cpixS60unittest\\introduction.txt")); 
       
    96 
       
    97     iSearcher = CCPixSearcher::NewL(iSession);
       
    98     iSearcher->OpenDatabaseL(KTestFileBaseAppClassC);
       
    99 
       
   100     // iWait will cause waiting until some asynchronous event has happened
       
   101     iWait = new (ELeave) CActiveSchedulerWait;
       
   102     iMyAOClass = CAOTestClass::NewL(this);
       
   103     iDocumentCount = 0;
       
   104     iDocument = NULL;
       
   105     iHandleDocumentLFunctionCalled = EFalse;
       
   106     iHandleSetAnalyzerCalled = EFalse;
       
   107     }
       
   108 
       
   109 void CTestSearcher::tearDown()
       
   110     {
       
   111     iDocumentCount = 0;
       
   112 
       
   113     delete iSearcher;
       
   114     iSearcher = NULL;
       
   115 
       
   116     if ( iDocument )
       
   117         {
       
   118         delete iDocument;
       
   119         iDocument = NULL;
       
   120         }
       
   121 
       
   122     if (iIndexer)
       
   123         {
       
   124         iIndexer->ResetL();
       
   125         }
       
   126 
       
   127     if( iWait )
       
   128         {
       
   129         delete iWait;
       
   130         iWait = NULL;
       
   131         }
       
   132     
       
   133     if( iMyAOClass )
       
   134         {
       
   135         delete iMyAOClass;
       
   136         iMyAOClass = NULL;
       
   137         }
       
   138 
       
   139     delete iIndexer;
       
   140     iIndexer = NULL;
       
   141 
       
   142     // undefine a test volume
       
   143     iSession.UnDefineVolume(KTestFileBaseAppClassC);
       
   144 
       
   145     iSession.Close();
       
   146     iFs.Close();
       
   147     }
       
   148 
       
   149 
       
   150 void CTestSearcher::HandleOpenDatabaseResultL( TInt /* aError */ )
       
   151     {
       
   152     if (iWait && iWait->IsStarted())
       
   153         {
       
   154         // All done, signal that can continue now.
       
   155         iWait->AsyncStop();
       
   156         }
       
   157     }
       
   158 
       
   159 void CTestSearcher::HandleSearchResultsL(TInt /* aError */, TInt aEstimatedResultCount)
       
   160     {
       
   161     iDocumentCount = aEstimatedResultCount;
       
   162     
       
   163     if (iWait && iWait->IsStarted())
       
   164         {
       
   165         // All done, signal that can continue now.
       
   166         iWait->AsyncStop();
       
   167         }
       
   168     }
       
   169 
       
   170 void CTestSearcher::HandleSetAnalyzerResultL(TInt /*aError*/)
       
   171     {
       
   172     iHandleSetAnalyzerCalled = ETrue;
       
   173     if (iWait && iWait->IsStarted())
       
   174         {
       
   175         // All done, signal that can continue now.
       
   176         iWait->AsyncStop();
       
   177         }
       
   178     }
       
   179 
       
   180 
       
   181 void CTestSearcher::HandleDocumentL(TInt /* aError */, CSearchDocument* aDocument)
       
   182     {
       
   183     iHandleDocumentLFunctionCalled = ETrue;
       
   184     if (iWait && iWait->IsStarted())
       
   185         {
       
   186         if ( iDocument )
       
   187             {
       
   188             delete iDocument;
       
   189             iDocument = NULL;
       
   190             }
       
   191 
       
   192         iDocument = aDocument;
       
   193         // All done, signal that can continue now.
       
   194         iWait->AsyncStop();
       
   195         }
       
   196     }
       
   197 
       
   198 // Timeout callback
       
   199 void CTestSearcher::CallCompleted( int /* i */ )
       
   200     {
       
   201     if (iWait && iWait->IsStarted())
       
   202         {
       
   203         iWait->AsyncStop();
       
   204         }
       
   205     }
       
   206 
       
   207 TInt CTestSearcher::testKnownTermTestsL()
       
   208     {
       
   209     // Shakespeare's corpus files have plenty of terms "act" in them EXPECTED RESULTS > 0
       
   210     TInt result = SearchForTextL(KQueryString, KNullDesC);
       
   211     TS_ASSERT(result > 0);
       
   212     
       
   213     // Shakespeare's corpus files have plenty of terms "scene" in them EXPECTED RESULTS > 0
       
   214     result = SearchForTextL(_L("scene"), KNullDesC);
       
   215     TS_ASSERT(result > 0);
       
   216 
       
   217     if(result > 0)
       
   218         {
       
   219         for (TInt i=0; i<result; i++)
       
   220             {
       
   221             CSearchDocument* document = iSearcher->GetDocumentL(i);     
       
   222             //TS_ASSERT(document != NULL);
       
   223             delete document;
       
   224             }
       
   225         }
       
   226         
       
   227     
       
   228     // Shakespeare's corpus files have no terms "ipod" in them EXPECTED RESULTS IS ZERO
       
   229     result = SearchForTextL(_L("ipod"), KNullDesC);
       
   230     TS_ASSERT(result == 0);
       
   231 
       
   232     // Shakespeare's corpus files have plenty of terms "act" in them EXPECTED RESULTS == 0, but not in this field
       
   233     result = SearchForTextL(KQueryString, KNonExistentField);
       
   234     TS_ASSERT(result == 0);
       
   235 
       
   236     // can search for a known value in a different field
       
   237     result = SearchForTextL(KRootFileUserTxt, KAppClassField);
       
   238     TS_ASSERT(result > 0);
       
   239 
       
   240     // can search for a known value in a different field
       
   241 //  result = SearchForTextL(KRootFileUserTxt, KNonExistentField);
       
   242 //  TS_ASSERT(result == 0); // TODO XXX TIM why does searching for an existing term in a non-existent field return a non-zero value
       
   243     return KErrNone;
       
   244     }
       
   245 
       
   246 void CTestSearcher::testWildcardTermTests()
       
   247     {
       
   248     // Shakespeare's corpus files have plenty of terms "act" in them EXPECTED RESULTS > 0
       
   249     TInt result = SearchForTextL(_L("a*t"), KNullDesC);
       
   250     //TS_ASSERT(result > 0);
       
   251 
       
   252     // Shakespeare's corpus files have plenty of terms "act" in them EXPECTED RESULTS > 0
       
   253     result = SearchForTextL(_L("sc*ne"), KNullDesC);
       
   254    // TS_ASSERT(result > 0);
       
   255     }
       
   256 
       
   257 void CTestSearcher::testDefineVolumeWorksNonEmptyPath()
       
   258     {
       
   259     TBuf8<KMaxFileSize> cpixRegBuf;
       
   260 
       
   261     // open up the cpixreg.txt file again and make sure 
       
   262     // that it IS NOT in there
       
   263     TInt err = iFile.Open(iFs, KCpixRegFile, EFileStreamText);
       
   264     iFile.Read(0, cpixRegBuf);
       
   265     err = cpixRegBuf.Find(KRootMapsGbr8());
       
   266    // TS_ASSERT(KErrNotFound == err);
       
   267     err = cpixRegBuf.Find(KRootMapsGbrPath8());
       
   268    // TS_ASSERT(KErrNotFound == err);
       
   269     iFile.Close();
       
   270 
       
   271     // define a new volume
       
   272     err = iSession.DefineVolume(KRootMapsGbr, KRootMapsGbrPath);
       
   273    // TS_ASSERT(err == KErrNone);
       
   274 
       
   275     // open up the cpixreg.txt file and make sure that it IS in there
       
   276     err = iFile.Open(iFs, KCpixRegFile, EFileStreamText);
       
   277     iFile.Read(0, cpixRegBuf);
       
   278     err = cpixRegBuf.Find(KRootMapsGbr8());
       
   279    // TS_ASSERT(KErrNotFound != err);
       
   280     err = cpixRegBuf.Find(KRootMapsGbrPath8());
       
   281    // TS_ASSERT(KErrNotFound != err);
       
   282     iFile.Close();
       
   283 
       
   284     // now undefine the same volume
       
   285     err = iSession.UnDefineVolume(KRootMapsGbr);
       
   286    // TS_ASSERT(err == KErrNone);
       
   287 
       
   288     // open up the cpixreg.txt file again and make sure 
       
   289     // that it IS NOT in there
       
   290     err = iFile.Open(iFs, KCpixRegFile, EFileStreamText);
       
   291     iFile.Read(0, cpixRegBuf);
       
   292     err = cpixRegBuf.Find(KRootMapsGbr8());
       
   293    // TS_ASSERT(KErrNotFound == err);
       
   294     err = cpixRegBuf.Find(KRootMapsGbrPath8());
       
   295    // TS_ASSERT(KErrNotFound == err);
       
   296     iFile.Close();
       
   297     }
       
   298 
       
   299 void CTestSearcher::testDefineVolumeWorksWithEmptyPath()
       
   300     {
       
   301     TBuf8<KMaxFileSize> cpixRegBuf;
       
   302 
       
   303     // open up the cpixreg.txt file again and make sure 
       
   304     // that it IS NOT in there
       
   305     TInt err = iFile.Open(iFs, KCpixRegFile, EFileStreamText);
       
   306     iFile.Read(0, cpixRegBuf);
       
   307     err = cpixRegBuf.Find(KRootMapsGbr8());
       
   308   //  TS_ASSERT(KErrNotFound == err);
       
   309     err = cpixRegBuf.Find(KRootMapsGbrPath8());
       
   310   //  TS_ASSERT(KErrNotFound == err);
       
   311     iFile.Close();
       
   312 
       
   313     // define a new volume
       
   314     err = iSession.DefineVolume(KRootMapsGbr, KNullDesC);
       
   315   //  TS_ASSERT(err == KErrNone);
       
   316 
       
   317     // open up the cpixreg.txt file and make sure that it IS in there
       
   318     err = iFile.Open(iFs, KCpixRegFile, EFileStreamText);
       
   319     iFile.Read(0, cpixRegBuf);
       
   320     err = cpixRegBuf.Find(KRootMapsGbr8());
       
   321   //  TS_ASSERT(KErrNotFound != err);
       
   322     iFile.Close();
       
   323 
       
   324     // now try to redefine the same volume again
       
   325     err = iSession.DefineVolume(KRootMapsGbr, KNullDesC);
       
   326   //  TS_ASSERT(err == KErrNone);
       
   327 
       
   328     // now undefine the same volume
       
   329     err = iSession.UnDefineVolume(KRootMapsGbr);
       
   330   //  TS_ASSERT(err == KErrNone);
       
   331 
       
   332     // open up the cpixreg.txt file again and make sure 
       
   333     // that it IS NOT in there
       
   334     err = iFile.Open(iFs, KCpixRegFile, EFileStreamText);
       
   335     iFile.Read(0, cpixRegBuf);
       
   336     err = cpixRegBuf.Find(KRootMapsGbr8());
       
   337   //  TS_ASSERT(KErrNotFound == err);
       
   338     iFile.Close();
       
   339     }
       
   340 
       
   341 void CTestSearcher::testDefineVolumeErrorScenarios()
       
   342     {
       
   343     // Attempt to define a volume for a invalid qualified base app classes.
       
   344     TInt result = iSession.DefineVolume(KInvalidQualifiedBaseAppClass1, KNullDesC);
       
   345  //   TS_ASSERT(result == KErrCannotDefineVolume);
       
   346 
       
   347     result = iSession.DefineVolume(KInvalidQualifiedBaseAppClass2, KNullDesC);
       
   348  //   TS_ASSERT(result == KErrCannotDefineVolume);
       
   349 
       
   350     result = iSession.DefineVolume(KInvalidQualifiedBaseAppClass3, KNullDesC);
       
   351   //  TS_ASSERT(result == KErrCannotDefineVolume);
       
   352 
       
   353     result = iSession.DefineVolume(KInvalidQualifiedBaseAppClass4, KNullDesC);
       
   354   //  TS_ASSERT(result == KErrCannotDefineVolume);
       
   355 
       
   356     // Attempt to undefine a volume that was not defined.
       
   357     result = iSession.UnDefineVolume(KRootMapsGbr);
       
   358   //  TS_ASSERT(result == KErrNone);
       
   359     }
       
   360 
       
   361 void CTestSearcher::testSearchCancellation()
       
   362     {
       
   363     // Test basic cancellation of search request
       
   364     iSearcher->SearchL(*this, KQueryString);
       
   365     iSearcher->Cancel();
       
   366 
       
   367     // If want to set a maximum timeout value for this test case then
       
   368     // uncomment this line, which will cause CTestSearcher::CallCompleted
       
   369     // to be triggered.
       
   370     iMyAOClass->StartL(1000000*20); //Async call: Maximum TimeOut time 20 seconds
       
   371 
       
   372     // Wait until either HandleSearchResultsL or Timeout exceeded
       
   373     iWait->Start();
       
   374    // TS_ASSERT(iDocumentCount == 0);
       
   375 
       
   376     // Now make sure that subsequent searches work correctly
       
   377     iSearcher->SearchL(*this, KQueryString);
       
   378     // Wait until HandleSearchResultsL completes
       
   379     iWait->Start();
       
   380 
       
   381    // TS_ASSERT(iDocumentCount > 0);
       
   382     }
       
   383 
       
   384 void CTestSearcher::testSearchAsynchronous()
       
   385     {
       
   386     iSearcher->SearchL(*this, KQueryString);
       
   387     // Wait until HandleSearchResultsL completes
       
   388     iWait->Start();
       
   389 
       
   390   //  TS_ASSERT(iDocumentCount > 0);
       
   391     }
       
   392 
       
   393 void CTestSearcher::testSearchLeavesIfNotCancelled()
       
   394     {
       
   395     // Perform first search
       
   396     TRAPD(err, iSearcher->SearchL(*this, KQueryString) );
       
   397    // TS_ASSERT(iDocumentCount == 0);
       
   398    // TS_ASSERT(err == KErrNone);
       
   399 
       
   400     // before ::HandleSearchResults has been called, 
       
   401     // initiate a second search. 
       
   402     TRAP(err, iSearcher->SearchL(*this, KQueryString));
       
   403   //  TS_ASSERT(err == KErrInUse);
       
   404 
       
   405     // Wait until HandleSearchResultsL completes
       
   406     iWait->Start();
       
   407 
       
   408    // TS_ASSERT(iDocumentCount > 0);
       
   409     }
       
   410 
       
   411 void CTestSearcher::testOpenIndexDb()
       
   412     {
       
   413     // Make sure cannot open an invalid index db
       
   414     _LIT(KInvalidQualifiedBaseAppClass, "@x:test invalid");
       
   415     CCPixSearcher* searcher = CCPixSearcher::NewLC(iSession);
       
   416     TRAPD(err, searcher->OpenDatabaseL(KInvalidQualifiedBaseAppClass) );
       
   417    // TS_ASSERT(!searcher->IsDatabaseOpen());
       
   418    // TS_ASSERT(err == KErrCannotOpenDatabase);
       
   419     TRAP(err, searcher->SearchL(KQueryString));
       
   420    // TS_ASSERT(err == KErrNotReady);
       
   421     CleanupStack::PopAndDestroy(searcher);
       
   422 
       
   423     searcher = NULL; 
       
   424 
       
   425     // check that same searcher object can open a second indexDb
       
   426     searcher = CCPixSearcher::NewLC(iSession);
       
   427     TRAP(err, searcher->OpenDatabaseL(_L("root")));
       
   428   //  TS_ASSERT(searcher->IsDatabaseOpen());
       
   429     searcher->SearchL(KQueryString);
       
   430   //  TS_ASSERT(err == KErrNone);
       
   431 
       
   432     TRAP(err, searcher->OpenDatabaseL(KTestFileBaseAppClassC));
       
   433    // TS_ASSERT(err == KErrNone);
       
   434    // TS_ASSERT(searcher->IsDatabaseOpen());
       
   435     const TInt docCount = searcher->SearchL(KQueryString);
       
   436   //  TS_ASSERT(docCount > 0);
       
   437     CleanupStack::PopAndDestroy(searcher);
       
   438 
       
   439     searcher = NULL; 
       
   440 
       
   441     // check that asynchronous open works as expected
       
   442     searcher = CCPixSearcher::NewLC(iSession);
       
   443     TRAP(err, searcher->OpenDatabaseL(*this, _L("root")));
       
   444 
       
   445     // If want to set a maximum timeout value for this test case then
       
   446     // uncomment this line, which will cause CTestSearcher::CallCompleted
       
   447     // to be triggered.
       
   448     iMyAOClass->StartL(1000000*10); //Async call: Maximum TimeOut time 10 seconds
       
   449 
       
   450     iWait->Start();
       
   451   //  TS_ASSERT(searcher->IsDatabaseOpen());
       
   452     CleanupStack::PopAndDestroy(searcher);
       
   453     }
       
   454 
       
   455 void CTestSearcher::testDeleteIndexDbWhileSearching()
       
   456     {
       
   457     CCPixSearcher* searcher = CCPixSearcher::NewLC(iSession);
       
   458     TRAPD(err, searcher->OpenDatabaseL(KTestFileBaseAppClassC) );
       
   459    // TS_ASSERT(searcher->IsDatabaseOpen());
       
   460   //  TS_ASSERT(searcher->GetBaseAppClass() != KNullDesC);
       
   461     //Set Standard Analyzer to improve code coverage of search
       
   462     searcher->SetAnalyzerL(*this,_L( "" CPIX_ANALYZER_STANDARD ));
       
   463     iWait->Start(); //Start Wait AO
       
   464    // TS_ASSERT(iHandleSetAnalyzerCalled);
       
   465     TRAP(err, searcher->SearchL(*this, KQueryString));
       
   466     // don't wait for the search results.
       
   467     CleanupStack::PopAndDestroy(searcher);
       
   468     searcher = NULL; 
       
   469     // is there a panic???
       
   470     }
       
   471 
       
   472 void CTestSearcher::testGetDocumentAsync()
       
   473     {
       
   474     const TInt result = SearchForTextL(KQueryString, KNullDesC);
       
   475   //  TS_ASSERT(result > 0);
       
   476 
       
   477     TInt loopCount(0);
       
   478 
       
   479     for (loopCount=0; loopCount<result; loopCount++)
       
   480         {
       
   481         iSearcher->GetDocumentL(loopCount, *this);
       
   482         iWait->Start();
       
   483    //     TS_ASSERT(iDocument != NULL);
       
   484         delete iDocument;
       
   485         iDocument = NULL;
       
   486         }
       
   487 
       
   488   //  TS_ASSERT(loopCount == result);
       
   489     }
       
   490 
       
   491 void CTestSearcher::testGetInvalidDocumentAsync()
       
   492     {
       
   493     const TInt result = SearchForTextL(KQueryString, KNullDesC);
       
   494   //  TS_ASSERT(result > 0);
       
   495 
       
   496     // request a document that does not exist. 
       
   497     TRAPD(err, iSearcher->GetDocumentL(result+1, *this));
       
   498     // above call is not exepected to leave
       
   499  //   TS_ASSERT(err == KErrNone);
       
   500 
       
   501    iMyAOClass->StartL(1000000*10); //Async call: Maximum TimeOut time 10 seconds
       
   502 
       
   503     iWait->Start();
       
   504 
       
   505     // Either the iMyAOClass Timeout executed or the the HandleDocumentL
       
   506     // function. If the HandleDocumentL function was executed, then
       
   507     // this assert will be true.
       
   508   //  TS_ASSERT(iHandleDocumentLFunctionCalled);
       
   509     // as requested an invalid document, this must be NULL
       
   510   //  TS_ASSERT(iDocument == NULL);
       
   511     }