Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 1 | " Tests for the sound feature |
| 2 | |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 3 | CheckFeature sound |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 4 | |
| 5 | func PlayCallback(id, result) |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 6 | let g:playcallback_count += 1 |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 7 | let g:id = a:id |
| 8 | let g:result = a:result |
| 9 | endfunc |
| 10 | |
| 11 | func Test_play_event() |
Bram Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 12 | if has('win32') |
| 13 | throw 'Skipped: Playing event with callback is not supported on Windows' |
| 14 | endif |
Christian Brabandt | 3c4d2e7 | 2024-04-05 20:15:48 +0200 | [diff] [blame] | 15 | let g:result = 0 |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 16 | let g:playcallback_count = 0 |
Bram Moolenaar | 7b130b9 | 2020-12-06 21:43:44 +0100 | [diff] [blame] | 17 | let g:id = 0 |
Yee Cheng Chin | 4314e4f | 2022-10-08 13:50:05 +0100 | [diff] [blame] | 18 | let event_name = 'bell' |
| 19 | if has('osx') |
| 20 | let event_name = 'Tink' |
| 21 | endif |
| 22 | let id = event_name->sound_playevent('PlayCallback') |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 23 | if id == 0 |
| 24 | throw 'Skipped: bell event not available' |
| 25 | endif |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 26 | |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 27 | " Stop it quickly, avoid annoying the user. |
| 28 | sleep 20m |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 29 | eval id->sound_stop() |
Bram Moolenaar | 7b130b9 | 2020-12-06 21:43:44 +0100 | [diff] [blame] | 30 | call WaitForAssert({-> assert_equal(id, g:id)}) |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 31 | call assert_equal(1, g:result) " sound was aborted |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 32 | call assert_equal(1, g:playcallback_count) |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 33 | endfunc |
| 34 | |
| 35 | func Test_play_silent() |
| 36 | let fname = fnamemodify('silent.wav', '%p') |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 37 | let g:playcallback_count = 0 |
Christian Brabandt | 3c4d2e7 | 2024-04-05 20:15:48 +0200 | [diff] [blame] | 38 | let g:result = -1 |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 39 | |
| 40 | " play without callback |
| 41 | let id1 = sound_playfile(fname) |
Bram Moolenaar | 541faf7 | 2019-06-09 15:35:41 +0200 | [diff] [blame] | 42 | if id1 == 0 |
| 43 | throw 'Skipped: playing a sound is not working' |
| 44 | endif |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 45 | |
| 46 | " play until the end |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 47 | let id2 = fname->sound_playfile('PlayCallback') |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 48 | call assert_true(id2 > 0) |
Bram Moolenaar | 7b130b9 | 2020-12-06 21:43:44 +0100 | [diff] [blame] | 49 | call WaitForAssert({-> assert_equal(id2, g:id)}) |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 50 | call assert_equal(0, g:result) |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 51 | call assert_equal(1, g:playcallback_count) |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 52 | |
| 53 | let id2 = sound_playfile(fname, 'PlayCallback') |
| 54 | call assert_true(id2 > 0) |
| 55 | sleep 20m |
Bram Moolenaar | 3ff5f0f | 2019-06-10 13:11:22 +0200 | [diff] [blame] | 56 | call sound_clear() |
Bram Moolenaar | 7b130b9 | 2020-12-06 21:43:44 +0100 | [diff] [blame] | 57 | call WaitForAssert({-> assert_equal(id2, g:id)}) |
| 58 | call assert_equal(1, g:result) " sound was aborted |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 59 | 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 Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 66 | |
| 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 Moolenaar | 7b130b9 | 2020-12-06 21:43:44 +0100 | [diff] [blame] | 77 | call WaitForAssert({-> assert_true(g:id > 0)}) |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 78 | endfunc |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 79 | |
Dominique Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 80 | func Test_play_event_error() |
Dominique Pelle | 2f9c209 | 2021-06-07 20:28:45 +0200 | [diff] [blame] | 81 | " 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 Pelle | c64ed2b | 2021-06-06 15:07:09 +0200 | [diff] [blame] | 90 | |
| 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())) |
| 97 | endfunc |
| 98 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 99 | " vim: shiftwidth=2 sts=2 expandtab |