1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertSame;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.lang.reflect.Field;
|
---|
8 | import java.lang.reflect.Method;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 |
|
---|
13 | import org.junit.Rule;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
24 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
25 | import org.openstreetmap.josm.tools.GeoProperty;
|
---|
26 | import org.openstreetmap.josm.tools.GeoPropertyIndex;
|
---|
27 | import org.openstreetmap.josm.tools.Geometry;
|
---|
28 | import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
|
---|
29 | import org.openstreetmap.josm.tools.Utils;
|
---|
30 |
|
---|
31 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Unit tests for class {@link CreateCircleAction}.
|
---|
35 | */
|
---|
36 | public final class CreateCircleActionTest {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Setup test.
|
---|
40 | */
|
---|
41 | @Rule
|
---|
42 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
43 | public JOSMTestRules test = new JOSMTestRules().platform().projection().commands();
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * FIXME: Conveniance method to prevent Selection Change events.
|
---|
47 | * A more proper way should be to define a TestingDataSet class with limited
|
---|
48 | * functionalities, but DataSet is declare as final (due to Cloneable interface).
|
---|
49 | *
|
---|
50 | * I don't know why, but in other tests there are no problem to add selected primitives
|
---|
51 | * but in this case there is a problem with an even listener of selection change.
|
---|
52 | * @param p primitive
|
---|
53 | * @param ds data set
|
---|
54 | * @throws ReflectiveOperationException if an error occurs
|
---|
55 | */
|
---|
56 | public void addSelected(OsmPrimitive p, DataSet ds) throws ReflectiveOperationException {
|
---|
57 | Method method = ds.getClass().getDeclaredMethod("addSelected",
|
---|
58 | new Class<?>[] {Collection.class, boolean.class});
|
---|
59 | Utils.setObjectsAccessible(method);
|
---|
60 | method.invoke(ds, Collections.singleton(p), false);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Test case: When Create Circle action is performed with a single way selected,
|
---|
65 | * circle direction must equals way direction.
|
---|
66 | * see #7421
|
---|
67 | * @throws ReflectiveOperationException if an error occurs
|
---|
68 | */
|
---|
69 | @Test
|
---|
70 | public void testTicket7421case0() throws ReflectiveOperationException {
|
---|
71 | DataSet dataSet = new DataSet();
|
---|
72 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
73 |
|
---|
74 | Node n1 = new Node(new EastNorth(0, 0));
|
---|
75 | Node n2 = new Node(new EastNorth(-1, 1));
|
---|
76 | Node n3 = new Node(new EastNorth(1, 1));
|
---|
77 | dataSet.addPrimitive(n1);
|
---|
78 | dataSet.addPrimitive(n2);
|
---|
79 | dataSet.addPrimitive(n3);
|
---|
80 |
|
---|
81 | Way w = new Way(); // Way is Clockwize
|
---|
82 | w.setNodes(Arrays.asList(new Node[] {n1, n2, n3}));
|
---|
83 | dataSet.addPrimitive(w);
|
---|
84 |
|
---|
85 | addSelected(w, dataSet);
|
---|
86 |
|
---|
87 | CreateCircleAction action = new CreateCircleAction();
|
---|
88 | action.setEnabled(true);
|
---|
89 | try {
|
---|
90 | Main.getLayerManager().addLayer(layer);
|
---|
91 | action.actionPerformed(null);
|
---|
92 | } finally {
|
---|
93 | // Ensure we clean the place before leaving, even if test fails.
|
---|
94 | Main.getLayerManager().removeLayer(layer);
|
---|
95 | }
|
---|
96 |
|
---|
97 | // Expected result: Dataset contain one closed way, clockwise
|
---|
98 | Collection<Way> resultingWays = dataSet.getWays();
|
---|
99 | assertSame(String.format("Expect one way after perform action. %d found", resultingWays.size()),
|
---|
100 | resultingWays.size(), 1);
|
---|
101 | Way resultingWay = resultingWays.iterator().next();
|
---|
102 | assertTrue("Resulting way is not closed",
|
---|
103 | resultingWay.isClosed());
|
---|
104 | assertTrue("Found anti-clockwize circle while way was clockwize",
|
---|
105 | Geometry.isClockwise(resultingWay));
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Mock left/right hand traffic database with constant traffic hand
|
---|
110 | */
|
---|
111 | private static class ConstantTrafficHand implements GeoProperty<Boolean> {
|
---|
112 | boolean isLeft;
|
---|
113 |
|
---|
114 | ConstantTrafficHand(boolean isLeft) {
|
---|
115 | this.isLeft = isLeft;
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public Boolean get(LatLon ll) {
|
---|
120 | return isLeft;
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public Boolean get(BBox box) {
|
---|
125 | return isLeft;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Test case: When Create Circle action is performed with nodes, resulting
|
---|
131 | * circle direction depend on traffic hand. Simulate a left hand traffic.
|
---|
132 | * see #7421
|
---|
133 | * @throws ReflectiveOperationException if an error occurs
|
---|
134 | */
|
---|
135 | @Test
|
---|
136 | public void testTicket7421case1() throws ReflectiveOperationException {
|
---|
137 | DataSet dataSet = new DataSet();
|
---|
138 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
139 |
|
---|
140 | Node n1 = new Node(new EastNorth(0, 0));
|
---|
141 | Node n2 = new Node(new EastNorth(-1, 1));
|
---|
142 | Node n3 = new Node(new EastNorth(1, 1));
|
---|
143 | dataSet.addPrimitive(n1);
|
---|
144 | dataSet.addPrimitive(n2);
|
---|
145 | dataSet.addPrimitive(n3);
|
---|
146 |
|
---|
147 | addSelected(n1, dataSet);
|
---|
148 | addSelected(n2, dataSet);
|
---|
149 | addSelected(n3, dataSet);
|
---|
150 |
|
---|
151 | // Mock left/right hand traffic database
|
---|
152 | Field rlCache = RightAndLefthandTraffic.class.getDeclaredField("rlCache");
|
---|
153 | Utils.setObjectsAccessible(rlCache);
|
---|
154 | Object origRlCache = rlCache.get(null);
|
---|
155 | rlCache.set(null, new GeoPropertyIndex<>(new ConstantTrafficHand(true), 24));
|
---|
156 |
|
---|
157 | try {
|
---|
158 | CreateCircleAction action = new CreateCircleAction();
|
---|
159 | action.setEnabled(true);
|
---|
160 | try {
|
---|
161 | Main.getLayerManager().addLayer(layer);
|
---|
162 | action.actionPerformed(null);
|
---|
163 | } finally {
|
---|
164 | // Ensure we clean the place before leaving, even if test fails.
|
---|
165 | Main.getLayerManager().removeLayer(layer);
|
---|
166 | }
|
---|
167 |
|
---|
168 | // Expected result: Dataset contain one closed way, clockwise
|
---|
169 | Collection<Way> resultingWays = dataSet.getWays();
|
---|
170 | assertSame(String.format("Expect one way after perform action. %d found", resultingWays.size()),
|
---|
171 | resultingWays.size(), 1);
|
---|
172 | Way resultingWay = resultingWays.iterator().next();
|
---|
173 | assertTrue("Resulting way is not closed",
|
---|
174 | resultingWay.isClosed());
|
---|
175 | assertTrue("Found anti-clockwise way while traffic is left hand.",
|
---|
176 | Geometry.isClockwise(resultingWay));
|
---|
177 | } finally {
|
---|
178 | // Restore left/right hand traffic database
|
---|
179 | rlCache.set(null, origRlCache);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|