1 # -*- Mode: Python; py-indent-offset: 4 -*-
3 # vim: tabstop=4 shiftwidth=4 expandtab
10 gi
.require_foreign('cairo')
12 from gi
.repository
import Regress
18 from gi
.repository
import Gtk
23 from gi
.repository
import GObject
26 @unittest.skipUnless(has_cairo
, 'built without cairo support')
27 class Test(unittest
.TestCase
):
28 def test_cairo_context(self
):
29 context
= Regress
.test_cairo_context_full_return()
30 self
.assertTrue(isinstance(context
, cairo
.Context
))
32 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
33 context
= cairo
.Context(surface
)
34 Regress
.test_cairo_context_none_in(context
)
36 def test_cairo_surface(self
):
37 surface
= Regress
.test_cairo_surface_none_return()
38 self
.assertTrue(isinstance(surface
, cairo
.ImageSurface
))
39 self
.assertTrue(isinstance(surface
, cairo
.Surface
))
40 self
.assertEqual(surface
.get_format(), cairo
.FORMAT_ARGB32
)
41 self
.assertEqual(surface
.get_width(), 10)
42 self
.assertEqual(surface
.get_height(), 10)
44 surface
= Regress
.test_cairo_surface_full_return()
45 self
.assertTrue(isinstance(surface
, cairo
.ImageSurface
))
46 self
.assertTrue(isinstance(surface
, cairo
.Surface
))
47 self
.assertEqual(surface
.get_format(), cairo
.FORMAT_ARGB32
)
48 self
.assertEqual(surface
.get_width(), 10)
49 self
.assertEqual(surface
.get_height(), 10)
51 surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
52 Regress
.test_cairo_surface_none_in(surface
)
54 surface
= Regress
.test_cairo_surface_full_out()
55 self
.assertTrue(isinstance(surface
, cairo
.ImageSurface
))
56 self
.assertTrue(isinstance(surface
, cairo
.Surface
))
57 self
.assertEqual(surface
.get_format(), cairo
.FORMAT_ARGB32
)
58 self
.assertEqual(surface
.get_width(), 10)
59 self
.assertEqual(surface
.get_height(), 10)
61 def test_require_foreign(self
):
62 self
.assertEqual(gi
.require_foreign('cairo'), None)
63 self
.assertEqual(gi
.require_foreign('cairo', 'Context'), None)
64 self
.assertRaises(ImportError, gi
.require_foreign
, 'invalid_module')
65 self
.assertRaises(ImportError, gi
.require_foreign
, 'invalid_module', 'invalid_symbol')
66 self
.assertRaises(ImportError, gi
.require_foreign
, 'cairo', 'invalid_symbol')
69 @unittest.skipUnless(has_cairo
, 'built without cairo support')
70 @unittest.skipUnless(Gtk
, 'Gtk not available')
71 class TestPango(unittest
.TestCase
):
72 def test_cairo_font_options(self
):
73 screen
= Gtk
.Window().get_screen()
74 font_opts
= screen
.get_font_options()
75 self
.assertEqual(type(font_opts
.get_subpixel_order()), int)
79 from gi
.repository
import cairo
as CairoGObject
81 # Use PyGI signals to test non-introspected foreign marshaling.
82 class CairoSignalTester(GObject
.Object
):
83 sig_context
= GObject
.Signal(arg_types
=[CairoGObject
.Context
])
84 sig_surface
= GObject
.Signal(arg_types
=[CairoGObject
.Surface
])
85 sig_font_face
= GObject
.Signal(arg_types
=[CairoGObject
.FontFace
])
86 sig_scaled_font
= GObject
.Signal(arg_types
=[CairoGObject
.ScaledFont
])
87 sig_pattern
= GObject
.Signal(arg_types
=[CairoGObject
.Pattern
])
90 @unittest.skipUnless(has_cairo
, 'built without cairo support')
91 class TestSignalMarshaling(unittest
.TestCase
):
92 # Tests round tripping of cairo objects through non-introspected signals.
95 self
.surface
= cairo
.ImageSurface(cairo
.FORMAT_ARGB32
, 10, 10)
96 self
.context
= cairo
.Context(self
.surface
)
97 self
.tester
= CairoSignalTester()
99 def pass_object_through_signal(self
, obj
, signal
):
100 """Pass the given `obj` through the `signal` emission storing the
101 `obj` passed through the signal and returning it."""
102 passthrough_result
= []
104 def callback(instance
, passthrough
):
105 passthrough_result
.append(passthrough
)
107 signal
.connect(callback
)
110 return passthrough_result
[0]
112 def test_context(self
):
113 result
= self
.pass_object_through_signal(self
.context
, self
.tester
.sig_context
)
114 self
.assertTrue(isinstance(result
, cairo
.Context
))
116 def test_surface(self
):
117 result
= self
.pass_object_through_signal(self
.surface
, self
.tester
.sig_surface
)
118 self
.assertTrue(isinstance(result
, cairo
.Surface
))
120 def test_font_face(self
):
121 font_face
= self
.context
.get_font_face()
122 result
= self
.pass_object_through_signal(font_face
, self
.tester
.sig_font_face
)
123 self
.assertTrue(isinstance(result
, cairo
.FontFace
))
125 def test_scaled_font(self
):
126 scaled_font
= cairo
.ScaledFont(self
.context
.get_font_face(),
129 self
.context
.get_font_options())
130 result
= self
.pass_object_through_signal(scaled_font
, self
.tester
.sig_scaled_font
)
131 self
.assertTrue(isinstance(result
, cairo
.ScaledFont
))
133 def test_pattern(self
):
134 pattern
= cairo
.SolidPattern(1, 1, 1, 1)
135 result
= self
.pass_object_through_signal(pattern
, self
.tester
.sig_pattern
)
136 self
.assertTrue(isinstance(result
, cairo
.Pattern
))
137 self
.assertTrue(isinstance(result
, cairo
.SolidPattern
))
140 if __name__
== '__main__':