Debian Codegraph @

tests/test_cairo.py

raw
1 import unittest
2 import pytest
3
4 import gi
5
6 try:
7 gi.require_foreign("cairo")
8 import cairo
9
10 has_cairo = True
11 except ImportError:
12 has_cairo = False
13
14 has_region = has_cairo and hasattr(cairo, "Region")
15
16 try:
17 from gi.repository import Gtk, Gdk
18
19 Gtk, Gdk
20 except ImportError:
21 Gtk = None
22 Gdk = None
23
24 from gi.repository import GObject, Regress
25
26
27 @unittest.skipUnless(has_cairo, "built without cairo support")
28 class Test(unittest.TestCase):
29 def test_gvalue_converters(self):
30 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
31 context = cairo.Context(surface)
32 matrix = cairo.Matrix()
33 objects = {
34 "CairoContext": context,
35 "CairoSurface": surface,
36 "CairoFontFace": context.get_font_face(),
37 "CairoScaledFont": context.get_scaled_font(),
38 "CairoPattern": context.get_source(),
39 "CairoMatrix": matrix,
40 }
41 for type_name, cairo_obj in objects.items():
42 gtype = GObject.type_from_name(type_name)
43 v = GObject.Value()
44 assert v.init(gtype) is None
45 assert v.get_value() is None
46 v.set_value(None)
47 assert v.get_value() is None
48 v.set_value(cairo_obj)
49 assert v.get_value() == cairo_obj
50
51 def test_cairo_context(self):
52 context = Regress.test_cairo_context_full_return()
53 self.assertTrue(isinstance(context, cairo.Context))
54
55 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
56 context = cairo.Context(surface)
57 Regress.test_cairo_context_none_in(context)
58
59 def test_cairo_context_full_in(self):
60 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
61 context = cairo.Context(surface)
62 Regress.test_cairo_context_full_in(context)
63
64 with pytest.raises(TypeError):
65 Regress.test_cairo_context_full_in(object())
66
67 def test_cairo_context_none_return(self):
68 context = Regress.test_cairo_context_none_return()
69 self.assertTrue(isinstance(context, cairo.Context))
70
71 def test_cairo_path_full_return(self):
72 path = Regress.test_cairo_path_full_return()
73 if hasattr(cairo, "Path"): # pycairo 1.15.1+
74 assert isinstance(path, cairo.Path)
75
76 def test_cairo_path_none_in(self):
77 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
78 context = cairo.Context(surface)
79 path = context.copy_path()
80 Regress.test_cairo_path_none_in(path)
81 surface.finish()
82
83 with pytest.raises(TypeError):
84 Regress.test_cairo_path_none_in(object())
85
86 def test_cairo_path_full_in_full_return(self):
87 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
88 context = cairo.Context(surface)
89 context.move_to(10, 10)
90 context.curve_to(10, 10, 3, 4, 5, 6)
91 path = context.copy_path()
92 new_path = Regress.test_cairo_path_full_in_full_return(path)
93 assert list(path) == list(new_path)
94 surface.finish()
95
96 def test_cairo_font_options_full_return(self):
97 options = Regress.test_cairo_font_options_full_return()
98 assert isinstance(options, cairo.FontOptions)
99
100 def test_cairo_font_options_none_return(self):
101 options = Regress.test_cairo_font_options_none_return()
102 assert isinstance(options, cairo.FontOptions)
103
104 def test_cairo_font_options_full_in(self):
105 options = cairo.FontOptions()
106 Regress.test_cairo_font_options_full_in(options)
107
108 with pytest.raises(TypeError):
109 Regress.test_cairo_font_options_full_in(object())
110
111 def test_cairo_font_options_none_in(self):
112 options = cairo.FontOptions()
113 Regress.test_cairo_font_options_none_in(options)
114
115 def test_cairo_font_face_full_return(self):
116 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
117 context = cairo.Context(surface)
118 font_face = Regress.test_cairo_font_face_full_return(context)
119
120 assert font_face
121
122 def test_cairo_scaled_font_full_return(self):
123 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
124 context = cairo.Context(surface)
125 scaled_font = Regress.test_cairo_scaled_font_full_return(context)
126
127 assert scaled_font
128
129 def test_cairo_pattern_full_in(self):
130 pattern = cairo.SolidPattern(1, 1, 1, 1)
131 Regress.test_cairo_pattern_full_in(pattern)
132
133 with pytest.raises(TypeError):
134 Regress.test_cairo_pattern_full_in(object())
135
136 def test_cairo_pattern_none_in(self):
137 pattern = cairo.SolidPattern(1, 1, 1, 1)
138 Regress.test_cairo_pattern_none_in(pattern)
139
140 def test_cairo_pattern_full_return(self):
141 pattern = Regress.test_cairo_pattern_full_return()
142 self.assertTrue(isinstance(pattern, cairo.Pattern))
143 self.assertTrue(isinstance(pattern, cairo.SolidPattern))
144
145 def test_cairo_pattern_none_return(self):
146 pattern = Regress.test_cairo_pattern_none_return()
147 self.assertTrue(isinstance(pattern, cairo.Pattern))
148 self.assertTrue(isinstance(pattern, cairo.SolidPattern))
149
150 def test_cairo_region_full_in(self):
151 region = cairo.Region()
152 Regress.test_cairo_region_full_in(region)
153
154 with pytest.raises(TypeError):
155 Regress.test_cairo_region_full_in(object())
156
157 def test_cairo_matrix_none_in(self):
158 matrix = cairo.Matrix()
159 Regress.test_cairo_matrix_none_in(matrix)
160
161 with pytest.raises(TypeError):
162 Regress.test_cairo_matrix_none_in(object())
163
164 def test_cairo_matrix_none_return(self):
165 matrix = Regress.test_cairo_matrix_none_return()
166 assert matrix == cairo.Matrix()
167
168 def test_cairo_matrix_out_caller_allocates(self):
169 matrix = Regress.test_cairo_matrix_out_caller_allocates()
170 assert matrix == cairo.Matrix()
171
172 def test_cairo_surface(self):
173 surface = Regress.test_cairo_surface_none_return()
174 self.assertTrue(isinstance(surface, cairo.ImageSurface))
175 self.assertTrue(isinstance(surface, cairo.Surface))
176 self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
177 self.assertEqual(surface.get_width(), 10)
178 self.assertEqual(surface.get_height(), 10)
179
180 surface = Regress.test_cairo_surface_full_return()
181 self.assertTrue(isinstance(surface, cairo.ImageSurface))
182 self.assertTrue(isinstance(surface, cairo.Surface))
183 self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
184 self.assertEqual(surface.get_width(), 10)
185 self.assertEqual(surface.get_height(), 10)
186
187 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
188 Regress.test_cairo_surface_none_in(surface)
189
190 surface = Regress.test_cairo_surface_full_out()
191 self.assertTrue(isinstance(surface, cairo.ImageSurface))
192 self.assertTrue(isinstance(surface, cairo.Surface))
193 self.assertEqual(surface.get_format(), cairo.FORMAT_ARGB32)
194 self.assertEqual(surface.get_width(), 10)
195 self.assertEqual(surface.get_height(), 10)
196
197 def test_cairo_surface_full_in(self):
198 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
199 Regress.test_cairo_surface_full_in(surface)
200
201 with pytest.raises(TypeError):
202 Regress.test_cairo_surface_full_in(object())
203
204 def test_require_foreign(self):
205 self.assertEqual(gi.require_foreign("cairo"), None)
206 self.assertEqual(gi.require_foreign("cairo", "Context"), None)
207 self.assertRaises(ImportError, gi.require_foreign, "invalid_module")
208 self.assertRaises(
209 ImportError, gi.require_foreign, "invalid_module", "invalid_symbol"
210 )
211 self.assertRaises(ImportError, gi.require_foreign, "cairo", "invalid_symbol")
212
213
214 @unittest.skipUnless(has_cairo, "built without cairo support")
215 @unittest.skipUnless(has_region, "built without cairo.Region support")
216 @unittest.skipUnless(Gdk, "Gdk not available")
217 class TestRegion(unittest.TestCase):
218 def test_region_to_py(self):
219 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
220 context = cairo.Context(surface)
221 context.paint()
222 region = Gdk.cairo_region_create_from_surface(surface)
223 r = region.get_extents()
224 self.assertEqual((r.height, r.width), (10, 10))
225
226 def test_region_from_py(self):
227 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
228 context = cairo.Context(surface)
229 region = cairo.Region(cairo.RectangleInt(0, 0, 42, 42))
230 Gdk.cairo_region(context, region)
231 self.assertTrue("42" in repr(list(context.copy_path())))
232
233
234 @unittest.skipUnless(has_cairo, "built without cairo support")
235 @unittest.skipUnless(Gtk, "Gtk not available")
236 class TestPango(unittest.TestCase):
237 def test_cairo_font_options(self):
238 window = Gtk.Window()
239 if Gtk._version == "4.0":
240 window.set_font_options(cairo.FontOptions())
241 font_opts = window.get_font_options()
242 else:
243 screen = window.get_screen()
244 font_opts = screen.get_font_options()
245 assert font_opts is not None
246 self.assertTrue(isinstance(font_opts.get_subpixel_order(), int))
247
248
249 if has_cairo:
250 from gi.repository import cairo as CairoGObject
251
252 # Use PyGI signals to test non-introspected foreign marshaling.
253 class CairoSignalTester(GObject.Object):
254 sig_context = GObject.Signal(arg_types=[CairoGObject.Context])
255 sig_surface = GObject.Signal(arg_types=[CairoGObject.Surface])
256 sig_font_face = GObject.Signal(arg_types=[CairoGObject.FontFace])
257 sig_scaled_font = GObject.Signal(arg_types=[CairoGObject.ScaledFont])
258 sig_pattern = GObject.Signal(arg_types=[CairoGObject.Pattern])
259
260
261 @unittest.skipUnless(has_cairo, "built without cairo support")
262 class TestSignalMarshaling(unittest.TestCase):
263 # Tests round tripping of cairo objects through non-introspected signals.
264
265 def setUp(self):
266 self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
267 self.context = cairo.Context(self.surface)
268 self.tester = CairoSignalTester()
269
270 def pass_object_through_signal(self, obj, signal):
271 """Pass the given `obj` through the `signal` emission storing the
272 `obj` passed through the signal and returning it.
273 """
274 passthrough_result = []
275
276 def callback(instance, passthrough):
277 passthrough_result.append(passthrough)
278
279 signal.connect(callback)
280 signal.emit(obj)
281
282 return passthrough_result[0]
283
284 def test_context(self):
285 result = self.pass_object_through_signal(self.context, self.tester.sig_context)
286 self.assertTrue(isinstance(result, cairo.Context))
287
288 with pytest.raises(TypeError):
289 self.pass_object_through_signal(object(), self.tester.sig_context)
290
291 def test_surface(self):
292 result = self.pass_object_through_signal(self.surface, self.tester.sig_surface)
293 self.assertTrue(isinstance(result, cairo.Surface))
294
295 def test_font_face(self):
296 font_face = self.context.get_font_face()
297 result = self.pass_object_through_signal(font_face, self.tester.sig_font_face)
298 self.assertTrue(isinstance(result, cairo.FontFace))
299
300 with pytest.raises(TypeError):
301 self.pass_object_through_signal(object(), self.tester.sig_font_face)
302
303 def test_scaled_font(self):
304 scaled_font = cairo.ScaledFont(
305 self.context.get_font_face(),
306 cairo.Matrix(),
307 cairo.Matrix(),
308 self.context.get_font_options(),
309 )
310 result = self.pass_object_through_signal(
311 scaled_font, self.tester.sig_scaled_font
312 )
313 self.assertTrue(isinstance(result, cairo.ScaledFont))
314
315 with pytest.raises(TypeError):
316 result = self.pass_object_through_signal(
317 object(), self.tester.sig_scaled_font
318 )
319
320 def test_pattern(self):
321 pattern = cairo.SolidPattern(1, 1, 1, 1)
322 result = self.pass_object_through_signal(pattern, self.tester.sig_pattern)
323 self.assertTrue(isinstance(result, cairo.Pattern))
324 self.assertTrue(isinstance(result, cairo.SolidPattern))
325
326 with pytest.raises(TypeError):
327 result = self.pass_object_through_signal(object(), self.tester.sig_pattern)