1 # -*- Mode: Python; py-indent-offset: 4 -*-
3 # vim: tabstop=4 shiftwidth=4 expandtab
5 from __future__
import absolute_import
13 gi
.require_foreign('cairo')
19 has_region
= has_cairo
and hasattr(cairo
, "Region")
22 from gi
.repository
import Gtk
, Gdk
28 from gi
.repository
import GObject
, Regress
31 @unittest.skipUnless(has_cairo
, 'built without cairo support')
32 class Test(unittest
.TestCase
):
33 def test_cairo_context(self
):
34 context
= Regress
.test_cairo_context_full_return()
35 self
.assertTrue(isinstance(context
, cairo
.Context
))
37 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
38 context
= cairo
.Context(surface
)
39 Regress
.test_cairo_context_none_in(context
)
41 def test_cairo_context_full_in(self
):
42 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
43 context
= cairo
.Context(surface
)
44 Regress
.test_cairo_context_full_in(context
)
46 with pytest
.raises(TypeError):
47 Regress
.test_cairo_context_full_in(object())
49 def test_cairo_context_none_return(self
):
50 context
= Regress
.test_cairo_context_none_return()
51 self
.assertTrue(isinstance(context
, cairo
.Context
))
53 def test_cairo_path_full_return(self
):
54 path
= Regress
.test_cairo_path_full_return()
55 if hasattr(cairo
, "Path"): # pycairo 1.15.1+
56 assert isinstance(path
, cairo
.Path
)
58 def test_cairo_path_none_in(self
):
59 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
60 context
= cairo
.Context(surface
)
61 path
= context
.copy_path()
62 Regress
.test_cairo_path_none_in(path
)
65 with pytest
.raises(TypeError):
66 Regress
.test_cairo_path_none_in(object())
68 def test_cairo_path_full_in_full_return(self
):
69 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
70 context
= cairo
.Context(surface
)
71 context
.move_to(10, 10)
72 context
.curve_to(10, 10, 3, 4, 5, 6)
73 path
= context
.copy_path()
74 new_path
= Regress
.test_cairo_path_full_in_full_return(path
)
75 assert list(path
) == list(new_path
)
78 def test_cairo_font_options_full_return(self
):
79 options
= Regress
.test_cairo_font_options_full_return()
80 assert isinstance(options
, cairo
.FontOptions
)
82 def test_cairo_font_options_none_return(self
):
83 options
= Regress
.test_cairo_font_options_none_return()
84 assert isinstance(options
, cairo
.FontOptions
)
86 def test_cairo_font_options_full_in(self
):
87 options
= cairo
.FontOptions()
88 Regress
.test_cairo_font_options_full_in(options
)
90 with pytest
.raises(TypeError):
91 Regress
.test_cairo_font_options_full_in(object())
93 def test_cairo_font_options_none_in(self
):
94 options
= cairo
.FontOptions()
95 Regress
.test_cairo_font_options_none_in(options
)
97 def test_cairo_region_full_in(self
):
98 region
= cairo
.Region()
99 Regress
.test_cairo_region_full_in(region
)
101 with pytest
.raises(TypeError):
102 Regress
.test_cairo_region_full_in(object())
104 def test_cairo_matrix_none_in(self
):
105 matrix
= cairo
.Matrix()
106 Regress
.test_cairo_matrix_none_in(matrix
)
108 with pytest
.raises(TypeError):
109 Regress
.test_cairo_matrix_none_in(object())
111 def test_cairo_matrix_none_return(self
):
112 matrix
= Regress
.test_cairo_matrix_none_return()
113 assert matrix
== cairo
.Matrix()
115 def test_cairo_matrix_out_caller_allocates(self
):
116 matrix
= Regress
.test_cairo_matrix_out_caller_allocates()
117 assert matrix
== cairo
.Matrix()
119 def test_cairo_surface(self
):
120 surface
= Regress
.test_cairo_surface_none_return()
121 self
.assertTrue(isinstance(surface
, cairo
.ImageSurface
))
122 self
.assertTrue(isinstance(surface
, cairo
.Surface
))
123 self
.assertEqual(surface
.get_format(), cairo
.FORMAT_ARGB32
)
124 self
.assertEqual(surface
.get_width(), 10)
125 self
.assertEqual(surface
.get_height(), 10)
127 surface
= Regress
.test_cairo_surface_full_return()
128 self
.assertTrue(isinstance(surface
, cairo
.ImageSurface
))
129 self
.assertTrue(isinstance(surface
, cairo
.Surface
))
130 self
.assertEqual(surface
.get_format(), cairo
.FORMAT_ARGB32
)
131 self
.assertEqual(surface
.get_width(), 10)
132 self
.assertEqual(surface
.get_height(), 10)
134 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
135 Regress
.test_cairo_surface_none_in(surface
)
137 surface
= Regress
.test_cairo_surface_full_out()
138 self
.assertTrue(isinstance(surface
, cairo
.ImageSurface
))
139 self
.assertTrue(isinstance(surface
, cairo
.Surface
))
140 self
.assertEqual(surface
.get_format(), cairo
.FORMAT_ARGB32
)
141 self
.assertEqual(surface
.get_width(), 10)
142 self
.assertEqual(surface
.get_height(), 10)
144 def test_cairo_surface_full_in(self
):
145 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
146 Regress
.test_cairo_surface_full_in(surface
)
148 with pytest
.raises(TypeError):
149 Regress
.test_cairo_surface_full_in(object())
151 def test_require_foreign(self
):
152 self
.assertEqual(gi
.require_foreign('cairo'), None)
153 self
.assertEqual(gi
.require_foreign('cairo', 'Context'), None)
154 self
.assertRaises(ImportError, gi
.require_foreign
, 'invalid_module')
155 self
.assertRaises(ImportError, gi
.require_foreign
, 'invalid_module', 'invalid_symbol')
156 self
.assertRaises(ImportError, gi
.require_foreign
, 'cairo', 'invalid_symbol')
159 @unittest.skipUnless(has_cairo
, 'built without cairo support')
160 @unittest.skipUnless(has_region
, 'built without cairo.Region support')
161 @unittest.skipUnless(Gdk
, 'Gdk not available')
162 class TestRegion(unittest
.TestCase
):
164 def test_region_to_py(self
):
165 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
166 context
= cairo
.Context(surface
)
168 region
= Gdk
.cairo_region_create_from_surface(surface
)
169 r
= region
.get_extents()
170 self
.assertEqual((r
.height
, r
.width
), (10, 10))
172 def test_region_from_py(self
):
173 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
174 context
= cairo
.Context(surface
)
175 region
= cairo
.Region(cairo
.RectangleInt(0, 0, 42, 42))
176 Gdk
.cairo_region(context
, region
)
177 self
.assertTrue("42" in repr(list(context
.copy_path())))
180 @unittest.skipUnless(has_cairo
, 'built without cairo support')
181 @unittest.skipUnless(Gtk
, 'Gtk not available')
182 class TestPango(unittest
.TestCase
):
183 def test_cairo_font_options(self
):
184 screen
= Gtk
.Window().get_screen()
185 font_opts
= screen
.get_font_options()
186 self
.assertTrue(isinstance(font_opts
.get_subpixel_order(), int))
190 from gi
.repository
import cairo
as CairoGObject
192 # Use PyGI signals to test non-introspected foreign marshaling.
193 class CairoSignalTester(GObject
.Object
):
194 sig_context
= GObject
.Signal(arg_types
=[CairoGObject
.Context
])
195 sig_surface
= GObject
.Signal(arg_types
=[CairoGObject
.Surface
])
196 sig_font_face
= GObject
.Signal(arg_types
=[CairoGObject
.FontFace
])
197 sig_scaled_font
= GObject
.Signal(arg_types
=[CairoGObject
.ScaledFont
])
198 sig_pattern
= GObject
.Signal(arg_types
=[CairoGObject
.Pattern
])
201 @unittest.skipUnless(has_cairo
, 'built without cairo support')
202 class TestSignalMarshaling(unittest
.TestCase
):
203 # Tests round tripping of cairo objects through non-introspected signals.
206 self
.surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
207 self
.context
= cairo
.Context(self
.surface
)
208 self
.tester
= CairoSignalTester()
210 def pass_object_through_signal(self
, obj
, signal
):
211 """Pass the given `obj` through the `signal` emission storing the
212 `obj` passed through the signal and returning it."""
213 passthrough_result
= []
215 def callback(instance
, passthrough
):
216 passthrough_result
.append(passthrough
)
218 signal
.connect(callback
)
221 return passthrough_result
[0]
223 def test_context(self
):
224 result
= self
.pass_object_through_signal(self
.context
, self
.tester
.sig_context
)
225 self
.assertTrue(isinstance(result
, cairo
.Context
))
227 with pytest
.raises(TypeError):
228 self
.pass_object_through_signal(object(), self
.tester
.sig_context
)
230 def test_surface(self
):
231 result
= self
.pass_object_through_signal(self
.surface
, self
.tester
.sig_surface
)
232 self
.assertTrue(isinstance(result
, cairo
.Surface
))
234 def test_font_face(self
):
235 font_face
= self
.context
.get_font_face()
236 result
= self
.pass_object_through_signal(font_face
, self
.tester
.sig_font_face
)
237 self
.assertTrue(isinstance(result
, cairo
.FontFace
))
239 with pytest
.raises(TypeError):
240 self
.pass_object_through_signal(object(), self
.tester
.sig_font_face
)
242 def test_scaled_font(self
):
243 scaled_font
= cairo
.ScaledFont(self
.context
.get_font_face(),
246 self
.context
.get_font_options())
247 result
= self
.pass_object_through_signal(scaled_font
, self
.tester
.sig_scaled_font
)
248 self
.assertTrue(isinstance(result
, cairo
.ScaledFont
))
250 with pytest
.raises(TypeError):
251 result
= self
.pass_object_through_signal(object(), self
.tester
.sig_scaled_font
)
253 def test_pattern(self
):
254 pattern
= cairo
.SolidPattern(1, 1, 1, 1)
255 result
= self
.pass_object_through_signal(pattern
, self
.tester
.sig_pattern
)
256 self
.assertTrue(isinstance(result
, cairo
.Pattern
))
257 self
.assertTrue(isinstance(result
, cairo
.SolidPattern
))
259 with pytest
.raises(TypeError):
260 result
= self
.pass_object_through_signal(object(), self
.tester
.sig_pattern
)