diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:45:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:45:20 +0000 |
commit | ae1c76ff830d146d41e88d6fba724c0a54bce868 (patch) | |
tree | 3c354bec95af07be35fc71a4b738268496f1a1c4 /tests/keyboard | |
parent | Initial commit. (diff) | |
download | gnome-control-center-upstream/1%43.6.tar.xz gnome-control-center-upstream/1%43.6.zip |
Adding upstream version 1:43.6.upstream/1%43.6upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/keyboard')
-rw-r--r-- | tests/keyboard/meson.build | 33 | ||||
-rw-r--r-- | tests/keyboard/test-keyboard-shortcuts.c | 161 | ||||
-rw-r--r-- | tests/keyboard/test-keyboard.py | 46 |
3 files changed, 240 insertions, 0 deletions
diff --git a/tests/keyboard/meson.build b/tests/keyboard/meson.build new file mode 100644 index 0000000..4b724b4 --- /dev/null +++ b/tests/keyboard/meson.build @@ -0,0 +1,33 @@ +test_units = [ + 'test-keyboard-shortcuts' +] + +env = [ + 'G_MESSAGES_DEBUG=all', + 'BUILDDIR=' + meson.current_build_dir(), + 'TOP_BUILDDIR=' + meson.build_root(), +# Disable ATK, this should not be required but it caused CI failures -- 2018-12-07 + 'NO_AT_BRIDGE=1' +] +cflags = [ + '-DTEST_SRCDIR="@0@"'.format(meson.current_source_dir()) +] +includes = [top_inc, include_directories('../../panels/keyboard')] + +foreach unit: test_units + exe = executable( + unit, + [unit + '.c'], + dependencies : common_deps, + include_directories : includes, + link_with : [keyboard_panel_lib], + c_args : cflags + ) +endforeach + +test( + 'test-keyboard', + find_program('test-keyboard.py'), + env : env, + timeout : 60 +) diff --git a/tests/keyboard/test-keyboard-shortcuts.c b/tests/keyboard/test-keyboard-shortcuts.c new file mode 100644 index 0000000..37d8613 --- /dev/null +++ b/tests/keyboard/test-keyboard-shortcuts.c @@ -0,0 +1,161 @@ +#include <gdk/gdk.h> +#include <gtk/gtk.h> +#include <locale.h> +#include <stdlib.h> +#include "keyboard-shortcuts.h" + +#define NUM_LAYOUTS 4 + +typedef struct _shortcut_test { + guint16 keycode; + GdkModifierType modifiers; + char *expected_results[NUM_LAYOUTS]; +} shortcut_test; + +/* keycodes taken from /usr/share/X11/xkb/keycodes/evdev */ +shortcut_test shortcut_tests[] = { + { + 24 /* first key after tab in first letter row */, + GDK_SHIFT_MASK | GDK_SUPER_MASK, + { + "<Shift><Super>q" /* us */, + "<Shift><Super>apostrophe" /* us+dvorak */, + "<Shift><Super>a" /* fr+azerty */, + "<Shift><Super>Cyrillic_shorti" /* ru */ + /* "<Shift><Super>q" would be valid, too */, + }, + }, + { + 13 /* fifth key in num row */, + GDK_SUPER_MASK, + { + "<Super>4" /* us */, + "<Super>4" /* us+dvorak */, + "<Super>4" /* fr+azerty */, + "<Super>4" /* ru */, + }, + }, + { + 13 /* fifth key in num row */, + GDK_SHIFT_MASK | GDK_SUPER_MASK, + { + "<Shift><Super>4" /* us */, + "<Shift><Super>4" /* us+dvorak */, + "<Shift><Super>4" /* fr+azerty */, + "<Shift><Super>4" /* ru */, + }, + }, + { + 65 /* space key */, + GDK_SHIFT_MASK | GDK_SUPER_MASK, + { + "<Shift><Super>space" /* us */, + "<Shift><Super>space" /* us+dvorak */, + "<Shift><Super>space" /* fr+azerty */, + "<Shift><Super>space" /* ru */, + }, + }, + { + 23 /* tab key */, + GDK_SHIFT_MASK | GDK_SUPER_MASK, + { + "<Shift><Super>Tab" /* us */, + "<Shift><Super>Tab" /* us+dvorak */, + "<Shift><Super>Tab" /* fr+azerty */, + "<Shift><Super>Tab" /* ru */, + }, + }, + { + 107 /* print screen/sysrq key */, + GDK_ALT_MASK, + { + "<Alt>Print" /* us */, + "<Alt>Print" /* us+dvorak */, + "<Alt>Print" /* fr+azerty */, + "<Alt>Print" /* ru */, + }, + }, +}; + +static void +test_event_translation (shortcut_test *shortcut_test) +{ + g_autofree char *translated_name = NULL; + guint keyval; + GdkModifierType modifiers; + + for (int group = 0; group < NUM_LAYOUTS; group++) + { + if (!shortcut_test->expected_results[group]) + continue; + + normalize_keyval_and_mask (shortcut_test->keycode, + shortcut_test->modifiers, + group, + &keyval, + &modifiers); + + translated_name = gtk_accelerator_name (keyval, modifiers); + + if (g_strcmp0 (translated_name, shortcut_test->expected_results[group]) != 0) + { + g_error ("Result for keycode %u with modifieres %u for " + "group %d doesn't match '%s' (got: '%s')", + shortcut_test->keycode, + shortcut_test->modifiers, + group, + shortcut_test->expected_results[group], + translated_name); + g_test_fail (); + } + } +} + +static void +set_keyboard_layouts (char *layouts, + char *variants, + char *options) +{ + GSubprocess *proc; + GError *error = NULL; + + proc = g_subprocess_new (G_SUBPROCESS_FLAGS_NONE, + &error, + "setxkbmap", + "-layout", layouts, + "-variant", variants, + "-option", options, + "-model", "pc105", + NULL); + + if (!proc || !g_subprocess_wait_check(proc, NULL, &error)) + { + g_critical ("Failed to set layout: %s", error->message); + exit (1); + } + + g_object_unref (proc); +} + +static void +run_shortcut_tests (void) +{ + set_keyboard_layouts ("us,us,fr,ru", ",dvorak,azerty,", ""); + + for (int i = 0; i < G_N_ELEMENTS(shortcut_tests); i++) + test_event_translation (&shortcut_tests[i]); +} + +int main (int argc, char **argv) +{ + g_setenv ("GSETTINGS_BACKEND", "memory", TRUE); + g_setenv ("GDK_BACKEND", "x11", TRUE); + g_setenv ("LC_ALL", "C", TRUE); + + gtk_test_init (&argc, &argv, NULL); + + g_test_add_func ("/keyboard/shortcut-translation", + run_shortcut_tests); + + return g_test_run (); +} diff --git a/tests/keyboard/test-keyboard.py b/tests/keyboard/test-keyboard.py new file mode 100644 index 0000000..f6295f2 --- /dev/null +++ b/tests/keyboard/test-keyboard.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# Copyright © 2018 Red Hat, Inc +# 2021 Sebastian Keller +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see <http://www.gnu.org/licenses/>. +# +# Authors: Benjamin Berg <bberg@redhat.com> +# Sebastian Keller <skeller@gnome.org> + +import os +import sys +import unittest + +try: + import dbusmock +except ImportError: + sys.stderr.write('You need python-dbusmock (http://pypi.python.org/pypi/python-dbusmock) for this test suite.\n') + sys.exit(1) + +# Add the shared directory to the search path +sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'shared')) + +from gtest import GTest +from x11session import X11SessionTestCase + +BUILDDIR = os.environ.get('BUILDDIR', os.path.join(os.path.dirname(__file__))) + + +class PanelTestCase(X11SessionTestCase, GTest): + g_test_exe = os.path.join(BUILDDIR, 'test-keyboard-shortcuts') + + +if __name__ == '__main__': + # avoid writing to stderr + unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2)) |