blob: 020d458912ae97b965b50ba36f5f1e8028db705c [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
rhysde93d5ca2024-02-01 21:22:14 +010018 if has('osx') && !empty($CI) && system('uname -m') =~# 'arm64'
19 throw 'Skipped: FIXME: Running this test on M1 Mac hangs on GitHub Actions'
20 endif
Dominique Pellec64ed2b2021-06-06 15:07:09 +020021 let g:playcallback_count = 0
Bram Moolenaar7b130b92020-12-06 21:43:44 +010022 let g:id = 0
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +010023 let event_name = 'bell'
24 if has('osx')
25 let event_name = 'Tink'
26 endif
27 let id = event_name->sound_playevent('PlayCallback')
Bram Moolenaar427f5b62019-06-09 13:43:51 +020028 if id == 0
29 throw 'Skipped: bell event not available'
30 endif
Dominique Pellec64ed2b2021-06-06 15:07:09 +020031
Bram Moolenaar427f5b62019-06-09 13:43:51 +020032 " Stop it quickly, avoid annoying the user.
33 sleep 20m
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020034 eval id->sound_stop()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010035 call WaitForAssert({-> assert_equal(id, g:id)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020036 call assert_equal(1, g:result) " sound was aborted
Dominique Pellec64ed2b2021-06-06 15:07:09 +020037 call assert_equal(1, g:playcallback_count)
Bram Moolenaar427f5b62019-06-09 13:43:51 +020038endfunc
39
40func Test_play_silent()
rhysde93d5ca2024-02-01 21:22:14 +010041 if has('osx') && !empty($CI) && system('uname -m') =~# 'arm64'
42 throw 'Skipped: FIXME: Running this test on M1 Mac hangs on GitHub Actions'
43 endif
44
Bram Moolenaar427f5b62019-06-09 13:43:51 +020045 let fname = fnamemodify('silent.wav', '%p')
Dominique Pellec64ed2b2021-06-06 15:07:09 +020046 let g:playcallback_count = 0
Bram Moolenaar427f5b62019-06-09 13:43:51 +020047
48 " play without callback
49 let id1 = sound_playfile(fname)
Bram Moolenaar541faf72019-06-09 15:35:41 +020050 if id1 == 0
51 throw 'Skipped: playing a sound is not working'
52 endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +020053
54 " play until the end
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020055 let id2 = fname->sound_playfile('PlayCallback')
Bram Moolenaar427f5b62019-06-09 13:43:51 +020056 call assert_true(id2 > 0)
Bram Moolenaar7b130b92020-12-06 21:43:44 +010057 call WaitForAssert({-> assert_equal(id2, g:id)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020058 call assert_equal(0, g:result)
Dominique Pellec64ed2b2021-06-06 15:07:09 +020059 call assert_equal(1, g:playcallback_count)
Bram Moolenaar427f5b62019-06-09 13:43:51 +020060
61 let id2 = sound_playfile(fname, 'PlayCallback')
62 call assert_true(id2 > 0)
63 sleep 20m
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020064 call sound_clear()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010065 call WaitForAssert({-> assert_equal(id2, g:id)})
66 call assert_equal(1, g:result) " sound was aborted
Dominique Pellec64ed2b2021-06-06 15:07:09 +020067 call assert_equal(2, g:playcallback_count)
68
69 " Play 2 sounds almost at the same time to exercise
70 " code with multiple callbacks in the callback list.
71 call sound_playfile(fname, 'PlayCallback')
72 call sound_playfile(fname, 'PlayCallback')
73 call WaitForAssert({-> assert_equal(4, g:playcallback_count)})
Bram Moolenaar28e67e02019-08-15 23:05:49 +020074
75 " recursive use was causing a crash
76 func PlayAgain(id, fname)
77 let g:id = a:id
78 let g:id_again = sound_playfile(a:fname)
79 endfunc
80
81 let id3 = sound_playfile(fname, {id, res -> PlayAgain(id, fname)})
82 call assert_true(id3 > 0)
83 sleep 50m
84 call sound_clear()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010085 call WaitForAssert({-> assert_true(g:id > 0)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020086endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020087
Dominique Pellec64ed2b2021-06-06 15:07:09 +020088func Test_play_event_error()
Dominique Pelle2f9c2092021-06-07 20:28:45 +020089 " FIXME: sound_playevent() doesn't return 0 in case of error on Windows.
90 if !has('win32')
91 call assert_equal(0, sound_playevent(''))
92 call assert_equal(0, sound_playevent(test_null_string()))
93 call assert_equal(0, sound_playevent('doesnotexist'))
94 call assert_equal(0, sound_playevent('doesnotexist', 'doesnotexist'))
95 call assert_equal(0, sound_playevent(test_null_string(), test_null_string()))
96 call assert_equal(0, sound_playevent(test_null_string(), test_null_function()))
97 endif
Dominique Pellec64ed2b2021-06-06 15:07:09 +020098
99 call assert_equal(0, sound_playfile(''))
100 call assert_equal(0, sound_playfile(test_null_string()))
101 call assert_equal(0, sound_playfile('doesnotexist'))
102 call assert_equal(0, sound_playfile('doesnotexist', 'doesnotexist'))
103 call assert_equal(0, sound_playfile(test_null_string(), test_null_string()))
104 call assert_equal(0, sound_playfile(test_null_string(), test_null_function()))
105endfunc
106
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200107" vim: shiftwidth=2 sts=2 expandtab