forked from marioortizmanero/polybar-pulseaudio-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.bats
146 lines (132 loc) · 4.08 KB
/
tests.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/env bats
# vim: filetype=sh
#
# Polybar PulseAudio Control - tests.bats
#
# Simple test script to make sure the most basic functions in this script
# always work as intended. The tests will temporarily modify your pulseaudio
# setup, but they will be restored to the default after finishing.
# The tests can be run with BATS. See the README.md for more info.
# Loading the script and starting pulseaudio if it isn't already
function setup() {
echo "Loading script"
if ! pulseaudio --check &>/dev/null; then
echo "Starting pulseaudio"
pulseaudio --start -D
fi
source pulseaudio-control.bash --output &>/dev/null
}
function teardown() {
echo "Restarting pulseaudio"
killall pulseaudio
pulseaudio --start -D
}
@test "changeDevice()" {
# This test will only work if there is currently only one sink. It's
# kind of hardcoded to avoid excesive complexity.
pactl list short sinks
if [ "$(pactl list short sinks | wc -l)" -ne 1 ]; then
skip
fi
# Testing sink swapping with 8 sinks
for i in {1..15}; do
pacmd load-module module-null-sink sink_name="null-sink-$i"
done
pacmd set-default-sink 1
# The blacklist has valid and invalid sinks. The switching will be in
# the same order as the array.
# This test assumes that `getCurSink` works properly, and tries it 50
# times. The sink with ID zero must always be ignored, because it's
# reserved to special sinks, and it might cause issues in the test.
SINK_BLACKLIST=(
"null-sink-0"
"null-sink-8"
"null-sink-4"
"null-sink-2"
"null-sink-2"
"null-sink-doesntexist"
"null-sink-300"
"null-sink-noexist"
"null-sink-13"
"null-sink-10"
)
local order=(1 3 5 6 7 9 11 12 14 15)
for i in {1..50}; do
changeDevice
getCurSink
echo "Real sink is $curSink, expected ${order[$((i % ${#order[@]}))]} at iteration $i"
[ "$curSink" -eq "${order[$((i % ${#order[@]}))]}" ]
done
}
@test "volUp()" {
# Increases the volume from zero to a set maximum step by step, making
# sure that the results are expected.
MAX_VOL=350
INC=5
local vol=0
getCurSink
pactl set-sink-volume "$curSink" "$vol%"
for i in {1..100}; do
volUp
getCurVol "$curSink"
if [ "$vol" -lt $MAX_VOL ]; then
vol=$((vol + INC))
fi
echo "Real volume is $curVol, expected $vol"
[ "$curVol" -eq $vol ]
done
}
@test "volDown()" {
# Decreases the volume to 0 step by step, making sure that the results
# are expected.
MAX_VOL=350
INC=5
# It shouldn't matter that the current volume exceeds the maximum volume
local vol=375
getCurSink
pactl set-sink-volume "$curSink" "$vol%"
for i in {1..100}; do
volDown
getCurVol "$curSink"
if [ "$vol" -gt 0 ]; then
vol=$((vol - INC))
fi
echo "Real volume is $curVol, expected $vol"
[ "$curVol" -eq $vol ]
done
}
@test "volMute()" {
# Very simple tests to make sure that volume muting works. The sink starts
# muted, and its state is changed to check that the function doesn't fail.
# First of all, the toggle mode is tested.
getCurSink
local expected="no"
pactl set-sink-mute "$curSink" "$expected"
for i in {1..50}; do
volMute toggle
getIsMuted "$curSink"
if [ "$expected" = "no" ]; then expected="yes"; else expected="no"; fi
echo "Real status is '$isMuted', expected '$expected'"
[ "$isMuted" = "$expected" ]
done
# Testing that muting once or more results in a muted sink
volMute mute
getIsMuted "$curSink"
[ "$isMuted" = "yes" ]
volMute mute
getIsMuted "$curSink"
[ "$isMuted" = "yes" ]
volMute mute
getIsMuted "$curSink"
[ "$isMuted" = "yes" ]
# Same for unmuting
volMute unmute
getIsMuted "$curSink"
[ "$isMuted" = "no" ]
volMute unmute
getIsMuted "$curSink"
[ "$isMuted" = "no" ]
volMute unmute
getIsMuted "$curSink"
[ "$isMuted" = "no" ]
}