blob: c8f6305efb3b958cd687bea93e7cd1e35ef93061 [file] [log] [blame]
Bram Moolenaar427f5b62019-06-09 13:43:51 +02001" Tests for the sound feature
2
Bram Moolenaar7b130b92020-12-06 21:43:44 +01003source shared.vim
4
Bram Moolenaar427f5b62019-06-09 13:43:51 +02005if !has('sound')
6 throw 'Skipped: sound feature not available'
7endif
8
9func PlayCallback(id, result)
10 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
Bram Moolenaar7b130b92020-12-06 21:43:44 +010018 let g:id = 0
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020019 let id = 'bell'->sound_playevent('PlayCallback')
Bram Moolenaar427f5b62019-06-09 13:43:51 +020020 if id == 0
21 throw 'Skipped: bell event not available'
22 endif
23 " Stop it quickly, avoid annoying the user.
24 sleep 20m
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020025 eval id->sound_stop()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010026 call WaitForAssert({-> assert_equal(id, g:id)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020027 call assert_equal(1, g:result) " sound was aborted
28endfunc
29
30func Test_play_silent()
31 let fname = fnamemodify('silent.wav', '%p')
32
33 " play without callback
34 let id1 = sound_playfile(fname)
Bram Moolenaar541faf72019-06-09 15:35:41 +020035 if id1 == 0
36 throw 'Skipped: playing a sound is not working'
37 endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +020038
39 " play until the end
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020040 let id2 = fname->sound_playfile('PlayCallback')
Bram Moolenaar427f5b62019-06-09 13:43:51 +020041 call assert_true(id2 > 0)
Bram Moolenaar7b130b92020-12-06 21:43:44 +010042 call WaitForAssert({-> assert_equal(id2, g:id)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020043 call assert_equal(0, g:result)
44
45 let id2 = sound_playfile(fname, 'PlayCallback')
46 call assert_true(id2 > 0)
47 sleep 20m
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020048 call sound_clear()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010049 call WaitForAssert({-> assert_equal(id2, g:id)})
50 call assert_equal(1, g:result) " sound was aborted
Bram Moolenaar28e67e02019-08-15 23:05:49 +020051
52 " recursive use was causing a crash
53 func PlayAgain(id, fname)
54 let g:id = a:id
55 let g:id_again = sound_playfile(a:fname)
56 endfunc
57
58 let id3 = sound_playfile(fname, {id, res -> PlayAgain(id, fname)})
59 call assert_true(id3 > 0)
60 sleep 50m
61 call sound_clear()
Bram Moolenaar7b130b92020-12-06 21:43:44 +010062 call WaitForAssert({-> assert_true(g:id > 0)})
Bram Moolenaar427f5b62019-06-09 13:43:51 +020063endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020064
65" vim: shiftwidth=2 sts=2 expandtab