1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.datatransfer;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertNull;
|
---|
7 | import static org.junit.Assert.assertSame;
|
---|
8 | import static org.junit.Assert.assertTrue;
|
---|
9 |
|
---|
10 | import java.awt.GraphicsEnvironment;
|
---|
11 | import java.awt.datatransfer.Clipboard;
|
---|
12 | import java.awt.datatransfer.DataFlavor;
|
---|
13 | import java.awt.datatransfer.StringSelection;
|
---|
14 | import java.awt.datatransfer.Transferable;
|
---|
15 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
16 | import java.io.IOException;
|
---|
17 |
|
---|
18 | import org.junit.Rule;
|
---|
19 | import org.junit.Test;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 |
|
---|
22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
23 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Basic tests for the clipboard utils class.
|
---|
27 | * @author Michael Zangl
|
---|
28 | */
|
---|
29 | public class ClipboardUtilsTest {
|
---|
30 | private static final class ThrowIllegalStateClipboard extends Clipboard {
|
---|
31 | private int failingAccesses = 3;
|
---|
32 |
|
---|
33 | private ThrowIllegalStateClipboard(String name) {
|
---|
34 | super(name);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public synchronized Transferable getContents(Object requestor) {
|
---|
39 | if (failingAccesses >= 0) {
|
---|
40 | failingAccesses--;
|
---|
41 | throw new IllegalStateException();
|
---|
42 | }
|
---|
43 | return super.getContents(requestor);
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected synchronized void setFailingAccesses(int failingAccesses) {
|
---|
47 | this.failingAccesses = failingAccesses;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private static final class SupportNothingTransferable implements Transferable {
|
---|
52 | @Override
|
---|
53 | public boolean isDataFlavorSupported(DataFlavor flavor) {
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public DataFlavor[] getTransferDataFlavors() {
|
---|
59 | return new DataFlavor[0];
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
---|
64 | throw new UnsupportedFlavorException(flavor);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * No dependencies
|
---|
70 | */
|
---|
71 | @Rule
|
---|
72 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
73 | public JOSMTestRules test = new JOSMTestRules();
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Test {@link ClipboardUtils#getClipboard()}
|
---|
77 | */
|
---|
78 | @Test
|
---|
79 | public void testGetClipboard() {
|
---|
80 | Clipboard c = ClipboardUtils.getClipboard();
|
---|
81 | assertNotNull(c);
|
---|
82 | assertSame(c, ClipboardUtils.getClipboard());
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Test {@link ClipboardUtils#copyString(String)} and {@link ClipboardUtils#getClipboardStringContent()}
|
---|
87 | */
|
---|
88 | @Test
|
---|
89 | public void testCopyPasteString() {
|
---|
90 | ClipboardUtils.copyString("");
|
---|
91 | assertEquals("", ClipboardUtils.getClipboardStringContent());
|
---|
92 | ClipboardUtils.copyString("xxx\nx");
|
---|
93 | assertEquals("xxx\nx", ClipboardUtils.getClipboardStringContent());
|
---|
94 |
|
---|
95 | ClipboardUtils.copy(new SupportNothingTransferable());
|
---|
96 | assertEquals(null, ClipboardUtils.getClipboardStringContent());
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Test that {@link ClipboardUtils#getClipboardContent(Clipboard)} handles illegal state exceptions
|
---|
101 | */
|
---|
102 | @Test
|
---|
103 | public void testGetContentIllegalState() {
|
---|
104 | ThrowIllegalStateClipboard throwingClipboard = new ThrowIllegalStateClipboard("test");
|
---|
105 |
|
---|
106 | throwingClipboard.setContents(new StringSelection(""), null);
|
---|
107 | Transferable content = ClipboardUtils.getClipboardContent(throwingClipboard);
|
---|
108 | assertTrue(content.isDataFlavorSupported(DataFlavor.stringFlavor));
|
---|
109 |
|
---|
110 | throwingClipboard.setFailingAccesses(50);
|
---|
111 | content = ClipboardUtils.getClipboardContent(new ThrowIllegalStateClipboard("test"));
|
---|
112 | assertNull(content);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Test that {@link ClipboardUtils#getSystemSelection()} works in headless mode.
|
---|
117 | */
|
---|
118 | @Test
|
---|
119 | public void testSystemSelectionDoesNotFail() {
|
---|
120 | assertTrue(GraphicsEnvironment.isHeadless());
|
---|
121 | assertNull(ClipboardUtils.getSystemSelection());
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Tests that {@code ClipboardUtils} satisfies utility class criterias.
|
---|
126 | * @throws ReflectiveOperationException if an error occurs
|
---|
127 | */
|
---|
128 | @Test
|
---|
129 | public void testUtilityClass() throws ReflectiveOperationException {
|
---|
130 | UtilityClassTestUtil.assertUtilityClassWellDefined(ClipboardUtils.class);
|
---|
131 | }
|
---|
132 | }
|
---|