summaryrefslogtreecommitdiff
path: root/tests/test_gtk_template.py
blob: e55f2983131ce9e3ec2f8d28999464adbe72ea21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# coding: UTF-8

from __future__ import absolute_import

import tempfile
import os
import pytest

Gtk = pytest.importorskip("gi.repository.Gtk")
GLib = pytest.importorskip("gi.repository.GLib")
GObject = pytest.importorskip("gi.repository.GObject")
Gio = pytest.importorskip("gi.repository.Gio")


from .helper import capture_exceptions


def new_gtype_name(_count=[0]):
    _count[0] += 1
    return "GtkTemplateTest%d" % _count[0]


def ensure_resource_registered():
    resource_path = "/org/gnome/pygobject/test/a.ui"

    def is_registered(path):
        try:
            Gio.resources_get_info(path, Gio.ResourceLookupFlags.NONE)
        except GLib.Error:
            return False
        return True

    if is_registered(resource_path):
        return resource_path

    gresource_data = (
        b'GVariant\x00\x00\x00\x00\x00\x00\x00\x00\x18\x00\x00\x00'
        b'\xc8\x00\x00\x00\x00\x00\x00(\x06\x00\x00\x00\x00\x00\x00\x00'
        b'\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00'
        b'\x06\x00\x00\x00KP\x90\x0b\x03\x00\x00\x00\xc8\x00\x00\x00'
        b'\x04\x00L\x00\xcc\x00\x00\x00\xd0\x00\x00\x00\xb0\xb7$0'
        b'\x00\x00\x00\x00\xd0\x00\x00\x00\x06\x00L\x00\xd8\x00\x00\x00'
        b'\xdc\x00\x00\x00f\xc30\xd1\x01\x00\x00\x00\xdc\x00\x00\x00'
        b'\n\x00L\x00\xe8\x00\x00\x00\xec\x00\x00\x00\xd4\xb5\x02\x00'
        b'\xff\xff\xff\xff\xec\x00\x00\x00\x01\x00L\x00\xf0\x00\x00\x00'
        b'\xf4\x00\x00\x005H}\xe3\x02\x00\x00\x00\xf4\x00\x00\x00'
        b'\x05\x00L\x00\xfc\x00\x00\x00\x00\x01\x00\x00\xa2^\xd6t'
        b'\x04\x00\x00\x00\x00\x01\x00\x00\x04\x00v\x00\x08\x01\x00\x00'
        b'\xa5\x01\x00\x00org/\x01\x00\x00\x00gnome/\x00\x00\x02\x00\x00\x00'
        b'pygobject/\x00\x00\x04\x00\x00\x00/\x00\x00\x00\x00\x00\x00\x00'
        b'test/\x00\x00\x00\x05\x00\x00\x00a.ui\x00\x00\x00\x00'
        b'\x8d\x00\x00\x00\x00\x00\x00\x00<interface>\n  <template class="G'
        b'tkTemplateTestResource" parent="GtkBox">\n  <property name="spaci'
        b'ng">42</property>\n  </template>\n</interface>\n\x00\x00(uuay)'
    )

    resource = Gio.Resource.new_from_data(GLib.Bytes.new(gresource_data))
    Gio.resources_register(resource)
    assert is_registered(resource_path)
    return resource_path


def test_allow_init_template_call():

    type_name = new_gtype_name()

    xml = """\
<interface>
  <template class="{0}" parent="GtkBox">
  </template>
</interface>
""".format(type_name)

    @Gtk.Template.from_string(xml)
    class Foo(Gtk.Box):
        __gtype_name__ = type_name

        def __init__(self):
            super(Foo, self).__init__()
            self.init_template()

    Foo()


def test_main_example():

    type_name = new_gtype_name()

    example_xml = """\
<interface>
  <template class="{0}" parent="GtkBox">
    <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
    <property name="spacing">4</property>
    <child>
      <object class="GtkButton" id="hello_button">
        <property name="label">Hello World</property>
        <signal name="clicked" handler="hello_button_clicked"
                object="{0}" swapped="no"/>
        <signal name="clicked" handler="hello_button_clicked_after"
                object="{0}" swapped="no" after="yes"/>
      </object>
    </child>
    <child>
      <object class="GtkButton" id="goodbye_button">
        <property name="label">Goodbye World</property>
        <signal name="clicked" handler="goodbye_button_clicked"/>
        <signal name="clicked" handler="goodbye_button_clicked_after"
                after="yes"/>
      </object>
    </child>
  </template>
</interface>
""".format(type_name)

    @Gtk.Template.from_string(example_xml)
    class Foo(Gtk.Box):
        __gtype_name__ = type_name

        def __init__(self):
            super(Foo, self).__init__()
            self.callback_hello = []
            self.callback_hello_after = []
            self.callback_goodbye = []
            self.callback_goodbye_after = []

        @Gtk.Template.Callback("hello_button_clicked")
        def _hello_button_clicked(self, *args):
            self.callback_hello.append(args)

        @Gtk.Template.Callback("hello_button_clicked_after")
        def _hello_after(self, *args):
            self.callback_hello_after.append(args)

        _hello_button = Gtk.Template.Child("hello_button")

        goodbye_button = Gtk.Template.Child()

        @Gtk.Template.Callback("goodbye_button_clicked")
        def _goodbye_button_clicked(self, *args):
            self.callback_goodbye.append(args)

        @Gtk.Template.Callback("goodbye_button_clicked_after")
        def _goodbye_after(self, *args):
            self.callback_goodbye_after.append(args)

    w = Foo()
    assert w.__gtype__.name == type_name
    assert w.props.orientation == Gtk.Orientation.HORIZONTAL
    assert w.props.spacing == 4
    assert isinstance(w._hello_button, Gtk.Button)
    assert w._hello_button.props.label == "Hello World"
    assert isinstance(w.goodbye_button, Gtk.Button)
    assert w.goodbye_button.props.label == "Goodbye World"

    assert w.callback_hello == []
    w._hello_button.clicked()
    assert w.callback_hello == [(w,)]
    assert w.callback_hello_after == [(w,)]

    assert w.callback_goodbye == []
    w.goodbye_button.clicked()
    assert w.callback_goodbye == [(w.goodbye_button,)]
    assert w.callback_goodbye_after == [(w.goodbye_button,)]


def test_duplicate_handler():

    type_name = new_gtype_name()

    xml = """\
<interface>
  <template class="{0}" parent="GtkBox">
    <child>
      <object class="GtkButton" id="hello_button">
        <signal name="clicked" handler="hello_button_clicked">
      </object>
    </child>
  </template>
</interface>
""".format(type_name)

    class Foo(Gtk.Box):
        __gtype_name__ = type_name

        @Gtk.Template.Callback("hello_button_clicked")
        def _hello_button_clicked(self, *args):
            pass

        @Gtk.Template.Callback()
        def hello_button_clicked(self, *args):
            pass

    with pytest.raises(RuntimeError, match=".*hello_button_clicked.*"):
        Gtk.Template.from_string(xml)(Foo)


def test_duplicate_child():
    type_name = new_gtype_name()

    xml = """\
<interface>
  <template class="{0}" parent="GtkBox">
    <child>
      <object class="GtkButton" id="hello_button" />
    </child>
  </template>
</interface>
""".format(type_name)

    class Foo(Gtk.Box):
        __gtype_name__ = type_name

        foo = Gtk.Template.Child("hello_button")
        hello_button = Gtk.Template.Child()

    with pytest.raises(RuntimeError, match=".*hello_button.*"):
        Gtk.Template.from_string(xml)(Foo)


def test_nonexist_handler():
    type_name = new_gtype_name()

    xml = """\
<interface>
  <template class="{0}" parent="GtkBox">
  </template>
</interface>
""".format(type_name)

    @Gtk.Template.from_string(xml)
    class Foo(Gtk.Box):
        __gtype_name__ = type_name

        @Gtk.Template.Callback("nonexit")
        def foo(self, *args):
            pass

    with capture_exceptions() as exc_info:
        Foo()
    assert "nonexit" in str(exc_info[0].value)
    assert exc_info[0].type is RuntimeError


def test_missing_handler_callback():
    type_name = new_gtype_name()

    xml = """\
<interface>
  <template class="{0}" parent="GtkBox">
    <child>
      <object class="GtkButton" id="hello_button">
        <signal name="clicked" handler="i_am_not_used_in_python" />
      </object>
    </child>
  </template>
</interface>
""".format(type_name)

    class Foo(Gtk.Box):
        __gtype_name__ = type_name

    Gtk.Template.from_string(xml)(Foo)()


def test_handler_swapped_not_supported():

    type_name = new_gtype_name()

    xml = """\
<interface>
  <template class="{0}" parent="GtkBox">
    <child>
      <object class="GtkButton" id="hello_button">
        <signal name="clicked" handler="hello_button_clicked"
                object="{0}" swapped="yes" />
      </object>
    </child>
  </template>
</interface>
""".format(type_name)

    @Gtk.Template.from_string(xml)
    class Foo(Gtk.Box):
        __gtype_name__ = type_name

        hello_button = Gtk.Template.Child()

        @Gtk.Template.Callback("hello_button_clicked")
        def foo(self, *args):
            pass

    with capture_exceptions() as exc_info:
        Foo()
    assert "G_CONNECT_SWAPPED" in str(exc_info[0].value)


def test_handler_class_staticmethod():

    type_name = new_gtype_name()

    xml = """\
<interface>
  <template class="{0}" parent="GtkBox">
    <child>
      <object class="GtkButton" id="hello_button">
        <signal name="clicked" handler="clicked_class" />
        <signal name="clicked" handler="clicked_static" />
      </object>
    </child>
  </template>
</interface>
""".format(type_name)

    signal_args_class = []
    signal_args_static = []

    @Gtk.Template.from_string(xml)
    class Foo(Gtk.Box):
        __gtype_name__ = type_name

        hello_button = Gtk.Template.Child()

        @Gtk.Template.Callback("clicked_class")
        @classmethod
        def cb1(*args):
            signal_args_class.append(args)

        @Gtk.Template.Callback("clicked_static")
        @staticmethod
        def cb2(*args):
            signal_args_static.append(args)

    foo = Foo()
    foo.hello_button.clicked()
    assert signal_args_class == [(Foo, foo.hello_button)]
    assert signal_args_static == [(foo.hello_button,)]


def test_check_decorated_class():

    NonWidget = type("Foo", (object,), {})
    with pytest.raises(TypeError, match=".*on Widgets.*"):
        Gtk.Template.from_string("")(NonWidget)

    Widget = type("Foo", (Gtk.Widget,), {"__gtype_name__": new_gtype_name()})
    with pytest.raises(TypeError, match=".*Cannot nest.*"):
        Gtk.Template.from_string("")(Gtk.Template.from_string("")(Widget))

    Widget = type("Foo", (Gtk.Widget,), {})
    with pytest.raises(TypeError, match=".*__gtype_name__.*"):
        Gtk.Template.from_string("")(Widget)

    with pytest.raises(TypeError, match=".*on Widgets.*"):
        Gtk.Template.from_string("")(object())

    @Gtk.Template.from_string("")
    class Base(Gtk.Widget):
        __gtype_name__ = new_gtype_name()

    with capture_exceptions() as exc_info:
        type("Sub", (Base,), {})()
    assert "not allowed at this time" in str(exc_info[0].value)
    assert exc_info[0].type is TypeError


def test_from_file():
    fd, name = tempfile.mkstemp()
    try:
        os.close(fd)

        type_name = new_gtype_name()

        with open(name, "wb") as h:
            h.write(u"""\
    <interface>
      <template class="{0}" parent="GtkBox">
      <property name="spacing">42</property>
      </template>
    </interface>
    """.format(type_name).encode())

        @Gtk.Template.from_file(name)
        class Foo(Gtk.Box):
            __gtype_name__ = type_name

        foo = Foo()
        assert foo.props.spacing == 42
    finally:
        os.remove(name)


def test_property_override():
    type_name = new_gtype_name()

    xml = """\
    <interface>
      <template class="{0}" parent="GtkBox">
      <property name="spacing">42</property>
      </template>
    </interface>
""".format(type_name)

    @Gtk.Template.from_string(xml)
    class Foo(Gtk.Box):
        __gtype_name__ = type_name

    foo = Foo()
    assert foo.props.spacing == 42

    foo = Foo(spacing=124)
    assert foo.props.spacing == 124


def test_from_file_non_exist():
    dirname = tempfile.mkdtemp()
    try:
        path = os.path.join(dirname, "noexist")

        Widget = type(
            "Foo", (Gtk.Widget,), {"__gtype_name__": new_gtype_name()})
        with pytest.raises(GLib.Error, match=".*No such file.*"):
            Gtk.Template.from_file(path)(Widget)
    finally:
        os.rmdir(dirname)


def test_from_string_bytes():
    type_name = new_gtype_name()

    xml = u"""\
    <interface>
      <template class="{0}" parent="GtkBox">
      <property name="spacing">42</property>
      </template>
    </interface>
    """.format(type_name).encode()

    @Gtk.Template.from_string(xml)
    class Foo(Gtk.Box):
        __gtype_name__ = type_name

    foo = Foo()
    assert foo.props.spacing == 42


def test_from_resource():
    resource_path = ensure_resource_registered()

    @Gtk.Template.from_resource(resource_path)
    class Foo(Gtk.Box):
        __gtype_name__ = "GtkTemplateTestResource"

    foo = Foo()
    assert foo.props.spacing == 42


def test_from_resource_non_exit():
    Widget = type("Foo", (Gtk.Widget,), {"__gtype_name__": new_gtype_name()})
    with pytest.raises(GLib.Error, match=".*/or/gnome/pygobject/noexit.*"):
        Gtk.Template.from_resource("/or/gnome/pygobject/noexit")(Widget)


def test_constructors():
    with pytest.raises(TypeError):
        Gtk.Template()

    with pytest.raises(TypeError):
        Gtk.Template(foo=1)

    Gtk.Template(filename="foo")
    Gtk.Template(resource_path="foo")
    Gtk.Template(string="foo")

    with pytest.raises(TypeError):
        Gtk.Template(filename="foo", resource_path="bar")

    with pytest.raises(TypeError):
        Gtk.Template(filename="foo", nope="bar")

    Gtk.Template.from_string("bla")
    Gtk.Template.from_resource("foo")
    Gtk.Template.from_file("foo")