[gtk+/gtk-2-24] Bug 633291 - Handle Shift-keys in X11 gdk_test_simulate_key()



commit fa2d05ab4e9fa60d9f790a09e55679a00ed700d1
Author: Martin Nordholts <martinn src gnome org>
Date:   Tue Feb 1 23:07:21 2011 +0100

    Bug 633291 - Handle Shift-keys in X11 gdk_test_simulate_key()
    
    If a level 1 key maps to a key value passed to
    gdk_test_simulate_key(), raise the GDK_SHIFT_MASK flag so the reqested
    key value is generated. Also add a regression test for this fix.

 gdk/x11/gdktestutils-x11.c |    9 +++++++--
 gtk/tests/testing.c        |   41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 2 deletions(-)
---
diff --git a/gdk/x11/gdktestutils-x11.c b/gdk/x11/gdktestutils-x11.c
index 13677b6..343a2dd 100644
--- a/gdk/x11/gdktestutils-x11.c
+++ b/gdk/x11/gdktestutils-x11.c
@@ -129,12 +129,17 @@ gdk_test_simulate_key (GdkWindow      *window,
     {
       gint i;
       for (i = 0; i < n_keys; i++)
-        if (keys[i].group == 0 && keys[i].level == 0)
+        if (keys[i].group == 0 && (keys[i].level == 0 || keys[i].level == 1))
           {
             xev.keycode = keys[i].keycode;
+            if (keys[i].level == 1)
+              {
+                /* Assume shift takes us to level 1 */
+                xev.state |= GDK_SHIFT_MASK;
+              }
             break;
           }
-      if (i >= n_keys) /* no match for group==0 and level==0 */
+      if (i >= n_keys) /* no match for group==0 and level==0 or 1 */
         xev.keycode = keys[0].keycode;
     }
   g_free (keys);
diff --git a/gtk/tests/testing.c b/gtk/tests/testing.c
index 43ee25f..bfdf20b 100644
--- a/gtk/tests/testing.c
+++ b/gtk/tests/testing.c
@@ -22,6 +22,12 @@
 #include <string.h>
 #include <math.h>
 
+/* Use a keyval that requires Shift to be active (in typical layouts)
+ * like GDK_KEY_ampersand, which is '<shift>6'
+ */
+#define KEYVAL_THAT_REQUIRES_SHIFT GDK_KEY_ampersand
+
+
 /* --- test functions --- */
 static void
 test_button_clicks (void)
@@ -71,6 +77,40 @@ test_button_keys (void)
   g_assert (c == 0);
 }
 
+static gboolean
+store_last_key_release (GtkWidget   *widget,
+                        GdkEventKey *event,
+                        gpointer     user_data)
+{
+  *((gint *)user_data) = event->keyval;
+  return FALSE;
+}
+
+static void
+test_send_shift_key (void)
+{
+  GtkWidget *window = gtk_test_display_button_window ("Test Window",
+                                                      "Test: test_send_shift_key()",
+                                                      "IgnoreMe1", NULL,
+                                                      "SendMeKeys", NULL,
+                                                      "IgnoreMe2", NULL,
+                                                      NULL);
+  GtkWidget *button = gtk_test_find_widget (window, "SendMeKeys", GTK_TYPE_BUTTON);
+  gint last_key_release = 0;
+  gboolean simsuccess;
+  g_assert (button != NULL);
+  g_signal_connect (button, "key-release-event",
+                    G_CALLBACK (store_last_key_release),
+                    &last_key_release);
+  gtk_widget_grab_focus (button);
+  g_assert (gtk_widget_has_focus (button));
+  simsuccess = gtk_test_widget_send_key (button, KEYVAL_THAT_REQUIRES_SHIFT, 0 /*modifiers*/);
+  g_assert (simsuccess == TRUE);
+  while (gtk_events_pending ())
+    gtk_main_iteration ();
+  g_assert_cmpint (KEYVAL_THAT_REQUIRES_SHIFT, ==, last_key_release);
+}
+
 static void
 test_slider_ranges (void)
 {
@@ -245,6 +285,7 @@ main (int   argc,
   g_test_add_func ("/ui-tests/text-access", test_text_access);
   g_test_add_func ("/ui-tests/button-clicks", test_button_clicks);
   g_test_add_func ("/ui-tests/keys-events", test_button_keys);
+  g_test_add_func ("/ui-tests/send-shift-key", test_send_shift_key);
   g_test_add_func ("/ui-tests/slider-ranges", test_slider_ranges);
   g_test_add_func ("/ui-tests/xserver-sync", test_xserver_sync);
   g_test_add_func ("/ui-tests/spin-button-arrows", test_spin_button_arrows);



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