[maemo-commits] [maemo-commits] r15107 - in projects/haf/branches/hildon-1/gtester: . src/tests

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Mon Jan 21 09:26:59 EET 2008
Author: tko
Date: 2008-01-21 09:26:59 +0200 (Mon, 21 Jan 2008)
New Revision: 15107

Added:
   projects/haf/branches/hildon-1/gtester/src/tests/testdate.c
Modified:
   projects/haf/branches/hildon-1/gtester/ChangeLog
   projects/haf/branches/hildon-1/gtester/src/tests/Makefile.am
Log:
Add tests for HildonDateEditor using the glib testing framework.

2008-01-09  Tommi Komulainen  <tommi.komulainen at nokia.com>

	* src/tests/Makefile.am
	* src/tests/testdate.c: Add tests for HildonDateEditor using the glib
	testing framework.


Modified: projects/haf/branches/hildon-1/gtester/ChangeLog
===================================================================
--- projects/haf/branches/hildon-1/gtester/ChangeLog	2008-01-21 07:24:58 UTC (rev 15106)
+++ projects/haf/branches/hildon-1/gtester/ChangeLog	2008-01-21 07:26:59 UTC (rev 15107)
@@ -1,5 +1,11 @@
 2008-01-09  Tommi Komulainen  <tommi.komulainen at nokia.com>
 
+	* src/tests/Makefile.am
+	* src/tests/testdate.c: Add tests for HildonDateEditor using the glib
+	testing framework.
+
+2008-01-09  Tommi Komulainen  <tommi.komulainen at nokia.com>
+
 	Integrate glib testing framework into the build system.
 
 	* Makefile.decl: Copy from gtk+, with gdktarget and GTESTER variables

Modified: projects/haf/branches/hildon-1/gtester/src/tests/Makefile.am
===================================================================
--- projects/haf/branches/hildon-1/gtester/src/tests/Makefile.am	2008-01-21 07:24:58 UTC (rev 15106)
+++ projects/haf/branches/hildon-1/gtester/src/tests/Makefile.am	2008-01-21 07:26:59 UTC (rev 15107)
@@ -1 +1,8 @@
 include $(top_srcdir)/Makefile.decl
+
+noinst_PROGRAMS = $(TEST_PROGS)
+
+TEST_PROGS      += testdate
+testdate_SOURCES = testdate.c
+testdate_LDADD   = $(HILDON_OBJ_LIBS)
+testdate_CFLAGS  = $(HILDON_OBJ_CFLAGS)

Added: projects/haf/branches/hildon-1/gtester/src/tests/testdate.c
===================================================================
--- projects/haf/branches/hildon-1/gtester/src/tests/testdate.c	2008-01-21 07:24:58 UTC (rev 15106)
+++ projects/haf/branches/hildon-1/gtester/src/tests/testdate.c	2008-01-21 07:26:59 UTC (rev 15107)
@@ -0,0 +1,364 @@
+/*
+ * This file is a part of hildon tests
+ *
+ * Copyright (C) 2008 Nokia Corporation, all rights reserved.
+ *
+ * Contact: Michael Dominic Kostrzewa <michael.kostrzewa at nokia.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+#undef G_DISABLE_ASSERT
+#include <glib.h>
+#include <gtk/gtk.h>
+#include "hildon-date-editor.h"
+#include "hildon-enum-types.h"
+#include <gdk/gdkkeysyms.h>
+
+static void
+test_create (void)
+{
+  GtkWidget *editor;
+  guint year;
+  guint month;
+  guint day;
+  guint min_year;
+  guint max_year;
+
+  editor = hildon_date_editor_new ();
+  /* gtk_test_widget_sanity (editor) */
+  g_assert (HILDON_IS_DATE_EDITOR (editor));
+
+  /* check properties have their default values */
+  g_object_get (G_OBJECT (editor),
+                "year",     &year,
+                "month",    &month,
+                "day",      &day,
+                "min-year", &min_year,
+                "max-year", &max_year,
+                NULL);
+
+  g_assert_cmpint (year, ==, 2007);
+  g_assert_cmpint (month, ==, 1);
+  g_assert_cmpint (day, ==, 1);
+  g_assert_cmpint (min_year, ==, 1970);
+  g_assert_cmpint (max_year, ==, 2037);
+
+  gtk_widget_destroy (editor);
+}
+
+static void
+test_properties (void)
+{
+  HildonDateEditor *editor;
+  guint year;
+  guint month;
+  guint day;
+
+  editor = HILDON_DATE_EDITOR (hildon_date_editor_new ());
+
+  g_object_get (G_OBJECT (editor),
+                "year",  &year,
+                "month", &month,
+                "day",   &day,
+                NULL);
+
+  g_assert_cmpint (year, ==, hildon_date_editor_get_year (editor));
+  g_assert_cmpint (month, ==, hildon_date_editor_get_month (editor));
+  g_assert_cmpint (day, ==, hildon_date_editor_get_day (editor));
+
+  gtk_widget_destroy (GTK_WIDGET (editor));
+}
+
+/* Poking into the internals... */
+typedef struct {
+  GtkWidget *editor;
+  GtkWidget *day_entry;
+  GtkWidget *month_entry;
+  GtkWidget *year_entry;
+} DateEditorFixture;
+
+static void
+pick_entries (GtkWidget *widget, gpointer user_data)
+{
+  DateEditorFixture *widgets = user_data;
+  gchar *name;
+
+  name = gtk_widget_get_composite_name (widget);
+  if (name)
+    {
+      if (g_str_equal (name, "day_entry"))
+        widgets->day_entry = widget;
+      else if (g_str_equal (name, "month_entry"))
+        widgets->month_entry = widget;
+      else if (g_str_equal (name, "year_entry"))
+        widgets->year_entry = widget;
+    }
+
+  if (GTK_IS_CONTAINER (widget))
+    gtk_container_forall (GTK_CONTAINER (widget), pick_entries, widgets);
+}
+
+static void
+int_setup (DateEditorFixture *fix, gconstpointer test_data)
+{
+  GtkWidget *window;
+
+  window = gtk_test_create_widget (GTK_TYPE_WINDOW, NULL);
+
+  fix->editor = gtk_test_create_widget (HILDON_TYPE_DATE_EDITOR, "parent", window, NULL);
+  g_assert (HILDON_IS_DATE_EDITOR (fix->editor));
+
+  gtk_widget_show (window);
+
+  /* poking into the internals of HildonDateEditor */
+  gtk_container_forall (GTK_CONTAINER (fix->editor), pick_entries, fix);
+  g_assert (GTK_IS_ENTRY (fix->day_entry));
+  g_assert (GTK_IS_ENTRY (fix->month_entry));
+  g_assert (GTK_IS_ENTRY (fix->year_entry));
+}
+
+static void
+int_teardown (DateEditorFixture *fix, gconstpointer test_data)
+{
+  g_assert (HILDON_IS_DATE_EDITOR (fix->editor));
+}
+
+static void
+test_int_focus (DateEditorFixture *fix, gconstpointer test_data)
+{
+  GtkWidget *window;
+  GtkWidget *focus_widget;
+
+  window = gtk_widget_get_toplevel (fix->editor);
+  g_assert (GTK_IS_WINDOW (window));
+
+  /* ensure first entry (month field, assuming US locale) gets focus */
+  gtk_widget_grab_focus (fix->editor);
+  focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+  g_assert (focus_widget == fix->month_entry); /* MM/DD/YYYY */
+
+  /* ensure focus moves from month to day when entering double-digit month (11) */
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_1, 0));
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_1, 0));
+  while (gtk_events_pending ()) gtk_main_iteration ();
+  focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+  g_assert (focus_widget == fix->day_entry);
+
+  /* ensure focus moves from day to year when entering double-digit day (11) */
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_1, 0));
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_1, 0));
+  while (gtk_events_pending ()) gtk_main_iteration ();
+  focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+  g_assert (focus_widget == fix->year_entry);
+
+  /* ensure focus does *NOT* move from year when entering four-digit year (2000) */
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_2, 0));
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_0, 0));
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_0, 0));
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_0, 0));
+  while (gtk_events_pending ()) gtk_main_iteration ();
+  focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+  g_assert (focus_widget == fix->year_entry);
+}
+
+typedef struct {
+  gint                count;
+  HildonDateTimeError last_error;
+} SignalInfo;
+
+static gboolean
+catch_date_editor_date_error (HildonDateEditor *editor, HildonDateTimeError type, SignalInfo *signal_info)
+{
+  signal_info->count++;
+  signal_info->last_error = type;
+  return TRUE; /* no need for infoprint */
+}
+
+static void
+my_assertion_message_cmpenum (const char *domain,
+                              const char *file,
+                              int         line,
+                              const char *func,
+                              const char *expr,
+                              GType       enum_type,
+                              gint        arg1,
+                              const char *cmp,
+                              gint        arg2)
+{
+  GEnumClass *enum_class;
+  GEnumValue *val1, *val2;
+  char *a1, *a2, *s;
+
+  enum_class = g_type_class_ref (enum_type);
+  g_assert (enum_class != NULL);
+
+  val1 = g_enum_get_value (enum_class, arg1);
+  val2 = g_enum_get_value (enum_class, arg2);
+
+  a1 = val1 ? g_strdup_printf ("%d (%s)", arg1, val1->value_name) : g_strdup_printf ("%d", arg1);
+  a2 = val2 ? g_strdup_printf ("%d (%s)", arg2, val2->value_name) : g_strdup_printf ("%d", arg2);
+
+  s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
+  g_free (a1);
+  g_free (a2);
+
+  g_assertion_message (domain, file, line, func, s);
+  g_free (s);
+
+  g_type_class_unref (enum_class);
+}
+
+
+#define my_assert_cmpenum(n1, cmp, n2, t) do { gint64 __n1 = (n1), __n2 = (n2); \
+                                              if (__n1 cmp __n2) ; else { \
+                                                my_assertion_message_cmpenum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+                                                #n1 " " #cmp " " #n2, (t), __n1, #cmp, __n2); \
+                                              } \
+                                            } while (0)
+
+static void
+test_int_validation (DateEditorFixture *fix, gconstpointer test_data)
+{
+  GtkWidget *window;
+  GtkWidget *focus_widget;
+  SignalInfo signal_info;
+  guint      value_before;
+
+  /* FAIL: After 00 focus is on day entry, for unknown reason. If this is done
+   * before showing the window, it doesn't happen.
+  hildon_date_editor_set_month (HILDON_DATE_EDITOR (fix->editor), 6);
+  */
+
+  g_signal_connect (fix->editor, "date-error", G_CALLBACK (catch_date_editor_date_error), &signal_info);
+
+  window = gtk_widget_get_toplevel (fix->editor);
+  gtk_widget_grab_focus (fix->editor);
+  focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+  g_assert (focus_widget == fix->month_entry);
+
+  /* ensure focus does *NOT* move when entering invalid two-digit month (00) */
+  signal_info.count = 0;
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_0, 0));
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_0, 0));
+  while (gtk_events_pending ()) gtk_main_iteration ();
+  focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+  g_assert (focus_widget == fix->month_entry);
+
+  g_assert_cmpint (signal_info.count, ==, 1);
+  my_assert_cmpenum (signal_info.last_error, ==, HILDON_DATE_TIME_ERROR_MIN_MONTH, HILDON_TYPE_DATE_TIME_ERROR);
+  g_assert_cmpint (1, ==, hildon_date_editor_get_month (HILDON_DATE_EDITOR (fix->editor)));
+
+  /* ensure focus does *NOT* move when entering invalid two-digit month (99) */
+  signal_info.count = 0;
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_9, 0));
+  g_assert (gtk_test_widget_send_key (focus_widget, GDK_9, 0));
+  while (gtk_events_pending ()) gtk_main_iteration ();
+  focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+  g_assert (focus_widget == fix->month_entry);
+
+  g_assert_cmpint (signal_info.count, ==, 1);
+  my_assert_cmpenum (signal_info.last_error, ==, HILDON_DATE_TIME_ERROR_MAX_MONTH, HILDON_TYPE_DATE_TIME_ERROR);
+  g_assert_cmpint (12, ==, hildon_date_editor_get_month (HILDON_DATE_EDITOR (fix->editor)));
+
+  if (g_test_thorough ())
+    {
+      /* FAIL: focus does move.. */
+      /* ensure focus does *NOT* move when entering invalid three-digit month (666) */
+      signal_info.count = 0;
+      g_assert (gtk_test_widget_send_key (focus_widget, GDK_6, 0));
+      g_assert (gtk_test_widget_send_key (focus_widget, GDK_6, 0));
+      g_assert (gtk_test_widget_send_key (focus_widget, GDK_6, 0));
+      while (gtk_events_pending ()) gtk_main_iteration ();
+      focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+      g_assert (focus_widget == fix->month_entry);
+
+      g_assert (signal_info.count == 1);
+      my_assert_cmpenum (signal_info.last_error, ==, HILDON_DATE_TIME_ERROR_MAX_MONTH, HILDON_TYPE_DATE_TIME_ERROR);
+      g_assert_cmpint (12, ==, hildon_date_editor_get_month (HILDON_DATE_EDITOR (fix->editor)));
+
+      /* FAIL: no filtering is done.. */
+      /* ensure non-numbers are not accepted */
+      signal_info.count = 0;
+      value_before = hildon_date_editor_get_month (HILDON_DATE_EDITOR (fix->editor));
+      g_assert (gtk_test_widget_send_key (focus_widget, GDK_a, 0));
+      while (gtk_events_pending ()) gtk_main_iteration ();
+      focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+      g_assert (focus_widget == fix->month_entry);
+
+      g_assert_cmpint (signal_info.count, ==, 1);
+      my_assert_cmpenum (signal_info.last_error, ==, HILDON_DATE_TIME_ERROR_INVALID_CHAR, HILDON_TYPE_DATE_TIME_ERROR);
+      g_assert_cmpint (value_before, ==, hildon_date_editor_get_month (HILDON_DATE_EDITOR (fix->editor)));
+    }
+}
+
+static void
+test_int_leading_zero (DateEditorFixture *fix, gconstpointer test_data)
+{
+  GtkWidget *window;
+  GtkWidget *focus_widget;
+  SignalInfo signal_info;
+  int        i;
+
+  g_signal_connect (fix->editor, "date-error", G_CALLBACK (catch_date_editor_date_error), &signal_info);
+
+  window = gtk_widget_get_toplevel (fix->editor);
+
+  for (i = 1; i < 10; i++)
+    {
+      gtk_widget_grab_focus (fix->month_entry);
+
+      focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+      g_assert (focus_widget == fix->month_entry);
+
+      /* ensure leading zero is accepted for month */
+      signal_info.count = 0;
+      g_assert (gtk_test_widget_send_key (focus_widget, GDK_0, 0));
+      g_assert (gtk_test_widget_send_key (focus_widget, GDK_0 + i, 0));
+      while (gtk_events_pending ()) gtk_main_iteration ();
+      focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+      g_assert (focus_widget == fix->day_entry);
+
+      g_assert_cmpint (signal_info.count, ==, 0);
+      g_assert_cmpint (i, ==, hildon_date_editor_get_month (HILDON_DATE_EDITOR (fix->editor)));
+
+      /* ensure leading zero is accepted for day */
+      signal_info.count = 0;
+      g_assert (gtk_test_widget_send_key (focus_widget, GDK_0, 0));
+      g_assert (gtk_test_widget_send_key (focus_widget, GDK_0 + i, 0));
+      while (gtk_events_pending ()) gtk_main_iteration ();
+      focus_widget = gtk_window_get_focus (GTK_WINDOW (window));
+      g_assert (focus_widget == fix->year_entry);
+
+      g_assert_cmpint (signal_info.count, ==, 0);
+      g_assert_cmpint (i, ==, hildon_date_editor_get_day (HILDON_DATE_EDITOR (fix->editor)));
+    }
+}
+
+int
+main (int argc, char **argv)
+{
+  gtk_test_init (&argc, &argv, NULL);
+
+  if (g_test_thorough ())
+    /* FAIL: Default values for properties are set to current date, rather than default values */
+    g_test_add_func ("/date-editor/create", test_create);
+  g_test_add_func ("/date-editor/properties", test_properties);
+  g_test_add ("/date-editor/internal/focus", DateEditorFixture, 0, int_setup, test_int_focus, int_teardown);
+  g_test_add ("/date-editor/internal/validation", DateEditorFixture, 0, int_setup, test_int_validation, int_teardown);
+  g_test_add ("/date-editor/internal/leading-zero", DateEditorFixture, 0, int_setup, test_int_leading_zero, int_teardown);
+
+  return g_test_run ();
+}


More information about the maemo-commits mailing list