1" Tests for the sound feature 2 3source check.vim 4source shared.vim 5 6CheckFeature sound 7 8func PlayCallback(id, result) 9 let g:playcallback_count += 1 10 let g:id = a:id 11 let g:result = a:result 12endfunc 13 14func Test_play_event() 15 if has('win32') 16 throw 'Skipped: Playing event with callback is not supported on Windows' 17 endif 18 let g:playcallback_count = 0 19 let g:id = 0 20 let id = 'bell'->sound_playevent('PlayCallback') 21 if id == 0 22 throw 'Skipped: bell event not available' 23 endif 24 25 " Stop it quickly, avoid annoying the user. 26 sleep 20m 27 eval id->sound_stop() 28 call WaitForAssert({-> assert_equal(id, g:id)}) 29 call assert_equal(1, g:result) " sound was aborted 30 call assert_equal(1, g:playcallback_count) 31endfunc 32 33func Test_play_silent() 34 let fname = fnamemodify('silent.wav', '%p') 35 let g:playcallback_count = 0 36 37 " play without callback 38 let id1 = sound_playfile(fname) 39 if id1 == 0 40 throw 'Skipped: playing a sound is not working' 41 endif 42 43 " play until the end 44 let id2 = fname->sound_playfile('PlayCallback') 45 call assert_true(id2 > 0) 46 call WaitForAssert({-> assert_equal(id2, g:id)}) 47 call assert_equal(0, g:result) 48 call assert_equal(1, g:playcallback_count) 49 50 let id2 = sound_playfile(fname, 'PlayCallback') 51 call assert_true(id2 > 0) 52 sleep 20m 53 call sound_clear() 54 call WaitForAssert({-> assert_equal(id2, g:id)}) 55 call assert_equal(1, g:result) " sound was aborted 56 call assert_equal(2, g:playcallback_count) 57 58 " Play 2 sounds almost at the same time to exercise 59 " code with multiple callbacks in the callback list. 60 call sound_playfile(fname, 'PlayCallback') 61 call sound_playfile(fname, 'PlayCallback') 62 call WaitForAssert({-> assert_equal(4, g:playcallback_count)}) 63 64 " recursive use was causing a crash 65 func PlayAgain(id, fname) 66 let g:id = a:id 67 let g:id_again = sound_playfile(a:fname) 68 endfunc 69 70 let id3 = sound_playfile(fname, {id, res -> PlayAgain(id, fname)}) 71 call assert_true(id3 > 0) 72 sleep 50m 73 call sound_clear() 74 call WaitForAssert({-> assert_true(g:id > 0)}) 75endfunc 76 77func Test_play_event_error() 78 " FIXME: sound_playevent() doesn't return 0 in case of error on Windows. 79 if !has('win32') 80 call assert_equal(0, sound_playevent('')) 81 call assert_equal(0, sound_playevent(test_null_string())) 82 call assert_equal(0, sound_playevent('doesnotexist')) 83 call assert_equal(0, sound_playevent('doesnotexist', 'doesnotexist')) 84 call assert_equal(0, sound_playevent(test_null_string(), test_null_string())) 85 call assert_equal(0, sound_playevent(test_null_string(), test_null_function())) 86 endif 87 88 call assert_equal(0, sound_playfile('')) 89 call assert_equal(0, sound_playfile(test_null_string())) 90 call assert_equal(0, sound_playfile('doesnotexist')) 91 call assert_equal(0, sound_playfile('doesnotexist', 'doesnotexist')) 92 call assert_equal(0, sound_playfile(test_null_string(), test_null_string())) 93 call assert_equal(0, sound_playfile(test_null_string(), test_null_function())) 94endfunc 95 96" vim: shiftwidth=2 sts=2 expandtab 97