1 /* 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.scene.input; 27 28 import java.util.ArrayList; 29 import java.io.File; 30 import java.util.List; 31 import com.sun.javafx.pgstub.StubPlatformImageInfo; 32 import com.sun.javafx.pgstub.StubToolkit; 33 import com.sun.javafx.tk.Toolkit; 34 import java.util.Arrays; 35 import javafx.scene.image.Image; 36 import org.junit.Test; 37 import static org.junit.Assert.*; 38 39 public class ClipboardContentTest { 40 41 @Test 42 public void stringShouldBePut() { 43 ClipboardContent cc = new ClipboardContent(); 44 45 assertFalse(cc.hasString()); 46 47 cc.putString("Hello"); 48 49 assertTrue(cc.hasString()); 50 assertEquals("Hello", cc.getString()); 51 } 52 53 @Test 54 public void nullStringShouldRemovePrevious() { 55 ClipboardContent cc = new ClipboardContent(); 56 cc.putString("Hello"); 57 cc.putString(null); 58 assertFalse(cc.hasString()); 59 assertNull(cc.getString()); 60 } 61 62 @Test 63 public void urlShouldBePut() { 64 ClipboardContent cc = new ClipboardContent(); 65 66 assertFalse(cc.hasUrl()); 67 68 cc.putUrl("http://hello"); 69 70 assertTrue(cc.hasUrl()); 71 assertEquals("http://hello", cc.getUrl()); 72 } 73 74 @Test 75 public void nullUrlShouldRemovePrevious() { 76 ClipboardContent cc = new ClipboardContent(); 77 cc.putUrl("http://hello"); 78 cc.putUrl(null); 79 assertFalse(cc.hasUrl()); 80 assertNull(cc.getUrl()); 81 } 82 83 @Test 84 public void htmlShouldBePut() { 85 ClipboardContent cc = new ClipboardContent(); 86 87 assertFalse(cc.hasHtml()); 88 89 cc.putHtml("<html><head></head><body>Hello</body></html>"); 90 91 assertTrue(cc.hasHtml()); 92 assertEquals("<html><head></head><body>Hello</body></html>", cc.getHtml()); 93 } 94 95 @Test 96 public void nullHtmlShouldRemovePrevious() { 97 ClipboardContent cc = new ClipboardContent(); 98 cc.putHtml("<html><head></head><body>Hello</body></html>"); 99 cc.putHtml(null); 100 assertFalse(cc.hasHtml()); 101 assertNull(cc.getHtml()); 102 } 103 104 @Test 105 public void rtfShouldBePut() { 106 ClipboardContent cc = new ClipboardContent(); 107 108 assertFalse(cc.hasRtf()); 109 110 cc.putRtf("{\\rtf1\\ansi\\uc1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i FRED\\par rtf\\par text}"); 111 112 assertTrue(cc.hasRtf()); 113 assertEquals("{\\rtf1\\ansi\\uc1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i FRED\\par rtf\\par text}", 114 cc.getRtf()); 115 } 116 117 @Test 118 public void nullRtfShouldRemovePrevious() { 119 ClipboardContent cc = new ClipboardContent(); 120 cc.putRtf("{\\rtf1\\ansi\\uc1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i FRED\\par rtf\\par text}"); 121 cc.putRtf(null); 122 assertFalse(cc.hasRtf()); 123 assertNull(cc.getRtf()); 124 } 125 126 @Test 127 public void imageShouldBePut() { 128 StubToolkit toolkit = (StubToolkit) Toolkit.getToolkit(); 129 toolkit.getImageLoaderFactory().reset(); 130 toolkit.getImageLoaderFactory().registerImage("file:test.png", 131 new StubPlatformImageInfo(100, 200)); 132 133 ClipboardContent cc = new ClipboardContent(); 134 Image i = new Image("file:test.png"); 135 136 assertFalse(cc.hasImage()); 137 138 139 cc.putImage(i); 140 141 assertTrue(cc.hasImage()); 142 assertSame(i, cc.getImage()); 143 } 144 145 @Test 146 public void nullImageShouldRemovePrevious() { 147 StubToolkit toolkit = (StubToolkit) Toolkit.getToolkit(); 148 toolkit.getImageLoaderFactory().reset(); 149 toolkit.getImageLoaderFactory().registerImage("file:test.png", 150 new StubPlatformImageInfo(100, 200)); 151 152 ClipboardContent cc = new ClipboardContent(); 153 Image i = new Image("file:test.png"); 154 155 cc.putImage(i); 156 cc.putImage(null); 157 assertFalse(cc.hasImage()); 158 assertNull(cc.getImage()); 159 } 160 161 @Test 162 public void filesShouldBePut() { 163 164 ClipboardContent cc = new ClipboardContent(); 165 List<File> files = Arrays.asList(new File("."), new File("/")); 166 167 assertFalse(cc.hasFiles()); 168 169 cc.putFiles(files); 170 171 assertTrue(cc.hasFiles()); 172 assertEquals(files, cc.getFiles()); 173 } 174 175 @Test 176 public void noFilesShouldBePut() { 177 178 ClipboardContent cc = new ClipboardContent(); 179 180 assertFalse(cc.hasFiles()); 181 182 cc.putFiles(new ArrayList<File>(0)); 183 184 assertTrue(cc.hasFiles()); 185 assertEquals(0, cc.getFiles().size()); 186 } 187 188 @Test 189 public void nullFilesShouldRemovePrevious() { 190 ClipboardContent cc = new ClipboardContent(); 191 List<File> files = Arrays.asList(new File("."), new File("/")); 192 193 cc.putFiles(files); 194 cc.putFiles(null); 195 assertFalse(cc.hasFiles()); 196 assertNull(cc.getFiles()); 197 } 198 199 @Test 200 public void filesShouldBePutByPath() { 201 202 ClipboardContent cc = new ClipboardContent(); 203 List<File> files = Arrays.asList(new File("."), new File("/")); 204 205 assertFalse(cc.hasFiles()); 206 207 cc.putFilesByPath(Arrays.asList(".", "/")); 208 209 assertTrue(cc.hasFiles()); 210 assertEquals(files, cc.getFiles()); 211 } 212 213 @Test 214 public void noFilesShouldBePutByPath() { 215 216 ClipboardContent cc = new ClipboardContent(); 217 218 assertFalse(cc.hasFiles()); 219 220 cc.putFilesByPath(new ArrayList<String>(0)); 221 222 assertTrue(cc.hasFiles()); 223 assertEquals(0, cc.getFiles().size()); 224 } 225 226 @Test(expected=NullPointerException.class) 227 public void nullFilesByPathShouldRemoveFiles() { 228 ClipboardContent cc = new ClipboardContent(); 229 cc.putFilesByPath(new ArrayList<String>(0)); 230 cc.putFilesByPath(null); 231 232 assertFalse(cc.hasFiles()); 233 assertNull(cc.getFiles()); 234 } 235 }