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