1 /*
   2  * Copyright (c) 2019, 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 java.awt.Dialog;
  25 import java.awt.Dimension;
  26 import java.awt.EventQueue;
  27 import java.awt.Font;
  28 import java.awt.FontMetrics;
  29 import java.awt.Frame;
  30 import java.awt.Graphics2D;
  31 import java.awt.GraphicsConfiguration;
  32 import java.awt.GraphicsDevice;
  33 import java.awt.GraphicsEnvironment;
  34 import java.awt.HeadlessException;
  35 import java.awt.Image;
  36 import java.awt.Insets;
  37 import java.awt.PrintJob;
  38 import java.awt.Rectangle;
  39 import java.awt.Toolkit;
  40 import java.awt.datatransfer.Clipboard;
  41 import java.awt.font.TextAttribute;
  42 import java.awt.geom.AffineTransform;
  43 import java.awt.im.InputMethodHighlight;
  44 import java.awt.image.BufferedImage;
  45 import java.awt.image.ColorModel;
  46 import java.awt.image.ImageObserver;
  47 import java.awt.image.ImageProducer;
  48 import java.net.URL;
  49 import java.util.Map;
  50 import java.util.Properties;
  51 
  52 /**
  53  * @test
  54  * @bug 8233573
  55  * @key headful
  56  * @summary Toolkit.getScreenInsets should work for custom GraphicsConfiguration
  57  */
  58 public final class GetScreenInsetsCustomGC {
  59 
  60     public static void main(final String[] args) {
  61         // Default GraphicsConfiguration
  62         GraphicsConfiguration dc =
  63                 GraphicsEnvironment.getLocalGraphicsEnvironment()
  64                         .getDefaultScreenDevice()
  65                         .getDefaultConfiguration();
  66         // Custom GraphicsConfiguration
  67         GraphicsConfiguration cd = new _GraphicsConfiguration();
  68         // GraphicsConfiguration of BufferedImage
  69         BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
  70         Graphics2D g = bi.createGraphics();
  71         GraphicsConfiguration bic = g.getDeviceConfiguration();
  72         g.dispose();
  73 
  74         // Default toolkit
  75 
  76         // mey return empty and non-empty insets but never null
  77         if (Toolkit.getDefaultToolkit().getScreenInsets(dc) == null) {
  78             throw new NullPointerException();
  79         }
  80         // return empty insets only for non-default GC
  81         testEmpty(Toolkit.getDefaultToolkit().getScreenInsets(cd));
  82         testEmpty(Toolkit.getDefaultToolkit().getScreenInsets(bic));
  83 
  84         try {
  85             Toolkit.getDefaultToolkit().getScreenInsets(null);
  86             throw new RuntimeException("NullPointerException is not thrown");
  87         } catch (NullPointerException ignored) {
  88             // ok
  89         }
  90 
  91         // Custom toolkit
  92 
  93         Toolkit tk = new _Toolkit();
  94         // mey return empty and non-empty insets but never null
  95         if (tk.getScreenInsets(dc) == null) {
  96             throw new NullPointerException();
  97         }
  98         // return empty insets only for non-default GC
  99         testEmpty(tk.getScreenInsets(cd));
 100         testEmpty(tk.getScreenInsets(bic));
 101 
 102         try {
 103             tk.getScreenInsets(null);
 104             throw new RuntimeException("NullPointerException is not thrown");
 105         } catch (NullPointerException ignored) {
 106             // ok
 107         }
 108     }
 109 
 110     private static void testEmpty(final Insets insets) {
 111         if ((insets.left | insets.top | insets.bottom | insets.right) != 0) {
 112             System.err.println("Expected empty insets");
 113             System.err.println("Actual: " + insets);
 114             throw new RuntimeException();
 115         }
 116     }
 117 
 118     private static class _GraphicsConfiguration extends GraphicsConfiguration {
 119 
 120         @Override
 121         public GraphicsDevice getDevice() {
 122             return null;
 123         }
 124 
 125         @Override
 126         public ColorModel getColorModel() {
 127             return null;
 128         }
 129 
 130         @Override
 131         public ColorModel getColorModel(int transparency) {
 132             return null;
 133         }
 134 
 135         @Override
 136         public AffineTransform getDefaultTransform() {
 137             return null;
 138         }
 139 
 140         @Override
 141         public AffineTransform getNormalizingTransform() {
 142             return null;
 143         }
 144 
 145         @Override
 146         public Rectangle getBounds() {
 147             return null;
 148         }
 149     }
 150 
 151     private static class _Toolkit extends Toolkit {
 152 
 153         @Override
 154         public Dimension getScreenSize() throws HeadlessException {
 155             return null;
 156         }
 157 
 158         @Override
 159         public int getScreenResolution() throws HeadlessException {
 160             return 0;
 161         }
 162 
 163         @Override
 164         public ColorModel getColorModel() throws HeadlessException {
 165             return null;
 166         }
 167 
 168         @Override
 169         public String[] getFontList() {
 170             return new String[0];
 171         }
 172 
 173         @Override
 174         public FontMetrics getFontMetrics(Font font) {
 175             return null;
 176         }
 177 
 178         @Override
 179         public void sync() {
 180 
 181         }
 182 
 183         @Override
 184         public Image getImage(String filename) {
 185             return null;
 186         }
 187 
 188         @Override
 189         public Image getImage(URL url) {
 190             return null;
 191         }
 192 
 193         @Override
 194         public Image createImage(String filename) {
 195             return null;
 196         }
 197 
 198         @Override
 199         public Image createImage(URL url) {
 200             return null;
 201         }
 202 
 203         @Override
 204         public boolean prepareImage(Image image, int width, int height, ImageObserver observer) {
 205             return false;
 206         }
 207 
 208         @Override
 209         public int checkImage(Image image, int width, int height, ImageObserver observer) {
 210             return 0;
 211         }
 212 
 213         @Override
 214         public Image createImage(ImageProducer producer) {
 215             return null;
 216         }
 217 
 218         @Override
 219         public Image createImage(byte[] imagedata, int imageoffset, int imagelength) {
 220             return null;
 221         }
 222 
 223         @Override
 224         public PrintJob getPrintJob(Frame frame, String jobtitle, Properties props) {
 225             return null;
 226         }
 227 
 228         @Override
 229         public void beep() {
 230 
 231         }
 232 
 233         @Override
 234         public Clipboard getSystemClipboard() throws HeadlessException {
 235             return null;
 236         }
 237 
 238         @Override
 239         protected EventQueue getSystemEventQueueImpl() {
 240             return null;
 241         }
 242 
 243         @Override
 244         public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
 245             return false;
 246         }
 247 
 248         @Override
 249         public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType modalExclusionType) {
 250             return false;
 251         }
 252 
 253         @Override
 254         public Map<TextAttribute, ?> mapInputMethodHighlight(InputMethodHighlight highlight) throws HeadlessException {
 255             return null;
 256         }
 257     }
 258 }