[gnome-todo] tests: Add tests to GtdTaskModel



commit 2d66808c9f2a42139fe6638c793ca0a4ee20e0b7
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Sep 10 22:02:36 2018 -0300

    tests: Add tests to GtdTaskModel
    
    The interactive test is a masterpiece!

 tests/interactive/test-task-model.c | 170 ++++++++++++++++++++++++++++++++++++
 tests/meson.build                   |   2 +
 tests/test-task-model.c             |  67 ++++++++++++++
 3 files changed, 239 insertions(+)
---
diff --git a/tests/interactive/test-task-model.c b/tests/interactive/test-task-model.c
new file mode 100644
index 0000000..e79847d
--- /dev/null
+++ b/tests/interactive/test-task-model.c
@@ -0,0 +1,170 @@
+/* test-task-model.c
+ *
+ * Copyright 2018 Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <gtk/gtk.h>
+
+#include "contrib/gtd-task-model.h"
+#include "contrib/gtd-task-model-private.h"
+#include "logging/gtd-log.h"
+#include "dummy-provider.h"
+#include "gtd-manager.h"
+#include "gtd-manager-protected.h"
+#include "gtd-provider.h"
+#include "gtd-task.h"
+#include "gtd-task-list.h"
+
+static GtkWidget*
+create_bold_label_for_task_row (GtkListBoxRow *row)
+{
+  g_autofree gchar *markup = NULL;
+  GtdTask *task;
+
+  task = g_object_get_data (G_OBJECT (row), "task");
+  markup = g_strdup_printf ("<big><b>%s</b></big>", gtd_task_list_get_name (gtd_task_get_list (task)));
+
+  return g_object_new (GTK_TYPE_LABEL,
+                       "margin", 6,
+                       "margin-top", 18,
+                       "use-markup", TRUE,
+                       "label", markup,
+                       "xalign", 0.0,
+                       NULL);
+}
+
+static void
+header_func (GtkListBoxRow *row,
+             GtkListBoxRow *before,
+             gpointer       user_data)
+{
+  GtkWidget *header = NULL;
+
+  if (!before)
+    {
+      header = create_bold_label_for_task_row (row);
+    }
+  else
+    {
+      GtdTask *before_task;
+      GtdTask *task;
+
+      before_task = g_object_get_data (G_OBJECT (before), "task");
+      task = g_object_get_data (G_OBJECT (row), "task");
+
+      if (gtd_task_get_list (task) != gtd_task_get_list (before_task))
+        header = create_bold_label_for_task_row (row);
+    }
+
+  gtk_list_box_row_set_header (row, header);
+}
+
+static GtkWidget*
+create_label_for_string (const gchar *string)
+{
+  return g_object_new (GTK_TYPE_LABEL,
+                       "label", string,
+                       "hexpand", TRUE,
+                       "xalign", 0.0,
+                       "margin", 6,
+                       "margin-start", 18,
+                       NULL);
+}
+
+static GtkWidget*
+create_task_cb (gpointer item,
+                gpointer user_data)
+{
+  GtkWidget *row;
+  GtkWidget *box;
+  GtdTask *task;
+
+  task = item;
+
+  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 16);
+
+  gtk_container_add (GTK_CONTAINER (box), create_label_for_string (gtd_task_get_title (task)));
+
+  row = gtk_list_box_row_new ();
+  gtk_container_add (GTK_CONTAINER (row), box);
+
+  g_object_set_data (G_OBJECT (row), "task", task);
+
+  return row;
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  g_autoptr (DummyProvider) dummy_provider = NULL;
+  GtdTaskModel *model = NULL;
+  GtkWidget *scrolledwindow = NULL;
+  GtkWindow *window = NULL;
+  GtkWidget *listbox = NULL;
+
+  g_set_prgname ("test-task-model");
+  g_set_application_name ("GNOME To Do | Task Model Test");
+
+  gtk_init ();
+  gtd_log_init ();
+
+  /* Create a DumbProvider and pre-populate it */
+  dummy_provider = dummy_provider_new ();
+  dummy_provider_generate_task_lists (dummy_provider);
+
+  /* Inject a dumb fake provider */
+  _gtd_manager_inject_provider (gtd_manager_get_default (), GTD_PROVIDER (dummy_provider));
+
+  /* Now create the model - the initial providers must be there already */
+  model = _gtd_task_model_new (gtd_manager_get_default ());
+
+  /* Listbox */
+  listbox = gtk_list_box_new ();
+  gtk_list_box_bind_model (GTK_LIST_BOX (listbox),
+                           G_LIST_MODEL (model),
+                           create_task_cb,
+                           NULL,
+                           NULL);
+
+  gtk_list_box_set_header_func (GTK_LIST_BOX (listbox), header_func, NULL, NULL);
+
+  /* Scrolled window */
+  scrolledwindow = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
+                                 "propagate-natural-height", TRUE,
+                                 "max-content-height", 600,
+                                 "hscrollbar-policy", GTK_POLICY_NEVER,
+                                 NULL);
+  gtk_container_add (GTK_CONTAINER (scrolledwindow), listbox);
+
+  /* Window */
+  window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
+  gtk_window_set_default_size (window, 800, 600);
+  gtk_container_add (GTK_CONTAINER (window), scrolledwindow);
+  gtk_window_present (window);
+
+  /* Now, generate more tasks and lists after injecting to the manager */
+  dummy_provider_generate_task_lists (dummy_provider);
+
+  /* Schedule a live removal of tasks */
+  dummy_provider_schedule_remove_task (dummy_provider);
+
+  gtk_main ();
+
+  return 0;
+}
diff --git a/tests/meson.build b/tests/meson.build
index f584e90..33e0307 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -50,6 +50,7 @@ static_test_link_args = ['-fPIC']
 
 static_tests = [
   'test-model-filter',
+  'test-task-model',
 ]
 
 foreach static_test : static_tests
@@ -77,6 +78,7 @@ endforeach
 
 interactive_tests = [
   'test-colorbutton',
+  'test-task-model',
 ]
 
 foreach interactive_test : interactive_tests
diff --git a/tests/test-task-model.c b/tests/test-task-model.c
new file mode 100644
index 0000000..dae8b40
--- /dev/null
+++ b/tests/test-task-model.c
@@ -0,0 +1,67 @@
+/* test-task-model.c
+ *
+ * Copyright 2018 Georges Basile Stavracas Neto <georges stavracas gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include "gnome-todo.h"
+
+#include "contrib/gtd-task-model.h"
+#include "contrib/gtd-task-model-private.h"
+#include "logging/gtd-log.h"
+#include "gtd-manager-protected.h"
+#include "dummy-provider.h"
+
+static void
+test_basic (void)
+{
+  g_autoptr (DummyProvider) dummy_provider = NULL;
+  GtdTaskModel *model = NULL;
+  guint n_tasks;
+
+  /* Create a DumbProvider and pre-populate it */
+  dummy_provider = dummy_provider_new ();
+  n_tasks = dummy_provider_generate_task_lists (dummy_provider);
+  _gtd_manager_inject_provider (gtd_manager_get_default (), GTD_PROVIDER (dummy_provider));
+
+  model = _gtd_task_model_new (gtd_manager_get_default ());
+  g_assert_cmpint (g_list_model_get_n_items (G_LIST_MODEL (model)), ==, n_tasks);
+
+  /* Generate more */
+  n_tasks = dummy_provider_generate_task_lists (dummy_provider);
+  g_assert_cmpint (g_list_model_get_n_items (G_LIST_MODEL (model)), ==, n_tasks);
+
+  while (n_tasks > 0)
+    {
+      n_tasks = dummy_provider_randomly_remove_task (dummy_provider);
+      g_assert_cmpint (g_list_model_get_n_items (G_LIST_MODEL (model)), ==, n_tasks);
+    }
+}
+
+gint
+main (gint argc,
+      gchar *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  if (g_getenv ("G_MESSAGES_DEBUG"))
+    gtd_log_init ();
+
+  g_test_add_func ("/contrib/task-model/basic", test_basic);
+
+  return g_test_run ();
+}


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