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