diff options
author | Steve Chaplin <stevech1097@yahoo.com.au> | 2009-08-06 21:01:59 +0800 |
---|---|---|
committer | Steve Chaplin <stevech1097@yahoo.com.au> | 2009-08-06 21:01:59 +0800 |
commit | ca09d078984c5310f5052477da1ae0f65d017a92 (patch) | |
tree | 5deefac73e729e373809bdd8f462752dc7747da1 /test | |
download | py2cairo-ca09d078984c5310f5052477da1ae0f65d017a92.tar.gz |
Initial commit
Diffstat (limited to 'test')
-rw-r--r-- | test/.gitignore | 5 | ||||
-rw-r--r-- | test/Makefile.am | 11 | ||||
-rw-r--r-- | test/api_test.py | 54 | ||||
-rw-r--r-- | test/examples_test.py | 29 | ||||
-rwxr-xr-x | test/isurface_create_for_data1.py | 27 | ||||
-rwxr-xr-x | test/isurface_create_for_data2.py | 26 | ||||
-rwxr-xr-x | test/isurface_create_from_png.py | 37 | ||||
-rwxr-xr-x | test/isurface_get_data.py | 43 | ||||
-rwxr-xr-x | test/pygame-test1.py | 65 | ||||
-rwxr-xr-x | test/pygame-test2.py | 49 | ||||
-rwxr-xr-x | test/surface_create_for_stream.py | 81 | ||||
-rwxr-xr-x | test/surface_write_to_png.py | 69 |
12 files changed, 496 insertions, 0 deletions
diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..39ca8f9 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,5 @@ +Makefile +Makefile.in +*.png +*.pyc +*.pyo diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..af0a436 --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,11 @@ +EXTRA_DIST = \ + examples_test.py + isurface_create_for_data1.py \ + isurface_create_for_data2.py \ + isurface_create_from_png.py \ + isurface_get_data.py \ + pygame-test1.py \ + pygame-test2.py \ + surface_create_for_stream.py \ + surface_write_to_png.py + diff --git a/test/api_test.py b/test/api_test.py new file mode 100644 index 0000000..6a8db8c --- /dev/null +++ b/test/api_test.py @@ -0,0 +1,54 @@ +'''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. +''' +from __future__ import division # new in 2.2, redundant in 3.0 +from __future__ import absolute_import # new in 2.5, redundant in 2.7/3.0 +from __future__ import print_function # new in 2.6, redundant in 3.0 + +import tempfile as tfi + +import cairo +import py.test as test + + +def test_context(): + pass + +def test_matrix(): + pass + +def test_path(): + pass + +def test_patterns(): + pass + + +def test_surfaces(): + # 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..f5fe924 --- /dev/null +++ b/test/examples_test.py @@ -0,0 +1,29 @@ +'''test by running example scripts +''' +from __future__ import division # new in 2.2, redundant in 3.0 +from __future__ import absolute_import # new in 2.5, redundant in 2.7/3.0 +from __future__ import print_function # new in 2.6, redundant in 3.0 + +import os +import os.path +import subprocess + +#import py.test as test + + +def test_examples(): + '''run non-gui example scripts and check they exit successfully. + ''' + os.chdir(os.path.join(os.path.dirname(__file__), '..', 'examples')) + for f in (x for x in os.listdir('.') if x.endswith('.py')): + retcode = subprocess.call('python %s' % f, shell=True) + assert retcode == 0, 'Error: {0} retcode == {1}'.format(f, retcode) + + +def test_snippets_png(): + '''run all snippets in png mode and check they exit successfully. + ''' + os.chdir(os.path.join(os.path.dirname(__file__), '..', 'examples', + 'cairo_snippets')) + retcode = subprocess.call('python snippets_png.py -s', shell=True) + assert retcode == 0, 'Error: retcode == {0}'.format(retcode) diff --git a/test/isurface_create_for_data1.py b/test/isurface_create_for_data1.py new file mode 100755 index 0000000..c497991 --- /dev/null +++ b/test/isurface_create_for_data1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +"""test cairo.ImageSurface.create_for_data() with a Python array +""" + +import array + +import cairo + +dir_ = "/tmp/" +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(dir_ + 'for_data1.png') diff --git a/test/isurface_create_for_data2.py b/test/isurface_create_for_data2.py new file mode 100755 index 0000000..88b4c5c --- /dev/null +++ b/test/isurface_create_for_data2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +"""test cairo.ImageSurface.create_for_data() with a numpy array +""" + +import cairo + +import numpy + +dir_ = "/tmp/" +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) + data[y][x][1] = int(y * alpha/255.0) + data[y][x][2] = 0 + data[y][x][3] = alpha + +surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32, + width, height) +ctx = cairo.Context(surface) +surface.write_to_png(dir_ + 'for_data2.png') diff --git a/test/isurface_create_from_png.py b/test/isurface_create_from_png.py new file mode 100755 index 0000000..951acb3 --- /dev/null +++ b/test/isurface_create_from_png.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# test cairo.ImageSurface.create_from_png() + +import cairo + +surface = cairo.ImageSurface.create_from_png("/tmp/warpedtext.png") + +# write to filename +surface.write_to_png("/tmp/t1.png") + +# write to file object +f2=file("/tmp/t2.png", "w") +surface.write_to_png(f2) +f2.close() + +# write to object that has a "write" method +import StringIO +buffer = StringIO.StringIO() +surface.write_to_png(buffer) +png_string = buffer.getvalue() +buffer.close() +f3=file("/tmp/t3.png", "w") +f3.write(png_string) +f3.close() + +# write to object that has a "write" method +import cStringIO +buffer = cStringIO.StringIO() +surface.write_to_png(buffer) +png_string = buffer.getvalue() +buffer.close() +f4=file("/tmp/t4.png", "w") +f4.write(png_string) +f4.close() + +# 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..c19a17e --- /dev/null +++ b/test/isurface_get_data.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +""" +Test ImageSurface.get_data() +""" + +import cairo +import numpy + +dir_ = "/tmp/" +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() +surface.write_to_png(dir_ + "get_data_test1.png") + +# 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 +surface.write_to_png(dir_ + "get_data_test2.png") diff --git a/test/pygame-test1.py b/test/pygame-test1.py new file mode 100755 index 0000000..25871e5 --- /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..9ebc1ae --- /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..210badd --- /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..f4ba83b --- /dev/null +++ b/test/surface_write_to_png.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +""" +Test Surface.write_to_png() +""" + +import cStringIO +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 + + +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() |