| 1 |
import re |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import unittest |
| 5 |
import pytest |
| 6 |
|
| 7 |
import gi |
| 8 |
import gi.overrides |
| 9 |
from gi import PyGIDeprecationWarning |
| 10 |
|
| 11 |
try: |
| 12 |
from gi.repository import Gio, Gdk, GdkPixbuf, Gtk |
| 13 |
|
| 14 |
GDK4 = Gdk._version == "4.0" |
| 15 |
except ImportError: |
| 16 |
Gdk = None |
| 17 |
Gtk = None |
| 18 |
GDK4 = False |
| 19 |
|
| 20 |
|
| 21 |
def gtkver(): |
| 22 |
if Gtk is None: |
| 23 |
return (0, 0, 0) |
| 24 |
return (Gtk.get_major_version(), Gtk.get_minor_version(), Gtk.get_micro_version()) |
| 25 |
|
| 26 |
|
| 27 |
try: |
| 28 |
gi.require_foreign("cairo") |
| 29 |
has_cairo = True |
| 30 |
except ImportError: |
| 31 |
has_cairo = False |
| 32 |
|
| 33 |
from .helper import capture_glib_deprecation_warnings |
| 34 |
|
| 35 |
|
| 36 |
@unittest.skipUnless(Gdk, "Gdk not available") |
| 37 |
class TestGdk(unittest.TestCase): |
| 38 |
@unittest.skipIf(sys.platform == "darwin" or os.name == "nt", "crashes") |
| 39 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 40 |
def test_constructor(self): |
| 41 |
attribute = Gdk.WindowAttr() |
| 42 |
attribute.window_type = Gdk.WindowType.CHILD |
| 43 |
attributes_mask = Gdk.WindowAttributesType.X | Gdk.WindowAttributesType.Y |
| 44 |
window = Gdk.Window(None, attribute, attributes_mask) |
| 45 |
self.assertEqual(window.get_window_type(), Gdk.WindowType.CHILD) |
| 46 |
|
| 47 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 48 |
def test_color(self): |
| 49 |
color = Gdk.Color(100, 200, 300) |
| 50 |
self.assertEqual(color.red, 100) |
| 51 |
self.assertEqual(color.green, 200) |
| 52 |
self.assertEqual(color.blue, 300) |
| 53 |
with capture_glib_deprecation_warnings(): |
| 54 |
self.assertEqual(color, Gdk.Color(100, 200, 300)) |
| 55 |
self.assertNotEqual(color, Gdk.Color(1, 2, 3)) |
| 56 |
self.assertNotEqual(color, None) |
| 57 |
# assertNotEqual only tests __ne__. Following line explicitly |
| 58 |
# tests __eq__ with objects of other types |
| 59 |
self.assertFalse(color == object()) |
| 60 |
|
| 61 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 62 |
def test_color_floats(self): |
| 63 |
self.assertEqual( |
| 64 |
Gdk.Color(13107, 21845, 65535), Gdk.Color.from_floats(0.2, 1.0 / 3.0, 1.0) |
| 65 |
) |
| 66 |
|
| 67 |
self.assertEqual( |
| 68 |
Gdk.Color(13107, 21845, 65535).to_floats(), (0.2, 1.0 / 3.0, 1.0) |
| 69 |
) |
| 70 |
|
| 71 |
self.assertEqual( |
| 72 |
Gdk.RGBA(0.2, 1.0 / 3.0, 1.0, 0.5).to_color(), |
| 73 |
Gdk.Color.from_floats(0.2, 1.0 / 3.0, 1.0), |
| 74 |
) |
| 75 |
|
| 76 |
self.assertEqual( |
| 77 |
Gdk.RGBA.from_color(Gdk.Color(13107, 21845, 65535)), |
| 78 |
Gdk.RGBA(0.2, 1.0 / 3.0, 1.0, 1.0), |
| 79 |
) |
| 80 |
|
| 81 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 82 |
def test_color_to_floats_attrs(self): |
| 83 |
color = Gdk.Color(13107, 21845, 65535) |
| 84 |
assert color.red_float == 0.2 |
| 85 |
color.red_float = 0 |
| 86 |
assert color.red_float == 0 |
| 87 |
assert color.green_float == 1.0 / 3.0 |
| 88 |
color.green_float = 0 |
| 89 |
assert color.green_float == 0 |
| 90 |
assert color.blue_float == 1.0 |
| 91 |
color.blue_float = 0 |
| 92 |
assert color.blue_float == 0 |
| 93 |
|
| 94 |
def test_rgba(self): |
| 95 |
self.assertEqual(Gdk.RGBA, gi.overrides.Gdk.RGBA) |
| 96 |
rgba = Gdk.RGBA(0.1, 0.2, 0.3, 0.4) |
| 97 |
self.assertEqual(rgba, Gdk.RGBA(0.1, 0.2, 0.3, 0.4)) |
| 98 |
self.assertNotEqual(rgba, Gdk.RGBA(0.0, 0.2, 0.3, 0.4)) |
| 99 |
self.assertAlmostEqual(rgba.red, 0.1) |
| 100 |
self.assertAlmostEqual(rgba.green, 0.2) |
| 101 |
self.assertAlmostEqual(rgba.blue, 0.3) |
| 102 |
self.assertAlmostEqual(rgba.alpha, 0.4) |
| 103 |
rgba.green = 0.9 |
| 104 |
self.assertAlmostEqual(rgba.green, 0.9) |
| 105 |
self.assertNotEqual(rgba, None) |
| 106 |
# assertNotEqual only tests __ne__. Following line explicitly |
| 107 |
# tests __eq__ with objects of other types |
| 108 |
self.assertFalse(rgba == object()) |
| 109 |
|
| 110 |
# Iterator/tuple convsersion |
| 111 |
assert tuple(Gdk.RGBA(0.1, 0.2, 0.3, 0.4)) == pytest.approx( |
| 112 |
(0.1, 0.2, 0.3, 0.4) |
| 113 |
) |
| 114 |
|
| 115 |
@unittest.skipUnless(GDK4, "only in gdk4") |
| 116 |
def test_rgba_gtk4(self): |
| 117 |
c = Gdk.RGBA(0, 0, 0, 0) |
| 118 |
assert c.to_string() == "rgba(0,0,0,0)" |
| 119 |
|
| 120 |
@unittest.skipIf(not has_cairo or GDK4, "not in gdk4") |
| 121 |
def test_window(self): |
| 122 |
w = Gtk.Window() |
| 123 |
w.realize() |
| 124 |
window = w.get_window() |
| 125 |
with capture_glib_deprecation_warnings(): |
| 126 |
assert window.cairo_create() is not None |
| 127 |
|
| 128 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 129 |
def test_drag_context(self): |
| 130 |
context = Gdk.DragContext() |
| 131 |
# using it this way crashes.. |
| 132 |
assert hasattr(context, "finish") |
| 133 |
|
| 134 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 135 |
def test_event(self): |
| 136 |
event = Gdk.Event.new(Gdk.EventType.CONFIGURE) |
| 137 |
self.assertEqual(event.type, Gdk.EventType.CONFIGURE) |
| 138 |
self.assertEqual(event.send_event, 0) |
| 139 |
|
| 140 |
event = Gdk.Event() |
| 141 |
event.type = Gdk.EventType.SCROLL |
| 142 |
self.assertRaises(AttributeError, lambda: getattr(event, "foo_bar")) |
| 143 |
|
| 144 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 145 |
def test_scroll_event(self): |
| 146 |
event = Gdk.Event.new(Gdk.EventType.SCROLL) |
| 147 |
assert event.direction == Gdk.ScrollDirection.UP |
| 148 |
|
| 149 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 150 |
def test_event_strip_boolean(self): |
| 151 |
ev = Gdk.EventButton() |
| 152 |
ev.type = Gdk.EventType.BUTTON_PRESS |
| 153 |
assert ev.get_coords() == (0.0, 0.0) |
| 154 |
|
| 155 |
# https://gitlab.gnome.org/GNOME/pygobject/issues/85 |
| 156 |
ev = Gdk.Event.new(Gdk.EventType.BUTTON_PRESS) |
| 157 |
assert ev.get_coords() == (True, 0.0, 0.0) |
| 158 |
|
| 159 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 160 |
def test_event_touch(self): |
| 161 |
event = Gdk.Event.new(Gdk.EventType.TOUCH_BEGIN) |
| 162 |
self.assertEqual(event.type, Gdk.EventType.TOUCH_BEGIN) |
| 163 |
|
| 164 |
# emulating_pointer is unique to touch events |
| 165 |
self.assertFalse(event.emulating_pointer) |
| 166 |
self.assertFalse(event.touch.emulating_pointer) |
| 167 |
|
| 168 |
event.emulating_pointer = True |
| 169 |
self.assertTrue(event.emulating_pointer) |
| 170 |
self.assertTrue(event.touch.emulating_pointer) |
| 171 |
|
| 172 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 173 |
def test_event_setattr(self): |
| 174 |
event = Gdk.Event.new(Gdk.EventType.DRAG_MOTION) |
| 175 |
event.x_root, event.y_root = 0, 5 |
| 176 |
self.assertEqual(event.dnd.x_root, 0) |
| 177 |
self.assertEqual(event.dnd.y_root, 5) |
| 178 |
self.assertEqual(event.x_root, 0) |
| 179 |
self.assertEqual(event.y_root, 5) |
| 180 |
|
| 181 |
# this used to work, keep it that way |
| 182 |
self.assertFalse(hasattr(event, "foo_bar")) |
| 183 |
event.foo_bar = 42 |
| 184 |
|
| 185 |
# unhandled type |
| 186 |
event.type = Gdk.EventType.EVENT_LAST |
| 187 |
with pytest.raises(AttributeError): |
| 188 |
event.foo_bar |
| 189 |
event.foo_bar = 42 |
| 190 |
assert event.foo_bar == 42 |
| 191 |
|
| 192 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 193 |
def test_event_repr(self): |
| 194 |
event = Gdk.Event.new(Gdk.EventType.CONFIGURE) |
| 195 |
self.assertTrue("CONFIGURE" in repr(event)) |
| 196 |
|
| 197 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 198 |
def test_event_structures(self): |
| 199 |
def button_press_cb(button, event): |
| 200 |
self.assertTrue(isinstance(event, Gdk.EventButton)) |
| 201 |
self.assertTrue(event.type == Gdk.EventType.BUTTON_PRESS) |
| 202 |
self.assertEqual(event.send_event, 0) |
| 203 |
self.assertEqual(event.get_state(), Gdk.ModifierType.CONTROL_MASK) |
| 204 |
self.assertEqual(event.get_root_coords(), (2, 5)) |
| 205 |
|
| 206 |
event.time = 12345 |
| 207 |
self.assertEqual(event.get_time(), 12345) |
| 208 |
|
| 209 |
w = Gtk.Window() |
| 210 |
b = Gtk.Button() |
| 211 |
b.connect("button-press-event", button_press_cb) |
| 212 |
w.add(b) |
| 213 |
b.show() |
| 214 |
b.realize() |
| 215 |
Gdk.test_simulate_button( |
| 216 |
b.get_window(), |
| 217 |
2, |
| 218 |
5, |
| 219 |
0, |
| 220 |
Gdk.ModifierType.CONTROL_MASK, |
| 221 |
Gdk.EventType.BUTTON_PRESS, |
| 222 |
) |
| 223 |
|
| 224 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 225 |
def test_cursor(self): |
| 226 |
self.assertEqual(Gdk.Cursor, gi.overrides.Gdk.Cursor) |
| 227 |
with capture_glib_deprecation_warnings(): |
| 228 |
c = Gdk.Cursor(Gdk.CursorType.WATCH) |
| 229 |
self.assertNotEqual(c, None) |
| 230 |
with capture_glib_deprecation_warnings(): |
| 231 |
c = Gdk.Cursor(cursor_type=Gdk.CursorType.WATCH) |
| 232 |
self.assertNotEqual(c, None) |
| 233 |
|
| 234 |
display_manager = Gdk.DisplayManager.get() |
| 235 |
display = display_manager.get_default_display() |
| 236 |
|
| 237 |
test_pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, 5, 10) |
| 238 |
|
| 239 |
with capture_glib_deprecation_warnings() as warn: |
| 240 |
c = Gdk.Cursor(display, test_pixbuf, y=0, x=0) |
| 241 |
self.assertNotEqual(c, None) |
| 242 |
|
| 243 |
self.assertEqual(len(warn), 1) |
| 244 |
self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning)) |
| 245 |
self.assertRegex(str(warn[0].message), ".*new_from_pixbuf.*") |
| 246 |
|
| 247 |
self.assertRaises(ValueError, Gdk.Cursor, 1, 2, 3) |
| 248 |
|
| 249 |
with capture_glib_deprecation_warnings() as warn: |
| 250 |
c = Gdk.Cursor(display, Gdk.CursorType.WATCH) |
| 251 |
assert len(warn) == 1 |
| 252 |
# on macOS the type is switched to PIXMAP behind the scenes |
| 253 |
assert c.props.cursor_type in ( |
| 254 |
Gdk.CursorType.WATCH, |
| 255 |
Gdk.CursorType.CURSOR_IS_PIXMAP, |
| 256 |
) |
| 257 |
assert c.props.display == display |
| 258 |
|
| 259 |
@unittest.skipUnless(GDK4, "only gdk4") |
| 260 |
def test_cursor_gdk4(self): |
| 261 |
Gdk.Cursor() |
| 262 |
Gdk.Cursor(name="foo") |
| 263 |
Gdk.Cursor(fallback=Gdk.Cursor()) |
| 264 |
|
| 265 |
def test_flags(self): |
| 266 |
self.assertEqual(Gdk.ModifierType.META_MASK | 0, 0x10000000) |
| 267 |
self.assertEqual(hex(Gdk.ModifierType.META_MASK), "0x10000000") |
| 268 |
self.assertEqual( |
| 269 |
repr(Gdk.ModifierType.META_MASK), "<ModifierType.META_MASK: 268435456>" |
| 270 |
) |
| 271 |
|
| 272 |
# RELEASE_MASK does not exist in gdk4 |
| 273 |
if not GDK4: |
| 274 |
self.assertEqual(Gdk.ModifierType.RELEASE_MASK | 0, 0x40000000) |
| 275 |
self.assertEqual(hex(Gdk.ModifierType.RELEASE_MASK), "0x40000000") |
| 276 |
self.assertEqual( |
| 277 |
repr(Gdk.ModifierType.RELEASE_MASK), |
| 278 |
"<ModifierType.RELEASE_MASK: 1073741824>", |
| 279 |
) |
| 280 |
|
| 281 |
self.assertEqual( |
| 282 |
Gdk.ModifierType.RELEASE_MASK | Gdk.ModifierType.META_MASK, 0x50000000 |
| 283 |
) |
| 284 |
self.assertIn( |
| 285 |
repr(Gdk.ModifierType.RELEASE_MASK | Gdk.ModifierType.META_MASK), |
| 286 |
{ |
| 287 |
"<ModifierType.META_MASK|RELEASE_MASK: 1342177280>", |
| 288 |
"<ModifierType.RELEASE_MASK|META_MASK: 1342177280>", |
| 289 |
}, |
| 290 |
) |
| 291 |
|
| 292 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 293 |
def test_color_parse(self): |
| 294 |
with capture_glib_deprecation_warnings(): |
| 295 |
c = Gdk.color_parse("#00FF80") |
| 296 |
self.assertEqual(c.red, 0) |
| 297 |
self.assertEqual(c.green, 65535) |
| 298 |
self.assertEqual(c.blue, 32896) |
| 299 |
self.assertEqual(Gdk.color_parse("bogus"), None) |
| 300 |
|
| 301 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 302 |
def test_color_representations(self): |
| 303 |
# __repr__ should generate a string which is parsable when possible |
| 304 |
# http://docs.python.org/2/reference/datamodel.html#object.__repr__ |
| 305 |
color = Gdk.Color(red=65535, green=32896, blue=1) |
| 306 |
self.assertEqual(eval(repr(color)), color) |
| 307 |
|
| 308 |
def test_rgba_representations(self): |
| 309 |
rgba = Gdk.RGBA(red=1.0, green=0.8, blue=0.6, alpha=0.4) |
| 310 |
self.assertEqual(eval(repr(rgba)), rgba) |
| 311 |
|
| 312 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 313 |
def test_rectangle_functions(self): |
| 314 |
# https://bugzilla.gnome.org/show_bug.cgi?id=756364 |
| 315 |
a = Gdk.Rectangle() |
| 316 |
b = Gdk.Rectangle() |
| 317 |
self.assertTrue(isinstance(Gdk.rectangle_union(a, b), Gdk.Rectangle)) |
| 318 |
intersect, rect = Gdk.rectangle_intersect(a, b) |
| 319 |
self.assertTrue(isinstance(rect, Gdk.Rectangle)) |
| 320 |
self.assertTrue(isinstance(intersect, bool)) |
| 321 |
|
| 322 |
@unittest.skipIf(GDK4, "not in gdk4") |
| 323 |
def test_atom_repr_str(self): |
| 324 |
atom = Gdk.atom_intern("", True) |
| 325 |
assert re.match(r"<Gdk.Atom\(\d+\)>", repr(atom)) |
| 326 |
assert re.match(r"Gdk.Atom<\d+>", str(atom)) |
| 327 |
|
| 328 |
@unittest.skipUnless(GDK4, "only in gdk4") |
| 329 |
@unittest.skipUnless(gtkver() >= (4, 8, 0), "constructor available since 4.8") |
| 330 |
def test_file_list(self): |
| 331 |
f = Gio.File.new_for_path("/tmp") |
| 332 |
filelist = Gdk.FileList([f]) |
| 333 |
self.assertTrue(isinstance(filelist, Gdk.FileList)) |
| 334 |
self.assertEqual(len(filelist), 1) |
| 335 |
self.assertEqual(filelist[0], f) |
| 336 |
|
| 337 |
@unittest.skipUnless(GDK4, "only in gdk4") |
| 338 |
@unittest.skipUnless(gtkver() >= (4, 21, 0), "available since 4.21") |
| 339 |
def test_paintable_flags(self): |
| 340 |
self.assertEqual(Gdk.PaintableFlags.SIZE, Gdk.PaintableFlags.STATIC_SIZE) |
| 341 |
self.assertEqual( |
| 342 |
Gdk.PaintableFlags.CONTENTS, Gdk.PaintableFlags.STATIC_CONTENTS |
| 343 |
) |