vuls: init at 0.27.0
[NixPkgs.git] / nixos / tests / firefox.nix
blob8243defbb9f2e040c4aedabdcd2f67670723537c
1 import ./make-test-python.nix ({ lib, pkgs, firefoxPackage, ... }:
3   name = firefoxPackage.pname;
5   meta = with pkgs.lib.maintainers; {
6     maintainers = [ shlevy ];
7   };
9   nodes.machine =
10     { pkgs, ... }:
12     { imports = [ ./common/x11.nix ];
13       environment.systemPackages = [ pkgs.xdotool ];
15       programs.firefox = {
16         enable = true;
17         preferences."media.autoplay.default" = 0;
18         package = firefoxPackage;
19       };
21       # Create a virtual sound device, with mixing
22       # and all, for recording audio.
23       boot.kernelModules = [ "snd-aloop" ];
24       environment.etc."asound.conf".text = ''
25         pcm.!default {
26           type plug
27           slave.pcm pcm.dmixer
28         }
29         pcm.dmixer {
30           type dmix
31           ipc_key 1
32           slave {
33             pcm "hw:Loopback,0,0"
34             rate 48000
35             periods 128
36             period_time 0
37             period_size 1024
38             buffer_size 8192
39           }
40         }
41         pcm.recorder {
42           type hw
43           card "Loopback"
44           device 1
45           subdevice 0
46         }
47       '';
49       systemd.services.audio-recorder = {
50         description = "Record NixOS test audio to /tmp/record.wav";
51         script = "${pkgs.alsa-utils}/bin/arecord -D recorder -f S16_LE -r48000 /tmp/record.wav";
52       };
54     };
56   testScript = let
57     exe = lib.getExe firefoxPackage;
58   in ''
59       from contextlib import contextmanager
62       @contextmanager
63       def record_audio(machine: Machine):
64           """
65           Perform actions while recording the
66           machine audio output.
67           """
68           machine.systemctl("start audio-recorder")
69           yield
70           machine.systemctl("stop audio-recorder")
73       def wait_for_sound(machine: Machine):
74           """
75           Wait until any sound has been emitted.
76           """
77           machine.wait_for_file("/tmp/record.wav")
78           while True:
79               # Get at most 2M of the recording
80               machine.execute("tail -c 2M /tmp/record.wav > /tmp/last")
81               # Get the exact size
82               size = int(machine.succeed("stat -c '%s' /tmp/last").strip())
83               # Compare it against /dev/zero using `cmp` (skipping 50B of WAVE header).
84               # If some non-NULL bytes are found it returns 1.
85               status, output = machine.execute(
86                   f"cmp -i 50 -n {size - 50} /tmp/last /dev/zero 2>&1"
87               )
88               if status == 1:
89                   break
90               machine.sleep(2)
93       machine.wait_for_x()
95       with subtest("Wait until Firefox has finished loading the Valgrind docs page"):
96           machine.execute(
97               "xterm -e '${exe} file://${pkgs.valgrind.doc}/share/doc/valgrind/html/index.html' >&2 &"
98           )
99           machine.wait_for_window("Valgrind")
100           machine.sleep(40)
102       with subtest("Check whether Firefox can play sound"):
103           with record_audio(machine):
104               machine.succeed(
105                   "${exe} file://${pkgs.sound-theme-freedesktop}/share/sounds/freedesktop/stereo/phone-incoming-call.oga >&2 &"
106               )
107               wait_for_sound(machine)
108           machine.copy_from_vm("/tmp/record.wav")
110       with subtest("Close sound test tab"):
111           machine.execute("xdotool key ctrl+w")
113       with subtest("Close default browser prompt"):
114           machine.execute("xdotool key space")
116       with subtest("Wait until Firefox draws the developer tool panel"):
117           machine.sleep(10)
118           machine.succeed("xwininfo -root -tree | grep Valgrind")
119           machine.screenshot("screen")
120     '';