# Make sure that we can easily bootstrap a private dbus instance.
add_definitions(-DCORE_DBUS_ENABLE_GOOGLE_TEST_FIXTURE -DTP_QT_ENABLE_LOWLEVEL_API -fPIC)

# Enable handy automock'ing and auto include of generated files.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

# Adding the current source dir as it is required when
# compiling the testing framework.
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${DBUS_CPP_INCLUDE_DIRS} ${PROCESS_CPP_INCLUDE_DIRS} ${TP_QT5_INCLUDE_DIRS})

# Beginnings of a testing framework that bundles common tools used
# in testing the messaging framework.
configure_file(test_data.cpp.in test_data.cpp)
add_library(
  testing-framework 
  
  blocking_connector.cpp
  dbus_monitor.cpp
  fork_and_wait.cpp 
  did_finish.cpp
  mission_control.cpp
  qt/tp/client.cpp
  qt/tp/utils.cpp

  ${CMAKE_CURRENT_BINARY_DIR}/test_data.cpp)

macro(MSG_FW_ADD_TEST test_name src)
  execute_process(COMMAND mktemp -d /tmp/${test_name}.XXXXX OUTPUT_VARIABLE TMPDIR)
  string(REPLACE "\n" "" TMPDIR ${TMPDIR})

  if (NOT DEFINED ARG_ENVIRONMENT)
      set(ARG_ENVIRONMENT HOME=${TMPDIR}
                          HISTORY_SQLITE_DBPATH=:memory:
                          MC_ACCOUNT_DIR=${TMPDIR}
                          MC_MANAGER_DIR=${TMPDIR}
                          MC_CLIENTS_DIR=${TMPDIR})
  endif ()

  add_executable(
    ${test_name}
    ${src})

  target_link_libraries(
    ${test_name}

    testing-framework
    testing-mocks
    messaging-framework

    ${GMOCK_LIBRARY}
    ${GTEST_BOTH_LIBRARIES}

    ${DBUS_CPP_LDFLAGS} 
    ${PROCESS_CPP_LDFLAGS} 

    ${TP_QT5_LDFLAGS} 

    Qt5::Test
    ${ARGN})

  add_test(${test_name} ${CMAKE_CURRENT_BINARY_DIR}/${test_name} --gtest_filter=*-*requires*)
  set_tests_properties(${test_name} PROPERTIES ENVIRONMENT "${ARG_ENVIRONMENT}")
 
endmacro(MSG_FW_ADD_TEST)

# Simple shared object that is used in unit-testing the messaging::DynamicLibrary.
add_library(test-library SHARED test_library.cpp)
target_link_libraries(test-library testing-mocks)

MSG_FW_ADD_TEST(associative-dictionary-test associative_dictionary_test.cpp)
MSG_FW_ADD_TEST(boost-variant-test boost_variant_test.cpp)
MSG_FW_ADD_TEST(connection-test connection_test.cpp)
MSG_FW_ADD_TEST(runner-test runner_test.cpp)
MSG_FW_ADD_TEST(connector-factory-test connector_factory_test.cpp)
MSG_FW_ADD_TEST(connector-factory-initializer-test connector_factory_initializer_test.cpp)
MSG_FW_ADD_TEST(dynamic-library-test dynamic_library_test.cpp)
MSG_FW_ADD_TEST(parameter-test parameter_test.cpp)
MSG_FW_ADD_TEST(plugin-connector-test plugin_connector_test.cpp)
MSG_FW_ADD_TEST(qt-runtime-test qt_runtime_test.cpp)
#MSG_FW_ADD_TEST(qt-telepathy-adapter-test qt_telepathy_adapter_test.cpp)
MSG_FW_ADD_TEST(qt-variant-test qt_variant_test.cpp)
MSG_FW_ADD_TEST(qt-variant-facade-test qt_variant_facade_test.cpp)
MSG_FW_ADD_TEST(members-test members_test.cpp)
MSG_FW_ADD_TEST(flags-test flags_test.cpp)
MSG_FW_ADD_TEST(group-manager-test group_manager_test.cpp)

add_subdirectory(qt)
