# 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:

include(SetupDummyPhone)

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 ${LOGO_TEST_FILE} "${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 ${RINGTONE_TEST_FILE} "${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 ${RINGTONE_TEST_FILE} "${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 ${RINGTONE_TEST_FILE} "${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)

# Backup conversions
file(GLOB VFORMATS "${CMAKE_CURRENT_SOURCE_DIR}/../tests/vcards/*.vcf")
foreach(VCF ${VFORMATS})
    get_filename_component(TESTNAME "${VCF}" NAME_WE)
    add_test(gammu-savefile-vcf1-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" savefile VCARD10 ${TESTNAME}.1.vcf ${VCF} ME 1)
    add_test(gammu-savefile-vcf2-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" savefile VCARD21 ${TESTNAME}.2.vcf ${VCF} ME 1)
endforeach(VCF)

file(GLOB VFORMATS "${CMAKE_CURRENT_SOURCE_DIR}/../tests/vnote/*.vnt")
foreach(VNT ${VFORMATS})
    get_filename_component(TESTNAME "${VNT}" NAME_WE)
    add_test(gammu-savefile-vnt-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" savefile NOTE ${TESTNAME}.2.vnt ${VNT} 1)
endforeach(VNT)

file(GLOB VFORMATS
    "${CMAKE_CURRENT_SOURCE_DIR}/../tests/vcal/*.ics"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tests/vcal/*.vcs")
foreach(VCS ${VFORMATS})
    get_filename_component(TESTNAME "${VCS}" NAME_WE)
    add_test(gammu-savefile-vcs-cal-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" savefile CALENDAR ${TESTNAME}-cal.vcs ${VCS} 1)
    if (TESTNAME MATCHES "01|02")
        set_tests_properties(
            gammu-savefile-vcs-cal-${TESTNAME}
            PROPERTIES WILL_FAIL TRUE)
    endif (TESTNAME MATCHES "01|02")
    add_test(gammu-savefile-vcs-todo-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${GAMMU_TEST_SUFFIX}" savefile TODO ${TESTNAME}-todo.vcs ${VCS} 1)
    if (TESTNAME MATCHES "dtstart|rrule|UK32Holidays|badline|rrule-1")
        set_tests_properties(
            gammu-savefile-vcs-todo-${TESTNAME}
            PROPERTIES WILL_FAIL TRUE)
    endif (TESTNAME MATCHES "dtstart|rrule|UK32Holidays|badline|rrule-1")
endforeach(VCS)

# 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)
        get_filename_component(__param ${__param} NAME)
        string (REPLACE " " "_" __param "${__param}")
        string (REPLACE "<" "_" __param "${__param}")
        string (REPLACE ">" "_" __param "${__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)
        get_filename_component(__param ${__param} NAME)
        string (REPLACE " " "_" __param "${__param}")
        string (REPLACE "<" "_" __param "${__param}")
        string (REPLACE ">" "_" __param "${__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 GammuTel 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@") <> cause problems to CTest
gammu_test(presskeysequence "" "mMnNpPuUdD+-123456789*0#gGrR[]hHcCjJfFoOmMdD@")
gammu_test(displaysms "F4F29C9E769F41F4329E0E" TEXT 132456 -text "testing text")
gammu_test(getfilesystemstatus "1010101 bytes")
gammu_test(getsecuritystatus "Nothing to enter.")
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(backupsms "" "${CMAKE_CURRENT_BINARY_DIR}/test.smsbackup" "-all")
gammu_test(deletesms "" 1 2)

gammu_test(getallmemory "Name                 : \"firstname lastname\"" SM)
gammu_test(getallmemory "LUID                 : \"000200000012\"" ME)
gammu_test(getmemory "ISP Ph 019833206" ME 101)
gammu_test(deletememory "Entry was empty" ME 99)
gammu_test(searchmemory "3899-5174" "3899-1411")

gammu_test(getallcalendar "C/S/007")
gammu_test(getcalendar "Coffee with Jason" 22)
gammu_test(getcalendar "" 42)
gammu_test(deletecalendar "Entry is empty" 42)

gammu_test(getalltodo "Wammu Bug erforschen")
gammu_test(gettodo "Gorusme" 3)
gammu_test(gettodo "" 99)
gammu_test(deletetodo "Entry was empty" 99)

gammu_test(backup "" test.backup -yes)
gammu_test(backup "" test.vcf -yes)
gammu_test(backup "" test.vcs -yes)
gammu_test(backup "" test.ics -yes)
gammu_test(backup "" test.lmb -yes)
gammu_test(backup "" test.ldif -yes)

gammu_test(getfilesystem "     .    .-- Folder \"dir1-2-3\"")
gammu_test(getfilesystem "" -flat)
gammu_test(getfilesystem "dir2/dir2-5/file4" -flatall)
gammu_test(getfilefolder "" "dir2/dir2-5")
gammu_test(getfiles "" "file5" "dir2/dir2-5/file2")
gammu_test(addfile "ID of new file is \"dir2/dir2-5/file5\"" "dir2/dir2-5" "file5" -type NRT -protected -readonly -hidden -system)
gammu_test(sendfile "ID of new file is \"incoming/test.vcf\"" test.vcf)
gammu_test(setfileattrib "" incoming/test.vcf -protected -readonly -hidden -system)
gammu_test(deletefiles "" incoming/test.vcf dir2/dir2-5/file5)
gammu_test(addfolder "" "/" "foobar")
gammu_test(deletefolder "" "foobar")
gammu_test(getfolderlisting "dir2/dir2-5" "dir2")

if (ONLINE_TESTING AND CURL_FOUND)
    gammu_test(checkfirmware "")
    gammu_test(checkversion "")
endif (ONLINE_TESTING AND CURL_FOUND)
