| 1 |
import gc |
| 2 |
import os |
| 3 |
import tempfile |
| 4 |
import weakref |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
Gtk = pytest.importorskip("gi.repository.Gtk") |
| 9 |
GLib = pytest.importorskip("gi.repository.GLib") |
| 10 |
GObject = pytest.importorskip("gi.repository.GObject") |
| 11 |
Gio = pytest.importorskip("gi.repository.Gio") |
| 12 |
|
| 13 |
from .helper import capture_exceptions |
| 14 |
|
| 15 |
GTK4 = Gtk._version == "4.0" |
| 16 |
|
| 17 |
|
| 18 |
def new_gtype_name(_count=[0]): |
| 19 |
_count[0] += 1 |
| 20 |
return f"GtkTemplateTest{_count[0]:d}" |
| 21 |
|
| 22 |
|
| 23 |
def ensure_resource_registered(): |
| 24 |
resource_path = "/org/gnome/pygobject/test/a.ui" |
| 25 |
|
| 26 |
def is_registered(path): |
| 27 |
try: |
| 28 |
Gio.resources_get_info(path, Gio.ResourceLookupFlags.NONE) |
| 29 |
except GLib.Error: |
| 30 |
return False |
| 31 |
return True |
| 32 |
|
| 33 |
if is_registered(resource_path): |
| 34 |
return resource_path |
| 35 |
|
| 36 |
gresource_data = ( |
| 37 |
b"GVariant\x00\x00\x00\x00\x00\x00\x00\x00\x18\x00\x00\x00" |
| 38 |
b"\xc8\x00\x00\x00\x00\x00\x00(\x06\x00\x00\x00\x00\x00\x00\x00" |
| 39 |
b"\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00" |
| 40 |
b"\x06\x00\x00\x00KP\x90\x0b\x03\x00\x00\x00\xc8\x00\x00\x00" |
| 41 |
b"\x04\x00L\x00\xcc\x00\x00\x00\xd0\x00\x00\x00\xb0\xb7$0" |
| 42 |
b"\x00\x00\x00\x00\xd0\x00\x00\x00\x06\x00L\x00\xd8\x00\x00\x00" |
| 43 |
b"\xdc\x00\x00\x00f\xc30\xd1\x01\x00\x00\x00\xdc\x00\x00\x00" |
| 44 |
b"\n\x00L\x00\xe8\x00\x00\x00\xec\x00\x00\x00\xd4\xb5\x02\x00" |
| 45 |
b"\xff\xff\xff\xff\xec\x00\x00\x00\x01\x00L\x00\xf0\x00\x00\x00" |
| 46 |
b"\xf4\x00\x00\x005H}\xe3\x02\x00\x00\x00\xf4\x00\x00\x00" |
| 47 |
b"\x05\x00L\x00\xfc\x00\x00\x00\x00\x01\x00\x00\xa2^\xd6t" |
| 48 |
b"\x04\x00\x00\x00\x00\x01\x00\x00\x04\x00v\x00\x08\x01\x00\x00" |
| 49 |
b"\xa5\x01\x00\x00org/\x01\x00\x00\x00gnome/\x00\x00\x02\x00\x00\x00" |
| 50 |
b"pygobject/\x00\x00\x04\x00\x00\x00/\x00\x00\x00\x00\x00\x00\x00" |
| 51 |
b"test/\x00\x00\x00\x05\x00\x00\x00a.ui\x00\x00\x00\x00" |
| 52 |
b'\x8d\x00\x00\x00\x00\x00\x00\x00<interface>\n <template class="G' |
| 53 |
b'tkTemplateTestResource" parent="GtkBox">\n <property name="spaci' |
| 54 |
b'ng">42</property>\n </template>\n</interface>\n\x00\x00(uuay)' |
| 55 |
) |
| 56 |
|
| 57 |
resource = Gio.Resource.new_from_data(GLib.Bytes.new(gresource_data)) |
| 58 |
Gio.resources_register(resource) |
| 59 |
assert is_registered(resource_path) |
| 60 |
return resource_path |
| 61 |
|
| 62 |
|
| 63 |
def test_allow_init_template_call(): |
| 64 |
type_name = new_gtype_name() |
| 65 |
|
| 66 |
xml = f"""\ |
| 67 |
<interface> |
| 68 |
<template class="{type_name}" parent="GtkBox"> |
| 69 |
</template> |
| 70 |
</interface> |
| 71 |
""" |
| 72 |
|
| 73 |
@Gtk.Template.from_string(xml) |
| 74 |
class Foo(Gtk.Box): |
| 75 |
__gtype_name__ = type_name |
| 76 |
|
| 77 |
def __init__(self): |
| 78 |
super().__init__() |
| 79 |
self.init_template() |
| 80 |
|
| 81 |
# Stop current pygobject from handling the initialisation |
| 82 |
del Foo.__dontuse_ginstance_init__ |
| 83 |
|
| 84 |
Foo() |
| 85 |
|
| 86 |
|
| 87 |
def test_init_template_second_instance(): |
| 88 |
type_name = new_gtype_name() |
| 89 |
|
| 90 |
xml = f"""\ |
| 91 |
<interface> |
| 92 |
<template class="{type_name}" parent="GtkBox"> |
| 93 |
<child> |
| 94 |
<object class="GtkLabel" id="label"> |
| 95 |
</object> |
| 96 |
</child> |
| 97 |
</template> |
| 98 |
</interface> |
| 99 |
""" |
| 100 |
|
| 101 |
@Gtk.Template.from_string(xml) |
| 102 |
class Foo(Gtk.Box): |
| 103 |
__gtype_name__ = type_name |
| 104 |
|
| 105 |
label = Gtk.Template.Child("label") |
| 106 |
|
| 107 |
def __init__(self): |
| 108 |
super().__init__() |
| 109 |
self.init_template() |
| 110 |
|
| 111 |
# Stop current pygobject from handling the initialisation |
| 112 |
del Foo.__dontuse_ginstance_init__ |
| 113 |
|
| 114 |
foo = Foo() |
| 115 |
assert isinstance(foo.label, Gtk.Label) |
| 116 |
|
| 117 |
foo2 = Foo() |
| 118 |
assert isinstance(foo2.label, Gtk.Label) |
| 119 |
|
| 120 |
|
| 121 |
def test_main_example(): |
| 122 |
type_name = new_gtype_name() |
| 123 |
|
| 124 |
example_xml = f"""\ |
| 125 |
<interface> |
| 126 |
<template class="{type_name}" parent="GtkBox"> |
| 127 |
<property name="orientation">GTK_ORIENTATION_HORIZONTAL</property> |
| 128 |
<property name="spacing">4</property> |
| 129 |
<child> |
| 130 |
<object class="GtkButton" id="hello_button"> |
| 131 |
<property name="label">Hello World</property> |
| 132 |
<signal name="clicked" handler="hello_button_clicked" |
| 133 |
object="{type_name}" swapped="no"/> |
| 134 |
<signal name="clicked" handler="hello_button_clicked_after" |
| 135 |
object="{type_name}" swapped="no" after="yes"/> |
| 136 |
</object> |
| 137 |
</child> |
| 138 |
<child> |
| 139 |
<object class="GtkButton" id="goodbye_button"> |
| 140 |
<property name="label">Goodbye World</property> |
| 141 |
<signal name="clicked" handler="goodbye_button_clicked"/> |
| 142 |
<signal name="clicked" handler="goodbye_button_clicked_after" |
| 143 |
after="yes"/> |
| 144 |
</object> |
| 145 |
</child> |
| 146 |
</template> |
| 147 |
</interface> |
| 148 |
""" |
| 149 |
|
| 150 |
@Gtk.Template.from_string(example_xml) |
| 151 |
class Foo(Gtk.Box): |
| 152 |
__gtype_name__ = type_name |
| 153 |
|
| 154 |
def __init__(self): |
| 155 |
super().__init__() |
| 156 |
self.callback_hello = [] |
| 157 |
self.callback_hello_after = [] |
| 158 |
self.callback_goodbye = [] |
| 159 |
self.callback_goodbye_after = [] |
| 160 |
|
| 161 |
@Gtk.Template.Callback("hello_button_clicked") |
| 162 |
def _hello_button_clicked(self, *args): |
| 163 |
self.callback_hello.append(args) |
| 164 |
|
| 165 |
@Gtk.Template.Callback("hello_button_clicked_after") |
| 166 |
def _hello_after(self, *args): |
| 167 |
self.callback_hello_after.append(args) |
| 168 |
|
| 169 |
_hello_button = Gtk.Template.Child("hello_button") |
| 170 |
|
| 171 |
goodbye_button = Gtk.Template.Child() |
| 172 |
|
| 173 |
@Gtk.Template.Callback("goodbye_button_clicked") |
| 174 |
def _goodbye_button_clicked(self, *args): |
| 175 |
self.callback_goodbye.append(args) |
| 176 |
|
| 177 |
@Gtk.Template.Callback("goodbye_button_clicked_after") |
| 178 |
def _goodbye_after(self, *args): |
| 179 |
self.callback_goodbye_after.append(args) |
| 180 |
|
| 181 |
w = Foo() |
| 182 |
assert w.__gtype__.name == type_name |
| 183 |
assert w.props.orientation == Gtk.Orientation.HORIZONTAL |
| 184 |
assert w.props.spacing == 4 |
| 185 |
assert isinstance(w._hello_button, Gtk.Button) |
| 186 |
assert w._hello_button.props.label == "Hello World" |
| 187 |
assert isinstance(w.goodbye_button, Gtk.Button) |
| 188 |
assert w.goodbye_button.props.label == "Goodbye World" |
| 189 |
|
| 190 |
assert w.callback_hello == [] |
| 191 |
w._hello_button.emit("clicked") |
| 192 |
assert w.callback_hello == [(w,)] |
| 193 |
assert w.callback_hello_after == [(w,)] |
| 194 |
|
| 195 |
assert w.callback_goodbye == [] |
| 196 |
w.goodbye_button.emit("clicked") |
| 197 |
assert w.callback_goodbye == [(w.goodbye_button,)] |
| 198 |
assert w.callback_goodbye_after == [(w.goodbye_button,)] |
| 199 |
|
| 200 |
|
| 201 |
def test_duplicate_handler(): |
| 202 |
type_name = new_gtype_name() |
| 203 |
|
| 204 |
xml = f"""\ |
| 205 |
<interface> |
| 206 |
<template class="{type_name}" parent="GtkBox"> |
| 207 |
<child> |
| 208 |
<object class="GtkButton" id="hello_button"> |
| 209 |
<signal name="clicked" handler="hello_button_clicked" /> |
| 210 |
</object> |
| 211 |
</child> |
| 212 |
</template> |
| 213 |
</interface> |
| 214 |
""" |
| 215 |
|
| 216 |
class Foo(Gtk.Box): |
| 217 |
__gtype_name__ = type_name |
| 218 |
|
| 219 |
@Gtk.Template.Callback("hello_button_clicked") |
| 220 |
def _hello_button_clicked(self, *args): |
| 221 |
pass |
| 222 |
|
| 223 |
@Gtk.Template.Callback() |
| 224 |
def hello_button_clicked(self, *args): |
| 225 |
pass |
| 226 |
|
| 227 |
with pytest.raises(RuntimeError, match=r".*hello_button_clicked.*"): |
| 228 |
Gtk.Template.from_string(xml)(Foo) |
| 229 |
|
| 230 |
|
| 231 |
def test_duplicate_child(): |
| 232 |
type_name = new_gtype_name() |
| 233 |
|
| 234 |
xml = f"""\ |
| 235 |
<interface> |
| 236 |
<template class="{type_name}" parent="GtkBox"> |
| 237 |
<child> |
| 238 |
<object class="GtkButton" id="hello_button" /> |
| 239 |
</child> |
| 240 |
</template> |
| 241 |
</interface> |
| 242 |
""" |
| 243 |
|
| 244 |
class Foo(Gtk.Box): |
| 245 |
__gtype_name__ = type_name |
| 246 |
|
| 247 |
foo = Gtk.Template.Child("hello_button") |
| 248 |
hello_button = Gtk.Template.Child() |
| 249 |
|
| 250 |
with pytest.raises(RuntimeError, match=r".*hello_button.*"): |
| 251 |
Gtk.Template.from_string(xml)(Foo) |
| 252 |
|
| 253 |
|
| 254 |
def test_nonexist_handler(): |
| 255 |
type_name = new_gtype_name() |
| 256 |
|
| 257 |
xml = f"""\ |
| 258 |
<interface> |
| 259 |
<template class="{type_name}" parent="GtkBox"> |
| 260 |
</template> |
| 261 |
</interface> |
| 262 |
""" |
| 263 |
|
| 264 |
@Gtk.Template.from_string(xml) |
| 265 |
class Foo(Gtk.Box): |
| 266 |
__gtype_name__ = type_name |
| 267 |
|
| 268 |
@Gtk.Template.Callback("nonexit") |
| 269 |
def foo(self, *args): |
| 270 |
pass |
| 271 |
|
| 272 |
with capture_exceptions() as exc_info: |
| 273 |
Foo() |
| 274 |
assert "nonexit" in str(exc_info[0].value) |
| 275 |
assert exc_info[0].type is RuntimeError |
| 276 |
|
| 277 |
|
| 278 |
def test_missing_handler_callback(): |
| 279 |
type_name = new_gtype_name() |
| 280 |
|
| 281 |
xml = f"""\ |
| 282 |
<interface> |
| 283 |
<template class="{type_name}" parent="GtkBox"> |
| 284 |
<child> |
| 285 |
<object class="GtkButton" id="hello_button"> |
| 286 |
<signal name="clicked" handler="i_am_not_used_in_python" /> |
| 287 |
</object> |
| 288 |
</child> |
| 289 |
</template> |
| 290 |
</interface> |
| 291 |
""" |
| 292 |
|
| 293 |
class Foo(Gtk.Box): |
| 294 |
__gtype_name__ = type_name |
| 295 |
|
| 296 |
Gtk.Template.from_string(xml)(Foo)() |
| 297 |
|
| 298 |
|
| 299 |
def test_handler_swapped_not_supported(): |
| 300 |
type_name = new_gtype_name() |
| 301 |
|
| 302 |
xml = f"""\ |
| 303 |
<interface> |
| 304 |
<template class="{type_name}" parent="GtkBox"> |
| 305 |
<child> |
| 306 |
<object class="GtkButton" id="hello_button"> |
| 307 |
<signal name="clicked" handler="hello_button_clicked" |
| 308 |
object="{type_name}" swapped="yes" /> |
| 309 |
</object> |
| 310 |
</child> |
| 311 |
</template> |
| 312 |
</interface> |
| 313 |
""" |
| 314 |
|
| 315 |
@Gtk.Template.from_string(xml) |
| 316 |
class Foo(Gtk.Box): |
| 317 |
__gtype_name__ = type_name |
| 318 |
|
| 319 |
hello_button = Gtk.Template.Child() |
| 320 |
|
| 321 |
@Gtk.Template.Callback("hello_button_clicked") |
| 322 |
def foo(self, *args): |
| 323 |
pass |
| 324 |
|
| 325 |
with capture_exceptions() as exc_info: |
| 326 |
Foo() |
| 327 |
assert "ConnectFlags.SWAPPED" in str(exc_info[0].value) |
| 328 |
|
| 329 |
|
| 330 |
def test_handler_class_staticmethod(): |
| 331 |
type_name = new_gtype_name() |
| 332 |
|
| 333 |
xml = f"""\ |
| 334 |
<interface> |
| 335 |
<template class="{type_name}" parent="GtkBox"> |
| 336 |
<child> |
| 337 |
<object class="GtkButton" id="hello_button"> |
| 338 |
<signal name="clicked" handler="clicked_class" /> |
| 339 |
<signal name="clicked" handler="clicked_static" /> |
| 340 |
</object> |
| 341 |
</child> |
| 342 |
</template> |
| 343 |
</interface> |
| 344 |
""" |
| 345 |
|
| 346 |
signal_args_class = [] |
| 347 |
signal_args_static = [] |
| 348 |
|
| 349 |
@Gtk.Template.from_string(xml) |
| 350 |
class Foo(Gtk.Box): |
| 351 |
__gtype_name__ = type_name |
| 352 |
|
| 353 |
hello_button = Gtk.Template.Child() |
| 354 |
|
| 355 |
@Gtk.Template.Callback("clicked_class") |
| 356 |
@classmethod |
| 357 |
def cb1(*args): |
| 358 |
signal_args_class.append(args) |
| 359 |
|
| 360 |
@Gtk.Template.Callback("clicked_static") |
| 361 |
@staticmethod |
| 362 |
def cb2(*args): |
| 363 |
signal_args_static.append(args) |
| 364 |
|
| 365 |
foo = Foo() |
| 366 |
foo.hello_button.emit("clicked") |
| 367 |
assert signal_args_class == [(Foo, foo.hello_button)] |
| 368 |
assert signal_args_static == [(foo.hello_button,)] |
| 369 |
|
| 370 |
|
| 371 |
@pytest.mark.skipif(Gtk._version == "4.0", reason="errors out first with gtk4") |
| 372 |
def test_check_decorated_class(): |
| 373 |
NonWidget = type("Foo", (object,), {}) |
| 374 |
with pytest.raises(TypeError, match=r".*on Widgets.*"): |
| 375 |
Gtk.Template.from_string("")(NonWidget) |
| 376 |
|
| 377 |
Widget = type("Foo", (Gtk.Widget,), {"__gtype_name__": new_gtype_name()}) |
| 378 |
with pytest.raises(TypeError, match=r".*Cannot nest.*"): |
| 379 |
Gtk.Template.from_string("")(Gtk.Template.from_string("")(Widget)) |
| 380 |
|
| 381 |
Widget = type("Foo", (Gtk.Widget,), {}) |
| 382 |
with pytest.raises(TypeError, match=r".*__gtype_name__.*"): |
| 383 |
Gtk.Template.from_string("")(Widget) |
| 384 |
|
| 385 |
with pytest.raises(TypeError, match=r".*on Widgets.*"): |
| 386 |
Gtk.Template.from_string("")(object()) |
| 387 |
|
| 388 |
|
| 389 |
@pytest.mark.skipif(Gtk._version == "4.0", reason="errors out first with gtk4") |
| 390 |
def test_subclass_fail(): |
| 391 |
@Gtk.Template.from_string("") |
| 392 |
class Base(Gtk.Widget): |
| 393 |
__gtype_name__ = new_gtype_name() |
| 394 |
|
| 395 |
with capture_exceptions() as exc_info: |
| 396 |
type("Sub", (Base,), {})() |
| 397 |
assert "not allowed at this time" in str(exc_info[0].value) |
| 398 |
assert exc_info[0].type is TypeError |
| 399 |
|
| 400 |
|
| 401 |
def test_from_file(): |
| 402 |
fd, name = tempfile.mkstemp() |
| 403 |
try: |
| 404 |
os.close(fd) |
| 405 |
|
| 406 |
type_name = new_gtype_name() |
| 407 |
|
| 408 |
with open(name, "wb") as h: |
| 409 |
h.write( |
| 410 |
f"""\ |
| 411 |
<interface> |
| 412 |
<template class="{type_name}" parent="GtkBox"> |
| 413 |
<property name="spacing">42</property> |
| 414 |
</template> |
| 415 |
</interface> |
| 416 |
""".encode() |
| 417 |
) |
| 418 |
|
| 419 |
@Gtk.Template.from_file(name) |
| 420 |
class Foo(Gtk.Box): |
| 421 |
__gtype_name__ = type_name |
| 422 |
|
| 423 |
foo = Foo() |
| 424 |
assert foo.props.spacing == 42 |
| 425 |
finally: |
| 426 |
os.remove(name) |
| 427 |
|
| 428 |
|
| 429 |
def test_property_override(): |
| 430 |
type_name = new_gtype_name() |
| 431 |
|
| 432 |
xml = f"""\ |
| 433 |
<interface> |
| 434 |
<template class="{type_name}" parent="GtkBox"> |
| 435 |
<property name="spacing">42</property> |
| 436 |
</template> |
| 437 |
</interface> |
| 438 |
""" |
| 439 |
|
| 440 |
@Gtk.Template.from_string(xml) |
| 441 |
class Foo(Gtk.Box): |
| 442 |
__gtype_name__ = type_name |
| 443 |
|
| 444 |
foo = Foo() |
| 445 |
assert foo.props.spacing == 42 |
| 446 |
|
| 447 |
foo = Foo(spacing=124) |
| 448 |
assert foo.props.spacing == 124 |
| 449 |
|
| 450 |
|
| 451 |
def test_from_file_non_exist(): |
| 452 |
dirname = tempfile.mkdtemp() |
| 453 |
try: |
| 454 |
path = os.path.join(dirname, "noexist") |
| 455 |
|
| 456 |
Widget = type("Foo", (Gtk.Widget,), {"__gtype_name__": new_gtype_name()}) |
| 457 |
with pytest.raises(GLib.Error, match=r".*No such file.*"): |
| 458 |
Gtk.Template.from_file(path)(Widget) |
| 459 |
finally: |
| 460 |
os.rmdir(dirname) |
| 461 |
|
| 462 |
|
| 463 |
def test_from_string_bytes(): |
| 464 |
type_name = new_gtype_name() |
| 465 |
|
| 466 |
xml = f"""\ |
| 467 |
<interface> |
| 468 |
<template class="{type_name}" parent="GtkBox"> |
| 469 |
<property name="spacing">42</property> |
| 470 |
</template> |
| 471 |
</interface> |
| 472 |
""".encode() |
| 473 |
|
| 474 |
@Gtk.Template.from_string(xml) |
| 475 |
class Foo(Gtk.Box): |
| 476 |
__gtype_name__ = type_name |
| 477 |
|
| 478 |
foo = Foo() |
| 479 |
assert foo.props.spacing == 42 |
| 480 |
|
| 481 |
|
| 482 |
def test_from_resource(): |
| 483 |
resource_path = ensure_resource_registered() |
| 484 |
|
| 485 |
@Gtk.Template.from_resource(resource_path) |
| 486 |
class Foo(Gtk.Box): |
| 487 |
__gtype_name__ = "GtkTemplateTestResource" |
| 488 |
|
| 489 |
foo = Foo() |
| 490 |
assert foo.props.spacing == 42 |
| 491 |
|
| 492 |
|
| 493 |
def test_from_resource_non_exit(): |
| 494 |
Widget = type("Foo", (Gtk.Widget,), {"__gtype_name__": new_gtype_name()}) |
| 495 |
with pytest.raises(GLib.Error, match=r".*/or/gnome/pygobject/noexit.*"): |
| 496 |
Gtk.Template.from_resource("/or/gnome/pygobject/noexit")(Widget) |
| 497 |
|
| 498 |
|
| 499 |
def test_constructors(): |
| 500 |
with pytest.raises(TypeError): |
| 501 |
Gtk.Template() |
| 502 |
|
| 503 |
with pytest.raises(TypeError): |
| 504 |
Gtk.Template(foo=1) |
| 505 |
|
| 506 |
Gtk.Template(filename="foo") |
| 507 |
Gtk.Template(resource_path="foo") |
| 508 |
Gtk.Template(string="foo") |
| 509 |
|
| 510 |
with pytest.raises(TypeError): |
| 511 |
Gtk.Template(filename="foo", resource_path="bar") |
| 512 |
|
| 513 |
with pytest.raises(TypeError): |
| 514 |
Gtk.Template(filename="foo", nope="bar") |
| 515 |
|
| 516 |
Gtk.Template.from_string("bla") |
| 517 |
Gtk.Template.from_resource("foo") |
| 518 |
Gtk.Template.from_file("foo") |
| 519 |
|
| 520 |
|
| 521 |
def test_child_construct(): |
| 522 |
Gtk.Template.Child() |
| 523 |
Gtk.Template.Child("name") |
| 524 |
with pytest.raises(TypeError): |
| 525 |
Gtk.Template.Child("name", True) |
| 526 |
Gtk.Template.Child("name", internal=True) |
| 527 |
with pytest.raises(TypeError): |
| 528 |
Gtk.Template.Child("name", internal=True, something=False) |
| 529 |
|
| 530 |
|
| 531 |
def test_internal_child(): |
| 532 |
main_type_name = new_gtype_name() |
| 533 |
|
| 534 |
xml = f"""\ |
| 535 |
<interface> |
| 536 |
<template class="{main_type_name}" parent="GtkBox"> |
| 537 |
<child> |
| 538 |
<object class="GtkBox" id="somechild"> |
| 539 |
<property name="margin-top">42</property> |
| 540 |
</object> |
| 541 |
</child> |
| 542 |
</template> |
| 543 |
</interface> |
| 544 |
""" |
| 545 |
|
| 546 |
@Gtk.Template.from_string(xml) |
| 547 |
class MainThing(Gtk.Box): |
| 548 |
__gtype_name__ = main_type_name |
| 549 |
|
| 550 |
somechild = Gtk.Template.Child(internal=True) |
| 551 |
|
| 552 |
thing = MainThing() |
| 553 |
assert thing.somechild.props.margin_top == 42 |
| 554 |
|
| 555 |
other_type_name = new_gtype_name() |
| 556 |
|
| 557 |
xml = f"""\ |
| 558 |
<interface> |
| 559 |
<template class="{other_type_name}" parent="GtkBox"> |
| 560 |
<child> |
| 561 |
<object class="{main_type_name}"> |
| 562 |
<child internal-child="somechild"> |
| 563 |
<object class="GtkBox"> |
| 564 |
<property name="margin-top">24</property> |
| 565 |
<child> |
| 566 |
<object class="GtkLabel"> |
| 567 |
<property name="label">foo</property> |
| 568 |
</object> |
| 569 |
</child> |
| 570 |
</object> |
| 571 |
</child> |
| 572 |
</object> |
| 573 |
</child> |
| 574 |
</template> |
| 575 |
</interface> |
| 576 |
""" |
| 577 |
|
| 578 |
@Gtk.Template.from_string(xml) |
| 579 |
class OtherThing(Gtk.Box): |
| 580 |
__gtype_name__ = other_type_name |
| 581 |
|
| 582 |
other = OtherThing() |
| 583 |
child = other.get_children()[0] if not GTK4 else other.get_first_child() |
| 584 |
|
| 585 |
assert isinstance(child, MainThing) |
| 586 |
|
| 587 |
child = child.get_children()[0] if not GTK4 else child.get_first_child() |
| 588 |
|
| 589 |
assert isinstance(child, Gtk.Box) |
| 590 |
assert child.props.margin_top == 24 |
| 591 |
|
| 592 |
child = child.get_children()[0] if not GTK4 else child.get_first_child() |
| 593 |
|
| 594 |
assert isinstance(child, Gtk.Label) |
| 595 |
assert child.props.label == "foo" |
| 596 |
|
| 597 |
|
| 598 |
def test_template_hierarchy(): |
| 599 |
testlabel = """ |
| 600 |
<interface> |
| 601 |
<template class="TestLabel" parent="GtkLabel"> |
| 602 |
</template> |
| 603 |
</interface> |
| 604 |
""" |
| 605 |
|
| 606 |
@Gtk.Template(string=testlabel) |
| 607 |
class TestLabel(Gtk.Label): |
| 608 |
__gtype_name__ = "TestLabel" |
| 609 |
|
| 610 |
def __init__(self): |
| 611 |
super().__init__() |
| 612 |
self.props.label = "TestLabel" |
| 613 |
|
| 614 |
testbox = """ |
| 615 |
<interface> |
| 616 |
<template class="TestBox" parent="GtkBox"> |
| 617 |
<child> |
| 618 |
<object class="TestLabel" id="_testlabel"/> |
| 619 |
</child> |
| 620 |
</template> |
| 621 |
</interface> |
| 622 |
""" |
| 623 |
|
| 624 |
@Gtk.Template(string=testbox) |
| 625 |
class TestBox(Gtk.Box): |
| 626 |
__gtype_name__ = "TestBox" |
| 627 |
|
| 628 |
_testlabel = Gtk.Template.Child() |
| 629 |
|
| 630 |
def __init__(self): |
| 631 |
super().__init__() |
| 632 |
|
| 633 |
assert isinstance(self._testlabel, TestLabel) |
| 634 |
|
| 635 |
window = """ |
| 636 |
<interface> |
| 637 |
<template class="MyWindow" parent="GtkWindow"> |
| 638 |
<property name="title">"Hellow World"</property> |
| 639 |
<child> |
| 640 |
<object class="TestBox" id="_testbox"> |
| 641 |
<child> |
| 642 |
<object class="TestLabel" id="_testlabel"/> |
| 643 |
</child> |
| 644 |
</object> |
| 645 |
</child> |
| 646 |
</template> |
| 647 |
</interface> |
| 648 |
""" |
| 649 |
|
| 650 |
@Gtk.Template(string=window) |
| 651 |
class MyWindow(Gtk.Window): |
| 652 |
__gtype_name__ = "MyWindow" |
| 653 |
|
| 654 |
_testbox = Gtk.Template.Child() |
| 655 |
_testlabel = Gtk.Template.Child() |
| 656 |
|
| 657 |
def __init__(self): |
| 658 |
super().__init__() |
| 659 |
|
| 660 |
assert isinstance(self._testbox, TestBox) |
| 661 |
assert isinstance(self._testlabel, TestLabel) |
| 662 |
children = self._testbox.get_children() if not GTK4 else list(self._testbox) |
| 663 |
|
| 664 |
assert len(children) == 2 |
| 665 |
|
| 666 |
win = MyWindow() |
| 667 |
assert isinstance(win, MyWindow) |
| 668 |
|
| 669 |
|
| 670 |
def test_multiple_init_template_calls(): |
| 671 |
xml = """ |
| 672 |
<interface> |
| 673 |
<template class="MyBox" parent="GtkBox"> |
| 674 |
<child> |
| 675 |
<object class="GtkLabel" id="_label"/> |
| 676 |
</child> |
| 677 |
</template> |
| 678 |
</interface> |
| 679 |
""" |
| 680 |
|
| 681 |
@Gtk.Template(string=xml) |
| 682 |
class MyBox(Gtk.Box): |
| 683 |
__gtype_name__ = "MyBox" |
| 684 |
|
| 685 |
_label = Gtk.Template.Child() |
| 686 |
|
| 687 |
def __init__(self): |
| 688 |
super().__init__() |
| 689 |
self._label.props.label = "awesome label" |
| 690 |
|
| 691 |
my_box = MyBox() |
| 692 |
assert isinstance(my_box, MyBox) |
| 693 |
children = my_box.get_children() if not GTK4 else list(my_box) |
| 694 |
|
| 695 |
assert len(children) == 1 |
| 696 |
my_box.init_template() |
| 697 |
assert isinstance(my_box, MyBox) |
| 698 |
children = my_box.get_children() if not GTK4 else list(my_box) |
| 699 |
|
| 700 |
assert len(children) == 1 |
| 701 |
|
| 702 |
|
| 703 |
def test_python_class_hierarchy(): |
| 704 |
xml = """ |
| 705 |
<interface> |
| 706 |
<template class="ChildBox" parent="ParentBox"> |
| 707 |
<child> |
| 708 |
<object class="GtkLabel" id="_label"/> |
| 709 |
</child> |
| 710 |
</template> |
| 711 |
</interface> |
| 712 |
""" |
| 713 |
|
| 714 |
class ParentBox(Gtk.Box): |
| 715 |
__gtype_name__ = "ParentBox" |
| 716 |
|
| 717 |
def __init__(self): |
| 718 |
super().__init__() |
| 719 |
|
| 720 |
@Gtk.Template(string=xml) |
| 721 |
class ChildBox(ParentBox): |
| 722 |
__gtype_name__ = "ChildBox" |
| 723 |
|
| 724 |
_label = Gtk.Template.Child() |
| 725 |
|
| 726 |
def __init__(self): |
| 727 |
super().__init__() |
| 728 |
|
| 729 |
childBox = ChildBox() |
| 730 |
assert isinstance(childBox, ChildBox) |
| 731 |
children = childBox.get_children() if not GTK4 else list(childBox) |
| 732 |
|
| 733 |
assert len(children) == 1 |
| 734 |
|
| 735 |
|
| 736 |
@pytest.mark.skipif(not GTK4, reason="GTK 4 only") |
| 737 |
def test_finalization_of_custom_child_objects(): |
| 738 |
xml = """ |
| 739 |
<?xml version="1.0" encoding="UTF-8"?> |
| 740 |
<interface> |
| 741 |
<requires lib="gtk" version="4.0" /> |
| 742 |
<template class="CustomBox" parent="GtkBox"> |
| 743 |
<child> |
| 744 |
<object class="CustomLabel"> |
| 745 |
<property name="label">CustomLabel</property> |
| 746 |
</object> |
| 747 |
</child> |
| 748 |
</template> |
| 749 |
</interface> |
| 750 |
""" |
| 751 |
|
| 752 |
class CustomLabel(Gtk.Label): |
| 753 |
__gtype_name__ = "CustomLabel" |
| 754 |
|
| 755 |
def do_unroot(self) -> None: |
| 756 |
check_finalize(self) |
| 757 |
|
| 758 |
@Gtk.Template(string=xml) |
| 759 |
class CustomBox(Gtk.Box): |
| 760 |
__gtype_name__ = "CustomBox" |
| 761 |
|
| 762 |
def do_unroot(self) -> None: |
| 763 |
check_finalize(self) |
| 764 |
|
| 765 |
finalized = [] |
| 766 |
|
| 767 |
def check_finalize(obj) -> None: |
| 768 |
name = obj.__class__.__name__ |
| 769 |
obj.weak_ref(lambda: finalized.append(f"{name} -> GObject finalized")) |
| 770 |
weakref.finalize(obj, finalized.append, f"{name} -> PyObject finalized") |
| 771 |
|
| 772 |
custom_box = CustomBox() |
| 773 |
box = Gtk.Box() |
| 774 |
box.append(custom_box) |
| 775 |
window = Gtk.Window() |
| 776 |
window.set_child(box) |
| 777 |
window.present() |
| 778 |
box.remove(custom_box) |
| 779 |
|
| 780 |
del custom_box |
| 781 |
|
| 782 |
assert "CustomBox -> PyObject finalized" in finalized |
| 783 |
assert "CustomBox -> GObject finalized" in finalized |
| 784 |
assert "CustomLabel -> PyObject finalized" in finalized |
| 785 |
assert "CustomLabel -> GObject finalized" in finalized |
| 786 |
|
| 787 |
|
| 788 |
def test_signal_handler_with_object_does_not_leak_memory(): |
| 789 |
"""Regression test for a memory leak when using signal handlers in GTK templates. |
| 790 |
Note that this example is intentionally minimal, i.e. the handler doesn't even |
| 791 |
need to exist on the class. The only requirement is that the `object` is specified. |
| 792 |
""" |
| 793 |
type_name = new_gtype_name() |
| 794 |
xml = f"""\ |
| 795 |
<?xml version="1.0" encoding="UTF-8"?> |
| 796 |
<interface> |
| 797 |
<requires lib="gtk" version="4.0" /> |
| 798 |
<template class="{type_name}" parent="GtkWidget"> |
| 799 |
<signal name="show" handler="on_show" object="{type_name}" swapped="no" /> |
| 800 |
</template> |
| 801 |
</interface> |
| 802 |
""" |
| 803 |
|
| 804 |
@Gtk.Template(string=xml) |
| 805 |
class MyWidget(Gtk.Widget): |
| 806 |
__gtype_name__ = type_name |
| 807 |
|
| 808 |
weak = MyWidget().weak_ref() |
| 809 |
|
| 810 |
gc.collect() |
| 811 |
gc.collect() # One more for PyPy |
| 812 |
|
| 813 |
assert weak() is None |