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