3 <script src="../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_Style_constructor(t) {
11 symbolizer: {fillColor: "red"},
12 filter: new OpenLayers.Filter.Comparison({
13 type: OpenLayers.Filter.Comparison.EQUAL_TO,
19 symbolizer: {fillColor: "yellow"},
20 filter: new OpenLayers.Filter.Comparison({
21 type: OpenLayers.Filter.Comparison.EQUAL_TO,
27 var style = new OpenLayers.Style(null, {
31 t.ok(style instanceof OpenLayers.Style,
32 "new OpenLayers.Style returns object" );
33 t.eq(style.foo, "bar", "constructor sets options correctly");
34 t.eq(style.rules.length, 2, "correct number of rules added");
35 t.ok(style.rules[0] === rules[0], "correct first rule added");
36 t.ok(style.rules[1] === rules[1], "correct second rule added");
37 t.eq(typeof style.createSymbolizer, "function", "style has a createSymbolizer function");
40 function test_Style_create(t) {
43 var map = new OpenLayers.Map("map");
45 var layer = new OpenLayers.Layer.Vector("layer");
47 var baseStyle = OpenLayers.Util.extend(
48 OpenLayers.Feature.Vector.style["default"],
49 {externalGraphic: "bar${foo}.png"}
52 var style = new OpenLayers.Style(baseStyle);
54 var rule1 = new OpenLayers.Rule({
55 symbolizer: {"Point": {fillColor: "green"}},
56 maxScaleDenominator: 500000,
57 filter: new OpenLayers.Filter.FeatureId({
61 var rule2 = new OpenLayers.Rule({
62 symbolizer: {"Point": {fillColor: "yellow"}},
63 minScaleDenominator: 500000,
64 maxScaleDenominator: 1000000,
65 filter: new OpenLayers.Filter.FeatureId({
69 var rule3 = new OpenLayers.Rule({
70 symbolizer: {"Point": {fillColor: "red"}},
71 minScaleDenominator: 1000000,
72 maxScaleDenominator: 2500000,
73 filter: new OpenLayers.Filter.FeatureId({
78 var feature = new OpenLayers.Feature.Vector(
79 new OpenLayers.Geometry.Point(3,5),
84 // for this fid, the above rule should apply
86 layer.styleMap = new OpenLayers.StyleMap(style);
88 layer.addFeatures([feature]);
90 map.setBaseLayer(layer);
92 map.setCenter(new OpenLayers.LonLat(3,5), 10);
94 var createdStyle = style.createSymbolizer(feature);
95 t.eq(createdStyle.externalGraphic, "barbar.png", "Calculated property style for default symbolizer correctly.");
97 style.addRules([rule1, rule2, rule3]);
98 createdStyle = style.createSymbolizer(feature);
100 // at this scale, the feature should be green
101 t.eq(createdStyle.display, undefined, "Feature is visible at scale "+map.getScale());
102 t.eq(createdStyle.fillColor, "green", "Point symbolizer from rule applied correctly.");
104 map.setCenter(new OpenLayers.LonLat(3,5), 9);
105 // at this scale, the feature should be red
106 createdStyle = style.createSymbolizer(feature);
107 t.eq(createdStyle.display, undefined, "Feature is visible at scale "+map.getScale());
108 t.eq(createdStyle.fillColor, "yellow", "Point symbolizer from rule applied correctly.");
110 map.setCenter(new OpenLayers.LonLat(3,5), 8);
111 // at this scale, the feature should be yellow
112 createdStyle = style.createSymbolizer(feature);
113 t.eq(createdStyle.display, undefined, "Feature is visible at scale "+map.getScale());
114 t.eq(createdStyle.fillColor, "red", "Point symbolizer from rule applied correctly.");
116 map.setCenter(new OpenLayers.LonLat(3,5), 7);
117 // at this scale, the feature should be invisible
118 createdStyle = style.createSymbolizer(feature);
119 t.eq(createdStyle.display, "none", "Feature is invisible at scale "+map.getScale());
120 t.eq(createdStyle.fillColor, baseStyle.fillColor, "Point symbolizer from base style applied correctly.");
123 // now the rule should not apply
125 createdStyle = style.createSymbolizer(feature);
126 t.eq(createdStyle.fillColor, baseStyle.fillColor, "Correct style for rule that does not apply to fid=\"2\".");
129 function test_Style_createSymbolizer(t) {
131 var style = new OpenLayers.Style();
132 var rule = new OpenLayers.Rule({
135 var elseRule = new OpenLayers.Rule({
139 style.addRules([rule, elseRule]);
141 // test that applySymbolizer is only called with rule
142 style.applySymbolizer = function(r) {
143 t.eq(r.id, rule.id, "(plain) applySymbolizer called with correct rule");
145 style.createSymbolizer(new OpenLayers.Feature.Vector());
147 rule.evaluate = function() {return false;};
148 style.applySymbolizer = function(r) {
149 t.eq(r.id, elseRule.id, "(else) applySymbolizer called with correct rule");
151 style.createSymbolizer(new OpenLayers.Feature.Vector());
154 function test_Style_applySymbolizer(t) {
157 var feature = new OpenLayers.Feature.Vector();
158 var defaults = OpenLayers.Feature.Vector.style["default"];
159 var style, symbolizer;
161 style = new OpenLayers.Style();
162 symbolizer = style.createSymbolizer(feature);
163 t.eq(symbolizer.pointRadius, defaults.pointRadius, "symbolizer has the correct pointRadius");
164 t.eq(symbolizer.strokeWidth, defaults.strokeWidth, "symbolizer has the correct strokeWidth");
165 t.eq(symbolizer.fillColor, defaults.fillColor, "symbolizer has the correct fillColor");
166 t.eq(symbolizer.graphicName, defaults.graphicName, "symbolizer has the correct graphicName");
168 style = new OpenLayers.Style(null, {
169 defaultsPerSymbolizer: true,
171 new OpenLayers.Rule({
178 symbolizer = style.createSymbolizer(feature);
179 t.eq(symbolizer.strokeWidth, defaults.strokeWidth, "symbolizer has the correct strokeWidth");
180 t.ok(symbolizer.fillColor == undefined, "fillColor is undefined");
182 style = new OpenLayers.Style(null, {
183 defaultsPerSymbolizer: true,
185 new OpenLayers.Rule({
191 symbolizer = style.createSymbolizer(feature);
192 t.eq(symbolizer.pointRadius, defaults.pointRadius, "symbolizer has the correct pointRadius");
193 t.ok(symbolizer.strokeWidth == undefined, "strokeWidth is undefined");
194 t.ok(symbolizer.fillColor == undefined, "fillColor is undefined");
195 t.ok(symbolizer.graphicName == undefined, "graphicName is undefined");
197 style = new OpenLayers.Style(null, {
198 defaultsPerSymbolizer: true,
200 new OpenLayers.Rule({
207 symbolizer = style.createSymbolizer(feature);
208 t.eq(symbolizer.strokeWidth, defaults.strokeWidth, "symbolizer has the correct strokeWidth");
209 t.ok(symbolizer.fillColor == undefined, "fillColor is undefined");
211 style = new OpenLayers.Style(null, {
212 defaultsPerSymbolizer: true,
214 new OpenLayers.Rule({
221 symbolizer = style.createSymbolizer(feature);
222 t.eq(symbolizer.fillColor, defaults.fillColor, "symbolizer has the correct fillColor");
223 t.ok(symbolizer.strokeWidth == undefined, "strokeWidth is undefined");
225 style = new OpenLayers.Style(null, {
226 defaultsPerSymbolizer: true,
228 new OpenLayers.Rule({
235 symbolizer = style.createSymbolizer(feature);
236 t.eq(symbolizer.graphicName, defaults.graphicName, "symbolizer has the correct graphicName");
239 function test_Style_context(t) {
241 var rule = new OpenLayers.Rule({
242 symbolizer: {"Point": {externalGraphic: "${img1}"}},
243 filter: new OpenLayers.Filter.Comparison({
244 type: OpenLayers.Filter.Comparison.LESS_THAN,
249 var style = new OpenLayers.Style();
251 "img1": "myImage.png"
253 style.addRules([rule]);
254 var feature = new OpenLayers.Feature.Vector();
255 feature.attributes = {size: 10};
256 var styleHash = style.createSymbolizer(feature);
257 t.eq(styleHash.externalGraphic, "myImage.png", "correctly evaluated rule and calculated property styles from a custom context");
259 // same as above, but without rule (#1526)
260 style = new OpenLayers.Style(
261 {externalGraphic: "${getExternalGraphic}"},
263 getExternalGraphic: function(feature) {
264 return "foo" + feature.attributes.size + ".png";
267 t.eq(style.createSymbolizer(feature).externalGraphic, "foo10.png", "correctly evaluated symbolizer without rule");
270 function test_Style_findPropertyStyles(t) {
272 var rule1 = new OpenLayers.Rule({symbolizer: {
274 externalGraphic: "${foo}.bar"
276 var rule2 = new OpenLayers.Rule({symbolizer: {"Point": {
277 strokeWidth: "${foo}"
279 var style = new OpenLayers.Style({
281 strokeColor: "${foo}"
283 style.addRules([rule1, rule2]);
284 var propertyStyles = style.findPropertyStyles();
285 t.ok(propertyStyles.externalGraphic, "detected externalGraphic from rule correctly");
286 t.ok(propertyStyles.strokeWidth, "detected strokeWidth from Point symbolizer correctly");
287 t.ok(propertyStyles.strokeColor, "detected strokeColor from style correctly");
288 t.eq(typeof propertyStyles.pointRadius, "undefined", "correctly detected pointRadius as non-property style");
291 function test_createLiteral(t) {
294 var value, context, feature, result, expected;
295 var func = OpenLayers.Style.createLiteral;
300 result = func(value);
301 t.eq(result, expected, "(no template) preserves literal");
306 context = {foo: expected};
307 result = func(value, context);
308 t.eq(result, expected, "(template) preserves literal");
311 context = {foo: expected};
312 result = func(value, context);
313 t.eq(result, expected, "(template) preserves empty string");
315 expected = "16/03/2008";
316 context = {foo: expected};
317 result = func(value, context);
318 t.eq(result, expected, "(template) preserves string with numbers");
321 context = {foo: expected + ""};
322 result = func(value, context);
323 t.eq(result, expected, "(template) casts integer in a string");
326 context = {foo: " " + expected + " "};
327 result = func(value, context);
328 t.eq(result, expected, "(template) casts integer in a space padded string");
332 function test_Style_destroy(t) {
335 var style = new OpenLayers.Style();
337 t.eq(style.rules, null, "rules array nulled properly");
343 <div id="map" style="width:500px;height:500px"></div>