1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.datatransfer;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertAll;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.stream.Stream;
|
---|
10 |
|
---|
11 | import org.junit.jupiter.api.Test;
|
---|
12 | import org.junit.jupiter.params.ParameterizedTest;
|
---|
13 | import org.junit.jupiter.params.provider.Arguments;
|
---|
14 | import org.junit.jupiter.params.provider.MethodSource;
|
---|
15 | import org.openstreetmap.josm.actions.CopyAction;
|
---|
16 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
17 | import org.openstreetmap.josm.command.Command;
|
---|
18 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
19 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
24 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
25 | import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
|
---|
26 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
27 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
28 | import org.openstreetmap.josm.testutils.annotations.Main;
|
---|
29 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests of {@link OsmTransferHandler} class.
|
---|
33 | */
|
---|
34 | @BasicPreferences
|
---|
35 | @Main
|
---|
36 | @Projection
|
---|
37 | class OsmTransferHandlerTest {
|
---|
38 | private final OsmTransferHandler transferHandler = new OsmTransferHandler();
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Test of {@link OsmTransferHandler#pasteOn} method
|
---|
42 | */
|
---|
43 | @Test
|
---|
44 | void testPasteOn() {
|
---|
45 | DataSet ds1 = new DataSet();
|
---|
46 | Node n1 = new Node(new LatLon(43, 1));
|
---|
47 | ds1.addPrimitive(n1);
|
---|
48 | OsmDataLayer source = new OsmDataLayer(ds1, "source", null);
|
---|
49 |
|
---|
50 | CopyAction.copy(source, Collections.singleton(n1));
|
---|
51 |
|
---|
52 | DataSet ds2 = new DataSet();
|
---|
53 | OsmDataLayer target = new OsmDataLayer(ds2, "target", null);
|
---|
54 |
|
---|
55 | transferHandler.pasteOn(target, null);
|
---|
56 | assertTrue(n1.equalsEpsilon(ds2.getNodes().iterator().next()));
|
---|
57 |
|
---|
58 | ds2.clear();
|
---|
59 | assertTrue(ds2.getNodes().isEmpty());
|
---|
60 |
|
---|
61 | LatLon pos = new LatLon(55, -5);
|
---|
62 | transferHandler.pasteOn(target, ProjectionRegistry.getProjection().latlon2eastNorth(pos));
|
---|
63 | assertTrue(pos.equalsEpsilon(ds2.getNodes().iterator().next()));
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Test of {@link OsmTransferHandler#pasteTags} method
|
---|
68 | */
|
---|
69 | @Test
|
---|
70 | void testPasteTags() {
|
---|
71 | Node n = new Node(LatLon.ZERO);
|
---|
72 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(n), "testPasteTags", null));
|
---|
73 |
|
---|
74 | ClipboardUtils.copyString("test=ok");
|
---|
75 | transferHandler.pasteTags(Collections.singleton(n));
|
---|
76 |
|
---|
77 | assertEquals("ok", n.get("test"));
|
---|
78 | }
|
---|
79 |
|
---|
80 | static Stream<Arguments> testNonRegression21324() {
|
---|
81 | return Stream.of(
|
---|
82 | Arguments.of((Runnable) () -> ClipboardUtils.copyString("test=ok\rsomething=else\nnew=line")),
|
---|
83 | Arguments.of((Runnable) () -> {
|
---|
84 | Node nData = new Node();
|
---|
85 | nData.put("test", "ok");
|
---|
86 | nData.put("something", "else");
|
---|
87 | nData.put("new", "line");
|
---|
88 | ClipboardUtils.copy(new PrimitiveTransferable(
|
---|
89 | PrimitiveTransferData.getDataWithReferences(Collections.singletonList(nData)), null));
|
---|
90 | })
|
---|
91 | );
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Non-regression test for #21324: Command stack says "pasting 1 tag to [number] objects" regardless of how many tags are pasted
|
---|
96 | */
|
---|
97 | @ParameterizedTest
|
---|
98 | @MethodSource
|
---|
99 | void testNonRegression21324(Runnable clipboardCopy) {
|
---|
100 | clipboardCopy.run();
|
---|
101 | Node n = new Node(LatLon.ZERO);
|
---|
102 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(n), "testNonRegression21324", null));
|
---|
103 |
|
---|
104 | transferHandler.pasteTags(Collections.singleton(n));
|
---|
105 | Command command = UndoRedoHandler.getInstance().getLastCommand();
|
---|
106 |
|
---|
107 | assertAll(() -> assertEquals("ok", n.get("test")),
|
---|
108 | () -> assertEquals("else", n.get("something")),
|
---|
109 | () -> assertEquals("line", n.get("new")),
|
---|
110 | () -> assertEquals(1, UndoRedoHandler.getInstance().getUndoCommands().size()),
|
---|
111 | () -> assertTrue((command instanceof SequenceCommand
|
---|
112 | && command.getChildren().stream().allMatch(ChangePropertyCommand.class::isInstance))
|
---|
113 | || command instanceof ChangePropertyCommand),
|
---|
114 | () -> assertEquals("Sequence: Pasting 3 tags to 1 object", command.getDescriptionText()));
|
---|
115 | }
|
---|
116 | }
|
---|