1 /* 2 * Copyright (c) 2007, 2017, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import javax.imageio.ImageIO; 25 import java.awt.*; 26 import java.awt.datatransfer.Clipboard; 27 import java.awt.event.AWTEventListener; 28 import java.awt.event.KeyEvent; 29 import java.awt.im.InputMethodHighlight; 30 import java.awt.image.BufferedImage; 31 import java.awt.image.ColorModel; 32 import java.awt.image.MemoryImageSource; 33 import java.beans.PropertyChangeEvent; 34 import java.beans.PropertyChangeListener; 35 import java.io.File; 36 import java.io.FileInputStream; 37 import java.io.IOException; 38 import java.net.URL; 39 import java.util.Map; 40 41 /* 42 * @test 43 * @summary Check that Toolkit methods do not throw unexpected exceptions 44 * in headless mode 45 * @run main/othervm -Djava.awt.headless=true HeadlessToolkit 46 */ 47 48 public class HeadlessToolkit { 49 50 class awtEventListener implements AWTEventListener { 51 public void eventDispatched(AWTEvent e) { 52 } 53 } 54 55 class propChangeListener implements PropertyChangeListener { 56 public void propertyChange(PropertyChangeEvent e) { 57 } 58 } 59 60 public static void main(String args[]) throws IOException { 61 new HeadlessToolkit().doTest(); 62 } 63 64 void doTest() throws IOException { 65 Toolkit tk = Toolkit.getDefaultToolkit(); 66 String[] fl = tk.getFontList(); 67 FontMetrics fm = tk.getFontMetrics(new Font(fl[0], Font.PLAIN, 10)); 68 tk.sync(); 69 tk.beep(); 70 71 boolean exceptions = false; 72 try { 73 Dimension d = tk.getScreenSize(); 74 } catch (HeadlessException e) { 75 exceptions = true; 76 } 77 if (!exceptions) 78 throw new RuntimeException("HeadlessException did not occur when expected"); 79 80 exceptions = false; 81 try { 82 int res = tk.getScreenResolution(); 83 } catch (HeadlessException e) { 84 exceptions = true; 85 } 86 if (!exceptions) 87 throw new RuntimeException("HeadlessException did not occur when expected"); 88 89 exceptions = false; 90 try { 91 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 92 Graphics2D gd = ge.createGraphics(new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR)); 93 GraphicsConfiguration gc = gd.getDeviceConfiguration(); 94 Insets res = tk.getScreenInsets(gc); 95 } catch (HeadlessException e) { 96 exceptions = true; 97 } 98 if (!exceptions) 99 throw new RuntimeException("HeadlessException did not occur when expected"); 100 101 exceptions = false; 102 try { 103 ColorModel cm = tk.getColorModel(); 104 } catch (HeadlessException e) { 105 exceptions = true; 106 } 107 if (!exceptions) 108 throw new RuntimeException("HeadlessException did not occur when expected"); 109 110 exceptions = false; 111 try { 112 int km = tk.getMenuShortcutKeyMask(); 113 } catch (HeadlessException e) { 114 exceptions = true; 115 } 116 if (!exceptions) 117 throw new RuntimeException("HeadlessException did not occur when expected"); 118 119 exceptions = false; 120 try { 121 int km = tk.getMenuShortcutKeyMaskEx(); 122 } catch (HeadlessException e) { 123 exceptions = true; 124 } 125 if (!exceptions) 126 throw new RuntimeException("HeadlessException did not occur when expected"); 127 128 exceptions = false; 129 try { 130 boolean state = tk.getLockingKeyState(KeyEvent.VK_CAPS_LOCK); 131 } catch (HeadlessException e) { 132 exceptions = true; 133 } 134 if (!exceptions) 135 throw new RuntimeException("HeadlessException did not occur when expected"); 136 137 exceptions = false; 138 try { 139 boolean state = tk.getLockingKeyState(KeyEvent.VK_NUM_LOCK); 140 } catch (HeadlessException e) { 141 exceptions = true; 142 } 143 if (!exceptions) 144 throw new RuntimeException("HeadlessException did not occur when expected"); 145 146 exceptions = false; 147 try { 148 boolean state = tk.getLockingKeyState(KeyEvent.VK_KANA_LOCK); 149 } catch (HeadlessException e) { 150 exceptions = true; 151 } 152 if (!exceptions) 153 throw new RuntimeException("HeadlessException did not occur when expected"); 154 155 exceptions = false; 156 try { 157 boolean state = tk.getLockingKeyState(KeyEvent.VK_SCROLL_LOCK); 158 } catch (HeadlessException e) { 159 exceptions = true; 160 } 161 if (!exceptions) 162 throw new RuntimeException("HeadlessException did not occur when expected"); 163 164 exceptions = false; 165 try { 166 tk.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true); 167 } catch (HeadlessException e) { 168 exceptions = true; 169 } 170 if (!exceptions) 171 throw new RuntimeException("HeadlessException did not occur when expected"); 172 173 exceptions = false; 174 try { 175 tk.setLockingKeyState(KeyEvent.VK_NUM_LOCK, true); 176 } catch (HeadlessException e) { 177 exceptions = true; 178 } 179 if (!exceptions) 180 throw new RuntimeException("HeadlessException did not occur when expected"); 181 182 exceptions = false; 183 try { 184 tk.setLockingKeyState(KeyEvent.VK_KANA_LOCK, true); 185 } catch (HeadlessException e) { 186 exceptions = true; 187 } 188 if (!exceptions) 189 throw new RuntimeException("HeadlessException did not occur when expected"); 190 191 exceptions = false; 192 try { 193 tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, true); 194 } catch (HeadlessException e) { 195 exceptions = true; 196 } 197 if (!exceptions) 198 throw new RuntimeException("HeadlessException did not occur when expected"); 199 200 exceptions = false; 201 try { 202 tk.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, false); 203 } catch (HeadlessException e) { 204 exceptions = true; 205 } 206 if (!exceptions) 207 throw new RuntimeException("HeadlessException did not occur when expected"); 208 209 exceptions = false; 210 try { 211 tk.setLockingKeyState(KeyEvent.VK_NUM_LOCK, false); 212 } catch (HeadlessException e) { 213 exceptions = true; 214 } 215 if (!exceptions) 216 throw new RuntimeException("HeadlessException did not occur when expected"); 217 218 exceptions = false; 219 try { 220 tk.setLockingKeyState(KeyEvent.VK_KANA_LOCK, false); 221 } catch (HeadlessException e) { 222 exceptions = true; 223 } 224 if (!exceptions) 225 throw new RuntimeException("HeadlessException did not occur when expected"); 226 227 exceptions = false; 228 try { 229 tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, false); 230 } catch (HeadlessException e) { 231 exceptions = true; 232 } 233 if (!exceptions) 234 throw new RuntimeException("HeadlessException did not occur when expected"); 235 236 exceptions = false; 237 try { 238 Dimension d = tk.getBestCursorSize(32, 32); 239 } catch (HeadlessException e) { 240 exceptions = true; 241 } 242 if (!exceptions) 243 throw new RuntimeException("HeadlessException did not occur when expected"); 244 245 exceptions = false; 246 try { 247 int n = tk.getMaximumCursorColors(); 248 } catch (HeadlessException e) { 249 exceptions = true; 250 } 251 if (!exceptions) 252 throw new RuntimeException("HeadlessException did not occur when expected"); 253 254 EventQueue eq = tk.getSystemEventQueue(); 255 awtEventListener el = new awtEventListener(); 256 tk.addAWTEventListener(el, 0xffffffff); 257 tk.removeAWTEventListener(el); 258 259 File[] images = new File[]{new File("image.png"), new File("image.jpg"), new File("image.gif")}; 260 Image im; 261 for (File image : images) { 262 String path = image.getCanonicalPath(); 263 ImageIO.write(new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB), path.substring(path.lastIndexOf('.')+1), image); 264 265 im = tk.getImage(image.getAbsolutePath()); 266 im.flush(); 267 268 FileInputStream fis = new FileInputStream(image); 269 byte[] b = new byte[(int) (image.length())]; 270 fis.read(b); 271 fis.close(); 272 im = tk.createImage(b); 273 im.flush(); 274 275 im = tk.createImage(image.getAbsolutePath()); 276 im.flush(); 277 278 } 279 280 im = tk.getImage(new URL("http://openjdk.java.net/images/openjdk.png")); 281 im.flush(); 282 283 im = tk.createImage(new URL("http://openjdk.java.net/images/openjdk.png")); 284 im.flush(); 285 286 MemoryImageSource mis; 287 int pixels[] = new int[50 * 50]; 288 int index = 0; 289 for (int y = 0; y < 50; y++) { 290 int red = (y * 255) / 49; 291 for (int x = 0; x < 50; x++) { 292 int blue = (x * 255) / 49; 293 pixels[index++] = (255 << 24) | (red << 16) | blue; 294 } 295 } 296 mis = new MemoryImageSource(50, 50, pixels, 0, 50); 297 im = tk.createImage(mis); 298 im.flush(); 299 300 301 exceptions = false; 302 try { 303 Cursor cur = tk.createCustomCursor(new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB), new Point(0, 0), "Stop"); 304 } catch (HeadlessException e) { 305 exceptions = true; 306 } 307 if (!exceptions) 308 throw new RuntimeException("HeadlessException did not occur when expected"); 309 310 exceptions = false; 311 try { 312 Cursor cur = tk.createCustomCursor(new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB), new Point(0, 0), "Stop"); 313 } catch (HeadlessException e) { 314 exceptions = true; 315 } 316 if (!exceptions) 317 throw new RuntimeException("HeadlessException did not occur when expected"); 318 319 exceptions = false; 320 try { 321 InputMethodHighlight imh = new InputMethodHighlight(true, InputMethodHighlight.CONVERTED_TEXT); 322 Map m = tk.mapInputMethodHighlight(imh); 323 } catch (HeadlessException e) { 324 exceptions = true; 325 } 326 if (!exceptions) 327 throw new RuntimeException("HeadlessException did not occur when expected"); 328 329 exceptions = false; 330 try { 331 Clipboard cl = tk.getSystemClipboard(); 332 } catch (HeadlessException e) { 333 exceptions = true; 334 } 335 if (!exceptions) 336 throw new RuntimeException("HeadlessException did not occur when expected"); 337 } 338 }