[gimp/soc-2011-gimpunitentry: 14/68] GimpUnitEntry: add unit-test



commit 160b88cd5ba395e634fe3097e99870f9f10678b1
Author: Enrico Schröder <enni schroeder gmail com>
Date:   Thu Jun 9 12:57:57 2011 +0200

    GimpUnitEntry: add unit-test
    
    moves the gui testprogram to test-unitentrygui and implements unit-test as test-unitentry

 .gitignore                         |    3 +-
 libgimpwidgets/Makefile.am         |   21 +++-
 libgimpwidgets/test-unitentry.c    |  245 ++++++++++++++++++++++++++++--------
 libgimpwidgets/test-unitentrygui.c |   75 +++++++++++
 4 files changed, 288 insertions(+), 56 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index 3827c2b..002ed9c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -47,4 +47,5 @@
 /stamp-h.in
 /stamp-h1
 
-/libgimpwidgets/test-unitentry
\ No newline at end of file
+/libgimpwidgets/test-unitentry
+/libgimpwidgets/test-unitentrygui
\ No newline at end of file
diff --git a/libgimpwidgets/Makefile.am b/libgimpwidgets/Makefile.am
index 8b3394e..2a39f81 100644
--- a/libgimpwidgets/Makefile.am
+++ b/libgimpwidgets/Makefile.am
@@ -344,7 +344,8 @@ gimp-wilber-pixbufs.h: $(WILBER_IMAGES) Makefile.am
 EXTRA_PROGRAMS = \
        test-preview-area       \
        test-eevl               \
-       test-unitentry
+       test-unitentry \
+       test-unitentrygui
 
 
 test_preview_area_SOURCES = test-preview-area.c 
@@ -368,13 +369,26 @@ test_eevl_LDADD = \
        $(GLIB_LIBS)            \
        $(test_eevl_DEPENDENCIES)
 
+test_unitentrygui_SOURCES = \
+       test-unitentrygui.c \
+       $(top_builddir)/devel-docs/tools/units.c
+
+test_unitentrygui_DEPENDENCIES = \
+       $(top_builddir)/libgimpbase/libgimpbase-$(GIMP_API_VERSION).la \
+       $(top_builddir)/libgimp/libgimp-$(GIMP_API_VERSION).la \
+       $(top_builddir)/libgimpwidgets/libgimpwidgets-$(GIMP_API_VERSION).la
+
+test_unitentrygui_LDADD = \
+       $(GLIB_LIBS)            \
+       $(test_unitentrygui_DEPENDENCIES)
+
 test_unitentry_SOURCES = \
        test-unitentry.c \
        $(top_builddir)/devel-docs/tools/units.c
 
 test_unitentry_DEPENDENCIES = \
        $(top_builddir)/libgimpbase/libgimpbase-$(GIMP_API_VERSION).la \
-       $(top_builddir)/libgimp/libgimp-$(GIMP_API_VERSION).la \        
+       $(top_builddir)/libgimp/libgimp-$(GIMP_API_VERSION).la \
        $(top_builddir)/libgimpwidgets/libgimpwidgets-$(GIMP_API_VERSION).la
 
 test_unitentry_LDADD = \
@@ -386,7 +400,8 @@ test_unitentry_LDADD = \
 # test programs, not to be built by default and never installed
 #
 
-TESTS = test-eevl$(EXEEXT)
+TESTS = test-eevl$(EXEEXT) \
+                               test-unitentry$(EXEEXT)
 
 
 
diff --git a/libgimpwidgets/test-unitentry.c b/libgimpwidgets/test-unitentry.c
index df7ea46..260ac14 100644
--- a/libgimpwidgets/test-unitentry.c
+++ b/libgimpwidgets/test-unitentry.c
@@ -1,5 +1,3 @@
-/* small test app for the new unit entry widget developed during Google Summer of Code 2011 */
-
 #include "config.h"
 
 #include <string.h>
@@ -10,66 +8,209 @@
 
 #include "devel-docs/tools/units.h"
 
-#include "gimpunitentrytable.h"
+#include "gimpunitentry.h"
+
+#define ADD_TEST(function) \
+  g_test_add ("/gimpunitentry/" #function, \
+              GimpTestFixture, \
+              NULL, \
+              gimp_test_unitentry_setup, \
+              function, \
+              gimp_test_unitentry_teardown);
+
+typedef struct
+{
+  GimpUnitEntry *entry1, *entry2;
+} GimpTestFixture;
+
+static void
+gimp_test_unitentry_setup (GimpTestFixture *fixture,
+                           gconstpointer    data)
+{
+  fixture->entry1 = GIMP_UNIT_ENTRY (gimp_unit_entry_new ("entry1"));
+  fixture->entry2 = GIMP_UNIT_ENTRY (gimp_unit_entry_new ("entry2"));
+  gimp_unit_entry_connect (fixture->entry1, fixture->entry2);
+  gimp_unit_entry_connect (fixture->entry2, fixture->entry1);
+}
+
+static void
+gimp_test_unitentry_teardown (GimpTestFixture *fixture,
+                              gconstpointer    data)
+{
+  g_object_ref_sink (fixture->entry1);
+  g_object_unref    (fixture->entry1);
+  fixture->entry1 = NULL;
+  g_object_ref_sink (fixture->entry2);
+  g_object_unref    (fixture->entry2);
+  fixture->entry1 = NULL;
+}
+
+/**
+ * parse:
+ *
+ * Test that simple parsing and unit determination works
+ **/
+static void
+parse (GimpTestFixture *f,
+       gconstpointer    data)
+{
+  gtk_entry_set_text (GTK_ENTRY (f->entry1), "10 px");
+
+  g_assert (gimp_unit_entry_get_value_in_unit (f->entry1, GIMP_UNIT_PIXEL) == 10.0);
+  g_assert (gimp_unit_entry_get_unit          (f->entry1) == GIMP_UNIT_PIXEL);
+}
+
+/**
+ * parse_without_unit:
+ *
+ * Test that default unit is correcty applied when the user just
+ * enters a value without unit
+ **/
+static void
+parse_without_unit (GimpTestFixture *f,
+                    gconstpointer    data)
+{
+  gimp_unit_entry_set_unit (f->entry1, GIMP_UNIT_PIXEL);
+
+  gtk_entry_set_text (GTK_ENTRY (f->entry1), "10");
+
+  g_assert (gimp_unit_entry_get_value_in_unit (f->entry1, GIMP_UNIT_PIXEL) == 10.0);
+  g_assert (gimp_unit_entry_get_unit          (f->entry1) == GIMP_UNIT_PIXEL);
+}
+
+/**
+ * parse_addition_without_unit:
+ *
+ * Test that input in the form "10 in + 20" works
+ **/
+static void
+parse_addition_without_unit (GimpTestFixture *f,
+                             gconstpointer    data)
+{
+  gimp_unit_entry_set_unit (f->entry1, GIMP_UNIT_INCH);
+
+  gtk_entry_set_text (GTK_ENTRY (f->entry1), "10 in + 20");
+
+  g_assert (gimp_unit_entry_get_value (f->entry1) == 30.0);
+  g_assert (gimp_unit_entry_get_unit  (f->entry1) == GIMP_UNIT_INCH);
+}
+
+/**
+ * parse_different_units:
+ * 
+ * Test that entering a term with different units gets calculated
+ * correctly
+ */
+static void
+parse_different_units (GimpTestFixture *f,
+                       gconstpointer    data)
+{
+  gimp_unit_entry_set_resolution (f->entry1, 72);
+
+  gtk_entry_set_text (GTK_ENTRY (f->entry1), "10 in + 72 px");
+
+  g_assert (gimp_unit_entry_get_value_in_unit (f->entry1, GIMP_UNIT_INCH) == 11.0);
+  g_assert (gimp_unit_entry_get_unit          (f->entry1) == GIMP_UNIT_INCH);
+}
 
-/* global objects */
-GtkWidget *window;
-GtkWidget *vbox;
-GtkWidget *valign;
+/**
+ * change_units:
+ * 
+ * Test that changing the unit of one entry correctly changes
+ * the connected entry
+ */
+static void
+change_units (GimpTestFixture *f,
+              gconstpointer    data)
+{
+  gimp_unit_entry_set_resolution (f->entry1, 72);
+  gimp_unit_entry_set_resolution (f->entry2, 72);
+  gimp_unit_entry_set_unit       (f->entry1, GIMP_UNIT_PIXEL);
+  gimp_unit_entry_set_unit       (f->entry2, GIMP_UNIT_PIXEL);
+  gimp_unit_entry_set_value      (f->entry2, 72);
+
+  gtk_entry_set_text (GTK_ENTRY (f->entry1), "10 in");
 
-GimpUnitEntryTable *entryTable;
+  g_assert (gimp_unit_entry_get_value         (f->entry2) == 1.0);
+  g_assert (gimp_unit_entry_get_unit          (f->entry2) == GIMP_UNIT_INCH);
+}
 
-/* set up interface */
-void
-create_interface(void)
+/**
+ * parse_addition_to_default_unit
+ *
+ * A term entered in the form "10 + 5mm" should be treated as "10 in + 5 mm"
+ * (if default unit is inch)
+ * FIXME: doesn't work right now because GimpEevl internally combines
+ * "10 + 5 mm" to 15 mm so we are not informed of the single "10 " and
+ * we can not apply our default unit .
+ * In other words: unit resolver callback is not called for the "10 " but
+ * instead directly with "15 mm"
+ **/
+static void
+parse_addition_to_default_unit (GimpTestFixture *f,
+                                gconstpointer    data)
 {
-  GimpUnitEntry *a, *b;
-
-  /* main window */
-  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
-  gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
-  gtk_window_set_default_size (GTK_WINDOW(window), 200, 100);
-
-  /* vbox (used for the entries) */
-  vbox = gtk_vbox_new (TRUE, 1);
-
-  /* valign */
-  valign = gtk_alignment_new (0, 0, 1, 0);
-  gtk_container_add (GTK_CONTAINER (valign), vbox);
-  gtk_container_add (GTK_CONTAINER (window), valign);
-
-  /* entry table */
-  entryTable = GIMP_UNIT_ENTRY_TABLE (gimp_unit_entry_table_new ());
-  gimp_unit_entry_table_add_entry (entryTable, "width", "Width");
-  gimp_unit_entry_table_add_entry (entryTable, "height", "Height");
-  gimp_unit_entry_table_add_label (entryTable, GIMP_UNIT_PIXEL);
-
-  /* set some default values */
-  a = gimp_unit_entry_table_get_entry (entryTable, "width");
-  b = gimp_unit_entry_table_get_entry (entryTable, "height");
-  gimp_unit_adjustment_set_value (gimp_unit_entry_get_adjustment (a), 20);
-  gimp_unit_adjustment_set_value (gimp_unit_entry_get_adjustment (b), 20);
-
-  gtk_box_pack_start (GTK_BOX (vbox), entryTable->table, TRUE, TRUE, 0);
-
-  /* signals */
-  g_signal_connect_swapped (G_OBJECT(window), "destroy",
-                            G_CALLBACK(gtk_main_quit), NULL);
- 
-  gtk_widget_show_all (window);
+  gimp_unit_entry_set_resolution (f->entry1, 72);
+  gimp_unit_entry_set_unit       (f->entry1, GIMP_UNIT_INCH);
+
+  gtk_entry_set_text (GTK_ENTRY (f->entry1), "10 + 72 px");
+
+  g_assert (gimp_unit_entry_get_value_in_unit (f->entry1, GIMP_UNIT_INCH) == 11.0);
+  g_assert (gimp_unit_entry_get_unit          (f->entry1) == GIMP_UNIT_INCH);
+}
+
+/**
+ * parse_multiplication
+ *
+ * Test that multiplication with constant factor works
+ **/
+static void
+parse_multiplication (GimpTestFixture *f,
+                      gconstpointer    data)
+{
+  gimp_unit_entry_set_unit (f->entry1, GIMP_UNIT_INCH);
+
+  gtk_entry_set_text (GTK_ENTRY (f->entry1), "10 in * 10");
+
+  g_assert (gimp_unit_entry_get_value (f->entry1) == 100.0);
+  g_assert (gimp_unit_entry_get_unit  (f->entry1) == GIMP_UNIT_INCH);
+}
+
+/**
+ * parse_multidim_multiplication
+ *
+ * Test that multidimensional multiplication is not accepted as correct
+ * input (e.g. "10 in * 10 px")
+ **/
+static void
+parse_multidim_multiplication (GimpTestFixture *f,
+                               gconstpointer    data)
+{
+  gimp_unit_entry_set_unit  (f->entry1, GIMP_UNIT_INCH);
+  gimp_unit_entry_set_value (f->entry1, 10.0);
+
+  gtk_entry_set_text (GTK_ENTRY (f->entry1), "10 in * 10 px");
+
+  g_assert (gimp_unit_entry_get_value (f->entry1) == 10.0);
+  g_assert (gimp_unit_entry_get_unit  (f->entry1) == GIMP_UNIT_INCH);
 }
 
 int main (int   argc,
           char *argv[])
 {
-    units_init();
-
+    g_type_init ();
+    units_init  ();
+    g_test_init (&argc, &argv, NULL);
     gtk_init (&argc, &argv);
 
-    create_interface ();
-
-    gtk_main ();
+    ADD_TEST (parse);
+    ADD_TEST (parse_without_unit);
+    ADD_TEST (parse_addition_without_unit)
+    ADD_TEST (parse_different_units);
+    ADD_TEST (change_units);
+    //ADD_TEST (parse_addition_to_default_unit);
+    ADD_TEST (parse_multiplication);
+    ADD_TEST (parse_multidim_multiplication)
     
-    return 0;
+    return g_test_run ();
 }
diff --git a/libgimpwidgets/test-unitentrygui.c b/libgimpwidgets/test-unitentrygui.c
new file mode 100644
index 0000000..a4d7c7a
--- /dev/null
+++ b/libgimpwidgets/test-unitentrygui.c
@@ -0,0 +1,75 @@
+/* small gui test app for the new unit entry widget developed during Google Summer of Code 2011 */
+
+#include "config.h"
+
+#include <string.h>
+#include <glib-object.h>
+#include <glib/gprintf.h>
+#include <gtk/gtk.h>
+#include "libgimpbase/gimpbase.h"
+
+#include "devel-docs/tools/units.h"
+
+#include "gimpunitentrytable.h"
+
+/* global objects */
+GtkWidget *window;
+GtkWidget *vbox;
+GtkWidget *valign;
+
+GimpUnitEntryTable *entryTable;
+
+/* set up interface */
+void
+create_interface(void)
+{
+  GimpUnitEntry *a, *b;
+
+  /* main window */
+  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
+  gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
+  gtk_window_set_default_size (GTK_WINDOW(window), 200, 100);
+
+  /* vbox (used for the entries) */
+  vbox = gtk_vbox_new (TRUE, 1);
+
+  /* valign */
+  valign = gtk_alignment_new (0, 0, 1, 0);
+  gtk_container_add (GTK_CONTAINER (valign), vbox);
+  gtk_container_add (GTK_CONTAINER (window), valign);
+
+  /* entry table */
+  entryTable = GIMP_UNIT_ENTRY_TABLE (gimp_unit_entry_table_new ());
+  gimp_unit_entry_table_add_entry (entryTable, "width", "Width");
+  gimp_unit_entry_table_add_entry (entryTable, "height", "Height");
+  gimp_unit_entry_table_add_label (entryTable, GIMP_UNIT_PIXEL);
+
+  /* set some default values */
+  a = gimp_unit_entry_table_get_entry (entryTable, "width");
+  b = gimp_unit_entry_table_get_entry (entryTable, "height");
+  gimp_unit_adjustment_set_value (gimp_unit_entry_get_adjustment (a), 20);
+  gimp_unit_adjustment_set_value (gimp_unit_entry_get_adjustment (b), 20);
+
+  gtk_box_pack_start (GTK_BOX (vbox), entryTable->table, TRUE, TRUE, 0);
+
+  /* signals */
+  g_signal_connect_swapped (G_OBJECT(window), "destroy",
+                            G_CALLBACK(gtk_main_quit), NULL);
+ 
+  gtk_widget_show_all (window);
+}
+
+int main (int   argc,
+          char *argv[])
+{
+    units_init();
+
+    gtk_init (&argc, &argv);
+
+    create_interface ();
+
+    gtk_main ();
+    
+    return 0;
+}


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