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.assertNull;
|
---|
6 |
|
---|
7 | import java.util.Collections;
|
---|
8 |
|
---|
9 | import javax.swing.JLabel;
|
---|
10 | import javax.swing.JPanel;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.BeforeEach;
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
22 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
23 | import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
|
---|
24 | import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker;
|
---|
25 |
|
---|
26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Unit tests for class {@link MergeLayerAction}.
|
---|
30 | */
|
---|
31 | public class MergeLayerActionTest {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Setup test.
|
---|
35 | */
|
---|
36 | @RegisterExtension
|
---|
37 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
38 | public JOSMTestRules test = new JOSMTestRules().main().projection();
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * MergeLayerExtendedDialog mocker.
|
---|
42 | */
|
---|
43 | public static class MergeLayerExtendedDialogMocker extends ExtendedDialogMocker {
|
---|
44 | @Override
|
---|
45 | protected void act(final ExtendedDialog instance) {
|
---|
46 | ((JosmComboBox<?>) ((JPanel) this.getContent(instance)).getComponent(1)).setSelectedIndex(0);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | protected String getString(final ExtendedDialog instance) {
|
---|
51 | return ((JLabel) ((JPanel) this.getContent(instance)).getComponent(0)).getText();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | private MergeLayerAction action;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Setup test.
|
---|
59 | */
|
---|
60 | @BeforeEach
|
---|
61 | public void setUp() {
|
---|
62 | if (action == null) {
|
---|
63 | action = new MergeLayerAction();
|
---|
64 | }
|
---|
65 | for (TestLayer testLayer : MainApplication.getLayerManager().getLayersOfType(TestLayer.class)) {
|
---|
66 | MainApplication.getLayerManager().removeLayer(testLayer);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Tests that no error occurs when no source layer exists.
|
---|
72 | */
|
---|
73 | @Test
|
---|
74 | void testMergeNoSourceLayer() {
|
---|
75 | assertNull(MainApplication.getLayerManager().getActiveLayer());
|
---|
76 | action.actionPerformed(null);
|
---|
77 | assertEquals(0, MainApplication.getLayerManager().getLayers().size());
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Tests that no error occurs when no target layer exists.
|
---|
82 | */
|
---|
83 | @Test
|
---|
84 | void testMergeNoTargetLayer() {
|
---|
85 | TestUtils.assumeWorkingJMockit();
|
---|
86 | final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(
|
---|
87 | Collections.singletonMap("<html>There are no layers the source layer<br>'onion'<br>could be merged to.</html>", 0)
|
---|
88 | );
|
---|
89 |
|
---|
90 | OsmDataLayer layer = new OsmDataLayer(new DataSet(), "onion", null);
|
---|
91 | MainApplication.getLayerManager().addLayer(layer);
|
---|
92 | assertEquals(1, MainApplication.getLayerManager().getLayers().size());
|
---|
93 | assertNull(action.merge(layer));
|
---|
94 | assertEquals(1, MainApplication.getLayerManager().getLayers().size());
|
---|
95 |
|
---|
96 | assertEquals(1, jopsMocker.getInvocationLog().size());
|
---|
97 | Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(0);
|
---|
98 | assertEquals(0, (int) invocationLogEntry[0]);
|
---|
99 | assertEquals("No target layers", invocationLogEntry[2]);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Tests that the merge is done with two empty layers.
|
---|
104 | * @throws Exception if any error occurs
|
---|
105 | */
|
---|
106 | @Test
|
---|
107 | void testMergeTwoEmptyLayers() throws Exception {
|
---|
108 | TestUtils.assumeWorkingJMockit();
|
---|
109 | final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker();
|
---|
110 | edMocker.getMockResultMap().put("Please select the target layer.", "Merge layer");
|
---|
111 |
|
---|
112 | OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "1", null);
|
---|
113 | OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "2", null);
|
---|
114 | MainApplication.getLayerManager().addLayer(layer1);
|
---|
115 | MainApplication.getLayerManager().addLayer(layer2);
|
---|
116 | assertEquals(2, MainApplication.getLayerManager().getLayers().size());
|
---|
117 | action.merge(layer2).get();
|
---|
118 | assertEquals(1, MainApplication.getLayerManager().getLayers().size());
|
---|
119 |
|
---|
120 | assertEquals(1, edMocker.getInvocationLog().size());
|
---|
121 | Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
|
---|
122 | assertEquals(1, (int) invocationLogEntry[0]);
|
---|
123 | assertEquals("Select target layer", invocationLogEntry[2]);
|
---|
124 | }
|
---|
125 | }
|
---|