summaryrefslogtreecommitdiff
path: root/tests/test_pygtkcompat.py
blob: c01892a1e3db087fc269b4b8e3f979251b230301 (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
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab

import unittest
import base64

import pytest
import gi
import pygtkcompat
from pygtkcompat.pygtkcompat import _disable_all as disable_all

from .helper import capture_gi_deprecation_warnings, capture_glib_warnings

try:
    from gi.repository import Gtk, Gdk
except ImportError:
    Gtk = None
else:
    if Gtk._version != "3.0":
        Gtk = None


class TestGlibCompat(unittest.TestCase):

    def setUp(self):
        pygtkcompat.enable()

    def tearDown(self):
        disable_all()

    def test_import(self):
        import glib
        import gio
        glib, gio


@unittest.skipUnless(Gtk, 'Gtk not available')
class TestMultipleEnable(unittest.TestCase):

    def tearDown(self):
        disable_all()

    def test_main(self):
        pygtkcompat.enable()
        pygtkcompat.enable()

    def test_gtk(self):
        pygtkcompat.enable_gtk("3.0")
        pygtkcompat.enable_gtk("3.0")
        import gtk

        # https://bugzilla.gnome.org/show_bug.cgi?id=759009
        w = gtk.Window()
        w.realize()
        self.assertEqual(len(w.window.get_origin()), 2)
        w.destroy()

    def test_gtk_no_4(self):
        self.assertRaises(ValueError, pygtkcompat.enable_gtk, version='4.0')

    def test_gtk_version_conflict(self):
        pygtkcompat.enable_gtk("3.0")
        self.assertRaises(ValueError, pygtkcompat.enable_gtk, version='2.0')


@unittest.skipUnless(Gtk, 'Gtk not available')
class TestATKCompat(unittest.TestCase):

    def setUp(self):
        pygtkcompat.enable_gtk("3.0")

    def tearDown(self):
        disable_all()

    def test_object(self):
        import atk
        self.assertTrue(hasattr(atk, 'Object'))


@unittest.skipUnless(Gtk, 'Gtk not available')
class TestPangoCompat(unittest.TestCase):

    def setUp(self):
        pygtkcompat.enable_gtk("3.0")

    def tearDown(self):
        disable_all()

    def test_layout(self):
        import pango
        self.assertTrue(hasattr(pango, 'Layout'))


@unittest.skipUnless(Gtk, 'Gtk not available')
class TestPangoCairoCompat(unittest.TestCase):

    def setUp(self):
        pygtkcompat.enable_gtk("3.0")

    def tearDown(self):
        disable_all()

    def test_error_underline_path(self):
        import pangocairo
        self.assertTrue(hasattr(pangocairo, 'error_underline_path'))


@unittest.skipUnless(Gtk, 'Gtk not available')
class TestGTKCompat(unittest.TestCase):

    def setUp(self):
        pygtkcompat.enable_gtk("3.0")

    def tearDown(self):
        disable_all()

    def test_window_get_frame_extents(self):
        import gtk
        import gtk.gdk
        w = gtk.Window()
        w.realize()
        rect = w.window.get_frame_extents()
        assert isinstance(rect, gtk.gdk.Rectangle)

    def test_window_get_geometry(self):
        import gtk
        w = gtk.Window()
        w.realize()
        with capture_gi_deprecation_warnings():
            geo = w.window.get_geometry()
        assert isinstance(geo, tuple)
        assert len(geo) == 5

    def test_action_set_tool_item_type(self):
        import gtk
        with pytest.warns(gi.PyGIDeprecationWarning):
            gtk.Action().set_tool_item_type(gtk.Action)

    def test_treeviewcolumn_pack(self):
        import gtk
        col = gtk.TreeViewColumn()
        col.pack_end(gtk.CellRendererText())
        col.pack_start(gtk.CellRendererText())

    def test_cell_layout_pack(self):
        import gtk
        layout = gtk.EntryCompletion()
        layout.pack_end(gtk.CellRendererText())
        layout.pack_start(gtk.CellRendererText())

    def test_cell_layout_cell_data_func(self):
        import gtk

        def func(*args):
            pass

        layout = gtk.EntryCompletion()
        render = gtk.CellRendererText()
        layout.set_cell_data_func(render, func)

    def test_combo_row_separator_func(self):
        import gtk

        def func(*args):
            pass

        combo = gtk.ComboBox()
        combo.set_row_separator_func(func)

    def test_container_install_child_property(self):
        import gtk

        box = gtk.Box()
        with pytest.warns(gi.PyGIDeprecationWarning):
            box.install_child_property(0, None)

    def test_combo_box_new_text(self):
        import gtk

        combo = gtk.combo_box_new_text()
        assert isinstance(combo, gtk.ComboBox)
        combo.append_text("foo")

    def test_scale(self):
        import gtk

        adjustment = gtk.Adjustment()
        assert gtk.HScale()
        assert gtk.HScale(adjustment).get_adjustment() == adjustment
        adjustment = gtk.Adjustment()
        assert gtk.VScale()
        assert gtk.VScale(adjustment).get_adjustment() == adjustment

    def test_stock_add(self):
        import gtk

        gtk.stock_add([])

    def test_text_view_scroll_to_mark(self):
        import gtk

        view = gtk.TextView()
        buf = view.get_buffer()
        mark = gtk.TextMark(name="foo")
        buf.add_mark(mark, buf.get_end_iter())
        view.scroll_to_mark(mark, 0.0)

    def test_window_set_geometry_hints(self):
        import gtk

        w = gtk.Window()
        w.set_geometry_hints(None, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        w.set_geometry_hints(None, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1)
        with pytest.raises(TypeError):
            w.set_geometry_hints(None, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

    def test_buttons(self):
        import gtk.gdk
        self.assertEqual(gtk.gdk._2BUTTON_PRESS, 5)
        self.assertEqual(gtk.gdk.BUTTON_PRESS, 4)

    def test_enums(self):
        import gtk
        self.assertEqual(gtk.WINDOW_TOPLEVEL, Gtk.WindowType.TOPLEVEL)
        self.assertEqual(gtk.PACK_START, Gtk.PackType.START)

    def test_flags(self):
        import gtk
        self.assertEqual(gtk.EXPAND, Gtk.AttachOptions.EXPAND)
        self.assertEqual(gtk.gdk.SHIFT_MASK, Gdk.ModifierType.SHIFT_MASK)

    def test_keysyms(self):
        import gtk.keysyms
        self.assertEqual(gtk.keysyms.Escape, Gdk.KEY_Escape)
        self.assertTrue(gtk.keysyms._0, Gdk.KEY_0)

    def test_style(self):
        import gtk
        widget = gtk.Button()
        with capture_gi_deprecation_warnings():
            widget.get_style_context().set_state(gtk.STATE_NORMAL)
            self.assertTrue(isinstance(widget.style.base[gtk.STATE_NORMAL],
                                       gtk.gdk.Color))

    def test_alignment(self):
        import gtk
        # Creation of pygtk.Alignment causes hard warnings, ignore this in testing.
        with capture_glib_warnings(allow_warnings=True):
            a = gtk.Alignment()

        self.assertEqual(a.props.xalign, 0.0)
        self.assertEqual(a.props.yalign, 0.0)
        self.assertEqual(a.props.xscale, 0.0)
        self.assertEqual(a.props.yscale, 0.0)

    def test_box(self):
        import gtk
        box = gtk.Box()
        child = gtk.Button()

        box.pack_start(child)
        expand, fill, padding, pack_type = box.query_child_packing(child)
        self.assertTrue(expand)
        self.assertTrue(fill)
        self.assertEqual(padding, 0)
        self.assertEqual(pack_type, gtk.PACK_START)

        child = gtk.Button()
        box.pack_end(child)
        expand, fill, padding, pack_type = box.query_child_packing(child)
        self.assertTrue(expand)
        self.assertTrue(fill)
        self.assertEqual(padding, 0)
        self.assertEqual(pack_type, gtk.PACK_END)

    def test_combobox_entry(self):
        import gtk
        liststore = gtk.ListStore(int, str)
        liststore.append((1, 'One'))
        liststore.append((2, 'Two'))
        liststore.append((3, 'Three'))
        # might cause a Pango warning, do not break on this
        with capture_glib_warnings(allow_warnings=True):
            combo = gtk.ComboBoxEntry(model=liststore)
        combo.set_text_column(1)
        combo.set_active(0)
        self.assertEqual(combo.get_text_column(), 1)
        self.assertEqual(combo.get_child().get_text(), 'One')
        combo = gtk.combo_box_entry_new()
        combo.set_model(liststore)
        combo.set_text_column(1)
        combo.set_active(0)
        self.assertEqual(combo.get_text_column(), 1)
        self.assertEqual(combo.get_child().get_text(), 'One')
        combo = gtk.combo_box_entry_new_with_model(liststore)
        combo.set_text_column(1)
        combo.set_active(0)
        self.assertEqual(combo.get_text_column(), 1)
        self.assertEqual(combo.get_child().get_text(), 'One')

    def test_size_request(self):
        import gtk
        box = gtk.Box()
        with capture_gi_deprecation_warnings():
            self.assertEqual(box.size_request(), [0, 0])

    def test_pixbuf(self):
        import gtk.gdk
        gtk.gdk.Pixbuf()

    def test_pixbuf_loader(self):
        import gtk.gdk
        # load a 1x1 pixel PNG from memory
        data = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP4n8Dw'
                                'HwAGIAJf85Z3XgAAAABJRU5ErkJggg==')
        loader = gtk.gdk.PixbufLoader('png')
        loader.write(data)
        loader.close()

        pixbuf = loader.get_pixbuf()
        self.assertEqual(pixbuf.get_width(), 1)
        self.assertEqual(pixbuf.get_height(), 1)

    def test_pixbuf_formats(self):
        import gtk.gdk
        formats = gtk.gdk.pixbuf_get_formats()
        self.assertEqual(type(formats[0]), dict)
        self.assertTrue('name' in formats[0])
        self.assertTrue('description' in formats[0])
        self.assertTrue('mime_types' in formats[0])
        self.assertEqual(type(formats[0]['extensions']), list)

    def test_gdk_window(self):
        import gtk
        w = gtk.Window()
        w.realize()
        origin = w.get_window().get_origin()
        self.assertTrue(isinstance(origin, tuple))
        self.assertEqual(len(origin), 2)