source: josm/trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.groovy@ 6774

Last change on this file since 6774 was 6774, checked in by simon04, 10 years ago

fix #9633 - MapCSS: allow (named) colours with alpha

File size: 11.4 KB
Line 
1package org.openstreetmap.josm.gui.mappaint.mapcss
2
3import org.junit.Before
4import org.junit.Test
5import org.openstreetmap.TestUtils
6import org.openstreetmap.josm.Main
7import org.openstreetmap.josm.data.Preferences
8import org.openstreetmap.josm.data.osm.OsmPrimitive
9import org.openstreetmap.josm.data.osm.Way
10import org.openstreetmap.josm.gui.mappaint.Environment
11import org.openstreetmap.josm.gui.mappaint.MultiCascade
12import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser
13import org.openstreetmap.josm.tools.ColorHelper
14import org.openstreetmap.josm.tools.Utils
15
16import java.awt.Color
17
18class MapCSSParserTest {
19
20 protected static OsmPrimitive getPrimitive(String key, String value) {
21 def w = new Way()
22 w.put(key, value)
23 return w
24 }
25
26 protected static Environment getEnvironment(String key, String value) {
27 return new Environment().withPrimitive(getPrimitive(key, value))
28 }
29
30 protected static MapCSSParser getParser(String stringToParse) {
31 return new MapCSSParser(new StringReader(stringToParse));
32 }
33
34 @Before
35 public void setUp() throws Exception {
36 Main.pref = new Preferences()
37 }
38
39 @Test
40 public void testKothicStylesheets() throws Exception {
41 new MapCSSParser(new URL("http://kothic.googlecode.com/hg/src/styles/default.mapcss").openStream(), "UTF-8")
42 new MapCSSParser(new URL("http://kothic.googlecode.com/hg/src/styles/mapink.mapcss").openStream(), "UTF-8")
43 }
44
45 @Test
46 public void testDeclarations() {
47 getParser("{ opacity: 0.5; color: rgb(1.0, 0.0, 0.0); }").declaration()
48 getParser("{ set tag=value; }").declaration() //set a tag
49 getParser("{ set tag; }").declaration() // set a tag to 'yes'
50 getParser("{ opacity: eval(\"tag('population')/100000\"); }").declaration()
51 getParser("{ set width_in_metres=eval(\"tag('lanes')*3\"); }").declaration()
52 }
53
54 @Test
55 public void testClassCondition() throws Exception {
56 def conditions = ((Selector.GeneralSelector) getParser("way[name=X].highway:closed").selector()).conds
57 assert conditions.get(0) instanceof Condition.KeyValueCondition
58 assert conditions.get(0).applies(getEnvironment("name", "X"))
59 assert conditions.get(1) instanceof Condition.ClassCondition
60 assert conditions.get(2) instanceof Condition.PseudoClassCondition
61 }
62
63 @Test
64 public void testClassMatching() throws Exception {
65 def css = new MapCSSStyleSource("")
66 getParser("" +
67 "way[highway=footway] { set .path; color: #FF6644; width: 2; }\n" +
68 "way[highway=path] { set path; color: brown; width: 2; }\n" +
69 "way[\"set\"=escape] { }\n" +
70 "way.path { text:auto; text-color: green; text-position: line; text-offset: 5; }\n" +
71 "way!.path { color: orange; }\n"
72 ).sheet(css)
73 assert css.getErrors().isEmpty()
74 def mc1 = new MultiCascade()
75 css.apply(mc1, getPrimitive("highway", "path"), 1, null, false);
76 assert "green".equals(mc1.getCascade("default").get("text-color", null, String.class))
77 assert "brown".equals(mc1.getCascade("default").get("color", null, String.class))
78 def mc2 = new MultiCascade()
79 css.apply(mc2, getPrimitive("highway", "residential"), 1, null, false);
80 assert "orange".equals(mc2.getCascade("default").get("color", null, String.class))
81 assert mc2.getCascade("default").get("text-color", null, String.class) == null
82 def mc3 = new MultiCascade()
83 css.apply(mc3, getPrimitive("highway", "footway"), 1, null, false);
84 assert ColorHelper.html2color("#FF6644").equals(mc3.getCascade("default").get("color", null, Color.class))
85 }
86
87 @Test
88 public void testEqualCondition() throws Exception {
89 def condition = (Condition.KeyValueCondition) getParser("[surface=paved]").condition(Condition.Context.PRIMITIVE)
90 assert condition instanceof Condition.KeyValueCondition
91 assert Condition.Op.EQ.equals(condition.op)
92 assert "surface".equals(condition.k)
93 assert "paved".equals(condition.v)
94 assert condition.applies(getEnvironment("surface", "paved"))
95 assert !condition.applies(getEnvironment("surface", "unpaved"))
96 }
97
98 @Test
99 public void testNotEqualCondition() throws Exception {
100 def condition = (Condition.KeyValueCondition) getParser("[surface!=paved]").condition(Condition.Context.PRIMITIVE)
101 assert Condition.Op.NEQ.equals(condition.op)
102 assert !condition.applies(getEnvironment("surface", "paved"))
103 assert condition.applies(getEnvironment("surface", "unpaved"))
104 }
105
106 @Test
107 public void testRegexCondition() throws Exception {
108 def condition = (Condition.KeyValueCondition) getParser("[surface=~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE)
109 assert Condition.Op.REGEX.equals(condition.op)
110 assert condition.applies(getEnvironment("surface", "unpaved"))
111 assert !condition.applies(getEnvironment("surface", "grass"))
112 }
113
114 @Test
115 public void testNegatedRegexCondition() throws Exception {
116 def condition = (Condition.KeyValueCondition) getParser("[surface!~/paved|unpaved/]").condition(Condition.Context.PRIMITIVE)
117 assert Condition.Op.NREGEX.equals(condition.op)
118 assert !condition.applies(getEnvironment("surface", "unpaved"))
119 assert condition.applies(getEnvironment("surface", "grass"))
120 }
121
122 @Test
123 public void testStandardKeyCondition() throws Exception {
124 def c1 = (Condition.KeyCondition) getParser("[ highway ]").condition(Condition.Context.PRIMITIVE)
125 assert c1.matchType == null
126 assert c1.applies(getEnvironment("highway", "unclassified"))
127 assert !c1.applies(getEnvironment("railway", "rail"))
128 def c2 = (Condition.KeyCondition) getParser("[\"/slash/\"]").condition(Condition.Context.PRIMITIVE)
129 assert c2.matchType == null
130 assert c2.applies(getEnvironment("/slash/", "yes"))
131 assert !c2.applies(getEnvironment("\"slash\"", "no"))
132 }
133
134 @Test
135 public void testYesNoKeyCondition() throws Exception {
136 def c1 = (Condition.KeyCondition) getParser("[oneway?]").condition(Condition.Context.PRIMITIVE)
137 def c2 = (Condition.KeyCondition) getParser("[oneway?!]").condition(Condition.Context.PRIMITIVE)
138 def c3 = (Condition.KeyCondition) getParser("[!oneway?]").condition(Condition.Context.PRIMITIVE)
139 def c4 = (Condition.KeyCondition) getParser("[!oneway?!]").condition(Condition.Context.PRIMITIVE)
140 def yes = getEnvironment("oneway", "yes")
141 def no = getEnvironment("oneway", "no")
142 def none = getEnvironment("no-oneway", "foo")
143 assert c1.applies(yes)
144 assert !c1.applies(no)
145 assert !c1.applies(none)
146 assert !c2.applies(yes)
147 assert c2.applies(no)
148 assert !c2.applies(none)
149 assert !c3.applies(yes)
150 assert c3.applies(no)
151 assert c3.applies(none)
152 assert c4.applies(yes)
153 assert !c4.applies(no)
154 assert c4.applies(none)
155 }
156
157 @Test
158 public void testRegexKeyCondition() throws Exception {
159 def c1 = (Condition.KeyCondition) getParser("[/.*:(backward|forward)\$/]").condition(Condition.Context.PRIMITIVE)
160 assert Condition.KeyMatchType.REGEX.equals(c1.matchType)
161 assert !c1.applies(getEnvironment("lanes", "3"))
162 assert c1.applies(getEnvironment("lanes:forward", "3"))
163 assert c1.applies(getEnvironment("lanes:backward", "3"))
164 assert !c1.applies(getEnvironment("lanes:foobar", "3"))
165 }
166
167 @Test
168 public void testKeyKeyCondition() throws Exception {
169 def c1 = (Condition.KeyValueCondition) getParser("[foo = *bar]").condition(Condition.Context.PRIMITIVE)
170 def w1 = new Way()
171 w1.put("foo", "123")
172 w1.put("bar", "456")
173 assert !c1.applies(new Environment().withPrimitive(w1))
174 w1.put("bar", "123")
175 assert c1.applies(new Environment().withPrimitive(w1))
176 def c2 = (Condition.KeyValueCondition) getParser("[foo =~ */bar/]").condition(Condition.Context.PRIMITIVE)
177 def w2 = new Way(w1)
178 w2.put("bar", "[0-9]{3}")
179 assert c2.applies(new Environment().withPrimitive(w2))
180 w2.put("bar", "[0-9]")
181 assert c2.applies(new Environment().withPrimitive(w2))
182 w2.put("bar", "^[0-9]\$")
183 assert !c2.applies(new Environment().withPrimitive(w2))
184 }
185
186 @Test
187 public void testTicket8568() throws Exception {
188 def sheet = new MapCSSStyleSource("")
189 getParser("" +
190 "way { width: 5; }\n" +
191 "way[keyA], way[keyB] { width: eval(prop(width)+10); }").sheet(sheet)
192 def mc = new MultiCascade()
193 sheet.apply(mc, TestUtils.createPrimitive("way foo=bar"), 20, null, false)
194 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 5
195 sheet.apply(mc, TestUtils.createPrimitive("way keyA=true"), 20, null, false)
196 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
197 sheet.apply(mc, TestUtils.createPrimitive("way keyB=true"), 20, null, false)
198 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
199 sheet.apply(mc, TestUtils.createPrimitive("way keyA=true keyB=true"), 20, null, false)
200 assert mc.getCascade(Environment.DEFAULT_LAYER).get("width") == 15
201 }
202
203 @Test
204 public void testTicket80711() throws Exception {
205 def sheet = new MapCSSStyleSource("")
206 getParser("*[rcn_ref], *[name] {text: concat(tag(rcn_ref), \" \", tag(name)); }").sheet(sheet)
207 def mc = new MultiCascade()
208 sheet.apply(mc, TestUtils.createPrimitive("way name=Foo"), 20, null, false)
209 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == " Foo"
210 sheet.apply(mc, TestUtils.createPrimitive("way rcn_ref=15"), 20, null, false)
211 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 "
212 sheet.apply(mc, TestUtils.createPrimitive("way rcn_ref=15 name=Foo"), 20, null, false)
213 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 Foo"
214
215 sheet = new MapCSSStyleSource("")
216 getParser("*[rcn_ref], *[name] {text: join(\" - \", tag(rcn_ref), tag(ref), tag(name)); }").sheet(sheet)
217 sheet.apply(mc, TestUtils.createPrimitive("way rcn_ref=15 ref=1.5 name=Foo"), 20, null, false)
218 assert mc.getCascade(Environment.DEFAULT_LAYER).get("text") == "15 - 1.5 - Foo"
219 }
220
221 @Test
222 public void testColorNameTicket9191() throws Exception {
223 def e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null)
224 getParser("{color: testcolour1#88DD22}").declaration().get(0).execute(e)
225 def expected = new Color(0x88DD22)
226 assert e.getCascade(Environment.DEFAULT_LAYER).get("color") == expected
227 assert Main.pref.getDefaultColor("mappaint.mapcss.testcolour1") == expected
228 }
229
230 @Test
231 public void testColorNameTicket9191Alpha() throws Exception {
232 def e = new Environment(null, new MultiCascade(), Environment.DEFAULT_LAYER, null)
233 getParser("{color: testcolour2#12345678}").declaration().get(0).execute(e)
234 def expected = new Color(0x12, 0x34, 0x56, 0x78)
235 assert e.getCascade(Environment.DEFAULT_LAYER).get("color") == expected
236 assert Main.pref.getDefaultColor("mappaint.mapcss.testcolour2") == expected
237 }
238
239 @Test
240 public void testColorParsing() throws Exception {
241 assert ColorHelper.html2color("#12345678") == new Color(0x12, 0x34, 0x56, 0x78)
242 }
243}
Note: See TracBrowser for help on using the repository browser.