[gimp/soc-2010-cage] app: Introduce error margin in sessionrc in test cases



commit 8cfee065f6e30573b42b05ae48179c6b488f78a9
Author: Martin Nordholts <martinn src gnome org>
Date:   Sat Jun 19 23:24:23 2010 +0200

    app: Introduce error margin in sessionrc in test cases
    
    Introduce a few pixels of error margin in sessionrc, otherwise the
    tests are too fragile, widgets have a tendency to change size a bit
    and window managers have a tendency to move windows a bit.
    
    Still needs more work, should probably do the same on book positions
    too...

 app/tests.c                          |   11 +++++-
 app/tests/gimpdir/dockrc-expected    |    6 ++--
 app/tests/gimpdir/sessionrc-expected |   20 +++++-----
 app/widgets/gimpsessioninfo.c        |   63 +++++++++++++++++++++++++++++++--
 app/widgets/gimpsessioninfo.h        |   56 ++++++++++++++++-------------
 5 files changed, 113 insertions(+), 43 deletions(-)
---
diff --git a/app/tests.c b/app/tests.c
index e78102a..90272c3 100644
--- a/app/tests.c
+++ b/app/tests.c
@@ -27,6 +27,8 @@
 
 #include "menus/menus.h"
 
+#include "widgets/gimpsessioninfo.h"
+
 #include "base/base.h"
 
 #include "config/gimpbaseconfig.h"
@@ -81,12 +83,17 @@ gimp_status_func_dummy (const gchar *text1,
 Gimp *
 gimp_init_for_gui_testing (gboolean use_cpu_accel, gboolean show_gui)
 {
-  Gimp *gimp;
+  GimpSessionInfoClass *klass;
+  Gimp                 *gimp;
 
   /* from main() */
   g_thread_init(NULL);
   g_type_init();
 
+  /* Introduce an error margin for positions written to sessionrc */
+  klass = g_type_class_ref (GIMP_TYPE_SESSION_INFO);
+  gimp_session_info_class_set_position_accuracy (klass, 5);
+
   /* from app_run() */
   gimp = gimp_new ("Unit Tested GIMP", NULL, FALSE, TRUE, TRUE, !show_gui,
                    FALSE, TRUE, TRUE, FALSE);
@@ -98,6 +105,8 @@ gimp_init_for_gui_testing (gboolean use_cpu_accel, gboolean show_gui)
   gimp_initialize (gimp, gimp_status_func_dummy);
   gimp_restore (gimp, gimp_status_func_dummy);
 
+  g_type_class_unref (klass);
+
   return gimp;
 }
 
diff --git a/app/tests/gimpdir/dockrc-expected b/app/tests/gimpdir/dockrc-expected
index 045fa4b..ed2da0e 100644
--- a/app/tests/gimpdir/dockrc-expected
+++ b/app/tests/gimpdir/dockrc-expected
@@ -2,8 +2,8 @@
 
 (GimpSessionInfo "Palettes - FG/BG - Images"
     (factory-entry "gimp-dock-window")
-    (position 586 116)
-    (size 200 573)
+    (position 585 115)
+    (size 200 575)
     (open-on-exit)
     (aux-info
         (show-image-menu "false")
@@ -27,7 +27,7 @@
                 (tab-style preview)))))
 (GimpSessionInfo "Selection, Fonts"
     (factory-entry "gimp-dock-window")
-    (position 200 179)
+    (position 200 180)
     (size 200 300)
     (open-on-exit)
     (aux-info
diff --git a/app/tests/gimpdir/sessionrc-expected b/app/tests/gimpdir/sessionrc-expected
index 61a264f..2dcc553 100644
--- a/app/tests/gimpdir/sessionrc-expected
+++ b/app/tests/gimpdir/sessionrc-expected
@@ -7,7 +7,7 @@
 
 (session-info "toplevel"
     (factory-entry "gimp-dock-window")
-    (position 566 171)
+    (position 565 170)
     (size 210 535)
     (open-on-exit)
     (aux-info
@@ -28,8 +28,8 @@
                 (tab-style preview)))))
 (session-info "toplevel"
     (factory-entry "gimp-dock-window")
-    (position 141 291)
-    (size 406 300)
+    (position 140 290)
+    (size 405 300)
     (open-on-exit)
     (aux-info
         (show-image-menu "false")
@@ -46,7 +46,7 @@
                 (tab-style icon)))))
 (session-info "toplevel"
     (factory-entry "gimp-dock-window")
-    (position 795 43)
+    (position 795 45)
     (size 200 265)
     (open-on-exit)
     (aux-info
@@ -61,8 +61,8 @@
                 (tab-style preview)))))
 (session-info "toplevel"
     (factory-entry "gimp-dock-window")
-    (position 803 344)
-    (size 200 421)
+    (position 805 345)
+    (size 200 420)
     (open-on-exit)
     (aux-info
         (show-image-menu "true")
@@ -74,7 +74,7 @@
                 (tab-style automatic)
                 (preview-size 32)))
         (book
-            (position 174)
+            (position 162)
             (current-page 0)
             (dockable "gimp-palette-editor"
                 (tab-style icon)
@@ -84,14 +84,14 @@
                     (zoom-factor "2.80"))))))
 (session-info "toplevel"
     (factory-entry "gimp-file-open-dialog")
-    (position 392 140)
-    (size 902 816))
+    (position 390 140)
+    (size 900 815))
 (session-info "toplevel"
     (factory-entry "gimp-image-new-dialog")
     (position 100 100))
 (session-info "toplevel"
     (factory-entry "gimp-empty-image-window")
-    (position 140 32)
+    (position 140 30)
     (size 610 190))
 
 (last-tip-shown 0)
diff --git a/app/widgets/gimpsessioninfo.c b/app/widgets/gimpsessioninfo.c
index 1c2d91f..297fc16 100644
--- a/app/widgets/gimpsessioninfo.c
+++ b/app/widgets/gimpsessioninfo.c
@@ -138,8 +138,13 @@ gimp_session_info_serialize (GimpConfig       *config,
                              GimpConfigWriter *writer,
                              gpointer          data)
 {
-  GimpSessionInfo *info = GIMP_SESSION_INFO (config);
-  GList           *iter = NULL;
+  GimpSessionInfo      *info  = GIMP_SESSION_INFO (config);
+  GimpSessionInfoClass *klass = GIMP_SESSION_INFO_GET_CLASS (info);
+  GList                *iter  = NULL;
+  gint                  x_to_write;
+  gint                  y_to_write;
+  gint                  w_to_write;
+  gint                  h_to_write;
 
   if (info->p->factory_entry && info->p->factory_entry->identifier)
     {
@@ -148,14 +153,23 @@ gimp_session_info_serialize (GimpConfig       *config,
       gimp_config_writer_close (writer);
     }
 
+  x_to_write = gimp_session_info_class_apply_position_accuracy (klass,
+                                                                info->p->x);
+  y_to_write = gimp_session_info_class_apply_position_accuracy (klass,
+                                                                info->p->y);
+  w_to_write = gimp_session_info_class_apply_position_accuracy (klass,
+                                                                info->p->width);
+  h_to_write = gimp_session_info_class_apply_position_accuracy (klass,
+                                                                info->p->height);
+
   gimp_config_writer_open (writer, "position");
-  gimp_config_writer_printf (writer, "%d %d", info->p->x, info->p->y);
+  gimp_config_writer_printf (writer, "%d %d", x_to_write, y_to_write);
   gimp_config_writer_close (writer);
 
   if (info->p->width > 0 && info->p->height > 0)
     {
       gimp_config_writer_open (writer, "size");
-      gimp_config_writer_printf (writer, "%d %d", info->p->width, info->p->height);
+      gimp_config_writer_printf (writer, "%d %d", w_to_write, h_to_write);
       gimp_config_writer_close (writer);
     }
 
@@ -851,3 +865,44 @@ gimp_session_info_get_height (GimpSessionInfo *info)
 
   return info->p->height;
 }
+
+/**
+ * gimp_session_info_class_set_position_accuracy:
+ * @accuracy:
+ *
+ * When writing sessionrc, make positions and sizes a multiple of
+ * @accuracy. Meant to be used by test cases that does regression
+ * testing on session managed window positions and sizes, to allow for
+ * some deviations from the original setup, that the window manager
+ * might impose.
+ **/
+void
+gimp_session_info_class_set_position_accuracy (GimpSessionInfoClass *klass,
+                                               gint                  accuracy)
+{
+  g_return_if_fail (GIMP_IS_SESSION_INFO_CLASS (klass));
+
+  klass->position_accuracy = accuracy;
+}
+
+/**
+ * gimp_session_info_class_apply_position_accuracy:
+ * @position:
+ *
+ * Rounds @position to the nearest multiple of what was set with
+ * gimp_session_info_class_set_position_accuracy().
+ *
+ * Returns: Result.
+ **/
+gint
+gimp_session_info_class_apply_position_accuracy (GimpSessionInfoClass *klass,
+                                                 gint                  position)
+{
+  gint n = klass->position_accuracy;
+  gint to_floor;
+
+  g_return_val_if_fail (GIMP_IS_SESSION_INFO_CLASS (klass), position);
+
+  to_floor = position + n / 2;
+  return to_floor - to_floor % n;
+}
diff --git a/app/widgets/gimpsessioninfo.h b/app/widgets/gimpsessioninfo.h
index 13c8bf0..3dbe8ca 100644
--- a/app/widgets/gimpsessioninfo.h
+++ b/app/widgets/gimpsessioninfo.h
@@ -52,34 +52,40 @@ struct _GimpSessionInfo
 struct _GimpSessionInfoClass
 {
   GimpObjectClass  parent_class;
+
+  gint             position_accuracy;
 };
 
 
-GType                    gimp_session_info_get_type             (void) G_GNUC_CONST;
-GimpSessionInfo *        gimp_session_info_new                  (void);
-void                     gimp_session_info_restore              (GimpSessionInfo        *info,
-                                                                 GimpDialogFactory      *factory);
-void                     gimp_session_info_apply_geometry       (GimpSessionInfo        *info);
-void                     gimp_session_info_read_geometry        (GimpSessionInfo        *info);
-void                     gimp_session_info_get_info             (GimpSessionInfo        *info);
-void                     gimp_session_info_clear_info           (GimpSessionInfo        *info);
-gboolean                 gimp_session_info_is_singleton         (GimpSessionInfo        *info);
-gboolean                 gimp_session_info_is_session_managed   (GimpSessionInfo        *info);
-gboolean                 gimp_session_info_get_remember_size    (GimpSessionInfo        *info);
-gboolean                 gimp_session_info_get_remember_if_open (GimpSessionInfo        *info);
-GtkWidget              * gimp_session_info_get_widget           (GimpSessionInfo        *info);
-void                     gimp_session_info_set_widget           (GimpSessionInfo        *info,
-                                                                 GtkWidget              *widget);
-GimpDialogFactoryEntry * gimp_session_info_get_factory_entry    (GimpSessionInfo        *info);
-void                     gimp_session_info_set_factory_entry    (GimpSessionInfo        *info,
-                                                                 GimpDialogFactoryEntry *entry);
-gboolean                 gimp_session_info_get_open             (GimpSessionInfo        *info);
-void                     gimp_session_info_append_book          (GimpSessionInfo        *info,
-                                                                 GimpSessionInfoBook    *book);
-gint                     gimp_session_info_get_x                (GimpSessionInfo        *info);
-gint                     gimp_session_info_get_y                (GimpSessionInfo        *info);
-gint                     gimp_session_info_get_width            (GimpSessionInfo        *info);
-gint                     gimp_session_info_get_height           (GimpSessionInfo        *info);
+GType                    gimp_session_info_get_type                      (void) G_GNUC_CONST;
+GimpSessionInfo *        gimp_session_info_new                           (void);
+void                     gimp_session_info_restore                       (GimpSessionInfo        *info,
+                                                                          GimpDialogFactory      *factory);
+void                     gimp_session_info_apply_geometry                (GimpSessionInfo        *info);
+void                     gimp_session_info_read_geometry                 (GimpSessionInfo        *info);
+void                     gimp_session_info_get_info                      (GimpSessionInfo        *info);
+void                     gimp_session_info_clear_info                    (GimpSessionInfo        *info);
+gboolean                 gimp_session_info_is_singleton                  (GimpSessionInfo        *info);
+gboolean                 gimp_session_info_is_session_managed            (GimpSessionInfo        *info);
+gboolean                 gimp_session_info_get_remember_size             (GimpSessionInfo        *info);
+gboolean                 gimp_session_info_get_remember_if_open          (GimpSessionInfo        *info);
+GtkWidget              * gimp_session_info_get_widget                    (GimpSessionInfo        *info);
+void                     gimp_session_info_set_widget                    (GimpSessionInfo        *info,
+                                                                          GtkWidget              *widget);
+GimpDialogFactoryEntry * gimp_session_info_get_factory_entry             (GimpSessionInfo        *info);
+void                     gimp_session_info_set_factory_entry             (GimpSessionInfo        *info,
+                                                                          GimpDialogFactoryEntry *entry);
+gboolean                 gimp_session_info_get_open                      (GimpSessionInfo        *info);
+void                     gimp_session_info_append_book                   (GimpSessionInfo        *info,
+                                                                          GimpSessionInfoBook    *book);
+gint                     gimp_session_info_get_x                         (GimpSessionInfo        *info);
+gint                     gimp_session_info_get_y                         (GimpSessionInfo        *info);
+gint                     gimp_session_info_get_width                     (GimpSessionInfo        *info);
+gint                     gimp_session_info_get_height                    (GimpSessionInfo        *info);
+void                     gimp_session_info_class_set_position_accuracy   (GimpSessionInfoClass   *klass,
+                                                                          gint                    accuracy);
+gint                     gimp_session_info_class_apply_position_accuracy (GimpSessionInfoClass   *klass,
+                                                                          gint                    position);
 
 
 #endif  /* __GIMP_SESSION_INFO_H__ */



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