summaryrefslogtreecommitdiff
path: root/test/api_test.py
blob: 86317b2e4e4755790fb7207e4dae9ddac3f9389e (plain)
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
'''test pycairo API
- can be expanded later as required.
- is not able to test the quality of images created. We assume cairo itself
  tests for this.
'''
import tempfile as tfi

import cairo
import py.test as test


def test_context():
  if cairo.HAS_IMAGE_SURFACE:
    f, w, h = cairo.FORMAT_ARGB32, 100, 100
    s = cairo.ImageSurface(f, w, h)
    ctx = cairo.Context(s)
    ctx.set_source_rgb(1.0, 1.0, 1.0)
    ctx.set_operator(cairo.OPERATOR_SOURCE)
    ctx.paint()


def test_matrix():
  m = cairo.Matrix()
  m.rotate(10)
  m.scale(1.5, 2.5)
  m.translate(10, 20)


def test_path():
  # AttributeError: 'module' object has no attribute 'Path'
  test.raises(AttributeError, "p = cairo.Path()")
  # see examples/warpedtext.py


def test_pattern():
  # TypeError: The Pattern type cannot be instantiated
  test.raises(TypeError, "p = cairo.Pattern()")

  r,g,b,a = 0.1, 0.2, 0.3, 0.4
  p = cairo.SolidPattern(r,g,b,a)
  assert p.get_rgba() == (r,g,b,a)

  # SurfacePattern

  # TypeError: The Gradient type cannot be instantiated
  test.raises(TypeError, "p = cairo.Gradient()")

  x0,y0,x1,y1 = 0.0, 0.0, 0.0, 1.0
  p = cairo.LinearGradient(x0,y0,x1,y1)
  assert p.get_linear_points() == (x0,y0,x1,y1)
  p.add_color_stop_rgba(1, 0, 0, 0, 1)
  p.add_color_stop_rgba(0, 1, 1, 1, 1)

  cx0, cy0, radius0, cx1, cy1, radius1 = 1.0, 1.0, 1.0, 2.0, 2.0, 1.0
  p = cairo.RadialGradient(cx0, cy0, radius0, cx1, cy1, radius1)
  assert p.get_radial_circles() == (cx0, cy0, radius0, cx1, cy1, radius1)
  p.add_color_stop_rgba(0, 1, 1, 1, 1)
  p.add_color_stop_rgba(1, 0, 0, 0, 1)


def test_surface():
  # TypeError: The Surface type cannot be instantiated
  test.raises(TypeError, "s = cairo.Surface()")

  if cairo.HAS_IMAGE_SURFACE:
    f, w, h = cairo.FORMAT_ARGB32, 100, 100
    s = cairo.ImageSurface(f, w, h)
    assert s.get_format() == f
    assert s.get_width() == w
    assert s.get_height() == h

  if cairo.HAS_PDF_SURFACE:
    f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
    s = cairo.PDFSurface(f, w, h)

  if cairo.HAS_PS_SURFACE:
    f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
    s = cairo.PSSurface(f, w, h)

  if cairo.HAS_RECORDING_SURFACE:
    s = cairo.RecordingSurface(cairo.CONTENT_COLOR, None)
    s = cairo.RecordingSurface(cairo.CONTENT_COLOR, (1,1,10,10))

  if cairo.HAS_SVG_SURFACE:
    f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
    s = cairo.SVGSurface(f, w, h)


def test_text():
  pass


def test_region():
  a = cairo.Region()
  assert a.is_empty() == True
  assert a.num_rectangles() == 0

  b = cairo.RectangleInt(1, 2, 10, 12)
  d = cairo.RectangleInt(1, 1, 10, 12)
  e = cairo.RectangleInt(1, 3, 8, 12)
  assert (b.x, b.y, b.width, b.height) == (1, 2, 10, 12)
  c = cairo.Region((b, e))
  assert not c.is_empty()
  assert c.num_rectangles() == 2
  assert c.get_rectangle(1).y == 14

  ex = c.get_extents()
  assert ex == cairo.RectangleInt(1, 2, 10, 13)
  assert c.contains_rectangle(d) == cairo.REGION_OVERLAP_PART

  c.translate(10, 20)
  assert c.contains_rectangle(d) == cairo.REGION_OVERLAP_OUT
  assert c.get_rectangle(1) == cairo.RectangleInt(11, 34, 8, 1)

  cp = c.copy()
  assert c.num_rectangles() == cp.num_rectangles()
  assert c.get_rectangle(0) == cp.get_rectangle(0)
  assert c == cp
  assert 3 != c
  assert c != "test"

  c = cairo.Region((b, e))
  c.intersect(d)
  assert c.num_rectangles() == 1
  assert c.get_rectangle(0) == cairo.RectangleInt(1, 2, 10, 11)

  c = cairo.Region((b, e))
  c.subtract(d)
  assert c.num_rectangles() == 2
  assert c == cairo.Region([
      cairo.RectangleInt(1, 13, 10, 1), cairo.RectangleInt(1, 14, 8, 1) ])

  d = cairo.Region(d)
  c = cairo.Region((b, e))
  c.subtract(d)
  assert c.num_rectangles() == 2
  assert c.get_rectangle(0) == cairo.RectangleInt(1, 13, 10, 1)

  c = cairo.Region((b, e))
  c.union(d)
  assert c.num_rectangles() == 2
  assert c == cairo.Region([
      cairo.RectangleInt(1, 1, 10, 13), cairo.RectangleInt(1, 14, 8, 1) ])

  c = cairo.Region((b, e))
  c.xor(d)
  assert c.num_rectangles() == 3
  assert c == cairo.Region([
      cairo.RectangleInt(1, 1, 10, 1),
      cairo.RectangleInt(1, 14, 8, 1),
      cairo.RectangleInt(1, 13, 10, 1),
      ])