source: josm/trunk/test/unit/org/openstreetmap/josm/actions/SimplifyWayActionTest.java@ 12636

Last change on this file since 12636 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

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