xref: /src/usr.sbin/mixer/tests/mixer_test.sh (revision 5fa297f4bcd277dca7eef03e68363fb1e721799e)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2024 The FreeBSD Foundation
5#
6# This software was developed by Christos Margiolis <christos@FreeBSD.org>
7# under sponsorship from the FreeBSD Foundation.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29
30mixer_exists()
31{
32	if ! mixer >/dev/null 2>&1; then
33		atf_skip "no mixer available"
34	fi
35}
36
37save_conf()
38{
39	atf_check -o save:test_mixer_conf mixer -o
40}
41
42restore_conf()
43{
44	mixer_exists
45	test -r "test_mixer_conf" && mixer $(cat test_mixer_conf)
46}
47
48set_default()
49{
50	deflt_unit="$(mixer | grep ^pcm | cut -f1 -d:)"
51	dummy_unit="$(mixer -a | grep "Dummy Audio Device" | cut -f1 -d:)"
52
53	atf_check -o save:test_mixer_deflt_unit echo ${deflt_unit}
54	atf_check -o save:test_mixer_dummy_unit echo ${dummy_unit}
55
56	# Set the dummy as the default
57	mixer -d ${dummy_unit}
58}
59
60restore_default()
61{
62	test -r "test_mixer_deflt_unit" &&
63	mixer -d $(cat test_mixer_deflt_unit) || true
64}
65
66atf_test_case o_flag cleanup
67o_flag_head()
68{
69	atf_set "descr" "Verify that the output of the -o flag can be used " \
70		"as valid input"
71}
72o_flag_body()
73{
74	mixer_exists
75	set_default
76
77	atf_check -o ignore -e empty mixer $(mixer -o)
78}
79o_flag_cleanup()
80{
81	restore_default
82}
83
84atf_test_case d_flag cleanup
85d_flag_head()
86{
87	atf_set "descr" "Test default unit setting"
88}
89d_flag_body()
90{
91	mixer_exists
92	set_default
93
94	dev="${dummy_unit}"
95	unit=$(echo ${dev} | sed 's/pcm//')
96
97	atf_check -o ignore -e empty mixer -d ${dev}
98	atf_check -o ignore -e empty mixer -d ${unit}
99}
100d_flag_cleanup()
101{
102	restore_default
103}
104
105atf_test_case volume cleanup
106volume_head()
107{
108	atf_set "descr" "Test volume setting"
109}
110volume_body()
111{
112	mixer_exists
113	set_default
114	save_conf
115
116	# Test lower bound
117	mixer vol.volume=0
118	atf_check -o match:"0.00:0.00" mixer vol.volume
119
120	mixer vol.volume=-2
121	atf_check -o match:"0.00:0.00" mixer vol.volume
122
123	mixer vol.volume=-1:-2
124	atf_check -o match:"0.00:0.00" mixer vol.volume
125
126	mixer vol.volume=-110%
127	atf_check -o match:"0.00:0.00" mixer vol.volume
128
129	# Test higher bound
130	mixer vol.volume=1
131	atf_check -o match:"1.00:1.00" mixer vol.volume
132
133	mixer vol.volume=+1.01
134	atf_check -o match:"1.00:1.00" mixer vol.volume
135
136	mixer vol.volume=2
137	atf_check -o match:"1.00:1.00" mixer vol.volume
138
139	mixer vol.volume=+1:+1
140	atf_check -o match:"1.00:1.00" mixer vol.volume
141
142	mixer vol.volume=2:2
143	atf_check -o match:"1.00:1.00" mixer vol.volume
144
145	mixer vol.volume=100%
146	atf_check -o match:"1.00:1.00" mixer vol.volume
147
148	mixer vol.volume=110%
149	atf_check -o match:"1.00:1.00" mixer vol.volume
150
151	mixer vol.volume=+110%
152	atf_check -o match:"1.00:1.00" mixer vol.volume
153
154	# Test percentages
155	mixer vol.volume=1
156
157	mixer vol.volume=-10%
158	atf_check -o match:"0.90:0.90" mixer vol.volume
159
160	mixer vol.volume=+5%
161	atf_check -o match:"0.95:0.95" mixer vol.volume
162
163	mixer vol.volume=80%
164	atf_check -o match:"0.80:0.80" mixer vol.volume
165
166	# Test left:right assignment
167	mixer vol.volume=0.80:0.70
168	atf_check -o match:"0.80:0.70" mixer vol.volume
169
170	mixer vol.volume=+5%:+10%
171	atf_check -o match:"0.85:0.80" mixer vol.volume
172
173	mixer vol.volume=-5%:-10%
174	atf_check -o match:"0.80:0.70" mixer vol.volume
175
176	mixer vol.volume=+10%:-15%
177	atf_check -o match:"0.90:0.55" mixer vol.volume
178
179	# Test wrong values
180	atf_check -o ignore -e not-empty mixer vol.volume=foobar
181	atf_check -o ignore -e not-empty mixer vol.volume=2oo:b4r
182	atf_check -o ignore -e not-empty mixer vol.volume=+f0o:1
183}
184volume_cleanup()
185{
186	restore_conf
187	restore_default
188}
189
190atf_test_case mute cleanup
191mute_head()
192{
193	atf_set "descr" "Test muting"
194}
195mute_body()
196{
197	mixer_exists
198	set_default
199	save_conf
200
201	# Check that the mute control exists
202	atf_check -o not-empty mixer vol.mute
203
204	atf_check -o ignore -e empty mixer vol.mute=off
205	atf_check -o match:"=off" mixer vol.mute
206
207	atf_check -o ignore -e empty mixer vol.mute=on
208	atf_check -o match:"=on" mixer vol.mute
209
210	atf_check -o ignore -e empty mixer vol.mute=toggle
211	atf_check -o match:"=off" mixer vol.mute
212
213	# Test deprecated interface
214	atf_check -o ignore -e empty mixer vol.mute=0
215	atf_check -o match:"=off" mixer vol.mute
216
217	atf_check -o ignore -e empty mixer vol.mute=1
218	atf_check -o match:"=on" mixer vol.mute
219
220	atf_check -o ignore -e empty mixer vol.mute=^
221	atf_check -o match:"=off" mixer vol.mute
222
223	# Test wrong values
224	atf_check -o ignore -e not-empty mixer vol.mute=foobar
225	atf_check -o ignore -e not-empty mixer vol.mute=10
226}
227mute_cleanup()
228{
229	restore_conf
230	restore_default
231}
232
233atf_test_case recsrc cleanup
234recsrc_head()
235{
236	atf_set "descr" "Test recording source handling"
237}
238recsrc_body()
239{
240	mixer_exists
241	set_default
242	save_conf
243	test -n "$(mixer -s)" || atf_skip "no recording source found"
244
245	recsrc=$(mixer -s | awk '{print $2}')
246	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=add
247	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=remove
248	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=set
249	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=toggle
250
251	# Test deprecated interface
252	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=+
253	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=-
254	atf_check -o ignore -e empty mixer ${recsrc}.recsrc==
255	atf_check -o ignore -e empty mixer ${recsrc}.recsrc=^
256
257	# Test wrong values
258	atf_check -o ignore -e not-empty mixer ${recsrc}.recsrc=foobar
259	atf_check -o ignore -e not-empty mixer ${recsrc}.recsrc=10
260}
261recsrc_cleanup()
262{
263	restore_conf
264	restore_default
265}
266
267atf_init_test_cases()
268{
269	atf_add_test_case o_flag
270	atf_add_test_case d_flag
271	atf_add_test_case volume
272	atf_add_test_case mute
273	atf_add_test_case recsrc
274}
275