Blame gio/tests/defaultvalue.c

Packit ae235b
/* GIO default value tests
Packit ae235b
 * Copyright (C) 2013 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public License
Packit ae235b
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <gio/gio.h>
Packit ae235b
Packit ae235b
static void
Packit ae235b
check_property (const char *output,
Packit ae235b
	        GParamSpec *pspec,
Packit ae235b
		GValue *value)
Packit ae235b
{
Packit ae235b
  GValue default_value = G_VALUE_INIT;
Packit ae235b
  char *v, *dv, *msg;
Packit ae235b
Packit ae235b
  if (g_param_value_defaults (pspec, value))
Packit ae235b
      return;
Packit ae235b
Packit ae235b
  g_value_init (&default_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
Packit ae235b
  g_param_value_set_default (pspec, &default_value);
Packit ae235b
Packit ae235b
  v = g_strdup_value_contents (value);
Packit ae235b
  dv = g_strdup_value_contents (&default_value);
Packit ae235b
Packit ae235b
  msg = g_strdup_printf ("%s %s.%s: %s != %s\n",
Packit ae235b
			 output,
Packit ae235b
			 g_type_name (pspec->owner_type),
Packit ae235b
			 pspec->name,
Packit ae235b
			 dv, v);
Packit ae235b
  g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
Packit ae235b
  g_free (msg);
Packit ae235b
Packit ae235b
  g_free (v);
Packit ae235b
  g_free (dv);
Packit ae235b
  g_value_unset (&default_value);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_type (gconstpointer data)
Packit ae235b
{
Packit ae235b
  GObjectClass *klass;
Packit ae235b
  GObject *instance;
Packit ae235b
  GParamSpec **pspecs;
Packit ae235b
  guint n_pspecs, i;
Packit ae235b
  GType type;
Packit ae235b
Packit ae235b
  type = * (GType *) data;
Packit ae235b
Packit ae235b
  if (g_type_is_a (type, G_TYPE_APP_INFO_MONITOR))
Packit ae235b
    {
Packit ae235b
      g_test_skip ("singleton");
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_type_is_a (type, G_TYPE_BINDING) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_BUFFERED_INPUT_STREAM) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_BUFFERED_OUTPUT_STREAM) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_CHARSET_CONVERTER) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_DBUS_ACTION_GROUP) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_DBUS_CONNECTION) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_DBUS_OBJECT_MANAGER_CLIENT) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_DBUS_OBJECT_MANAGER_SERVER) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_DBUS_PROXY) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_DBUS_SERVER) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_FILTER_OUTPUT_STREAM) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_FILTER_INPUT_STREAM) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_INET_ADDRESS) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_INET_SOCKET_ADDRESS) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_PROPERTY_ACTION) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_SETTINGS) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_SOCKET_CONNECTION) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_SIMPLE_IO_STREAM) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_THEMED_ICON) ||
Packit ae235b
      FALSE)
Packit ae235b
    {
Packit ae235b
      g_test_skip ("mandatory construct params");
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_type_is_a (type, G_TYPE_DBUS_MENU_MODEL) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_DBUS_METHOD_INVOCATION))
Packit ae235b
    {
Packit ae235b
      g_test_skip ("crash in finalize");
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_type_is_a (type, G_TYPE_FILE_ENUMERATOR) ||
Packit ae235b
      g_type_is_a (type, G_TYPE_FILE_IO_STREAM))
Packit ae235b
    {
Packit ae235b
      g_test_skip ("should be abstract");
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  klass = g_type_class_ref (type);
Packit ae235b
  instance = g_object_new (type, NULL);
Packit ae235b
Packit ae235b
  if (G_IS_INITABLE (instance) &&
Packit ae235b
      !g_initable_init (G_INITABLE (instance), NULL, NULL))
Packit ae235b
    {
Packit ae235b
      g_test_skip ("initialization failed");
Packit ae235b
      g_object_unref (instance);
Packit ae235b
      g_type_class_unref (klass);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_type_is_a (type, G_TYPE_INITIALLY_UNOWNED))
Packit ae235b
    g_object_ref_sink (instance);
Packit ae235b
Packit ae235b
  pspecs = g_object_class_list_properties (klass, &n_pspecs);
Packit ae235b
  for (i = 0; i < n_pspecs; ++i)
Packit ae235b
    {
Packit ae235b
      GParamSpec *pspec = pspecs[i];
Packit ae235b
      GValue value = G_VALUE_INIT;
Packit ae235b
Packit ae235b
      if (pspec->owner_type != type)
Packit ae235b
	continue;
Packit ae235b
Packit ae235b
      if ((pspec->flags & G_PARAM_READABLE) == 0)
Packit ae235b
	continue;
Packit ae235b
Packit ae235b
      if (g_type_is_a (type, G_TYPE_APPLICATION) &&
Packit ae235b
          (strcmp (pspec->name, "is-remote") == 0))
Packit ae235b
        {
Packit ae235b
          g_test_message ("skipping GApplication:is-remote");
Packit ae235b
          continue;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (g_type_is_a (type, G_TYPE_PROXY_ADDRESS_ENUMERATOR) &&
Packit ae235b
          (strcmp (pspec->name, "proxy-resolver") == 0))
Packit ae235b
        {
Packit ae235b
          g_test_message ("skipping GProxyAddressEnumerator:proxy-resolver");
Packit ae235b
          continue;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (g_type_is_a (type, G_TYPE_SOCKET_CLIENT) &&
Packit ae235b
          (strcmp (pspec->name, "proxy-resolver") == 0))
Packit ae235b
        {
Packit ae235b
          g_test_message ("skipping GSocketClient:proxy-resolver");
Packit ae235b
          continue;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (g_test_verbose ())
Packit ae235b
        g_printerr ("Property %s.%s\n", g_type_name (pspec->owner_type), pspec->name);
Packit ae235b
      g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
Packit ae235b
      g_object_get_property (instance, pspec->name, &value);
Packit ae235b
      check_property ("Property", pspec, &value);
Packit ae235b
      g_value_unset (&value);
Packit ae235b
    }
Packit ae235b
  g_free (pspecs);
Packit ae235b
  g_object_unref (instance);
Packit ae235b
  g_type_class_unref (klass);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GType *all_registered_types;
Packit ae235b
Packit ae235b
static const GType *
Packit ae235b
list_all_types (void)
Packit ae235b
{
Packit ae235b
  if (!all_registered_types)
Packit ae235b
    {
Packit ae235b
      GType *tp;
Packit ae235b
      all_registered_types = g_new0 (GType, 1000);
Packit ae235b
      tp = all_registered_types;
Packit ae235b
#include "giotypefuncs.inc"
Packit ae235b
      *tp = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return all_registered_types;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char **argv)
Packit ae235b
{
Packit ae235b
  const GType *otypes;
Packit ae235b
  guint i;
Packit ae235b
  GTestDBus *bus;
Packit ae235b
  gint result;
Packit ae235b
Packit ae235b
  g_setenv ("GIO_USE_VFS", "local", TRUE);
Packit ae235b
  g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
Packit ae235b
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  /* Create one test bus for all tests, as we have a lot of very small
Packit ae235b
   * and quick tests.
Packit ae235b
   */
Packit ae235b
  bus = g_test_dbus_new (G_TEST_DBUS_NONE);
Packit ae235b
  g_test_dbus_up (bus);
Packit ae235b
Packit ae235b
  otypes = list_all_types ();
Packit ae235b
  for (i = 0; otypes[i]; i++)
Packit ae235b
    {
Packit ae235b
      gchar *testname;
Packit ae235b
Packit ae235b
      if (!G_TYPE_IS_CLASSED (otypes[i]))
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      if (G_TYPE_IS_ABSTRACT (otypes[i]))
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      if (!g_type_is_a (otypes[i], G_TYPE_OBJECT))
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      testname = g_strdup_printf ("/Default Values/%s",
Packit ae235b
				  g_type_name (otypes[i]));
Packit ae235b
      g_test_add_data_func (testname, &otypes[i], test_type);
Packit ae235b
      g_free (testname);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = g_test_run ();
Packit ae235b
Packit ae235b
  g_test_dbus_down (bus);
Packit ae235b
  g_object_unref (bus);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}