[grilo-plugins] tests: lua-factory check media from lua sources



commit 1c6519bed0739219945f3730547aaaccc08b9416
Author: Victor Toso <me victortoso com>
Date:   Sat Sep 19 23:16:28 2015 +0200

    tests: lua-factory check media from lua sources
    
    Introducing tests to the lua-factory plugin and its API.
    The test-lua-factory-grl-media has the goal to check if the data
    provided by lua-library is the same that the lua-source received.
    
    This is done by 'fake' lua-source provided for this test.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=755447

 tests/lua-factory/Makefile.am                      |   51 ++++
 tests/lua-factory/data/config.ini                  |    7 +
 .../data/grl-media-test-all-metadata.json          |   59 ++++
 tests/lua-factory/data/test-source-grl-media.lua   |   70 +++++
 tests/lua-factory/test_lua_factory_grl_media.c     |  295 ++++++++++++++++++++
 .../test_lua_factory_tests.gresource.xml           |    6 +
 6 files changed, 488 insertions(+), 0 deletions(-)
---
diff --git a/tests/lua-factory/Makefile.am b/tests/lua-factory/Makefile.am
index 88d78a4..cbca250 100644
--- a/tests/lua-factory/Makefile.am
+++ b/tests/lua-factory/Makefile.am
@@ -9,4 +9,55 @@ include $(top_srcdir)/gtester.mk
 
 SUBDIRS = sources
 
+TEST_PROGS +=  \
+       test_lua_factory_grl_media
+
+test_lua_factory_defines =     \
+       -DLUA_FACTORY_PLUGIN_PATH=\""$(abs_top_srcdir)/src/lua-factory/.libs/"\"        \
+       -DLUA_FACTORY_SOURCES_PATH=\""$(abs_top_srcdir)/src/lua-factory/sources/"\"     \
+       -DLUA_FACTORY_DATA_PATH=\""$(abs_top_srcdir)/tests/lua-factory/data/"\"         \
+       -DLUA_FACTORY_PLUGIN_TEST_DATA_PATH=\""$(abs_top_builddir)/tests/lua-factory/sources/"\"
+
+test_lua_factory_grl_media_SOURCES =   \
+       test_lua_factory_grl_media.c    \
+       luafactorytestsresources.h luafactorytestsresources.c
+
+test_lua_factory_grl_media_LDADD =     \
+       @DEPS_LIBS@                     \
+       @JSON_LIBS@
+
+test_lua_factory_grl_media_CFLAGS =    \
+       @DEPS_CFLAGS@                   \
+       @JSON_CFLAGS@                   \
+       $(test_lua_factory_defines)
+
+lua_factory_tests_resources_files =    \
+  data/grl-media-test-all-metadata.json
+
+luafactorytestsresources.h: test_lua_factory_tests.gresource.xml
+       $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $(srcdir)/test_lua_factory_tests.gresource.xml \
+               --target=$@ --sourcedir=$(srcdir) --c-name _grl_lua_factory_tests --generate-header
+
+luafactorytestsresources.c:    \
+       test_lua_factory_tests.gresource.xml    \
+       luafactorytestsresources.h              \
+       $(lua_factory_tests_resources_files)
+       $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $(srcdir)/test_lua_factory_tests.gresource.xml \
+               --target=$@ --sourcedir=$(srcdir) --c-name _grl_lua_factory_tests --generate-source
+
+CLEANFILES = \
+       luafactorytestsresources.h      \
+       luafactorytestsresources.c
+
+dist_noinst_DATA =                             \
+       data/config.ini                         \
+       data/test-source-grl-media.lua          \
+       data/grl-media-test-all-metadata.json
+
+noinst_PROGRAMS = $(TEST_PROGS)
+
+EXTRA_DIST += \
+       $(lua_factory_tests_resources_files)    \
+       test_lua_factory_tests.gresource.xml
+
 -include $(top_srcdir)/git.mk
diff --git a/tests/lua-factory/data/config.ini b/tests/lua-factory/data/config.ini
new file mode 100644
index 0000000..369c8ec
--- /dev/null
+++ b/tests/lua-factory/data/config.ini
@@ -0,0 +1,7 @@
+[default]
+version=1
+
+# test_lua_factory_grl_media
+[http://grl.media.test/lua-factory/all-metadata.json]
+data = grl-media-test-all-metadata.json
+
diff --git a/tests/lua-factory/data/grl-media-test-all-metadata.json 
b/tests/lua-factory/data/grl-media-test-all-metadata.json
new file mode 100644
index 0000000..60acfca
--- /dev/null
+++ b/tests/lua-factory/data/grl-media-test-all-metadata.json
@@ -0,0 +1,59 @@
+{
+    "album" : "A Night At The Opera",
+    "artist" : "Queen",
+    "author" : "Queen",
+    "publication-date" : "1975-11-21",
+    "description" : "A Night at the Opera is the fourth studio album by the British rock band Queen, 
released in November 1975. Co-produced by Roy Thomas Baker and Queen, it was the most expensive album ever 
recorded at the time of its release. The album takes its name from the Marx Brothers film A Night at the 
Opera, which the band watched one night at the studio complex when recording.",
+    "genre" : [ "Hard rock", "progressive rock" ],
+    "id" : "media/queen/a-night-at-the-opera",
+    "last-played-time" : "2015-09-16T18:06:14Z",
+    "lyrics" : "Is this the real life?\nIs this just fantasy?\nCaught in a landslide\nNo escape from 
reality\nOpen your eyes\nLook up to the skies and see\nI'm just a poor boy, I need no sympathy\n",
+    "mime-type" : "application/octet-stream",
+    "site" : "https://wiki.gnome.org/Projects/Grilo";,
+    "source" : "grl-fake-test-src",
+    "thumbnail" : "https://www.gnome.org/wp-content/themes/gnome-grass/images/gnome-logo.png";,
+    "thumbnail-binary" : "ffd8ffe000104a464946000102000064",
+    "title" : "Queen with A Night At The Opera",
+    "url" : "https://en.wikipedia.org/wiki/A_Night_at_the_Opera_%28Queen_album%29";,
+    "external-url" : "https://en.wikipedia.org/wiki/Queen_%28band%29";,
+    "external-player" : "none",
+    "studio" : "Sarm, Roadhouse, Olympic Studios, Scorpio and Lansdowne, London and Rockfield, 
Monmouthshire",
+    "certificate" : "none",
+    "license" : "http://creativecommons.org/licenses/by-sa/3.0/";,
+    "bitrate" : 256,
+    "childcount" : -1,
+    "duration" : 2590,
+    "height" : 1080,
+    "playback-interrupted-time" : 1295,
+    "play-count" : 372,
+    "width" : 1920,
+    "framerate" : 29.9,
+    "rating" : 9.8,
+    "season" : 19,
+    "episode" : 75,
+    "episode-title" : "Freddie Mercury - a British singer, songwriter and producer",
+    "show" : "Queen",
+    "creation-date" : "2015-09-16T18:06:14+00:00",
+    "camera-model" : "grilo-shot 2",
+    "orientation" : 36,
+    "flash-used" : false,
+    "exposure-time" : 7.3,
+    "iso-speed" : 400,
+    "track-number" : 2,
+    "modification-date" : "20150916T180614Z",
+    "start-time" : 0.3,
+    "favourite" : true,
+    "region" : "Tanzania",
+    "keyword" : [ "queen", "rock", "hard rock" ],
+    "performer" : [ "Brian May", "Roger Taylor", "Freddie Mercury", "John Deacon", "Paul Rodgers", "Adam 
Lambert" ],
+    "producer" : "Roy Thomas Baker",
+    "director" : "Stephen Frears",
+    "original-title" : "Queen",
+    "size" : 12839123,
+    "title-from-filename" : false,
+    "mb-album-id" : "f14638e1-fb36-358c-aba2-39b084864b13",
+    "mb-track-id": "d864d1df-15b4-3e5f-8d34-bf873a3d5dfe",
+    "mb-artist-id" : "0383dadf-2a4e-4d10-a46a-e9e041da8eb3",
+    "mb-recording-id" : "4572c539-07be-4398-ade4-ac443c3883f1",
+    "audio-track" : 3
+}
diff --git a/tests/lua-factory/data/test-source-grl-media.lua 
b/tests/lua-factory/data/test-source-grl-media.lua
new file mode 100644
index 0000000..849c36d
--- /dev/null
+++ b/tests/lua-factory/data/test-source-grl-media.lua
@@ -0,0 +1,70 @@
+--[[
+ * Copyright (C) 2015 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-grl-media",
+  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' },
+}
+
+---------------------------------
+-- Handlers of Grilo functions --
+---------------------------------
+
+function grl_source_resolve()
+  -- This source expects an url which will be fetched and converted
+  -- to a GrlMedia with grl.lua.json.string_to_table().
+  local req = grl.get_media_keys()
+  if not req or not req.url or #req.url == 0 then
+    grl.warning("resolve was called without metadata-key url")
+    grl.callback()
+    return
+  end
+  grl.fetch(req.url, "fetch_url_cb")
+end
+
+function fetch_url_cb(feed)
+  if not feed or #feed == 0 then
+    grl.warning("failed to load json")
+    grl.callback()
+    return
+  end
+
+  local media = grl.lua.json.string_to_table(feed)
+  if not media then
+    grl.warning ("fail to make media from json")
+    grl.callback()
+    return
+  end
+  grl.callback(media, 0)
+end
diff --git a/tests/lua-factory/test_lua_factory_grl_media.c b/tests/lua-factory/test_lua_factory_grl_media.c
new file mode 100644
index 0000000..5dadc92
--- /dev/null
+++ b/tests/lua-factory/test_lua_factory_grl_media.c
@@ -0,0 +1,295 @@
+/*
+ * Copyright (C) 2015. 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>
+#include <json-glib/json-glib.h>
+
+#define GRESOURCE_PREFIX "resource:///org/gnome/grilo/plugins/test/lua-factory/data/"
+
+#define TEST_GRL_MEDIA_URL "http://grl.media.test/lua-factory/";
+
+#define LUA_FACTORY_ID "grl-lua-factory"
+#define FAKE_SOURCE_ID "test-source-grl-media"
+
+
+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_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
+check_metadata (GrlMedia *media,
+                GrlKeyID key_id,
+                JsonReader *reader)
+{
+  GrlRegistry *registry = grl_registry_get_default ();
+  GType type = grl_registry_lookup_metadata_key_type (registry, key_id);
+
+  switch (type) {
+  case G_TYPE_INT:
+  case G_TYPE_INT64: {
+    gint64 from_json = json_reader_get_int_value (reader);
+    gint64 from_media = (type == G_TYPE_INT) ?
+        grl_data_get_int (GRL_DATA (media), key_id) :
+        grl_data_get_int64 (GRL_DATA (media), key_id);
+    g_assert_cmpint (from_json, ==, from_media);
+  }
+  break;
+
+  case G_TYPE_FLOAT: {
+    gfloat from_json = (gfloat) json_reader_get_double_value (reader);
+    gfloat from_media = grl_data_get_float (GRL_DATA (media), key_id);
+    g_assert_cmpfloat (from_json, ==, from_media);
+  }
+  break;
+
+  case G_TYPE_STRING:
+    if (json_reader_is_array (reader)) {
+      GList *list = grl_data_get_single_values_for_key_string (GRL_DATA (media),
+                                                               key_id);
+      guint num_elements = json_reader_count_elements (reader);
+      guint i;
+      for (i = 0; i < num_elements; i++) {
+        GList *it = NULL;
+        json_reader_read_element (reader, i);
+        const gchar *from_json = json_reader_get_string_value (reader);
+        json_reader_end_element (reader);
+        for (it = list; it != NULL; it = it->next) {
+          const gchar *from_media = it->data;
+          if (g_strcmp0(from_json, from_media) == 0)
+            break;
+        }
+        list = g_list_delete_link (list, it);
+      }
+      g_assert_null (list);
+
+    } else {
+      const gchar *from_json = json_reader_get_string_value (reader);
+      const gchar *from_media = grl_data_get_string (GRL_DATA (media), key_id);
+      g_assert_cmpstr (from_json, ==, from_media);
+    }
+  break;
+
+  case G_TYPE_BOOLEAN: {
+    gboolean from_json = json_reader_get_boolean_value (reader);
+    gboolean from_media = grl_data_get_boolean (GRL_DATA (media), key_id);
+    g_assert_cmpint (from_json, ==, from_media);
+  }
+  break;
+
+  default:
+    /* Non-fundamental types don't reduce to ints, so can't be
+     * in the switch statement */
+    if (type == G_TYPE_DATE_TIME) {
+      const gchar *date_str = json_reader_get_string_value (reader);
+      GDateTime *from_json = grl_date_time_from_iso8601 (date_str);
+      GDateTime *from_media = grl_data_get_boxed (GRL_DATA (media), key_id);
+
+      /* Try a number of seconds since Epoch */
+      if (!from_json) {
+        gint64 date_int = g_ascii_strtoll (date_str, NULL, 0);
+        if (date_int)
+          from_json = g_date_time_new_from_unix_utc (date_int);
+      }
+
+      g_assert_true (g_date_time_compare (from_json, from_media) == 0);
+      g_date_time_unref (from_json);
+
+    } else if (type == G_TYPE_BYTE_ARRAY) {
+       gsize size;
+       const gpointer from_media = (const gpointer) grl_data_get_binary (GRL_DATA (media), key_id, &size);
+       const gpointer from_json = (const gpointer) json_reader_get_string_value (reader);
+       g_assert_true (memcmp(from_media, from_json, size) == 0);
+
+    } else {
+      g_assert_not_reached ();
+    }
+  }
+}
+
+static void
+test_metadata_from_media (GrlMedia *media,
+                          const gchar *input)
+{
+  JsonParser *parser = NULL;
+  JsonReader *reader = NULL;
+  GError *error = NULL;
+
+  GrlRegistry *registry = grl_registry_get_default ();
+  parser = json_parser_new ();
+  json_parser_load_from_data (parser, input, -1, &error);
+  g_assert_no_error (error);
+
+  /* FIXME: use https://bugzilla.gnome.org/show_bug.cgi?id=755448 */
+  reader = json_reader_new (json_parser_get_root (parser));
+  if (json_reader_is_object (reader)) {
+    guint i, len;
+    len = json_reader_count_members (reader);
+
+    for (i = 0; i < len; i++) {
+      GrlKeyID key_id = GRL_METADATA_KEY_INVALID;
+      const gchar *key_name;
+
+      json_reader_read_element (reader, i);
+      key_name = json_reader_get_member_name (reader);
+      if (g_strcmp0 (key_name, "type") != 0) {
+        key_id = grl_registry_lookup_metadata_key (registry, key_name);
+        g_assert (key_id != GRL_METADATA_KEY_INVALID);
+        check_metadata (media, key_id, reader);
+      }
+      json_reader_end_element (reader);
+    }
+  }
+
+  g_object_unref (reader);
+  g_object_unref (parser);
+}
+
+static GrlMedia *
+get_media (GrlSource *source,
+           GrlMedia *media)
+{
+  GrlOperationOptions *options;
+  GList *keys;
+
+  /* Keys can't be NULL, by default we ask for title */
+  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);
+
+  grl_source_resolve_sync (source, media, keys, options, NULL);
+
+  g_list_free (keys);
+  g_object_unref (options);
+  return media;
+}
+
+static void
+resolve_fake_src (const gchar *input,
+                  const gchar *url)
+{
+  GrlSource *source = NULL;
+  GrlMedia *media = NULL;
+
+  source = test_lua_factory_get_source (FAKE_SOURCE_ID, GRL_OP_RESOLVE);
+  g_assert_nonnull (source);
+
+  media = grl_media_new ();
+  grl_media_set_url (media, url);
+  media = get_media (source, media);
+  g_assert_nonnull (media);
+  test_metadata_from_media (media, input);
+  g_object_unref (media);
+}
+
+static void
+test_build_media (void)
+{
+  gint i;
+
+  struct {
+    gchar *uri;
+    gchar *url;
+  } media_tests[] = {
+    /* This is a basic test to check all metadata keys provided by core in the
+     * simplest way possible. Boolean, strings, numbers, arrays. */
+    { GRESOURCE_PREFIX "grl-media-test-all-metadata.json",
+      TEST_GRL_MEDIA_URL "all-metadata.json" }
+  };
+
+  for (i = 0; i < G_N_ELEMENTS (media_tests); i++) {
+    GFile *file;
+    gchar *input;
+    GError *error = NULL;
+
+    file = g_file_new_for_uri (media_tests[i].uri);
+    g_file_load_contents (file, NULL, &input, NULL, NULL, &error);
+    g_assert_no_error (error);
+    g_object_unref (file);
+    resolve_fake_src (input, media_tests[i].url);
+    g_free (input);
+  }
+}
+
+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);
+
+  /* Check if metadata-keys are created with value we expect with no errors */
+  g_test_add_func ("/lua-factory/lua-library/metadata-keys", test_build_media);
+  /* TODO:
+   * 1-) keys with array of all provided by grl_data_add_* (binary, boxed, float,..)
+   * 2-) test for GrlRelatedKeys after: https://bugzilla.gnome.org/show_bug.cgi?id=756203
+   */
+
+  gint result = g_test_run ();
+
+  test_lua_factory_shutdown ();
+  grl_deinit ();
+
+  return result;
+}
diff --git a/tests/lua-factory/test_lua_factory_tests.gresource.xml 
b/tests/lua-factory/test_lua_factory_tests.gresource.xml
new file mode 100644
index 0000000..f936db6
--- /dev/null
+++ b/tests/lua-factory/test_lua_factory_tests.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/grilo/plugins/test/lua-factory/">
+    <file compressed="false">data/grl-media-test-all-metadata.json</file>
+  </gresource>
+</gresources>


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