python310Packages.pydeconz: 104 -> 105
[NixPkgs.git] / nixos / tests / keymap.nix
blob4306a9ae2cf9408e1d9cd8e33f1f7211914aaf2c
1 { system ? builtins.currentSystem,
2   config ? {},
3   pkgs ? import ../.. { inherit system config; }
4 }:
6 with import ../lib/testing-python.nix { inherit system pkgs; };
8 let
9   readyFile  = "/tmp/readerReady";
10   resultFile = "/tmp/readerResult";
12   testReader = pkgs.writeScript "test-input-reader" ''
13     rm -f ${resultFile} ${resultFile}.tmp
14     logger "testReader: START: Waiting for $1 characters, expecting '$2'."
15     touch ${readyFile}
16     read -r -N $1 chars
17     rm -f ${readyFile}
19     if [ "$chars" == "$2" ]; then
20       logger -s "testReader: PASS: Got '$2' as expected." 2>${resultFile}.tmp
21     else
22       logger -s "testReader: FAIL: Expected '$2' but got '$chars'." 2>${resultFile}.tmp
23     fi
24     # rename after the file is written to prevent a race condition
25     mv  ${resultFile}.tmp ${resultFile}
26   '';
29   mkKeyboardTest = layout: { extraConfig ? {}, tests }: with pkgs.lib; makeTest {
30     name = "keymap-${layout}";
32     machine.console.keyMap = mkOverride 900 layout;
33     machine.services.xserver.desktopManager.xterm.enable = false;
34     machine.services.xserver.layout = mkOverride 900 layout;
35     machine.imports = [ ./common/x11.nix extraConfig ];
37     testScript = ''
38       import json
39       import shlex
42       def run_test_case(cmd, xorg_keymap, test_case_name, inputs, expected):
43           with subtest(test_case_name):
44               assert len(inputs) == len(expected)
45               machine.execute("rm -f ${readyFile} ${resultFile}")
47               # set up process that expects all the keys to be entered
48               machine.succeed(
49                   "{} {} {} {} >&2 &".format(
50                       cmd,
51                       "${testReader}",
52                       len(inputs),
53                       shlex.quote("".join(expected)),
54                   )
55               )
57               if xorg_keymap:
58                   # make sure the xterm window is open and has focus
59                   machine.wait_for_window("testterm")
60                   machine.wait_until_succeeds(
61                       "${pkgs.xdotool}/bin/xdotool search --sync --onlyvisible "
62                       "--class testterm windowfocus --sync"
63                   )
65               # wait for reader to be ready
66               machine.wait_for_file("${readyFile}")
67               machine.sleep(1)
69               # send all keys
70               for key in inputs:
71                   machine.send_key(key)
73               # wait for result and check
74               machine.wait_for_file("${resultFile}")
75               machine.succeed("grep -q 'PASS:' ${resultFile}")
78       with open("${pkgs.writeText "tests.json" (builtins.toJSON tests)}") as json_file:
79           tests = json.load(json_file)
81       keymap_environments = {
82           "VT Keymap": "openvt -sw --",
83           "Xorg Keymap": "DISPLAY=:0 xterm -title testterm -class testterm -fullscreen -e",
84       }
86       machine.wait_for_x()
88       for keymap_env_name, command in keymap_environments.items():
89           with subtest(keymap_env_name):
90               for test_case_name, test_data in tests.items():
91                   run_test_case(
92                       command,
93                       False,
94                       test_case_name,
95                       test_data["qwerty"],
96                       test_data["expect"],
97                   )
98     '';
99   };
101 in pkgs.lib.mapAttrs mkKeyboardTest {
102   azerty = {
103     tests = {
104       azqw.qwerty = [ "q" "w" ];
105       azqw.expect = [ "a" "z" ];
106       altgr.qwerty = [ "alt_r-2" "alt_r-3" "alt_r-4" "alt_r-5" "alt_r-6" ];
107       altgr.expect = [ "~"       "#"       "{"       "["       "|"       ];
108     };
110     extraConfig.console.keyMap = "fr";
111     extraConfig.services.xserver.layout = "fr";
112   };
114   bone = {
115     tests = {
116       layer1.qwerty = [ "f"           "j"                     ];
117       layer1.expect = [ "e"           "n"                     ];
118       layer2.qwerty = [ "shift-f"     "shift-j"     "shift-6" ];
119       layer2.expect = [ "E"           "N"           "$"       ];
120       layer3.qwerty = [ "caps_lock-d" "caps_lock-f"           ];
121       layer3.expect = [ "{"           "}"                     ];
122     };
124     extraConfig.console.keyMap = "bone";
125     extraConfig.services.xserver.layout = "de";
126     extraConfig.services.xserver.xkbVariant = "bone";
127   };
129   colemak = {
130     tests = {
131       homerow.qwerty = [ "a" "s" "d" "f" "j" "k" "l" "semicolon" ];
132       homerow.expect = [ "a" "r" "s" "t" "n" "e" "i" "o"         ];
133     };
135     extraConfig.console.keyMap = "colemak";
136     extraConfig.services.xserver.layout = "us";
137     extraConfig.services.xserver.xkbVariant = "colemak";
138   };
140   dvorak = {
141     tests = {
142       homerow.qwerty = [ "a" "s" "d" "f" "j" "k" "l" "semicolon" ];
143       homerow.expect = [ "a" "o" "e" "u" "h" "t" "n" "s"         ];
144       symbols.qwerty = [ "q" "w" "e" "minus" "equal" ];
145       symbols.expect = [ "'" "," "." "["     "]"     ];
146     };
148     extraConfig.console.keyMap = "dvorak";
149     extraConfig.services.xserver.layout = "us";
150     extraConfig.services.xserver.xkbVariant = "dvorak";
151   };
153   dvorak-programmer = {
154     tests = {
155       homerow.qwerty = [ "a" "s" "d" "f" "j" "k" "l" "semicolon" ];
156       homerow.expect = [ "a" "o" "e" "u" "h" "t" "n" "s"         ];
157       numbers.qwerty = map (x: "shift-${x}")
158                        [ "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "minus" ];
159       numbers.expect = [ "%" "7" "5" "3" "1" "9" "0" "2" "4" "6" "8" ];
160       symbols.qwerty = [ "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "minus" ];
161       symbols.expect = [ "&" "[" "{" "}" "(" "=" "*" ")" "+" "]" "!" ];
162     };
164     extraConfig.console.keyMap = "dvorak-programmer";
165     extraConfig.services.xserver.layout = "us";
166     extraConfig.services.xserver.xkbVariant = "dvp";
167   };
169   neo = {
170     tests = {
171       layer1.qwerty = [ "f"           "j"                     ];
172       layer1.expect = [ "e"           "n"                     ];
173       layer2.qwerty = [ "shift-f"     "shift-j"     "shift-6" ];
174       layer2.expect = [ "E"           "N"           "$"       ];
175       layer3.qwerty = [ "caps_lock-d" "caps_lock-f"           ];
176       layer3.expect = [ "{"           "}"                     ];
177     };
179     extraConfig.console.keyMap = "neo";
180     extraConfig.services.xserver.layout = "de";
181     extraConfig.services.xserver.xkbVariant = "neo";
182   };
184   qwertz = {
185     tests = {
186       zy.qwerty = [ "z" "y" ];
187       zy.expect = [ "y" "z" ];
188       altgr.qwerty = map (x: "alt_r-${x}")
189                      [ "q" "less" "7" "8" "9" "0" ];
190       altgr.expect = [ "@" "|"    "{" "[" "]" "}" ];
191     };
193     extraConfig.console.keyMap = "de";
194     extraConfig.services.xserver.layout = "de";
195   };