[gtk/emoji-grid: 32/34] Add a test for filterlistmodel sections




commit 4f9a092f16d9d36f9881d170a4583de6c3e70c80
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Feb 26 18:55:16 2022 -0500

    Add a test for filterlistmodel sections

 testsuite/gtk/filterlistmodel.c | 93 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 2 deletions(-)
---
diff --git a/testsuite/gtk/filterlistmodel.c b/testsuite/gtk/filterlistmodel.c
index cd109c2e0b..1b0d64404a 100644
--- a/testsuite/gtk/filterlistmodel.c
+++ b/testsuite/gtk/filterlistmodel.c
@@ -207,7 +207,7 @@ static void
 test_create (void)
 {
   GtkFilterListModel *filter;
-  
+
   filter = new_model (10, NULL, NULL);
   assert_model (filter, "1 2 3 4 5 6 7 8 9 10");
   assert_changes (filter, "");
@@ -305,7 +305,7 @@ test_change_filter (void)
 {
   GtkFilterListModel *filter;
   GtkFilter *custom;
-  
+
   filter = new_model (10, is_not_near, GUINT_TO_POINTER (5));
   assert_model (filter, "1 2 8 9 10");
   assert_changes (filter, "");
@@ -405,6 +405,94 @@ test_empty (void)
   g_object_unref (filter);
 }
 
+static int
+sort_func (gconstpointer p1,
+           gconstpointer p2,
+           gpointer      data)
+{
+  const char *s1 = gtk_string_object_get_string ((GtkStringObject *)p1);
+  const char *s2 = gtk_string_object_get_string ((GtkStringObject *)p2);
+
+  /* compare just the first byte */
+  return (int)(s1[0]) - (int)(s2[0]);
+}
+
+static gboolean
+filter_func (gpointer item,
+             gpointer data)
+{
+  const char *s = gtk_string_object_get_string ((GtkStringObject *)item);
+
+  return s[0] == s[1];
+}
+
+static void
+test_sections (void)
+{
+  GtkStringList *list;
+  const char *strings[] = {
+    "aaa",
+    "aab",
+    "abc",
+    "bbb",
+    "bq1",
+    "bq2",
+    "cc",
+    "cx",
+    NULL
+  };
+  GtkSorter *sorter;
+  GtkSortListModel *sorted;
+  GtkSorter *section_sorter;
+  guint s, e;
+  GtkFilterListModel *filtered;
+  GtkFilter *filter;
+
+  list = gtk_string_list_new (strings);
+  sorter = GTK_SORTER (gtk_string_sorter_new (gtk_property_expression_new (GTK_TYPE_STRING_OBJECT, NULL, 
"string")));
+  sorted = gtk_sort_list_model_new (G_LIST_MODEL (list), sorter);
+  section_sorter = GTK_SORTER (gtk_custom_sorter_new (sort_func, NULL, NULL));
+  gtk_sort_list_model_set_section_sorter (GTK_SORT_LIST_MODEL (sorted), section_sorter);
+  g_object_unref (section_sorter);
+
+  gtk_section_model_get_section (GTK_SECTION_MODEL (sorted), 0, &s, &e);
+  g_assert_cmpint (s, ==, 0);
+  g_assert_cmpint (e, ==, 3);
+  gtk_section_model_get_section (GTK_SECTION_MODEL (sorted), 3, &s, &e);
+  g_assert_cmpint (s, ==, 3);
+  g_assert_cmpint (e, ==, 6);
+  gtk_section_model_get_section (GTK_SECTION_MODEL (sorted), 6, &s, &e);
+  g_assert_cmpint (s, ==, 6);
+  g_assert_cmpint (e, ==, 8);
+
+  filtered = gtk_filter_list_model_new (NULL, NULL);
+  gtk_section_model_get_section (GTK_SECTION_MODEL (filtered), 0, &s, &e);
+  g_assert_cmpint (s, ==, 0);
+  g_assert_cmpint (e, ==, G_MAXUINT);
+
+  gtk_filter_list_model_set_model (filtered, G_LIST_MODEL (sorted));
+  gtk_section_model_get_section (GTK_SECTION_MODEL (filtered), 0, &s, &e);
+  g_assert_cmpint (s, ==, 0);
+  g_assert_cmpint (e, ==, 3);
+
+  filter = GTK_FILTER (gtk_custom_filter_new (filter_func, NULL, NULL));
+  gtk_filter_list_model_set_filter (filtered, filter);
+  g_object_unref (filter);
+
+  gtk_section_model_get_section (GTK_SECTION_MODEL (filtered), 0, &s, &e);
+  g_assert_cmpint (s, ==, 0);
+  g_assert_cmpint (e, ==, 2);
+  gtk_section_model_get_section (GTK_SECTION_MODEL (filtered), 2, &s, &e);
+  g_assert_cmpint (s, ==, 2);
+  g_assert_cmpint (e, ==, 3);
+  gtk_section_model_get_section (GTK_SECTION_MODEL (filtered), 3, &s, &e);
+  g_assert_cmpint (s, ==, 3);
+  g_assert_cmpint (e, ==, 4);
+
+  g_object_unref (filtered);
+  g_object_unref (sorted);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -419,6 +507,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/filterlistmodel/change_filter", test_change_filter);
   g_test_add_func ("/filterlistmodel/incremental", test_incremental);
   g_test_add_func ("/filterlistmodel/empty", test_empty);
+  g_test_add_func ("/filterlistmodel/sections", test_sections);
 
   return g_test_run ();
 }


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