10 from gi
.repository
import GObject
11 from gi
.repository
.GObject
import ParamFlags
, GType
, new
12 from gi
.repository
.GObject
import \
13 TYPE_INT
, TYPE_UINT
, TYPE_LONG
, TYPE_ULONG
, TYPE_INT64
, \
14 TYPE_UINT64
, TYPE_GTYPE
, TYPE_INVALID
, TYPE_NONE
, TYPE_STRV
, \
15 TYPE_INTERFACE
, TYPE_CHAR
, TYPE_UCHAR
, TYPE_BOOLEAN
, TYPE_FLOAT
, \
16 TYPE_DOUBLE
, TYPE_POINTER
, TYPE_BOXED
, TYPE_PARAM
, TYPE_OBJECT
, \
17 TYPE_STRING
, TYPE_PYOBJECT
, TYPE_VARIANT
19 from gi
.repository
.GLib
import \
20 MININT
, MAXINT
, MAXUINT
, MINLONG
, MAXLONG
, MAXULONG
, \
21 MAXUINT64
, MAXINT64
, MININT64
23 from gi
.repository
import Gio
24 from gi
.repository
import GLib
25 gi
.require_version('GIMarshallingTests', '1.0')
26 from gi
.repository
import GIMarshallingTests
27 from gi
import _propertyhelper
as propertyhelper
30 gi
.require_version('Regress', '1.0')
31 from gi
.repository
import Regress
33 except (ValueError, ImportError):
36 if sys
.version_info
< (3, 0):
37 TEST_UTF8
= "\xe2\x99\xa5"
38 UNICODE_UTF8
= unicode(TEST_UTF8
, 'UTF-8')
41 UNICODE_UTF8
= TEST_UTF8
43 from compathelper
import _long
, _unicode
44 from helper
import capture_glib_warnings
, capture_output
47 class PropertyObject(GObject
.GObject
):
48 normal
= GObject
.Property(type=str)
49 construct
= GObject
.Property(
51 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
,
54 construct_only
= GObject
.Property(
56 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT_ONLY
)
58 uint64
= GObject
.Property(
60 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
)
62 enum
= GObject
.Property(
63 type=Gio
.SocketType
, default
=Gio
.SocketType
.STREAM
)
65 boxed
= GObject
.Property(
67 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
)
69 flags
= GObject
.Property(
70 type=GIMarshallingTests
.Flags
,
71 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
,
72 default
=GIMarshallingTests
.Flags
.VALUE1
)
74 gtype
= GObject
.Property(
76 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
)
78 strings
= GObject
.Property(
80 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
)
82 variant
= GObject
.Property(
84 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
)
86 variant_def
= GObject
.Property(
88 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
,
89 default
=GLib
.Variant('i', 42))
91 interface
= GObject
.Property(
93 flags
=ParamFlags
.READABLE | ParamFlags
.WRITABLE | ParamFlags
.CONSTRUCT
)
97 class PropertyInheritanceObject(Regress
.TestObj
):
98 # override property from the base class, with a different type
99 string
= GObject
.Property(type=int)
101 # a property entirely defined at the Python level
102 python_prop
= GObject
.Property(type=str)
104 class PropertySubClassObject(PropertyInheritanceObject
):
105 # override property from the base class, with a different type
106 python_prop
= GObject
.Property(type=int)
109 @unittest.skipUnless(has_regress
, 'Missing Regress typelib')
110 class TestPropertyInheritanceObject(unittest
.TestCase
):
111 def test_override_gi_property(self
):
112 self
.assertNotEqual(Regress
.TestObj
.props
.string
.value_type
,
113 PropertyInheritanceObject
.props
.string
.value_type
)
114 obj
= PropertyInheritanceObject()
115 self
.assertEqual(type(obj
.props
.string
), int)
117 self
.assertEqual(obj
.props
.string
, 4)
119 def test_override_python_property(self
):
120 obj
= PropertySubClassObject()
121 self
.assertEqual(type(obj
.props
.python_prop
), int)
122 obj
.props
.python_prop
= 5
123 self
.assertEqual(obj
.props
.python_prop
, 5)
126 class TestPropertyObject(unittest
.TestCase
):
127 def test_get_set(self
):
128 obj
= PropertyObject()
129 obj
.props
.normal
= "value"
130 self
.assertEqual(obj
.props
.normal
, "value")
132 def test_hasattr_on_object(self
):
133 obj
= PropertyObject()
134 self
.assertTrue(hasattr(obj
.props
, "normal"))
136 def test_hasattr_on_class(self
):
137 self
.assertTrue(hasattr(PropertyObject
.props
, "normal"))
139 def test_set_on_class(self
):
141 obj
.props
.normal
= "foobar"
143 self
.assertRaises(TypeError, set, PropertyObject
)
145 def test_iteration(self
):
146 for obj
in (PropertyObject
.props
, PropertyObject().props
):
150 self
.assertEqual(gtype
.parent
.name
, 'GParam')
151 names
.append(pspec
.name
)
154 self
.assertEqual(names
, ['boxed',
167 def test_normal(self
):
168 obj
= new(PropertyObject
, normal
="123")
169 self
.assertEqual(obj
.props
.normal
, "123")
170 obj
.set_property('normal', '456')
171 self
.assertEqual(obj
.props
.normal
, "456")
172 obj
.props
.normal
= '789'
173 self
.assertEqual(obj
.props
.normal
, "789")
175 def test_construct(self
):
176 obj
= new(PropertyObject
, construct
="123")
177 self
.assertEqual(obj
.props
.construct
, "123")
178 obj
.set_property('construct', '456')
179 self
.assertEqual(obj
.props
.construct
, "456")
180 obj
.props
.construct
= '789'
181 self
.assertEqual(obj
.props
.construct
, "789")
184 obj
= new(PropertyObject
, construct_only
=UNICODE_UTF8
)
185 self
.assertEqual(obj
.props
.construct_only
, TEST_UTF8
)
186 obj
.set_property('construct', UNICODE_UTF8
)
187 self
.assertEqual(obj
.props
.construct
, TEST_UTF8
)
188 obj
.props
.normal
= UNICODE_UTF8
189 self
.assertEqual(obj
.props
.normal
, TEST_UTF8
)
191 def test_int_to_str(self
):
192 obj
= new(PropertyObject
, construct_only
=1)
193 self
.assertEqual(obj
.props
.construct_only
, '1')
194 obj
.set_property('construct', '2')
195 self
.assertEqual(obj
.props
.construct
, '2')
197 self
.assertEqual(obj
.props
.normal
, '3')
199 def test_construct_only(self
):
200 obj
= new(PropertyObject
, construct_only
="123")
201 self
.assertEqual(obj
.props
.construct_only
, "123")
202 self
.assertRaises(TypeError,
203 setattr, obj
.props
, 'construct_only', '456')
204 self
.assertRaises(TypeError,
205 obj
.set_property
, 'construct-only', '456')
207 def test_uint64(self
):
208 obj
= new(PropertyObject
)
209 self
.assertEqual(obj
.props
.uint64
, 0)
210 obj
.props
.uint64
= _long(1)
211 self
.assertEqual(obj
.props
.uint64
, _long(1))
213 self
.assertEqual(obj
.props
.uint64
, _long(1))
215 self
.assertRaises((TypeError, OverflowError), obj
.set_property
, "uint64", _long(-1))
216 self
.assertRaises((TypeError, OverflowError), obj
.set_property
, "uint64", -1)
218 def test_uint64_default_value(self
):
220 class TimeControl(GObject
.GObject
):
222 'time': (TYPE_UINT64
, 'Time', 'Time',
223 _long(0), (1 << 64) - 1, _long(0),
226 except OverflowError:
227 (etype
, ex
) = sys
.exc_info()[2:]
231 obj
= new(PropertyObject
)
232 self
.assertEqual(obj
.props
.enum
, Gio
.SocketType
.STREAM
)
233 self
.assertEqual(obj
.enum
, Gio
.SocketType
.STREAM
)
234 obj
.enum
= Gio
.SocketType
.DATAGRAM
235 self
.assertEqual(obj
.props
.enum
, Gio
.SocketType
.DATAGRAM
)
236 self
.assertEqual(obj
.enum
, Gio
.SocketType
.DATAGRAM
)
237 obj
.props
.enum
= Gio
.SocketType
.STREAM
238 self
.assertEqual(obj
.props
.enum
, Gio
.SocketType
.STREAM
)
239 self
.assertEqual(obj
.enum
, Gio
.SocketType
.STREAM
)
241 self
.assertEqual(obj
.props
.enum
, Gio
.SocketType
.DATAGRAM
)
242 self
.assertEqual(obj
.enum
, Gio
.SocketType
.DATAGRAM
)
244 self
.assertEqual(obj
.props
.enum
, Gio
.SocketType
.STREAM
)
245 self
.assertEqual(obj
.enum
, Gio
.SocketType
.STREAM
)
247 self
.assertRaises(TypeError, setattr, obj
, 'enum', 'foo')
248 self
.assertRaises(TypeError, setattr, obj
, 'enum', object())
250 self
.assertRaises(TypeError, GObject
.Property
, type=Gio
.SocketType
)
251 self
.assertRaises(TypeError, GObject
.Property
, type=Gio
.SocketType
,
252 default
=Gio
.SocketProtocol
.TCP
)
253 self
.assertRaises(TypeError, GObject
.Property
, type=Gio
.SocketType
,
255 self
.assertRaises(TypeError, GObject
.Property
, type=Gio
.SocketType
,
258 def test_flags(self
):
259 obj
= new(PropertyObject
)
260 self
.assertEqual(obj
.props
.flags
, GIMarshallingTests
.Flags
.VALUE1
)
261 self
.assertEqual(obj
.flags
, GIMarshallingTests
.Flags
.VALUE1
)
263 obj
.flags
= GIMarshallingTests
.Flags
.VALUE2 | GIMarshallingTests
.Flags
.VALUE3
264 self
.assertEqual(obj
.props
.flags
, GIMarshallingTests
.Flags
.VALUE2 | GIMarshallingTests
.Flags
.VALUE3
)
265 self
.assertEqual(obj
.flags
, GIMarshallingTests
.Flags
.VALUE2 | GIMarshallingTests
.Flags
.VALUE3
)
267 self
.assertRaises(TypeError, setattr, obj
, 'flags', 'foo')
268 self
.assertRaises(TypeError, setattr, obj
, 'flags', object())
269 self
.assertRaises(TypeError, setattr, obj
, 'flags', None)
271 self
.assertRaises(TypeError, GObject
.Property
,
272 type=GIMarshallingTests
.Flags
, default
='foo')
273 self
.assertRaises(TypeError, GObject
.Property
,
274 type=GIMarshallingTests
.Flags
, default
=object())
275 self
.assertRaises(TypeError, GObject
.Property
,
276 type=GIMarshallingTests
.Flags
, default
=None)
278 def test_gtype(self
):
279 obj
= new(PropertyObject
)
281 self
.assertEqual(obj
.props
.gtype
, TYPE_NONE
)
282 self
.assertEqual(obj
.gtype
, TYPE_NONE
)
284 obj
.gtype
= TYPE_UINT64
285 self
.assertEqual(obj
.props
.gtype
, TYPE_UINT64
)
286 self
.assertEqual(obj
.gtype
, TYPE_UINT64
)
288 obj
.gtype
= TYPE_INVALID
289 self
.assertEqual(obj
.props
.gtype
, TYPE_INVALID
)
290 self
.assertEqual(obj
.gtype
, TYPE_INVALID
)
292 # GType parameters do not support defaults in GLib
293 self
.assertRaises(TypeError, GObject
.Property
, type=TYPE_GTYPE
,
297 self
.assertRaises(TypeError, setattr, obj
, 'gtype', 'foo')
298 self
.assertRaises(TypeError, setattr, obj
, 'gtype', object())
300 self
.assertRaises(TypeError, GObject
.Property
, type=TYPE_GTYPE
,
302 self
.assertRaises(TypeError, GObject
.Property
, type=TYPE_GTYPE
,
306 obj
= new(PropertyObject
, gtype
=TYPE_UINT
)
307 self
.assertEqual(obj
.props
.gtype
, TYPE_UINT
)
308 self
.assertEqual(obj
.gtype
, TYPE_UINT
)
310 def test_boxed(self
):
311 obj
= new(PropertyObject
)
313 regex
= GLib
.Regex
.new('[a-z]*', 0, 0)
314 obj
.props
.boxed
= regex
315 self
.assertEqual(obj
.props
.boxed
.get_pattern(), '[a-z]*')
316 self
.assertEqual(obj
.boxed
.get_pattern(), '[a-z]*')
318 self
.assertRaises(TypeError, setattr, obj
, 'boxed', 'foo')
319 self
.assertRaises(TypeError, setattr, obj
, 'boxed', object())
321 def test_strings(self
):
322 obj
= new(PropertyObject
)
324 # Should work with actual GStrv objects as well as
325 # Python string lists
327 __gtype__
= GObject
.TYPE_STRV
329 self
.assertEqual(obj
.props
.strings
, GStrv([]))
330 self
.assertEqual(obj
.strings
, GStrv([]))
331 self
.assertEqual(obj
.props
.strings
, [])
332 self
.assertEqual(obj
.strings
, [])
334 obj
.strings
= ['hello', 'world']
335 self
.assertEqual(obj
.props
.strings
, ['hello', 'world'])
336 self
.assertEqual(obj
.strings
, ['hello', 'world'])
338 obj
.strings
= GStrv(['hello', 'world'])
339 self
.assertEqual(obj
.props
.strings
, GStrv(['hello', 'world']))
340 self
.assertEqual(obj
.strings
, GStrv(['hello', 'world']))
343 self
.assertEqual(obj
.strings
, [])
344 obj
.strings
= GStrv([])
345 self
.assertEqual(obj
.strings
, GStrv([]))
347 p
= GObject
.Property(type=TYPE_STRV
, default
=['hello', '1'])
348 self
.assertEqual(p
.default
, ['hello', '1'])
349 self
.assertEqual(p
.type, TYPE_STRV
)
350 p
= GObject
.Property(type=TYPE_STRV
, default
=GStrv(['hello', '1']))
351 self
.assertEqual(p
.default
, ['hello', '1'])
352 self
.assertEqual(p
.type, TYPE_STRV
)
355 obj
= new(PropertyObject
, strings
=['hello', 'world'])
356 self
.assertEqual(obj
.props
.strings
, ['hello', 'world'])
357 self
.assertEqual(obj
.strings
, ['hello', 'world'])
360 self
.assertRaises(TypeError, setattr, obj
, 'strings', 1)
361 self
.assertRaises(TypeError, setattr, obj
, 'strings', 'foo')
362 self
.assertRaises(TypeError, setattr, obj
, 'strings', ['foo', 1])
364 self
.assertRaises(TypeError, GObject
.Property
, type=TYPE_STRV
,
366 self
.assertRaises(TypeError, GObject
.Property
, type=TYPE_STRV
,
368 self
.assertRaises(TypeError, GObject
.Property
, type=TYPE_STRV
,
369 default
=['hello', 1])
371 def test_variant(self
):
372 obj
= new(PropertyObject
)
374 self
.assertEqual(obj
.props
.variant
, None)
375 self
.assertEqual(obj
.variant
, None)
377 obj
.variant
= GLib
.Variant('s', 'hello')
378 self
.assertEqual(obj
.variant
.print_(True), "'hello'")
380 obj
.variant
= GLib
.Variant('b', True)
381 self
.assertEqual(obj
.variant
.print_(True), "true")
383 obj
.props
.variant
= GLib
.Variant('y', 2)
384 self
.assertEqual(obj
.variant
.print_(True), "byte 0x02")
387 self
.assertEqual(obj
.variant
, None)
390 obj
= new(PropertyObject
, variant
=GLib
.Variant('u', 5))
391 self
.assertEqual(obj
.props
.variant
.print_(True), 'uint32 5')
393 GObject
.Property(type=TYPE_VARIANT
, default
=GLib
.Variant('i', 1))
396 self
.assertRaises(TypeError, setattr, obj
, 'variant', 'foo')
397 self
.assertRaises(TypeError, setattr, obj
, 'variant', 42)
399 self
.assertRaises(TypeError, GObject
.Property
, type=TYPE_VARIANT
,
401 self
.assertRaises(TypeError, GObject
.Property
, type=TYPE_VARIANT
,
404 def test_variant_default(self
):
405 obj
= new(PropertyObject
)
407 self
.assertEqual(obj
.props
.variant_def
.print_(True), '42')
408 self
.assertEqual(obj
.variant_def
.print_(True), '42')
410 obj
.props
.variant_def
= GLib
.Variant('y', 2)
411 self
.assertEqual(obj
.variant_def
.print_(True), "byte 0x02")
414 obj
= new(PropertyObject
, variant_def
=GLib
.Variant('u', 5))
415 self
.assertEqual(obj
.props
.variant_def
.print_(True), 'uint32 5')
417 def test_interface(self
):
418 obj
= new(PropertyObject
)
420 file = Gio
.File
.new_for_path('/some/path')
421 obj
.props
.interface
= file
422 self
.assertEqual(obj
.props
.interface
.get_path(), '/some/path')
423 self
.assertEqual(obj
.interface
.get_path(), '/some/path')
425 self
.assertRaises(TypeError, setattr, obj
, 'interface', 'foo')
426 self
.assertRaises(TypeError, setattr, obj
, 'interface', object())
428 def test_range(self
):
431 return 2 ** ((8 * struct
.calcsize(c
)) - 1) - 1
434 return 2 ** (8 * struct
.calcsize(c
)) - 1
440 minlong
= -maxlong
- 1
443 minint64
= -maxint64
- 1
444 maxuint64
= umax('Q')
446 types_
= dict(int=(TYPE_INT
, minint
, maxint
),
447 uint
=(TYPE_UINT
, 0, maxuint
),
448 long=(TYPE_LONG
, minlong
, maxlong
),
449 ulong
=(TYPE_ULONG
, 0, maxulong
),
450 int64
=(TYPE_INT64
, minint64
, maxint64
),
451 uint64
=(TYPE_UINT64
, 0, maxuint64
))
453 def build_gproperties(types_
):
455 for key
, (gtype
, min, max) in types_
.items():
456 d
[key
] = (gtype
, 'blurb', 'desc', min, max, 0,
457 ParamFlags
.READABLE | ParamFlags
.WRITABLE
)
460 class RangeCheck(GObject
.GObject
):
461 __gproperties__
= build_gproperties(types_
)
465 GObject
.GObject
.__init
__(self
)
467 def do_set_property(self
, pspec
, value
):
468 self
.values
[pspec
.name
] = value
470 def do_get_property(self
, pspec
):
471 return self
.values
.get(pspec
.name
, pspec
.default_value
)
473 self
.assertEqual(RangeCheck
.props
.int.minimum
, minint
)
474 self
.assertEqual(RangeCheck
.props
.int.maximum
, maxint
)
475 self
.assertEqual(RangeCheck
.props
.uint
.minimum
, 0)
476 self
.assertEqual(RangeCheck
.props
.uint
.maximum
, maxuint
)
477 self
.assertEqual(RangeCheck
.props
.long.minimum
, minlong
)
478 self
.assertEqual(RangeCheck
.props
.long.maximum
, maxlong
)
479 self
.assertEqual(RangeCheck
.props
.ulong
.minimum
, 0)
480 self
.assertEqual(RangeCheck
.props
.ulong
.maximum
, maxulong
)
481 self
.assertEqual(RangeCheck
.props
.int64
.minimum
, minint64
)
482 self
.assertEqual(RangeCheck
.props
.int64
.maximum
, maxint64
)
483 self
.assertEqual(RangeCheck
.props
.uint64
.minimum
, 0)
484 self
.assertEqual(RangeCheck
.props
.uint64
.maximum
, maxuint64
)
487 for key
, (gtype
, min, max) in types_
.items():
488 self
.assertEqual(obj
.get_property(key
),
489 getattr(RangeCheck
.props
, key
).default_value
)
491 obj
.set_property(key
, min)
492 self
.assertEqual(obj
.get_property(key
), min)
494 obj
.set_property(key
, max)
495 self
.assertEqual(obj
.get_property(key
), max)
497 def test_multi(self
):
498 obj
= PropertyObject()
499 obj
.set_properties(normal
="foo",
501 normal
, uint64
= obj
.get_properties("normal", "uint64")
502 self
.assertEqual(normal
, "foo")
503 self
.assertEqual(uint64
, 7)
506 class TestProperty(unittest
.TestCase
):
507 def test_simple(self
):
508 class C(GObject
.GObject
):
509 str = GObject
.Property(type=str)
510 int = GObject
.Property(type=int)
511 float = GObject
.Property(type=float)
512 long = GObject
.Property(type=_long
)
514 self
.assertTrue(hasattr(C
.props
, 'str'))
515 self
.assertTrue(hasattr(C
.props
, 'int'))
516 self
.assertTrue(hasattr(C
.props
, 'float'))
517 self
.assertTrue(hasattr(C
.props
, 'long'))
520 self
.assertEqual(o
.str, '')
522 self
.assertEqual(o
.str, 'str')
524 self
.assertEqual(o
.int, 0)
526 self
.assertEqual(o
.int, 1138)
528 self
.assertEqual(o
.float, 0.0)
530 self
.assertEqual(o
.float, 3.14)
532 self
.assertEqual(o
.long, _long(0))
534 self
.assertEqual(o
.long, _long(100))
536 def test_custom_getter(self
):
537 class C(GObject
.GObject
):
540 prop
= GObject
.Property(getter
=get_prop
)
543 self
.assertEqual(o
.prop
, 'value')
544 self
.assertRaises(TypeError, setattr, o
, 'prop', 'xxx')
546 @unittest.expectedFailure
# https://bugzilla.gnome.org/show_bug.cgi?id=575652
547 def test_getter_exception(self
):
548 class C(GObject
.Object
):
549 @GObject.Property(type=int)
551 raise ValueError('something bad happend')
555 # silence exception printed to stderr
556 with
capture_output():
557 with self
.assertRaisesRegex(ValueError, 'something bad happend'):
560 with self
.assertRaisesRegex(ValueError, 'something bad happend'):
561 o
.get_property('prop')
563 with self
.assertRaisesRegex(ValueError, 'something bad happend'):
566 def test_custom_setter(self
):
567 class C(GObject
.GObject
):
568 def set_prop(self
, value
):
570 prop
= GObject
.Property(setter
=set_prop
)
574 GObject
.GObject
.__init
__(self
)
577 self
.assertEqual(o
._value
, None)
579 self
.assertEqual(o
._value
, 'bar')
580 self
.assertRaises(TypeError, getattr, o
, 'prop')
582 def test_decorator_default(self
):
583 class C(GObject
.GObject
):
591 def value_setter(self
, value
):
595 self
.assertEqual(o
.value
, 'value')
597 self
.assertEqual(o
.value
, 'blah')
598 self
.assertEqual(o
.props
.value
, 'blah')
600 def test_decorator_private_setter(self
):
601 class C(GObject
.GObject
):
609 def _set_value(self
, value
):
613 self
.assertEqual(o
.value
, 'value')
615 self
.assertEqual(o
.value
, 'blah')
616 self
.assertEqual(o
.props
.value
, 'blah')
618 def test_decorator_with_call(self
):
619 class C(GObject
.GObject
):
622 @GObject.Property(type=int, default
=1, minimum
=1, maximum
=10)
623 def typedValue(self
):
627 def typedValue_setter(self
, value
):
631 self
.assertEqual(o
.typedValue
, 1)
633 self
.assertEqual(o
.typedValue
, 5)
634 self
.assertEqual(o
.props
.typedValue
, 5)
636 def test_errors(self
):
637 self
.assertRaises(TypeError, GObject
.Property
, type='str')
638 self
.assertRaises(TypeError, GObject
.Property
, nick
=False)
639 self
.assertRaises(TypeError, GObject
.Property
, blurb
=False)
640 # this never fail while bool is a subclass of int
643 # self.assertRaises(TypeError, GObject.Property, type=bool, default=0)
644 self
.assertRaises(TypeError, GObject
.Property
, type=bool, default
='ciao mamma')
645 self
.assertRaises(TypeError, GObject
.Property
, type=bool)
646 self
.assertRaises(TypeError, GObject
.Property
, type=object, default
=0)
647 self
.assertRaises(TypeError, GObject
.Property
, type=complex)
649 def test_defaults(self
):
650 GObject
.Property(type=bool, default
=True)
651 GObject
.Property(type=bool, default
=False)
653 def test_name_with_underscore(self
):
654 class C(GObject
.GObject
):
655 prop_name
= GObject
.Property(type=int)
658 self
.assertEqual(o
.prop_name
, 10)
660 def test_range(self
):
662 (TYPE_INT
, MININT
, MAXINT
),
663 (TYPE_UINT
, 0, MAXUINT
),
664 (TYPE_LONG
, MINLONG
, MAXLONG
),
665 (TYPE_ULONG
, 0, MAXULONG
),
666 (TYPE_INT64
, MININT64
, MAXINT64
),
667 (TYPE_UINT64
, 0, MAXUINT64
),
670 for gtype
, min, max in types_
:
671 # Normal, everything is alright
672 prop
= GObject
.Property(type=gtype
, minimum
=min, maximum
=max)
673 subtype
= type('', (GObject
.GObject
,), dict(prop
=prop
))
674 self
.assertEqual(subtype
.props
.prop
.minimum
, min)
675 self
.assertEqual(subtype
.props
.prop
.maximum
, max)
678 self
.assertRaises(TypeError,
679 GObject
.Property
, type=gtype
, minimum
=min - 1,
682 # Higher than maximum
683 self
.assertRaises(TypeError,
684 GObject
.Property
, type=gtype
, minimum
=min,
687 def test_min_max(self
):
688 class C(GObject
.GObject
):
689 prop_int
= GObject
.Property(type=int, minimum
=1, maximum
=100, default
=1)
690 prop_float
= GObject
.Property(type=float, minimum
=0.1, maximum
=10.5, default
=1.1)
693 GObject
.GObject
.__init
__(self
)
695 # we test known-bad values here which cause Gtk-WARNING logs.
696 # Explicitly allow these for this test.
697 with
capture_glib_warnings(allow_warnings
=True):
699 self
.assertEqual(o
.prop_int
, 1)
702 self
.assertEqual(o
.prop_int
, 5)
705 self
.assertEqual(o
.prop_int
, 5)
708 self
.assertEqual(o
.prop_int
, 5)
710 self
.assertEqual(o
.prop_float
, 1.1)
713 self
.assertEqual(o
.prop_float
, 7.75)
716 self
.assertEqual(o
.prop_float
, 7.75)
719 self
.assertEqual(o
.prop_float
, 7.75)
721 def test_multiple_instances(self
):
722 class C(GObject
.GObject
):
723 prop
= GObject
.Property(type=str, default
='default')
727 self
.assertEqual(o1
.prop
, 'default')
728 self
.assertEqual(o2
.prop
, 'default')
730 self
.assertEqual(o1
.prop
, 'value')
731 self
.assertEqual(o2
.prop
, 'default')
733 def test_object_property(self
):
734 class PropertyObject(GObject
.GObject
):
735 obj
= GObject
.Property(type=GObject
.GObject
)
737 pobj1
= PropertyObject()
738 obj1_hash
= hash(pobj1
)
739 pobj2
= PropertyObject()
744 self
.assertEqual(hash(pobj1
), obj1_hash
)
746 def test_object_subclass_property(self
):
747 class ObjectSubclass(GObject
.GObject
):
748 __gtype_name__
= 'ObjectSubclass'
750 class PropertyObjectSubclass(GObject
.GObject
):
751 obj
= GObject
.Property(type=ObjectSubclass
)
753 PropertyObjectSubclass(obj
=ObjectSubclass())
755 def test_property_subclass(self
):
757 class A(GObject
.GObject
):
758 prop1
= GObject
.Property(type=int)
761 prop2
= GObject
.Property(type=int)
765 self
.assertEqual(b
.prop2
, 10)
767 self
.assertEqual(b
.prop1
, 20)
769 @unittest.skipUnless(has_regress
, 'Missing regress typelib')
770 def test_property_subclass_c(self
):
771 class A(Regress
.TestSubObj
):
772 prop1
= GObject
.Property(type=int)
776 self
.assertEqual(a
.prop1
, 10)
778 # also has parent properties
780 self
.assertEqual(a
.props
.int, 20)
782 # Some of which are unusable without introspection
783 a
.props
.list = ("str1", "str2")
784 self
.assertEqual(a
.props
.list, ["str1", "str2"])
786 a
.set_property("list", ("str3", "str4"))
787 self
.assertEqual(a
.props
.list, ["str3", "str4"])
789 def test_property_subclass_custom_setter(self
):
791 class A(GObject
.GObject
):
794 first
= GObject
.Property(type=str, getter
=get_first
)
797 def get_second(self
):
799 second
= GObject
.Property(type=str, getter
=get_second
)
802 self
.assertEqual(a
.first
, 'first')
803 self
.assertRaises(TypeError, setattr, a
, 'first', 'foo')
806 self
.assertEqual(b
.first
, 'first')
807 self
.assertRaises(TypeError, setattr, b
, 'first', 'foo')
808 self
.assertEqual(b
.second
, 'second')
809 self
.assertRaises(TypeError, setattr, b
, 'second', 'foo')
811 def test_property_subclass_custom_setter_error(self
):
813 class A(GObject
.GObject
):
816 first
= GObject
.Property(type=str, getter
=get_first
)
818 def do_get_property(self
, pspec
):
827 def test_float_min(self
):
828 GObject
.Property(type=float, minimum
=-1)
829 GObject
.Property(type=GObject
.TYPE_FLOAT
, minimum
=-1)
830 GObject
.Property(type=GObject
.TYPE_DOUBLE
, minimum
=-1)
834 def test_reference_count(self
):
835 # We can check directly if an object gets finalized, so we will
836 # observe it indirectly through the refcount of a member object.
838 # We create our dummy object and store its current refcount
840 rc
= sys
.getrefcount(o
)
842 # We add our object as a member to our newly created object we
843 # want to observe. Its refcount is increased by one.
844 t
= PropertyObject(normal
="test")
846 self
.assertEqual(sys
.getrefcount(o
), rc
+ 1)
848 # Now we want to ensure we do not leak any references to our
849 # object with properties. If no ref is leaked, then when deleting
850 # the local reference to this object, its reference count shoud
851 # drop to zero, and our dummy object should loose one reference.
853 self
.assertEqual(sys
.getrefcount(o
), rc
)
855 def test_doc_strings(self
):
856 class C(GObject
.GObject
):
857 foo_blurbed
= GObject
.Property(type=int, blurb
='foo_blurbed doc string')
860 def foo_getter(self
):
861 """foo_getter doc string"""
864 self
.assertEqual(C
.foo_blurbed
.blurb
, 'foo_blurbed doc string')
865 self
.assertEqual(C
.foo_blurbed
.__doc
__, 'foo_blurbed doc string')
867 self
.assertEqual(C
.foo_getter
.blurb
, 'foo_getter doc string')
868 self
.assertEqual(C
.foo_getter
.__doc
__, 'foo_getter doc string')
870 def test_python_to_glib_type_mapping(self
):
871 tester
= GObject
.Property()
872 self
.assertEqual(tester
._type
_from
_python
(int), GObject
.TYPE_INT
)
873 if sys
.version_info
< (3, 0):
874 self
.assertEqual(tester
._type
_from
_python
(long), GObject
.TYPE_LONG
)
875 self
.assertEqual(tester
._type
_from
_python
(bool), GObject
.TYPE_BOOLEAN
)
876 self
.assertEqual(tester
._type
_from
_python
(float), GObject
.TYPE_DOUBLE
)
877 self
.assertEqual(tester
._type
_from
_python
(str), GObject
.TYPE_STRING
)
878 self
.assertEqual(tester
._type
_from
_python
(object), GObject
.TYPE_PYOBJECT
)
880 self
.assertEqual(tester
._type
_from
_python
(GObject
.GObject
), GObject
.GObject
.__gtype
__)
881 self
.assertEqual(tester
._type
_from
_python
(GObject
.GEnum
), GObject
.GEnum
.__gtype
__)
882 self
.assertEqual(tester
._type
_from
_python
(GObject
.GFlags
), GObject
.GFlags
.__gtype
__)
883 self
.assertEqual(tester
._type
_from
_python
(GObject
.GBoxed
), GObject
.GBoxed
.__gtype
__)
884 self
.assertEqual(tester
._type
_from
_python
(GObject
.GInterface
), GObject
.GInterface
.__gtype
__)
886 for type_
in [TYPE_NONE
, TYPE_INTERFACE
, TYPE_CHAR
, TYPE_UCHAR
,
887 TYPE_INT
, TYPE_UINT
, TYPE_BOOLEAN
, TYPE_LONG
,
888 TYPE_ULONG
, TYPE_INT64
, TYPE_UINT64
,
889 TYPE_FLOAT
, TYPE_DOUBLE
, TYPE_POINTER
,
890 TYPE_BOXED
, TYPE_PARAM
, TYPE_OBJECT
, TYPE_STRING
,
891 TYPE_PYOBJECT
, TYPE_GTYPE
, TYPE_STRV
]:
892 self
.assertEqual(tester
._type
_from
_python
(type_
), type_
)
894 self
.assertRaises(TypeError, tester
._type
_from
_python
, types
.CodeType
)
897 class TestInstallProperties(unittest
.TestCase
):
898 # These tests only test how signalhelper.install_signals works
899 # with the __gsignals__ dict and therefore does not need to use
900 # GObject as a base class because that would automatically call
901 # install_signals within the meta-class.
903 __gproperties__
= {'test': (0, '', '', 0, 0, 0, 0)}
909 @GObject.Property(type=int)
913 class ClassWithPropertyAndGetterVFunc(object):
914 @GObject.Property(type=int)
918 def do_get_property(self
, name
):
921 class ClassWithPropertyRedefined(object):
922 __gproperties__
= {'test': (0, '', '', 0, 0, 0, 0)}
923 test
= GObject
.Property(type=int)
926 self
.assertEqual(len(self
.Base
.__gproperties
__), 1)
927 propertyhelper
.install_properties(self
.Base
)
928 self
.assertEqual(len(self
.Base
.__gproperties
__), 1)
930 def test_subclass_without_properties_is_not_modified(self
):
931 self
.assertFalse('__gproperties__' in self
.Sub1
.__dict__
)
932 propertyhelper
.install_properties(self
.Sub1
)
933 self
.assertFalse('__gproperties__' in self
.Sub1
.__dict__
)
935 def test_subclass_with_decorator_gets_gproperties_dict(self
):
936 # Sub2 has Property instances but will not have a __gproperties__
937 # until install_properties is called
938 self
.assertFalse('__gproperties__' in self
.Sub2
.__dict__
)
939 self
.assertFalse('do_get_property' in self
.Sub2
.__dict__
)
940 self
.assertFalse('do_set_property' in self
.Sub2
.__dict__
)
942 propertyhelper
.install_properties(self
.Sub2
)
943 self
.assertTrue('__gproperties__' in self
.Sub2
.__dict__
)
944 self
.assertEqual(len(self
.Base
.__gproperties
__), 1)
945 self
.assertEqual(len(self
.Sub2
.__gproperties__
), 1)
946 self
.assertTrue('sub2test' in self
.Sub2
.__gproperties__
)
948 # get/set vfuncs should have been added
949 self
.assertTrue('do_get_property' in self
.Sub2
.__dict__
)
950 self
.assertTrue('do_set_property' in self
.Sub2
.__dict__
)
952 def test_object_with_property_and_do_get_property_vfunc_raises(self
):
953 self
.assertRaises(TypeError, propertyhelper
.install_properties
,
954 self
.ClassWithPropertyAndGetterVFunc
)
956 def test_same_name_property_definitions_raises(self
):
957 self
.assertRaises(ValueError, propertyhelper
.install_properties
,
958 self
.ClassWithPropertyRedefined
)
961 class CPropertiesTestBase(object):
962 # Tests for properties implemented in C not Python.
965 self
.obj
= GIMarshallingTests
.PropertiesObject()
967 def get_prop(self
, obj
, name
):
968 raise NotImplementedError
970 def set_prop(self
, obj
, name
, value
):
971 raise NotImplementedError
973 def test_boolean(self
):
974 self
.assertEqual(self
.get_prop(self
.obj
, 'some-boolean'), False)
975 self
.set_prop(self
.obj
, 'some-boolean', True)
976 self
.assertEqual(self
.get_prop(self
.obj
, 'some-boolean'), True)
978 obj
= GIMarshallingTests
.PropertiesObject(some_boolean
=True)
979 self
.assertEqual(self
.get_prop(obj
, 'some-boolean'), True)
982 self
.assertEqual(self
.get_prop(self
.obj
, 'some-char'), 0)
983 self
.set_prop(self
.obj
, 'some-char', GLib
.MAXINT8
)
984 self
.assertEqual(self
.get_prop(self
.obj
, 'some-char'), GLib
.MAXINT8
)
986 obj
= GIMarshallingTests
.PropertiesObject(some_char
=-42)
987 self
.assertEqual(self
.get_prop(obj
, 'some-char'), -42)
989 def test_uchar(self
):
990 self
.assertEqual(self
.get_prop(self
.obj
, 'some-uchar'), 0)
991 self
.set_prop(self
.obj
, 'some-uchar', GLib
.MAXUINT8
)
992 self
.assertEqual(self
.get_prop(self
.obj
, 'some-uchar'), GLib
.MAXUINT8
)
994 obj
= GIMarshallingTests
.PropertiesObject(some_uchar
=42)
995 self
.assertEqual(self
.get_prop(obj
, 'some-uchar'), 42)
998 self
.assertEqual(self
.get_prop(self
.obj
, 'some_int'), 0)
999 self
.set_prop(self
.obj
, 'some-int', GLib
.MAXINT
)
1000 self
.assertEqual(self
.get_prop(self
.obj
, 'some_int'), GLib
.MAXINT
)
1002 obj
= GIMarshallingTests
.PropertiesObject(some_int
=-42)
1003 self
.assertEqual(self
.get_prop(obj
, 'some-int'), -42)
1005 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-int', 'foo')
1006 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-int', None)
1008 self
.assertEqual(self
.get_prop(obj
, 'some-int'), -42)
1010 def test_uint(self
):
1011 self
.assertEqual(self
.get_prop(self
.obj
, 'some_uint'), 0)
1012 self
.set_prop(self
.obj
, 'some-uint', GLib
.MAXUINT
)
1013 self
.assertEqual(self
.get_prop(self
.obj
, 'some_uint'), GLib
.MAXUINT
)
1015 obj
= GIMarshallingTests
.PropertiesObject(some_uint
=42)
1016 self
.assertEqual(self
.get_prop(obj
, 'some-uint'), 42)
1018 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-uint', 'foo')
1019 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-uint', None)
1021 self
.assertEqual(self
.get_prop(obj
, 'some-uint'), 42)
1023 def test_long(self
):
1024 self
.assertEqual(self
.get_prop(self
.obj
, 'some_long'), 0)
1025 self
.set_prop(self
.obj
, 'some-long', GLib
.MAXLONG
)
1026 self
.assertEqual(self
.get_prop(self
.obj
, 'some_long'), GLib
.MAXLONG
)
1028 obj
= GIMarshallingTests
.PropertiesObject(some_long
=-42)
1029 self
.assertEqual(self
.get_prop(obj
, 'some-long'), -42)
1031 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-long', 'foo')
1032 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-long', None)
1034 self
.assertEqual(self
.get_prop(obj
, 'some-long'), -42)
1036 def test_ulong(self
):
1037 self
.assertEqual(self
.get_prop(self
.obj
, 'some_ulong'), 0)
1038 self
.set_prop(self
.obj
, 'some-ulong', GLib
.MAXULONG
)
1039 self
.assertEqual(self
.get_prop(self
.obj
, 'some_ulong'), GLib
.MAXULONG
)
1041 obj
= GIMarshallingTests
.PropertiesObject(some_ulong
=42)
1042 self
.assertEqual(self
.get_prop(obj
, 'some-ulong'), 42)
1044 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-ulong', 'foo')
1045 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-ulong', None)
1047 self
.assertEqual(self
.get_prop(obj
, 'some-ulong'), 42)
1049 def test_int64(self
):
1050 self
.assertEqual(self
.get_prop(self
.obj
, 'some-int64'), 0)
1051 self
.set_prop(self
.obj
, 'some-int64', GLib
.MAXINT64
)
1052 self
.assertEqual(self
.get_prop(self
.obj
, 'some-int64'), GLib
.MAXINT64
)
1054 obj
= GIMarshallingTests
.PropertiesObject(some_int64
=-4200000000000000)
1055 self
.assertEqual(self
.get_prop(obj
, 'some-int64'), -4200000000000000)
1057 def test_uint64(self
):
1058 self
.assertEqual(self
.get_prop(self
.obj
, 'some-uint64'), 0)
1059 self
.set_prop(self
.obj
, 'some-uint64', GLib
.MAXUINT64
)
1060 self
.assertEqual(self
.get_prop(self
.obj
, 'some-uint64'), GLib
.MAXUINT64
)
1062 obj
= GIMarshallingTests
.PropertiesObject(some_uint64
=4200000000000000)
1063 self
.assertEqual(self
.get_prop(obj
, 'some-uint64'), 4200000000000000)
1065 def test_float(self
):
1066 self
.assertEqual(self
.get_prop(self
.obj
, 'some-float'), 0)
1067 self
.set_prop(self
.obj
, 'some-float', GLib
.MAXFLOAT
)
1068 self
.assertEqual(self
.get_prop(self
.obj
, 'some-float'), GLib
.MAXFLOAT
)
1070 obj
= GIMarshallingTests
.PropertiesObject(some_float
=42.42)
1071 self
.assertAlmostEqual(self
.get_prop(obj
, 'some-float'), 42.42, 4)
1073 obj
= GIMarshallingTests
.PropertiesObject(some_float
=42)
1074 self
.assertAlmostEqual(self
.get_prop(obj
, 'some-float'), 42.0, 4)
1076 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-float', 'foo')
1077 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-float', None)
1079 self
.assertAlmostEqual(self
.get_prop(obj
, 'some-float'), 42.0, 4)
1081 def test_double(self
):
1082 self
.assertEqual(self
.get_prop(self
.obj
, 'some-double'), 0)
1083 self
.set_prop(self
.obj
, 'some-double', GLib
.MAXDOUBLE
)
1084 self
.assertEqual(self
.get_prop(self
.obj
, 'some-double'), GLib
.MAXDOUBLE
)
1086 obj
= GIMarshallingTests
.PropertiesObject(some_double
=42.42)
1087 self
.assertAlmostEqual(self
.get_prop(obj
, 'some-double'), 42.42)
1089 obj
= GIMarshallingTests
.PropertiesObject(some_double
=42)
1090 self
.assertAlmostEqual(self
.get_prop(obj
, 'some-double'), 42.0)
1092 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-double', 'foo')
1093 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-double', None)
1095 self
.assertAlmostEqual(self
.get_prop(obj
, 'some-double'), 42.0)
1097 def test_strv(self
):
1098 self
.assertEqual(self
.get_prop(self
.obj
, 'some-strv'), [])
1099 self
.set_prop(self
.obj
, 'some-strv', ['hello', 'world'])
1100 self
.assertEqual(self
.get_prop(self
.obj
, 'some-strv'), ['hello', 'world'])
1102 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-strv', 1)
1103 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-strv', 'foo')
1104 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-strv', [1, 2])
1105 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-strv', ['foo', 1])
1107 self
.assertEqual(self
.get_prop(self
.obj
, 'some-strv'), ['hello', 'world'])
1109 obj
= GIMarshallingTests
.PropertiesObject(some_strv
=['hello', 'world'])
1110 self
.assertEqual(self
.get_prop(obj
, 'some-strv'), ['hello', 'world'])
1113 obj
= GIMarshallingTests
.PropertiesObject(some_strv
=[_unicode('foo')])
1114 self
.assertEqual(self
.get_prop(obj
, 'some-strv'), [_unicode('foo')])
1115 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-strv',
1116 [_unicode('foo'), 1])
1118 def test_boxed_struct(self
):
1119 self
.assertEqual(self
.get_prop(self
.obj
, 'some-boxed-struct'), None)
1122 __gtype__
= GObject
.TYPE_STRV
1124 struct1
= GIMarshallingTests
.BoxedStruct()
1127 self
.set_prop(self
.obj
, 'some-boxed-struct', struct1
)
1128 self
.assertEqual(self
.get_prop(self
.obj
, 'some-boxed-struct').long_
, 1)
1129 self
.assertEqual(self
.obj
.some_boxed_struct
.long_
, 1)
1131 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-boxed-struct', 1)
1132 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-boxed-struct', 'foo')
1134 obj
= GIMarshallingTests
.PropertiesObject(some_boxed_struct
=struct1
)
1135 self
.assertEqual(self
.get_prop(obj
, 'some-boxed-struct').long_
, 1)
1137 def test_boxed_glist(self
):
1138 self
.assertEqual(self
.get_prop(self
.obj
, 'some-boxed-glist'), [])
1140 l
= [GLib
.MININT
, 42, GLib
.MAXINT
]
1141 self
.set_prop(self
.obj
, 'some-boxed-glist', l
)
1142 self
.assertEqual(self
.get_prop(self
.obj
, 'some-boxed-glist'), l
)
1143 self
.set_prop(self
.obj
, 'some-boxed-glist', [])
1144 self
.assertEqual(self
.get_prop(self
.obj
, 'some-boxed-glist'), [])
1146 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-boxed-glist', 1)
1147 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-boxed-glist', 'foo')
1148 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-boxed-glist', ['a'])
1150 @unittest.skipUnless(has_regress
, 'built without cairo support')
1151 def test_annotated_glist(self
):
1152 obj
= Regress
.TestObj()
1153 self
.assertEqual(self
.get_prop(obj
, 'list'), [])
1155 self
.set_prop(obj
, 'list', ['1', '2', '3'])
1156 self
.assertTrue(isinstance(self
.get_prop(obj
, 'list'), list))
1157 self
.assertEqual(self
.get_prop(obj
, 'list'), ['1', '2', '3'])
1159 @unittest.expectedFailure
1160 def test_boxed_glist_ctor(self
):
1161 l
= [GLib
.MININT
, 42, GLib
.MAXINT
]
1162 obj
= GIMarshallingTests
.PropertiesObject(some_boxed_glist
=l
)
1163 self
.assertEqual(self
.get_prop(obj
, 'some-boxed-glist'), l
)
1165 def test_variant(self
):
1166 self
.assertEqual(self
.get_prop(self
.obj
, 'some-variant'), None)
1168 self
.set_prop(self
.obj
, 'some-variant', GLib
.Variant('o', '/myobj'))
1169 self
.assertEqual(self
.get_prop(self
.obj
, 'some-variant').get_type_string(), 'o')
1170 self
.assertEqual(self
.get_prop(self
.obj
, 'some-variant').print_(False), "'/myobj'")
1172 self
.set_prop(self
.obj
, 'some-variant', None)
1173 self
.assertEqual(self
.get_prop(self
.obj
, 'some-variant'), None)
1175 obj
= GIMarshallingTests
.PropertiesObject(some_variant
=GLib
.Variant('b', True))
1176 self
.assertEqual(self
.get_prop(obj
, 'some-variant').get_type_string(), 'b')
1177 self
.assertEqual(self
.get_prop(obj
, 'some-variant').get_boolean(), True)
1179 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-variant', 'foo')
1180 self
.assertRaises(TypeError, self
.set_prop
, self
.obj
, 'some-variant', 23)
1182 self
.assertEqual(self
.get_prop(obj
, 'some-variant').get_type_string(), 'b')
1183 self
.assertEqual(self
.get_prop(obj
, 'some-variant').get_boolean(), True)
1185 def test_setting_several_properties(self
):
1186 obj
= GIMarshallingTests
.PropertiesObject()
1187 obj
.set_properties(some_uchar
=54, some_int
=42)
1188 self
.assertEqual(42, self
.get_prop(obj
, 'some-int'))
1189 self
.assertEqual(54, self
.get_prop(obj
, 'some-uchar'))
1191 @unittest.skipUnless(has_regress
, 'built without cairo support')
1192 def test_gtype(self
):
1193 obj
= Regress
.TestObj()
1194 self
.assertEqual(self
.get_prop(obj
, 'gtype'), GObject
.TYPE_INVALID
)
1195 self
.set_prop(obj
, 'gtype', int)
1196 self
.assertEqual(self
.get_prop(obj
, 'gtype'), GObject
.TYPE_INT
)
1198 obj
= Regress
.TestObj(gtype
=int)
1199 self
.assertEqual(self
.get_prop(obj
, 'gtype'), GObject
.TYPE_INT
)
1200 self
.set_prop(obj
, 'gtype', str)
1201 self
.assertEqual(self
.get_prop(obj
, 'gtype'), GObject
.TYPE_STRING
)
1203 @unittest.skipUnless(has_regress
, 'built without cairo support')
1204 def test_hash_table(self
):
1205 obj
= Regress
.TestObj()
1206 self
.assertEqual(self
.get_prop(obj
, 'hash-table'), None)
1208 self
.set_prop(obj
, 'hash-table', {'mec': 56})
1209 self
.assertTrue(isinstance(self
.get_prop(obj
, 'hash-table'), dict))
1210 self
.assertEqual(list(self
.get_prop(obj
, 'hash-table').items())[0],
1213 @unittest.skipUnless(has_regress
, 'built without cairo support')
1214 def test_parent_class(self
):
1215 class A(Regress
.TestObj
):
1216 prop1
= GObject
.Property(type=int)
1219 self
.set_prop(a
, 'int', 20)
1220 self
.assertEqual(self
.get_prop(a
, 'int'), 20)
1222 # test parent property which needs introspection
1223 self
.set_prop(a
, 'list', ("str1", "str2"))
1224 self
.assertEqual(self
.get_prop(a
, 'list'), ["str1", "str2"])
1226 def test_held_object_ref_count_getter(self
):
1227 holder
= GIMarshallingTests
.PropertiesObject()
1228 held
= GObject
.Object()
1230 self
.assertEqual(holder
.__grefcount
__, 1)
1231 self
.assertEqual(held
.__grefcount
__, 1)
1233 self
.set_prop(holder
, 'some-object', held
)
1234 self
.assertEqual(holder
.__grefcount
__, 1)
1236 initial_ref_count
= held
.__grefcount
__
1237 self
.get_prop(holder
, 'some-object')
1239 self
.assertEqual(held
.__grefcount
__, initial_ref_count
)
1241 def test_held_object_ref_count_setter(self
):
1242 holder
= GIMarshallingTests
.PropertiesObject()
1243 held
= GObject
.Object()
1245 self
.assertEqual(holder
.__grefcount
__, 1)
1246 self
.assertEqual(held
.__grefcount
__, 1)
1248 # Setting property should only increase ref count by 1
1249 self
.set_prop(holder
, 'some-object', held
)
1250 self
.assertEqual(holder
.__grefcount
__, 1)
1251 self
.assertEqual(held
.__grefcount
__, 2)
1253 # Clearing should pull it back down
1254 self
.set_prop(holder
, 'some-object', None)
1255 self
.assertEqual(held
.__grefcount
__, 1)
1257 def test_set_object_property_to_invalid_type(self
):
1258 obj
= GIMarshallingTests
.PropertiesObject()
1259 self
.assertRaises(TypeError, self
.set_prop
, obj
, 'some-object', 'not_an_object')
1262 class TestCPropsAccessor(CPropertiesTestBase
, unittest
.TestCase
):
1263 # C property tests using the "props" accessor.
1264 def get_prop(self
, obj
, name
):
1265 return getattr(obj
.props
, name
.replace('-', '_'))
1267 def set_prop(self
, obj
, name
, value
):
1268 setattr(obj
.props
, name
.replace('-', '_'), value
)
1270 def test_props_accessor_dir(self
):
1272 props
= dir(GIMarshallingTests
.PropertiesObject
.props
)
1273 self
.assertTrue('some_float' in props
)
1274 self
.assertTrue('some_double' in props
)
1275 self
.assertTrue('some_variant' in props
)
1278 obj
= GIMarshallingTests
.PropertiesObject()
1279 props
= dir(obj
.props
)
1280 self
.assertTrue('some_float' in props
)
1281 self
.assertTrue('some_double' in props
)
1282 self
.assertTrue('some_variant' in props
)
1284 def test_param_spec_dir(self
):
1285 attrs
= dir(GIMarshallingTests
.PropertiesObject
.props
.some_float
)
1286 self
.assertTrue('name' in attrs
)
1287 self
.assertTrue('nick' in attrs
)
1288 self
.assertTrue('blurb' in attrs
)
1289 self
.assertTrue('flags' in attrs
)
1290 self
.assertTrue('default_value' in attrs
)
1291 self
.assertTrue('minimum' in attrs
)
1292 self
.assertTrue('maximum' in attrs
)
1295 class TestCGetPropertyMethod(CPropertiesTestBase
, unittest
.TestCase
):
1296 # C property tests using the "props" accessor.
1297 def get_prop(self
, obj
, name
):
1298 return obj
.get_property(name
)
1300 def set_prop(self
, obj
, name
, value
):
1301 obj
.set_property(name
, value
)
1304 if __name__
== '__main__':