context: Fix unit test for GstContext changes

This commit is contained in:
Sebastian Dröge 2013-09-17 21:36:22 +02:00
parent aacedb9da4
commit 921ab73340
4 changed files with 23 additions and 72 deletions

View file

@ -99,6 +99,7 @@ _gst_context_free (GstContext * context)
gst_structure_set_parent_refcount (structure, NULL); gst_structure_set_parent_refcount (structure, NULL);
gst_structure_free (structure); gst_structure_free (structure);
} }
g_free (context->context_type);
g_slice_free1 (sizeof (GstContext), context); g_slice_free1 (sizeof (GstContext), context);
} }

View file

@ -2245,7 +2245,7 @@ gst_message_new_need_context (GstObject * src, const gchar * context_type)
g_return_val_if_fail (context_type != NULL, NULL); g_return_val_if_fail (context_type != NULL, NULL);
structure = gst_structure_new_id (GST_QUARK (MESSAGE_NEED_CONTEXT), structure = gst_structure_new_id (GST_QUARK (MESSAGE_NEED_CONTEXT),
GST_QUARK (CONTEXT_TYPE), context_type, NULL); GST_QUARK (CONTEXT_TYPE), G_TYPE_STRING, context_type, NULL);
message = gst_message_new_custom (GST_MESSAGE_NEED_CONTEXT, src, structure); message = gst_message_new_custom (GST_MESSAGE_NEED_CONTEXT, src, structure);
return message; return message;

View file

@ -2508,7 +2508,7 @@ gst_query_new_context (const gchar * context_type)
g_return_val_if_fail (context_type != NULL, NULL); g_return_val_if_fail (context_type != NULL, NULL);
structure = gst_structure_new_id (GST_QUARK (QUERY_CONTEXT), structure = gst_structure_new_id (GST_QUARK (QUERY_CONTEXT),
GST_QUARK (CONTEXT_TYPE), context_type, NULL); GST_QUARK (CONTEXT_TYPE), G_TYPE_STRING, context_type, NULL);
query = gst_query_new_custom (GST_QUERY_CONTEXT, structure); query = gst_query_new_custom (GST_QUERY_CONTEXT, structure);
return query; return query;

View file

@ -1,6 +1,7 @@
/* GStreamer /* GStreamer
* Copyright (C) 2013 Collabora Ltd. * Copyright (C) 2013 Collabora Ltd.
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk> * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
* Copyright (C) 2013 Sebastian Dröge <slomo@circular-chaos.org>
* *
* gstcontext.c: Unit test for GstContext * gstcontext.c: Unit test for GstContext
* *
@ -28,7 +29,7 @@ GST_START_TEST (test_basic)
GstStructure *s1; GstStructure *s1;
const GstStructure *s2; const GstStructure *s2;
c1 = gst_context_new (); c1 = gst_context_new ("test", FALSE);
fail_unless (c1 != NULL); fail_unless (c1 != NULL);
fail_unless (GST_IS_CONTEXT (c1)); fail_unless (GST_IS_CONTEXT (c1));
s1 = gst_context_writable_structure (c1); s1 = gst_context_writable_structure (c1);
@ -38,6 +39,8 @@ GST_START_TEST (test_basic)
c2 = gst_context_copy (c1); c2 = gst_context_copy (c1);
fail_unless (c2 != NULL); fail_unless (c2 != NULL);
fail_unless (GST_IS_CONTEXT (c2)); fail_unless (GST_IS_CONTEXT (c2));
fail_unless (strcmp (gst_context_get_context_type (c1),
gst_context_get_context_type (c2)) == 0);
s2 = gst_context_get_structure (c2); s2 = gst_context_get_structure (c2);
fail_unless (s2 != NULL); fail_unless (s2 != NULL);
fail_unless (gst_structure_is_equal (s1, s2)); fail_unless (gst_structure_is_equal (s1, s2));
@ -54,6 +57,8 @@ typedef struct
gboolean set_before_ready; gboolean set_before_ready;
gboolean set_from_need_context; gboolean set_from_need_context;
gboolean create_self; gboolean create_self;
gboolean have_foobar;
} GstContextElement; } GstContextElement;
typedef struct typedef struct
@ -68,8 +73,8 @@ G_DEFINE_TYPE (GstContextElement, gst_context_element, GST_TYPE_ELEMENT);
static void static void
gst_context_element_set_context (GstElement * element, GstContext * context) gst_context_element_set_context (GstElement * element, GstContext * context)
{ {
GST_ELEMENT_CLASS (gst_context_element_parent_class)->set_context (element, if (strcmp (gst_context_get_context_type (context), "foobar") == 0)
context); ((GstContextElement *) element)->have_foobar = TRUE;
} }
static GstStateChangeReturn static GstStateChangeReturn
@ -80,17 +85,8 @@ gst_context_element_change_state (GstElement * element,
if (transition == GST_STATE_CHANGE_NULL_TO_READY) { if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
GstContext *context; GstContext *context;
const GstStructure *s;
GstMessage *msg; GstMessage *msg;
gboolean have_foobar = FALSE; gboolean have_foobar = celement->have_foobar;
context = gst_element_get_context (element);
if (context) {
s = gst_context_get_structure (context);
if (gst_structure_has_field (s, "foobar"))
have_foobar = TRUE;
gst_context_unref (context);
}
if (celement->set_before_ready && !have_foobar) if (celement->set_before_ready && !have_foobar)
return GST_STATE_CHANGE_FAILURE; return GST_STATE_CHANGE_FAILURE;
@ -104,17 +100,10 @@ gst_context_element_change_state (GstElement * element,
if (!have_foobar) { if (!have_foobar) {
/* Here we would first query downstream for a context but we have no pads */ /* Here we would first query downstream for a context but we have no pads */
msg = gst_message_new_need_context (GST_OBJECT (element)); msg = gst_message_new_need_context (GST_OBJECT (element), "foobar");
gst_message_add_context_type (msg, "foobar");
gst_element_post_message (element, msg); gst_element_post_message (element, msg);
context = gst_element_get_context (element); have_foobar = celement->have_foobar;
if (context) {
s = gst_context_get_structure (context);
if (gst_structure_has_field (s, "foobar"))
have_foobar = TRUE;
gst_context_unref (context);
}
} }
if (celement->set_from_need_context && !have_foobar) if (celement->set_from_need_context && !have_foobar)
@ -128,14 +117,7 @@ gst_context_element_change_state (GstElement * element,
return GST_STATE_CHANGE_FAILURE; return GST_STATE_CHANGE_FAILURE;
if (!have_foobar) { if (!have_foobar) {
GstStructure *s2; context = gst_context_new ("foobar", FALSE);
context = gst_element_get_context (element);
if (context)
context = gst_context_make_writable (context);
else
context = gst_context_new ();
s2 = gst_context_writable_structure (context);
gst_structure_set (s2, "foobar", G_TYPE_INT, 123, NULL);
gst_element_set_context (element, context); gst_element_set_context (element, context);
msg = msg =
gst_message_new_have_context (GST_OBJECT (element), gst_message_new_have_context (GST_OBJECT (element),
@ -172,9 +154,7 @@ GST_START_TEST (test_element_set_before_ready)
{ {
GstBus *bus; GstBus *bus;
GstElement *element; GstElement *element;
GstContext *context, *context2; GstContext *context;
GstStructure *s;
const GstStructure *s2;
GstMessage *msg; GstMessage *msg;
element = g_object_new (gst_context_element_get_type (), NULL); element = g_object_new (gst_context_element_get_type (), NULL);
@ -187,9 +167,7 @@ GST_START_TEST (test_element_set_before_ready)
GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS); GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS);
fail_if (gst_bus_pop (bus) != NULL); fail_if (gst_bus_pop (bus) != NULL);
context = gst_context_new (); context = gst_context_new ("foobar", FALSE);
s = gst_context_writable_structure (context);
gst_structure_set (s, "foobar", G_TYPE_INT, 123, NULL);
gst_element_set_context (element, context); gst_element_set_context (element, context);
fail_unless (gst_element_set_state (element, fail_unless (gst_element_set_state (element,
GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS); GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS);
@ -198,13 +176,9 @@ GST_START_TEST (test_element_set_before_ready)
gst_message_unref (msg); gst_message_unref (msg);
fail_if (gst_bus_pop (bus) != NULL); fail_if (gst_bus_pop (bus) != NULL);
context2 = gst_element_get_context (element); fail_unless (((GstContextElement *) element)->have_foobar);
fail_unless (GST_IS_CONTEXT (context2));
s2 = gst_context_get_structure (context2);
fail_unless (gst_structure_is_equal (s, s2));
gst_context_unref (context); gst_context_unref (context);
gst_context_unref (context2);
gst_element_set_bus (element, NULL); gst_element_set_bus (element, NULL);
fail_unless (gst_element_set_state (element, fail_unless (gst_element_set_state (element,
@ -220,23 +194,13 @@ static GstBusSyncReply
sync_handler (GstBus * bus, GstMessage * message, gpointer user_data) sync_handler (GstBus * bus, GstMessage * message, gpointer user_data)
{ {
if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEED_CONTEXT) { if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEED_CONTEXT) {
guint n;
const gchar *type; const gchar *type;
GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message)); GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message));
GstContext *context; GstContext *context;
GstStructure *s;
n = gst_message_get_n_context_types (message); fail_unless (gst_message_parse_context_type (message, &type));
fail_unless (n == 1);
fail_unless (gst_message_parse_nth_context_type (message, 0, &type));
fail_unless_equals_string (type, "foobar"); fail_unless_equals_string (type, "foobar");
context = gst_element_get_context (element); context = gst_context_new ("foobar", FALSE);
if (context)
context = gst_context_make_writable (context);
else
context = gst_context_new ();
s = gst_context_writable_structure (context);
gst_structure_set (s, "foobar", G_TYPE_INT, 123, NULL);
gst_element_set_context (element, context); gst_element_set_context (element, context);
gst_context_unref (context); gst_context_unref (context);
} }
@ -248,8 +212,6 @@ GST_START_TEST (test_element_set_from_need_context)
{ {
GstBus *bus; GstBus *bus;
GstElement *element; GstElement *element;
GstContext *context;
const GstStructure *s;
GstMessage *msg; GstMessage *msg;
element = g_object_new (gst_context_element_get_type (), NULL); element = g_object_new (gst_context_element_get_type (), NULL);
@ -269,12 +231,7 @@ GST_START_TEST (test_element_set_from_need_context)
gst_message_unref (msg); gst_message_unref (msg);
fail_if (gst_bus_pop (bus) != NULL); fail_if (gst_bus_pop (bus) != NULL);
context = gst_element_get_context (element); fail_unless (((GstContextElement *) element)->have_foobar);
fail_unless (GST_IS_CONTEXT (context));
s = gst_context_get_structure (context);
fail_unless (gst_structure_has_field (s, "foobar"));
gst_context_unref (context);
gst_element_set_bus (element, NULL); gst_element_set_bus (element, NULL);
fail_unless (gst_element_set_state (element, fail_unless (gst_element_set_state (element,
@ -291,7 +248,6 @@ GST_START_TEST (test_element_create_self)
GstBus *bus; GstBus *bus;
GstElement *element; GstElement *element;
GstContext *context; GstContext *context;
const GstStructure *s;
GstMessage *msg; GstMessage *msg;
element = g_object_new (gst_context_element_get_type (), NULL); element = g_object_new (gst_context_element_get_type (), NULL);
@ -309,8 +265,7 @@ GST_START_TEST (test_element_create_self)
gst_bus_pop_filtered (bus, GST_MESSAGE_HAVE_CONTEXT)) != NULL); gst_bus_pop_filtered (bus, GST_MESSAGE_HAVE_CONTEXT)) != NULL);
gst_message_parse_have_context (msg, &context); gst_message_parse_have_context (msg, &context);
fail_unless (GST_IS_CONTEXT (context)); fail_unless (GST_IS_CONTEXT (context));
s = gst_context_get_structure (context); fail_unless_equals_string (gst_context_get_context_type (context), "foobar");
fail_unless (gst_structure_has_field (s, "foobar"));
gst_context_unref (context); gst_context_unref (context);
gst_message_unref (msg); gst_message_unref (msg);
fail_unless ((msg = fail_unless ((msg =
@ -318,12 +273,7 @@ GST_START_TEST (test_element_create_self)
gst_message_unref (msg); gst_message_unref (msg);
fail_if (gst_bus_pop (bus) != NULL); fail_if (gst_bus_pop (bus) != NULL);
context = gst_element_get_context (element); fail_unless (((GstContextElement *) element)->have_foobar);
fail_unless (GST_IS_CONTEXT (context));
s = gst_context_get_structure (context);
fail_unless (gst_structure_has_field (s, "foobar"));
gst_context_unref (context);
gst_element_set_bus (element, NULL); gst_element_set_bus (element, NULL);
fail_unless (gst_element_set_state (element, fail_unless (gst_element_set_state (element,