blob: fba904c1dd099086b289ff198fa95ca69da0626e [file] [log] [blame]
Bram Moolenaar427f5b62019-06-09 13:43:51 +02001" Tests for the sound feature
2
Dominique Pellec64ed2b2021-06-06 15:07:09 +02003CheckFeature sound
Bram Moolenaar427f5b62019-06-09 13:43:51 +02004
5func PlayCallback(id, result)
Dominique Pellec64ed2b2021-06-06 15:07:09 +02006 let g:playcallback_count += 1
Bram Moolenaar427f5b62019-06-09 13:43:51 +02007 let g:id = a:id
8 let g:result = a:result
9endfunc
10
11func Test_play_event()
Bram Moolenaar9b283522019-06-17 22:19:33 +020012 if has('win32')
13 throw 'Skipped: Playing event with callback is not supported on Windows'
14 endif
Christian Brabandt3c4d2e72024-04-05 20:15:48 +020015 let g:result = 0
Dominique Pellec64ed2b2021-06-06 15:07:09 +020016 let g:playcallback_count = 0
Bram Moolenaar7b130b92020-12-06 21:43:44 +010017 let g:id = 0
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +010018 let event_name = 'bell'
19 if has('osx')
20 let event_name = 'Tink'
21 endif
22 let id = event_name->sound_playevent('PlayCallback')
Bram Moolenaar427f5b62019-06-09 13:43:51 +020023 if id == 0
24 throw 'Skipped: bell event not available'
25 endif
Dominique Pellec64ed2b2021-06-06 15:07:09 +020026
Bram Moolenaar427f5b62019-06-09 13:43:51 +020027 " Stop it quickly, avoid annoying the user.
28 sleep 20m
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020029 eval id->sound_stop()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010030 call WaitForAssert({-> assert_equal(id, g:id)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020031 call assert_equal(1, g:result) " sound was aborted
Dominique Pellec64ed2b2021-06-06 15:07:09 +020032 call assert_equal(1, g:playcallback_count)
Bram Moolenaar427f5b62019-06-09 13:43:51 +020033endfunc
34
35func Test_play_silent()
36 let fname = fnamemodify('silent.wav', '%p')
Dominique Pellec64ed2b2021-06-06 15:07:09 +020037 let g:playcallback_count = 0
Christian Brabandt3c4d2e72024-04-05 20:15:48 +020038 let g:result = -1
Bram Moolenaar427f5b62019-06-09 13:43:51 +020039
40 " play without callback
41 let id1 = sound_playfile(fname)
Bram Moolenaar541faf72019-06-09 15:35:41 +020042 if id1 == 0
43 throw 'Skipped: playing a sound is not working'
44 endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +020045
46 " play until the end
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020047 let id2 = fname->sound_playfile('PlayCallback')
Bram Moolenaar427f5b62019-06-09 13:43:51 +020048 call assert_true(id2 > 0)
Bram Moolenaar7b130b92020-12-06 21:43:44 +010049 call WaitForAssert({-> assert_equal(id2, g:id)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020050 call assert_equal(0, g:result)
Dominique Pellec64ed2b2021-06-06 15:07:09 +020051 call assert_equal(1, g:playcallback_count)
Bram Moolenaar427f5b62019-06-09 13:43:51 +020052
53 let id2 = sound_playfile(fname, 'PlayCallback')
54 call assert_true(id2 > 0)
55 sleep 20m
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020056 call sound_clear()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010057 call WaitForAssert({-> assert_equal(id2, g:id)})
58 call assert_equal(1, g:result) " sound was aborted
Dominique Pellec64ed2b2021-06-06 15:07:09 +020059 call assert_equal(2, g:playcallback_count)
60
61 " Play 2 sounds almost at the same time to exercise
62 " code with multiple callbacks in the callback list.
63 call sound_playfile(fname, 'PlayCallback')
64 call sound_playfile(fname, 'PlayCallback')
65 call WaitForAssert({-> assert_equal(4, g:playcallback_count)})
Bram Moolenaar28e67e02019-08-15 23:05:49 +020066
67 " recursive use was causing a crash
68 func PlayAgain(id, fname)
69 let g:id = a:id
70 let g:id_again = sound_playfile(a:fname)
71 endfunc
72
73 let id3 = sound_playfile(fname, {id, res -> PlayAgain(id, fname)})
74 call assert_true(id3 > 0)
75 sleep 50m
76 call sound_clear()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010077 call WaitForAssert({-> assert_true(g:id > 0)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020078endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020079
Dominique Pellec64ed2b2021-06-06 15:07:09 +020080func Test_play_event_error()
Dominique Pelle2f9c2092021-06-07 20:28:45 +020081 " FIXME: sound_playevent() doesn't return 0 in case of error on Windows.
82 if !has('win32')
83 call assert_equal(0, sound_playevent(''))
84 call assert_equal(0, sound_playevent(test_null_string()))
85 call assert_equal(0, sound_playevent('doesnotexist'))
86 call assert_equal(0, sound_playevent('doesnotexist', 'doesnotexist'))
87 call assert_equal(0, sound_playevent(test_null_string(), test_null_string()))
88 call assert_equal(0, sound_playevent(test_null_string(), test_null_function()))
89 endif
Dominique Pellec64ed2b2021-06-06 15:07:09 +020090
91 call assert_equal(0, sound_playfile(''))
92 call assert_equal(0, sound_playfile(test_null_string()))
93 call assert_equal(0, sound_playfile('doesnotexist'))
94 call assert_equal(0, sound_playfile('doesnotexist', 'doesnotexist'))
95 call assert_equal(0, sound_playfile(test_null_string(), test_null_string()))
96 call assert_equal(0, sound_playfile(test_null_string(), test_null_function()))
97endfunc
98
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020099" vim: shiftwidth=2 sts=2 expandtab