# cmake <http://www.cmake.org> test file for Gammu
# Copyright (c) 2007 - 2017 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
sendsms TEXT 132465 -text 'Lorem ipsum'
sendsms TEXT 132465 -text \"Lorem ipsum\"
")

# Basic functionality tests

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

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

add_test(gammu-nets "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" listnetworks)
set_tests_properties(gammu-nets PROPERTIES
    PASS_REGULAR_EXPRESSION "901 29     Telenor"
    )
add_test(gammu-nets-230 "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" listnetworks 230)
set_tests_properties(gammu-nets-230 PROPERTIES
    PASS_REGULAR_EXPRESSION "230 03     Vodafone"
    )

# Bitmap conversions

foreach(EXT nlm ngg nol xpm nsl wbmp bmp)
    add_test(gammu-copybitmap-${EXT} "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}"
        copybitmap ${LOGO_TEST_FILE} "${CMAKE_CURRENT_BINARY_DIR}/test-bmp.${EXT}" PICTURE)
    add_test(gammu-displaybitmap-${EXT} "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_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${CMAKE_EXECUTABLE_SUFFIX}"
    copyringtone ${RINGTONE_TEST_FILE} "${CMAKE_CURRENT_BINARY_DIR}/test-rng.bin" BINARY)
add_test(gammu-copyringtone-binary-binary "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_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${CMAKE_EXECUTABLE_SUFFIX}"
        copyringtone ${RINGTONE_TEST_FILE} "${CMAKE_CURRENT_BINARY_DIR}/test-rng.${EXT}" RTTL)
    add_test(gammu-copyringtone-binary-${EXT} "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_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${CMAKE_EXECUTABLE_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${CMAKE_EXECUTABLE_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)

if (WITH_BACKUP)
    # 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${CMAKE_EXECUTABLE_SUFFIX}" savefile VCARD10 ${TESTNAME}.1.vcf ${VCF} ME 1)
        add_test(gammu-savefile-vcf2-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" savefile VCARD21 ${TESTNAME}.2.vcf ${VCF} ME 1)
        add_test(gammu-convertbackup-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" convertbackup ${VCF} ${TESTNAME}.3.vcf)
    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${CMAKE_EXECUTABLE_SUFFIX}" savefile NOTE ${TESTNAME}.2.vnt ${VNT} 1)
        add_test(gammu-convertbackup-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" convertbackup ${VNT} ${TESTNAME}.3.vnt)
    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${CMAKE_EXECUTABLE_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${CMAKE_EXECUTABLE_SUFFIX}" savefile TODO ${TESTNAME}-todo.vcs ${VCS} 1)
        if (TESTNAME MATCHES "dtstart|rrule|UK32Holidays|badline|rrule-1|p990|k770|outlook|date-time")
            set_tests_properties(
                gammu-savefile-vcs-todo-${TESTNAME}
                PROPERTIES WILL_FAIL TRUE)
        endif (TESTNAME MATCHES "dtstart|rrule|UK32Holidays|badline|rrule-1|p990|k770|outlook|date-time")
        add_test(gammu-convertbackup-${TESTNAME} "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" convertbackup ${VCS} ${TESTNAME}.3.vcs)
    endforeach(VCS)
endif (WITH_BACKUP)

# Dummy phone tests
macro (gammu_test_named _testname _name _expression)
    if (WITH_BACKUP)
        add_test("gammu-dummy-${_testname}" "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" -c "${CMAKE_CURRENT_BINARY_DIR}/.gammurc" "${_name}" ${ARGN})
        set_tests_properties("gammu-dummy-${_testname}" PROPERTIES
            PASS_REGULAR_EXPRESSION "${_expression}"
            )
        add_test("gammu-dummy-nonext-${_testname}" "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" -s 2 -c "${CMAKE_CURRENT_BINARY_DIR}/.gammurc" "${_name}" ${ARGN})
        set_tests_properties("gammu-dummy-nonext-${_testname}" PROPERTIES
            PASS_REGULAR_EXPRESSION "${_expression}"
            )
    endif (WITH_BACKUP)
endmacro (gammu_test_named _testname _name _expression)

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)
    gammu_test_named("${_name}${__extraname}" "${_name}" "${_expression}" ${ARGN})
endmacro (gammu_test _name _expression)

# Tests which require GetNext* working
macro (gammu_test_next _name _expression)
    if (WITH_BACKUP)
        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${CMAKE_EXECUTABLE_SUFFIX}" -c "${CMAKE_CURRENT_BINARY_DIR}/.gammurc" "${_name}" ${ARGN})
        set_tests_properties("gammu-dummy-${_name}${__extraname}" PROPERTIES
            PASS_REGULAR_EXPRESSION "${_expression}"
            )
    endif (WITH_BACKUP)
endmacro (gammu_test_next _name _expression)

macro (gammu_test_fail _name _expression)
    if (WITH_BACKUP)
        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${CMAKE_EXECUTABLE_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
            )
        add_test("gammu-dummy-nonext-fail-${_name}${__extraname}" "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" -s 2 -c "${CMAKE_CURRENT_BINARY_DIR}/.gammurc" "${_name}" ${ARGN})
        set_tests_properties("gammu-dummy-nonext-fail-${_name}${__extraname}" PROPERTIES
            FAIL_REGULAR_EXPRESSION "${_expression}"
            WILL_FAIL TRUE
            )
    endif (WITH_BACKUP)
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")

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

# TODO For some reason this test fails on Windows
if (NOT WIN32)
    configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/test-incoming.sh.in" "${CMAKE_CURRENT_BINARY_DIR}/test-incoming.sh" ESCAPE_QUOTES)
    add_test(NAME "gammu-incoming" COMMAND "${SH_BIN}" "${CMAKE_CURRENT_BINARY_DIR}/test-incoming.sh" "${CMAKE_CURRENT_BINARY_DIR}/gammu${CMAKE_EXECUTABLE_SUFFIX}" "${CMAKE_CURRENT_BINARY_DIR}")
    set_tests_properties("gammu-incoming" PROPERTIES PASS_REGULAR_EXPRESSION "incoming call from \"\\+800123456\"")
endif()

gammu_test(monitor GammuTel 1)
gammu_test(battery "Lithium Polymer")
gammu_test(networkinfo "B00B.*FACE")
gammu_test(identify DUMMY-001)
gammu_test(getdatetime "[0-9][0-9][0-9][0-9]")
gammu_test(setdatetime "Updating specified parts of date and time in phone." "9:22")
gammu_test(setdatetime "Updating specified parts of date and time in phone." "2009/03/31")
gammu_test(setdatetime "Setting time in phone to the same time as is set in computer." )
gammu_test_fail(setdatetime "Invalid date or time specified." "fail")
gammu_test_fail(setdatetime "Parameter is not a number" "9:xx")
gammu_test(getsmsfolders "\\n5.")
gammu_test(dialvoice "" 123456)
gammu_test(maketerminatedcall "" 123456 10)
gammu_test(reset "" HARD)
gammu_test(reset "" SOFT)
gammu_test(resetphonesettings "" PHONE)
gammu_test(senddtmf "" 123465)
gammu_test(getdisplaystatus "Keypad locked")
#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(entersecuritycode "" "NETWORK" "1234")
gammu_test(entersecuritycode "" "PUK" "123456" "1234")
gammu_test_fail(entersecuritycode "Invalid security code type" "XXX" "1234")
gammu_test(batch "Batch processed, terminating." "${CMAKE_CURRENT_BINARY_DIR}/.gammu-batch")

gammu_test(getussd "Reply for 666" "666")

gammu_test(getsmsc "123456" 1)
gammu_test(setsmsc "" 1 123456789)
gammu_test(getsms "Empty" 1 10)
gammu_test(getsms "41761234567" 2 10)
gammu_test(getsms "41761234567" 2 10)
gammu_test_next(getallsms "6 SMS parts in 6 SMS sequences")
gammu_test_next(getallsms "6 SMS parts in 6 SMS sequences" "-pbk")
gammu_test(geteachsms "6 SMS parts in 5 SMS sequences")
gammu_test(geteachsms "6 SMS parts in 5 SMS sequences" "-pbk")
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(sendsms "" TEXT 132465 -text "Lorem ipsum")
gammu_test(savesms "" TEXT -text "Lorem ipsum" -folder 2)
gammu_test(deletesms "" 2 20001)

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_next(getallcalendar "C/S/007")
gammu_test(getcalendar "Coffee with Jason" 22)
gammu_test(getcalendar "" 42)
gammu_test(deletecalendar "Entry is empty" 42)
gammu_test(getcalendarsettings "Week starts on Monday")

gammu_test_fail(getalarm "Alarm .* not set in phone" 42)
gammu_test(setalarm "" 12 22)
gammu_test(getalarm "Alarm in location 1:" 1)

gammu_test_next(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(addnew "" ${Gammu_SOURCE_DIR}/tests/vcards/yahoo.vcf -yes)
gammu_test(addnew "Add SIM phonebook entries?" ${Gammu_SOURCE_DIR}/tests/vcards/gammu.vcf -yes)
gammu_test(addnew "" ${Gammu_SOURCE_DIR}/tests/vcards/photo-2.vcf -memory SM -yes)
gammu_test(addnew "Add phone calendar notes?" ${Gammu_SOURCE_DIR}/tests/vcal/UK32Holidays.ics -yes)
gammu_test(addnew "Add phone ToDo?" ${Gammu_SOURCE_DIR}/tests/vcal/02.vcs -yes)

gammu_test(addnew "Add notes to phone?" ${Gammu_SOURCE_DIR}/tests/vnote/symbian.vnt -yes)

gammu_test_next(getallnotes "Teolog")

gammu_test(restore "" ${Gammu_SOURCE_DIR}/tests/vcards/yahoo.vcf -yes)
gammu_test(restore "" ${Gammu_SOURCE_DIR}/tests/vcal/UK32Holidays.ics -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")

gammu_test_named(divert-get-all-all divert "" get all all)
gammu_test_named(divert-get-busy-all divert "" get busy all)
gammu_test_named(divert-get-noans-all divert "" get noans all)
gammu_test_named(divert-get-outofreach-all divert "" get outofreach all)
gammu_test_named(divert-get-all-all divert "" get all all)
gammu_test_named(divert-get-all-voice divert "" get all voice)
gammu_test_named(divert-get-all-fax divert "" get all fax)
gammu_test_named(divert-get-all-data divert "" get all data)
gammu_test_named(divert-set-all-all divert "" set all all 12345 0)
gammu_test_named(divert-set-busy-all divert "" set busy all 54321 10)
gammu_test_named(divert-set-noans-all divert "" set noans all 12345 0)
gammu_test_named(divert-set-outofreach-all divert "" set outofreach all 54321 10)
gammu_test_named(divert-set-all-all divert "" set all all 12345 0)
gammu_test_named(divert-set-all-voice divert "" set all voice 54321 10)
gammu_test_named(divert-set-all-fax divert "" set all fax 12345 0)
gammu_test_named(divert-set-all-data divert "" set all data 54321 10)
gammu_test(getwapbookmark "wammu.eu" 1 2)
gammu_test_fail(getwapbookmark "Entry is empty" 3)

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

# MMS decoding
file(GLOB MMSES "${Gammu_SOURCE_DIR}/tests/mms/*.mms")
list(SORT MMSES)

foreach(TESTMMS ${MMSES})
    gammu_test(readmmsfile "Message type" "${TESTMMS}")
endforeach(TESTMMS ${MMSES})
