summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/.gitignore3
-rw-r--r--test/README13
-rw-r--r--test/api_test.py86
-rw-r--r--test/examples_test.py29
-rwxr-xr-xtest/isurface_create_for_data1.py32
-rwxr-xr-xtest/isurface_create_for_data2.py30
-rwxr-xr-xtest/isurface_create_from_png.py56
-rwxr-xr-xtest/isurface_get_data.py50
-rwxr-xr-xtest/pygame-test1.py65
-rwxr-xr-xtest/pygame-test2.py49
-rwxr-xr-xtest/surface_create_for_stream.py81
-rwxr-xr-xtest/surface_write_to_png.py73
12 files changed, 567 insertions, 0 deletions
diff --git a/test/.gitignore b/test/.gitignore
new file mode 100644
index 0000000..8bc8786
--- /dev/null
+++ b/test/.gitignore
@@ -0,0 +1,3 @@
+*.png
+*.pyc
+*.pyo
diff --git a/test/README b/test/README
new file mode 100644
index 0000000..15a9b7d
--- /dev/null
+++ b/test/README
@@ -0,0 +1,13 @@
+pycairo tests
+-------------
+
+The main test files are the '*_test.py' files.
+They use py.test from pylib.
+http://codespeak.net/py/dist/
+
+$ cd test
+$ py.test
+
+The other files are tests that were used to test/develop specific
+functions. They usually require you run the test and then visually examine the
+output.
diff --git a/test/api_test.py b/test/api_test.py
new file mode 100644
index 0000000..c83bf99
--- /dev/null
+++ b/test/api_test.py
@@ -0,0 +1,86 @@
+'''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_SVG_SURFACE:
+ f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
+ s = cairo.SVGSurface(f, w, h)
+
+
+def test_text():
+ pass
diff --git a/test/examples_test.py b/test/examples_test.py
new file mode 100644
index 0000000..57e58c4
--- /dev/null
+++ b/test/examples_test.py
@@ -0,0 +1,29 @@
+'''test by running example scripts
+'''
+import os
+import os.path
+import subprocess
+
+import cairo
+#import py.test as test
+
+
+def test_snippets():
+ '''Run all snippets in png,pdf,ps,svg mode and check they exit successfully.
+ This will create *.{pdf,png,ps,svg} output files in
+ examples/cairo_snippets/snippets/
+ '''
+ def doSnippets(name):
+ retcode = subprocess.call('python %s -s' % name, shell=True)
+ assert retcode == 0, 'Error: retcode == {0}'.format(retcode)
+
+ os.chdir(os.path.join(os.path.dirname(__file__), '..', 'examples',
+ 'cairo_snippets'))
+ if cairo.HAS_PDF_SURFACE:
+ doSnippets('snippets_pdf.py')
+ if cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS:
+ doSnippets('snippets_png.py')
+ if cairo.HAS_PS_SURFACE:
+ doSnippets('snippets_ps.py')
+ if cairo.HAS_SVG_SURFACE:
+ doSnippets('snippets_svg.py')
diff --git a/test/isurface_create_for_data1.py b/test/isurface_create_for_data1.py
new file mode 100755
index 0000000..867fde4
--- /dev/null
+++ b/test/isurface_create_for_data1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+"""test cairo.ImageSurface.create_for_data() with a Python array
+"""
+
+import array
+import tempfile
+
+import cairo
+
+if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
+ raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')
+
+h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
+width, height = 255, 255
+data = array.array('B', [0] * width * height * 4)
+
+for y in range(height):
+ for x in range(width):
+ offset = (x + (y * width)) * 4
+ alpha = y
+
+ # cairo.FORMAT_ARGB32 uses pre-multiplied alpha
+ data[offset+0] = int(x * alpha/255.0) # B
+ data[offset+1] = int(y * alpha/255.0) # G
+ data[offset+2] = 0 # R
+ data[offset+3] = alpha # A
+
+surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32,
+ width, height)
+ctx = cairo.Context(surface)
+surface.write_to_png(fileName)
+print "see %s output file" % fileName
diff --git a/test/isurface_create_for_data2.py b/test/isurface_create_for_data2.py
new file mode 100755
index 0000000..c4f290b
--- /dev/null
+++ b/test/isurface_create_for_data2.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+"""test cairo.ImageSurface.create_for_data() with a numpy array
+"""
+import tempfile
+
+import cairo
+import numpy
+
+if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
+ raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')
+
+h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
+width, height = 255, 255
+data = numpy.ndarray (shape=(height,width,4), dtype=numpy.uint8)
+
+for x in range(width):
+ for y in range(height):
+ alpha = y
+
+ # cairo.FORMAT_ARGB32 uses pre-multiplied alpha
+ data[y][x][0] = int(x * alpha/255.0) # B
+ data[y][x][1] = int(y * alpha/255.0) # G
+ data[y][x][2] = 0 # R
+ data[y][x][3] = alpha # A
+
+surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32,
+ width, height)
+ctx = cairo.Context(surface)
+surface.write_to_png(fileName)
+print "see %s output file" % fileName
diff --git a/test/isurface_create_from_png.py b/test/isurface_create_from_png.py
new file mode 100755
index 0000000..129016d
--- /dev/null
+++ b/test/isurface_create_from_png.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+'''test cairo.ImageSurface.create_from_png() and
+ cairo.Surface.write_to_png()
+'''
+
+import os
+import tempfile
+
+import cairo
+
+if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
+ raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')
+
+inFileName = os.path.join(os.path.dirname(__file__), '..', 'examples',
+ 'cairo_snippets', 'data', 'romedalen.png')
+surface = cairo.ImageSurface.create_from_png(inFileName)
+
+# write to filename
+_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
+surface.write_to_png(outFileName)
+print "see %s output file" % outFileName
+
+# write to file object
+h, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
+os.close(h)
+f=file(outFileName, "w")
+surface.write_to_png(f)
+f.close()
+print "see %s output file" % outFileName
+
+# write to object that has a "write" method
+import StringIO
+_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
+buf = StringIO.StringIO()
+surface.write_to_png(buf)
+png_string = buf.getvalue()
+buf.close()
+f=file(outFileName, "w")
+f.write(png_string)
+f.close()
+print "see %s output file" % outFileName
+
+# write to object that has a "write" method
+_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
+import cStringIO
+buf = cStringIO.StringIO()
+surface.write_to_png(buf)
+png_string = buf.getvalue()
+buf.close()
+f=file(outFileName, "w")
+f.write(png_string)
+f.close()
+print "see %s output file" % outFileName
+
+# error test - to check the error message, should raise TypeError
+#surface.write_to_png(101)
diff --git a/test/isurface_get_data.py b/test/isurface_get_data.py
new file mode 100755
index 0000000..f2662d4
--- /dev/null
+++ b/test/isurface_get_data.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+"""
+Test ImageSurface.get_data()
+"""
+import tempfile
+
+import cairo
+import numpy
+
+if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
+ raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')
+
+w, h = 128, 128
+
+surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
+ctx = cairo.Context(surface)
+
+ctx.set_source_rgb(1, 1, 1) # white
+ctx.set_operator(cairo.OPERATOR_SOURCE)
+ctx.paint()
+
+# Draw out the triangle using absolute coordinates
+ctx.move_to(w/2, h/3)
+ctx.line_to(2*w/3, 2*h/3)
+ctx.rel_line_to(-1*w/3, 0)
+ctx.close_path()
+
+ctx.set_source_rgb(0, 0, 0) # black
+ctx.set_line_width(15)
+ctx.stroke()
+_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
+surface.write_to_png(outFileName)
+print "see %s output file" % outFileName
+
+# modify surface using numpy
+buf = surface.get_data()
+# alternative which should work (?) but reports
+# TypeError: buffer is read-only
+# - is a Python bug?
+#buf = buffer (surface1)
+
+a = numpy.ndarray(shape=(w,h,4), dtype=numpy.uint8, buffer=buf)
+
+# draw a vertical line
+a[:,40,0] = 255 # byte 0 is blue on little-endian systems
+a[:,40,1] = 0
+a[:,40,2] = 0
+_, outFileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
+surface.write_to_png(outFileName)
+print "see %s output file" % outFileName
diff --git a/test/pygame-test1.py b/test/pygame-test1.py
new file mode 100755
index 0000000..bd96b88
--- /dev/null
+++ b/test/pygame-test1.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+"""demonstrate pycairo and pygame
+method1: use pycairo and pygame directly
+"""
+
+import array
+import math
+import sys
+
+import cairo
+import pygame
+
+def draw(surface):
+ x,y, radius = (250,250, 200)
+ ctx = cairo.Context(surface)
+ ctx.set_line_width(15)
+ ctx.arc(x, y, radius, 0, 2.0 * math.pi)
+ ctx.set_source_rgb(0.8, 0.8, 0.8)
+ ctx.fill_preserve()
+ ctx.set_source_rgb(1, 1, 1)
+ ctx.stroke()
+
+def input(events):
+ for event in events:
+ if event.type == pygame.QUIT:
+ sys.exit(0)
+ else:
+ print event
+
+
+Width, Height = 512, 512
+surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, Width, Height)
+
+pygame.init()
+window = pygame.display.set_mode( (Width,Height) )
+screen = pygame.display.get_surface()
+
+draw(surface)
+
+#Create PyGame surface from Cairo Surface
+buf = surface.get_data()
+image = pygame.image.frombuffer(buf,(Width,Height),"ARGB",)
+#Tranfer to Screen
+screen.blit(image, (0,0))
+pygame.display.flip()
+
+while True:
+ input(pygame.event.get())
+
+
+"""
+with pycairo 1.4.12 and pygame 1.7.1 you get the error message:
+
+Traceback (most recent call last):
+ File "./pygame-test1.py", line 42, in <module>
+ image = pygame.image.frombuffer(buf,(Width,Height),"ARGB",)
+TypeError: char buffer type not available
+
+This is because with
+ buf = surface.get_data()
+pycairo provides a binary image buffer,
+whereas with
+ image = pygame.image.frombuffer(buf,(Width,Height),"ARGB",)
+pygame is expecting a text-based character buffer!
+"""
diff --git a/test/pygame-test2.py b/test/pygame-test2.py
new file mode 100755
index 0000000..bec32de
--- /dev/null
+++ b/test/pygame-test2.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+"""demonstrate pycairo and pygame
+method1: use an intermediate Python array object
+"""
+
+import array
+import math
+import sys
+
+import cairo
+import pygame
+
+def draw(surface):
+ x,y, radius = (250,250, 200)
+ ctx = cairo.Context(surface)
+ ctx.set_line_width(15)
+ ctx.arc(x, y, radius, 0, 2.0 * math.pi)
+ ctx.set_source_rgb(0.8, 0.8, 0.8)
+ ctx.fill_preserve()
+ ctx.set_source_rgb(1, 1, 1)
+ ctx.stroke()
+
+def input(events):
+ for event in events:
+ if event.type == pygame.QUIT:
+ sys.exit(0)
+ else:
+ print event
+
+
+Width, Height = 512, 512
+data = array.array('c', chr(0) * Width * Height * 4)
+stride = Width * 4
+surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32,Width, Height, stride)
+
+pygame.init()
+window = pygame.display.set_mode( (Width,Height) )
+screen = pygame.display.get_surface()
+
+draw(surface)
+
+#Create PyGame surface from Cairo Surface
+image = pygame.image.frombuffer(data.tostring(),(Width,Height),"ARGB",)
+#Tranfer to Screen
+screen.blit(image, (0,0))
+pygame.display.flip()
+
+while True:
+ input(pygame.event.get())
diff --git a/test/surface_create_for_stream.py b/test/surface_create_for_stream.py
new file mode 100755
index 0000000..61997e0
--- /dev/null
+++ b/test/surface_create_for_stream.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+"""
+Test PDF/PS/SVG constructors (using streams)
+"""
+
+import cStringIO
+import gc
+import math
+import sys
+import StringIO
+
+import cairo
+
+
+class C(object):
+ """a file-like object (for testing), it simulates sys.stdout
+ """
+ def __init__ (self):
+ self.closed = False
+
+ def write(self, s):
+ """just echo to stdout, without newlines"""
+ if self.closed:
+ raise ValueError ("I/O operation on closed file")
+ sys.stdout.write(s)
+
+ def close(self):
+ self.closed = True
+
+
+# a selection of possible args to surface.write_to_png()
+#fo = '/tmp/f.ps'
+fo = file('/tmp/f.svg', 'w')
+#fo = StringIO.StringIO()
+#fo = cStringIO.StringIO()
+#fo = sys.stdout
+#fo = C()
+
+#fo.close() # this should cause: ValueError: I/O operation on closed file
+
+WIDTH, HEIGHT = 256, 256
+
+#surface = cairo.PDFSurface(fo, WIDTH, HEIGHT)
+#surface = cairo.PSSurface(fo, WIDTH, HEIGHT)
+surface = cairo.SVGSurface(fo, WIDTH, HEIGHT)
+
+#sys.stdout.write ('1\n'); sys.stdout.flush()
+ctx = cairo.Context(surface)
+
+#del fo # test that 'fo' is referenced to keep it alive
+#gc.collect()
+
+#fo.close() # this should cause: ValueError: I/O operation on closed file
+
+ctx.scale(WIDTH/1.0, HEIGHT/1.0)
+
+pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0)
+pat.add_color_stop_rgba(1, 0, 0, 0, 1)
+pat.add_color_stop_rgba(0, 1, 1, 1, 1)
+
+ctx.rectangle(0,0,1,1)
+ctx.set_source(pat)
+ctx.fill()
+
+pat = cairo.RadialGradient(0.45, 0.4, 0.1,
+ 0.4, 0.4, 0.5)
+pat.add_color_stop_rgba(0, 1, 1, 1, 1)
+pat.add_color_stop_rgba(1, 0, 0, 0, 1)
+
+ctx.set_source(pat)
+ctx.arc(0.5, 0.5, 0.3, 0, 2 * math.pi)
+ctx.fill()
+
+ctx.show_page()
+surface.finish()
+
+# for testing StringIO: get data and write to file
+#string = fo.getvalue()
+#f2 = file('/tmp/f.ps', 'w')
+#f2.write(string)
+#f2.close()
diff --git a/test/surface_write_to_png.py b/test/surface_write_to_png.py
new file mode 100755
index 0000000..61674f4
--- /dev/null
+++ b/test/surface_write_to_png.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+"""
+Test Surface.write_to_png()
+"""
+
+import cStringIO
+import math
+import sys
+import StringIO
+
+import cairo
+
+
+if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
+ raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')
+
+
+class C(object):
+ """a file-like object (for testing), it simulates sys.stdout
+ """
+ def __init__(self):
+ self.closed = False
+
+ def write(self, s):
+ """just echo to stdout, without newlines"""
+ if self.closed:
+ raise ValueError("I/O operation on closed file")
+ sys.stdout.write(s)
+
+ def close(self):
+ self.closed = True
+
+
+WIDTH, HEIGHT = 256, 256
+
+surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
+ctx = cairo.Context(surface)
+
+ctx.scale(WIDTH/1.0, HEIGHT/1.0)
+
+pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0)
+pat.add_color_stop_rgba(1, 0, 0, 0, 1)
+pat.add_color_stop_rgba(0, 1, 1, 1, 1)
+
+ctx.rectangle(0,0,1,1)
+ctx.set_source(pat)
+ctx.fill()
+
+pat = cairo.RadialGradient(0.45, 0.4, 0.1,
+ 0.4, 0.4, 0.5)
+pat.add_color_stop_rgba(0, 1, 1, 1, 1)
+pat.add_color_stop_rgba(1, 0, 0, 0, 1)
+
+ctx.set_source(pat)
+ctx.arc(0.5, 0.5, 0.3, 0, 2 * math.pi)
+ctx.fill()
+
+# a selection of possible args to surface.write_to_png()
+#fo = '/tmp/f.png'
+fo = file('/tmp/f.png', 'w')
+#fo = StringIO.StringIO()
+#fo = cStringIO.StringIO()
+#fo = sys.stdout
+#fo = C()
+
+#fo.close() # this should cause: ValueError: I/O operation on closed file
+surface.write_to_png(fo)
+
+# for testing StringIO: get data and write to file
+#string = fo.getvalue()
+#f2 = file('/tmp/f.png', 'w')
+#f2.write(string)
+#f2.close()