[gtk/matthiasc/for-master: 8/12] selection: Emit ::selection-changed for autoselection



commit 4637b8d5090fcd12516f81288a9f17c1c79af94f
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Dec 21 20:31:52 2019 -0500

    selection: Emit ::selection-changed for autoselection
    
    If a single selection gains a selected item because the
    underlying model goes from empty to non-empty, and the
    selection is set to autoselect, emit ::selection-changed.
    
    Update the existing tests that were previously checking
    the wrong thing, and add a new test specifically for this.

 gtk/gtksingleselection.c        |  1 +
 testsuite/gtk/singleselection.c | 46 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 46 insertions(+), 1 deletion(-)
---
diff --git a/gtk/gtksingleselection.c b/gtk/gtksingleselection.c
index 63db37d0de..6382485cbb 100644
--- a/gtk/gtksingleselection.c
+++ b/gtk/gtksingleselection.c
@@ -211,6 +211,7 @@ gtk_single_selection_items_changed_cb (GListModel         *model,
               self->selected = 0;
               g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED]);
               g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED_ITEM]);
+              gtk_selection_model_selection_changed (GTK_SELECTION_MODEL (self), 0, 1);
             }
         }
     }
diff --git a/testsuite/gtk/singleselection.c b/testsuite/gtk/singleselection.c
index 24de458e1c..f92e09f6dd 100644
--- a/testsuite/gtk/singleselection.c
+++ b/testsuite/gtk/singleselection.c
@@ -425,7 +425,7 @@ test_autoselect (void)
   assert_model (selection, "1");
   assert_changes (selection, "+0");
   assert_selection (selection, "1");
-  assert_selection_changes (selection, "");
+  assert_selection_changes (selection, "0:1");
 
   splice (store, 0, 1, (guint[]) { 7, 8, 9 }, 3);
   assert_model (selection, "7 8 9");
@@ -642,6 +642,49 @@ test_query_range (void)
   g_object_unref (selection);
 }
 
+static void
+selection_changed_cb (GtkSelectionModel *model,
+                      guint position,
+                      guint n_items,
+                      gpointer data)
+{
+  int *counter = data;
+
+  (*counter)++;
+}
+
+/* Test that the selection emits selection-changed when an item
+ * is autoselected due to the underlying store going from empty
+ * to non-empty.
+ */
+static void
+test_empty (void)
+{
+  GListStore *store;
+  GtkSingleSelection *selection;
+  int counter;
+  GtkFilter *filter;
+
+  store = g_list_store_new (GTK_TYPE_FILTER);
+  
+  counter = 0;
+  selection = gtk_single_selection_new (G_LIST_MODEL (store));
+  g_signal_connect (selection, "selection-changed", G_CALLBACK (selection_changed_cb), &counter);
+
+  g_assert_cmpint (gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (selection)), ==, 
GTK_INVALID_LIST_POSITION);
+
+  filter = gtk_string_filter_new ();
+  g_list_store_append (store, filter);
+  g_object_unref (filter);
+
+  g_assert_cmpint (gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (selection)), ==, 0);
+
+  g_assert_cmpint (counter, ==, 1);
+
+  g_object_unref (selection);
+  g_object_unref (store);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -660,6 +703,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/singleselection/persistence", test_persistence);
   g_test_add_func ("/singleselection/query-range", test_query_range);
   g_test_add_func ("/singleselection/changes", test_changes);
+  g_test_add_func ("/singleselection/empty", test_empty);
 
   return g_test_run ();
 }


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