gimp/app/config/test-config.c

279 lines
6.9 KiB
C
Raw Normal View History

/* GIMP - The GNU Image Manipulation Program
2001-12-10 00:31:30 +08:00
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* Test suite for GimpConfig.
* Copyright (C) 2001-2002 Sven Neumann <sven@gimp.org>
app/widgets/gimpdocked.[ch] renamed GimpDockedIface to 2003-10-11 Sven Neumann <sven@gimp.org> * app/widgets/gimpdocked.[ch] * app/widgets/widgets-types.h: renamed GimpDockedIface to GimpDockedInterface. * app/display/gimpnavigationview.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcontainereditor.c * app/widgets/gimpcontainerview.c * app/widgets/gimpeditor.c * app/widgets/gimpimageeditor.c * app/widgets/gimpitemtreeview.c * app/widgets/gimptooloptionseditor.c: changed accordingly. * app/config/config-types.h * app/config/gimpconfig.[ch] * app/config/gimpconfig-deserialize.[ch] * app/config/gimpconfig-serialize.[ch] * app/config/gimpconfig-utils.[ch]: added a GimpConfig typedef and changed the GimpConfig API to take GimpConfig instead of GObject pointers. * app/config/gimpconfig-dump.c * app/config/gimprc.c * app/config/test-config.c * app/core/gimp-documents.c * app/core/gimp-parasites.c * app/core/gimp-templates.c * app/core/gimp.[ch] * app/core/gimpcontainer.c * app/core/gimpcontext.c * app/core/gimpdocumentlist.c * app/core/gimpgrid.c * app/core/gimpimage-duplicate.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpparasitelist.c * app/core/gimptemplate.c * app/core/gimptooloptions.c * app/core/gimpviewable.c * app/gui/grid-dialog.c * app/gui/preferences-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/tool-options-commands.c * app/paint/gimppaintcore.c * app/pdb/gimprc_cmds.c * app/text/gimptext-parasite.c * app/text/gimptext.c * app/text/gimptextlayer.c * app/tools/gimp-tools.c * app/tools/gimptexttool.c * app/widgets/gimpdevices.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * tools/pdbgen/pdb/gimprc.pdb: changed accordingly.
2003-10-11 22:30:18 +08:00
*
* This program is free software: you can redistribute it and/or modify
2001-12-10 00:31:30 +08:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
2001-12-10 00:31:30 +08:00
* (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, see <http://www.gnu.org/licenses/>.
2001-12-10 00:31:30 +08:00
*/
#include "config.h"
#include "stdlib.h"
#include "string.h"
#include <gio/gio.h>
2001-12-10 00:31:30 +08:00
#include "libgimpbase/gimpbase.h"
#include "libgimpbase/gimpbase-private.h"
2005-01-26 03:11:26 +08:00
#include "libgimpconfig/gimpconfig.h"
#include "core/core-types.h"
#include "core/gimpgrid.h"
#include "gimprc-unknown.h"
2001-12-10 00:31:30 +08:00
static void notify_callback (GObject *object,
GParamSpec *pspec);
static void output_unknown_token (const gchar *key,
const gchar *value,
gpointer data);
2001-12-10 00:31:30 +08:00
static void units_init (void);
2001-12-10 00:31:30 +08:00
int
main (int argc,
char *argv[])
{
GimpConfig *grid;
GimpConfig *grid2;
const gchar *filename = "foorc";
gchar *header;
gchar *result;
GList *list;
gint i;
GError *error = NULL;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "--g-fatal-warnings") == 0)
{
GLogLevelFlags fatal_mask;
app/widgets/gimpdocked.[ch] renamed GimpDockedIface to 2003-10-11 Sven Neumann <sven@gimp.org> * app/widgets/gimpdocked.[ch] * app/widgets/widgets-types.h: renamed GimpDockedIface to GimpDockedInterface. * app/display/gimpnavigationview.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcontainereditor.c * app/widgets/gimpcontainerview.c * app/widgets/gimpeditor.c * app/widgets/gimpimageeditor.c * app/widgets/gimpitemtreeview.c * app/widgets/gimptooloptionseditor.c: changed accordingly. * app/config/config-types.h * app/config/gimpconfig.[ch] * app/config/gimpconfig-deserialize.[ch] * app/config/gimpconfig-serialize.[ch] * app/config/gimpconfig-utils.[ch]: added a GimpConfig typedef and changed the GimpConfig API to take GimpConfig instead of GObject pointers. * app/config/gimpconfig-dump.c * app/config/gimprc.c * app/config/test-config.c * app/core/gimp-documents.c * app/core/gimp-parasites.c * app/core/gimp-templates.c * app/core/gimp.[ch] * app/core/gimpcontainer.c * app/core/gimpcontext.c * app/core/gimpdocumentlist.c * app/core/gimpgrid.c * app/core/gimpimage-duplicate.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpparasitelist.c * app/core/gimptemplate.c * app/core/gimptooloptions.c * app/core/gimpviewable.c * app/gui/grid-dialog.c * app/gui/preferences-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/tool-options-commands.c * app/paint/gimppaintcore.c * app/pdb/gimprc_cmds.c * app/text/gimptext-parasite.c * app/text/gimptext.c * app/text/gimptextlayer.c * app/tools/gimp-tools.c * app/tools/gimptexttool.c * app/widgets/gimpdevices.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * tools/pdbgen/pdb/gimprc.pdb: changed accordingly.
2003-10-11 22:30:18 +08:00
fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
g_log_set_always_fatal (fatal_mask);
}
}
2001-12-10 00:31:30 +08:00
units_init ();
g_print ("\nTesting GimpConfig ...\n");
2001-12-10 00:31:30 +08:00
g_print (" Creating a new Grid object ...");
grid = g_object_new (GIMP_TYPE_GRID, NULL);
g_print (" done.\n");
app/widgets/gimpdocked.[ch] renamed GimpDockedIface to 2003-10-11 Sven Neumann <sven@gimp.org> * app/widgets/gimpdocked.[ch] * app/widgets/widgets-types.h: renamed GimpDockedIface to GimpDockedInterface. * app/display/gimpnavigationview.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcontainereditor.c * app/widgets/gimpcontainerview.c * app/widgets/gimpeditor.c * app/widgets/gimpimageeditor.c * app/widgets/gimpitemtreeview.c * app/widgets/gimptooloptionseditor.c: changed accordingly. * app/config/config-types.h * app/config/gimpconfig.[ch] * app/config/gimpconfig-deserialize.[ch] * app/config/gimpconfig-serialize.[ch] * app/config/gimpconfig-utils.[ch]: added a GimpConfig typedef and changed the GimpConfig API to take GimpConfig instead of GObject pointers. * app/config/gimpconfig-dump.c * app/config/gimprc.c * app/config/test-config.c * app/core/gimp-documents.c * app/core/gimp-parasites.c * app/core/gimp-templates.c * app/core/gimp.[ch] * app/core/gimpcontainer.c * app/core/gimpcontext.c * app/core/gimpdocumentlist.c * app/core/gimpgrid.c * app/core/gimpimage-duplicate.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpparasitelist.c * app/core/gimptemplate.c * app/core/gimptooloptions.c * app/core/gimpviewable.c * app/gui/grid-dialog.c * app/gui/preferences-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/tool-options-commands.c * app/paint/gimppaintcore.c * app/pdb/gimprc_cmds.c * app/text/gimptext-parasite.c * app/text/gimptext.c * app/text/gimptextlayer.c * app/tools/gimp-tools.c * app/tools/gimptexttool.c * app/widgets/gimpdevices.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * tools/pdbgen/pdb/gimprc.pdb: changed accordingly.
2003-10-11 22:30:18 +08:00
g_print (" Adding the unknown token (foobar \"hadjaha\") ...");
gimp_rc_add_unknown_token (grid, "foobar", "hadjaha");
g_print (" done.\n");
2001-12-10 00:31:30 +08:00
app/widgets/gimpdocked.[ch] renamed GimpDockedIface to 2003-10-11 Sven Neumann <sven@gimp.org> * app/widgets/gimpdocked.[ch] * app/widgets/widgets-types.h: renamed GimpDockedIface to GimpDockedInterface. * app/display/gimpnavigationview.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcontainereditor.c * app/widgets/gimpcontainerview.c * app/widgets/gimpeditor.c * app/widgets/gimpimageeditor.c * app/widgets/gimpitemtreeview.c * app/widgets/gimptooloptionseditor.c: changed accordingly. * app/config/config-types.h * app/config/gimpconfig.[ch] * app/config/gimpconfig-deserialize.[ch] * app/config/gimpconfig-serialize.[ch] * app/config/gimpconfig-utils.[ch]: added a GimpConfig typedef and changed the GimpConfig API to take GimpConfig instead of GObject pointers. * app/config/gimpconfig-dump.c * app/config/gimprc.c * app/config/test-config.c * app/core/gimp-documents.c * app/core/gimp-parasites.c * app/core/gimp-templates.c * app/core/gimp.[ch] * app/core/gimpcontainer.c * app/core/gimpcontext.c * app/core/gimpdocumentlist.c * app/core/gimpgrid.c * app/core/gimpimage-duplicate.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpparasitelist.c * app/core/gimptemplate.c * app/core/gimptooloptions.c * app/core/gimpviewable.c * app/gui/grid-dialog.c * app/gui/preferences-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/tool-options-commands.c * app/paint/gimppaintcore.c * app/pdb/gimprc_cmds.c * app/text/gimptext-parasite.c * app/text/gimptext.c * app/text/gimptextlayer.c * app/tools/gimp-tools.c * app/tools/gimptexttool.c * app/widgets/gimpdevices.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * tools/pdbgen/pdb/gimprc.pdb: changed accordingly.
2003-10-11 22:30:18 +08:00
g_print (" Serializing %s to '%s' ...",
g_type_name (G_TYPE_FROM_INSTANCE (grid)), filename);
if (! gimp_config_serialize_to_file (grid,
filename,
"foorc", "end of foorc",
NULL, &error))
{
g_print ("%s\n", error->message);
return EXIT_FAILURE;
}
g_print (" done.\n");
2001-12-10 00:31:30 +08:00
g_signal_connect (grid, "notify",
G_CALLBACK (notify_callback),
NULL);
g_print (" Deserializing from '%s' ...\n", filename);
if (! gimp_config_deserialize_file (grid, filename, NULL, &error))
{
g_print ("%s\n", error->message);
return EXIT_FAILURE;
}
header = " Unknown string tokens:\n";
gimp_rc_foreach_unknown_token (grid, output_unknown_token, &header);
g_print (" done.\n\n");
g_print (" Changing a property ...");
g_object_set (grid, "style", GIMP_GRID_DOTS, NULL);
g_print (" Testing gimp_config_duplicate() ...");
grid2 = gimp_config_duplicate (grid);
g_print (" done.\n");
g_signal_connect (grid2, "notify",
G_CALLBACK (notify_callback),
NULL);
g_print (" Changing a property in the duplicate ...");
g_object_set (grid2, "xspacing", 20.0, NULL);
g_print (" Creating a diff between the two ...");
for (list = gimp_config_diff (G_OBJECT (grid), G_OBJECT (grid2), 0);
list;
list = list->next)
{
GParamSpec *pspec = list->data;
g_print ("%c%s", (list->prev ? ',' : ' '), pspec->name);
}
g_print ("\n\n");
g_object_unref (grid2);
g_print (" Deserializing from gimpconfig.c (should fail) ...");
if (! gimp_config_deserialize_file (grid, "gimpconfig.c", NULL, &error))
{
g_print (" OK, failed. The error was:\n %s\n", error->message);
g_error_free (error);
error = NULL;
}
else
{
g_print ("This test should have failed :-(\n");
return EXIT_FAILURE;
}
g_print (" Serializing to a string and back ... ");
result = gimp_config_serialize_to_string (grid, NULL);
grid2 = g_object_new (GIMP_TYPE_GRID, NULL);
if (! gimp_config_deserialize_string (grid2, result, -1, NULL, &error))
{
g_print ("failed!\nThe error was:\n %s\n", error->message);
g_error_free (error);
return EXIT_FAILURE;
}
else
{
GList *diff = gimp_config_diff (G_OBJECT (grid), G_OBJECT (grid2), 0);
if (diff)
{
GList *list;
g_print ("succeeded but properties differ:\n");
for (list = diff; list; list = list->next)
{
GParamSpec *pspec = list->data;
g_print (" %s\n", pspec->name);
}
return EXIT_FAILURE;
}
g_print ("OK (%u bytes)\n", result ? (guint) strlen (result) : 0);
}
g_free (result);
g_object_unref (grid2);
g_object_unref (grid);
app/widgets/gimpdocked.[ch] renamed GimpDockedIface to 2003-10-11 Sven Neumann <sven@gimp.org> * app/widgets/gimpdocked.[ch] * app/widgets/widgets-types.h: renamed GimpDockedIface to GimpDockedInterface. * app/display/gimpnavigationview.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcontainereditor.c * app/widgets/gimpcontainerview.c * app/widgets/gimpeditor.c * app/widgets/gimpimageeditor.c * app/widgets/gimpitemtreeview.c * app/widgets/gimptooloptionseditor.c: changed accordingly. * app/config/config-types.h * app/config/gimpconfig.[ch] * app/config/gimpconfig-deserialize.[ch] * app/config/gimpconfig-serialize.[ch] * app/config/gimpconfig-utils.[ch]: added a GimpConfig typedef and changed the GimpConfig API to take GimpConfig instead of GObject pointers. * app/config/gimpconfig-dump.c * app/config/gimprc.c * app/config/test-config.c * app/core/gimp-documents.c * app/core/gimp-parasites.c * app/core/gimp-templates.c * app/core/gimp.[ch] * app/core/gimpcontainer.c * app/core/gimpcontext.c * app/core/gimpdocumentlist.c * app/core/gimpgrid.c * app/core/gimpimage-duplicate.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpparasitelist.c * app/core/gimptemplate.c * app/core/gimptooloptions.c * app/core/gimpviewable.c * app/gui/grid-dialog.c * app/gui/preferences-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/tool-options-commands.c * app/paint/gimppaintcore.c * app/pdb/gimprc_cmds.c * app/text/gimptext-parasite.c * app/text/gimptext.c * app/text/gimptextlayer.c * app/tools/gimp-tools.c * app/tools/gimptexttool.c * app/widgets/gimpdevices.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * tools/pdbgen/pdb/gimprc.pdb: changed accordingly.
2003-10-11 22:30:18 +08:00
g_print ("\nFinished test of GimpConfig.\n\n");
2001-12-10 00:31:30 +08:00
return EXIT_SUCCESS;
2001-12-10 00:31:30 +08:00
}
static void
2001-12-10 00:31:30 +08:00
notify_callback (GObject *object,
GParamSpec *pspec)
{
GString *str;
GValue value = { 0, };
2001-12-10 00:31:30 +08:00
g_return_if_fail (G_IS_OBJECT (object));
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
g_value_init (&value, pspec->value_type);
g_object_get_property (object, pspec->name, &value);
2001-12-10 00:31:30 +08:00
str = g_string_new (NULL);
2001-12-10 00:31:30 +08:00
if (gimp_config_serialize_value (&value, str, TRUE))
{
g_print (" %s -> %s\n", pspec->name, str->str);
2001-12-10 00:31:30 +08:00
}
else
{
app/widgets/gimpdocked.[ch] renamed GimpDockedIface to 2003-10-11 Sven Neumann <sven@gimp.org> * app/widgets/gimpdocked.[ch] * app/widgets/widgets-types.h: renamed GimpDockedIface to GimpDockedInterface. * app/display/gimpnavigationview.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcontainereditor.c * app/widgets/gimpcontainerview.c * app/widgets/gimpeditor.c * app/widgets/gimpimageeditor.c * app/widgets/gimpitemtreeview.c * app/widgets/gimptooloptionseditor.c: changed accordingly. * app/config/config-types.h * app/config/gimpconfig.[ch] * app/config/gimpconfig-deserialize.[ch] * app/config/gimpconfig-serialize.[ch] * app/config/gimpconfig-utils.[ch]: added a GimpConfig typedef and changed the GimpConfig API to take GimpConfig instead of GObject pointers. * app/config/gimpconfig-dump.c * app/config/gimprc.c * app/config/test-config.c * app/core/gimp-documents.c * app/core/gimp-parasites.c * app/core/gimp-templates.c * app/core/gimp.[ch] * app/core/gimpcontainer.c * app/core/gimpcontext.c * app/core/gimpdocumentlist.c * app/core/gimpgrid.c * app/core/gimpimage-duplicate.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpparasitelist.c * app/core/gimptemplate.c * app/core/gimptooloptions.c * app/core/gimpviewable.c * app/gui/grid-dialog.c * app/gui/preferences-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/tool-options-commands.c * app/paint/gimppaintcore.c * app/pdb/gimprc_cmds.c * app/text/gimptext-parasite.c * app/text/gimptext.c * app/text/gimptextlayer.c * app/tools/gimp-tools.c * app/tools/gimptexttool.c * app/widgets/gimpdevices.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * tools/pdbgen/pdb/gimprc.pdb: changed accordingly.
2003-10-11 22:30:18 +08:00
g_print (" %s changed but we failed to serialize its value!\n",
pspec->name);
2001-12-10 00:31:30 +08:00
}
g_string_free (str, TRUE);
g_value_unset (&value);
2001-12-10 00:31:30 +08:00
}
static void
output_unknown_token (const gchar *key,
const gchar *value,
gpointer data)
{
gchar **header = (gchar **) data;
gchar *escaped = g_strescape (value, NULL);
if (*header)
{
g_print ("%s", *header);
*header = NULL;
}
g_print (" %s \"%s\"\n", key, escaped);
g_free (escaped);
}
/* minimal dummy units implementation */
static const gchar *
unit_get_identifier (GimpUnit unit)
{
switch (unit)
{
case GIMP_UNIT_PIXEL:
return "pixels";
case GIMP_UNIT_INCH:
return "inches";
case GIMP_UNIT_MM:
return "millimeters";
case GIMP_UNIT_POINT:
return "points";
case GIMP_UNIT_PICA:
return "picas";
default:
return NULL;
}
}
static gint
unit_get_number_of_units (void)
{
return GIMP_UNIT_END;
}
static void
units_init (void)
{
GimpUnitVtable vtable;
vtable.unit_get_number_of_units = unit_get_number_of_units;
vtable.unit_get_identifier = unit_get_identifier;
gimp_base_init (&vtable);
}