Commit d966c357 authored by Rafael Monnerat's avatar Rafael Monnerat

Included some tests for SVG to PNG conversions

Using Image Document, this commit includes tests to assert
the conversion outcome when convert svg to png. The main goal
is assert if the image backgronud (external and embeded cases)
is incorpoted into the PNG generated.

The image comparation is very basic level, but enough for this tests.

It exposes an error when convert one svg that uses url with http.
parent aff30449
......@@ -30,6 +30,7 @@
import unittest
import os
import transaction
from StringIO import StringIO
from lxml import etree
from AccessControl import Unauthorized
......@@ -39,6 +40,12 @@ from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase,\
_getConversionServerDict
from Products.ERP5Type.tests.utils import FileUpload, createZODBPythonScript
try:
from PIL import Image
except ImportError:
# When the PIL eggs is developed it seems to use an different
# way to import it.
import Image
LANGUAGE_LIST = ('en', 'fr', 'de', 'bg',)
......@@ -51,6 +58,20 @@ def makeFileUpload(name, as_name=None):
path = makeFilePath(name)
return FileUpload(path, as_name)
def process_image(image, size=(40, 40)):
# open the images to compare, resize them, and convert to grayscale
# get the rgb values of the pixels in the image
image = Image.open(image)
return list(image.resize(size).convert("L").getdata())
def compare_image(image_data_1, image_data_2):
""" Find the total difference in RGB value for all pixels in the images
and return the "amount" of differences that the 2 images contains. """
data1 = process_image(image_data_1)
data2 = process_image(image_data_2)
return abs(sum([data1[x] - data2[x] for x in range(len(data1))]))
class TestERP5WebWithDms(ERP5TypeTestCase, ZopeTestCase.Functional):
"""Test for erp5_web business template.
"""
......@@ -756,6 +777,94 @@ return True
self._test_document_publication_workflow('Web Page',
'share_alive_action')
def test_ImageConversionFromSVGToPNG_embeeded_data(self):
""" Test Convert one SVG Image with an image with the data
at the url of the image tag.ie:
<image xlink:href="data:...." >
"""
portal = self.portal
image_reference = 'NXD-IMAGE'
image_module = portal.getDefaultModule(portal_type='Image')
upload_file = makeFileUpload('user-TESTSVG-CASE-EMBEDDEDDATA.svg')
image = image_module.newContent(portal_type='Image',
file=upload_file,
reference=image_reference)
image.publish()
transaction.commit()
self.tic()
self.assertEquals(image.getContentType(), 'image/svg+xml')
mime, converted_data = image.convert("png")
self.assertEquals(mime, 'image/png')
expected_image = makeFileUpload('user-TESTSVG-CASE-EMBEDDEDDATA.png')
# Compare images and accept some minimal difference,
difference_value = compare_image(StringIO(converted_data), expected_image)
self.assertTrue(difference_value < 100,
"Conversion from svg to png create one too small image, " + \
"so it failed to download the image. (%s >= 100)" % difference_value)
def test_ImageConversionFromSVGToPNG_http_url(self):
""" Test Convert one SVG Image with an image with a full
url at the url of the image tag. ie:
<image xlink:href="http://www.erp5.com/user-XXX-XXX"
"""
portal = self.portal
image_reference = 'NXD-IMAGE'
image_module = portal.getDefaultModule(portal_type='Image')
upload_file = makeFileUpload('user-TESTSVG-CASE-FULLURL.svg')
image = image_module.newContent(portal_type='Image',
file=upload_file,
reference=image_reference)
image.publish()
transaction.commit()
self.tic()
self.assertEquals(image.getContentType(), 'image/svg+xml')
mime, converted_data = image.convert("png")
self.assertEquals(mime, 'image/png')
expected_image = makeFileUpload('user-TESTSVG-CASE-FULLURL.png')
# Compare images and accept some minimal difference,
difference_value = compare_image(StringIO(converted_data), expected_image)
self.assertTrue(difference_value < 100,
"Conversion from svg to png create one too small image, " + \
"so it failed to download the image. (%s >= 100)" % difference_value)
def test_ImageConversionFromSVGToPNG_file_url(self):
""" Test Convert one SVG Image with an image using local path (file)
at the url of the image tag. ie:
<image xlink:href="file:///../../user-XXX-XXX"
This is not used by ERP5 in production, but this is way that
prooves that conversion from SVG to PNG can use external images.
"""
portal = self.portal
image_reference = 'NXD-IMAGE'
image_module = portal.getDefaultModule(portal_type='Image')
upload_file = makeFileUpload('user-TESTSVG-CASE-FULLURL-TEMPLATE.svg')
svg_content = upload_file.read().replace("REPLACE_THE_URL_HERE",
makeFilePath("user-TESTSVG-BACKGROUND-IMAGE.png"))
# Add image using data instead file this time as it is not the goal of
# This test assert this topic.
image = image_module.newContent(portal_type='Image',
data=svg_content,
filename=upload_file.filename,
content_type="image/svg+xml",
reference=image_reference)
image.publish()
transaction.commit()
self.tic()
self.assertEquals(image.getContentType(), 'image/svg+xml')
mime, converted_data = image.convert("png")
self.assertEquals(mime, 'image/png')
expected_image = makeFileUpload('user-TESTSVG-CASE-FULLURL.png')
# Compare images and accept some minimal difference,
difference_value = compare_image(StringIO(converted_data), expected_image)
self.assertTrue(difference_value < 100,
"Conversion from svg to png create one too small image, " + \
"so it failed to download the image. (%s >= 100)" % difference_value)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestERP5WebWithDms))
......
This source diff could not be displayed because it is too large. You can view the blob instead.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
id="svg2"
version="1.1"
inkscape:version="0.48.2 r9819"
width="524.40942"
height="372.04724"
sodipodi:docname="user-howto.manage.support.requests.assign.event.to.ticket.create.follow.up.ticket-002-en.svg">
<metadata
id="metadata8">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs6" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1024"
inkscape:window-height="718"
id="namedview4"
showgrid="false"
inkscape:zoom="1.3707937"
inkscape:cx="270.63548"
inkscape:cy="273.56415"
inkscape:window-x="-3"
inkscape:window-y="-3"
inkscape:window-maximized="1"
inkscape:current-layer="svg2" />
<image
y="0.99838382"
x="0.11206181"
id="image3065"
xlink:href="file://REPLACE_THE_URL_HERE"
height="369.32098"
width="523.56" />
<rect
style="fill:none;stroke:#ff0000;stroke-width:2.696;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
id="rect2987"
width="81.987816"
height="11.938368"
x="64.116737"
y="43.860283"
rx="2.5779424"
ry="0.29129499" />
<rect
style="fill:none;stroke:#ff0000;stroke-width:2.81803679;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
id="rect2989"
width="82.598396"
height="9.534873"
x="64.417244"
y="68.28804"
rx="2.4387791"
ry="0.28804284" />
<path
sodipodi:type="arc"
style="fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-width:2;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="path3898"
sodipodi:cx="303.51724"
sodipodi:cy="155.48277"
sodipodi:rx="25.13793"
sodipodi:ry="25.13793"
d="m 328.65517,155.48277 a 25.13793,25.13793 0 1 1 -50.27586,0 25.13793,25.13793 0 1 1 50.27586,0 z"
transform="matrix(0.63956863,0,0,0.63956863,-19.039052,-62.99123)" />
<text
xml:space="preserve"
style="font-size:23px;font-style:normal;font-weight:bold;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#ff0000;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans Bold"
x="166.65985"
y="45.086403"
id="text3900"
sodipodi:linespacing="125%"
transform="scale(1.0034798,0.99653226)"><tspan
sodipodi:role="line"
id="tspan3902"
x="166.65985"
y="45.086403">1</tspan></text>
<path
sodipodi:type="arc"
style="fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-width:2;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="path3898-3"
sodipodi:cx="303.51724"
sodipodi:cy="155.48277"
sodipodi:rx="25.13793"
sodipodi:ry="25.13793"
d="m 328.65517,155.48277 a 25.13793,25.13793 0 1 1 -50.27586,0 25.13793,25.13793 0 1 1 50.27586,0 z"
transform="matrix(0.63956863,0,0,0.63956863,-17.252115,-25.057001)" />
<text
xml:space="preserve"
style="font-size:23px;font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;text-align:start;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:start;fill:#0000ff;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans Bold"
x="168.57806"
y="83.152695"
id="text3900-2"
sodipodi:linespacing="125%"
transform="scale(1.0034798,0.99653226)"><tspan
sodipodi:role="line"
id="tspan3902-0"
x="168.57806"
y="83.152695">2</tspan></text>
</svg>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
id="svg2"
version="1.1"
inkscape:version="0.48.2 r9819"
width="524.40942"
height="372.04724"
sodipodi:docname="user-howto.manage.support.requests.assign.event.to.ticket.create.follow.up.ticket-002-en.svg">
<metadata
id="metadata8">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs6" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1024"
inkscape:window-height="718"
id="namedview4"
showgrid="false"
inkscape:zoom="1.3707937"
inkscape:cx="270.63548"
inkscape:cy="273.56415"
inkscape:window-x="-3"
inkscape:window-y="-3"
inkscape:window-maximized="1"
inkscape:current-layer="svg2" />
<image
y="0.99838382"
x="0.11206181"
id="image3065"
xlink:href="http://www.erp5.com/user-howto.manage.support.requests.assign.event.to.ticket.create.follow.up.ticket.screenshot?format=png"
height="369.32098"
width="523.56" />
<rect
style="fill:none;stroke:#ff0000;stroke-width:2.696;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
id="rect2987"
width="81.987816"
height="11.938368"
x="64.116737"
y="43.860283"
rx="2.5779424"
ry="0.29129499" />
<rect
style="fill:none;stroke:#ff0000;stroke-width:2.81803679;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
id="rect2989"
width="82.598396"
height="9.534873"
x="64.417244"
y="68.28804"
rx="2.4387791"
ry="0.28804284" />
<path
sodipodi:type="arc"
style="fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-width:2;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="path3898"
sodipodi:cx="303.51724"
sodipodi:cy="155.48277"
sodipodi:rx="25.13793"
sodipodi:ry="25.13793"
d="m 328.65517,155.48277 a 25.13793,25.13793 0 1 1 -50.27586,0 25.13793,25.13793 0 1 1 50.27586,0 z"
transform="matrix(0.63956863,0,0,0.63956863,-19.039052,-62.99123)" />
<text
xml:space="preserve"
style="font-size:23px;font-style:normal;font-weight:bold;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#ff0000;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans Bold"
x="166.65985"
y="45.086403"
id="text3900"
sodipodi:linespacing="125%"
transform="scale(1.0034798,0.99653226)"><tspan
sodipodi:role="line"
id="tspan3902"
x="166.65985"
y="45.086403">1</tspan></text>
<path
sodipodi:type="arc"
style="fill:#ffffff;fill-opacity:1;stroke:#000000;stroke-width:2;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="path3898-3"
sodipodi:cx="303.51724"
sodipodi:cy="155.48277"
sodipodi:rx="25.13793"
sodipodi:ry="25.13793"
d="m 328.65517,155.48277 a 25.13793,25.13793 0 1 1 -50.27586,0 25.13793,25.13793 0 1 1 50.27586,0 z"
transform="matrix(0.63956863,0,0,0.63956863,-17.252115,-25.057001)" />
<text
xml:space="preserve"
style="font-size:23px;font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;text-align:start;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:start;fill:#0000ff;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans Bold"
x="168.57806"
y="83.152695"
id="text3900-2"
sodipodi:linespacing="125%"
transform="scale(1.0034798,0.99653226)"><tspan
sodipodi:role="line"
id="tspan3902-0"
x="168.57806"
y="83.152695">2</tspan></text>
</svg>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment