1/* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20#undef	G_LOG_DOMAIN
21#define	G_LOG_DOMAIN "TestDynamicType"
22
23#undef G_DISABLE_ASSERT
24#undef G_DISABLE_CHECKS
25#undef G_DISABLE_CAST_CHECKS
26
27#include <glib-object.h>
28
29#include "testcommon.h"
30#include "testmodule.h"
31
32/* This test tests the macros for defining dynamic types.
33 */
34
35static gboolean loaded = FALSE;
36
37#define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
38
39typedef GObject DynamicObject;
40typedef struct _DynamicObjectClass DynamicObjectClass;
41
42struct _DynamicObjectClass
43{
44  GObjectClass parent_class;
45  guint val;
46};
47
48G_DEFINE_DYNAMIC_TYPE(DynamicObject, dynamic_object, G_TYPE_OBJECT);
49
50static void
51dynamic_object_class_init (DynamicObjectClass *class)
52{
53  class->val = 42;
54  loaded = TRUE;
55}
56
57static void
58dynamic_object_class_finalize (DynamicObjectClass *class)
59{
60  loaded = FALSE;
61}
62
63static void
64dynamic_object_init (DynamicObject *dynamic_object)
65{
66}
67
68
69static void
70module_register (GTypeModule *module)
71{
72  dynamic_object_register_type (module);
73}
74
75static void
76test_dynamic_type (void)
77{
78  GTypeModule *module;
79  DynamicObjectClass *class;
80
81  module = test_module_new (module_register);
82
83  /* Not loaded until we call ref for the first time */
84  class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
85  g_assert (class == NULL);
86  g_assert (!loaded);
87
88  /* Ref loads */
89  class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
90  g_assert (class && class->val == 42);
91  g_assert (loaded);
92
93  /* Peek then works */
94  class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
95  g_assert (class && class->val == 42);
96  g_assert (loaded);
97
98  /* Unref causes finalize */
99  g_type_class_unref (class);
100
101  /* Peek returns NULL */
102  class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
103  g_assert (!class);
104  g_assert (!loaded);
105
106  /* Ref reloads */
107  class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
108  g_assert (class && class->val == 42);
109  g_assert (loaded);
110
111  /* And Unref causes finalize once more*/
112  g_type_class_unref (class);
113  class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
114  g_assert (!class);
115  g_assert (!loaded);
116}
117
118int
119main (int   argc,
120      char *argv[])
121{
122  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
123			  G_LOG_LEVEL_WARNING |
124			  G_LOG_LEVEL_CRITICAL);
125  g_type_init ();
126
127  test_dynamic_type ();
128
129  return 0;
130}
131