diff options
author | Shaun McCance <shaunm@gnome.org> | 2009-10-12 16:45:37 -0500 |
---|---|---|
committer | Shaun McCance <shaunm@gnome.org> | 2009-10-12 16:45:37 -0500 |
commit | 3a382a0027a627ebd76ed3449df872c73e1874f3 (patch) | |
tree | 154826621348ab9a4d21716ecaedafdcf553c69f /tests | |
parent | 96737655ed953f3705a22dab41573a252ca7847d (diff) | |
download | yelp-3a382a0027a627ebd76ed3449df872c73e1874f3.tar.gz |
[yelp-transform] Adding yelp-transform to libyelp
I've changed YelpTransform to be a GObject. This changes considerably the
way we track references to the object to make sure we don't nuke it while
there's still a pending idle function or a running thread.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 5 | ||||
-rw-r--r-- | tests/test-transform.c | 185 |
2 files changed, 190 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 14d53e28..d305e349 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -2,6 +2,7 @@ YELP_COMMON_CFLAGS = \ $(YELP_CFLAGS) \ $(AM_CFLAGS) \ $(YELP_DEFINES) \ + -DDATADIR=\""$(datadir)"\" \ -I$(top_srcdir)/libyelp YELP_COMMON_LDADD = \ $(YELP_LIBS) \ @@ -10,6 +11,7 @@ YELP_COMMON_LDADD = \ check_PROGRAMS = \ test-location-entry \ test-settings \ + test-transform \ test-uri \ test-view @@ -19,6 +21,9 @@ test_location_entry_LDADD = $(YELP_COMMON_LDADD) test_settings_CFLAGS = $(YELP_COMMON_CFLAGS) test_settings_LDADD = $(YELP_COMMON_LDADD) +test_transform_CFLAGS = $(YELP_COMMON_CFLAGS) +test_transform_LDADD = $(YELP_COMMON_LDADD) + test_uri_CFLAGS = $(YELP_COMMON_CFLAGS) test_uri_LDADD = $(YELP_COMMON_LDADD) diff --git a/tests/test-transform.c b/tests/test-transform.c new file mode 100644 index 00000000..efda9b8e --- /dev/null +++ b/tests/test-transform.c @@ -0,0 +1,185 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2006 Shaun McCance + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA. + * + * Author: Shaun McCance <shaunm@gnome.org> + */ + +#include <config.h> + +#include <glib.h> +#include <libxml/parser.h> +#include <libxml/xinclude.h> + +#include "yelp-error.h" +#include "yelp-transform.h" + +static gint num_chunks = 0; +static gboolean freed; + +static gint timeout = -1; +static gboolean random_timeout = FALSE; +static gchar **files = NULL; +static const GOptionEntry options[] = { + { "random-timeout", 'r', + 0, G_OPTION_ARG_NONE, + &random_timeout, + "Time out after a random amount of time", NULL }, + { "timeout", 't', + 0, G_OPTION_ARG_INT, + &timeout, + "Time out after N milliseconds", "N" }, + { G_OPTION_REMAINING, + 0, 0, G_OPTION_ARG_FILENAME_ARRAY, + &files, NULL, NULL }, + { NULL } +}; + +GMainLoop *loop; + +static gboolean +transform_release (YelpTransform *transform) +{ + printf ("\nRELEASE\n"); + if (!freed) { + yelp_transform_cancel (transform); + g_object_unref (transform); + } + freed = TRUE; + return FALSE; +} + +static void +transform_chunk (YelpTransform *transform, + const gchar *chunk_id, + gpointer user_data) +{ + gchar *chunk, *small; + num_chunks++; + printf ("\nCHUNK %i: %s\n", num_chunks, chunk_id); + + chunk = yelp_transform_take_chunk (transform, chunk_id); + small = g_strndup (chunk, 300); + printf ("%s\n", small); + + g_free (small); + g_free (chunk); +} + +static void +transform_finished (YelpTransform *transform, + gpointer user_data) +{ + printf ("\nFINAL\n"); + if (!freed) { + yelp_transform_cancel (transform); + g_object_unref (transform); + } + freed = TRUE; +} + +static void +transform_error (YelpTransform *transform, + gpointer user_data) +{ + printf ("\nERROR\n"); +} + +static void +transform_destroyed (gpointer data, + GObject *object) +{ + printf ("\nFREED\n"); + g_main_loop_quit (loop); +} + +gint +main (gint argc, gchar **argv) +{ + GOptionContext *context; + xmlParserCtxtPtr parser; + xmlDocPtr doc; + YelpTransform *transform; + gchar **params; + gchar *stylesheet; + gchar *file; + + g_type_init (); + g_thread_init (NULL); + + context = g_option_context_new ("[STYLESHEET] FILE"); + g_option_context_add_main_entries (context, options, NULL); + g_option_context_parse (context, &argc, &argv, NULL); + + if (files == NULL || files[0] == NULL) { + g_printerr ("Usage: test-transform [OPTION...] [STYLESHEET] FILE\n"); + return 1; + } + + if (files[1] == NULL) { + stylesheet = DATADIR"/yelp/xslt/db2html.xsl"; + file = files[0]; + } else { + stylesheet = files[0]; + file = files[1]; + } + + params = g_new0 (gchar *, 7); + params[0] = "db.chunk.extension"; + params[1] = "\"\""; + params[2] = "db.chunk.info_basename"; + params[3] = "\"x-yelp-titlepage\""; + params[4] = "db.chunk.max_depth"; + params[5] = "2"; + params[6] = NULL; + + transform = yelp_transform_new (); + g_object_weak_ref ((GObject *) transform, transform_destroyed, NULL); + if (!yelp_transform_set_stylesheet (transform, stylesheet, NULL)) + return 1; + + g_signal_connect (transform, "chunk-ready", (GCallback) transform_chunk, NULL); + g_signal_connect (transform, "finished", (GCallback) transform_finished, NULL); + g_signal_connect (transform, "error", (GCallback) transform_error, NULL); + + parser = xmlNewParserCtxt (); + doc = xmlCtxtReadFile (parser, + file, + NULL, + XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA | + XML_PARSE_NOENT | XML_PARSE_NONET ); + xmlFreeParserCtxt (parser); + xmlXIncludeProcessFlags (doc, + XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA | + XML_PARSE_NOENT | XML_PARSE_NONET ); + if (!yelp_transform_start (transform, doc, params, NULL)) + return 1; + + if (random_timeout) { + GRand *rand = g_rand_new (); + timeout = g_rand_int_range (rand, 80, 280); + g_rand_free (rand); + } + + if (timeout >= 0) + g_timeout_add (timeout, (GSourceFunc) transform_release, transform); + + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + + return 0; +} |