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 |
|
---|
24 | /**
|
---|
25 | * Basic tests for the clipboard utils class.
|
---|
26 | * @author Michael Zangl
|
---|
27 | */
|
---|
28 | public class ClipboardUtilsTest {
|
---|
29 | private static final class ThrowIllegalStateClipboard extends Clipboard {
|
---|
30 | private int failingAccesses = 3;
|
---|
31 |
|
---|
32 | private ThrowIllegalStateClipboard(String name) {
|
---|
33 | super(name);
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public synchronized Transferable getContents(Object requestor) {
|
---|
38 | if (failingAccesses >= 0) {
|
---|
39 | failingAccesses--;
|
---|
40 | throw new IllegalStateException();
|
---|
41 | }
|
---|
42 | return super.getContents(requestor);
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected synchronized void setFailingAccesses(int failingAccesses) {
|
---|
46 | this.failingAccesses = failingAccesses;
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private static final class SupportNothingTransferable implements Transferable {
|
---|
51 | @Override
|
---|
52 | public boolean isDataFlavorSupported(DataFlavor flavor) {
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public DataFlavor[] getTransferDataFlavors() {
|
---|
58 | return new DataFlavor[0];
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
---|
63 | throw new UnsupportedFlavorException(flavor);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * No dependencies
|
---|
69 | */
|
---|
70 | @Rule
|
---|
71 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
72 | public JOSMTestRules test = new JOSMTestRules();
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Test {@link ClipboardUtils#getClipboard()}
|
---|
76 | */
|
---|
77 | @Test
|
---|
78 | public void testGetClipboard() {
|
---|
79 | Clipboard c = ClipboardUtils.getClipboard();
|
---|
80 | assertNotNull(c);
|
---|
81 | assertSame(c, ClipboardUtils.getClipboard());
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Test {@link ClipboardUtils#copyString(String)} and {@link ClipboardUtils#getClipboardStringContent()}
|
---|
86 | */
|
---|
87 | @Test
|
---|
88 | public void testCopyPasteString() {
|
---|
89 | ClipboardUtils.copyString("");
|
---|
90 | assertEquals("", ClipboardUtils.getClipboardStringContent());
|
---|
91 | ClipboardUtils.copyString("xxx\nx");
|
---|
92 | assertEquals("xxx\nx", ClipboardUtils.getClipboardStringContent());
|
---|
93 |
|
---|
94 | ClipboardUtils.copy(new SupportNothingTransferable());
|
---|
95 | assertEquals(null, ClipboardUtils.getClipboardStringContent());
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Test that {@link ClipboardUtils#getClipboardContent(Clipboard)} handles illegal state exceptions
|
---|
100 | */
|
---|
101 | @Test
|
---|
102 | public void testGetContentIllegalState() {
|
---|
103 | ThrowIllegalStateClipboard throwingClipboard = new ThrowIllegalStateClipboard("test");
|
---|
104 |
|
---|
105 | throwingClipboard.setContents(new StringSelection(""), null);
|
---|
106 | Transferable content = ClipboardUtils.getClipboardContent(throwingClipboard);
|
---|
107 | assertTrue(content.isDataFlavorSupported(DataFlavor.stringFlavor));
|
---|
108 |
|
---|
109 | throwingClipboard.setFailingAccesses(50);
|
---|
110 | content = ClipboardUtils.getClipboardContent(new ThrowIllegalStateClipboard("test"));
|
---|
111 | assertNull(content);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Test that {@link ClipboardUtils#getSystemSelection()} works in headless mode.
|
---|
116 | */
|
---|
117 | @Test
|
---|
118 | public void testSystemSelectionDoesNotFail() {
|
---|
119 | assertTrue(GraphicsEnvironment.isHeadless());
|
---|
120 | assertNull(ClipboardUtils.getSystemSelection());
|
---|
121 | }
|
---|
122 | }
|
---|