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