blob: 401753e6606bb56e466c5e7dc058f08c13fe6628 [file] [log] [blame]
Bram Moolenaar427f5b62019-06-09 13:43:51 +02001" Tests for the sound feature
2
Dominique Pellec64ed2b2021-06-06 15:07:09 +02003source check.vim
Bram Moolenaar7b130b92020-12-06 21:43:44 +01004source shared.vim
5
Dominique Pellec64ed2b2021-06-06 15:07:09 +02006CheckFeature sound
Bram Moolenaar427f5b62019-06-09 13:43:51 +02007
8func PlayCallback(id, result)
Dominique Pellec64ed2b2021-06-06 15:07:09 +02009 let g:playcallback_count += 1
Bram Moolenaar427f5b62019-06-09 13:43:51 +020010 let g:id = a:id
11 let g:result = a:result
12endfunc
13
14func Test_play_event()
Bram Moolenaar9b283522019-06-17 22:19:33 +020015 if has('win32')
16 throw 'Skipped: Playing event with callback is not supported on Windows'
17 endif
Christian Brabandt3c4d2e72024-04-05 20:15:48 +020018 let g:result = 0
Dominique Pellec64ed2b2021-06-06 15:07:09 +020019 let g:playcallback_count = 0
Bram Moolenaar7b130b92020-12-06 21:43:44 +010020 let g:id = 0
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +010021 let event_name = 'bell'
22 if has('osx')
23 let event_name = 'Tink'
24 endif
25 let id = event_name->sound_playevent('PlayCallback')
Bram Moolenaar427f5b62019-06-09 13:43:51 +020026 if id == 0
27 throw 'Skipped: bell event not available'
28 endif
Dominique Pellec64ed2b2021-06-06 15:07:09 +020029
Bram Moolenaar427f5b62019-06-09 13:43:51 +020030 " Stop it quickly, avoid annoying the user.
31 sleep 20m
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020032 eval id->sound_stop()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010033 call WaitForAssert({-> assert_equal(id, g:id)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020034 call assert_equal(1, g:result) " sound was aborted
Dominique Pellec64ed2b2021-06-06 15:07:09 +020035 call assert_equal(1, g:playcallback_count)
Bram Moolenaar427f5b62019-06-09 13:43:51 +020036endfunc
37
38func Test_play_silent()
39 let fname = fnamemodify('silent.wav', '%p')
Dominique Pellec64ed2b2021-06-06 15:07:09 +020040 let g:playcallback_count = 0
Christian Brabandt3c4d2e72024-04-05 20:15:48 +020041 let g:result = -1
Bram Moolenaar427f5b62019-06-09 13:43:51 +020042
43 " play without callback
44 let id1 = sound_playfile(fname)
Bram Moolenaar541faf72019-06-09 15:35:41 +020045 if id1 == 0
46 throw 'Skipped: playing a sound is not working'
47 endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +020048
49 " play until the end
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020050 let id2 = fname->sound_playfile('PlayCallback')
Bram Moolenaar427f5b62019-06-09 13:43:51 +020051 call assert_true(id2 > 0)
Bram Moolenaar7b130b92020-12-06 21:43:44 +010052 call WaitForAssert({-> assert_equal(id2, g:id)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020053 call assert_equal(0, g:result)
Dominique Pellec64ed2b2021-06-06 15:07:09 +020054 call assert_equal(1, g:playcallback_count)
Bram Moolenaar427f5b62019-06-09 13:43:51 +020055
56 let id2 = sound_playfile(fname, 'PlayCallback')
57 call assert_true(id2 > 0)
58 sleep 20m
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020059 call sound_clear()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010060 call WaitForAssert({-> assert_equal(id2, g:id)})
61 call assert_equal(1, g:result) " sound was aborted
Dominique Pellec64ed2b2021-06-06 15:07:09 +020062 call assert_equal(2, g:playcallback_count)
63
64 " Play 2 sounds almost at the same time to exercise
65 " code with multiple callbacks in the callback list.
66 call sound_playfile(fname, 'PlayCallback')
67 call sound_playfile(fname, 'PlayCallback')
68 call WaitForAssert({-> assert_equal(4, g:playcallback_count)})
Bram Moolenaar28e67e02019-08-15 23:05:49 +020069
70 " recursive use was causing a crash
71 func PlayAgain(id, fname)
72 let g:id = a:id
73 let g:id_again = sound_playfile(a:fname)
74 endfunc
75
76 let id3 = sound_playfile(fname, {id, res -> PlayAgain(id, fname)})
77 call assert_true(id3 > 0)
78 sleep 50m
79 call sound_clear()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010080 call WaitForAssert({-> assert_true(g:id > 0)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020081endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020082
Dominique Pellec64ed2b2021-06-06 15:07:09 +020083func Test_play_event_error()
Dominique Pelle2f9c2092021-06-07 20:28:45 +020084 " FIXME: sound_playevent() doesn't return 0 in case of error on Windows.
85 if !has('win32')
86 call assert_equal(0, sound_playevent(''))
87 call assert_equal(0, sound_playevent(test_null_string()))
88 call assert_equal(0, sound_playevent('doesnotexist'))
89 call assert_equal(0, sound_playevent('doesnotexist', 'doesnotexist'))
90 call assert_equal(0, sound_playevent(test_null_string(), test_null_string()))
91 call assert_equal(0, sound_playevent(test_null_string(), test_null_function()))
92 endif
Dominique Pellec64ed2b2021-06-06 15:07:09 +020093
94 call assert_equal(0, sound_playfile(''))
95 call assert_equal(0, sound_playfile(test_null_string()))
96 call assert_equal(0, sound_playfile('doesnotexist'))
97 call assert_equal(0, sound_playfile('doesnotexist', 'doesnotexist'))
98 call assert_equal(0, sound_playfile(test_null_string(), test_null_string()))
99 call assert_equal(0, sound_playfile(test_null_string(), test_null_function()))
100endfunc
101
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200102" vim: shiftwidth=2 sts=2 expandtab