[gnome-software] trivial: Add some self tests for the shell-extension plugin



commit 0ceb0f0e68bc6a4fab5ad42a87ef984e3f2e54ca
Author: Richard Hughes <richard hughsie com>
Date:   Fri Mar 10 10:13:16 2017 +0000

    trivial: Add some self tests for the shell-extension plugin

 plugins/shell-extensions/Makefile.am               |   14 ++-
 .../shell-extensions/gs-plugin-shell-extensions.c  |   16 ++-
 plugins/shell-extensions/gs-self-test.c            |  148 ++++++++++++++++++++
 3 files changed, 172 insertions(+), 6 deletions(-)
---
diff --git a/plugins/shell-extensions/Makefile.am b/plugins/shell-extensions/Makefile.am
index f3604bd..186b613 100644
--- a/plugins/shell-extensions/Makefile.am
+++ b/plugins/shell-extensions/Makefile.am
@@ -1,5 +1,7 @@
 AM_CPPFLAGS =                                          \
-       -DG_LOG_DOMAIN=\"GsPluginShellExtensions\"
+       -DG_LOG_DOMAIN=\"GsPluginShellExtensions\"      \
+       -DTESTDATADIR=\""$(srcdir)/tests"\"             \
+       -DLOCALPLUGINDIR=\""$(builddir)/.libs"\"
 
 plugindir = $(GS_PLUGIN_DIR)
 plugin_LTLIBRARIES = libgs_plugin_shell-extensions.la
@@ -8,4 +10,14 @@ libgs_plugin_shell_extensions_la_LIBADD = $(GS_PLUGIN_LIBS) $(JSON_GLIB_LIBS)
 libgs_plugin_shell_extensions_la_LDFLAGS = -module -avoid-version
 libgs_plugin_shell_extensions_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(JSON_GLIB_CFLAGS)
 
+if ENABLE_TESTS
+check_PROGRAMS = gs-self-test
+gs_self_test_SOURCES = gs-self-test.c
+gs_self_test_LDADD = $(top_builddir)/lib/libgnomesoftware.a $(GS_PRIVATE_LIBS)
+gs_self_test_CFLAGS = $(GS_PRIVATE_CFLAGS)
+TESTS = gs-self-test
+endif
+
+#EXTRA_DIST = tests/chiron-1.1-1.fc24.x86_64.rpm
+
 -include $(top_srcdir)/git.mk
diff --git a/plugins/shell-extensions/gs-plugin-shell-extensions.c 
b/plugins/shell-extensions/gs-plugin-shell-extensions.c
index 6d55b4c..9abc3e5 100644
--- a/plugins/shell-extensions/gs-plugin-shell-extensions.c
+++ b/plugins/shell-extensions/gs-plugin-shell-extensions.c
@@ -699,6 +699,7 @@ gs_plugin_shell_extensions_refresh (GsPlugin *plugin,
                                    GError **error)
 {
        AsApp *app;
+       const gchar *fn_test;
        guint i;
        g_autofree gchar *fn = NULL;
        g_autoptr(GPtrArray) apps = NULL;
@@ -710,11 +711,16 @@ gs_plugin_shell_extensions_refresh (GsPlugin *plugin,
                return TRUE;
 
        /* check age */
-       fn = g_build_filename (g_get_user_data_dir (),
-                              "app-info",
-                              "xmls",
-                              "extensions-web.xml",
-                              NULL);
+       fn_test = g_getenv ("GS_SELF_TEST_SHELL_EXTENSIONS_XML_FN");
+       if (fn_test != NULL) {
+               fn = g_strdup (fn_test);
+       } else {
+               fn = g_build_filename (g_get_user_data_dir (),
+                                      "app-info",
+                                      "xmls",
+                                      "extensions-web.xml",
+                                      NULL);
+       }
        file = g_file_new_for_path (fn);
        if (g_file_query_exists (file, NULL)) {
                guint age = gs_utils_get_file_age (file);
diff --git a/plugins/shell-extensions/gs-self-test.c b/plugins/shell-extensions/gs-self-test.c
new file mode 100644
index 0000000..0bb98e5
--- /dev/null
+++ b/plugins/shell-extensions/gs-self-test.c
@@ -0,0 +1,148 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <glib/gstdio.h>
+
+#include "gnome-software-private.h"
+
+#include "gs-test.h"
+
+static void
+gs_plugins_shell_extensions_installed_func (GsPluginLoader *plugin_loader)
+{
+       GsApp *app;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GsAppList) list = NULL;
+
+       /* no shell-extensions, abort */
+       if (!gs_plugin_loader_get_enabled (plugin_loader, "shell-extensions")) {
+               g_test_skip ("not enabled");
+               return;
+       }
+
+       /* get installed packages */
+       list = gs_plugin_loader_get_installed (plugin_loader,
+                                              GS_PLUGIN_REFINE_FLAGS_DEFAULT,
+                                              GS_PLUGIN_FAILURE_FLAGS_FATAL_ANY,
+                                              NULL, &error);
+       gs_test_flush_main_context ();
+       g_assert_no_error (error);
+       g_assert (list != NULL);
+       g_assert_cmpint (gs_app_list_length (list), >, 1);
+
+       /* test properties */
+       app = gs_app_list_lookup (list, "*/*/*/*/background-logo_fedorahosted.org/*");
+       if (app == NULL) {
+               g_test_skip ("not found");
+               return;
+       }
+
+       g_assert_cmpint (gs_app_get_state (app), ==, AS_APP_STATE_INSTALLED);
+       g_assert_cmpint (gs_app_get_scope (app), ==, AS_APP_SCOPE_USER);
+       g_assert_cmpstr (gs_app_get_name (app), ==, "Background Logo");
+       g_assert_cmpstr (gs_app_get_summary (app), ==, "GNOME Shell Extension");
+       g_assert_cmpstr (gs_app_get_description (app), ==,
+                        "Overlay a tasteful logo on the background to "
+                        "enhance the user experience");
+       g_assert_cmpstr (gs_app_get_license (app), ==, "GPL-2.0+");
+       g_assert_cmpstr (gs_app_get_management_plugin (app), ==, "shell-extensions");
+       g_assert (gs_app_has_category (app, "Addons"));
+       g_assert (gs_app_has_category (app, "ShellExtensions"));
+       g_assert_cmpstr (gs_app_get_metadata_item (app, "shell-extensions::has-prefs"), ==, "");
+       g_assert_cmpstr (gs_app_get_metadata_item (app, "shell-extensions::uuid"), ==,
+                        "background-logo fedorahosted org");
+}
+
+static void
+gs_plugins_shell_extensions_remote_func (GsPluginLoader *plugin_loader)
+{
+       const gchar *xml_fn = "/var/tmp/self-test/extensions-web.xml";
+       gboolean ret;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GFile) file = NULL;
+       g_autoptr(AsStore) store = NULL;
+
+       /* ensure files are removed */
+       g_unlink (xml_fn);
+
+       /* refresh the metadata */
+       g_setenv ("GS_SELF_TEST_SHELL_EXTENSIONS_XML_FN", xml_fn, TRUE);
+       ret = gs_plugin_loader_refresh (plugin_loader,
+                                       G_MAXUINT,
+                                       GS_PLUGIN_REFRESH_FLAGS_METADATA,
+                                       GS_PLUGIN_FAILURE_FLAGS_FATAL_ANY,
+                                       NULL,
+                                       &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+
+       /* ensure file was populated */
+       store = as_store_new ();
+       file = g_file_new_for_path (xml_fn);
+       ret = as_store_from_file (store, file, NULL, NULL, &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+       g_assert_cmpint (as_store_get_size (store), >, 20);
+}
+
+int
+main (int argc, char **argv)
+{
+       gboolean ret;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GsPluginLoader) plugin_loader = NULL;
+       const gchar *whitelist[] = {
+               "shell-extensions",
+               NULL
+       };
+
+       g_test_init (&argc, &argv, NULL);
+       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+
+       /* only critical and error are fatal */
+       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+       /* we can only load this once per process */
+       plugin_loader = gs_plugin_loader_new ();
+       gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR);
+       ret = gs_plugin_loader_setup (plugin_loader,
+                                     (gchar**) whitelist,
+                                     NULL,
+                                     GS_PLUGIN_FAILURE_FLAGS_NONE,
+                                     NULL,
+                                     &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+
+       /* plugin tests go here */
+       g_test_add_data_func ("/gnome-software/plugins/shell-extensions/installed",
+                             plugin_loader,
+                             (GTestDataFunc) gs_plugins_shell_extensions_installed_func);
+       g_test_add_data_func ("/gnome-software/plugins/shell-extensions/remote",
+                             plugin_loader,
+                             (GTestDataFunc) gs_plugins_shell_extensions_remote_func);
+
+       return g_test_run ();
+}
+
+/* vim: set noexpandtab: */


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