1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertSame;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 | import static org.junit.Assert.fail;
|
---|
8 |
|
---|
9 | import java.awt.event.InputEvent;
|
---|
10 | import java.awt.event.MouseEvent;
|
---|
11 | import java.lang.reflect.Field;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 |
|
---|
15 | import org.junit.BeforeClass;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.JOSMFixture;
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.Preferences;
|
---|
20 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
25 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
26 | import org.openstreetmap.josm.tools.Utils;
|
---|
27 |
|
---|
28 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Unit tests for class {@link SelectAction}.
|
---|
32 | */
|
---|
33 | public class SelectActionTest {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Override some configuration variables without change in preferences.xml
|
---|
37 | */
|
---|
38 | static class PreferencesMock extends Preferences {
|
---|
39 | @Override
|
---|
40 | public synchronized int getInteger(String key, int def) {
|
---|
41 | if ("edit.initial-move-delay".equals(key)) {
|
---|
42 | return 0;
|
---|
43 | } else {
|
---|
44 | return super.getInteger(key, def);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | boolean nodesMerged;
|
---|
50 |
|
---|
51 | class SelectActionMock extends SelectAction {
|
---|
52 | SelectActionMock(MapFrame mapFrame, DataSet dataSet, OsmDataLayer layer) {
|
---|
53 | super(mapFrame);
|
---|
54 | try {
|
---|
55 | Field mv = SelectAction.class.getDeclaredField("mv");
|
---|
56 | Utils.setObjectsAccessible(mv);
|
---|
57 | mv.set(this, new MapViewMock());
|
---|
58 | } catch (ReflectiveOperationException e) {
|
---|
59 | e.printStackTrace();
|
---|
60 | fail("Can't setup testing environnement");
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void mergeNodes(OsmDataLayer layer, Collection<Node> nodes,
|
---|
66 | Node targetLocationNode) {
|
---|
67 | assertSame(String.format("Should merge two nodes, %d found", nodes.size()),
|
---|
68 | nodes.size(), 2);
|
---|
69 | nodesMerged = true;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Setup test.
|
---|
75 | */
|
---|
76 | @BeforeClass
|
---|
77 | public static void setUpBeforeClass() {
|
---|
78 | JOSMFixture.createUnitTestFixture().init(true);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Test case: Move a two nodes way near a third node.
|
---|
83 | * Resulting way should be attach to the third node.
|
---|
84 | * see #10748
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
|
---|
88 | public void testTicket10748() {
|
---|
89 | DataSet dataSet = new DataSet();
|
---|
90 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
91 |
|
---|
92 | Node n1 = new Node(new EastNorth(0, 0));
|
---|
93 | Node n2 = new Node(new EastNorth(100, 0));
|
---|
94 | Node n3 = new Node(new EastNorth(200, 0));
|
---|
95 | dataSet.addPrimitive(n1);
|
---|
96 | dataSet.addPrimitive(n2);
|
---|
97 | dataSet.addPrimitive(n3);
|
---|
98 |
|
---|
99 | Way w = new Way();
|
---|
100 | w.setNodes(Arrays.asList(new Node[] {n2, n3}));
|
---|
101 | dataSet.addPrimitive(w);
|
---|
102 |
|
---|
103 | dataSet.addSelected(n2);
|
---|
104 | dataSet.addSelected(w);
|
---|
105 |
|
---|
106 | Main.pref = new PreferencesMock();
|
---|
107 | Main.getLayerManager().addLayer(layer);
|
---|
108 | try {
|
---|
109 | SelectAction action = new SelectActionMock(Main.map, dataSet, layer);
|
---|
110 | nodesMerged = false;
|
---|
111 |
|
---|
112 | action.setEnabled(true);
|
---|
113 | action.putValue("active", true);
|
---|
114 |
|
---|
115 | MouseEvent event;
|
---|
116 | event = new MouseEvent(Main.map,
|
---|
117 | MouseEvent.MOUSE_PRESSED,
|
---|
118 | 0,
|
---|
119 | InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK,
|
---|
120 | 100, 0,
|
---|
121 | 1,
|
---|
122 | false);
|
---|
123 | action.mousePressed(event);
|
---|
124 | event = new MouseEvent(Main.map,
|
---|
125 | MouseEvent.MOUSE_DRAGGED,
|
---|
126 | 1000,
|
---|
127 | InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK,
|
---|
128 | 50, 0,
|
---|
129 | 1,
|
---|
130 | false);
|
---|
131 | action.mouseDragged(event);
|
---|
132 | event = new MouseEvent(Main.map,
|
---|
133 | MouseEvent.MOUSE_RELEASED,
|
---|
134 | 2000,
|
---|
135 | InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK,
|
---|
136 | 5, 0,
|
---|
137 | 1,
|
---|
138 | false);
|
---|
139 | action.mouseReleased(event);
|
---|
140 |
|
---|
141 | // As result of test, we must find a 2 nodes way, from EN(0, 0) to EN(100, 0)
|
---|
142 | assertTrue("Nodes are not merged", nodesMerged);
|
---|
143 | assertSame(String.format("Expect exactly one way, found %d%n", dataSet.getWays().size()),
|
---|
144 | dataSet.getWays().size(), 1);
|
---|
145 | Way rw = dataSet.getWays().iterator().next();
|
---|
146 | assertFalse("Way shouldn't be deleted\n", rw.isDeleted());
|
---|
147 | assertSame(String.format("Way shouldn't have 2 nodes, %d found%n", w.getNodesCount()),
|
---|
148 | rw.getNodesCount(), 2);
|
---|
149 | Node r1 = rw.firstNode();
|
---|
150 | Node r2 = rw.lastNode();
|
---|
151 | if (r1.getEastNorth().east() > r2.getEastNorth().east()) {
|
---|
152 | Node tmp = r1;
|
---|
153 | r1 = r2;
|
---|
154 | r2 = tmp;
|
---|
155 | }
|
---|
156 | assertSame(String.format("East should be 0, found %f%n", r1.getEastNorth().east()),
|
---|
157 | Double.compare(r1.getEastNorth().east(), 0), 0);
|
---|
158 | assertSame(String.format("East should be 100, found %f%n", r2.getEastNorth().east()),
|
---|
159 | Double.compare(r2.getEastNorth().east(), 100), 0);
|
---|
160 | } finally {
|
---|
161 | // Ensure we clean the place before leaving, even if test fails.
|
---|
162 | Main.getLayerManager().removeLayer(layer);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|