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