| 1 |
import os |
| 2 |
import io |
| 3 |
import tempfile |
| 4 |
|
| 5 |
import cairo |
| 6 |
import pytest |
| 7 |
|
| 8 |
|
| 9 |
def test_context_manager(): |
| 10 |
f = io.BytesIO() |
| 11 |
with cairo.ScriptDevice(f) as dev: |
| 12 |
dev.acquire() |
| 13 |
dev.release() |
| 14 |
|
| 15 |
with pytest.raises(cairo.Error) as excinfo: |
| 16 |
dev.acquire() |
| 17 |
assert excinfo.value.status == cairo.Status.DEVICE_FINISHED |
| 18 |
|
| 19 |
|
| 20 |
def test_cmp_hash(): |
| 21 |
f = io.BytesIO() |
| 22 |
dev = cairo.ScriptDevice(f) |
| 23 |
surface = cairo.ScriptSurface(dev, cairo.Content.COLOR_ALPHA, 42, 10) |
| 24 |
other = surface.get_device() |
| 25 |
assert dev == other |
| 26 |
assert not dev != other |
| 27 |
assert hash(dev) == hash(other) |
| 28 |
assert dev != object() |
| 29 |
|
| 30 |
|
| 31 |
def test_get_device(): |
| 32 |
surface = cairo.ImageSurface(cairo.Format.ARGB32, 10, 10) |
| 33 |
assert surface.get_device() is None |
| 34 |
|
| 35 |
|
| 36 |
def test_type(): |
| 37 |
assert cairo.Device |
| 38 |
with pytest.raises(TypeError): |
| 39 |
cairo.Device() |
| 40 |
|
| 41 |
|
| 42 |
def test_has(): |
| 43 |
assert hasattr(cairo, "HAS_SCRIPT_SURFACE") |
| 44 |
|
| 45 |
|
| 46 |
def test_script_device(): |
| 47 |
dev = cairo.ScriptDevice(io.BytesIO()) |
| 48 |
assert dev |
| 49 |
assert issubclass(cairo.ScriptDevice, cairo.Device) |
| 50 |
assert isinstance(dev, cairo.ScriptDevice) |
| 51 |
|
| 52 |
with pytest.raises(TypeError): |
| 53 |
cairo.ScriptDevice(None) |
| 54 |
|
| 55 |
with pytest.raises(TypeError): |
| 56 |
cairo.ScriptDevice() |
| 57 |
|
| 58 |
with pytest.raises(TypeError): |
| 59 |
cairo.ScriptDevice(io.StringIO()) |
| 60 |
|
| 61 |
with pytest.raises((ValueError, TypeError)): |
| 62 |
cairo.ScriptDevice("\x00") |
| 63 |
|
| 64 |
|
| 65 |
def test_script_device_mode(): |
| 66 |
assert hasattr(cairo, "ScriptMode") |
| 67 |
assert cairo.ScriptMode.ASCII != cairo.ScriptMode.BINARY |
| 68 |
|
| 69 |
dev = cairo.ScriptDevice(io.BytesIO()) |
| 70 |
mode = dev.get_mode() |
| 71 |
assert isinstance(mode, cairo.ScriptMode) |
| 72 |
assert mode == cairo.ScriptMode.ASCII |
| 73 |
dev.set_mode(cairo.ScriptMode.BINARY) |
| 74 |
assert dev.get_mode() == cairo.ScriptMode.BINARY |
| 75 |
with pytest.raises(TypeError): |
| 76 |
dev.set_mode(object()) |
| 77 |
|
| 78 |
|
| 79 |
def test_script_device_write_comment(): |
| 80 |
f = io.BytesIO() |
| 81 |
dev = cairo.ScriptDevice(f) |
| 82 |
dev.write_comment("pycairo foo") |
| 83 |
dev.write_comment("pycairo bar") |
| 84 |
dev.flush() |
| 85 |
assert b"pycairo foo" in f.getvalue() |
| 86 |
assert b"pycairo bar" in f.getvalue() |
| 87 |
with pytest.raises(TypeError): |
| 88 |
dev.write_comment(object()) |
| 89 |
|
| 90 |
|
| 91 |
def test_from_recording_surface(): |
| 92 |
s = cairo.RecordingSurface(cairo.CONTENT_COLOR, None) |
| 93 |
ctx = cairo.Context(s) |
| 94 |
ctx.paint() |
| 95 |
f = io.BytesIO() |
| 96 |
dev = cairo.ScriptDevice(f) |
| 97 |
dev.from_recording_surface(s) |
| 98 |
dev.flush() |
| 99 |
assert b"paint" in f.getvalue() |
| 100 |
|
| 101 |
|
| 102 |
dev.finish() |
| 103 |
with pytest.raises(cairo.Error): |
| 104 |
dev.from_recording_surface(s) |
| 105 |
|
| 106 |
|
| 107 |
image = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10) |
| 108 |
with pytest.raises(TypeError): |
| 109 |
dev.from_recording_surface(image) |
| 110 |
|
| 111 |
|
| 112 |
with pytest.raises(TypeError): |
| 113 |
dev.from_recording_surface(None) |
| 114 |
|
| 115 |
|
| 116 |
def test_device_acquire(): |
| 117 |
f = io.BytesIO() |
| 118 |
dev = cairo.ScriptDevice(f) |
| 119 |
dev.acquire() |
| 120 |
dev.release() |
| 121 |
|
| 122 |
|
| 123 |
def test_script_device_to_path(): |
| 124 |
fd, fname = tempfile.mkstemp() |
| 125 |
os.close(fd) |
| 126 |
try: |
| 127 |
cairo.ScriptDevice(fname).finish() |
| 128 |
finally: |
| 129 |
os.unlink(fname) |