-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Template test case for graphic contexts #160
Changes from 71 commits
175062e
4628d42
5d77690
d841b1a
0a08e70
d6bb033
9095472
569770a
64a65a4
c81a92d
7b8d2ae
006dac0
7f1ba01
b90f722
12d3f44
ba0d2ed
204e9f5
6e3fd9c
6d62b0c
bd250ff
dd487f9
c41bf17
316f9b7
f6973a3
c056117
3169b32
4995e45
2745144
826d50c
17ccd1d
8a6c2ec
ebc1a76
53f723a
decba37
a5f7aa4
0030d59
208cf28
6069e05
31cb6a8
caae1b3
319cb71
acf7abc
b05a051
db19c40
7c42a81
7c76783
4c6ec14
e93db87
4560bf7
56b3229
2fab14e
e8d6b3f
5d2f3e3
814ec6e
b587eba
624ae2f
29b7c24
6f86cd9
da4b4bf
a0b3f01
de7d04a
7ba2544
27dba40
f5d31d4
a1f35c5
707a45b
137798e
6132a9e
ab0b4f8
012bba5
66f1e7a
229415e
b1fb165
70edcee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import contextlib | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
import numpy | ||
from PIL import Image | ||
|
||
from kiva.fonttools import Font | ||
from kiva.constants import MODERN | ||
|
||
|
||
class DrawingTester(object): | ||
""" Basic drawing tests for graphics contexts. | ||
|
||
""" | ||
|
||
def setUp(self): | ||
self.directory = tempfile.mkdtemp() | ||
self.filename = os.path.join(self.directory, 'rendered') | ||
self.gc = self.create_graphics_context(300, 300) | ||
self.gc.clear() | ||
self.gc.set_stroke_color((1.0, 0.0, 0.0)) | ||
self.gc.set_fill_color((1.0, 0.0, 0.0)) | ||
self.gc.set_line_width(5) | ||
|
||
def tearDown(self): | ||
del self.gc | ||
shutil.rmtree(self.directory) | ||
|
||
def test_line(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.move_to(107, 204) | ||
self.gc.line_to(107, 104) | ||
self.gc.stroke_path() | ||
|
||
def test_rectangle(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.move_to(107, 104) | ||
self.gc.line_to(107, 184) | ||
self.gc.line_to(187, 184) | ||
self.gc.line_to(187, 104) | ||
self.gc.line_to(107, 104) | ||
self.gc.stroke_path() | ||
|
||
def test_rect(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.rect(0, 0, 200, 200) | ||
self.gc.stroke_path() | ||
|
||
def test_circle(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.arc(150, 150, 100, 0.0, 2 * numpy.pi) | ||
self.gc.stroke_path() | ||
|
||
def test_quarter_circle(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.arc(150, 150, 100, 0.0, numpy.pi / 2) | ||
self.gc.stroke_path() | ||
|
||
def test_text(self): | ||
with self.draw_and_check(): | ||
font = Font(family=MODERN) | ||
font.size = 24 | ||
self.gc.set_font(font) | ||
self.gc.set_text_position(23, 67) | ||
self.gc.show_text("hello kiva") | ||
|
||
def test_circle_fill(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.arc(150, 150, 100, 0.0, 2 * numpy.pi) | ||
self.gc.fill_path() | ||
|
||
def test_star_fill(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.move_to(100, 100) | ||
self.gc.line_to(150, 200) | ||
self.gc.line_to(200, 100) | ||
self.gc.line_to(100, 150) | ||
self.gc.line_to(200, 150) | ||
self.gc.line_to(100, 100) | ||
self.gc.fill_path() | ||
|
||
def test_star_eof_fill(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.move_to(100, 100) | ||
self.gc.line_to(150, 200) | ||
self.gc.line_to(200, 100) | ||
self.gc.line_to(100, 150) | ||
self.gc.line_to(200, 150) | ||
self.gc.line_to(100, 100) | ||
self.gc.eof_fill_path() | ||
|
||
def test_circle_clip(self): | ||
with self.draw_and_check(): | ||
self.gc.clip_to_rect(150, 150, 100, 100) | ||
self.gc.begin_path() | ||
self.gc.arc(150, 150, 100, 0.0, 2 * numpy.pi) | ||
self.gc.fill_path() | ||
|
||
def test_text_clip(self): | ||
with self.draw_and_check(): | ||
self.gc.clip_to_rect(23, 77, 100, 23) | ||
font = Font(family=MODERN) | ||
font.size = 24 | ||
self.gc.set_font(font) | ||
self.gc.set_text_position(23, 67) | ||
self.gc.show_text("hello kiva") | ||
|
||
def test_star_clip(self): | ||
with self.draw_and_check(): | ||
self.gc.begin_path() | ||
self.gc.move_to(100, 100) | ||
self.gc.line_to(150, 200) | ||
self.gc.line_to(200, 100) | ||
self.gc.line_to(100, 150) | ||
self.gc.line_to(200, 150) | ||
self.gc.line_to(100, 100) | ||
self.gc.close_path() | ||
self.gc.clip() | ||
|
||
self.gc.begin_path() | ||
self.gc.arc(150, 150, 100, 0.0, 2 * numpy.pi) | ||
self.gc.fill_path() | ||
|
||
#### Required methods #################################################### | ||
|
||
@contextlib.contextmanager | ||
def draw_and_check(self): | ||
""" A context manager to check the result. | ||
|
||
""" | ||
raise NotImplementedError() | ||
|
||
def create_graphics_context(self, width, length): | ||
""" Create the desired graphics context | ||
|
||
""" | ||
raise NotImplementedError() | ||
|
||
|
||
class DrawingImageTester(DrawingTester): | ||
""" Basic drawing tests for graphics contexts of gui toolkits. | ||
|
||
""" | ||
|
||
@contextlib.contextmanager | ||
def draw_and_check(self): | ||
yield | ||
filename = "{0}.png".format(self.filename) | ||
self.gc.save(filename) | ||
self.assertImageSavedWithContent(filename) | ||
|
||
def assertImageSavedWithContent(self, filename): | ||
""" Load the image and check that there is some content in it. | ||
|
||
""" | ||
image = numpy.array(Image.open(filename)) | ||
# default is expected to be a totally white image | ||
|
||
self.assertEqual(image.shape[:2], (300, 300)) | ||
if image.shape[2] == 3: | ||
check = numpy.sum(image == [255, 0, 0], axis=2) == 3 | ||
elif image.shape[2] == 4: | ||
check = numpy.sum(image == [255, 0, 0, 255], axis=2) == 4 | ||
else: | ||
self.fail( | ||
'Pixel size is not 3 or 4, but {0}'.format(image.shape[2])) | ||
if check.any(): | ||
return | ||
self.fail('The image looks empty, no red pixels where drawn') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from kiva.tests.drawing_tester import DrawingImageTester | ||
from kiva.image import GraphicsContext | ||
from traits.testing.unittest_tools import unittest | ||
|
||
|
||
class TestAggDrawing(DrawingImageTester, unittest.TestCase): | ||
|
||
def setUp(self): | ||
DrawingImageTester.setUp(self) | ||
|
||
def create_graphics_context(self, width, height): | ||
return GraphicsContext((width, height)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from kiva.tests.drawing_tester import DrawingImageTester | ||
from traits.testing.unittest_tools import unittest | ||
|
||
try: | ||
import cairo # noqa | ||
except ImportError: | ||
CAIRO_NOT_AVAILABLE = True | ||
else: | ||
CAIRO_NOT_AVAILABLE = False | ||
|
||
|
||
@unittest.skipIf(CAIRO_NOT_AVAILABLE, "Cannot import cairo") | ||
class TestCairoDrawing(DrawingImageTester, unittest.TestCase): | ||
|
||
def create_graphics_context(self, width, height): | ||
from kiva.cairo import GraphicsContext | ||
return GraphicsContext((width, height)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import contextlib | ||
|
||
try: | ||
import pyglet | ||
except ImportError: | ||
PYGLET_NOT_AVAILABLE = True | ||
else: | ||
PYGLET_NOT_AVAILABLE = False | ||
|
||
|
||
from pyglet.image.codecs.png import PNGImageEncoder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should you re-raise above if the pyglet import fails? Otherwise, you'll die when you get here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, I should move this import inside the related method |
||
|
||
from kiva.tests.drawing_tester import DrawingImageTester | ||
from traits.testing.unittest_tools import unittest | ||
|
||
|
||
@unittest.skipIf(PYGLET_NOT_AVAILABLE, "Cannot import pyglet") | ||
class TestGLDrawing(DrawingImageTester, unittest.TestCase): | ||
|
||
def tearDown(self): | ||
if hasattr(self, 'window') and self.window is not None: | ||
self.window.close() | ||
del self.window | ||
DrawingImageTester.tearDown(self) | ||
|
||
def create_graphics_context(self, width, height): | ||
from kiva.gl import GraphicsContext | ||
self.window = pyglet.window.Window(width=width, height=height) | ||
gc = GraphicsContext((width, height)) | ||
gc.gl_init() | ||
return gc | ||
|
||
@unittest.skip("gl graphics context does not support start_clip") | ||
def test_star_clip(self): | ||
# FIXME: overriding test since it segfaults | ||
DrawingImageTester.test_star_clip(self) | ||
|
||
@unittest.expectedFailure | ||
def test_text_clip(self): | ||
# gl graphics context does not clip text properly | ||
DrawingImageTester.test_text_clip(self) | ||
|
||
@contextlib.contextmanager | ||
def draw_and_check(self): | ||
self.window.clear() | ||
self.window.switch_to() | ||
self.window.dispatch_events() | ||
yield | ||
self.window.dispatch_events() | ||
filename = "{0}.png".format(self.filename) | ||
buffer = pyglet.image.get_buffer_manager() | ||
buffer.get_color_buffer().save(filename, encoder=PNGImageEncoder()) | ||
self.assertImageSavedWithContent(filename) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
DrawingImageTester
subclassDrawingTester
which has a setUpThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh I see, what you mean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes you are right it is not necessary