Files
openlayers/tests/Renderer/test_SVG.html
crschmidt 45c420782c Don't flip SVG Y values anymore, since we're working in pixel space these days,
and the Y transforms just make working with the SVG directly -- for example,
with people modifying it to support text -- more difficult than it should be.


git-svn-id: http://svn.openlayers.org/trunk/openlayers@5430 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2007-12-15 16:24:31 +00:00

368 lines
10 KiB
HTML

<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
var geometry = null, node = null;
function test_SVG_constructor(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(1);
var r = new OpenLayers.Renderer.SVG(document.body);
t.ok(r instanceof OpenLayers.Renderer.SVG, "new OpenLayers.Renderer.SVG returns SVG object" );
}
function test_SVG_destroy(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(1);
var g_Destroy = false;
OpenLayers.Renderer.Elements.prototype._destroy =
OpenLayers.Renderer.Elements.prototype.destroy;
OpenLayers.Renderer.prototype.destroy = function() {
g_Destroy = true;
}
var r = new OpenLayers.Renderer.SVG(document.body);
r.destroy();
t.eq(g_Destroy, true, "OpenLayers.Renderer.Elements.destroy() called");
OpenLayers.Renderer.prototype.destroy =
OpenLayers.Renderer.prototype._destroy;
}
function test_SVG_setextent(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(5);
OpenLayers.Renderer.Elements.prototype._setExtent =
OpenLayers.Renderer.Elements.prototype.setExtent;
var g_SetExtent = false;
OpenLayers.Renderer.Elements.prototype.setExtent = function() {
g_SetExtent = true;
}
var r = new OpenLayers.Renderer.SVG(document.body);
r.map = {
getResolution: function() {
return 0.5;
}
}
var extent = new OpenLayers.Bounds(1,2,3,4);
r.setExtent(extent);
t.eq(g_SetExtent, true, "Elements.setExtent() called");
t.eq(r.left, -2, "left is correct");
t.eq(r.top, 8, "top is correct");
t.eq(r.rendererRoot.getAttributeNS(null, "viewBox"), "0 0 4 4", "rendererRoot viewBox is correct");
// test extent changes
var extent = new OpenLayers.Bounds(4,3,2,1);
r.setExtent(extent);
t.eq(r.rendererRoot.getAttributeNS(null, "viewBox"), "6 6 -4 -4", "rendererRoot viewBox is correct after a new setExtent");
OpenLayers.Renderer.Elements.prototype.setExtent =
OpenLayers.Renderer.Elements.prototype._setExtent;
}
function test_SVG_setsize(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(2);
var r = new OpenLayers.Renderer.SVG(document.body);
var size = new OpenLayers.Size(1,2);
r.setSize(size);
t.eq(r.rendererRoot.getAttributeNS(null, "width"), size.w.toString(), "width is correct");
t.eq(r.rendererRoot.getAttributeNS(null, "height"), size.h.toString(), "height is correct");
}
function test_SVG_drawpoint(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(1);
var r = new OpenLayers.Renderer.SVG(document.body);
var properDraw = false;
var g_Radius = null;
r.drawCircle = function(n, g, r) {
properDraw = true;
g_Radius = 1;
}
r.drawPoint();
t.ok(properDraw && g_Radius == 1, "drawPoint called drawCircle with radius set to 1");
}
function test_SVG_drawcircle(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(3);
var r = new OpenLayers.Renderer.SVG(document.body);
r.resolution = 0.5;
r.left = 0;
r.top = 0;
var node = document.createElement('div');
var geometry = {
x: 1,
y: 2
}
r.drawCircle(node, geometry, 3);
t.eq(node.getAttributeNS(null, 'cx'), '2', "cx is correct");
t.eq(node.getAttributeNS(null, 'cy'), '-4', "cy is correct");
t.eq(node.getAttributeNS(null, 'r'), '3', "r is correct");
}
function test_SVG_drawlinestring(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(2);
var r = new OpenLayers.Renderer.SVG(document.body);
var node = document.createElement('div');
var geometry = {
components: "foo"
}
g_GetString = false;
g_Components = null;
r.getComponentsString = function(c) {
g_GetString = true;
g_Components = c;
return "bar";
}
r.drawLineString(node, geometry);
t.ok(g_GetString && g_Components == "foo", "getComponentString is called with valid arguments");
t.eq(node.getAttributeNS(null, "points"), "bar", "points attribute is correct");
}
function test_SVG_drawlinearring(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(2);
var r = new OpenLayers.Renderer.SVG(document.body);
var node = document.createElement('div');
var geometry = {
components: "foo"
}
g_GetString = false;
g_Components = null;
r.getComponentsString = function(c) {
g_GetString = true;
g_Components = c;
return "bar";
}
r.drawLinearRing(node, geometry);
t.ok(g_GetString, "getComponentString is called with valid arguments");
t.eq(node.getAttributeNS(null, "points"), "bar", "points attribute is correct");
}
function test_SVG_drawpolygon(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(4);
var r = new OpenLayers.Renderer.SVG(document.body);
var node = document.createElement('div');
var linearRings = [{
components: ["foo"]
},{
components: ["bar"]
}]
var geometry = {
components: linearRings
}
g_GetString = false;
r.getShortString = function(c) {
g_GetString = true;
return c;
}
r.drawPolygon(node, geometry);
t.ok(g_GetString, "getShortString is called");
t.eq(node.getAttributeNS(null, "d"), " M foo M bar z", "d attribute is correctly set");
t.eq(node.getAttributeNS(null, "fill-rule"), "evenodd", "fill-rule attribute is correctly set");
r.getShortString = function(c) {
return false;
}
r.drawPolygon(node, geometry);
t.eq(node.getAttributeNS(null, "d"), "", "d attribute is empty if one linearRing cannot be drawn");
}
function test_SVG_drawrectangle(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(4);
var r = new OpenLayers.Renderer.SVG(document.body);
r.resolution = 0.5;
r.left = 0;
r.top = 0;
var node = document.createElement('div');
var geometry = {
x: 1,
y: 2,
width: 3,
height: 4
}
r.drawRectangle(node, geometry);
t.eq(node.getAttributeNS(null, "x"), "2", "x attribute is correctly set");
t.eq(node.getAttributeNS(null, "y"), "-4", "y attribute is correctly set");
t.eq(node.getAttributeNS(null, "width"), "6", "width attribute is correctly set");
t.eq(node.getAttributeNS(null, "height"), "8", "height attribute is correctly set");
}
function test_SVG_drawsurface(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(3);
var r = new OpenLayers.Renderer.SVG(document.body);
var node = document.createElement('div');
var geometry = {
components: ['foo', 'bar', 'dude']
}
g_GetString = false;
r.getShortString = function(c) {
g_GetString = true;
return c;
}
r.drawSurface(node, geometry);
t.ok(g_GetString, "getShortString is called");
t.eq(node.getAttributeNS(null, "d"), "M foo C bar dude Z", "d attribute is correctly set");
r.getShortString = function(c) {
return false;
}
r.drawSurface(node, geometry);
t.eq(node.getAttributeNS(null, "d"), "", "d attribute is empty if one linearRing cannot be drawn");
}
function test_SVG_getcomponentsstring(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(1);
var components = ['foo', 'bar'];
OpenLayers.Renderer.SVG.prototype._getShortString =
OpenLayers.Renderer.SVG.prototype.getShortString;
OpenLayers.Renderer.SVG.prototype.getShortString = function(p) {
return p;
};
var string = OpenLayers.Renderer.SVG.prototype.getComponentsString(components);
t.eq(string, "foo,bar", "returned string is correct");
OpenLayers.Renderer.SVG.prototype.getShortString =
OpenLayers.Renderer.SVG.prototype._getShortString;
}
function test_SVG_getshortstring(t) {
if (!OpenLayers.Renderer.SVG.prototype.supported()) {
t.plan(0);
return;
}
t.plan(1);
var r = new OpenLayers.Renderer.SVG(document.body);
r.resolution = 0.5;
r.left = 0;
r.top = 0;
var point = {
x: 1,
y: 2
};
var string = r.getShortString(point);
t.eq(string, "2,-4", "returned string is correct");
}
</script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>