648597eafaa505ba71e6c4e8af9203024fa3406a
[platform/upstream/pygobject2.git] / tests / test_overrides_gdk.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
3
4 import unittest
5 import warnings
6
7 import gi.overrides
8 from gi import PyGIDeprecationWarning
9
10 try:
11     from gi.repository import Gdk, GdkPixbuf, Gtk
12     Gdk  # pyflakes
13 except ImportError:
14     Gdk = None
15
16
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)
26
27     def test_color(self):
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))
34
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))
38
39         self.assertEqual(Gdk.Color(13107, 21845, 65535).to_floats(),
40                          (0.2, 1.0 / 3.0, 1.0))
41
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))
44
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))
47
48     def test_rgba(self):
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)
57         rgba.green = 0.9
58         self.assertEqual(rgba.green, 0.9)
59
60         # Iterator/tuple convsersion
61         self.assertEqual(tuple(Gdk.RGBA(0.1, 0.2, 0.3, 0.4)),
62                          (0.1, 0.2, 0.3, 0.4))
63
64     def test_event(self):
65         event = Gdk.Event.new(Gdk.EventType.CONFIGURE)
66         self.assertEqual(event.type, Gdk.EventType.CONFIGURE)
67         self.assertEqual(event.send_event, 0)
68
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)
73
74         event = Gdk.Event()
75         event.type = Gdk.EventType.SCROLL
76         self.assertRaises(AttributeError, lambda: getattr(event, 'foo_bar'))
77
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))
85
86             event.time = 12345
87             self.assertEqual(event.get_time(), 12345)
88
89         w = Gtk.Window()
90         b = Gtk.Button()
91         b.connect('button-press-event', button_press_cb)
92         w.add(b)
93         w.show_all()
94         Gdk.test_simulate_button(b.get_window(),
95                                  2, 5,
96                                  0,
97                                  Gdk.ModifierType.CONTROL_MASK,
98                                  Gdk.EventType.BUTTON_PRESS)
99
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)
106
107         display_manager = Gdk.DisplayManager.get()
108         display = display_manager.get_default_display()
109
110         test_pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB,
111                                            False,
112                                            8,
113                                            5,
114                                            10)
115
116         with warnings.catch_warnings(record=True) as warn:
117             warnings.simplefilter('always')
118             c = Gdk.Cursor(display,
119                            test_pixbuf,
120                            y=0, x=0)
121             self.assertNotEqual(c, None)
122
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.*')
127
128         self.assertRaises(ValueError, Gdk.Cursor, 1, 2, 3)
129
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>')
135
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>')
140
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>')
144
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)