source: josm/trunk/test/unit/org/openstreetmap/josm/actions/CopyActionTest.java@ 11241

Last change on this file since 11241 was 10604, checked in by Don-vip, 8 years ago

fix #12478, fix #12565, fix #11114 - Use ​Swing Copy/Paste instead of CopyAction/PasteAction with custom buffer (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.assertTrue;
8import static org.junit.Assert.fail;
9
10import java.awt.datatransfer.Clipboard;
11import java.awt.datatransfer.DataFlavor;
12import java.awt.datatransfer.StringSelection;
13import java.awt.datatransfer.UnsupportedFlavorException;
14import java.io.IOException;
15import java.util.Arrays;
16
17import org.junit.Rule;
18import org.junit.Test;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.osm.DataSet;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
25import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
27import org.openstreetmap.josm.testutils.JOSMTestRules;
28
29import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
30
31/**
32 * Unit tests for class {@link CopyAction}.
33 */
34public 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 Main.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 Main.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}
Note: See TracBrowser for help on using the repository browser.