1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNotNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 | import static org.junit.Assert.fail;
|
---|
9 |
|
---|
10 | import java.awt.datatransfer.Clipboard;
|
---|
11 | import java.awt.datatransfer.DataFlavor;
|
---|
12 | import java.awt.datatransfer.StringSelection;
|
---|
13 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
14 | import java.io.IOException;
|
---|
15 | import java.util.Arrays;
|
---|
16 |
|
---|
17 | import org.junit.Rule;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
24 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
25 | import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
|
---|
26 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
27 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
28 |
|
---|
29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests for class {@link CopyAction}.
|
---|
33 | */
|
---|
34 | public class CopyActionTest {
|
---|
35 | private static final class CapturingCopyAction extends CopyAction {
|
---|
36 | private boolean warningShown;
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | protected void showEmptySelectionWarning() {
|
---|
40 | warningShown = true;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * We need prefs for this.
|
---|
46 | */
|
---|
47 | @Rule
|
---|
48 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
49 | public JOSMTestRules test = new JOSMTestRules().preferences().platform().fakeAPI();
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Test that copy action copies the selected primitive
|
---|
53 | * @throws IOException if an I/O error occurs
|
---|
54 | * @throws UnsupportedFlavorException if the requested data flavor is not supported
|
---|
55 | */
|
---|
56 | @Test
|
---|
57 | public void testWarnOnEmpty() throws UnsupportedFlavorException, IOException {
|
---|
58 | Clipboard clipboard = ClipboardUtils.getClipboard();
|
---|
59 | clipboard.setContents(new StringSelection("test"), null);
|
---|
60 |
|
---|
61 | CapturingCopyAction action = new CapturingCopyAction();
|
---|
62 |
|
---|
63 | action.updateEnabledState();
|
---|
64 | assertFalse(action.isEnabled());
|
---|
65 | action.actionPerformed(null);
|
---|
66 | assertTrue(action.warningShown);
|
---|
67 |
|
---|
68 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(new DataSet(), "test", null));
|
---|
69 | action.warningShown = false;
|
---|
70 |
|
---|
71 | action.updateEnabledState();
|
---|
72 | assertFalse(action.isEnabled());
|
---|
73 | action.actionPerformed(null);
|
---|
74 | assertTrue(action.warningShown);
|
---|
75 |
|
---|
76 | assertEquals("test", clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor));
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Test that copy action copies the selected primitive
|
---|
81 | * @throws Exception if an error occurs
|
---|
82 | */
|
---|
83 | @Test
|
---|
84 | public void testCopySinglePrimitive() throws Exception {
|
---|
85 | DataSet data = new DataSet();
|
---|
86 |
|
---|
87 | Node node1 = new Node();
|
---|
88 | node1.setCoor(LatLon.ZERO);
|
---|
89 | data.addPrimitive(node1);
|
---|
90 |
|
---|
91 | Node node2 = new Node();
|
---|
92 | node2.setCoor(LatLon.ZERO);
|
---|
93 | data.addPrimitive(node2);
|
---|
94 | Way way = new Way();
|
---|
95 | way.setNodes(Arrays.asList(node1, node2));
|
---|
96 | data.addPrimitive(way);
|
---|
97 | data.setSelected(way);
|
---|
98 |
|
---|
99 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(data, "test", null));
|
---|
100 |
|
---|
101 | CopyAction action = new CopyAction() {
|
---|
102 | @Override
|
---|
103 | protected void showEmptySelectionWarning() {
|
---|
104 | fail("Selection is not empty.");
|
---|
105 | }
|
---|
106 | };
|
---|
107 | action.updateEnabledState();
|
---|
108 | assertTrue(action.isEnabled());
|
---|
109 | action.actionPerformed(null);
|
---|
110 |
|
---|
111 | Object copied = ClipboardUtils.getClipboard().getContents(null).getTransferData(PrimitiveTransferData.DATA_FLAVOR);
|
---|
112 | assertNotNull(copied);
|
---|
113 | assertTrue(copied instanceof PrimitiveTransferData);
|
---|
114 | PrimitiveTransferData ptd = (PrimitiveTransferData) copied;
|
---|
115 | Object[] direct = ptd.getDirectlyAdded().toArray();
|
---|
116 | assertEquals(1, direct.length);
|
---|
117 | Object[] referenced = ptd.getReferenced().toArray();
|
---|
118 | assertEquals(2, referenced.length);
|
---|
119 | }
|
---|
120 | }
|
---|