1import ./make-test-python.nix ({ pkgs, lib, ... }: {
2 name = "sway";
3 meta = {
4 maintainers = with lib.maintainers; [ primeos synthetica ];
5 };
6
7 # testScriptWithTypes:49: error: Cannot call function of unknown type
8 # (machine.succeed if succeed else machine.execute)(
9 # ^
10 # Found 1 error in 1 file (checked 1 source file)
11 skipTypeCheck = true;
12
13 nodes.machine = { config, ... }: {
14 # Automatically login on tty1 as a normal user:
15 imports = [ ./common/user-account.nix ];
16 services.getty.autologinUser = "alice";
17
18 environment = {
19 # For glinfo and wayland-info:
20 systemPackages = with pkgs; [ mesa-demos wayland-utils alacritty ];
21 # Use a fixed SWAYSOCK path (for swaymsg):
22 variables = {
23 "SWAYSOCK" = "/tmp/sway-ipc.sock";
24 # TODO: Investigate if we can get hardware acceleration to work (via
25 # virtio-gpu and Virgil). We currently have to use the Pixman software
26 # renderer since the GLES2 renderer doesn't work inside the VM (even
27 # with WLR_RENDERER_ALLOW_SOFTWARE):
28 # "WLR_RENDERER_ALLOW_SOFTWARE" = "1";
29 "WLR_RENDERER" = "pixman";
30 };
31 # For convenience:
32 shellAliases = {
33 test-x11 = "glinfo | tee /tmp/test-x11.out && touch /tmp/test-x11-exit-ok";
34 test-wayland = "wayland-info | tee /tmp/test-wayland.out && touch /tmp/test-wayland-exit-ok";
35 };
36
37 # To help with OCR:
38 etc."xdg/foot/foot.ini".text = lib.generators.toINI { } {
39 main = {
40 font = "inconsolata:size=14";
41 };
42 colors = rec {
43 foreground = "000000";
44 background = "ffffff";
45 regular2 = foreground;
46 };
47 };
48
49 etc."gpg-agent.conf".text = ''
50 pinentry-timeout 86400
51 '';
52 };
53
54 fonts.packages = [ pkgs.inconsolata ];
55
56 # Automatically configure and start Sway when logging in on tty1:
57 programs.bash.loginShellInit = ''
58 if [ "$(tty)" = "/dev/tty1" ]; then
59 set -e
60
61 mkdir -p ~/.config/sway
62 sed s/Mod4/Mod1/ /etc/sway/config > ~/.config/sway/config
63
64 sway --validate
65 sway && touch /tmp/sway-exit-ok
66 fi
67 '';
68
69 programs.sway.enable = true;
70
71 # To test pinentry via gpg-agent:
72 programs.gnupg.agent.enable = true;
73
74 # Need to switch to a different GPU driver than the default one (-vga std) so that Sway can launch:
75 virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci" ];
76 };
77
78 testScript = { nodes, ... }: ''
79 import shlex
80 import json
81
82 q = shlex.quote
83 NODE_GROUPS = ["nodes", "floating_nodes"]
84
85
86 def swaymsg(command: str = "", succeed=True, type="command"):
87 assert command != "" or type != "command", "Must specify command or type"
88 shell = q(f"swaymsg -t {q(type)} -- {q(command)}")
89 with machine.nested(
90 f"sending swaymsg {shell!r}" + " (allowed to fail)" * (not succeed)
91 ):
92 ret = (machine.succeed if succeed else machine.execute)(
93 f"su - alice -c {shell}"
94 )
95
96 # execute also returns a status code, but disregard.
97 if not succeed:
98 _, ret = ret
99
100 if not succeed and not ret:
101 return None
102
103 parsed = json.loads(ret)
104 return parsed
105
106
107 def walk(tree):
108 yield tree
109 for group in NODE_GROUPS:
110 for node in tree.get(group, []):
111 yield from walk(node)
112
113
114 def wait_for_window(pattern):
115 def func(last_chance):
116 nodes = (node["name"] for node in walk(swaymsg(type="get_tree")))
117
118 if last_chance:
119 nodes = list(nodes)
120 machine.log(f"Last call! Current list of windows: {nodes}")
121
122 return any(pattern in name for name in nodes)
123
124 retry(func)
125
126 start_all()
127 machine.wait_for_unit("multi-user.target")
128
129 # To check the version:
130 print(machine.succeed("sway --version"))
131
132 # Wait for Sway to complete startup:
133 machine.wait_for_file("/run/user/1000/wayland-1")
134 machine.wait_for_file("/tmp/sway-ipc.sock")
135
136 # Test XWayland (foot does not support X):
137 swaymsg("exec WINIT_UNIX_BACKEND=x11 WAYLAND_DISPLAY=invalid alacritty")
138 wait_for_window("alice@machine")
139 machine.send_chars("test-x11\n")
140 machine.wait_for_file("/tmp/test-x11-exit-ok")
141 print(machine.succeed("cat /tmp/test-x11.out"))
142 machine.copy_from_vm("/tmp/test-x11.out")
143 machine.screenshot("alacritty_glinfo")
144 machine.succeed("pkill alacritty")
145
146 # Start a terminal (foot) on workspace 3:
147 machine.send_key("alt-3")
148 machine.sleep(3)
149 machine.send_key("alt-ret")
150 wait_for_window("alice@machine")
151 machine.send_chars("test-wayland\n")
152 machine.wait_for_file("/tmp/test-wayland-exit-ok")
153 print(machine.succeed("cat /tmp/test-wayland.out"))
154 machine.copy_from_vm("/tmp/test-wayland.out")
155 machine.screenshot("foot_wayland_info")
156 machine.send_key("alt-shift-q")
157 machine.wait_until_fails("pgrep foot")
158
159 # Test gpg-agent starting pinentry-gnome3 via D-Bus (tests if
160 # $WAYLAND_DISPLAY is correctly imported into the D-Bus user env):
161 swaymsg("exec mkdir -p ~/.gnupg")
162 swaymsg("exec cp /etc/gpg-agent.conf ~/.gnupg")
163
164 swaymsg("exec DISPLAY=INVALID gpg --no-tty --yes --quick-generate-key test", succeed=False)
165 machine.wait_until_succeeds("pgrep --exact gpg")
166 wait_for_window("gpg")
167 machine.succeed("pgrep --exact gpg")
168 machine.screenshot("gpg_pinentry")
169 machine.send_key("alt-shift-q")
170 machine.wait_until_fails("pgrep --exact gpg")
171
172 # Test swaynag:
173 def get_height():
174 return [node['rect']['height'] for node in walk(swaymsg(type="get_tree")) if node['focused']][0]
175
176 before = get_height()
177 machine.send_key("alt-shift-e")
178 retry(lambda _: get_height() < before)
179 machine.screenshot("sway_exit")
180
181 swaymsg("exec swaylock")
182 machine.wait_until_succeeds("pgrep -x swaylock")
183 machine.sleep(3)
184 machine.send_chars("${nodes.machine.config.users.users.alice.password}")
185 machine.send_key("ret")
186 machine.wait_until_fails("pgrep -x swaylock")
187
188 # Exit Sway and verify process exit status 0:
189 swaymsg("exit", succeed=False)
190 machine.wait_until_fails("pgrep -x sway")
191 machine.wait_for_file("/tmp/sway-exit-ok")
192 '';
193})