summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Renderer/SVG.html
blob: 31eb058ad58cf39cd002b9b5f5de5983f277f72c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<html>
<head>
<script src="../OLLoader.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.setSize(new OpenLayers.Size(4,4));
        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);
        var el = r.createNode("g");
        el.setAttributeNS(null, "transform", "translate(-6,-6)");
        t.eq(r.root.getAttributeNS(null, "transform"), el.getAttributeNS(null, "transform"), "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(5);
        
        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");
       
        // #1274: out of bound node fails when first added
        var geometry = {
            x: 10000000,
            y: 200000000,
            CLASS_NAME: "OpenLayers.Geometry.Point",
            id: "foo",
            getBounds: function() {return {bottom: 0}}
        }
        node.id = geometry.id;
        r.root.appendChild(node);

        var drawCircleCalled = false;
        r.drawCircle = function() {
            drawCircleCalled = true;
            return OpenLayers.Renderer.SVG.prototype.drawCircle.apply(r, arguments);
        }
        
        r.drawGeometry(geometry, {pointRadius: 3}, "blah_4000");
        t.eq(drawCircleCalled, true, "drawCircle called on drawGeometry for a point geometry.")
        t.ok(node.parentNode != r.root, "circle will not be drawn when coordinates are outside the valid range");
    }
    
    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 {path: "bar", complete: true};
        }
        
        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 {path: "bar", complete: true};
        }
        
        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;
        }
        t.eq(r.drawPolygon(node, geometry), false, "drawPolygon returns false 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_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).path;
        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");
    }
        
    function test_svg_importsymbol(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }

        t.plan(2);
        
        var r = new OpenLayers.Renderer.SVG(document.body);

        r.importSymbol("square");

        var polygon = document.getElementById(r.container.id + "_defs").firstChild.firstChild;
        
        var pass = false;
        for (var i = 0; i < polygon.points.numberOfItems; i++) {
            var p = polygon.points.getItem(i);
            pass = p.x === OpenLayers.Renderer.symbol.square[2*i] && 
                   p.y === OpenLayers.Renderer.symbol.square[2*i+1];
            if (!pass) {
                break;
            }
        }
        t.ok(pass, "Square symbol rendered correctly");
        t.ok(r.symbolMetrics["-square"], "Symbol metrics cached correctly.");
    }
        
    function test_svg_dashstyle(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }

        t.plan(5);
        
        var r = new OpenLayers.Renderer.SVG(document.body);

        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "dot"}, 1), "1,4", "dot dasharray created correctly");
        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "dash"}, 1), "4,4", "dash dasharray created correctly");
        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "longdash"}, 1), "8,4", "longdash dasharray created correctly");
        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "dashdot"}, 1), "4,4,1,4", "dashdot dasharray created correctly");
        t.eq(r.dashStyle({strokeWidth: 1, strokeDashstyle: "longdashdot"}, 1), "8,4,1,4", "dashdot dasharray created correctly");
    }
    
    function test_svg_clipline(t) {
        if (!OpenLayers.Renderer.SVG.prototype.supported()) {
            t.plan(0);
            return;
        }
        
        t.plan(3);
        
        var r = new OpenLayers.Renderer.SVG(document.body);
        r.setSize(new OpenLayers.Size(0, 0));
        r.map = {
            getResolution: function() {
                return 0.5;
            }
        }
        r.setExtent(new OpenLayers.Bounds(0, 0, 0, 0));

        var node = document.createElement('div');

        var geometry = new OpenLayers.Geometry.LineString([
            new OpenLayers.Geometry.Point(0, -5000),
            new OpenLayers.Geometry.Point(10000, 0),
            new OpenLayers.Geometry.Point(0, 5000)
        ]);
        r.drawLineString(node, geometry);
        t.eq(node.getAttribute("points"), "0,10000,15000,2500,15000,-2500,0,-10000", "Line with 3 points correctly clipped at inValidRange bounds");
        
        geometry = new OpenLayers.Geometry.LineString([
            new OpenLayers.Geometry.Point(0, -5000),
            new OpenLayers.Geometry.Point(10000, 0)
        ]);
        r.drawLineString(node, geometry);
        t.eq(node.getAttribute("points"), "0,10000,15000,2500", "2-point line with 2nd point outside range correctly clipped at inValidRange bounds");

        var geometry = new OpenLayers.Geometry.LineString([
            new OpenLayers.Geometry.Point(10000, 0),
            new OpenLayers.Geometry.Point(0, 5000)
        ]);
        r.drawLineString(node, geometry);
        t.eq(node.getAttribute("points"), "15000,-2500,0,-10000", "2-point line with 1st point outside range correctly clipped at inValidRange bounds");
    }

  </script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>