-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtests.bats
196 lines (174 loc) · 5.68 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/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 modify the system's PulseAudio
# setup until it's restarted, so either do that after running the test, or
# launch the tests inside a container (see the Dockerfile in the main
# repository).
#
# The tests can be run with BATS. See the README.md for more info.
function restartPulseaudio() {
if pulseaudio --check; then
echo "Killing PulseAudio"
killall pulseaudio
fi
# Starting and killing PulseAudio is performed asynchronously, so this
# makes sure the requested state is real.
while pgrep pulseaudio &>/dev/null; do :; done
echo "Starting PulseAudio"
pulseaudio --start -D
while ! pgrep pulseaudio &>/dev/null; do :; done
}
# Loading the script and starting pulseaudio if it isn't already
function setup() {
restartPulseaudio
echo "Loading script"
source pulseaudio-control output &>/dev/null
}
@test "nextNode()" {
# This test will only work if there is currently only one sink. It's
# kind of hardcoded to avoid excessive complexity.
numSinks=$(pactl list short sinks | wc -l)
if [ "$numSinks" -ne 1 ]; then
skip
fi
# Testing sink swapping with 8 sinks
for i in {1..15}; do
pactl load-module module-null-sink sink_name="null-sink-$i"
done
pactl 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 `getCurNode` 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.
NODE_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
nextNode
getCurNode
echo "Real sink is $curNode, expected ${order[$((i % ${#order[@]}))]} at iteration $i"
[ "$curNode" -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.
VOLUME_MAX=350
VOLUME_STEP=5
local vol=0
getCurNode
pactl set-sink-volume "$curNode" "$vol%"
for i in {1..100}; do
volUp
getCurVol "$curNode"
if [ "$vol" -lt $VOLUME_MAX ]; then
vol=$((vol + VOLUME_STEP))
fi
echo "Real volume is $VOL_LEVEL, expected $vol"
[ "$VOL_LEVEL" -eq $vol ]
done
}
@test "volDown()" {
# Decreases the volume to 0 step by step, making sure that the results
# are expected.
VOLUME_MAX=350
VOLUME_STEP=5
# It shouldn't matter that the current volume exceeds the maximum volume
local vol=375
getCurNode
pactl set-sink-volume "$curNode" "$vol%"
for i in {1..100}; do
volDown
getCurVol "$curNode"
if [ "$vol" -gt 0 ]; then
vol=$((vol - VOLUME_STEP))
fi
echo "Real volume is $VOL_LEVEL, expected $vol"
[ "$VOL_LEVEL" -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.
getCurNode
local expected="no"
pactl set-sink-mute "$curNode" "$expected"
for i in {1..50}; do
volMute toggle
getIsMuted "$curNode"
if [ "$expected" = "no" ]; then expected="yes"; else expected="no"; fi
echo "Real status is '$IS_MUTED', expected '$expected'"
[ "$IS_MUTED" = "$expected" ]
done
# Testing that muting once or more results in a muted sink
volMute mute
getIsMuted "$curNode"
[ "$IS_MUTED" = "yes" ]
volMute mute
getIsMuted "$curNode"
[ "$IS_MUTED" = "yes" ]
volMute mute
getIsMuted "$curNode"
[ "$IS_MUTED" = "yes" ]
# Same for unmuting
volMute unmute
getIsMuted "$curNode"
[ "$IS_MUTED" = "no" ]
volMute unmute
getIsMuted "$curNode"
[ "$IS_MUTED" = "no" ]
volMute unmute
getIsMuted "$curNode"
[ "$IS_MUTED" = "no" ]
}
@test "getNickname()" {
# The already existing sinks will be ignored.
offset=$(pactl list short sinks | wc -l)
# Testing sink nicknames with 10 null sinks. Only a few of them will
# have a name in the nickname map.
for i in {0..9}; do
pactl load-module module-null-sink sink_name="null-sink-$i"
done
unset NODE_NICKNAMES
declare -A NODE_NICKNAMES
# Checking with an empty map.
for i in {0..9}; do
getNickname "$((i + offset))"
[ "$NODE_NICKNAME" = "Sink #$((i + offset))" ]
done
# Populating part of the map.
NODE_NICKNAMES["does-not-exist"]="null"
for i in {0..4}; do
NODE_NICKNAMES["null-sink-$i"]="Null Sink $i"
getNickname "$((i + offset))"
[ "$NODE_NICKNAME" = "Null Sink $i" ]
done
for i in {5..9}; do
getNickname "$((i + offset))"
[ "$NODE_NICKNAME" = "Sink #$((i + offset))" ]
done
# Testing empty $nodeName.
# Observed to happen when a sink is removed (e.g. Bluetooth disconnect)
# possibly only with unlucky timing of when `getNodeName` runs. cf. #41
function getNodeName() {
nodeName=''
}
getNickname "$((10 + offset))" # beyond what exists
[ "$NODE_NICKNAME" = "Sink #$((10 + offset))" ]
}