/usr/lib/python3/dist-packages/cairocffi/test_xcb.py is in python3-cairocffi 0.7.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | # coding: utf8
"""
cairocffi.test_xcb
~~~~~~~~~~~~~~~~~~
Test suite for cairocffi.xcb.
:copyright: Copyright 2014 by Simon Sapin
:license: BSD, see LICENSE for details.
"""
import os
import time
import pytest
xcffib = pytest.importorskip('xcffib')
import xcffib.xproto
from xcffib.xproto import ConfigWindow, CW, EventMask, GC
from . import Context, XCBSurface, cairo_version
@pytest.fixture
def xcb_conn(request):
"""
Fixture that will setup and take down a xcffib.Connection object running on
a display spawned by xvfb
"""
display = os.environ.get('DISPLAY')
if display:
conn = xcffib.connect(display)
def teardown_conn():
conn.disconnect()
else:
pytest.skip('DISPLAY environment variable not set')
request.addfinalizer(teardown_conn)
return conn
def find_root_visual(conn):
"""Find the xcffib.xproto.VISUALTYPE corresponding to the root visual"""
default_screen = conn.setup.roots[conn.pref_screen]
for i in default_screen.allowed_depths:
for v in i.visuals:
if v.visual_id == default_screen.root_visual:
return v
def create_window(conn, width, height):
"""Creates a window of the given dimensions and returns the XID"""
wid = conn.generate_id()
default_screen = conn.setup.roots[conn.pref_screen]
conn.core.CreateWindow(
default_screen.root_depth, # depth
wid, # id
default_screen.root, # parent
0, 0, width, height, 0, # x, y, w, h, border width
xcffib.xproto.WindowClass.InputOutput, # window class
default_screen.root_visual, # visual
CW.BackPixel | CW.EventMask, # value mask
[ # value list
default_screen.black_pixel,
EventMask.Exposure | EventMask.StructureNotify
]
)
return wid
def create_pixmap(conn, wid, width, height):
"""Creates a window of the given dimensions and returns the XID"""
pixmap = conn.generate_id()
default_screen = conn.setup.roots[conn.pref_screen]
conn.core.CreatePixmap(
default_screen.root_depth, # depth
pixmap, wid, # pixmap id, drawable id (window)
width, height
)
return pixmap
def remove_pixmap(conn, pixmap):
conn.core.FreePixmap(pixmap)
def create_gc(conn):
"""Creates a simple graphics context"""
gc = conn.generate_id()
default_screen = conn.setup.roots[conn.pref_screen]
conn.core.CreateGC(
gc, default_screen.root, # gc id, drawable
GC.Foreground | GC.Background, # value mask
[ # value list
default_screen.black_pixel,
default_screen.white_pixel
]
)
return gc
def remove_gc(conn, gc):
conn.core.FreeGC(gc)
def test_xcb_pixmap(xcb_conn):
width = 10
height = 10
# create a new window
wid = create_window(xcb_conn, width, height)
# create the pixmap used to draw with cairo
pixmap = create_pixmap(xcb_conn, wid, width, height)
# create graphics context to copy pixmap on window
gc = create_gc(xcb_conn)
# create XCB surface on pixmap
root_visual = find_root_visual(xcb_conn)
surface = XCBSurface(xcb_conn, pixmap, root_visual, width, height)
assert surface
# use xcb surface to create context, draw white
ctx = Context(surface)
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
# map the window and wait for it to appear
xcb_conn.core.MapWindow(wid)
xcb_conn.flush()
start = time.time()
while time.time() < start + 10:
event = xcb_conn.wait_for_event()
if isinstance(event, xcffib.xproto.ExposeEvent):
break
else:
pytest.fail("Never received ExposeEvent")
# copy the pixmap to the window
xcb_conn.core.CopyArea(
pixmap, # source
wid, # dest
gc, # gc
0, 0, # source x, source y
0, 0, # dest x, dest y
width, height
)
ctx = None
surface = None
remove_gc(xcb_conn, gc)
remove_pixmap(xcb_conn, pixmap)
# flush the connection, make sure no errors were thrown
xcb_conn.flush()
while event:
event = xcb_conn.poll_for_event()
@pytest.mark.xfail(cairo_version() < 11200,
reason="Cairo version too low")
def test_xcb_window(xcb_conn):
width = 10
height = 10
# create a new window used to draw with cairo
wid = create_window(xcb_conn, width, height)
# map the window and wait for it to appear
xcb_conn.core.MapWindow(wid)
xcb_conn.flush()
start = time.time()
while time.time() < start + 10:
event = xcb_conn.wait_for_event()
if isinstance(event, xcffib.xproto.ExposeEvent):
break
else:
pytest.fail("Never received ExposeEvent")
# create XCB surface on window
root_visual = find_root_visual(xcb_conn)
surface = XCBSurface(xcb_conn, wid, root_visual, width, height)
assert surface
# use xcb surface to create context, draw white
ctx = Context(surface)
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
xcb_conn.flush()
# now move the window and change its size
xcb_conn.core.ConfigureWindow(
wid,
(ConfigWindow.X | ConfigWindow.Y
| ConfigWindow.Width | ConfigWindow.Height),
[
5, 5, # x, y
width * 2, height * 2 # width, height
]
)
xcb_conn.flush()
# wait for the notification of the size change
start = time.time()
while time.time() < start + 10:
event = xcb_conn.wait_for_event()
if isinstance(event, xcffib.xproto.ConfigureNotifyEvent):
assert event.width == 2*width
assert event.height == 2*height
width = event.width
height = event.height
break
else:
pytest.fail("Never received ConfigureNotifyEvent")
# re-size and re-draw the surface
surface.set_size(width, height)
ctx = Context(surface)
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
ctx = None
surface = None
# flush the connection, make sure no errors were thrown
xcb_conn.flush()
while event:
event = xcb_conn.poll_for_event()
|