[grilo-plugins] tests: include tests for errors in lua sources



commit c55a863cc4f9ffa4de470a89634c65a94315d346
Author: Victor Toso <me victortoso com>
Date:   Sun Mar 13 00:34:56 2016 +0100

    tests: include tests for errors in lua sources
    
    Three tests were introduced at this time:
    * grl.callback is never called;
    * grl.callback is called when operation is finished;
    * grl.callback is not called after an async operation
    
    https://bugzilla.gnome.org/show_bug.cgi?id=763046

 tests/lua-factory/Makefile.am                      |   13 ++
 tests/lua-factory/data/test-source-lua-errors.lua  |  136 ++++++++++++++
 tests/lua-factory/test_lua_factory_source_errors.c |  189 ++++++++++++++++++++
 3 files changed, 338 insertions(+), 0 deletions(-)
---
diff --git a/tests/lua-factory/Makefile.am b/tests/lua-factory/Makefile.am
index 66e105b..6abaf62 100644
--- a/tests/lua-factory/Makefile.am
+++ b/tests/lua-factory/Makefile.am
@@ -10,6 +10,7 @@ include $(top_srcdir)/gtester.mk
 SUBDIRS = sources
 
 TEST_PROGS +=  \
+       test_lua_factory_source_errors  \
        test_lua_factory_grl_media      \
        test_lua_factory_xml_parser
 
@@ -43,6 +44,17 @@ test_lua_factory_xml_parser_CFLAGS = \
        @DEPS_CFLAGS@                   \
        $(test_lua_factory_defines)
 
+test_lua_factory_source_errors_SOURCES =       \
+       test_lua_factory_source_errors.c        \
+       luafactorytestsresources.h luafactorytestsresources.c
+
+test_lua_factory_source_errors_LDADD = \
+       @DEPS_LIBS@
+
+test_lua_factory_source_errors_CFLAGS =        \
+       @DEPS_CFLAGS@                   \
+       $(test_lua_factory_defines)
+
 lua_factory_tests_resources_files =    \
   data/grl-media-test-all-metadata.json
 
@@ -66,6 +78,7 @@ dist_noinst_DATA =                            \
        data/config.ini                         \
        data/test-source-grl-media.lua          \
        data/test-source-xml-parser.lua         \
+       data/test-source-lua-errors.lua         \
        data/xml-parser-test-simple-table.lua   \
        data/xml-parser-test-simple.xml
 
diff --git a/tests/lua-factory/data/test-source-lua-errors.lua 
b/tests/lua-factory/data/test-source-lua-errors.lua
new file mode 100644
index 0000000..cc2c2b8
--- /dev/null
+++ b/tests/lua-factory/data/test-source-lua-errors.lua
@@ -0,0 +1,136 @@
+--[[
+ * Copyright (C) 2016 Victor Toso.
+ *
+ * Contact: Victor Toso <me victortoso com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+--]]
+
+---------------------------
+-- Source initialization --
+---------------------------
+
+source = {
+  id = "test-source-lua-errors",
+  name = "Fake Source",
+  description = "a source to test GrlMedia",
+  supported_keys = { "title" },
+  supported_media = "all",
+  resolve_keys = {
+    ["type"] = "all",
+    required = { "url" } ,
+  },
+  tags = { 'test', 'net:plaintext' },
+}
+
+TEST_NOT_CALLBACK_SIMPLE = "test-not-calback-simple"
+TEST_NOT_CALLBACK_ASYNC =  "test-not-callback-on-async"
+TEST_CALLBACK_ON_FINISHED_OP = "test-callback-after-finished"
+TEST_MULTIPLE_FETCH = "multiple-fetch-with-callback"
+
+---------------------------------
+-- Handlers of Grilo functions --
+---------------------------------
+function grl_source_resolve()
+  local media = grl.get_media_keys()
+  local operation_id = grl.get_options("operation-id" )
+
+  if not media or not media.id or not media.url then
+    grl.warning ("test failed due lack of information")
+    grl.callback()
+    return
+  end
+  grl.debug ("source-resolve-test: " .. media.id)
+
+  if media.id == TEST_NOT_CALLBACK_SIMPLE then
+    test_not_callback_simple (media, operation_id)
+
+  elseif media.id == TEST_NOT_CALLBACK_ASYNC then
+    test_not_callback_async (media, operation_id)
+
+  elseif media.id == TEST_CALLBACK_ON_FINISHED_OP then
+    test_callback_on_finished_op (media, operation_id)
+
+  else
+    grl.warning ("test unknow: " .. media.id)
+    grl.callback()
+  end
+end
+
+function grl_source_search(test_id)
+  local url = "http://xml.parser.test/lua-factory/simple.xml";
+  local operation_id = grl.get_options("operation-id" )
+  grl.debug ("source-search-test: " .. test_id)
+
+  if test_id == TEST_MULTIPLE_FETCH then
+    test_multiple_fetch (url, operation_id)
+  else
+    grl.warning ("test unknow: " .. test_id)
+    grl.callback()
+  end
+end
+
+---------------------------------
+-- Handlers of Tests functions --
+---------------------------------
+function test_multiple_fetch(url, operation_id)
+  grl.debug ("calling multiple grl.fetch and only grl.callback() "
+             .. "in the last one | operation-id: " .. operation_id)
+  grl.debug (url)
+  local test_t = { num_op = 4, received = 0 }
+  for i = 1, test_t.num_op do
+    grl.debug ("operation: " .. i)
+    grl.fetch(url, fetch_multiple_url_cb, test_t)
+  end
+end
+
+function test_not_callback_simple (media, operation_id)
+    grl.debug ("not calling grl.callback, operation-id:  " .. operation_id)
+end
+
+function test_callback_on_finished_op (media, operation_id)
+  grl.debug ("calling grl.callback after operation is over, "
+             .. "operation-id: " .. operation_id)
+  grl.callback ({title = "grilo-1" }, 0)
+  grl.callback ({title = "grilo-2" }, 0)
+end
+
+function test_not_callback_async (media, operation_id)
+  grl.debug ("calling grl.fetch but not grl.callback, "
+             .. "operation-id: " .. operation_id)
+  grl.fetch(media.url, fetch_url_cb)
+end
+
+---------------------------------
+-- Callbacks --------------------
+---------------------------------
+function fetch_multiple_url_cb(feeds, data)
+  if not data then
+    grl.warning ("Fail to get userdata")
+    return
+  end
+  data.received = data.received + 1
+  grl.debug (string.format("fetch_multiple_url_cb: received %d/%d",
+                           data.received, data.num_op))
+  local media = { title = "title: " .. data.received }
+  local count = data.num_op - data.received
+  grl.callback (media, count)
+end
+
+function fetch_url_cb(feeds)
+  grl.debug ("fetch_url_cb: not calling grl.callback()" )
+end
diff --git a/tests/lua-factory/test_lua_factory_source_errors.c 
b/tests/lua-factory/test_lua_factory_source_errors.c
new file mode 100644
index 0000000..0f521a3
--- /dev/null
+++ b/tests/lua-factory/test_lua_factory_source_errors.c
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 2016. All rights reserved.
+ *
+ * Author: Victor Toso <me victortoso com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include <grilo.h>
+#include <string.h>
+
+#define TEST_URL_XML_PARSER "http://xml.parser.test/lua-factory/";
+#define LOCAL_CONTENT       TEST_URL_XML_PARSER "simple.xml"
+
+#define LUA_FACTORY_ID "grl-lua-factory"
+#define FAKE_SOURCE_ID "test-source-lua-errors"
+
+#define TEST_NOT_CALLBACK_SIMPLE      "test-not-calback-simple"
+#define TEST_NOT_CALLBACK_ASYNC       "test-not-callback-on-async"
+#define TEST_CALLBACK_ON_FINISHED_OP  "test-callback-after-finished"
+#define TEST_MULTIPLE_FETCH           "multiple-fetch-with-callback"
+
+static void
+test_lua_factory_setup (GrlConfig *config)
+{
+  GrlRegistry *registry;
+  GError *error = NULL;
+
+  registry = grl_registry_get_default ();
+
+  if (config != NULL) {
+    grl_registry_add_config (registry, config, &error);
+    g_assert_no_error (error);
+  }
+
+  grl_registry_load_all_plugins (registry, FALSE, NULL);
+  grl_registry_activate_plugin_by_id (registry, LUA_FACTORY_ID, &error);
+  g_assert_no_error (error);
+}
+
+static GrlSource*
+test_lua_factory_get_source (gchar *source_id,
+                             GrlSupportedOps source_ops)
+{
+  GrlRegistry *registry = grl_registry_get_default ();
+  GrlSource *source = grl_registry_lookup_source (registry, source_id);
+  g_assert_nonnull (source);
+  g_assert (grl_source_supported_operations (source) & source_ops);
+  return source;
+}
+
+static void
+test_lua_factory_shutdown (void)
+{
+  GrlRegistry *registry;
+  GError *error = NULL;
+
+  registry = grl_registry_get_default ();
+  grl_registry_unload_plugin (registry, LUA_FACTORY_ID, &error);
+  g_assert_no_error (error);
+}
+
+static void
+set_test_data (GrlSource **source,
+               GrlMedia **media,
+               GrlOperationOptions **options,
+               GList **keys,
+               GrlSupportedOps source_op)
+{
+  *source = test_lua_factory_get_source (FAKE_SOURCE_ID, source_op);
+  g_assert_nonnull (*source);
+
+  *media = grl_media_new ();
+  grl_data_add_string (GRL_DATA (*media), GRL_METADATA_KEY_URL, LOCAL_CONTENT);
+
+  *keys = grl_metadata_key_list_new (GRL_METADATA_KEY_TITLE, NULL);
+  *options = grl_operation_options_new (NULL);
+  grl_operation_options_set_resolution_flags (*options, GRL_RESOLVE_NORMAL);
+}
+
+static void
+free_test_data (GrlMedia **media,
+                GrlOperationOptions **options,
+                GList **keys)
+{
+  g_object_unref (*media);
+  g_list_free (*keys);
+  g_object_unref (*options);
+}
+
+static void
+execute_resolve_test (const gchar *test_id)
+{
+  GrlSource *source;
+  GrlMedia *media;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  set_test_data (&source, &media, &options, &keys, GRL_OP_RESOLVE);
+  grl_media_set_id (media, test_id);
+  grl_source_resolve_sync (source, media, keys, options, NULL);
+  free_test_data (&media, &options, &keys);
+}
+
+static void
+execute_search_test (const gchar *test_id)
+{
+  GrlSource *source;
+  GrlMedia *media;
+  GrlOperationOptions *options;
+  GList *keys, *list_medias;
+
+  set_test_data (&source, &media, &options, &keys, GRL_OP_RESOLVE);
+  list_medias = grl_source_search_sync (source, test_id, keys, options, NULL);
+  g_list_free_full (list_medias, g_object_unref);
+  free_test_data (&media, &options, &keys);
+}
+
+static void
+test_correct_state_on_multiple_fetch (void)
+{
+  execute_search_test (TEST_MULTIPLE_FETCH);
+}
+
+static void
+test_callback_after_end_of_operation (void)
+{
+  g_test_expect_message("Grilo", G_LOG_LEVEL_WARNING,
+      "*is on FINALIZED state and cannot be changed*");
+  g_test_expect_message("Grilo", G_LOG_LEVEL_WARNING,
+      "*Source is broken as callback was called after the operation has been finalized*");
+  execute_resolve_test (TEST_CALLBACK_ON_FINISHED_OP);
+}
+
+static void
+test_not_callback_on_async (void)
+{
+  g_test_expect_message("Grilo", G_LOG_LEVEL_WARNING,
+        "*Source 'test-source-lua-errors' is broken, as the finishing callback was not called for*");
+  execute_resolve_test (TEST_NOT_CALLBACK_ASYNC);
+}
+
+static void
+test_not_callback_simple (void)
+{
+  g_test_expect_message("Grilo", G_LOG_LEVEL_WARNING,
+        "*Source 'test-source-lua-errors' is broken, as the finishing callback was not called for*");
+  execute_resolve_test (TEST_NOT_CALLBACK_SIMPLE);
+}
+
+gint
+main (gint argc, gchar **argv)
+{
+  g_setenv ("GRL_PLUGIN_PATH", LUA_FACTORY_PLUGIN_PATH, TRUE);
+  g_setenv ("GRL_PLUGIN_LIST", LUA_FACTORY_ID, TRUE);
+  g_setenv ("GRL_NET_MOCKED", LUA_FACTORY_DATA_PATH "config.ini", TRUE);
+  g_setenv ("GRL_LUA_SOURCES_PATH", LUA_FACTORY_DATA_PATH, TRUE);
+
+  grl_init (&argc, &argv);
+  g_test_init (&argc, &argv, NULL);
+
+  test_lua_factory_setup (NULL);
+
+  g_test_add_func ("/lua-factory/lua-library/not-callback-simple", test_not_callback_simple);
+  g_test_add_func ("/lua-factory/lua-library/not-callback-after-async", test_not_callback_on_async);
+  g_test_add_func ("/lua-factory/lua-library/callback-after-end", test_callback_after_end_of_operation);
+  g_test_add_func ("/lua-factory/lua-library/multiple-fetch", test_correct_state_on_multiple_fetch);
+
+  gint result = g_test_run ();
+
+  test_lua_factory_shutdown ();
+  grl_deinit ();
+
+  return result;
+}


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