1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 |
|
---|
9 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
10 | import org.junit.jupiter.api.Test;
|
---|
11 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 |
|
---|
22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
23 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Unit tests for class {@link SelectByInternalPointAction}.
|
---|
27 | */
|
---|
28 | final class SelectByInternalPointActionTest {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Setup test.
|
---|
32 | */
|
---|
33 | @RegisterExtension
|
---|
34 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
35 | public JOSMTestRules rules = new JOSMTestRules().preferences().projection().main();
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Tests that {@code SelectByInternalPointAction} satisfies utility class criteria.
|
---|
39 | * @throws ReflectiveOperationException if an error occurs
|
---|
40 | */
|
---|
41 | @Test
|
---|
42 | void testUtilityClass() throws ReflectiveOperationException {
|
---|
43 | UtilityClassTestUtil.assertUtilityClassWellDefined(SelectByInternalPointAction.class);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Unit test - no dataset.
|
---|
48 | */
|
---|
49 | @Test
|
---|
50 | void testNoDataSet() {
|
---|
51 | assertNull(MainApplication.getLayerManager().getEditDataSet());
|
---|
52 | assertEquals(0, SelectByInternalPointAction.getSurroundingObjects(null).size());
|
---|
53 | assertNull(SelectByInternalPointAction.getSmallestSurroundingObject(null));
|
---|
54 | SelectByInternalPointAction.performSelection(null, false, false);
|
---|
55 | }
|
---|
56 |
|
---|
57 | static Layer initDataSet() {
|
---|
58 | DataSet ds = new DataSet();
|
---|
59 | Node n1 = new Node(new EastNorth(1, 1));
|
---|
60 | Node n2 = new Node(new EastNorth(1, 2));
|
---|
61 | Node n3 = new Node(new EastNorth(2, 2));
|
---|
62 | Node n4 = new Node(new EastNorth(2, 1));
|
---|
63 | ds.addPrimitive(n1);
|
---|
64 | ds.addPrimitive(n2);
|
---|
65 | ds.addPrimitive(n3);
|
---|
66 | ds.addPrimitive(n4);
|
---|
67 | Way w = new Way();
|
---|
68 | w.addNode(n1);
|
---|
69 | w.addNode(n2);
|
---|
70 | w.addNode(n3);
|
---|
71 | w.addNode(n4);
|
---|
72 | w.addNode(n1);
|
---|
73 | assertTrue(w.isClosed());
|
---|
74 | ds.addPrimitive(w);
|
---|
75 | Relation r = new Relation();
|
---|
76 | r.addMember(new RelationMember("outer", w));
|
---|
77 | ds.addPrimitive(r);
|
---|
78 | OsmDataLayer layer = new OsmDataLayer(ds, "", null);
|
---|
79 | MainApplication.getLayerManager().addLayer(layer);
|
---|
80 | return layer;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Unit test of {@link SelectByInternalPointAction#getSurroundingObjects} method.
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | void testGetSurroundingObjects() {
|
---|
88 | initDataSet();
|
---|
89 | assertEquals(0, SelectByInternalPointAction.getSurroundingObjects(null).size());
|
---|
90 | assertEquals(0, SelectByInternalPointAction.getSurroundingObjects(new EastNorth(0, 0)).size());
|
---|
91 | assertEquals(1, SelectByInternalPointAction.getSurroundingObjects(new EastNorth(1.5, 1.5)).size());
|
---|
92 | assertEquals(0, SelectByInternalPointAction.getSurroundingObjects(new EastNorth(3, 3)).size());
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Unit test of {@link SelectByInternalPointAction#getSmallestSurroundingObject} method.
|
---|
97 | */
|
---|
98 | @Test
|
---|
99 | void testGetSmallestSurroundingObject() {
|
---|
100 | initDataSet();
|
---|
101 | assertNull(SelectByInternalPointAction.getSmallestSurroundingObject(null));
|
---|
102 | assertNotNull(SelectByInternalPointAction.getSmallestSurroundingObject(new EastNorth(1.5, 1.5)));
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Unit test of {@link SelectByInternalPointAction#performSelection} method.
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | void testPerformSelection() {
|
---|
110 | initDataSet();
|
---|
111 | DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
---|
112 |
|
---|
113 | assertEquals(0, ds.getSelected().size());
|
---|
114 | SelectByInternalPointAction.performSelection(null, false, false);
|
---|
115 | assertEquals(0, ds.getSelected().size());
|
---|
116 | SelectByInternalPointAction.performSelection(new EastNorth(0, 0), false, false);
|
---|
117 | assertEquals(0, ds.getSelected().size());
|
---|
118 | SelectByInternalPointAction.performSelection(new EastNorth(1.5, 1.5), false, false);
|
---|
119 | assertEquals(1, ds.getSelected().size());
|
---|
120 | ds.clearSelection();
|
---|
121 | ds.addSelected(ds.getNodes());
|
---|
122 | assertEquals(4, ds.getSelected().size());
|
---|
123 | SelectByInternalPointAction.performSelection(new EastNorth(1.5, 1.5), true, false);
|
---|
124 | assertEquals(5, ds.getSelected().size());
|
---|
125 | SelectByInternalPointAction.performSelection(new EastNorth(1.5, 1.5), false, true);
|
---|
126 | assertEquals(4, ds.getSelected().size());
|
---|
127 | }
|
---|
128 | }
|
---|