1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.stream.Stream;
|
---|
11 |
|
---|
12 | import org.junit.BeforeClass;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.JOSMFixture;
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
17 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import org.openstreetmap.josm.tools.Utils;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Unit tests for class {@link SimplifyWayAction}.
|
---|
27 | */
|
---|
28 | public final class SimplifyWayActionTest {
|
---|
29 |
|
---|
30 | /** Class under test. */
|
---|
31 | private static SimplifyWayAction action;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Setup test.
|
---|
35 | */
|
---|
36 | @BeforeClass
|
---|
37 | public static void setUp() {
|
---|
38 | JOSMFixture.createUnitTestFixture().init(true);
|
---|
39 | action = Main.main.menu.simplifyWay;
|
---|
40 | action.setEnabled(true);
|
---|
41 | }
|
---|
42 |
|
---|
43 | private static Way createWaySelected(DataSet ds, double latStart) {
|
---|
44 | Node n1 = new Node(new LatLon(latStart, 1.0));
|
---|
45 | ds.addPrimitive(n1);
|
---|
46 | Node n2 = new Node(new LatLon(latStart+1.0, 1.0));
|
---|
47 | ds.addPrimitive(n2);
|
---|
48 | Way w = new Way();
|
---|
49 | w.addNode(n1);
|
---|
50 | w.addNode(n2);
|
---|
51 | ds.addPrimitive(w);
|
---|
52 | ds.addSelected(w);
|
---|
53 | return w;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Test without any selection.
|
---|
58 | */
|
---|
59 | @Test
|
---|
60 | public void testSelectionEmpty() {
|
---|
61 | DataSet ds = new DataSet();
|
---|
62 | OsmDataLayer layer = new OsmDataLayer(ds, "", null);
|
---|
63 | try {
|
---|
64 | Main.getLayerManager().addLayer(layer);
|
---|
65 | assertTrue(ds.getSelected().isEmpty());
|
---|
66 | action.actionPerformed(null);
|
---|
67 | } finally {
|
---|
68 | Main.getLayerManager().removeLayer(layer);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Test with a single way.
|
---|
74 | */
|
---|
75 | @Test
|
---|
76 | public void testSingleWay() {
|
---|
77 | DataSet ds = new DataSet();
|
---|
78 | createWaySelected(ds, 0.0);
|
---|
79 | OsmDataLayer layer = new OsmDataLayer(ds, "", null);
|
---|
80 | try {
|
---|
81 | Main.getLayerManager().addLayer(layer);
|
---|
82 | assertEquals(1, ds.getSelected().size());
|
---|
83 | action.actionPerformed(null);
|
---|
84 | } finally {
|
---|
85 | Main.getLayerManager().removeLayer(layer);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Test with more than 10 ways.
|
---|
91 | */
|
---|
92 | @Test
|
---|
93 | public void testMoreThanTenWays() {
|
---|
94 | DataSet ds = new DataSet();
|
---|
95 | for (int i = 0; i < 11; i++) {
|
---|
96 | createWaySelected(ds, i);
|
---|
97 | }
|
---|
98 | OsmDataLayer layer = new OsmDataLayer(ds, "", null);
|
---|
99 | try {
|
---|
100 | Main.getLayerManager().addLayer(layer);
|
---|
101 | assertEquals(11, ds.getSelected().size());
|
---|
102 | action.actionPerformed(null);
|
---|
103 | } finally {
|
---|
104 | Main.getLayerManager().removeLayer(layer);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Tests that also the first node may be simplified, see #13094.
|
---|
110 | */
|
---|
111 | @Test
|
---|
112 | public void testSimplifyFirstNode() {
|
---|
113 | final DataSet ds = new DataSet();
|
---|
114 | final Node n1 = new Node(new LatLon(47.26269614984, 11.34044231149));
|
---|
115 | final Node n2 = new Node(new LatLon(47.26274590831, 11.34053120859));
|
---|
116 | final Node n3 = new Node(new LatLon(47.26276562382, 11.34034715039));
|
---|
117 | final Node n4 = new Node(new LatLon(47.26264639132, 11.34035341438));
|
---|
118 | final Way w = new Way();
|
---|
119 | Stream.of(n1, n2, n3, n4, w).forEach(ds::addPrimitive);
|
---|
120 | Stream.of(n1, n2, n3, n4, n1).forEach(w::addNode);
|
---|
121 | final SequenceCommand command = action.simplifyWay(w);
|
---|
122 | assertNotNull(command);
|
---|
123 | assertEquals(2, command.getChildren().size());
|
---|
124 | final Collection<DeleteCommand> deleteCommands = Utils.filteredCollection(command.getChildren(), DeleteCommand.class);
|
---|
125 | assertEquals(1, deleteCommands.size());
|
---|
126 | assertEquals(Collections.singleton(n1), deleteCommands.iterator().next().getParticipatingPrimitives());
|
---|
127 | }
|
---|
128 | }
|
---|