Blame tests/check/gst/gstregistry.c

Packit a6ee4b
/* GStreamer unit tests for the plugin registry
Packit a6ee4b
 *
Packit a6ee4b
 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
Packit a6ee4b
 *
Packit a6ee4b
 * This library is free software; you can redistribute it and/or
Packit a6ee4b
 * modify it under the terms of the GNU Library General Public
Packit a6ee4b
 * License as published by the Free Software Foundation; either
Packit a6ee4b
 * version 2 of the License, or (at your option) any later version.
Packit a6ee4b
 *
Packit a6ee4b
 * This library is distributed in the hope that it will be useful,
Packit a6ee4b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a6ee4b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a6ee4b
 * Library General Public License for more details.
Packit a6ee4b
 *
Packit a6ee4b
 * You should have received a copy of the GNU Library General Public
Packit a6ee4b
 * License along with this library; if not, write to the
Packit a6ee4b
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit a6ee4b
 * Boston, MA 02110-1301, USA.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
#ifdef HAVE_CONFIG_H
Packit a6ee4b
# include <config.h>
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
#include <gst/check/gstcheck.h>
Packit a6ee4b
#include <string.h>
Packit a6ee4b
Packit a6ee4b
static gint
Packit a6ee4b
plugin_name_cmp (GstPlugin * a, GstPlugin * b)
Packit a6ee4b
{
Packit a6ee4b
  const gchar *name_a = gst_plugin_get_name (a);
Packit a6ee4b
  const gchar *name_b = gst_plugin_get_name (b);
Packit a6ee4b
Packit a6ee4b
  return strcmp (name_a, name_b);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static gint
Packit a6ee4b
plugin_ptr_cmp (GstPlugin * a, GstPlugin * b)
Packit a6ee4b
{
Packit a6ee4b
  return (a == b) ? 0 : 1;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
print_plugin (const gchar * marker, GstRegistry * registry, GstPlugin * plugin)
Packit a6ee4b
{
Packit a6ee4b
  const gchar *name;
Packit a6ee4b
  GList *features, *f;
Packit a6ee4b
Packit a6ee4b
  name = gst_plugin_get_name (plugin);
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG ("%s: plugin %p %d %s file: %s", marker, plugin,
Packit a6ee4b
      GST_OBJECT_REFCOUNT (plugin), name,
Packit a6ee4b
      GST_STR_NULL (gst_plugin_get_filename (plugin)));
Packit a6ee4b
Packit a6ee4b
  features = gst_registry_get_feature_list_by_plugin (registry, name);
Packit a6ee4b
  for (f = features; f != NULL; f = f->next) {
Packit a6ee4b
    GstPluginFeature *feature;
Packit a6ee4b
Packit a6ee4b
    feature = GST_PLUGIN_FEATURE (f->data);
Packit a6ee4b
Packit a6ee4b
    GST_LOG ("%s:    feature: %p %s", marker, feature,
Packit a6ee4b
        GST_OBJECT_NAME (feature));
Packit a6ee4b
  }
Packit a6ee4b
  gst_plugin_feature_list_free (features);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
GST_START_TEST (test_registry_update)
Packit a6ee4b
{
Packit a6ee4b
  GstPluginFeature *old_identity, *new_identity;
Packit a6ee4b
  GstPluginFeature *old_pipeline, *new_pipeline;
Packit a6ee4b
  GstRegistry *registry;
Packit a6ee4b
  GList *plugins_before, *plugins_after, *l;
Packit a6ee4b
Packit a6ee4b
  registry = gst_registry_get ();
Packit a6ee4b
  fail_unless (registry != NULL);
Packit a6ee4b
  ASSERT_OBJECT_REFCOUNT (registry, "default registry", 1);
Packit a6ee4b
Packit a6ee4b
  /* refcount should still be 1 the second time */
Packit a6ee4b
  registry = gst_registry_get ();
Packit a6ee4b
  fail_unless (registry != NULL);
Packit a6ee4b
  ASSERT_OBJECT_REFCOUNT (registry, "default registry", 1);
Packit a6ee4b
Packit a6ee4b
  old_identity = gst_registry_lookup_feature (registry, "identity");
Packit a6ee4b
  fail_unless (old_identity != NULL, "Can't find plugin feature 'identity'");
Packit a6ee4b
Packit a6ee4b
  old_pipeline = gst_registry_lookup_feature (registry, "pipeline");
Packit a6ee4b
  fail_unless (old_pipeline != NULL, "Can't find plugin feature 'pipeline'");
Packit a6ee4b
Packit a6ee4b
  /* plugins should have a refcount of 2: the registry holds one reference,
Packit a6ee4b
   * and the other one is ours for the list */
Packit a6ee4b
  plugins_before = gst_registry_get_plugin_list (registry);
Packit a6ee4b
  for (l = plugins_before; l; l = l->next) {
Packit a6ee4b
    GstPlugin *plugin;
Packit a6ee4b
Packit a6ee4b
    plugin = GST_PLUGIN (l->data);
Packit a6ee4b
Packit a6ee4b
    print_plugin ("before1", registry, plugin);
Packit a6ee4b
Packit a6ee4b
    ASSERT_OBJECT_REFCOUNT (plugin, "plugin", 2);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  GST_LOG (" ----- calling gst_update_registry -----");
Packit a6ee4b
Packit a6ee4b
  fail_unless (gst_update_registry () != FALSE, "registry update failed");
Packit a6ee4b
Packit a6ee4b
  GST_LOG (" ----- registry updated -----");
Packit a6ee4b
Packit a6ee4b
  /* static plugins should have the same refcount as before (ie. 2), whereas
Packit a6ee4b
   * file-based plugins *may* have been replaced by a newly-created object
Packit a6ee4b
   * if the on-disk file changed (and was not yet loaded). There should be
Packit a6ee4b
   * only one reference left for those, and that's ours */
Packit a6ee4b
  for (l = plugins_before; l; l = l->next) {
Packit a6ee4b
    GstPlugin *plugin;
Packit a6ee4b
Packit a6ee4b
    plugin = GST_PLUGIN (l->data);
Packit a6ee4b
Packit a6ee4b
    print_plugin ("before2", registry, plugin);
Packit a6ee4b
Packit a6ee4b
    if (gst_plugin_get_filename (plugin)) {
Packit a6ee4b
      /* file-based plugin. */
Packit a6ee4b
      ASSERT_OBJECT_REFCOUNT_BETWEEN (plugin, "plugin", 1, 2);
Packit a6ee4b
    } else {
Packit a6ee4b
      /* static plugin */
Packit a6ee4b
      ASSERT_OBJECT_REFCOUNT (plugin, "plugin", 2);
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  GST_LOG (" -----------------------------------");
Packit a6ee4b
Packit a6ee4b
  plugins_after = gst_registry_get_plugin_list (registry);
Packit a6ee4b
  for (l = plugins_after; l; l = l->next) {
Packit a6ee4b
    GstPlugin *plugin = GST_PLUGIN (l->data);
Packit a6ee4b
Packit a6ee4b
    print_plugin ("after  ", registry, plugin);
Packit a6ee4b
Packit a6ee4b
    /* file-based plugins should have a refcount of 2 (one for the registry,
Packit a6ee4b
     * one for us for the list) or 3 (one for the registry, one for the before
Packit a6ee4b
     * list, one for the after list), static plugins should have one of 3
Packit a6ee4b
     * (one for the registry, one for the new list and one for the old list).
Packit a6ee4b
     * This implicitly also makes sure that all static plugins are the same
Packit a6ee4b
     * objects as they were before. Non-static ones may or may not have been
Packit a6ee4b
     * replaced by new objects */
Packit a6ee4b
    if (gst_plugin_get_filename (plugin)) {
Packit a6ee4b
      if (g_list_find_custom (plugins_before, plugin,
Packit a6ee4b
              (GCompareFunc) plugin_ptr_cmp) != NULL) {
Packit a6ee4b
        /* Same plugin existed in the before list. Refcount must be 3 */
Packit a6ee4b
        ASSERT_OBJECT_REFCOUNT (plugin, "plugin", 3);
Packit a6ee4b
      } else {
Packit a6ee4b
        /* This plugin is newly created, so should only exist in the after list
Packit a6ee4b
         * and the registry: Refcount must be 2 */
Packit a6ee4b
        ASSERT_OBJECT_REFCOUNT (plugin, "plugin", 2);
Packit a6ee4b
      }
Packit a6ee4b
    } else {
Packit a6ee4b
      ASSERT_OBJECT_REFCOUNT (plugin, "plugin", 3);
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* check that we still have all plugins in the new list that we had before */
Packit a6ee4b
  for (l = plugins_after; l; l = l->next) {
Packit a6ee4b
    GstPlugin *plugin;
Packit a6ee4b
Packit a6ee4b
    plugin = GST_PLUGIN (l->data);
Packit a6ee4b
Packit a6ee4b
    fail_unless (g_list_find_custom (plugins_before, plugin,
Packit a6ee4b
            (GCompareFunc) plugin_name_cmp) != NULL,
Packit a6ee4b
        "Plugin %s is in new list but not in old one?!",
Packit a6ee4b
        gst_plugin_get_name (plugin));
Packit a6ee4b
  }
Packit a6ee4b
  for (l = plugins_before; l; l = l->next) {
Packit a6ee4b
    GstPlugin *plugin;
Packit a6ee4b
Packit a6ee4b
    plugin = GST_PLUGIN (l->data);
Packit a6ee4b
    fail_unless (g_list_find_custom (plugins_after, plugin,
Packit a6ee4b
            (GCompareFunc) plugin_name_cmp) != NULL,
Packit a6ee4b
        "Plugin %s is in old list but not in new one?!",
Packit a6ee4b
        gst_plugin_get_name (plugin));
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  new_identity = gst_registry_lookup_feature (registry, "identity");
Packit a6ee4b
  fail_unless (new_identity != NULL, "Can't find plugin feature 'identity'");
Packit a6ee4b
  fail_unless (old_identity == new_identity, "Old and new 'identity' feature "
Packit a6ee4b
      "objects should be the same, but are different objects");
Packit a6ee4b
Packit a6ee4b
  /* One ref each for: the registry, old_identity, new_identity */
Packit a6ee4b
  ASSERT_OBJECT_REFCOUNT (old_identity, "old identity feature after update", 3);
Packit a6ee4b
Packit a6ee4b
  new_pipeline = gst_registry_lookup_feature (registry, "pipeline");
Packit a6ee4b
  fail_unless (new_pipeline != NULL, "Can't find plugin feature 'pipeline'");
Packit a6ee4b
  fail_unless (old_pipeline == new_pipeline, "Old and new 'pipeline' feature "
Packit a6ee4b
      "objects should be the same, but are different objects");
Packit a6ee4b
Packit a6ee4b
  gst_plugin_list_free (plugins_before);
Packit a6ee4b
  plugins_before = NULL;
Packit a6ee4b
  gst_plugin_list_free (plugins_after);
Packit a6ee4b
  plugins_after = NULL;
Packit a6ee4b
  registry = NULL;
Packit a6ee4b
Packit a6ee4b
  gst_object_unref (old_identity);
Packit a6ee4b
  gst_object_unref (new_identity);
Packit a6ee4b
  gst_object_unref (old_pipeline);
Packit a6ee4b
  gst_object_unref (new_pipeline);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
GST_END_TEST;
Packit a6ee4b
Packit a6ee4b
static Suite *
Packit a6ee4b
registry_suite (void)
Packit a6ee4b
{
Packit a6ee4b
  Suite *s = suite_create ("registry");
Packit a6ee4b
  TCase *tc_chain = tcase_create ("general");
Packit a6ee4b
Packit a6ee4b
  suite_add_tcase (s, tc_chain);
Packit a6ee4b
Packit a6ee4b
  tcase_add_test (tc_chain, test_registry_update);
Packit a6ee4b
Packit a6ee4b
  return s;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
GST_CHECK_MAIN (registry);