glib/tsrc/BC/tests/gobject/ifacecheck.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* GObject - GLib Type, Object, Parameter and Signal Library
       
     2  * Copyright (C) 2001, 2003 Red Hat, Inc.
       
     3  * Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     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 "TestIfaceCheck"
       
    22 
       
    23 #undef G_DISABLE_ASSERT
       
    24 #undef G_DISABLE_CHECKS
       
    25 #undef G_DISABLE_CAST_CHECKS
       
    26 
       
    27 #include <string.h>
       
    28 
       
    29 #include <glib-object.h>
       
    30 
       
    31 #include "testcommon.h"
       
    32 #ifdef SYMBIAN
       
    33   #include "mrt2_glib2_test.h"
       
    34 #endif /*SYMBIAN*/
       
    35 
       
    36 /* This test tests g_type_add_interface_check_func(), which allows
       
    37  * installing a post-initialization check function.
       
    38  */
       
    39 
       
    40 #define TEST_TYPE_IFACE           (test_iface_get_type ())
       
    41 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
       
    42 typedef struct _TestIfaceClass  TestIfaceClass;
       
    43 
       
    44 struct _TestIfaceClass
       
    45 {
       
    46   GTypeInterface base_iface;
       
    47   GString *history;
       
    48 };
       
    49 
       
    50 static void
       
    51 test_iface_base_init (TestIfaceClass *iface)
       
    52 {
       
    53   iface->history = g_string_new (iface->history ? iface->history->str : NULL);
       
    54 }
       
    55 
       
    56 static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, NULL)
       
    57 
       
    58 /*
       
    59  * TestObject1
       
    60  */
       
    61 #define TEST_TYPE_OBJECT1         (test_object1_get_type ())
       
    62 typedef struct _GObject           TestObject1;
       
    63 typedef struct _GObjectClass      TestObject1Class;
       
    64 
       
    65 static DEFINE_TYPE_FULL (TestObject1, test_object1,
       
    66 			 NULL, NULL, NULL,
       
    67 			 G_TYPE_OBJECT,
       
    68 			 INTERFACE (NULL, TEST_TYPE_IFACE))
       
    69      
       
    70 /*
       
    71  * TestObject2
       
    72  */
       
    73 #define TEST_TYPE_OBJECT2         (test_object2_get_type ())
       
    74 typedef struct _GObject           TestObject2;
       
    75 typedef struct _GObjectClass      TestObject2Class;
       
    76 
       
    77 static DEFINE_TYPE_FULL (TestObject2, test_object2,
       
    78 			 NULL, NULL, NULL,
       
    79 			 G_TYPE_OBJECT,
       
    80 			 INTERFACE (NULL, TEST_TYPE_IFACE))
       
    81      
       
    82 /*
       
    83  * TestObject3
       
    84  */
       
    85 #define TEST_TYPE_OBJECT3         (test_object3_get_type ())
       
    86 typedef struct _GObject           TestObject3;
       
    87 typedef struct _GObjectClass      TestObject3Class;
       
    88 
       
    89 static DEFINE_TYPE_FULL (TestObject3, test_object3,
       
    90 			 NULL, NULL, NULL,
       
    91 			 G_TYPE_OBJECT,
       
    92 			 INTERFACE (NULL, TEST_TYPE_IFACE))
       
    93      
       
    94 /*
       
    95  * TestObject4
       
    96  */
       
    97 #define TEST_TYPE_OBJECT4         (test_object4_get_type ())
       
    98 typedef struct _GObject           TestObject4;
       
    99 typedef struct _GObjectClass      TestObject4Class;
       
   100 
       
   101 
       
   102 static DEFINE_TYPE_FULL (TestObject4, test_object4,
       
   103 			 NULL, NULL, NULL,
       
   104 			 G_TYPE_OBJECT, {})
       
   105 
       
   106 static void
       
   107 check_func (gpointer check_data,
       
   108 	    gpointer g_iface)
       
   109 {
       
   110   TestIfaceClass *iface = g_iface;
       
   111 
       
   112   g_string_append (iface->history, check_data);
       
   113 }
       
   114 
       
   115 int
       
   116 main (int   argc,
       
   117       char *argv[])
       
   118 {
       
   119 
       
   120   TestIfaceClass *iface;
       
   121   GObject *object;
       
   122   char *string1 = "A";
       
   123   char *string2 = "B";
       
   124 #ifdef SYMBIAN
       
   125   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);
       
   126   g_set_print_handler(mrtPrintHandler);
       
   127   #endif /*SYMBIAN*/
       
   128   g_type_init ();
       
   129   
       
   130   /* Basic check of interfaces added before class_init time
       
   131    */
       
   132   g_type_add_interface_check (string1, check_func);
       
   133 
       
   134   object = g_object_new (TEST_TYPE_OBJECT1, NULL);
       
   135   iface = TEST_IFACE_GET_CLASS (object);
       
   136     g_assert (strcmp (iface->history->str, "A") == 0);
       
   137   g_object_unref (object);
       
   138 
       
   139   /* Add a second check function
       
   140    */
       
   141   g_type_add_interface_check (string2, check_func);
       
   142 
       
   143   object = g_object_new (TEST_TYPE_OBJECT2, NULL);
       
   144   iface = TEST_IFACE_GET_CLASS (object);
       
   145   g_assert (strcmp (iface->history->str, "AB") == 0);
       
   146   g_object_unref (object);
       
   147 
       
   148   /* Remove the first check function
       
   149    */
       
   150   g_type_remove_interface_check (string1, check_func);
       
   151 
       
   152   object = g_object_new (TEST_TYPE_OBJECT3, NULL);
       
   153   iface = TEST_IFACE_GET_CLASS (object);
       
   154   g_assert (strcmp (iface->history->str, "B") == 0);
       
   155   g_object_unref (object);
       
   156 
       
   157   /* Test interfaces added after class_init time
       
   158    */
       
   159   g_type_class_ref (TEST_TYPE_OBJECT4);
       
   160   {
       
   161     static GInterfaceInfo const iface = {
       
   162       NULL, NULL, NULL
       
   163     };
       
   164     
       
   165     g_type_add_interface_static (TEST_TYPE_OBJECT4, TEST_TYPE_IFACE, &iface);
       
   166   }
       
   167   
       
   168   object = g_object_new (TEST_TYPE_OBJECT4, NULL);
       
   169   iface = TEST_IFACE_GET_CLASS (object);
       
   170   g_assert (strcmp (iface->history->str, "B") == 0);
       
   171   g_object_unref (object);
       
   172   #ifdef SYMBIAN
       
   173   testResultXml("ifacecheck");
       
   174   #endif /*SYMBIAN*/
       
   175   return 0;
       
   176 }