1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
4 from __future__
import absolute_import
11 from gi
import PyGIDeprecationWarning
14 from gi
.repository
import Gdk
, GdkPixbuf
, Gtk
15 Gdk_version
= Gdk
._version
20 from .helper
import capture_glib_deprecation_warnings
23 @unittest.skipUnless(Gdk
, 'Gdk not available')
24 class TestGdk(unittest
.TestCase
):
26 @unittest.skipIf(sys
.platform
== "darwin" or os
.name
== "nt", "crashes")
27 @unittest.skipIf(Gdk_version
== "4.0", "not in gdk4")
28 def test_constructor(self
):
29 attribute
= Gdk
.WindowAttr()
30 attribute
.window_type
= Gdk
.WindowType
.CHILD
31 attributes_mask
= Gdk
.WindowAttributesType
.X | \
32 Gdk
.WindowAttributesType
.Y
33 window
= Gdk
.Window(None, attribute
, attributes_mask
)
34 self
.assertEqual(window
.get_window_type(), Gdk
.WindowType
.CHILD
)
36 @unittest.skipIf(Gdk_version
== "4.0", "not in gdk4")
38 color
= Gdk
.Color(100, 200, 300)
39 self
.assertEqual(color
.red
, 100)
40 self
.assertEqual(color
.green
, 200)
41 self
.assertEqual(color
.blue
, 300)
42 with
capture_glib_deprecation_warnings():
43 self
.assertEqual(color
, Gdk
.Color(100, 200, 300))
44 self
.assertNotEqual(color
, Gdk
.Color(1, 2, 3))
46 @unittest.skipIf(Gdk_version
== "4.0", "not in gdk4")
47 def test_color_floats(self
):
48 self
.assertEqual(Gdk
.Color(13107, 21845, 65535),
49 Gdk
.Color
.from_floats(0.2, 1.0 / 3.0, 1.0))
51 self
.assertEqual(Gdk
.Color(13107, 21845, 65535).to_floats(),
52 (0.2, 1.0 / 3.0, 1.0))
54 self
.assertEqual(Gdk
.RGBA(0.2, 1.0 / 3.0, 1.0, 0.5).to_color(),
55 Gdk
.Color
.from_floats(0.2, 1.0 / 3.0, 1.0))
57 self
.assertEqual(Gdk
.RGBA
.from_color(Gdk
.Color(13107, 21845, 65535)),
58 Gdk
.RGBA(0.2, 1.0 / 3.0, 1.0, 1.0))
61 self
.assertEqual(Gdk
.RGBA
, gi
.overrides
.Gdk
.RGBA
)
62 rgba
= Gdk
.RGBA(0.1, 0.2, 0.3, 0.4)
63 self
.assertEqual(rgba
, Gdk
.RGBA(0.1, 0.2, 0.3, 0.4))
64 self
.assertNotEqual(rgba
, Gdk
.RGBA(0.0, 0.2, 0.3, 0.4))
65 self
.assertEqual(rgba
.red
, 0.1)
66 self
.assertEqual(rgba
.green
, 0.2)
67 self
.assertEqual(rgba
.blue
, 0.3)
68 self
.assertEqual(rgba
.alpha
, 0.4)
70 self
.assertEqual(rgba
.green
, 0.9)
72 # Iterator/tuple convsersion
73 self
.assertEqual(tuple(Gdk
.RGBA(0.1, 0.2, 0.3, 0.4)),
77 event
= Gdk
.Event
.new(Gdk
.EventType
.CONFIGURE
)
78 self
.assertEqual(event
.type, Gdk
.EventType
.CONFIGURE
)
79 self
.assertEqual(event
.send_event
, 0)
82 event
.type = Gdk
.EventType
.SCROLL
83 self
.assertRaises(AttributeError, lambda: getattr(event
, 'foo_bar'))
85 def test_event_touch(self
):
86 event
= Gdk
.Event
.new(Gdk
.EventType
.TOUCH_BEGIN
)
87 self
.assertEqual(event
.type, Gdk
.EventType
.TOUCH_BEGIN
)
89 # emulating_pointer is unique to touch events
90 self
.assertFalse(event
.emulating_pointer
)
91 self
.assertFalse(event
.touch
.emulating_pointer
)
93 event
.emulating_pointer
= True
94 self
.assertTrue(event
.emulating_pointer
)
95 self
.assertTrue(event
.touch
.emulating_pointer
)
97 def test_event_setattr(self
):
98 event
= Gdk
.Event
.new(Gdk
.EventType
.DRAG_MOTION
)
99 event
.x_root
, event
.y_root
= 0, 5
100 self
.assertEqual(event
.dnd
.x_root
, 0)
101 self
.assertEqual(event
.dnd
.y_root
, 5)
102 self
.assertEqual(event
.x_root
, 0)
103 self
.assertEqual(event
.y_root
, 5)
105 # this used to work, keep it that way
106 self
.assertFalse(hasattr(event
, "foo_bar"))
109 def test_event_repr(self
):
110 event
= Gdk
.Event
.new(Gdk
.EventType
.CONFIGURE
)
111 self
.assertTrue("CONFIGURE" in repr(event
))
113 @unittest.skipIf(Gdk_version
== "4.0", "not in gdk4")
114 def test_event_structures(self
):
115 def button_press_cb(button
, event
):
116 self
.assertTrue(isinstance(event
, Gdk
.EventButton
))
117 self
.assertTrue(event
.type == Gdk
.EventType
.BUTTON_PRESS
)
118 self
.assertEqual(event
.send_event
, 0)
119 self
.assertEqual(event
.get_state(), Gdk
.ModifierType
.CONTROL_MASK
)
120 self
.assertEqual(event
.get_root_coords(), (2, 5))
123 self
.assertEqual(event
.get_time(), 12345)
127 b
.connect('button-press-event', button_press_cb
)
131 Gdk
.test_simulate_button(b
.get_window(),
134 Gdk
.ModifierType
.CONTROL_MASK
,
135 Gdk
.EventType
.BUTTON_PRESS
)
137 @unittest.skipIf(Gdk_version
== "4.0", "not in gdk4")
138 def test_cursor(self
):
139 self
.assertEqual(Gdk
.Cursor
, gi
.overrides
.Gdk
.Cursor
)
140 with
capture_glib_deprecation_warnings():
141 c
= Gdk
.Cursor(Gdk
.CursorType
.WATCH
)
142 self
.assertNotEqual(c
, None)
143 with
capture_glib_deprecation_warnings():
144 c
= Gdk
.Cursor(cursor_type
=Gdk
.CursorType
.WATCH
)
145 self
.assertNotEqual(c
, None)
147 display_manager
= Gdk
.DisplayManager
.get()
148 display
= display_manager
.get_default_display()
150 test_pixbuf
= GdkPixbuf
.Pixbuf
.new(GdkPixbuf
.Colorspace
.RGB
,
156 with
capture_glib_deprecation_warnings() as warn
:
157 c
= Gdk
.Cursor(display
,
160 self
.assertNotEqual(c
, None)
162 self
.assertEqual(len(warn
), 1)
163 self
.assertTrue(issubclass(warn
[0].category
, PyGIDeprecationWarning
))
164 self
.assertRegexpMatches(str(warn
[0].message
),
165 '.*new_from_pixbuf.*')
167 self
.assertRaises(ValueError, Gdk
.Cursor
, 1, 2, 3)
169 def test_flags(self
):
170 self
.assertEqual(Gdk
.ModifierType
.META_MASK |
0, 0x10000000)
171 self
.assertEqual(hex(Gdk
.ModifierType
.META_MASK
), '0x10000000')
172 self
.assertEqual(str(Gdk
.ModifierType
.META_MASK
),
173 '<flags GDK_META_MASK of type Gdk.ModifierType>')
175 self
.assertEqual(Gdk
.ModifierType
.RELEASE_MASK |
0, 0x40000000)
176 self
.assertEqual(hex(Gdk
.ModifierType
.RELEASE_MASK
), '0x40000000')
177 self
.assertEqual(str(Gdk
.ModifierType
.RELEASE_MASK
),
178 '<flags GDK_RELEASE_MASK of type Gdk.ModifierType>')
180 self
.assertEqual(Gdk
.ModifierType
.RELEASE_MASK | Gdk
.ModifierType
.META_MASK
, 0x50000000)
181 self
.assertEqual(str(Gdk
.ModifierType
.RELEASE_MASK | Gdk
.ModifierType
.META_MASK
),
182 '<flags GDK_META_MASK | GDK_RELEASE_MASK of type Gdk.ModifierType>')
184 @unittest.skipIf(Gdk_version
== "4.0", "not in gdk4")
185 def test_color_parse(self
):
186 with
capture_glib_deprecation_warnings():
187 c
= Gdk
.color_parse('#00FF80')
188 self
.assertEqual(c
.red
, 0)
189 self
.assertEqual(c
.green
, 65535)
190 self
.assertEqual(c
.blue
, 32896)
191 self
.assertEqual(Gdk
.color_parse('bogus'), None)
193 @unittest.skipIf(Gdk_version
== "4.0", "not in gdk4")
194 def test_color_representations(self
):
195 # __repr__ should generate a string which is parsable when possible
196 # http://docs.python.org/2/reference/datamodel.html#object.__repr__
197 color
= Gdk
.Color(red
=65535, green
=32896, blue
=1)
198 self
.assertEqual(eval(repr(color
)), color
)
200 rgba
= Gdk
.RGBA(red
=1.0, green
=0.8, blue
=0.6, alpha
=0.4)
201 self
.assertEqual(eval(repr(rgba
)), rgba
)
203 def test_rectangle_functions(self
):
204 # https://bugzilla.gnome.org/show_bug.cgi?id=756364
207 self
.assertTrue(isinstance(Gdk
.rectangle_union(a
, b
), Gdk
.Rectangle
))
208 intersect
, rect
= Gdk
.rectangle_intersect(a
, b
)
209 self
.assertTrue(isinstance(rect
, Gdk
.Rectangle
))
210 self
.assertTrue(isinstance(intersect
, bool))