diff --git a/akonadi/tests/CMakeLists.txt b/akonadi/tests/CMakeLists.txt index 35d4c6343..d857f09f6 100644 --- a/akonadi/tests/CMakeLists.txt +++ b/akonadi/tests/CMakeLists.txt @@ -1,117 +1,118 @@ if(${EXECUTABLE_OUTPUT_PATH}) set( PREVIOUS_EXEC_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH} ) else(${EXECUTABLE_OUTPUT_PATH}) set( PREVIOUS_EXEC_OUTPUT_PATH . ) endif(${EXECUTABLE_OUTPUT_PATH}) set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} ) set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}" ) include_directories( ${CMAKE_SOURCE_DIR}/akonadi ${CMAKE_CURRENT_SOURCE_DIR}/../ ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/../ ${Boost_INCLUDE_DIR} ${AKONADI_INCLUDE_DIR} ) # add testrunner (application for managing a self-contained test # environment) add_subdirectory(testrunner) # add benchmarker add_subdirectory(benchmarker) # convenience macro to add akonadi demo application macro(add_akonadi_demo _source) set(_test ${_source}) get_filename_component(_name ${_source} NAME_WE) kde4_add_executable(${_name} TEST ${_test}) target_link_libraries(${_name} akonadi-kde akonadi-kmime ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS}) endmacro(add_akonadi_demo) # convenience macro to add akonadi qtestlib unit-tests macro(add_akonadi_test _source) set(_test ${_source}) get_filename_component(_name ${_source} NAME_WE) kde4_add_unit_test(${_name} TESTNAME akonadi-${_name} ${_test}) target_link_libraries(${_name} akonadi-kde akonadi-kmime ${QT_QTTEST_LIBRARY} ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${KDE4_KDECORE_LIBS} ${AKONADI_COMMON_LIBRARIES}) endmacro(add_akonadi_test) # convenience macro to add akonadi testrunner unit-tests macro(add_akonadi_isolated_test _source) set(_test ${_source}) get_filename_component(_name ${_source} NAME_WE) kde4_add_executable(${_name} TEST ${_test}) target_link_libraries(${_name} akonadi-kde akonadi-kmime ${QT_QTTEST_LIBRARY} ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${KDE4_KDECORE_LIBS} ${AKONADI_COMMON_LIBRARIES}) # based on kde4_add_unit_test if (WIN32) get_target_property( _loc ${_name} LOCATION ) set(_executable ${_loc}.bat) set(_testrunner ${PREVIOUS_EXEC_OUTPUT_PATH}/akonaditest.bat) else (WIN32) set(_executable ${EXECUTABLE_OUTPUT_PATH}/${_name}) set(_testrunner ${PREVIOUS_EXEC_OUTPUT_PATH}/akonaditest) endif (WIN32) if (UNIX) set(_executable ${_executable}.shell) set(_testrunner ${_testrunner}.shell) endif (UNIX) add_test( akonadi-db-${_name} ${_testrunner} -c ${CMAKE_CURRENT_SOURCE_DIR}/unittestenv/config-db.xml ${_executable} ) add_test( akonadi-fs-${_name} ${_testrunner} -c ${CMAKE_CURRENT_SOURCE_DIR}/unittestenv/config-fs.xml ${_executable} ) #add_test( akonadi-sqlite-${_name} ${_testrunner} -c ${CMAKE_CURRENT_SOURCE_DIR}/unittestenv/config-sqlite.xml ${_executable} ) endmacro(add_akonadi_isolated_test) # demo applications add_akonadi_demo(itemdumper.cpp) add_akonadi_demo(subscriber.cpp) add_akonadi_demo(headfetcher.cpp) add_akonadi_demo(agentinstancewidgettest.cpp) add_akonadi_demo(agenttypewidgettest.cpp) add_akonadi_demo(pluginloadertest.cpp) add_akonadi_demo(selftester.cpp) kde4_add_executable( akonadi-firstrun TEST ../firstrun.cpp firstrunner.cpp ) target_link_libraries( akonadi-firstrun akonadi-kde ${KDE4_KDEUI_LIBS} ) # qtestlib unit tests add_akonadi_test(imapparsertest.cpp) add_akonadi_test(imapsettest.cpp) add_akonadi_test(itemhydratest.cpp) add_akonadi_test(itemtest.cpp) add_akonadi_test(itemserializertest.cpp) add_akonadi_test(mimetypecheckertest.cpp) add_akonadi_test(protocolhelpertest.cpp) # testrunner tests add_akonadi_isolated_test(testenvironmenttest.cpp) add_akonadi_isolated_test(autoincrementtest.cpp) add_akonadi_isolated_test(attributefactorytest.cpp) add_akonadi_isolated_test(collectionjobtest.cpp) add_akonadi_isolated_test(collectionpathresolvertest.cpp) add_akonadi_isolated_test(collectionattributetest.cpp) add_akonadi_isolated_test(itemfetchtest.cpp) add_akonadi_isolated_test(itemappendtest.cpp) add_akonadi_isolated_test(itemstoretest.cpp) add_akonadi_isolated_test(itemdeletetest.cpp) add_akonadi_isolated_test(monitortest.cpp) add_akonadi_isolated_test(searchjobtest.cpp) add_akonadi_isolated_test(changerecordertest.cpp) add_akonadi_isolated_test(resourcetest.cpp) add_akonadi_isolated_test(subscriptiontest.cpp) add_akonadi_isolated_test(transactiontest.cpp) add_akonadi_isolated_test(itemcopytest.cpp) add_akonadi_isolated_test(itemmovetest.cpp) add_akonadi_isolated_test(collectioncopytest.cpp) add_akonadi_isolated_test(collectionmovetest.cpp) +add_akonadi_isolated_test(collectionsynctest.cpp) add_akonadi_isolated_test(itemsynctest.cpp) add_akonadi_isolated_test(linktest.cpp) add_akonadi_isolated_test(cachetest.cpp) add_akonadi_isolated_test(servermanagertest.cpp) add_akonadi_isolated_test(collectioncreator.cpp) add_akonadi_isolated_test(itembenchmark.cpp) diff --git a/akonadi/tests/collectionsynctest.cpp b/akonadi/tests/collectionsynctest.cpp new file mode 100644 index 000000000..ec86fb6e2 --- /dev/null +++ b/akonadi/tests/collectionsynctest.cpp @@ -0,0 +1,197 @@ +/* + Copyright (c) 2009 Volker Krause + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "test_utils.h" + +#include +#include +#include +#include +#include + +#include "../akonadi/collectionsync.cpp" + +#include + +#include +#include + +#include + +using namespace Akonadi; + +Q_DECLARE_METATYPE( KJob* ) + +class CollectionSyncTest : public QObject +{ + Q_OBJECT + private: + Collection::List fetchCollections( const QString &res ) + { + CollectionFetchJob *fetch = new CollectionFetchJob( Collection::root(), CollectionFetchJob::Recursive, this ); + fetch->setResource( res ); + Q_ASSERT( fetch->exec() ); + Q_ASSERT( !fetch->collections().isEmpty() ); + return fetch->collections(); + } + + private slots: + void initTestCase() + { + Control::start(); + qRegisterMetaType(); + + // switch all resources offline to reduce interference from them + foreach ( Akonadi::AgentInstance agent, Akonadi::AgentManager::self()->instances() ) + agent.setIsOnline( false ); + } + + void testFullSync() + { + Collection::List origCols = fetchCollections( "akonadi_knut_resource_0" ); + + CollectionSync* syncer = new CollectionSync( "akonadi_knut_resource_0" ); + syncer->setRemoteCollections( origCols ); + AKVERIFYEXEC( syncer ); + + Collection::List resultCols = fetchCollections( "akonadi_knut_resource_0" ); + QCOMPARE( resultCols.count(), origCols.count() ); + } + + void testFullStreamingSync() + { + Collection::List origCols = fetchCollections( "akonadi_knut_resource_0" ); + + CollectionSync* syncer = new CollectionSync( "akonadi_knut_resource_0" ); + syncer->setAutoDelete( false ); + QSignalSpy spy( syncer, SIGNAL(result(KJob*)) ); + QVERIFY( spy.isValid() ); + // ### streaming not implemented yet +// syncer->setTotalItems( origCols.count() ); + QTest::qWait( 10 ); + QCOMPARE( spy.count(), 0 ); +// + for ( int i = 0; i < origCols.count(); ++i ) { +// Item::List l; +// l << origCols[i]; +// syncer->setFullSyncItems( l ); +// if ( i < origCols.count() - 1 ) +// QTest::qWait( 10 ); // enter the event loop so itemsync actually can do something +// QCOMPARE( spy.count(), 0 ); + } +// QTest::qWait( 1000 ); // let it finish its job +// QCOMPARE( spy.count(), 1 ); +// KJob *job = spy.at( 0 ).at( 0 ).value(); +// QCOMPARE( job, syncer ); +// QCOMPARE( job->error(), 0 ); +// + Collection::List resultCols = fetchCollections( "akonadi_knut_resource_0" ); + QCOMPARE( resultCols.count(), origCols.count() ); + + delete syncer; + } + + void testIncrementalSync() + { + Collection::List origCols = fetchCollections( "akonadi_knut_resource_0" ); + + CollectionSync* syncer = new CollectionSync( "akonadi_knut_resource_0" ); + syncer->setRemoteCollections( origCols, Collection::List() ); + AKVERIFYEXEC( syncer ); + + Collection::List resultCols = fetchCollections( "akonadi_knut_resource_0" ); + QCOMPARE( resultCols.count(), origCols.count() ); + + Collection::List delCols; + delCols << resultCols.front(); + resultCols.pop_front(); + + // ### not implemented yet I guess +#if 0 + Collection colWithOnlyRemoteId; + colWithOnlyRemoteId.setRemoteId( resultCols.front().remoteId() ); + delCols << colWithOnlyRemoteId; + resultCols.pop_front(); +#endif + +#if 0 + // ### should this work? + Collection colWithRandomRemoteId; + colWithRandomRemoteId.setRemoteId( KRandom::randomString( 100 ) ); + delCols << colWithRandomRemoteId; +#endif + + syncer = new CollectionSync( "akonadi_knut_resource_0" ); + syncer->setRemoteCollections( resultCols, delCols ); + AKVERIFYEXEC( syncer ); + + Collection::List resultCols2 = fetchCollections( "akonadi_knut_resource_0" ); + QCOMPARE( resultCols2.count(), resultCols.count() ); + } + + void testIncrementalStreamingSync() + { + Collection::List origCols = fetchCollections( "akonadi_knut_resource_0" ); + + CollectionSync* syncer = new CollectionSync( "akonadi_knut_resource_0" ); + syncer->setAutoDelete( false ); + QSignalSpy spy( syncer, SIGNAL(result(KJob*)) ); + QVERIFY( spy.isValid() ); + // ### not implemented yet +// syncer->setStreamingEnabled( true ); + QTest::qWait( 10 ); + QCOMPARE( spy.count(), 0 ); + + for ( int i = 0; i < origCols.count(); ++i ) { +/* Item::List l; + l << origCols[i]; + syncer->setIncrementalSyncItems( l, Item::List() ); + if ( i < origCols.count() - 1 ) + QTest::qWait( 10 ); // enter the event loop so itemsync actually can do something + QCOMPARE( spy.count(), 0 );*/ + } +// syncer->deliveryDone(); +// QTest::qWait( 1000 ); // let it finish its job +// QCOMPARE( spy.count(), 1 ); +// KJob *job = spy.at( 0 ).at( 0 ).value(); +// QCOMPARE( job, syncer ); +// QCOMPARE( job->error(), 0 ); + + Collection::List resultCols = fetchCollections( "akonadi_knut_resource_0" ); + QCOMPARE( resultCols.count(), origCols.count() ); + + delete syncer; + } + + void testEmptyIncrementalSync() + { + Collection::List origCols = fetchCollections( "akonadi_knut_resource_0" ); + + CollectionSync* syncer = new CollectionSync( "akonadi_knut_resource_0" ); + syncer->setRemoteCollections( Collection::List(), Collection::List() ); + AKVERIFYEXEC( syncer ); + + Collection::List resultCols = fetchCollections( "akonadi_knut_resource_0" ); + QCOMPARE( resultCols.count(), origCols.count() ); + } +}; + +QTEST_AKONADIMAIN( CollectionSyncTest, NoGUI ) + +#include "collectionsynctest.moc"