[libdazzle] tests: make suggestion test async



commit 549c3c156d84d0f2ad345c0334002e237421200c
Author: Christian Hergert <chergert redhat com>
Date:   Sun Oct 21 14:58:23 2018 -0700

    tests: make suggestion test async
    
    This allows us to test some corner cases better

 tests/test-suggestion.c | 48 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 4 deletions(-)
---
diff --git a/tests/test-suggestion.c b/tests/test-suggestion.c
index 719abfb..3f1c581 100644
--- a/tests/test-suggestion.c
+++ b/tests/test-suggestion.c
@@ -108,11 +108,27 @@ replace_typed_text (DzlSuggestion  *suggestion,
   return g_strdup (data->url);
 }
 
+typedef struct
+{
+  GListStore *store;
+  gchar      *full_query;
+  gchar      *query;
+} AddSearchResults;
+
+static void
+add_search_results_free (AddSearchResults *r)
+{
+  g_clear_object (&r->store);
+  g_clear_pointer (&r->query, g_free);
+  g_clear_pointer (&r->full_query, g_free);
+  g_slice_free (AddSearchResults, r);
+}
+
 static GListModel *
-create_search_results (const gchar *full_query,
-                       const gchar *query)
+add_search_results (GListStore  *store,
+                    const gchar *full_query,
+                    const gchar *query)
 {
-  GListStore *store = g_list_store_new (DZL_TYPE_SUGGESTION);
   g_autoptr(GArray) matches = NULL;
   g_autofree gchar *search_url = NULL;
   g_autofree gchar *with_slashes = g_strdup_printf ("://%s", query);
@@ -185,12 +201,21 @@ key_press (GtkWidget   *widget,
   return FALSE;
 }
 
+static gboolean
+do_add_search_results (gpointer data)
+{
+  AddSearchResults *r = data;
+  add_search_results (r->store, r->full_query, r->query);
+  return G_SOURCE_REMOVE;
+}
+
 static void
 search_changed (DzlSuggestionEntry *entry,
                 gpointer            user_data)
 {
   g_autoptr(GListModel) model = NULL;
   GString *str = g_string_new (NULL);
+  AddSearchResults *res = NULL;
   const gchar *text;
 
   g_assert (DZL_IS_SUGGESTION_ENTRY (entry));
@@ -206,7 +231,14 @@ search_changed (DzlSuggestionEntry *entry,
     }
 
   if (str->len)
-    model = create_search_results (text, str->str);
+    {
+      model = G_LIST_MODEL (g_list_store_new (DZL_TYPE_SUGGESTION));
+
+      res = g_slice_new0 (AddSearchResults);
+      res->store = g_object_ref (G_LIST_STORE (model));
+      res->full_query = g_strdup (text);
+      res->query = g_strdup (str->str);
+    }
 
   /* Update the model, but ignore selection events while
    * that happens so that we don't update the entry box.
@@ -215,6 +247,14 @@ search_changed (DzlSuggestionEntry *entry,
   dzl_suggestion_entry_set_model (entry, model);
   g_signal_handler_unblock (entry, notify_suggestion_handler);
 
+  /* Update the model asynchonrously to ensure we test that use case */
+  if (res != NULL)
+    g_timeout_add_full (G_PRIORITY_HIGH,
+                        1,
+                        do_add_search_results,
+                        res,
+                        (GDestroyNotify)add_search_results_free);
+
   g_string_free (str, TRUE);
 }
 


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