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
|
<html>
<head>
<script src="OLLoader.js"></script>
<script type="text/javascript">
function test_Style_constructor(t) {
t.plan(6);
var rules = [
new OpenLayers.Rule({
symbolizer: {fillColor: "red"},
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "type",
value: "fire engine"
})
}),
new OpenLayers.Rule({
symbolizer: {fillColor: "yellow"},
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "type",
value: "sports car"
})
})
];
var style = new OpenLayers.Style(null, {
foo: "bar",
rules: rules
});
t.ok(style instanceof OpenLayers.Style,
"new OpenLayers.Style returns object" );
t.eq(style.foo, "bar", "constructor sets options correctly");
t.eq(style.rules.length, 2, "correct number of rules added");
t.ok(style.rules[0] === rules[0], "correct first rule added");
t.ok(style.rules[1] === rules[1], "correct second rule added");
t.eq(typeof style.createSymbolizer, "function", "style has a createSymbolizer function");
}
function test_Style_create(t) {
t.plan(10);
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector("layer");
var baseStyle = OpenLayers.Util.extend(
OpenLayers.Feature.Vector.style["default"],
{externalGraphic: "bar${foo}.png"}
);
var style = new OpenLayers.Style(baseStyle);
var rule1 = new OpenLayers.Rule({
symbolizer: {"Point": {fillColor: "green"}},
maxScaleDenominator: 500000,
filter: new OpenLayers.Filter.FeatureId({
fids: ["1"]
})
});
var rule2 = new OpenLayers.Rule({
symbolizer: {"Point": {fillColor: "yellow"}},
minScaleDenominator: 500000,
maxScaleDenominator: 1000000,
filter: new OpenLayers.Filter.FeatureId({
fids: ["1"]
})
});
var rule3 = new OpenLayers.Rule({
symbolizer: {"Point": {fillColor: "red"}},
minScaleDenominator: 1000000,
maxScaleDenominator: 2500000,
filter: new OpenLayers.Filter.FeatureId({
fids: ["1"]
})
});
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(3,5),
{"foo": "bar"}
);
feature.fid = "1";
// for this fid, the above rule should apply
layer.styleMap = new OpenLayers.StyleMap(style);
layer.addFeatures([feature]);
map.addLayer(layer);
map.setBaseLayer(layer);
map.setCenter(new OpenLayers.LonLat(3,5), 10);
var createdStyle = style.createSymbolizer(feature);
t.eq(createdStyle.externalGraphic, "barbar.png", "Calculated property style for default symbolizer correctly.");
style.addRules([rule1, rule2, rule3]);
createdStyle = style.createSymbolizer(feature);
// at this scale, the feature should be green
t.eq(createdStyle.display, undefined, "Feature is visible at scale "+map.getScale());
t.eq(createdStyle.fillColor, "green", "Point symbolizer from rule applied correctly.");
map.setCenter(new OpenLayers.LonLat(3,5), 9);
// at this scale, the feature should be red
createdStyle = style.createSymbolizer(feature);
t.eq(createdStyle.display, undefined, "Feature is visible at scale "+map.getScale());
t.eq(createdStyle.fillColor, "yellow", "Point symbolizer from rule applied correctly.");
map.setCenter(new OpenLayers.LonLat(3,5), 8);
// at this scale, the feature should be yellow
createdStyle = style.createSymbolizer(feature);
t.eq(createdStyle.display, undefined, "Feature is visible at scale "+map.getScale());
t.eq(createdStyle.fillColor, "red", "Point symbolizer from rule applied correctly.");
map.setCenter(new OpenLayers.LonLat(3,5), 7);
// at this scale, the feature should be invisible
createdStyle = style.createSymbolizer(feature);
t.eq(createdStyle.display, "none", "Feature is invisible at scale "+map.getScale());
t.eq(createdStyle.fillColor, baseStyle.fillColor, "Point symbolizer from base style applied correctly.");
feature.fid = "2";
// now the rule should not apply
createdStyle = style.createSymbolizer(feature);
t.eq(createdStyle.fillColor, baseStyle.fillColor, "Correct style for rule that does not apply to fid=\"2\".");
}
function test_Style_createSymbolizer(t) {
t.plan(5);
var style = new OpenLayers.Style();
// override applySymbolizer to log arguments
var log = [];
style.applySymbolizer = function(r) {
log.push(r);
OpenLayers.Style.prototype.applySymbolizer.apply(this, arguments);
};
// rules for the style
var rule = new OpenLayers.Rule({
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "foo",
value: "bar"
}),
symbolizer: {
label: "${labelValue}"
}
});
rule.id = "foo=bar rule";
var elseRule = new OpenLayers.Rule({
elseFilter: true,
symbolizer: {
label: "${labelValue}"
}
});
elseRule.id = "else rule";
style.addRules([rule, elseRule]);
// a) test that applySymbolizer is only called with rule
log = [];
style.createSymbolizer(
new OpenLayers.Feature.Vector(null, {foo: "bar"})
);
t.eq(log.length, 1, "a) applySymbolizer called once");
t.eq(log[0] && log[0].id, rule.id, "a) applySymbolizer called with correct rule");
// b) test that applySymbolizer is only called with elseRule
log = [];
style.createSymbolizer(
new OpenLayers.Feature.Vector(null, {foo: "baz"})
);
t.eq(log.length, 1, "b) applySymbolizer called once");
t.eq(log[0] && log[0].id, elseRule.id, "b) applySymbolizer called with correct rule");
// c) test that label in returned symbolizer is a string even if property value is a number
var symbolizer = style.createSymbolizer(
new OpenLayers.Feature.Vector(null, {foo: "bar", labelValue: 0})
);
t.eq(symbolizer.label, "0", "c) feature property cast to string when used as symbolizer label");
}
function test_Style_applySymbolizer(t) {
t.plan(15);
var feature = new OpenLayers.Feature.Vector();
var defaults = OpenLayers.Feature.Vector.style["default"];
var style, symbolizer;
style = new OpenLayers.Style();
symbolizer = style.createSymbolizer(feature);
t.eq(symbolizer.pointRadius, defaults.pointRadius, "symbolizer has the correct pointRadius");
t.eq(symbolizer.strokeWidth, defaults.strokeWidth, "symbolizer has the correct strokeWidth");
t.eq(symbolizer.fillColor, defaults.fillColor, "symbolizer has the correct fillColor");
t.eq(symbolizer.graphicName, defaults.graphicName, "symbolizer has the correct graphicName");
style = new OpenLayers.Style(null, {
defaultsPerSymbolizer: true,
rules: [
new OpenLayers.Rule({
symbolizer: {
stroke: true
}
})
]
});
symbolizer = style.createSymbolizer(feature);
t.eq(symbolizer.strokeWidth, defaults.strokeWidth, "symbolizer has the correct strokeWidth");
t.ok(symbolizer.fillColor == undefined, "fillColor is undefined");
style = new OpenLayers.Style(null, {
defaultsPerSymbolizer: true,
rules: [
new OpenLayers.Rule({
symbolizer: {
}
})
]
});
symbolizer = style.createSymbolizer(feature);
t.eq(symbolizer.pointRadius, defaults.pointRadius, "symbolizer has the correct pointRadius");
t.ok(symbolizer.strokeWidth == undefined, "strokeWidth is undefined");
t.ok(symbolizer.fillColor == undefined, "fillColor is undefined");
t.ok(symbolizer.graphicName == undefined, "graphicName is undefined");
style = new OpenLayers.Style(null, {
defaultsPerSymbolizer: true,
rules: [
new OpenLayers.Rule({
symbolizer: {
stroke: true
}
})
]
});
symbolizer = style.createSymbolizer(feature);
t.eq(symbolizer.strokeWidth, defaults.strokeWidth, "symbolizer has the correct strokeWidth");
t.ok(symbolizer.fillColor == undefined, "fillColor is undefined");
style = new OpenLayers.Style(null, {
defaultsPerSymbolizer: true,
rules: [
new OpenLayers.Rule({
symbolizer: {
fill: true
}
})
]
});
symbolizer = style.createSymbolizer(feature);
t.eq(symbolizer.fillColor, defaults.fillColor, "symbolizer has the correct fillColor");
t.ok(symbolizer.strokeWidth == undefined, "strokeWidth is undefined");
style = new OpenLayers.Style(null, {
defaultsPerSymbolizer: true,
rules: [
new OpenLayers.Rule({
symbolizer: {
graphic: true
}
})
]
});
symbolizer = style.createSymbolizer(feature);
t.eq(symbolizer.graphicName, defaults.graphicName, "symbolizer has the correct graphicName");
}
function test_Style_context(t) {
t.plan(4);
var rule = new OpenLayers.Rule({
symbolizer: {"Point": {externalGraphic: "${img1}"}},
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.LESS_THAN,
property: "size",
value: 11
})
});
var style = new OpenLayers.Style();
style.context = {
"img1": "myImage.png"
};
style.addRules([rule]);
var feature = new OpenLayers.Feature.Vector();
feature.attributes = {size: 10};
var styleHash = style.createSymbolizer(feature);
t.eq(styleHash.externalGraphic, "myImage.png", "correctly evaluated rule and calculated property styles from a custom context");
// same as above, but without rule (#1526)
style = new OpenLayers.Style(
{externalGraphic: "${getExternalGraphic}"},
{context: {
getExternalGraphic: function(feature) {
return "foo" + feature.attributes.size + ".png";
}
}});
t.eq(style.createSymbolizer(feature).externalGraphic, "foo10.png", "correctly evaluated symbolizer without rule");
style = new OpenLayers.Style(
{externalGraphic: "${getExternalGraphic}",
pointRadius: "${size}"},
{context: {
getExternalGraphic: function(feature) {
return "foo" + feature.attributes.size + ".png";
}
}});
t.eq(style.createSymbolizer(feature).externalGraphic, "foo10.png", "correctly evaluated symbolizer from context");
t.eq(style.createSymbolizer(feature).pointRadius, 10, "correctly evaluated symbolizer from attributes");
};
function test_Style_findPropertyStyles(t) {
t.plan(4);
var rule1 = new OpenLayers.Rule({symbolizer: {
pointRadius: 3,
externalGraphic: "${foo}.bar"
}});
var rule2 = new OpenLayers.Rule({symbolizer: {"Point": {
strokeWidth: "${foo}"
}}});
var style = new OpenLayers.Style({
strokeOpacity: 1,
strokeColor: "${foo}"
});
style.addRules([rule1, rule2]);
var propertyStyles = style.findPropertyStyles();
t.ok(propertyStyles.externalGraphic, "detected externalGraphic from rule correctly");
t.ok(propertyStyles.strokeWidth, "detected strokeWidth from Point symbolizer correctly");
t.ok(propertyStyles.strokeColor, "detected strokeColor from style correctly");
t.eq(typeof propertyStyles.pointRadius, "undefined", "correctly detected pointRadius as non-property style");
}
function test_createLiteral(t) {
t.plan(6);
var value, context, feature, result, expected;
var func = OpenLayers.Style.createLiteral;
// without templates
value = "foo";
expected = value;
result = func(value);
t.eq(result, expected, "(no template) preserves literal");
// with templates
value = "${foo}"
expected = "bar";
context = {foo: expected};
result = func(value, context);
t.eq(result, expected, "(template) preserves literal");
expected = "";
context = {foo: expected};
result = func(value, context);
t.eq(result, expected, "(template) preserves empty string");
expected = "16/03/2008";
context = {foo: expected};
result = func(value, context);
t.eq(result, expected, "(template) preserves string with numbers");
expected = 16;
context = {foo: expected + ""};
result = func(value, context);
t.eq(result, expected, "(template) casts integer in a string");
expected = 16;
context = {foo: " " + expected + " "};
result = func(value, context);
t.eq(result, expected, "(template) casts integer in a space padded string");
}
function test_clone(t) {
t.plan(7);
var style = new OpenLayers.Style({bar: "baz"}, {
name: "test style",
rules: [new OpenLayers.Rule({
name: "test rule"
})],
context: {
foo: "bar"
}
});
var clone = style.clone();
t.eq(clone.name, "test style", "name copied");
t.eq(clone.rules[0].name, "test rule", "clone has correct rule");
// modify original
style.name = "new";
style.addRules([new OpenLayers.Rule({
name: "new rule"
})]);
style.context.foo = "baz";
// confirm that clone didn't change
t.eq(clone.defaultStyle.bar, "baz", "clone has clone of defaultStyle");
t.eq(clone.name, "test style", "clone has clone of name");
t.eq(clone.rules.length, 1, "clone has clone of rules");
t.eq(clone.context.foo, "bar", "clone has clone of context");
// confirm that ids are different
t.ok(clone.id !== style.id, "clone has different id");
style.destroy();
clone.destroy();
}
function test_Style_destroy(t) {
t.plan(1);
var style = new OpenLayers.Style();
style.destroy();
t.eq(style.rules, null, "rules array nulled properly");
}
</script>
</head>
<body>
<div id="map" style="width:500px;height:500px"></div>
</body>
</html>
|