[grilo-plugins/wip/carlosg/tracker3: 10/11] tests: Add tests for the tracker3 plugin



commit be070086bcfc7fa7fb384b2af5158f90e7764156
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Jun 28 17:19:35 2020 +0200

    tests: Add tests for the tracker3 plugin
    
    Use the tracker 3.0 testutils helpers to run a tracker sandbox, and put
    some sample files on it (borrowed from chromaprint and tracker-miners
    tests) so we can test features in the grilo plugin.

 meson.build                     |   1 +
 tests/meson.build               |   4 +
 tests/tracker3/data/sample.flac | Bin 0 -> 196902 bytes
 tests/tracker3/data/sample.mp3  | Bin 0 -> 61547 bytes
 tests/tracker3/data/sample.mp4  | Bin 0 -> 74438 bytes
 tests/tracker3/data/sample.ogg  | Bin 0 -> 10622 bytes
 tests/tracker3/data/sample.png  | Bin 0 -> 2374 bytes
 tests/tracker3/meson.build      |  24 ++
 tests/tracker3/test_tracker3.c  | 506 ++++++++++++++++++++++++++++++++++++++++
 9 files changed, 535 insertions(+)
---
diff --git a/meson.build b/meson.build
index e0a77fc8..5b00a9df 100644
--- a/meson.build
+++ b/meson.build
@@ -78,6 +78,7 @@ totem_plparser_dep = dependency('totem-plparser', version: '>= 3.4.1', required:
 totem_plparser_mini_dep = dependency('totem-plparser-mini', version: '>= 3.4.1', required: false)
 tracker_sparql_dep = dependency('tracker-sparql-2.0', version: '>= 2.3.0', required: false)
 tracker3_dep = dependency('tracker-sparql-3.0', required: false)
+tracker3_testutils_dep = dependency('tracker-testutils-3.0', required: false)
 
 lua_dep = dependency('lua', version: '>= 5.3.0', required: false)
 if not lua_dep.found()
diff --git a/tests/meson.build b/tests/meson.build
index 1eaf2435..4a975337 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -40,3 +40,7 @@ endif
 if local_metadata_enabled and lua_factory_enabled
     subdir('local-metadata')
 endif
+
+if tracker3_dep.found() and tracker3_testutils_dep.found()
+    subdir('tracker3')
+endif
diff --git a/tests/tracker3/data/sample.flac b/tests/tracker3/data/sample.flac
new file mode 100644
index 00000000..38ce954a
Binary files /dev/null and b/tests/tracker3/data/sample.flac differ
diff --git a/tests/tracker3/data/sample.mp3 b/tests/tracker3/data/sample.mp3
new file mode 100644
index 00000000..69e848a1
Binary files /dev/null and b/tests/tracker3/data/sample.mp3 differ
diff --git a/tests/tracker3/data/sample.mp4 b/tests/tracker3/data/sample.mp4
new file mode 100644
index 00000000..7fc8151a
Binary files /dev/null and b/tests/tracker3/data/sample.mp4 differ
diff --git a/tests/tracker3/data/sample.ogg b/tests/tracker3/data/sample.ogg
new file mode 100644
index 00000000..d0c7ad53
Binary files /dev/null and b/tests/tracker3/data/sample.ogg differ
diff --git a/tests/tracker3/data/sample.png b/tests/tracker3/data/sample.png
new file mode 100644
index 00000000..7ff9788a
Binary files /dev/null and b/tests/tracker3/data/sample.png differ
diff --git a/tests/tracker3/meson.build b/tests/tracker3/meson.build
new file mode 100644
index 00000000..4dc1f61b
--- /dev/null
+++ b/tests/tracker3/meson.build
@@ -0,0 +1,24 @@
+source_tests = [
+    'test_tracker3',
+]
+
+tracker_sandbox = find_program(tracker3_testutils_dep.get_pkgconfig_variable('command'))
+datadir = join_paths(meson.current_source_dir(), 'data')
+
+foreach t: source_tests
+    exe = executable(t, t + '.c',
+        install: false,
+        dependencies: must_deps,
+        c_args: [
+            '-DTRACKER3_PLUGIN_PATH="@0@/src/tracker3/"'.format(meson.build_root()),
+            '-DTRACKER3_DATA_PATH="@0@"'.format(datadir),
+        ])
+    test(t, tracker_sandbox,
+        args: [
+           '--index-recursive-directories', datadir,
+           '--store-tmpdir',
+            '--wait-for-miner=Files',
+            '--wait-for-miner=Extract',
+           exe
+       ])
+endforeach
diff --git a/tests/tracker3/test_tracker3.c b/tests/tracker3/test_tracker3.c
new file mode 100644
index 00000000..222e955f
--- /dev/null
+++ b/tests/tracker3/test_tracker3.c
@@ -0,0 +1,506 @@
+/*
+ * Copyright (C) 2020 Red Hat Inc.
+ *
+ * Author: Carlos Garnacho <carlosg gnome org>
+ *
+ * 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 <locale.h>
+#include <grilo.h>
+
+#define TRACKER3_ID "grl-tracker3"
+#define TRACKER3_SOURCE_ID "grl-tracker3-source"
+
+static void
+on_source_added (GrlRegistry *registry,
+                 GrlSource   *source,
+                 GMainLoop   *main_loop)
+{
+  g_assert_cmpstr (grl_source_get_id (source), ==, TRACKER3_SOURCE_ID);
+  g_main_loop_quit (main_loop);
+}
+
+static void
+test_setup (void)
+{
+  GError *error = NULL;
+  GrlRegistry *registry;
+  GMainLoop *main_loop;
+
+  registry = grl_registry_get_default ();
+  grl_registry_load_all_plugins (registry, TRUE, &error);
+  g_assert_no_error (error);
+
+  main_loop = g_main_loop_new (NULL, FALSE);
+
+  g_signal_connect (registry, "source-added",
+                    G_CALLBACK (on_source_added), main_loop);
+
+  g_main_loop_run (main_loop);
+
+  g_main_loop_unref (main_loop);
+
+  sleep (1);
+}
+
+static gint
+compare_by_url (gconstpointer a,
+                gconstpointer b)
+{
+  return g_strcmp0 (grl_media_get_url (GRL_MEDIA (a)),
+                    grl_media_get_url (GRL_MEDIA (b)));
+}
+
+static void
+test_browse (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GList *subelements, *elements, *l;
+  GError *error = NULL;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+  elements = grl_source_browse_sync (source, NULL, NULL, options, &error);
+  g_assert_no_error (error);
+
+  g_assert_cmpint (g_list_length (elements), ==, 3);
+
+  for (l = elements; l; l = l->next) {
+    GrlMedia *media = l->data;
+
+    if (g_strcmp0 (grl_media_get_title (media), "Music") == 0) {
+      subelements = grl_source_browse_sync (source, media, keys, options, &error);
+      g_assert_cmpint (g_list_length (subelements), ==, 3);
+      subelements = g_list_sort (subelements, compare_by_url);
+
+      g_assert_cmpstr (grl_media_get_url (g_list_nth_data (subelements, 0)),
+                       ==,
+                       "file://" TRACKER3_DATA_PATH "/sample.flac");
+      g_assert_cmpstr (grl_media_get_url (g_list_nth_data (subelements, 1)),
+                       ==,
+                       "file://" TRACKER3_DATA_PATH "/sample.mp3");
+      g_assert_cmpstr (grl_media_get_url (g_list_nth_data (subelements, 2)),
+                       ==,
+                       "file://" TRACKER3_DATA_PATH "/sample.ogg");
+      g_list_free_full (subelements, g_object_unref);
+    } else if (g_strcmp0 (grl_media_get_title (media), "Videos") == 0) {
+      subelements = grl_source_browse_sync (source, media, keys, options, &error);
+      g_assert_cmpint (g_list_length (subelements), ==, 1);
+      g_assert_cmpstr (grl_media_get_url (g_list_nth_data (subelements, 0)),
+                       ==,
+                       "file://" TRACKER3_DATA_PATH "/sample.mp4");
+      g_list_free_full (subelements, g_object_unref);
+    } else if (g_strcmp0 (grl_media_get_title (media), "Photos") == 0) {
+      subelements = grl_source_browse_sync (source, media, keys, options, &error);
+      g_assert_cmpint (g_list_length (subelements), ==, 1);
+      g_assert_cmpstr (grl_media_get_url (g_list_nth_data (subelements, 0)),
+                       ==,
+                       "file://" TRACKER3_DATA_PATH "/sample.png");
+      g_list_free_full (subelements, g_object_unref);
+    } else {
+      g_assert_cmpstr (grl_media_get_title (media), ==, "b0rk");
+    }
+  }
+
+  g_list_free_full (elements, g_object_unref);
+  g_object_unref (options);
+  g_list_free (keys);
+}
+
+static void
+test_browse_type_filter (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GList *elements;
+  GError *error = NULL;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+  grl_operation_options_set_type_filter (options, GRL_TYPE_FILTER_VIDEO);
+  elements = grl_source_browse_sync (source, NULL, keys, options, &error);
+  g_assert_no_error (error);
+
+  g_assert_cmpint (g_list_length (elements), ==, 1);
+
+  elements = g_list_sort (elements, compare_by_url);
+
+  g_assert_cmpstr (grl_media_get_url (g_list_nth_data (elements, 0)),
+                   ==,
+                   "file://" TRACKER3_DATA_PATH "/sample.mp4");
+
+  g_list_free_full (elements, g_object_unref);
+  g_object_unref (options);
+  g_list_free (keys);
+}
+
+static void
+test_media_from_uri (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GrlMedia *media;
+  GError *error = NULL;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+  media = grl_source_get_media_from_uri_sync (source,
+                                              "file://" TRACKER3_DATA_PATH "/sample.mp4",
+                                              keys,
+                                              options, &error);
+  g_assert_no_error (error);
+
+  g_assert_cmpstr (grl_media_get_url (media),
+                   ==,
+                   "file://" TRACKER3_DATA_PATH "/sample.mp4");
+  g_assert_cmpstr (grl_media_get_title (media),
+                   ==,
+                   "sample");
+
+  g_object_unref (options);
+  g_object_unref (media);
+  g_list_free (keys);
+}
+
+static void
+test_media_from_uri_nonexistent (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GrlMedia *media;
+  GError *error = NULL;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+  media = grl_source_get_media_from_uri_sync (source,
+                                              "file://" TRACKER3_DATA_PATH "/IDoNotExist",
+                                              keys,
+                                              options, &error);
+  g_assert_no_error (error);
+  g_assert_null (media);
+
+  g_object_unref (options);
+  g_list_free (keys);
+}
+
+static void
+test_resolve (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GrlMedia *media, *resolved;
+  GError *error = NULL;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+  media = grl_media_audio_new ();
+  grl_media_set_url (media, "file://" TRACKER3_DATA_PATH "/sample.mp3");
+
+  resolved = grl_source_resolve_sync (source,
+                                      media,
+                                      keys,
+                                      options, &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (resolved);
+  g_assert_true (resolved == media);
+
+  g_assert_cmpstr (grl_media_get_url (media),
+                   ==,
+                   "file://" TRACKER3_DATA_PATH "/sample.mp3");
+  g_assert_cmpstr (grl_media_get_title (media),
+                   ==,
+                   "Simply Juvenile");
+
+  g_object_unref (options);
+  g_object_unref (media);
+  g_list_free (keys);
+}
+
+static void
+test_resolve_non_existent (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GrlMedia *media, *resolved;
+  GError *error = NULL;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+  media = grl_media_audio_new ();
+  grl_media_set_url (media, "file://" TRACKER3_DATA_PATH "/IDoNotExist");
+
+  resolved = grl_source_resolve_sync (source,
+                                      media,
+                                      keys,
+                                      options, &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (resolved);
+
+  g_assert_cmpstr (grl_media_get_title (media),
+                   ==,
+                   NULL);
+
+  g_object_unref (options);
+  g_object_unref (media);
+  g_list_free (keys);
+}
+
+static void
+test_search (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GError *error = NULL;
+  GList *elements;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+
+  elements = grl_source_search_sync (source,
+                                     "Simply",
+                                     keys,
+                                     options, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_list_length (elements), ==, 1);
+
+  g_assert_cmpstr (grl_media_get_url (g_list_nth_data (elements, 0)),
+                   ==,
+                   "file://" TRACKER3_DATA_PATH "/sample.mp3");
+  g_assert_cmpstr (grl_media_get_title (g_list_nth_data (elements, 0)),
+                   ==,
+                   "Simply Juvenile");
+
+  g_object_unref (options);
+  g_list_free (keys);
+}
+
+static void
+test_search_non_existent (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GError *error = NULL;
+  GList *elements;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+
+  elements = grl_source_search_sync (source,
+                                     "I do not exist",
+                                     keys,
+                                     options, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_list_length (elements), ==, 0);
+
+  g_object_unref (options);
+  g_list_free (keys);
+}
+
+static void
+test_search_range_filter (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GError *error = NULL;
+  GList *elements;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+  grl_operation_options_set_key_range_filter (options,
+                                              GRL_METADATA_KEY_DURATION,
+                                              1, 4, NULL);
+
+  elements = grl_source_search_sync (source,
+                                     NULL,
+                                     keys,
+                                     options, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_list_length (elements), ==, 2);
+
+  elements = g_list_sort (elements, compare_by_url);
+
+  g_assert_cmpstr (grl_media_get_url (g_list_nth_data (elements, 0)),
+                   ==,
+                   "file://" TRACKER3_DATA_PATH "/sample.mp4");
+  g_assert_cmpstr (grl_media_get_title (g_list_nth_data (elements, 0)),
+                   ==,
+                   "sample");
+
+  g_assert_cmpstr (grl_media_get_url (g_list_nth_data (elements, 1)),
+                   ==,
+                   "file://" TRACKER3_DATA_PATH "/sample.ogg");
+  g_assert_cmpstr (grl_media_get_title (g_list_nth_data (elements, 1)),
+                   ==,
+                   "sample");
+
+  g_object_unref (options);
+  g_list_free (keys);
+}
+
+static void
+test_query (void)
+{
+  GrlRegistry *registry;
+  GrlSource *source;
+  GError *error = NULL;
+  GList *elements;
+  GrlOperationOptions *options;
+  GList *keys;
+
+  registry = grl_registry_get_default ();
+  source = grl_registry_lookup_source (registry, TRACKER3_SOURCE_ID);
+  g_assert (source);
+
+  keys = grl_metadata_key_list_new (GRL_METADATA_KEY_URL,
+                                    GRL_METADATA_KEY_TITLE,
+                                    GRL_METADATA_KEY_INVALID);
+
+  options = grl_operation_options_new (NULL);
+
+  elements = grl_source_query_sync (source,
+                                    "SELECT ('a' AS ?urn) ('file:///a' AS ?url) ('title' AS ?title) {}",
+                                    keys,
+                                    options, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_list_length (elements), ==, 1);
+
+  g_assert_cmpstr (grl_media_get_url (g_list_nth_data (elements, 0)),
+                   ==,
+                   "file:///a");
+  g_assert_cmpstr (grl_media_get_title (g_list_nth_data (elements, 0)),
+                   ==,
+                   "title");
+
+  g_object_unref (options);
+  g_list_free (keys);
+}
+
+int
+main(int argc, char **argv)
+{
+  gint result;
+
+  setlocale (LC_ALL, "");
+
+  g_setenv ("GRL_PLUGIN_PATH", TRACKER3_PLUGIN_PATH, TRUE);
+  g_setenv ("GRL_PLUGIN_LIST", TRACKER3_ID, TRUE);
+
+  grl_init (&argc, &argv);
+  g_test_init (&argc, &argv, NULL);
+
+  test_setup ();
+
+  g_test_add_func ("/tracker3/browse", test_browse);
+  g_test_add_func ("/tracker3/browse/type-filter", test_browse_type_filter);
+  g_test_add_func ("/tracker3/media-from-uri", test_media_from_uri);
+  g_test_add_func ("/tracker3/media-from-uri/non-existent", test_media_from_uri_nonexistent);
+  g_test_add_func ("/tracker3/resolve", test_resolve);
+  g_test_add_func ("/tracker3/resolve/non-existent", test_resolve_non_existent);
+  g_test_add_func ("/tracker3/search", test_search);
+  g_test_add_func ("/tracker3/search/non-existent", test_search_non_existent);
+  g_test_add_func ("/tracker3/search/range-filter", test_search_range_filter);
+  g_test_add_func ("/tracker3/query", test_query);
+
+  result = g_test_run ();
+
+  grl_deinit ();
+
+  return result;
+}


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