Path: blob/master/test/jdk/java/awt/FullScreen/SetFSWindow/FSFrame.java
41153 views
/*1* Copyright (c) 2005, 2018, 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* @key headful26* @bug 6240507 666264227* @summary verify that isFullScreenSupported and getFullScreenWindow work28* correctly with and without a SecurityManager. Note that the test may fail29* on older Gnome versions (see bug 6500686).30* @run main/othervm -Djava.security.manager=allow FSFrame31* @run main/othervm -Djava.security.manager=allow -Dsun.java2d.noddraw=true FSFrame32*/3334import java.awt.*;35import java.awt.image.*;36import java.io.File;37import java.io.IOException;38import java.lang.reflect.InvocationTargetException;39import javax.imageio.ImageIO;4041public class FSFrame extends Frame implements Runnable {4243// Don't start the test until the window is visible44boolean visible = false;45Robot robot = null;46static volatile boolean done = false;4748public void paint(Graphics g) {49if (!visible && getWidth() != 0 && getHeight() != 0) {50visible = true;51try {52GraphicsDevice gd = getGraphicsConfiguration().getDevice();53robot = new Robot(gd);54} catch (Exception e) {55System.out.println("Problem creating robot: cannot verify FS " +56"window display");57}58}59g.setColor(Color.green);60g.fillRect(0, 0, getWidth(), getHeight());61}6263@Override64public void update(Graphics g) {65paint(g);66}6768boolean checkColor(int x, int y, BufferedImage bImg) {69int pixelColor;70int correctColor = Color.green.getRGB();71pixelColor = bImg.getRGB(x, y);72if (pixelColor != correctColor) {73System.out.println("FAILURE: pixelColor " +74Integer.toHexString(pixelColor) +75" != correctColor " +76Integer.toHexString(correctColor) +77" at coordinates (" + x + ", " + y + ")");78return false;79}80return true;81}8283void checkFSDisplay(boolean fsSupported) {84GraphicsConfiguration gc = getGraphicsConfiguration();85GraphicsDevice gd = gc.getDevice();86Rectangle r = gc.getBounds();87Insets in = null;88if (!fsSupported) {89in = Toolkit.getDefaultToolkit().getScreenInsets(gc);90r = new Rectangle(in.left, in.top,91r.width - (in.left + in.right),92r.height - (in.top + in.bottom));93}94BufferedImage bImg = robot.createScreenCapture(r);95// Check that all four corners and middle pixel match the window's96// fill color97if (robot == null) {98return;99}100boolean colorCorrect = true;101colorCorrect &= checkColor(0, 0, bImg);102colorCorrect &= checkColor(0, bImg.getHeight() - 1, bImg);103colorCorrect &= checkColor(bImg.getWidth() - 1, 0, bImg);104colorCorrect &= checkColor(bImg.getWidth() - 1, bImg.getHeight() - 1, bImg);105colorCorrect &= checkColor(bImg.getWidth() / 2, bImg.getHeight() / 2, bImg);106if (!colorCorrect) {107System.err.println("Test failed for mode: fsSupported="+fsSupported);108if (in != null) {109System.err.println("screen insets : " + in);110}111System.err.println("screen shot rect: " + r);112String name = "FSFrame_fs_"+113(fsSupported?"supported":"not_supported")+".png";114try {115ImageIO.write(bImg, "png", new File(name));116System.out.println("Dumped screen shot to "+name);117} catch (IOException ex) {}118throw new Error("Some pixel colors not correct; FS window may not" +119" have been displayed correctly");120}121}122123void checkFSFunctionality(boolean withSecurity) {124GraphicsDevice gd = getGraphicsConfiguration().getDevice();125if (withSecurity) {126SecurityManager sm = new SecurityManager();127System.setSecurityManager(sm);128}129try {130// None of these should throw an exception131final boolean fs = gd.isFullScreenSupported();132System.out.println("FullscreenSupported: " + (fs ? "yes" : "no"));133gd.setFullScreenWindow(this);134try {135// Give the system time to set the FS window and display it136// properly137Thread.sleep(2000);138} catch (Exception e) {}139if (!withSecurity) {140// See if FS window got displayed correctly141try {142EventQueue.invokeAndWait(new Runnable() {143public void run() {144repaint();145checkFSDisplay(fs);146}147});148} catch (InvocationTargetException ex) {149ex.printStackTrace();150} catch (InterruptedException ex) {151ex.printStackTrace();152}153}154// reset window155gd.setFullScreenWindow(null);156try {157// Give the system time to set the FS window and display it158// properly159Thread.sleep(2000);160} catch (Exception e) {}161} catch (SecurityException e) {162e.printStackTrace();163throw new Error("Failure: should not get an exception when " +164"calling isFSSupported or setFSWindow");165}166}167168public void run() {169boolean firstTime = true;170while (!done) {171if (visible) {172checkFSFunctionality(false);173checkFSFunctionality(true);174done = true;175} else {176// sleep while we wait177try {178// Give the system time to set the FS window and display it179// properly180Thread.sleep(100);181} catch (Exception e) {}182}183}184System.out.println("PASS");185}186187public static void main(String args[]) {188FSFrame frame = new FSFrame();189frame.setUndecorated(true);190Thread t = new Thread(frame);191frame.setSize(500, 500);192frame.setVisible(true);193t.start();194while (!done) {195try {196// Do not exit the main thread until the test is finished197Thread.sleep(1000);198} catch (Exception e) {}199}200frame.dispose();201}202}203204205