1/* 2 * Copyright (c) 2016, 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 24import java.awt.Dimension; 25import java.awt.GraphicsConfiguration; 26import java.awt.GraphicsEnvironment; 27import java.awt.HeadlessException; 28import java.awt.Toolkit; 29import java.awt.image.ColorModel; 30 31/** 32 * @test 33 * @bug 8168307 34 * @run main/othervm IsToolkitUseTheMainScreen 35 * @run main/othervm -Djava.awt.headless=true IsToolkitUseTheMainScreen 36 */ 37public final class IsToolkitUseTheMainScreen { 38 39 public static void main(final String[] args) { 40 if (GraphicsEnvironment.isHeadless()) { 41 testHeadless(); 42 } else { 43 testHeadful(); 44 } 45 } 46 47 private static void testHeadless() { 48 try { 49 Toolkit.getDefaultToolkit().getScreenSize(); 50 throw new RuntimeException("HeadlessException is not thrown"); 51 } catch (final HeadlessException ignored) { 52 // expected exception 53 } 54 try { 55 Toolkit.getDefaultToolkit().getColorModel(); 56 throw new RuntimeException("HeadlessException is not thrown"); 57 } catch (final HeadlessException ignored) { 58 // expected exception 59 } 60 } 61 62 private static void testHeadful() { 63 GraphicsEnvironment ge 64 = GraphicsEnvironment.getLocalGraphicsEnvironment(); 65 GraphicsConfiguration gc 66 = ge.getDefaultScreenDevice().getDefaultConfiguration(); 67 Dimension gcSize = gc.getBounds().getSize(); 68 ColorModel gcCM = gc.getColorModel(); 69 70 Dimension toolkitSize = Toolkit.getDefaultToolkit().getScreenSize(); 71 ColorModel toolkitCM = Toolkit.getDefaultToolkit().getColorModel(); 72 73 if (!gcSize.equals(toolkitSize)) { 74 System.err.println("Toolkit size = " + toolkitSize); 75 System.err.println("GraphicsConfiguration size = " + gcSize); 76 throw new RuntimeException("Incorrect size"); 77 } 78 if (!gcCM.equals(toolkitCM)) { 79 System.err.println("Toolkit color model = " + toolkitCM); 80 System.err.println("GraphicsConfiguration color model = " + gcCM); 81 throw new RuntimeException("Incorrect color model"); 82 } 83 } 84} 85