[gnome-software] Add gs_plugin_list_filter() and some unit tests



commit ea5eaede1aeccd72131ef764516eff96240de8c3
Author: Richard Hughes <richard hughsie com>
Date:   Tue Oct 8 17:07:09 2013 +0100

    Add gs_plugin_list_filter() and some unit tests

 src/gs-plugin.c    |   27 +++++++++++++++++++++++++++
 src/gs-plugin.h    |    7 +++++++
 src/gs-self-test.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 0 deletions(-)
---
diff --git a/src/gs-plugin.c b/src/gs-plugin.c
index c30e6b2..0754902 100644
--- a/src/gs-plugin.c
+++ b/src/gs-plugin.c
@@ -65,6 +65,33 @@ gs_plugin_list_free (GList *list)
 }
 
 /**
+ * gs_plugin_list_filter:
+ *
+ * If func() returns TRUE for the GsApp, then the app is kept.
+ **/
+void
+gs_plugin_list_filter (GList **list, GsPluginListFilter func, gpointer user_data)
+{
+       GList *l;
+       GList *new = NULL;
+       GsApp *app;
+
+       g_return_if_fail (list != NULL);
+       g_return_if_fail (func != NULL);
+
+       /* see if any of the apps need filtering */
+       for (l = *list; l != NULL; l = l->next) {
+               app = GS_APP (l->data);
+               if (func (app, user_data))
+                       gs_plugin_add_app (&new, app);
+       }
+
+       /* replace the list */
+       gs_plugin_list_free (*list);
+       *list = new;
+}
+
+/**
  * gs_plugin_list_copy:
  **/
 GList *
diff --git a/src/gs-plugin.h b/src/gs-plugin.h
index 208c1dc..bd88347 100644
--- a/src/gs-plugin.h
+++ b/src/gs-plugin.h
@@ -51,6 +51,9 @@ typedef void (*GsPluginStatusUpdate)  (GsPlugin       *plugin,
                                         GsPluginStatus  status,
                                         gpointer        user_data);
 
+typedef gboolean (*GsPluginListFilter) (GsApp          *app,
+                                        gpointer        user_data);
+
 struct GsPlugin {
        GModule                 *module;
        gdouble                  priority;      /* largest number gets run first */
@@ -107,6 +110,10 @@ void                gs_plugin_add_app                      (GList          **list,
                                                         GsApp          *app);
 void            gs_plugin_list_free                    (GList          *list);
 GList          *gs_plugin_list_copy                    (GList          *list);
+void            gs_plugin_list_filter                  (GList          **list,
+                                                        GsPluginListFilter func,
+                                                        gpointer        user_data);
+
 void            gs_plugin_status_update                (GsPlugin       *plugin,
                                                         GsApp          *app,
                                                         GsPluginStatus  status);
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index d905590..f3a4483 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -26,9 +26,58 @@
 #include <gtk/gtk.h>
 
 #include "gs-app.h"
+#include "gs-plugin.h"
 #include "gs-plugin-loader.h"
 #include "gs-plugin-loader-sync.h"
 
+static gboolean
+gs_plugin_list_filter_cb (GsApp *app, gpointer user_data)
+{
+       if (g_strcmp0 (gs_app_get_id (app), "a") == 0)
+               return FALSE;
+       if (g_strcmp0 (gs_app_get_id (app), "c") == 0)
+               return FALSE;
+       return TRUE;
+}
+
+static void
+gs_plugin_func (void)
+{
+       GList *list = NULL;
+       GList *list_dup;
+       GList *list_remove = NULL;
+       GsApp *app;
+
+       /* add a couple of duplicate IDs */
+       app = gs_app_new ("a");
+       gs_plugin_add_app (&list, app);
+       g_object_unref (app);
+
+       /* test refcounting */
+       g_assert_cmpstr (gs_app_get_id (GS_APP (list->data)), ==, "a");
+       list_dup = gs_plugin_list_copy (list);
+       gs_plugin_list_free (list);
+       g_assert_cmpint (g_list_length (list_dup), ==, 1);
+       g_assert_cmpstr (gs_app_get_id (GS_APP (list_dup->data)), ==, "a");
+       gs_plugin_list_free (list_dup);
+
+       /* test removing obects */
+       app = gs_app_new ("a");
+       gs_plugin_add_app (&list_remove, app);
+       g_object_unref (app);
+       app = gs_app_new ("b");
+       gs_plugin_add_app (&list_remove, app);
+       g_object_unref (app);
+       app = gs_app_new ("c");
+       gs_plugin_add_app (&list_remove, app);
+       g_object_unref (app);
+       g_assert_cmpint (g_list_length (list_remove), ==, 3);
+       gs_plugin_list_filter (&list_remove, gs_plugin_list_filter_cb, NULL);
+       g_assert_cmpint (g_list_length (list_remove), ==, 1);
+       g_assert_cmpstr (gs_app_get_id (GS_APP (list_remove->data)), ==, "b");
+       gs_plugin_list_free (list_remove);
+}
+
 static void
 gs_app_func (void)
 {
@@ -326,6 +375,7 @@ main (int argc, char **argv)
        g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
 
        /* tests go here */
+       g_test_add_func ("/gnome-software/plugin", gs_plugin_func);
        g_test_add_func ("/gnome-software/app", gs_app_func);
        if (g_getenv ("HAS_APPSTREAM") != NULL)
                g_test_add_func ("/gnome-software/plugin-loader{empty}", gs_plugin_loader_empty_func);


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