1 /*
   2  * Copyright (c) 2007, 2014, 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             boolean state = tk.getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
 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_NUM_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_KANA_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_SCROLL_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             tk.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true);
 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_NUM_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_KANA_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_SCROLL_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_CAPS_LOCK, false);
 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_NUM_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_KANA_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_SCROLL_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             Dimension d = tk.getBestCursorSize(32, 32);
 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             int n = tk.getMaximumCursorColors();
 239         } catch (HeadlessException e) {
 240             exceptions = true;
 241         }
 242         if (!exceptions)
 243             throw new RuntimeException("HeadlessException did not occur when expected");
 244 
 245         EventQueue eq = tk.getSystemEventQueue();
 246         awtEventListener el = new awtEventListener();
 247         tk.addAWTEventListener(el, 0xffffffff);
 248         tk.removeAWTEventListener(el);
 249 
 250         File[] images = new File[]{new File("image.png"), new File("image.jpg"), new File("image.gif")};
 251         Image im;
 252         for (File image : images) {
 253             String path = image.getCanonicalPath();
 254             ImageIO.write(new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB), path.substring(path.lastIndexOf('.')+1), image);
 255 
 256             im = tk.getImage(image.getAbsolutePath());
 257             im.flush();
 258 
 259             FileInputStream fis = new FileInputStream(image);
 260             byte[] b = new byte[(int) (image.length())];
 261             fis.read(b);
 262             fis.close();
 263             im = tk.createImage(b);
 264             im.flush();
 265 
 266             im = tk.createImage(image.getAbsolutePath());
 267             im.flush();
 268 
 269         }
 270 
 271         im = tk.getImage(new URL("http://openjdk.java.net/images/openjdk.png"));
 272         im.flush();
 273 
 274         im = tk.createImage(new URL("http://openjdk.java.net/images/openjdk.png"));
 275         im.flush();
 276 
 277         MemoryImageSource mis;
 278         int pixels[] = new int[50 * 50];
 279         int index = 0;
 280         for (int y = 0; y < 50; y++) {
 281             int red = (y * 255) / 49;
 282             for (int x = 0; x < 50; x++) {
 283                 int blue = (x * 255) / 49;
 284                 pixels[index++] = (255 << 24) | (red << 16) | blue;
 285             }
 286         }
 287         mis = new MemoryImageSource(50, 50, pixels, 0, 50);
 288         im = tk.createImage(mis);
 289         im.flush();
 290 
 291 
 292         exceptions = false;
 293         try {
 294             Cursor cur = tk.createCustomCursor(new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB), new Point(0, 0), "Stop");
 295         } catch (HeadlessException e) {
 296             exceptions = true;
 297         }
 298         if (!exceptions)
 299             throw new RuntimeException("HeadlessException did not occur when expected");
 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             InputMethodHighlight imh = new InputMethodHighlight(true, InputMethodHighlight.CONVERTED_TEXT);
 313             Map m = tk.mapInputMethodHighlight(imh);
 314         } catch (HeadlessException e) {
 315             exceptions = true;
 316         }
 317         if (!exceptions)
 318             throw new RuntimeException("HeadlessException did not occur when expected");
 319 
 320         exceptions = false;
 321         try {
 322             Clipboard cl = tk.getSystemClipboard();
 323         } catch (HeadlessException e) {
 324             exceptions = true;
 325         }
 326         if (!exceptions)
 327             throw new RuntimeException("HeadlessException did not occur when expected");
 328     }
 329 }