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