gpointing-device-settings r200 - in trunk: src test



Author: hiikezoe
Date: Wed Mar 11 03:18:46 2009
New Revision: 200
URL: http://svn.gnome.org/viewvc/gpointing-device-settings?rev=200&view=rev

Log:
Handle X error.

Modified:
   trunk/src/gpds-xinput.c
   trunk/src/gpds-xinput.h
   trunk/test/test-xinput.c

Modified: trunk/src/gpds-xinput.c
==============================================================================
--- trunk/src/gpds-xinput.c	(original)
+++ trunk/src/gpds-xinput.c	Wed Mar 11 03:18:46 2009
@@ -195,6 +195,34 @@
     return priv->device;
 }
 
+static gchar *
+get_x_error_text (int x_error_code)
+{
+    gchar buf[64];
+
+    XGetErrorText(GDK_DISPLAY(), x_error_code, buf, sizeof(buf) - 1);
+
+    return g_strdup(buf);
+}
+
+static void
+set_x_error (GError **error , int x_error_code)
+{
+    gchar *error_message;
+
+    if (!error || x_error_code == 0)
+        return;
+
+    error_message = get_x_error_text(x_error_code);
+
+    g_set_error(error,
+                GPDS_XINPUT_ERROR,
+                GPDS_XINPUT_ERROR_X_ERROR,
+                _("An X error occurred. The error was %s."),
+                error_message);
+    g_free(error_message);
+}
+
 gboolean
 gpds_xinput_set_int_properties_by_name_with_format_type 
                                (GpdsXInput *xinput,
@@ -206,7 +234,7 @@
 {
     XDevice *device;
     Atom property_atom;
-    gint i;
+    gint i, x_error_code;
     gchar *property_data;
 
     g_return_val_if_fail(GPDS_IS_XINPUT(xinput), FALSE);
@@ -251,7 +279,11 @@
                           XA_INTEGER, format_type, PropModeReplace,
                           (unsigned char*)property_data, n_properties);
     gdk_flush();
-    gdk_error_trap_pop();
+    x_error_code = gdk_error_trap_pop();
+    if (x_error_code != 0) {
+        set_x_error(error, x_error_code);
+        return FALSE;
+    }
 
     g_free(property_data);
 
@@ -382,6 +414,7 @@
     unsigned char *data, *data_position;
     gulong i;
     gint *int_values;
+    gint x_error_code;
     Status status;
 
     g_return_val_if_fail(GPDS_IS_XINPUT(xinput), FALSE);
@@ -399,10 +432,11 @@
                                  XA_INTEGER, &actual_type, &actual_format,
                                  n_values, &bytes_after, &data);
     gdk_flush();
-    gdk_error_trap_pop();
-
-    if (status != Success)
+    x_error_code = gdk_error_trap_pop();
+    if (status != Success || x_error_code != 0) {
+        set_x_error(error, x_error_code);
         return FALSE;
+    }
 
     if (actual_type != XA_INTEGER) {
         XFree(data);
@@ -464,7 +498,7 @@
 {
     XDevice *device;
     Atom float_atom, property_atom;
-    gint i;
+    gint i, x_error_code;
     gfloat *property_data;
 
     g_return_val_if_fail(GPDS_IS_XINPUT(xinput), FALSE);
@@ -489,7 +523,11 @@
                           float_atom, 32, PropModeReplace,
                           (unsigned char*)property_data, n_properties);
     gdk_flush();
-    gdk_error_trap_pop();
+    x_error_code = gdk_error_trap_pop();
+    if (x_error_code != 0) {
+        set_x_error(error, x_error_code);
+        return FALSE;
+    }
 
     g_free(property_data);
 
@@ -532,6 +570,7 @@
     unsigned long bytes_after;
     unsigned char *data, *data_position;
     gulong i;
+    gint x_error_code;
     gdouble *double_values;
     Status status;
 
@@ -554,10 +593,11 @@
                                  float_atom, &actual_type, &actual_format,
                                  n_properties, &bytes_after, &data);
     gdk_flush();
-    gdk_error_trap_pop();
-
-    if (status != Success)
+    x_error_code = gdk_error_trap_pop();
+    if (status != Success || x_error_code != 0) {
+        set_x_error(error, x_error_code);
         return FALSE;
+    }
 
     if (actual_type != float_atom) {
         XFree(data);

Modified: trunk/src/gpds-xinput.h
==============================================================================
--- trunk/src/gpds-xinput.h	(original)
+++ trunk/src/gpds-xinput.h	Wed Mar 11 03:18:46 2009
@@ -35,7 +35,8 @@
 
 typedef enum
 {
-    GPDS_XINPUT_ERROR_NO_REGISTERED_PROPERTY
+    GPDS_XINPUT_ERROR_NO_REGISTERED_PROPERTY,
+    GPDS_XINPUT_ERROR_X_ERROR
 } GpdsXInputError;
 
 typedef struct _GpdsXInputPropertyEntry GpdsXInputPropertyEntry;

Modified: trunk/test/test-xinput.c
==============================================================================
--- trunk/test/test-xinput.c	(original)
+++ trunk/test/test-xinput.c	Wed Mar 11 03:18:46 2009
@@ -4,12 +4,41 @@
 
 void test_new (void);
 void test_device_name (void);
+void test_set_int_properties_by_name (void);
 void test_set_int_properties (void);
+void test_register_property_entries (void);
+void test_set_int_properties_by_name_invalid_format_type (void);
+void test_set_int_properties_invalid_n_values (void);
 
 static GpdsXInput *xinput;
 static gint *values;
 static gulong n_values;
 static GError *error;
+static GError *expected_error;
+
+typedef enum {
+    GPDS_MOUSE_MIDDLE_BUTTON_EMULATION,
+    GPDS_MOUSE_MIDDLE_BUTTON_TIMEOUT,
+    GPDS_MOUSE_WHEEL_EMULATION,
+    GPDS_MOUSE_WHEEL_EMULATION_INERTIA,
+    GPDS_MOUSE_WHEEL_EMULATION_AXES,
+    GPDS_MOUSE_WHEEL_EMULATION_TIMEOUT,
+    GPDS_MOUSE_WHEEL_EMULATION_BUTTON,
+    GPDS_MOUSE_DRAG_LOCK_BUTTONS,
+} GpdsMouseProperty;
+
+static const GpdsXInputPropertyEntry entries[] = {
+    {GPDS_MOUSE_MIDDLE_BUTTON_EMULATION, "Evdev Middle Button Emulation", G_TYPE_INT,  8, 1},
+    {GPDS_MOUSE_MIDDLE_BUTTON_TIMEOUT,   "Evdev Middle Button Timeout",   G_TYPE_INT, 32, 1},
+    {GPDS_MOUSE_WHEEL_EMULATION,         "Evdev Wheel Emulation",         G_TYPE_INT,  8, 1},
+    {GPDS_MOUSE_WHEEL_EMULATION_INERTIA, "Evdev Wheel Emulation Inertia", G_TYPE_INT, 16, 1},
+    {GPDS_MOUSE_WHEEL_EMULATION_AXES,    "Evdev Wheel Emulation Axes",    G_TYPE_INT,  8, 4},
+    {GPDS_MOUSE_WHEEL_EMULATION_TIMEOUT, "Evdev Wheel Emulation Timeout", G_TYPE_INT, 16, 1},
+    {GPDS_MOUSE_WHEEL_EMULATION_BUTTON,  "Evdev Wheel Emulation Button",  G_TYPE_INT,  8, 1},
+    {GPDS_MOUSE_DRAG_LOCK_BUTTONS,       "Evdev Drag Lock Buttons",       G_TYPE_INT,  8, 2}
+};
+
+static const gint n_entries = G_N_ELEMENTS(entries);
 
 void
 setup (void)
@@ -19,6 +48,7 @@
     n_values = 0;
 
     error = NULL;
+    expected_error = NULL;
 }
 
 void
@@ -27,47 +57,121 @@
     if (xinput)
         g_object_unref(xinput);
     g_free(values);
+
+    if (expected_error)
+        g_clear_error(&expected_error);
 }
 
 void
 test_new (void)
 {
-    xinput = gpds_xinput_new("TPPS/2 IBM TrackPoint");
+    xinput = gpds_xinput_new("Macintosh mouse button emulation");
     cut_assert(xinput);
 }
 
 void
+test_register_property_entries (void)
+{
+    cut_trace(test_new());
+    gpds_xinput_register_property_entries(xinput, entries, n_entries);
+}
+
+void
 test_device_name (void)
 {
     cut_trace(test_new());
 
-    cut_assert_equal_string("TPPS/2 IBM TrackPoint",
+    cut_assert_equal_string("Macintosh mouse button emulation",
                             gpds_xinput_get_device_name(xinput));
 }
 
 void
-test_set_int_properties (void)
+test_set_int_properties_by_name (void)
 {
     cut_trace(test_new());
 
-    cut_assert_true(gpds_xinput_get_int_properties_by_name
+    gpds_xinput_get_int_properties_by_name (xinput,
+                                            "Evdev Middle Button Emulation",
+                                            &error,
+                                            &values, &n_values);
+    gcut_assert_error(error);
+    cut_assert_equal_int(1, n_values);
+
+    gpds_xinput_set_int_properties_by_name_with_format_type
                                                   (xinput,
                                                    "Evdev Middle Button Emulation",
+                                                   8,
                                                    &error,
-                                                   &values, &n_values));
+                                                   (gint*)&values,
+                                                   n_values);
+    gcut_assert_error(error);
+}
+
+void
+test_set_int_properties_by_name_invalid_format_type (void)
+{
+    expected_error = g_error_new(GPDS_XINPUT_ERROR,
+                                 GPDS_XINPUT_ERROR_X_ERROR,
+                                 "An X error occurred. "
+                                 "The error was BadValue (integer parameter out of range for operation).");
+    cut_trace(test_new());
+
+    gpds_xinput_get_int_properties_by_name (xinput,
+                                            "Evdev Middle Button Emulation",
+                                            &error,
+                                            &values, &n_values);
     gcut_assert_error(error);
     cut_assert_equal_int(1, n_values);
 
-    cut_assert_true(gpds_xinput_set_int_properties_by_name_with_format_type
+    gpds_xinput_set_int_properties_by_name_with_format_type
                                                   (xinput,
                                                    "Evdev Middle Button Emulation",
-                                                   8,
+                                                   33,
                                                    &error,
                                                    (gint*)&values,
-                                                   n_values));
+                                                   n_values);
+    gcut_assert_equal_error(expected_error, error);
+}
+
+void
+test_set_int_properties (void)
+{
+    cut_trace(test_register_property_entries());
+
+    gpds_xinput_get_int_properties(xinput,
+                                   GPDS_MOUSE_MIDDLE_BUTTON_EMULATION,
+                                   &error,
+                                   &values, &n_values);
+    gcut_assert_error(error);
+    cut_assert_equal_int(1, n_values);
+
+    gpds_xinput_set_int_properties(xinput,
+                                   GPDS_MOUSE_MIDDLE_BUTTON_EMULATION,
+                                   &error,
+                                   (gint*)&values,
+                                   n_values);
     gcut_assert_error(error);
 }
 
+void
+test_set_int_properties_invalid_n_values (void)
+{
+    gint invalid_values[2];
+    expected_error = g_error_new(GPDS_XINPUT_ERROR,
+                                 GPDS_XINPUT_ERROR_X_ERROR,
+                                 "An X error occurred. "
+                                 "The error was BadMatch (invalid parameter attributes).");
+
+    cut_trace(test_register_property_entries());
+
+    gpds_xinput_set_int_properties(xinput,
+                                   GPDS_MOUSE_MIDDLE_BUTTON_EMULATION,
+                                   &error,
+                                   invalid_values,
+                                   2);
+    gcut_assert_equal_error(expected_error, error);
+}
+
 /*
 vi:ts=4:nowrap:ai:expandtab:sw=4
 */



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