1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/gobject/ifacecheck.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,175 @@
1.4 +/* GObject - GLib Type, Object, Parameter and Signal Library
1.5 + * Copyright (C) 2001, 2003 Red Hat, Inc.
1.6 + * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved.
1.7 + * This library is free software; you can redistribute it and/or
1.8 + * modify it under the terms of the GNU Lesser General Public
1.9 + * License as published by the Free Software Foundation; either
1.10 + * version 2 of the License, or (at your option) any later version.
1.11 + *
1.12 + * This library is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 + * Lesser General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU Lesser General
1.18 + * Public License along with this library; if not, write to the
1.19 + * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1.20 + * Boston, MA 02111-1307, USA.
1.21 + */
1.22 +
1.23 +#undef G_LOG_DOMAIN
1.24 +#define G_LOG_DOMAIN "TestIfaceCheck"
1.25 +
1.26 +#undef G_DISABLE_ASSERT
1.27 +#undef G_DISABLE_CHECKS
1.28 +#undef G_DISABLE_CAST_CHECKS
1.29 +
1.30 +#include <string.h>
1.31 +
1.32 +#include <glib-object.h>
1.33 +
1.34 +#include "testcommon.h"
1.35 +#ifdef __SYMBIAN32__
1.36 + #include "mrt2_glib2_test.h"
1.37 +#endif /*__SYMBIAN32__*/
1.38 +
1.39 +/* This test tests g_type_add_interface_check_func(), which allows
1.40 + * installing a post-initialization check function.
1.41 + */
1.42 +
1.43 +#define TEST_TYPE_IFACE (test_iface_get_type ())
1.44 +#define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
1.45 +typedef struct _TestIfaceClass TestIfaceClass;
1.46 +
1.47 +struct _TestIfaceClass
1.48 +{
1.49 + GTypeInterface base_iface;
1.50 + GString *history;
1.51 +};
1.52 +
1.53 +static void
1.54 +test_iface_base_init (TestIfaceClass *iface)
1.55 +{
1.56 + iface->history = g_string_new (iface->history ? iface->history->str : NULL);
1.57 +}
1.58 +
1.59 +static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, NULL)
1.60 +
1.61 +/*
1.62 + * TestObject1
1.63 + */
1.64 +#define TEST_TYPE_OBJECT1 (test_object1_get_type ())
1.65 +typedef struct _GObject TestObject1;
1.66 +typedef struct _GObjectClass TestObject1Class;
1.67 +
1.68 +static DEFINE_TYPE_FULL (TestObject1, test_object1,
1.69 + NULL, NULL, NULL,
1.70 + G_TYPE_OBJECT,
1.71 + INTERFACE (NULL, TEST_TYPE_IFACE))
1.72 +
1.73 +/*
1.74 + * TestObject2
1.75 + */
1.76 +#define TEST_TYPE_OBJECT2 (test_object2_get_type ())
1.77 +typedef struct _GObject TestObject2;
1.78 +typedef struct _GObjectClass TestObject2Class;
1.79 +
1.80 +static DEFINE_TYPE_FULL (TestObject2, test_object2,
1.81 + NULL, NULL, NULL,
1.82 + G_TYPE_OBJECT,
1.83 + INTERFACE (NULL, TEST_TYPE_IFACE))
1.84 +
1.85 +/*
1.86 + * TestObject3
1.87 + */
1.88 +#define TEST_TYPE_OBJECT3 (test_object3_get_type ())
1.89 +typedef struct _GObject TestObject3;
1.90 +typedef struct _GObjectClass TestObject3Class;
1.91 +
1.92 +static DEFINE_TYPE_FULL (TestObject3, test_object3,
1.93 + NULL, NULL, NULL,
1.94 + G_TYPE_OBJECT,
1.95 + INTERFACE (NULL, TEST_TYPE_IFACE))
1.96 +
1.97 +/*
1.98 + * TestObject4
1.99 + */
1.100 +#define TEST_TYPE_OBJECT4 (test_object4_get_type ())
1.101 +typedef struct _GObject TestObject4;
1.102 +typedef struct _GObjectClass TestObject4Class;
1.103 +
1.104 +
1.105 +static DEFINE_TYPE_FULL (TestObject4, test_object4,
1.106 + NULL, NULL, NULL,
1.107 + G_TYPE_OBJECT, {})
1.108 +
1.109 +static void
1.110 +check_func (gpointer check_data,
1.111 + gpointer g_iface)
1.112 +{
1.113 + TestIfaceClass *iface = g_iface;
1.114 +
1.115 + g_string_append (iface->history, check_data);
1.116 +}
1.117 +
1.118 +int
1.119 +main (int argc,
1.120 + char *argv[])
1.121 +{
1.122 + TestIfaceClass *iface;
1.123 + GObject *object;
1.124 + char *string1 = "A";
1.125 + char *string2 = "B";
1.126 +#ifdef __SYMBIAN32__
1.127 + g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
1.128 + g_set_print_handler(mrtPrintHandler);
1.129 + #endif /*__SYMBIAN32__*/
1.130 + g_type_init ();
1.131 +
1.132 + /* Basic check of interfaces added before class_init time
1.133 + */
1.134 + g_type_add_interface_check (string1, check_func);
1.135 +
1.136 + object = g_object_new (TEST_TYPE_OBJECT1, NULL);
1.137 + iface = TEST_IFACE_GET_CLASS (object);
1.138 + g_assert (strcmp (iface->history->str, "A") == 0);
1.139 + g_object_unref (object);
1.140 +
1.141 + /* Add a second check function
1.142 + */
1.143 + g_type_add_interface_check (string2, check_func);
1.144 +
1.145 + object = g_object_new (TEST_TYPE_OBJECT2, NULL);
1.146 + iface = TEST_IFACE_GET_CLASS (object);
1.147 + g_assert (strcmp (iface->history->str, "AB") == 0);
1.148 + g_object_unref (object);
1.149 +
1.150 + /* Remove the first check function
1.151 + */
1.152 + g_type_remove_interface_check (string1, check_func);
1.153 +
1.154 + object = g_object_new (TEST_TYPE_OBJECT3, NULL);
1.155 + iface = TEST_IFACE_GET_CLASS (object);
1.156 + g_assert (strcmp (iface->history->str, "B") == 0);
1.157 + g_object_unref (object);
1.158 +
1.159 + /* Test interfaces added after class_init time
1.160 + */
1.161 + g_type_class_ref (TEST_TYPE_OBJECT4);
1.162 + {
1.163 + static GInterfaceInfo const iface = {
1.164 + NULL, NULL, NULL
1.165 + };
1.166 +
1.167 + g_type_add_interface_static (TEST_TYPE_OBJECT4, TEST_TYPE_IFACE, &iface);
1.168 + }
1.169 +
1.170 + object = g_object_new (TEST_TYPE_OBJECT4, NULL);
1.171 + iface = TEST_IFACE_GET_CLASS (object);
1.172 + g_assert (strcmp (iface->history->str, "B") == 0);
1.173 + g_object_unref (object);
1.174 + #ifdef __SYMBIAN32__
1.175 + testResultXml("ifacecheck");
1.176 + #endif /*__SYMBIAN32__*/
1.177 + return 0;
1.178 +}