PDA

View Full Version : Is it possible to use signal, slots and threads in Qtest application?



mismael85
20th November 2010, 17:34
Hello everybody,
I writing my first Unit test application but there are problems when using Signals and slots.
slots never been called. and also is it possible to test a class inherits QThread.
Here are my code sample

TestSearchEngine::TestSearchEngine()
{
m_pSearchEngine = new SearchEngine();
connect(m_pSearchEngine, SIGNAL(resultsDetected(int)), this, SLOT(onSearchResultDetected(int)));

}

void TestSearchEngine::testSearch()
{
QStringList searchWords;
searchWords <<tr("sampleword");
QList<int> booksId;
booksId << 11430;
QBENCHMARK{
m_pSearchEngine->search(searchWords, booksId);}
}

void TestSearchEngine::onSearchResultDetected(int count)
{
QCOMPARE(count, 3075 );
testSearchResult(1);

}

void TestSearchEngine::testSearchResult(int index)
{
QBENCHMARK{
m_pSearchEngine->searchResult(index);}
}
Note that m_pSearchEngine is an object of a class inherits QThread and it starts the thread when calling search function.
Thank you

MarekR22
21st November 2010, 10:26
1. read this: You’re doing it wrong… (http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/)
2. when connections between threads are made, then when signal is emitted event is send to another thread. So code which receives signals from another thread have to process event loop. So every time you are expecting signal to be received you have to call QCoreApplication::processEvents or QTest::qWait(200) (I recommend qWait since it may require to wait for this event).

mismael85
22nd November 2010, 14:31
1. read this: You’re doing it wrong…
I don't think that i use QThread incorrectly and you can see the followinf function

void SearchEngine::search(QStringList aSearchWord, QList<int>aBooksIds)
{
m_searchWord = aSearchWord;
m_booksIds = aBooksIds;
if(!isRunning())
{
start();
}
}
i just use the search function to init some variables and also to start the thread.

2. when connections between threads are made, then when signal is emitted event is send to another thread. So code which receives signals from another thread have to process event loop. So every time you are expecting signal to be received you have to call QCoreApplication::processEvents or QTest::qWait(200) (I recommend qWait since it may require to wait for this event).
i did just as you saied but still the slot never been called and here are the code the used

void TestSearchEngine::testSearch()
{
QStringList searchWords;
searchWords << tr("ظ…ط*ظ…ط¯");
QList<int> booksId;
booksId << 11430;
m_pSearchEngine->search(searchWords, booksId);
QCoreApplication::processEvents();
}

mismael85
21st December 2010, 12:31
is there any body knows how to test QThread class?