1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
8 from gi import PyGIDeprecationWarning
11 from gi.repository import Gdk, GdkPixbuf, Gtk
17 @unittest.skipUnless(Gdk, 'Gdk not available')
18 class TestGdk(unittest.TestCase):
19 def test_constructor(self):
20 attribute = Gdk.WindowAttr()
21 attribute.window_type = Gdk.WindowType.CHILD
22 attributes_mask = Gdk.WindowAttributesType.X | \
23 Gdk.WindowAttributesType.Y
24 window = Gdk.Window(None, attribute, attributes_mask)
25 self.assertEqual(window.get_window_type(), Gdk.WindowType.CHILD)
28 color = Gdk.Color(100, 200, 300)
29 self.assertEqual(color.red, 100)
30 self.assertEqual(color.green, 200)
31 self.assertEqual(color.blue, 300)
32 self.assertEqual(color, Gdk.Color(100, 200, 300))
33 self.assertNotEqual(color, Gdk.Color(1, 2, 3))
35 def test_color_floats(self):
36 self.assertEqual(Gdk.Color(13107, 21845, 65535),
37 Gdk.Color.from_floats(0.2, 1.0 / 3.0, 1.0))
39 self.assertEqual(Gdk.Color(13107, 21845, 65535).to_floats(),
40 (0.2, 1.0 / 3.0, 1.0))
42 self.assertEqual(Gdk.RGBA(0.2, 1.0 / 3.0, 1.0, 0.5).to_color(),
43 Gdk.Color.from_floats(0.2, 1.0 / 3.0, 1.0))
45 self.assertEqual(Gdk.RGBA.from_color(Gdk.Color(13107, 21845, 65535)),
46 Gdk.RGBA(0.2, 1.0 / 3.0, 1.0, 1.0))
49 self.assertEqual(Gdk.RGBA, gi.overrides.Gdk.RGBA)
50 rgba = Gdk.RGBA(0.1, 0.2, 0.3, 0.4)
51 self.assertEqual(rgba, Gdk.RGBA(0.1, 0.2, 0.3, 0.4))
52 self.assertNotEqual(rgba, Gdk.RGBA(0.0, 0.2, 0.3, 0.4))
53 self.assertEqual(rgba.red, 0.1)
54 self.assertEqual(rgba.green, 0.2)
55 self.assertEqual(rgba.blue, 0.3)
56 self.assertEqual(rgba.alpha, 0.4)
58 self.assertEqual(rgba.green, 0.9)
60 # Iterator/tuple convsersion
61 self.assertEqual(tuple(Gdk.RGBA(0.1, 0.2, 0.3, 0.4)),
65 event = Gdk.Event.new(Gdk.EventType.CONFIGURE)
66 self.assertEqual(event.type, Gdk.EventType.CONFIGURE)
67 self.assertEqual(event.send_event, 0)
69 event = Gdk.Event.new(Gdk.EventType.DRAG_MOTION)
70 event.x_root, event.y_root = 0, 5
71 self.assertEqual(event.x_root, 0)
72 self.assertEqual(event.y_root, 5)
75 event.type = Gdk.EventType.SCROLL
76 self.assertRaises(AttributeError, lambda: getattr(event, 'foo_bar'))
78 def test_event_structures(self):
79 def button_press_cb(button, event):
80 self.assertTrue(isinstance(event, Gdk.EventButton))
81 self.assertTrue(event.type == Gdk.EventType.BUTTON_PRESS)
82 self.assertEqual(event.send_event, 0)
83 self.assertEqual(event.get_state(), Gdk.ModifierType.CONTROL_MASK)
84 self.assertEqual(event.get_root_coords(), (2, 5))
87 self.assertEqual(event.get_time(), 12345)
91 b.connect('button-press-event', button_press_cb)
94 Gdk.test_simulate_button(b.get_window(),
97 Gdk.ModifierType.CONTROL_MASK,
98 Gdk.EventType.BUTTON_PRESS)
100 def test_cursor(self):
101 self.assertEqual(Gdk.Cursor, gi.overrides.Gdk.Cursor)
102 c = Gdk.Cursor(Gdk.CursorType.WATCH)
103 self.assertNotEqual(c, None)
104 c = Gdk.Cursor(cursor_type=Gdk.CursorType.WATCH)
105 self.assertNotEqual(c, None)
107 display_manager = Gdk.DisplayManager.get()
108 display = display_manager.get_default_display()
110 test_pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB,
116 with warnings.catch_warnings(record=True) as warn:
117 warnings.simplefilter('always')
118 c = Gdk.Cursor(display,
121 self.assertNotEqual(c, None)
123 self.assertEqual(len(warn), 1)
124 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
125 self.assertRegexpMatches(str(warn[0].message),
126 '.*new_from_pixbuf.*')
128 self.assertRaises(ValueError, Gdk.Cursor, 1, 2, 3)
130 def test_flags(self):
131 self.assertEqual(Gdk.ModifierType.META_MASK | 0, 0x10000000)
132 self.assertEqual(hex(Gdk.ModifierType.META_MASK), '0x10000000')
133 self.assertEqual(str(Gdk.ModifierType.META_MASK),
134 '<flags GDK_META_MASK of type GdkModifierType>')
136 self.assertEqual(Gdk.ModifierType.RELEASE_MASK | 0, 0x40000000)
137 self.assertEqual(hex(Gdk.ModifierType.RELEASE_MASK), '0x40000000')
138 self.assertEqual(str(Gdk.ModifierType.RELEASE_MASK),
139 '<flags GDK_RELEASE_MASK of type GdkModifierType>')
141 self.assertEqual(Gdk.ModifierType.RELEASE_MASK | Gdk.ModifierType.META_MASK, 0x50000000)
142 self.assertEqual(str(Gdk.ModifierType.RELEASE_MASK | Gdk.ModifierType.META_MASK),
143 '<flags GDK_META_MASK | GDK_RELEASE_MASK of type GdkModifierType>')
145 def test_color_parse(self):
146 c = Gdk.color_parse('#00FF80')
147 self.assertEqual(c.red, 0)
148 self.assertEqual(c.green, 65535)
149 self.assertEqual(c.blue, 32896)
150 self.assertEqual(Gdk.color_parse('bogus'), None)