[gnome-control-center] tests: Add test for keyboard shortcut normalization



commit 7f6dc4511fe6f0a0f7297be6dca1cd3f034f0521
Author: Sebastian Keller <skeller gnome org>
Date:   Thu Dec 2 13:44:21 2021 +0100

    tests: Add test for keyboard shortcut normalization
    
    This tests if the shortcuts generated from an event are in the format
    that the shell expects.

 .gitlab-ci.yml                           |   1 +
 panels/keyboard/meson.build              |   3 +-
 tests/keyboard/meson.build               |  33 +++++++
 tests/keyboard/test-keyboard-shortcuts.c | 161 +++++++++++++++++++++++++++++++
 tests/keyboard/test-keyboard.py          |  46 +++++++++
 tests/meson.build                        |   1 +
 6 files changed, 244 insertions(+), 1 deletion(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index e5a0fbbc8..83fa9500f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -222,6 +222,7 @@ test:
     - build
 
   script:
+    - dnf -y install setxkbmap
     - *environment_information
     - *run_tests
 
diff --git a/panels/keyboard/meson.build b/panels/keyboard/meson.build
index 6782ead76..cb886148a 100644
--- a/panels/keyboard/meson.build
+++ b/panels/keyboard/meson.build
@@ -94,12 +94,13 @@ if enable_ibus
   deps += ibus_dep
 endif
 
-panels_libs += static_library(
+keyboard_panel_lib = static_library(
   cappletname,
   sources: sources,
   include_directories: [top_inc, common_inc],
   dependencies: deps,
   c_args: cflags
 )
+panels_libs += keyboard_panel_lib
 
 subdir('icons')
diff --git a/tests/keyboard/meson.build b/tests/keyboard/meson.build
new file mode 100644
index 000000000..4b724b4b6
--- /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 000000000..37d86134a
--- /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 000000000..f6295f2a4
--- /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))
diff --git a/tests/meson.build b/tests/meson.build
index d4fe361ef..69667acb6 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -8,3 +8,4 @@ subdir('interactive-panels')
 
 subdir('printers')
 subdir('info')
+subdir('keyboard')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]