Debian Codegraph @

tests/test_pattern.py

raw symbols SCIP
1 import cairo
2 import pytest
3
4
5 def test_raster_source():
6 pattern = cairo.RasterSourcePattern(cairo.Content.COLOR, 2, 2)
7 assert isinstance(pattern, cairo.RasterSourcePattern)
8 assert issubclass(cairo.RasterSourcePattern, cairo.Pattern)
9
10 with pytest.raises(TypeError):
11 cairo.RasterSourcePattern(object())
12
13 was_called = []
14
15 def acquire_callback(target, extents):
16 surface = target.create_similar_image(
17 cairo.FORMAT_ARGB32, extents.width, extents.height)
18 surface.set_device_offset(extents.x, extents.y)
19 context = cairo.Context(surface)
20 context.set_source_rgb(1, 0, 0)
21 context.paint()
22 was_called.append("acquire")
23 return surface
24
25 def release_callback(surface):
26 was_called.append("release")
27 return None
28
29 with pytest.raises(TypeError):
30 pattern.set_acquire()
31
32 pattern.set_acquire(None, release_callback)
33 assert pattern.get_acquire() == (None, release_callback)
34
35 pattern.set_acquire(acquire_callback, None)
36 assert pattern.get_acquire() == (acquire_callback, None)
37
38 pattern.set_acquire(None, None)
39 assert pattern.get_acquire() == (None, None)
40
41 pattern.set_acquire(acquire_callback, release_callback)
42 assert pattern.get_acquire() == (acquire_callback, release_callback)
43
44 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
45 context = cairo.Context(surface)
46 context.set_source(pattern)
47 context.paint()
48 assert was_called == ["acquire", "release"]
49
50 with pytest.raises(TypeError):
51 pattern.set_acquire(None, object())
52
53 with pytest.raises(TypeError):
54 pattern.set_acquire(object(), None)
55
56
57 def test_cmp_hash():
58 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
59 context = cairo.Context(surface)
60 pattern = context.get_source()
61 other = context.get_source()
62 assert pattern == other
63 assert not pattern != other
64
65
66 def test_get_extend():
67 pattern = cairo.SolidPattern(1, 2, 4)
68 assert pattern.get_extend() == cairo.Extend.PAD
69 assert isinstance(pattern.get_extend(), cairo.Extend)
70
71
72 def test_get_filter():
73 pattern = cairo.SolidPattern(1, 2, 4)
74 assert pattern.get_filter() == cairo.Filter.GOOD
75
76
77 def test_linear_gradient():
78 with pytest.raises(TypeError):
79 cairo.LinearGradient()
80
81
82 def test_radial_gradient():
83 with pytest.raises(TypeError):
84 cairo.RadialGradient()
85
86
87 def test_gradient_get_color_stops():
88 pattern = cairo.LinearGradient(1, 2, 4, 5)
89 assert pattern.get_color_stops_rgba() == []
90 pattern.add_color_stop_rgb(0.125, 0.25, 0.5, 0.75)
91 assert pattern.get_color_stops_rgba() == [(0.125, 0.25, 0.5, 0.75, 1.0)]
92 pattern.add_color_stop_rgba(1.0, 0.75, 0.5, 0.25, 0.125)
93 assert pattern.get_color_stops_rgba() == \
94 [(0.125, 0.25, 0.5, 0.75, 1.0), (1.0, 0.75, 0.5, 0.25, 0.125)]
95
96
97 def test_gradient_add_color_stop_rgb():
98 pattern = cairo.LinearGradient(1, 2, 4, 5)
99 with pytest.raises(TypeError):
100 pattern.add_color_stop_rgb()
101
102
103 def test_gradient_add_color_stop_rgba():
104 pattern = cairo.LinearGradient(1, 2, 4, 5)
105 with pytest.raises(TypeError):
106 pattern.add_color_stop_rgba()
107
108
109 def test_solid_pattern():
110 with pytest.raises(TypeError):
111 cairo.SolidPattern()
112
113
114 def test_mesh_pattern():
115 mesh = cairo.MeshPattern()
116 assert isinstance(mesh, cairo.MeshPattern)
117 assert issubclass(cairo.MeshPattern, cairo.Pattern)
118 with pytest.raises(TypeError):
119 cairo.MeshPattern(object())
120
121
122 def test_mesh_pattern_example1():
123 pattern = cairo.MeshPattern()
124 assert pattern.get_patch_count() == 0
125
126 pattern.begin_patch()
127 pattern.move_to(0, 0)
128 pattern.curve_to(30, -30, 60, 30, 100, 0)
129 pattern.curve_to(60, 30, 130, 60, 100, 100)
130 pattern.curve_to(60, 70, 30, 130, 0, 100)
131 pattern.curve_to(30, 70, -30, 30, 0, 0)
132 pattern.set_corner_color_rgb(0, 1, 0, 0)
133 pattern.set_corner_color_rgb(1, 0, 1, 0)
134 pattern.set_corner_color_rgb(2, 0, 0, 1)
135 pattern.set_corner_color_rgb(3, 1, 1, 0)
136 pattern.end_patch()
137
138 assert pattern.get_patch_count() == 1
139 assert pattern.get_path(0)
140 assert pattern.get_corner_color_rgba(0, 0) == (1, 0, 0, 1)
141
142 with pytest.raises(cairo.Error):
143 pattern.get_corner_color_rgba(1, 0)
144
145 with pytest.raises(cairo.Error):
146 pattern.get_corner_color_rgba(0, 9)
147
148 with pytest.raises(cairo.Error):
149 pattern.get_path(9)
150
151
152 def test_mesh_pattern_curve_to():
153 pattern = cairo.MeshPattern()
154 with pytest.raises(TypeError):
155 pattern.curve_to(object())
156
157
158 def test_mesh_pattern_get_control_point():
159 pattern = cairo.MeshPattern()
160 with pytest.raises(TypeError):
161 pattern.get_control_point(object())
162
163
164 def test_mesh_pattern_get_corner_color_rgba():
165 pattern = cairo.MeshPattern()
166 with pytest.raises(TypeError):
167 pattern.get_corner_color_rgba(object())
168
169
170 def test_mesh_pattern_get_path():
171 pattern = cairo.MeshPattern()
172 with pytest.raises(TypeError):
173 pattern.get_path(object())
174
175
176 def test_mesh_pattern_line_to():
177 pattern = cairo.MeshPattern()
178 with pytest.raises(TypeError):
179 pattern.line_to(object())
180
181
182 def test_mesh_pattern_move_to():
183 pattern = cairo.MeshPattern()
184 with pytest.raises(TypeError):
185 pattern.move_to(object())
186
187
188 def test_mesh_pattern_set_control_point():
189 pattern = cairo.MeshPattern()
190 with pytest.raises(TypeError):
191 pattern.set_control_point(object())
192
193
194 def test_mesh_pattern_set_corner_color_rgb():
195 pattern = cairo.MeshPattern()
196 with pytest.raises(TypeError):
197 pattern.set_corner_color_rgb(object())
198
199
200 def test_mesh_pattern_set_corner_color_rgba():
201 pattern = cairo.MeshPattern()
202 with pytest.raises(TypeError):
203 pattern.set_corner_color_rgba(object())
204
205
206 def test_mesh_pattern_example2():
207 pattern = cairo.MeshPattern()
208 pattern.begin_patch()
209 pattern.move_to(100, 100)
210 pattern.line_to(130, 130)
211 pattern.line_to(130, 70)
212 pattern.set_corner_color_rgb(0, 1, 0, 0)
213 pattern.set_corner_color_rgb(1, 0, 1, 0)
214 pattern.set_corner_color_rgb(2, 0, 0, 1)
215 pattern.end_patch()
216
217
218 def test_mesh_pattern_rest():
219 pattern = cairo.MeshPattern()
220 pattern.begin_patch()
221 pattern.curve_to(0, 1, 2, 3, 4, 5)
222 pattern.set_corner_color_rgba(0, 0.125, 0.25, 0.5, 0.75)
223 pattern.set_control_point(0, 10, 12)
224 pattern.end_patch()
225
226 assert pattern.get_corner_color_rgba(0, 0) == (0.125, 0.25, 0.5, 0.75)
227 assert pattern.get_control_point(0, 0) == (10, 12)
228
229 pattern.get_control_point(0, 0)
230
231 with pytest.raises(cairo.Error):
232 pattern.get_control_point(1, 0)
233
234 with pytest.raises(cairo.Error):
235 pattern.get_control_point(0, 9)
236
237
238 def test_mesh_pattern_error_states():
239 pattern = cairo.MeshPattern()
240 pattern.begin_patch()
241 with pytest.raises(cairo.Error):
242 pattern.begin_patch()
243
244 with pytest.raises(cairo.Error):
245 cairo.MeshPattern().end_patch()
246
247 with pytest.raises(cairo.Error):
248 cairo.MeshPattern().curve_to(0, 1, 2, 3, 4, 5)
249
250 with pytest.raises(cairo.Error):
251 cairo.MeshPattern().line_to(130, 130)
252
253 with pytest.raises(cairo.Error):
254 cairo.MeshPattern().move_to(0, 0)
255
256 with pytest.raises(cairo.Error):
257 cairo.MeshPattern().set_corner_color_rgba(0, 0.125, 0.25, 0.5, 0.75)
258
259 with pytest.raises(cairo.Error):
260 cairo.MeshPattern().set_corner_color_rgb(0, 0.125, 0.25, 0.5)
261
262
263 def test_get_matrix():
264 pattern = cairo.SolidPattern(1, 2, 4)
265 assert isinstance(pattern.get_matrix(), cairo.Matrix)
266 pattern.set_matrix(cairo.Matrix())
267 with pytest.raises(TypeError):
268 pattern.set_matrix(object())
269
270
271 def test_set_extend():
272 pattern = cairo.SolidPattern(1, 2, 4)
273 pattern.set_extend(42)
274 assert pattern.get_extend() == 42
275 with pytest.raises(TypeError):
276 pattern.set_extend(object())
277
278
279 def test_set_filter():
280 pattern = cairo.SolidPattern(1, 2, 4)
281 with pytest.raises(TypeError):
282 pattern.set_filter(object())
283
284
285 def test_pattern():
286 with pytest.raises(TypeError):
287 cairo.Pattern()
288
289 r, g, b, a = 0.1, 0.2, 0.3, 0.4
290 p = cairo.SolidPattern(r, g, b, a)
291 assert p.get_rgba() == (r, g, b, a)
292
293 assert not p == object()
294 hash(p)
295
296 with pytest.raises(TypeError):
297 cairo.Gradient()
298
299 x0, y0, x1, y1 = 0.0, 0.0, 0.0, 1.0
300 p = cairo.LinearGradient(x0, y0, x1, y1)
301 assert p.get_linear_points() == (x0, y0, x1, y1)
302 p.add_color_stop_rgba(1, 0, 0, 0, 1)
303 p.add_color_stop_rgba(0, 1, 1, 1, 1)
304
305 cx0, cy0, radius0, cx1, cy1, radius1 = 1.0, 1.0, 1.0, 2.0, 2.0, 1.0
306 p = cairo.RadialGradient(cx0, cy0, radius0, cx1, cy1, radius1)
307 assert p.get_radial_circles() == (cx0, cy0, radius0, cx1, cy1, radius1)
308 p.add_color_stop_rgba(0, 1, 1, 1, 1)
309 p.add_color_stop_rgba(1, 0, 0, 0, 1)
310
311
312 def test_pattern_filter():
313 pattern = cairo.SolidPattern(1, 2, 3)
314 assert pattern.get_filter() == cairo.FILTER_GOOD
315 pattern.set_filter(cairo.FILTER_NEAREST)
316 assert pattern.get_filter() == cairo.FILTER_NEAREST
317
318
319 def test_surface_pattern():
320 with pytest.raises(TypeError):
321 cairo.SurfacePattern(object())
322
323
324 @pytest.mark.skipif(not hasattr(cairo.Pattern, "set_dither"),
325 reason="too old cairo")
326 def test_pattern_dither():
327 pattern = cairo.SolidPattern(1, 2, 3)
328 pattern.get_dither() == cairo.Dither.DEFAULT
329 pattern.set_dither(cairo.Dither.BEST)
330 assert pattern.get_dither() == cairo.Dither.BEST