PageRenderTime 1167ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/gobject/ifaceproperties.c

https://github.com/CyanogenMod/android_external_bluetooth_glib
C | 507 lines | 357 code | 77 blank | 73 comment | 40 complexity | 8b4b4d07a9a55efe34cb9e5e5a72e740 MD5 | raw file
  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. #undef G_LOG_DOMAIN
  20. #define G_LOG_DOMAIN "TestIfaceProperties"
  21. #undef G_DISABLE_ASSERT
  22. #undef G_DISABLE_CHECKS
  23. #undef G_DISABLE_CAST_CHECKS
  24. #include <string.h>
  25. #include <glib-object.h>
  26. #include "testcommon.h"
  27. /* This test tests interface properties, implementing interface
  28. * properties and #GParamSpecOverride.
  29. *
  30. * Four properties are tested:
  31. *
  32. * prop1: Defined in TestIface, Implemented in BaseObject with a GParamSpecOverride
  33. * prop2: Defined in TestIface, Implemented in BaseObject with a new property
  34. * prop3: Defined in TestIface, Implemented in BaseObject, Overridden in DerivedObject
  35. * prop4: Defined in BaseObject, Overridden in DerivedObject
  36. */
  37. static GType base_object_get_type ();
  38. static GType derived_object_get_type ();
  39. enum {
  40. BASE_PROP_0,
  41. BASE_PROP1,
  42. BASE_PROP2,
  43. BASE_PROP3,
  44. BASE_PROP4
  45. };
  46. enum {
  47. DERIVED_PROP_0,
  48. DERIVED_PROP3,
  49. DERIVED_PROP4
  50. };
  51. /*
  52. * BaseObject, a parent class for DerivedObject
  53. */
  54. #define BASE_TYPE_OBJECT (base_object_get_type ())
  55. #define BASE_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BASE_TYPE_OBJECT, BaseObject))
  56. typedef struct _BaseObject BaseObject;
  57. typedef struct _BaseObjectClass BaseObjectClass;
  58. struct _BaseObject
  59. {
  60. GObject parent_instance;
  61. gint val1;
  62. gint val2;
  63. gint val3;
  64. gint val4;
  65. };
  66. struct _BaseObjectClass
  67. {
  68. GObjectClass parent_class;
  69. };
  70. GObjectClass *base_parent_class;
  71. /*
  72. * DerivedObject, the child class of DerivedObject
  73. */
  74. #define DERIVED_TYPE_OBJECT (derived_object_get_type ())
  75. typedef struct _DerivedObject DerivedObject;
  76. typedef struct _DerivedObjectClass DerivedObjectClass;
  77. struct _DerivedObject
  78. {
  79. BaseObject parent_instance;
  80. };
  81. struct _DerivedObjectClass
  82. {
  83. BaseObjectClass parent_class;
  84. };
  85. /*
  86. * The interface
  87. */
  88. typedef struct _TestIfaceClass TestIfaceClass;
  89. struct _TestIfaceClass
  90. {
  91. GTypeInterface base_iface;
  92. };
  93. #define TEST_TYPE_IFACE (test_iface_get_type ())
  94. /* The paramspecs installed on our interface
  95. */
  96. static GParamSpec *iface_spec1, *iface_spec2, *iface_spec3;
  97. /* The paramspecs inherited by our derived object
  98. */
  99. static GParamSpec *inherited_spec1, *inherited_spec2, *inherited_spec3, *inherited_spec4;
  100. static void
  101. test_iface_default_init (TestIfaceClass *iface_vtable)
  102. {
  103. inherited_spec1 = iface_spec1 = g_param_spec_int ("prop1",
  104. "Prop1",
  105. "Property 1",
  106. G_MININT, /* min */
  107. 0xFFFF, /* max */
  108. 42, /* default */
  109. G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
  110. g_object_interface_install_property (iface_vtable, iface_spec1);
  111. iface_spec2 = g_param_spec_int ("prop2",
  112. "Prop2",
  113. "Property 2",
  114. G_MININT, /* min */
  115. G_MAXINT, /* max */
  116. 0, /* default */
  117. G_PARAM_WRITABLE);
  118. g_object_interface_install_property (iface_vtable, iface_spec2);
  119. inherited_spec3 = iface_spec3 = g_param_spec_int ("prop3",
  120. "Prop3",
  121. "Property 3",
  122. G_MININT, /* min */
  123. G_MAXINT, /* max */
  124. 0, /* default */
  125. G_PARAM_READWRITE);
  126. g_object_interface_install_property (iface_vtable, iface_spec3);
  127. }
  128. static DEFINE_IFACE (TestIface, test_iface, NULL, test_iface_default_init)
  129. static GObject*
  130. base_object_constructor (GType type,
  131. guint n_construct_properties,
  132. GObjectConstructParam *construct_properties)
  133. {
  134. /* The constructor is the one place where a GParamSpecOverride is visible
  135. * to the outside world, so we do a bunch of checks here
  136. */
  137. GValue value1 = { 0, };
  138. GValue value2 = { 0, };
  139. GParamSpec *pspec;
  140. g_assert (n_construct_properties == 1);
  141. pspec = construct_properties->pspec;
  142. /* Check we got the param spec we expected
  143. */
  144. g_assert (G_IS_PARAM_SPEC_OVERRIDE (pspec));
  145. g_assert (pspec->param_id == BASE_PROP1);
  146. g_assert (strcmp (g_param_spec_get_name (pspec), "prop1") == 0);
  147. g_assert (g_param_spec_get_redirect_target (pspec) == iface_spec1);
  148. /* Test redirection of the nick and blurb to the redirect target
  149. */
  150. g_assert (strcmp (g_param_spec_get_nick (pspec), "Prop1") == 0);
  151. g_assert (strcmp (g_param_spec_get_blurb (pspec), "Property 1") == 0);
  152. /* Test forwarding of the various GParamSpec methods to the redirect target
  153. */
  154. g_value_init (&value1, G_TYPE_INT);
  155. g_value_init (&value2, G_TYPE_INT);
  156. g_param_value_set_default (pspec, &value1);
  157. g_assert (g_value_get_int (&value1) == 42);
  158. g_value_reset (&value1);
  159. g_value_set_int (&value1, 0x10000);
  160. g_assert (g_param_value_validate (pspec, &value1));
  161. g_assert (g_value_get_int (&value1) == 0xFFFF);
  162. g_assert (!g_param_value_validate (pspec, &value1));
  163. g_value_reset (&value1);
  164. g_value_set_int (&value1, 1);
  165. g_value_set_int (&value2, 2);
  166. g_assert (g_param_values_cmp (pspec, &value1, &value2) < 0);
  167. g_assert (g_param_values_cmp (pspec, &value2, &value1) > 0);
  168. g_value_unset (&value1);
  169. g_value_unset (&value2);
  170. return base_parent_class->constructor (type,
  171. n_construct_properties,
  172. construct_properties);
  173. }
  174. static void
  175. base_object_set_property (GObject *object,
  176. guint prop_id,
  177. const GValue *value,
  178. GParamSpec *pspec)
  179. {
  180. BaseObject *base_object = BASE_OBJECT (object);
  181. switch (prop_id)
  182. {
  183. case BASE_PROP1:
  184. g_assert (pspec == inherited_spec1);
  185. base_object->val1 = g_value_get_int (value);
  186. break;
  187. case BASE_PROP2:
  188. g_assert (pspec == inherited_spec2);
  189. base_object->val2 = g_value_get_int (value);
  190. break;
  191. case BASE_PROP3:
  192. g_assert_not_reached ();
  193. break;
  194. case BASE_PROP4:
  195. g_assert_not_reached ();
  196. break;
  197. default:
  198. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  199. break;
  200. }
  201. }
  202. static void
  203. base_object_get_property (GObject *object,
  204. guint prop_id,
  205. GValue *value,
  206. GParamSpec *pspec)
  207. {
  208. BaseObject *base_object = BASE_OBJECT (object);
  209. switch (prop_id)
  210. {
  211. case BASE_PROP1:
  212. g_assert (pspec == inherited_spec1);
  213. g_value_set_int (value, base_object->val1);
  214. break;
  215. case BASE_PROP2:
  216. g_assert (pspec == inherited_spec2);
  217. g_value_set_int (value, base_object->val2);
  218. break;
  219. case BASE_PROP3:
  220. g_assert_not_reached ();
  221. break;
  222. case BASE_PROP4:
  223. g_assert_not_reached ();
  224. break;
  225. default:
  226. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  227. break;
  228. }
  229. }
  230. static void
  231. base_object_notify (GObject *object,
  232. GParamSpec *pspec)
  233. {
  234. /* The property passed to notify is the redirect target, not the
  235. * GParamSpecOverride
  236. */
  237. g_assert (pspec == inherited_spec1 ||
  238. pspec == inherited_spec2 ||
  239. pspec == inherited_spec3 ||
  240. pspec == inherited_spec4);
  241. }
  242. static void
  243. base_object_class_init (BaseObjectClass *class)
  244. {
  245. GObjectClass *object_class = G_OBJECT_CLASS (class);
  246. base_parent_class= g_type_class_peek_parent (class);
  247. object_class->constructor = base_object_constructor;
  248. object_class->set_property = base_object_set_property;
  249. object_class->get_property = base_object_get_property;
  250. object_class->notify = base_object_notify;
  251. g_object_class_override_property (object_class, BASE_PROP1, "prop1");
  252. /* We override this one using a real property, not GParamSpecOverride
  253. * We change the flags from READONLY to READWRITE to show that we
  254. * can make the flags less restrictive
  255. */
  256. inherited_spec2 = g_param_spec_int ("prop2",
  257. "Prop2",
  258. "Property 2",
  259. G_MININT, /* min */
  260. G_MAXINT, /* max */
  261. 0, /* default */
  262. G_PARAM_READWRITE);
  263. g_object_class_install_property (object_class, BASE_PROP2, inherited_spec2);
  264. g_object_class_override_property (object_class, BASE_PROP3, "prop3");
  265. inherited_spec4 = g_param_spec_int ("prop4",
  266. "Prop4",
  267. "Property 4",
  268. G_MININT, /* min */
  269. G_MAXINT, /* max */
  270. 0, /* default */
  271. G_PARAM_READWRITE);
  272. g_object_class_install_property (object_class, BASE_PROP4, inherited_spec4);
  273. }
  274. static void
  275. base_object_init (BaseObject *base_object)
  276. {
  277. base_object->val1 = 42;
  278. }
  279. static DEFINE_TYPE_FULL (BaseObject, base_object,
  280. base_object_class_init, NULL, base_object_init,
  281. G_TYPE_OBJECT,
  282. INTERFACE (NULL, TEST_TYPE_IFACE))
  283. static void
  284. derived_object_set_property (GObject *object,
  285. guint prop_id,
  286. const GValue *value,
  287. GParamSpec *pspec)
  288. {
  289. BaseObject *base_object = BASE_OBJECT (object);
  290. switch (prop_id)
  291. {
  292. case DERIVED_PROP3:
  293. g_assert (pspec == inherited_spec3);
  294. base_object->val3 = g_value_get_int (value);
  295. break;
  296. case DERIVED_PROP4:
  297. g_assert (pspec == inherited_spec4);
  298. base_object->val4 = g_value_get_int (value);
  299. break;
  300. default:
  301. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  302. break;
  303. }
  304. }
  305. static void
  306. derived_object_get_property (GObject *object,
  307. guint prop_id,
  308. GValue *value,
  309. GParamSpec *pspec)
  310. {
  311. BaseObject *base_object = BASE_OBJECT (object);
  312. switch (prop_id)
  313. {
  314. case DERIVED_PROP3:
  315. g_assert (pspec == inherited_spec3);
  316. g_value_set_int (value, base_object->val3);
  317. break;
  318. case DERIVED_PROP4:
  319. g_assert (pspec == inherited_spec4);
  320. g_value_set_int (value, base_object->val4);
  321. break;
  322. default:
  323. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  324. break;
  325. }
  326. }
  327. static void
  328. derived_object_class_init (DerivedObjectClass *class)
  329. {
  330. GObjectClass *object_class = G_OBJECT_CLASS (class);
  331. object_class->set_property = derived_object_set_property;
  332. object_class->get_property = derived_object_get_property;
  333. /* Overriding a property that is itself overridding an interface property */
  334. g_object_class_override_property (object_class, DERIVED_PROP3, "prop3");
  335. /* Overriding a property not from an interface */
  336. g_object_class_override_property (object_class, DERIVED_PROP4, "prop4");
  337. }
  338. static DEFINE_TYPE (DerivedObject, derived_object,
  339. derived_object_class_init, NULL, NULL,
  340. BASE_TYPE_OBJECT)
  341. /* Helper function for testing ...list_properties()
  342. */
  343. static void
  344. assert_in_properties (GParamSpec *param_spec,
  345. GParamSpec **properties,
  346. gint n_properties)
  347. {
  348. gint i;
  349. gboolean found = FALSE;
  350. for (i = 0; i < n_properties; i++)
  351. {
  352. if (properties[i] == param_spec)
  353. found = TRUE;
  354. }
  355. g_assert (found);
  356. }
  357. int
  358. main (gint argc,
  359. gchar *argv[])
  360. {
  361. BaseObject *object;
  362. GObjectClass *object_class;
  363. TestIfaceClass *iface_vtable;
  364. GParamSpec **properties;
  365. gint n_properties;
  366. gint val1, val2, val3, val4;
  367. g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
  368. G_LOG_LEVEL_WARNING |
  369. G_LOG_LEVEL_CRITICAL);
  370. g_type_init ();
  371. object = g_object_new (DERIVED_TYPE_OBJECT, NULL);
  372. /* Test setting and getting the properties
  373. */
  374. g_object_set (object,
  375. "prop1", 0x0101,
  376. "prop2", 0x0202,
  377. "prop3", 0x0303,
  378. "prop4", 0x0404,
  379. NULL);
  380. g_object_get (object,
  381. "prop1", &val1,
  382. "prop2", &val2,
  383. "prop3", &val3,
  384. "prop4", &val4,
  385. NULL);
  386. g_assert (val1 == 0x0101);
  387. g_assert (val2 == 0x0202);
  388. g_assert (val3 == 0x0303);
  389. g_assert (val4 == 0x0404);
  390. /* Test that the right spec is passed on explicit notifications
  391. */
  392. g_object_freeze_notify (G_OBJECT (object));
  393. g_object_notify (G_OBJECT (object), "prop1");
  394. g_object_notify (G_OBJECT (object), "prop2");
  395. g_object_notify (G_OBJECT (object), "prop3");
  396. g_object_notify (G_OBJECT (object), "prop4");
  397. g_object_thaw_notify (G_OBJECT (object));
  398. /* Test g_object_class_find_property() for overridden properties
  399. */
  400. object_class = G_OBJECT_GET_CLASS (object);
  401. g_assert (g_object_class_find_property (object_class, "prop1") == inherited_spec1);
  402. g_assert (g_object_class_find_property (object_class, "prop2") == inherited_spec2);
  403. g_assert (g_object_class_find_property (object_class, "prop3") == inherited_spec3);
  404. g_assert (g_object_class_find_property (object_class, "prop4") == inherited_spec4);
  405. /* Test g_object_class_list_properties() for overridden properties
  406. */
  407. properties = g_object_class_list_properties (object_class, &n_properties);
  408. g_assert (n_properties == 4);
  409. assert_in_properties (inherited_spec1, properties, n_properties);
  410. assert_in_properties (inherited_spec2, properties, n_properties);
  411. assert_in_properties (inherited_spec3, properties, n_properties);
  412. assert_in_properties (inherited_spec4, properties, n_properties);
  413. g_free (properties);
  414. /* Test g_object_interface_find_property()
  415. */
  416. iface_vtable = g_type_default_interface_peek (TEST_TYPE_IFACE);
  417. g_assert (g_object_interface_find_property (iface_vtable, "prop1") == iface_spec1);
  418. g_assert (g_object_interface_find_property (iface_vtable, "prop2") == iface_spec2);
  419. g_assert (g_object_interface_find_property (iface_vtable, "prop3") == iface_spec3);
  420. /* Test g_object_interface_list_properties()
  421. */
  422. properties = g_object_interface_list_properties (iface_vtable, &n_properties);
  423. g_assert (n_properties == 3);
  424. assert_in_properties (iface_spec1, properties, n_properties);
  425. assert_in_properties (iface_spec2, properties, n_properties);
  426. assert_in_properties (iface_spec3, properties, n_properties);
  427. g_free (properties);
  428. g_object_unref (object);
  429. return 0;
  430. }