1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.nio.file.Files;
|
---|
9 | import java.nio.file.Paths;
|
---|
10 | import java.util.Arrays;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.BeforeEach;
|
---|
13 | import org.junit.jupiter.api.Disabled;
|
---|
14 | import org.junit.jupiter.api.Test;
|
---|
15 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
21 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
24 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
---|
25 | import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
|
---|
26 | import org.openstreetmap.josm.io.OsmReader;
|
---|
27 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
28 |
|
---|
29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests of {@link ChildOrParentSelector}.
|
---|
33 | */
|
---|
34 | class ChildOrParentSelectorTest {
|
---|
35 |
|
---|
36 | private DataSet ds;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Setup rule
|
---|
40 | */
|
---|
41 | @RegisterExtension
|
---|
42 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
43 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Setup test
|
---|
47 | */
|
---|
48 | @BeforeEach
|
---|
49 | public void setUp() {
|
---|
50 | ds = new DataSet();
|
---|
51 | }
|
---|
52 |
|
---|
53 | Relation relation(int id) {
|
---|
54 | Relation r = new Relation(id, 1);
|
---|
55 | ds.addPrimitive(r);
|
---|
56 | return r;
|
---|
57 | }
|
---|
58 |
|
---|
59 | Node node(int id) {
|
---|
60 | Node n = new Node(id, 1);
|
---|
61 | n.setCoor(LatLon.ZERO);
|
---|
62 | ds.addPrimitive(n);
|
---|
63 | return n;
|
---|
64 | }
|
---|
65 |
|
---|
66 | Way way(int id) {
|
---|
67 | Way w = new Way(id, 1);
|
---|
68 | ds.addPrimitive(w);
|
---|
69 | return w;
|
---|
70 | }
|
---|
71 |
|
---|
72 | ChildOrParentSelector parse(String css) {
|
---|
73 | MapCSSStyleSource source = new MapCSSStyleSource(css);
|
---|
74 | source.loadStyleSource();
|
---|
75 | assertEquals(1, source.rules.size());
|
---|
76 | return (ChildOrParentSelector) source.rules.get(0).selectors.get(0);
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Test
|
---|
80 | @Disabled
|
---|
81 | public void matches_1() {
|
---|
82 | String css = "relation >[role=\"my_role\"] node {}";
|
---|
83 | ChildOrParentSelector selector = parse(css);
|
---|
84 |
|
---|
85 | Relation r = relation(1);
|
---|
86 | Node n = node(1);
|
---|
87 | r.addMember(new RelationMember("my_role", n));
|
---|
88 | Environment e = new Environment().withChild(n);
|
---|
89 |
|
---|
90 | assertTrue(selector.matches(e));
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Test
|
---|
94 | @Disabled
|
---|
95 | public void matches_2() {
|
---|
96 | String css = "relation >[\"my_role\"] node {}";
|
---|
97 | ChildOrParentSelector selector = parse(css);
|
---|
98 |
|
---|
99 | Relation r = relation(1);
|
---|
100 | Node n = node(1);
|
---|
101 | r.addMember(new RelationMember("my_role", n));
|
---|
102 | Environment e = new Environment().withChild(n);
|
---|
103 |
|
---|
104 | assertTrue(selector.matches(e));
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Test
|
---|
108 | @Disabled
|
---|
109 | public void matches_3() {
|
---|
110 | String css = "relation >[!\"my_role\"] node {}";
|
---|
111 | ChildOrParentSelector selector = parse(css);
|
---|
112 |
|
---|
113 | Relation r = relation(1);
|
---|
114 | Node n = node(1);
|
---|
115 | r.addMember(new RelationMember("my_role", n));
|
---|
116 | Environment e = new Environment().withChild(n);
|
---|
117 |
|
---|
118 | assertFalse(selector.matches(e));
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Test
|
---|
122 | @Disabled
|
---|
123 | public void matches_4() {
|
---|
124 | String css = "way < relation {}";
|
---|
125 | ChildOrParentSelector selector = parse(css);
|
---|
126 | assertEquals(Selector.ChildOrParentSelectorType.PARENT, selector.type);
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Test
|
---|
131 | public void matches_5() {
|
---|
132 | String css = "way <[role != \"my_role\"] relation {text: index();}";
|
---|
133 | ChildOrParentSelector selector = parse(css);
|
---|
134 | assertEquals(Selector.ChildOrParentSelectorType.PARENT, selector.type);
|
---|
135 |
|
---|
136 | Relation r = relation(1);
|
---|
137 | Way w1 = way(1);
|
---|
138 | w1.setNodes(Arrays.asList(node(11), node(12)));
|
---|
139 |
|
---|
140 | Way w2 = way(2);
|
---|
141 | w2.setNodes(Arrays.asList(node(21), node(22)));
|
---|
142 |
|
---|
143 | Way w3 = way(3);
|
---|
144 | w3.setNodes(Arrays.asList(node(31), node(32)));
|
---|
145 |
|
---|
146 | r.addMember(new RelationMember("my_role", w1));
|
---|
147 | r.addMember(new RelationMember("my_role", w2));
|
---|
148 | r.addMember(new RelationMember("another role", w3));
|
---|
149 | r.addMember(new RelationMember("yet another role", w3));
|
---|
150 |
|
---|
151 | Environment e = new Environment(r, new MultiCascade(), Environment.DEFAULT_LAYER, null);
|
---|
152 | assertTrue(selector.matches(e));
|
---|
153 |
|
---|
154 | MapCSSStyleSource source = new MapCSSStyleSource(css);
|
---|
155 | source.loadStyleSource();
|
---|
156 | source.rules.get(0).declaration.execute(e);
|
---|
157 | assertEquals(Float.valueOf(3f), e.getCascade(Environment.DEFAULT_LAYER).get("text", null, Float.class));
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Test
|
---|
161 | public void matches_6() {
|
---|
162 | String css = "relation >[role != \"my_role\"] way {}";
|
---|
163 | ChildOrParentSelector selector = parse(css);
|
---|
164 |
|
---|
165 | Relation r = relation(1);
|
---|
166 | Way w1 = way(1);
|
---|
167 | w1.setNodes(Arrays.asList(node(11), node(12)));
|
---|
168 |
|
---|
169 | Way w2 = way(2);
|
---|
170 | w2.setNodes(Arrays.asList(node(21), node(22)));
|
---|
171 |
|
---|
172 | Way w3 = way(3);
|
---|
173 | w3.setNodes(Arrays.asList(node(31), node(32)));
|
---|
174 |
|
---|
175 | r.addMember(new RelationMember("my_role", w1));
|
---|
176 | r.addMember(new RelationMember("my_role", w2));
|
---|
177 | r.addMember(new RelationMember("another role", w3));
|
---|
178 |
|
---|
179 | Environment e = new Environment(w1);
|
---|
180 | assertFalse(selector.matches(e));
|
---|
181 |
|
---|
182 | e = new Environment(w2);
|
---|
183 | assertFalse(selector.matches(e));
|
---|
184 |
|
---|
185 | e = new Environment(w3);
|
---|
186 | assertTrue(selector.matches(e));
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Test inside/contains selectors (spatial test)
|
---|
191 | * @throws Exception in case of any error
|
---|
192 | */
|
---|
193 | @Test
|
---|
194 | void testContains() throws Exception {
|
---|
195 | ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get("nodist/data/amenity-in-amenity.osm")), null);
|
---|
196 | ChildOrParentSelector css = parse("node[tag(\"amenity\") = parent_tag(\"amenity\")] ∈ *[amenity] {}");
|
---|
197 | assertFalse(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.NODE))));
|
---|
198 | assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY))));
|
---|
199 | assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION))));
|
---|
200 | css = parse("node[tag(\"amenity\") = parent_tag(\"amenity\")] ⊆ *[amenity] {}");
|
---|
201 | assertFalse(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.NODE))));
|
---|
202 | assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY))));
|
---|
203 | assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION))));
|
---|
204 | css = parse("node[tag(\"amenity\") = parent_tag(\"amenity\")] ⊈ *[amenity] {}");
|
---|
205 | assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.NODE))));
|
---|
206 | assertFalse(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY))));
|
---|
207 | assertFalse(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION))));
|
---|
208 | css = parse("*[tag(\"amenity\") = parent_tag(\"amenity\")] ⊇ *[amenity] {}");
|
---|
209 | assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.NODE))));
|
---|
210 | assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY))));
|
---|
211 | assertFalse(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION))));
|
---|
212 | css = parse("*[tag(\"amenity\") = parent_tag(\"amenity\")] ⊉ *[amenity] {}");
|
---|
213 | assertFalse(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.NODE))));
|
---|
214 | assertFalse(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.WAY))));
|
---|
215 | assertTrue(css.matches(new Environment(ds.getPrimitiveById(123, OsmPrimitiveType.RELATION))));
|
---|
216 | }
|
---|
217 | }
|
---|