# cmake <http://www.cmake.org> test file for Gammu
# Copyright (c) 2007-2009 Michal Cihar
# vim: expandtab sw=4 ts=4 sts=4 ft=cmake:


file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/1")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/2")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/3")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/4")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/5")
configure_file("${Gammu_SOURCE_DIR}/tests/at-sms-encode/01.backup" "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/1/1" COPY_ONLY)
configure_file("${Gammu_SOURCE_DIR}/tests/at-sms-encode/90.backup" "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/2/10" COPY_ONLY)
configure_file("${Gammu_SOURCE_DIR}/tests/at-sms-encode/99.backup" "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/3/42" COPY_ONLY)
configure_file("${Gammu_SOURCE_DIR}/tests/at-sms-encode/80.backup" "${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy/sms/4/15" COPY_ONLY)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/.gammurc" "
# Generated gammurc for test purposes
[gammu]
model = dummy
connection = none
port = ${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy
gammuloc = /dev/null

[gammu99]
model = dummy
connection = none
port = ${CMAKE_CURRENT_BINARY_DIR}/.gammu-dummy
gammuloc = /dev/null
")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/.gammu-batch" "
# Comment
identify
dialvoice 123456
reset SOFT
")

# Basic functionality tests

add_test(gammu-help "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" help)
set_tests_properties(gammu-help PROPERTIES
    PASS_REGULAR_EXPRESSION "${VERSION}"
    )
add_test(gammu-features "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" features)
add_test(gammu-version "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" version)

add_test(gammu-help-all "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" help all)
set_tests_properties(gammu-help-all PROPERTIES
    PASS_REGULAR_EXPRESSION "batch"
    )

add_test(gammu-nets "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" listnetworks)
set_tests_properties(gammu-nets PROPERTIES
    PASS_REGULAR_EXPRESSION "740 01[\\t ]*Conecel S.A. \\(Consorcio Ecuatoriano de Telecomunicaciones S.A.\\)"
    )
add_test(gammu-nets-230 "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" listnetworks 230)
set_tests_properties(gammu-nets-230 PROPERTIES
    PASS_REGULAR_EXPRESSION "230 03     Vodafone CZ"
    )

# Bitmap conversions

foreach(EXT nlm ngg nol xpm nsl wbmp bmp)
    add_test(gammu-copybitmap-${EXT} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}"
        copybitmap ${Gammu_SOURCE_DIR}/docs/examples/media/aliens.nlm "${CMAKE_CURRENT_BINARY_DIR}/test-bmp.${EXT}" PICTURE)
    add_test(gammu-displaybitmap-${EXT} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}"
        copybitmap "${CMAKE_CURRENT_BINARY_DIR}/test-bmp.${EXT}")
    # Reading of xpm is not supported
    if (${EXT} STREQUAL xpm)
        set_tests_properties(
            gammu-displaybitmap-${EXT}
            PROPERTIES WILL_FAIL TRUE)
    endif (${EXT} STREQUAL xpm)
endforeach(EXT)

add_test(gammu-copyringtone-binary "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}"
    copyringtone ${Gammu_SOURCE_DIR}/docs/examples/media/axelf.txt "${CMAKE_CURRENT_BINARY_DIR}/test-rng.bin" BINARY)
add_test(gammu-copyringtone-binary-binary "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}"
    copyringtone "${CMAKE_CURRENT_BINARY_DIR}/test-rng.bin" "${CMAKE_CURRENT_BINARY_DIR}/test-rng-bin.bin" BINARY)

# Ringtone conversions

foreach(EXT ott rng mid imy ime wav rttl)
    add_test(gammu-copyringtone-${EXT} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}"
        copyringtone ${Gammu_SOURCE_DIR}/docs/examples/media/axelf.txt "${CMAKE_CURRENT_BINARY_DIR}/test-rng.${EXT}" RTTL)
    add_test(gammu-copyringtone-binary-${EXT} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}"
        copyringtone "${CMAKE_CURRENT_BINARY_DIR}/test-rng.bin" "${CMAKE_CURRENT_BINARY_DIR}/test-rng-bin.${EXT}" RTTL)
    add_test(gammu-copyringtone-${EXT}-binary "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}"
        copyringtone ${Gammu_SOURCE_DIR}/docs/examples/media/axelf.txt "${CMAKE_CURRENT_BINARY_DIR}/test-rng-${EXT}.bin" BINARY)

    foreach(EXT2 ott rng mid imy ime wav rttl)
        add_test(gammu-copyringtone-${EXT}-${EXT} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}"
            copyringtone "${CMAKE_CURRENT_BINARY_DIR}/test-rng.${EXT}" "${CMAKE_CURRENT_BINARY_DIR}/test-rng-${EXT}.${EXT2}" RTTL)
        # Reading midi is not supported
        if (${EXT} STREQUAL mid OR ${EXT} STREQUAL wav)
            set_tests_properties(
                gammu-copyringtone-${EXT}-${EXT}
                PROPERTIES WILL_FAIL TRUE)
        endif (${EXT} STREQUAL mid OR ${EXT} STREQUAL wav)
    endforeach(EXT2)
endforeach(EXT)

# Dummy phone tests
macro (gammu_test _name _expression)
    set(_params_list ${ARGN})
    list(LENGTH _params_list _params_count)
    if (_params_count GREATER 0)
        list(GET _params_list 0 __param)
        string (REPLACE " " "_" __param "${__param}")
        string (REPLACE "-" "_" __param "${__param}")
        string (REPLACE "=" "_" __param "${__param}")
        string (REPLACE "," "_" __param "${__param}")
        string (REPLACE "#" "_" __param "${__param}")
        set(__extraname "-${__param}")
    else (_params_count GREATER 0)
        set(__extraname "")
    endif (_params_count GREATER 0)
    add_test("gammu-dummy-${_name}${__extraname}" "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" -c "${CMAKE_CURRENT_BINARY_DIR}/.gammurc" "${_name}" ${ARGN})
    set_tests_properties("gammu-dummy-${_name}${__extraname}" PROPERTIES
        PASS_REGULAR_EXPRESSION "${_expression}"
        )
endmacro (gammu_test _name _expression)

macro (gammu_test_fail _name _expression)
    set(_params_list ${ARGN})
    list(LENGTH _params_list _params_count)
    if (${_params_count} GREATER 0)
        list(GET _params_list 0 __param)
        string (REPLACE " " "_" __param "${__param}")
        string (REPLACE "-" "_" __param "${__param}")
        string (REPLACE "=" "_" __param "${__param}")
        string (REPLACE "," "_" __param "${__param}")
        string (REPLACE "#" "_" __param "${__param}")
        set(__extraname "-${__param}")
    else (${_params_count} GREATER 0)
        set(__extraname "")
    endif (${_params_count} GREATER 0)
    add_test("gammu-dummy-fail-${_name}${__extraname}" "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" -c "${CMAKE_CURRENT_BINARY_DIR}/.gammurc" "${_name}" ${ARGN})
    set_tests_properties("gammu-dummy-fail-${_name}${__extraname}" PROPERTIES
        FAIL_REGULAR_EXPRESSION "${_expression}"
        WILL_FAIL TRUE
        )
endmacro (gammu_test_fail _name _expression)

gammu_test_fail(nonsense "Bad option!")
gammu_test_fail(identify "Too many parameters" 1)
gammu_test_fail(reset "More parameters required")

add_test("gammu-dummy-99-identify" "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" -c "${CMAKE_CURRENT_BINARY_DIR}/.gammurc" -s 99 identify)
set_tests_properties("gammu-dummy-99-identify" PROPERTIES
    PASS_REGULAR_EXPRESSION "DUMMY-001"
    )

gammu_test(monitor NasraTel 1)
gammu_test(networkinfo "B00B.*FACE")
gammu_test(identify DUMMY-001)
gammu_test(getdatetime "[0-9][0-9][0-9][0-9]")
gammu_test(getsmsfolders "\\n5.")
gammu_test(dialvoice "" 123456)
gammu_test(reset "" HARD)
gammu_test(reset "" SOFT)
gammu_test(presskeysequence "" "mMnNpPuUdD+-123456789*0#gGrR<>[]hHcCjJfFoOmMdD@")
gammu_test(displaysms "F4F29C9E769F41F4329E0E" TEXT 132456 -text "testing text")
gammu_test(getfilesystemstatus "1010101 bytes")
gammu_test(batch "Batch processed, terminating." "${CMAKE_CURRENT_BINARY_DIR}/.gammu-batch")
gammu_test(getsmsc "123456" 1)
gammu_test(getsms "Empty" 1 10)
gammu_test(getsms "41761234567" 2 10)
gammu_test(getsms "41761234567" 2 10)
gammu_test(getallsms "4 SMS parts in 4 SMS sequences")
gammu_test(geteachsms "4 SMS parts in 4 SMS sequences")
gammu_test_fail(deletesms "Entry is empty" 1 99)
gammu_test(addsms "" 1 "${Gammu_SOURCE_DIR}/tests/at-sms-encode/66.backup" "-yes")
gammu_test(deletesms "" 1 2)
