Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/FullScreen/SetFSWindow/FSFrame.java
38828 views
/*1* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6240507 666264226* @summary verify that isFullScreenSupported and getFullScreenWindow work27* correctly with and without a SecurityManager. Note that the test may fail28* on older Gnome versions (see bug 6500686).29* @run main FSFrame30* @run main/othervm -Dsun.java2d.noddraw=true FSFrame31* @author cheth32*/3334import java.awt.*;35import java.awt.image.*;36import java.applet.*;37import java.io.File;38import java.io.IOException;39import java.lang.reflect.InvocationTargetException;40import javax.imageio.ImageIO;4142public class FSFrame extends Frame implements Runnable {4344// Don't start the test until the window is visible45boolean visible = false;46Robot robot = null;47static volatile boolean done = false;4849public void paint(Graphics g) {50if (!visible && getWidth() != 0 && getHeight() != 0) {51visible = true;52try {53GraphicsDevice gd = getGraphicsConfiguration().getDevice();54robot = new Robot(gd);55} catch (Exception e) {56System.out.println("Problem creating robot: cannot verify FS " +57"window display");58}59}60g.setColor(Color.green);61g.fillRect(0, 0, getWidth(), getHeight());62}6364@Override65public void update(Graphics g) {66paint(g);67}6869boolean checkColor(int x, int y, BufferedImage bImg) {70int pixelColor;71int correctColor = Color.green.getRGB();72pixelColor = bImg.getRGB(x, y);73if (pixelColor != correctColor) {74System.out.println("FAILURE: pixelColor " +75Integer.toHexString(pixelColor) +76" != correctColor " +77Integer.toHexString(correctColor) +78" at coordinates (" + x + ", " + y + ")");79return false;80}81return true;82}8384void checkFSDisplay(boolean fsSupported) {85GraphicsConfiguration gc = getGraphicsConfiguration();86GraphicsDevice gd = gc.getDevice();87Rectangle r = gc.getBounds();88Insets in = null;89if (!fsSupported) {90in = Toolkit.getDefaultToolkit().getScreenInsets(gc);91r = new Rectangle(in.left, in.top,92r.width - (in.left + in.right),93r.height - (in.top + in.bottom));94}95BufferedImage bImg = robot.createScreenCapture(r);96// Check that all four corners and middle pixel match the window's97// fill color98if (robot == null) {99return;100}101boolean colorCorrect = true;102colorCorrect &= checkColor(0, 0, bImg);103colorCorrect &= checkColor(0, bImg.getHeight() - 1, bImg);104colorCorrect &= checkColor(bImg.getWidth() - 1, 0, bImg);105colorCorrect &= checkColor(bImg.getWidth() - 1, bImg.getHeight() - 1, bImg);106colorCorrect &= checkColor(bImg.getWidth() / 2, bImg.getHeight() / 2, bImg);107if (!colorCorrect) {108System.err.println("Test failed for mode: fsSupported="+fsSupported);109if (in != null) {110System.err.println("screen insets : " + in);111}112System.err.println("screen shot rect: " + r);113String name = "FSFrame_fs_"+114(fsSupported?"supported":"not_supported")+".png";115try {116ImageIO.write(bImg, "png", new File(name));117System.out.println("Dumped screen shot to "+name);118} catch (IOException ex) {}119throw new Error("Some pixel colors not correct; FS window may not" +120" have been displayed correctly");121}122}123124void checkFSFunctionality(boolean withSecurity) {125GraphicsDevice gd = getGraphicsConfiguration().getDevice();126if (withSecurity) {127SecurityManager sm = new SecurityManager();128System.setSecurityManager(sm);129}130try {131// None of these should throw an exception132final boolean fs = gd.isFullScreenSupported();133System.out.println("FullscreenSupported: " + (fs ? "yes" : "no"));134gd.setFullScreenWindow(this);135try {136// Give the system time to set the FS window and display it137// properly138Thread.sleep(2000);139} catch (Exception e) {}140if (!withSecurity) {141// See if FS window got displayed correctly142try {143EventQueue.invokeAndWait(new Runnable() {144public void run() {145repaint();146checkFSDisplay(fs);147}148});149} catch (InvocationTargetException ex) {150ex.printStackTrace();151} catch (InterruptedException ex) {152ex.printStackTrace();153}154}155// reset window156gd.setFullScreenWindow(null);157try {158// Give the system time to set the FS window and display it159// properly160Thread.sleep(2000);161} catch (Exception e) {}162} catch (SecurityException e) {163e.printStackTrace();164throw new Error("Failure: should not get an exception when " +165"calling isFSSupported or setFSWindow");166}167}168169public void run() {170boolean firstTime = true;171while (!done) {172if (visible) {173checkFSFunctionality(false);174checkFSFunctionality(true);175done = true;176} else {177// sleep while we wait178try {179// Give the system time to set the FS window and display it180// properly181Thread.sleep(100);182} catch (Exception e) {}183}184}185System.out.println("PASS");186}187188public static void main(String args[]) {189FSFrame frame = new FSFrame();190frame.setUndecorated(true);191Thread t = new Thread(frame);192frame.setSize(500, 500);193frame.setVisible(true);194t.start();195while (!done) {196try {197// Do not exit the main thread until the test is finished198Thread.sleep(1000);199} catch (Exception e) {}200}201frame.dispose();202}203}204205206