git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #102249 from rnhmjoj/firefox-audio

nixos/tests/firefox: add audio subtest

commitfd0b3839b231d688b0780ef973e3eb9611d383eb authorAndreas Rammhold <andreas@rammhold.de> dateSat 31 Oct 2020, 21:23 (6 years ago) parent7d834eff7904ca
1 file changed+85−6
nixos/tests/firefox.nix+85−6
nixos/tests/firefox.nix+85−6
@@ -11,26 +11,105 @@ import ./make-test-python.nix ({ pkgs, esr ? false, ... }: {
1111 environment.systemPackages =
1212 (if esr then [ pkgs.firefox-esr ] else [ pkgs.firefox ])
1313 ++ [ pkgs.xdotool ];
14+
15+ # Need some more memory to record audio.
16+ virtualisation.memorySize = "500";
17+
18+ # Create a virtual sound device, with mixing
19+ # and all, for recording audio.
20+ boot.kernelModules = [ "snd-aloop" ];
21+ sound.enable = true;
22+ sound.extraConfig = ''
23+ pcm.!default {
24+ type plug
25+ slave.pcm pcm.dmixer
26+ }
27+ pcm.dmixer {
28+ type dmix
29+ ipc_key 1
30+ slave {
31+ pcm "hw:Loopback,0,0"
32+ rate 48000
33+ periods 128
34+ period_time 0
35+ period_size 1024
36+ buffer_size 8192
37+ }
38+ }
39+ pcm.recorder {
40+ type hw
41+ card "Loopback"
42+ device 1
43+ subdevice 0
44+ }
45+ '';
46+
47+ systemd.services.audio-recorder = {
48+ description = "Record NixOS test audio to /tmp/record.wav";
49+ script = "${pkgs.alsaUtils}/bin/arecord -D recorder -f S16_LE -r48000 /tmp/record.wav";
50+ };
51+
1452 };
1553
1654 testScript = ''
55+ from contextlib import contextmanager
56+
57+
58+ @contextmanager
59+ def audio_recording(machine: Machine) -> None:
60+ """
61+ Perform actions while recording the
62+ machine audio output.
63+ """
64+ machine.systemctl("start audio-recorder")
65+ yield
66+ machine.systemctl("stop audio-recorder")
67+
68+
69+ def wait_for_sound(machine: Machine) -> None:
70+ """
71+ Wait until any sound has been emitted.
72+ """
73+ machine.wait_for_file("/tmp/record.wav")
74+ while True:
75+ # Get at most 2M of the recording
76+ machine.execute("tail -c 2M /tmp/record.wav > /tmp/last")
77+ # Get the exact size
78+ size = int(machine.succeed("stat -c '%s' /tmp/last").strip())
79+ # Compare it against /dev/zero using `cmp` (skipping 50B of WAVE header).
80+ # If some non-NULL bytes are found it returns 1.
81+ status, output = machine.execute(
82+ f"cmp -i 50 -n {size - 50} /tmp/last /dev/zero 2>&1"
83+ )
84+ if status == 1:
85+ break
86+ machine.sleep(2)
87+
88+
1789 machine.wait_for_x()
1890
19− with subtest("wait until Firefox has finished loading the Valgrind docs page"):
91+ with subtest("Wait until Firefox has finished loading the Valgrind docs page"):
2092 machine.execute(
2193 "xterm -e 'firefox file://${pkgs.valgrind.doc}/share/doc/valgrind/html/index.html' &"
2294 )
2395 machine.wait_for_window("Valgrind")
2496 machine.sleep(40)
2597
98+ with subtest("Check whether Firefox can play sound"):
99+ with audio_recording(machine):
100+ machine.succeed(
101+ "firefox file://${pkgs.sound-theme-freedesktop}/share/sounds/freedesktop/stereo/phone-incoming-call.oga &"
102+ )
103+ wait_for_sound(machine)
104+ machine.copy_from_vm("/tmp/record.wav")
105+
106+ with subtest("Close sound test tab"):
107+ machine.execute("xdotool key ctrl+w")
108+
26109 with subtest("Close default browser prompt"):
27110 machine.execute("xdotool key space")
28111
29− with subtest("Hide default browser window"):
30− machine.sleep(2)
31− machine.execute("xdotool key F12")
32−
33− with subtest("wait until Firefox draws the developer tool panel"):
112+ with subtest("Wait until Firefox draws the developer tool panel"):
34113 machine.sleep(10)
35114 machine.succeed("xwininfo -root -tree | grep Valgrind")
36115 machine.screenshot("screen")