qcpix/tsrc/qtcpixunittests/src/ut_qtcpixsearcher.cpp
changeset 15 cf5c74390b98
parent 3 ae3f1779f6da
equal deleted inserted replaced
10:afe194b6b1cd 15:cf5c74390b98
    17 
    17 
    18 #include "ut_qtcpixsearcher.h"
    18 #include "ut_qtcpixsearcher.h"
    19 
    19 
    20 #include <QtCore>
    20 #include <QtCore>
    21 #include <QCoreApplication>
    21 #include <QCoreApplication>
    22 #include <qcpixsearcher.h>
    22 #include <cpixsearcher.h>
    23 #include <qcpixdocument.h>
    23 #include <cpixdocument.h>
    24 #include <QtTest/QtTest>
    24 #include <QtTest/QtTest>
    25 #include "../../QtTestUtil/QtTestUtil.h"
    25 #include "../../QtTestUtil/QtTestUtil.h"
    26 
    26 
    27 //The following data is common to all the functions. So avoid duplication by using the following macros.
    27 //The following data is common to all the functions. So avoid duplication by using the following macros.
    28 #define TEST_SEARCH_DATA \
    28 #define TEST_SEARCH_DATA \
    44     QTest::newRow("root contact") << "root contact";
    44     QTest::newRow("root contact") << "root contact";
    45 
    45 
    46 #define TEST_GET_DATA_FETCH \
    46 #define TEST_GET_DATA_FETCH \
    47     QFETCH(QString, baseAppClass);
    47     QFETCH(QString, baseAppClass);
    48 
    48 
    49 void TestQCPixSearcher::testNewInstance()
    49 void TestCpixSearcher::testNewInstance()
    50     {
    50     {
    51     QCPixSearcher* searcher = QCPixSearcher::newInstance();
    51     CpixSearcher* searcher = CpixSearcher::newInstance();
    52     QVERIFY( searcher != NULL );
    52     QVERIFY( searcher != NULL );
    53     delete searcher;
    53     delete searcher;
    54     }
    54     }
    55 
    55 
    56 void TestQCPixSearcher::init()
    56 void TestCpixSearcher::init()
    57     {
    57     {
    58     iSearcher = QCPixSearcher::newInstance("root", "_aggregate");//default to root searcher.
    58     iSearcher = CpixSearcher::newInstance("root", "_aggregate");//default to root searcher.
    59     QVERIFY( iSearcher!=NULL );
    59     QVERIFY( iSearcher!=NULL );
    60     }
    60     }
    61 
    61 
    62 void TestQCPixSearcher::cleanup()
    62 void TestCpixSearcher::cleanup()
    63     {
    63     {
    64     delete iSearcher;
    64     delete iSearcher;
    65     }
    65     }
    66 
    66 
    67 void TestQCPixSearcher::testSetDatabase_data()
    67 void TestCpixSearcher::testSetDatabase_data()
    68     {
    68     {
    69     TEST_GET_DATA;
    69     TEST_GET_DATA;
    70     }
    70     }
    71 
    71 
    72 void TestQCPixSearcher::testSetDatabase()
    72 void TestCpixSearcher::testSetDatabase()
    73     {
    73     {
    74     TEST_GET_DATA_FETCH;
    74     TEST_GET_DATA_FETCH;
       
    75     Q_ASSERT( iSearcher );
    75     iSearcher->setDatabase( baseAppClass );//no way to verify this :(
    76     iSearcher->setDatabase( baseAppClass );//no way to verify this :(
    76     }
    77     }
    77 
    78 
    78 void TestQCPixSearcher::HandleDatabaseSet(int aError)
    79 void TestCpixSearcher::HandleDatabaseSet(int aError)
    79     {
    80     {
    80     iHandleSetDatabaseComplete = true;
    81     iHandleSetDatabaseComplete = true;
    81     iError = aError;
    82     iError = aError;
    82     }
    83     }
    83 
    84 
    84 void TestQCPixSearcher::testSetDatabaseAsync_data()
    85 void TestCpixSearcher::testSetDatabaseAsync_data()
    85     {
    86     {
    86     TEST_GET_DATA;
    87     TEST_GET_DATA;
    87     }
    88     }
    88 
    89 
    89 void TestQCPixSearcher::testSetDatabaseAsync()
    90 void TestCpixSearcher::testSetDatabaseAsync()
    90     {
    91     {
    91     TEST_GET_DATA_FETCH;
    92     TEST_GET_DATA_FETCH;
    92     
    93     Q_ASSERT( iSearcher );
    93     iHandleSetDatabaseComplete = false;
    94     iHandleSetDatabaseComplete = false;
    94     iError = -2 /*General: Something non-specific*/;
    95     iError = -2 /*General: Something non-specific*/;
    95     connect( iSearcher, SIGNAL(handleDatabaseSet(int)), this, SLOT(HandleDatabaseSet(int)) );
    96     connect( iSearcher, SIGNAL(handleDatabaseSet(int)), this, SLOT(HandleDatabaseSet(int)) );
    96     iSearcher->setDatabaseAsync( baseAppClass );
    97     iSearcher->setDatabaseAsync( baseAppClass );
    97     while( !iHandleSetDatabaseComplete ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents );
    98     while( !iHandleSetDatabaseComplete ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents );
    98     QVERIFY( iError == 0/*No error*/ );
    99     QVERIFY( iError == 0/*No error*/ );
    99     }
   100     }
   100 
   101 
   101 void TestQCPixSearcher::testSearch_data()
   102 void TestCpixSearcher::testSearch_data()
   102     {
   103     {
   103     TEST_SEARCH_DATA;
   104     TEST_SEARCH_DATA;
   104     }
   105     }
   105 
   106 
   106 void TestQCPixSearcher::testSearch()
   107 void TestCpixSearcher::testSearch()
   107     {
   108     {
   108     TEST_SEARCH_DATA_FETCH;
   109     TEST_SEARCH_DATA_FETCH;
       
   110     Q_ASSERT( iSearcher );
   109     QVERIFY(  iSearcher->search( searchString ) == estimatedResultCount );
   111     QVERIFY(  iSearcher->search( searchString ) == estimatedResultCount );
   110     }
   112     }
   111 
   113 
   112 void TestQCPixSearcher::HandleSearchResults(int aError, int aEstimatedCount)
   114 void TestCpixSearcher::HandleSearchResults(int aError, int aEstimatedCount)
   113     {
   115     {
   114     if( aError == 0/*None*/ ) iHandleSearchResultsComplete = aEstimatedCount;
   116     if( aError == 0/*None*/ ) iHandleSearchResultsComplete = aEstimatedCount;
   115     else iHandleSearchResultsComplete = 0;
   117     else iHandleSearchResultsComplete = 0;
   116     }
   118     }
   117 
   119 
   118 void TestQCPixSearcher::testSearchAsync_data()
   120 void TestCpixSearcher::testSearchAsync_data()
   119     {
   121     {
   120     TEST_SEARCH_DATA;
   122     TEST_SEARCH_DATA;
   121     }
   123     }
   122 
   124 
   123 void TestQCPixSearcher::testSearchAsync()
   125 void TestCpixSearcher::testSearchAsync()
   124     {
   126     {
   125     TEST_SEARCH_DATA_FETCH;
   127     TEST_SEARCH_DATA_FETCH;
   126     
   128     Q_ASSERT( iSearcher );
   127     iHandleSearchResultsComplete = -1;
   129     iHandleSearchResultsComplete = -1;
   128     
   130     
   129     //Do Search
   131     //Do Search
   130     connect( iSearcher, SIGNAL(handleSearchResults(int,int)), this, SLOT(HandleSearchResults(int,int)) );
   132     connect( iSearcher, SIGNAL(handleSearchResults(int,int)), this, SLOT(HandleSearchResults(int,int)) );
   131     iSearcher->cancelSearch();
   133     iSearcher->cancelSearch();
   134     //Wait for SearchAsync to complete.
   136     //Wait for SearchAsync to complete.
   135     while( iHandleSearchResultsComplete == -1 ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents );
   137     while( iHandleSearchResultsComplete == -1 ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents );
   136     QVERIFY( iHandleSearchResultsComplete == estimatedResultCount );
   138     QVERIFY( iHandleSearchResultsComplete == estimatedResultCount );
   137     }
   139     }
   138 
   140 
   139 void TestQCPixSearcher::testGetDocument_data()
   141 void TestCpixSearcher::testGetDocument_data()
   140     {
   142     {
   141     TEST_SEARCH_DATA;
   143     TEST_SEARCH_DATA;
   142     }
   144     }
   143 
   145 
   144 void TestQCPixSearcher::testGetDocument()
   146 void TestCpixSearcher::testGetDocument()
   145     {
   147     {
   146     TEST_SEARCH_DATA_FETCH;
   148     TEST_SEARCH_DATA_FETCH;
       
   149     Q_ASSERT( iSearcher );
   147     QVERIFY( iSearcher->search( searchString ) == estimatedResultCount );
   150     QVERIFY( iSearcher->search( searchString ) == estimatedResultCount );
   148 
   151 
   149     //Check only baseAppClass for now. Not sure about the order of docs returned.
   152     //Check only baseAppClass for now. Not sure about the order of docs returned.
   150     //Hence check that we are getting "Robert" from contacts.
   153     //Hence check that we are getting "Robert" from contacts.
   151     if( estimatedResultCount>0 ){
   154     if( estimatedResultCount>0 ){
   152         QCPixDocument* doc = iSearcher->getDocument( 0 );
   155         CpixDocument* doc = iSearcher->document( 0 );
   153         QVERIFY( doc->baseAppClass() == "root contact" );
   156         QVERIFY( doc->baseAppClass() == "root contact" );
   154     }
   157     }
   155     else return;
   158     else return;
   156     }
   159     }
   157 
   160 
   158 void TestQCPixSearcher::HandleDocument(int /*aError*/, QCPixDocument* aDoc)
   161 void TestCpixSearcher::HandleDocument(int /*aError*/, CpixDocument* aDoc)
   159     {
   162     {
   160     iHandleGetDocumentComplete = true;
   163     iHandleGetDocumentComplete = true;
   161     iDoc = aDoc;
   164     iDoc = aDoc;
   162     }
   165     }
   163 
   166 
   164 void TestQCPixSearcher::testGetDocumentAsync_data()
   167 void TestCpixSearcher::testGetDocumentAsync_data()
   165     {
   168     {
   166     TEST_SEARCH_DATA;
   169     TEST_SEARCH_DATA;
   167     }
   170     }
   168 
   171 
   169 void TestQCPixSearcher::testGetDocumentAsync()
   172 void TestCpixSearcher::testGetDocumentAsync()
   170     {
   173     {
   171     TEST_SEARCH_DATA_FETCH;
   174     TEST_SEARCH_DATA_FETCH;
       
   175     Q_ASSERT( iSearcher );
   172     iHandleGetDocumentComplete = false;
   176     iHandleGetDocumentComplete = false;
   173     iHandleSearchResultsComplete = -1;
   177     iHandleSearchResultsComplete = -1;
   174     
   178     
   175     //Do Search
   179     //Do Search
   176     iSearcher->connect( iSearcher, SIGNAL(handleSearchResults(int,int)), this, SLOT(HandleSearchResults(int,int)) );
   180     iSearcher->connect( iSearcher, SIGNAL(handleSearchResults(int,int)), this, SLOT(HandleSearchResults(int,int)) );
   177     iSearcher->searchAsync( searchString );
   181     iSearcher->searchAsync( searchString );
   178     while( iHandleSearchResultsComplete == -1 ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents );
   182     while( iHandleSearchResultsComplete == -1 ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents );
   179     QVERIFY( iHandleSearchResultsComplete == estimatedResultCount );
   183     QVERIFY( iHandleSearchResultsComplete == estimatedResultCount );
   180     
   184     
   181     //Get Document
   185     //Get Document
   182     connect( iSearcher, SIGNAL(handleDocument(int,QCPixDocument*)), this, SLOT(HandleDocument(int,QCPixDocument*)) );
   186     connect( iSearcher, SIGNAL(handleDocument(int,CpixDocument*)), this, SLOT(HandleDocument(int,CpixDocument*)) );
   183     if( iHandleSearchResultsComplete>0 ) 
   187     if( iHandleSearchResultsComplete>0 ) 
   184         iSearcher->getDocumentAsync( 0 );
   188         iSearcher->documentAsync( 0 );
   185     else 
   189     else 
   186         return;
   190         return;
   187     
   191     
   188     //Wait for getDocumentAsync to complete.
   192     //Wait for getDocumentAsync to complete.
   189     while( !iHandleGetDocumentComplete ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents );
   193     while( !iHandleGetDocumentComplete ) QCoreApplication::processEvents( QEventLoop::WaitForMoreEvents );
   190     QVERIFY( iDoc->baseAppClass() == "root contact" );
   194     QVERIFY( iDoc->baseAppClass() == "root contact" );
   191     }
   195     }
   192 
   196 
   193 QTTESTUTIL_REGISTER_TEST( TestQCPixSearcher );
   197 QTTESTUTIL_REGISTER_TEST( TestCpixSearcher );