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

import unittest
import pytest

import gi

try:
    gi.require_foreign('cairo')
    import cairo
    has_cairo = True
except ImportError:
    has_cairo = False

has_region = has_cairo and hasattr(cairo, "Region")

try:
    from gi.repository import Gtk, Gdk
    Gtk, Gdk  # pyflakes
except:
    Gtk = None
    Gdk = None

from gi.repository import GObject, Regress


@unittest.skipUnless(has_cairo, 'built without cairo support')
class Test(unittest.TestCase):

    def test_gvalue_converters(self):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        context = cairo.Context(surface)
        matrix = cairo.Matrix()
        objects = {
            'CairoContext': context,
            'CairoSurface': surface,
            'CairoFontFace': context.get_font_face(),
            'CairoScaledFont': context.get_scaled_font(),
            'CairoPattern': context.get_source(),
            'CairoMatrix': matrix,
        }
        for type_name, cairo_obj in objects.items():
            gtype = GObject.type_from_name(type_name)
            v = GObject.Value()
            assert v.init(gtype) is None
            assert v.get_value() is None
            v.set_value(None)
            assert v.get_value() is None
            v.set_value(cairo_obj)
            assert v.get_value() == cairo_obj

    def test_cairo_context(self):
        context = Regress.test_cairo_context_full_return()
        self.assertTrue(isinstance(context, cairo.Context))

        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        context = cairo.Context(surface)
        Regress.test_cairo_context_none_in(context)

    def test_cairo_context_full_in(self):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        context = cairo.Context(surface)
        Regress.test_cairo_context_full_in(context)

        with pytest.raises(TypeError):
            Regress.test_cairo_context_full_in(object())

    def test_cairo_context_none_return(self):
        context = Regress.test_cairo_context_none_return()
        self.assertTrue(isinstance(context, cairo.Context))

    def test_cairo_path_full_return(self):
        path = Regress.test_cairo_path_full_return()
        if hasattr(cairo, "Path"):  # pycairo 1.15.1+
            assert isinstance(path, cairo.Path)

    def test_cairo_path_none_in(self):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        context = cairo.Context(surface)
        path = context.copy_path()
        Regress.test_cairo_path_none_in(path)
        surface.finish()

        with pytest.raises(TypeError):
            Regress.test_cairo_path_none_in(object())

    def test_cairo_path_full_in_full_return(self):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        context = cairo.Context(surface)
        context.move_to(10, 10)
        context.curve_to(10, 10, 3, 4, 5, 6)
        path = context.copy_path()
        new_path = Regress.test_cairo_path_full_in_full_return(path)
        assert list(path) == list(new_path)
        surface.finish()

    def test_cairo_font_options_full_return(self):
        options = Regress.test_cairo_font_options_full_return()
        assert isinstance(options, cairo.FontOptions)

    def test_cairo_font_options_none_return(self):
        options = Regress.test_cairo_font_options_none_return()
        assert isinstance(options, cairo.FontOptions)

    def test_cairo_font_options_full_in(self):
        options = cairo.FontOptions()
        Regress.test_cairo_font_options_full_in(options)

        with pytest.raises(TypeError):
            Regress.test_cairo_font_options_full_in(object())

    def test_cairo_font_options_none_in(self):
        options = cairo.FontOptions()
        Regress.test_cairo_font_options_none_in(options)

    def test_cairo_pattern_full_in(self):
        pattern = cairo.SolidPattern(1, 1, 1, 1)
        Regress.test_cairo_pattern_full_in(pattern)

        with pytest.raises(TypeError):
            Regress.test_cairo_pattern_full_in(object())

    def test_cairo_pattern_none_in(self):
        pattern = cairo.SolidPattern(1, 1, 1, 1)
        Regress.test_cairo_pattern_none_in(pattern)

    def test_cairo_pattern_full_return(self):
        pattern = Regress.test_cairo_pattern_full_return()
        self.assertTrue(isinstance(pattern, cairo.Pattern))
        self.assertTrue(isinstance(pattern, cairo.SolidPattern))

    def test_cairo_pattern_none_return(self):
        pattern = Regress.test_cairo_pattern_none_return()
        self.assertTrue(isinstance(pattern, cairo.Pattern))
        self.assertTrue(isinstance(pattern, cairo.SolidPattern))

    def test_cairo_region_full_in(self):
        region = cairo.Region()
        Regress.test_cairo_region_full_in(region)

        with pytest.raises(TypeError):
            Regress.test_cairo_region_full_in(object())

    def test_cairo_matrix_none_in(self):
        matrix = cairo.Matrix()
        Regress.test_cairo_matrix_none_in(matrix)

        with pytest.raises(TypeError):
            Regress.test_cairo_matrix_none_in(object())

    def test_cairo_matrix_none_return(self):
        matrix = Regress.test_cairo_matrix_none_return()
        assert matrix == cairo.Matrix()

    def test_cairo_matrix_out_caller_allocates(self):
        matrix = Regress.test_cairo_matrix_out_caller_allocates()
        assert matrix == cairo.Matrix()

    def test_cairo_surface(self):
        surface = Regress.test_cairo_surface_none_return()
        self.assertTrue(isinstance(surface, cairo.ImageSurface))
        self.assertTrue(isinstance(surface, cairo.Surface))
        self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
        self.assertEqual(surface.get_width(), 10)
        self.assertEqual(surface.get_height(), 10)

        surface = Regress.test_cairo_surface_full_return()
        self.assertTrue(isinstance(surface, cairo.ImageSurface))
        self.assertTrue(isinstance(surface, cairo.Surface))
        self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
        self.assertEqual(surface.get_width(), 10)
        self.assertEqual(surface.get_height(), 10)

        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        Regress.test_cairo_surface_none_in(surface)

        surface = Regress.test_cairo_surface_full_out()
        self.assertTrue(isinstance(surface, cairo.ImageSurface))
        self.assertTrue(isinstance(surface, cairo.Surface))
        self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
        self.assertEqual(surface.get_width(), 10)
        self.assertEqual(surface.get_height(), 10)

    def test_cairo_surface_full_in(self):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        Regress.test_cairo_surface_full_in(surface)

        with pytest.raises(TypeError):
            Regress.test_cairo_surface_full_in(object())

    def test_require_foreign(self):
        self.assertEqual(gi.require_foreign('cairo'), None)
        self.assertEqual(gi.require_foreign('cairo', 'Context'), None)
        self.assertRaises(ImportError, gi.require_foreign, 'invalid_module')
        self.assertRaises(ImportError, gi.require_foreign, 'invalid_module', 'invalid_symbol')
        self.assertRaises(ImportError, gi.require_foreign, 'cairo', 'invalid_symbol')


@unittest.skipUnless(has_cairo, 'built without cairo support')
@unittest.skipUnless(has_region, 'built without cairo.Region support')
@unittest.skipUnless(Gdk, 'Gdk not available')
class TestRegion(unittest.TestCase):

    def test_region_to_py(self):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        context = cairo.Context(surface)
        context.paint()
        region = Gdk.cairo_region_create_from_surface(surface)
        r = region.get_extents()
        self.assertEqual((r.height, r.width), (10, 10))

    def test_region_from_py(self):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        context = cairo.Context(surface)
        region = cairo.Region(cairo.RectangleInt(0, 0, 42, 42))
        Gdk.cairo_region(context, region)
        self.assertTrue("42" in repr(list(context.copy_path())))


@unittest.skipUnless(has_cairo, 'built without cairo support')
@unittest.skipUnless(Gtk, 'Gtk not available')
class TestPango(unittest.TestCase):

    def test_cairo_font_options(self):
        window = Gtk.Window()
        if Gtk._version == "4.0":
            window.set_font_options(cairo.FontOptions())
            font_opts = window.get_font_options()
        else:
            screen = window.get_screen()
            font_opts = screen.get_font_options()
        assert font_opts is not None
        self.assertTrue(isinstance(font_opts.get_subpixel_order(), int))


if has_cairo:
    from gi.repository import cairo as CairoGObject

    # Use PyGI signals to test non-introspected foreign marshaling.
    class CairoSignalTester(GObject.Object):
        sig_context = GObject.Signal(arg_types=[CairoGObject.Context])
        sig_surface = GObject.Signal(arg_types=[CairoGObject.Surface])
        sig_font_face = GObject.Signal(arg_types=[CairoGObject.FontFace])
        sig_scaled_font = GObject.Signal(arg_types=[CairoGObject.ScaledFont])
        sig_pattern = GObject.Signal(arg_types=[CairoGObject.Pattern])


@unittest.skipUnless(has_cairo, 'built without cairo support')
class TestSignalMarshaling(unittest.TestCase):
    # Tests round tripping of cairo objects through non-introspected signals.

    def setUp(self):
        self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
        self.context = cairo.Context(self.surface)
        self.tester = CairoSignalTester()

    def pass_object_through_signal(self, obj, signal):
        """Pass the given `obj` through the `signal` emission storing the
        `obj` passed through the signal and returning it."""
        passthrough_result = []

        def callback(instance, passthrough):
            passthrough_result.append(passthrough)

        signal.connect(callback)
        signal.emit(obj)

        return passthrough_result[0]

    def test_context(self):
        result = self.pass_object_through_signal(self.context, self.tester.sig_context)
        self.assertTrue(isinstance(result, cairo.Context))

        with pytest.raises(TypeError):
            self.pass_object_through_signal(object(), self.tester.sig_context)

    def test_surface(self):
        result = self.pass_object_through_signal(self.surface, self.tester.sig_surface)
        self.assertTrue(isinstance(result, cairo.Surface))

    def test_font_face(self):
        font_face = self.context.get_font_face()
        result = self.pass_object_through_signal(font_face, self.tester.sig_font_face)
        self.assertTrue(isinstance(result, cairo.FontFace))

        with pytest.raises(TypeError):
            self.pass_object_through_signal(object(), self.tester.sig_font_face)

    def test_scaled_font(self):
        scaled_font = cairo.ScaledFont(self.context.get_font_face(),
                                       cairo.Matrix(),
                                       cairo.Matrix(),
                                       self.context.get_font_options())
        result = self.pass_object_through_signal(scaled_font, self.tester.sig_scaled_font)
        self.assertTrue(isinstance(result, cairo.ScaledFont))

        with pytest.raises(TypeError):
            result = self.pass_object_through_signal(object(), self.tester.sig_scaled_font)

    def test_pattern(self):
        pattern = cairo.SolidPattern(1, 1, 1, 1)
        result = self.pass_object_through_signal(pattern, self.tester.sig_pattern)
        self.assertTrue(isinstance(result, cairo.Pattern))
        self.assertTrue(isinstance(result, cairo.SolidPattern))

        with pytest.raises(TypeError):
            result = self.pass_object_through_signal(object(), self.tester.sig_pattern)