430N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
430N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430N/A *
430N/A * This code is free software; you can redistribute it and/or modify it
430N/A * under the terms of the GNU General Public License version 2 only, as
430N/A * published by the Free Software Foundation.
430N/A *
430N/A * This code is distributed in the hope that it will be useful, but WITHOUT
430N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
430N/A * version 2 for more details (a copy is included in the LICENSE file that
430N/A * accompanied this code).
430N/A *
430N/A * You should have received a copy of the GNU General Public License version
430N/A * 2 along with this work; if not, write to the Free Software Foundation,
430N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
430N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
430N/A */
430N/A
430N/A/*
430N/A * @test
430N/A * @bug 6240507 6662642
430N/A * @summary verify that isFullScreenSupported and getFullScreenWindow work
430N/A * correctly with and without a SecurityManager. Note that the test may fail
430N/A * on older Gnome versions (see bug 6500686).
430N/A * @run main FSFrame
430N/A * @run main/othervm -Dsun.java2d.noddraw=true FSFrame
430N/A * @author cheth
430N/A */
430N/A
430N/Aimport java.awt.*;
430N/Aimport java.awt.image.*;
430N/Aimport java.applet.*;
430N/Aimport java.io.File;
430N/Aimport java.io.IOException;
430N/Aimport java.lang.reflect.InvocationTargetException;
430N/Aimport javax.imageio.ImageIO;
430N/A
430N/Apublic class FSFrame extends Frame implements Runnable {
430N/A
430N/A // Don't start the test until the window is visible
430N/A boolean visible = false;
430N/A Robot robot = null;
430N/A static volatile boolean done = false;
430N/A
430N/A public void paint(Graphics g) {
430N/A if (!visible && getWidth() != 0 && getHeight() != 0) {
430N/A visible = true;
430N/A try {
430N/A GraphicsDevice gd = getGraphicsConfiguration().getDevice();
430N/A robot = new Robot(gd);
430N/A } catch (Exception e) {
430N/A System.out.println("Problem creating robot: cannot verify FS " +
430N/A "window display");
430N/A }
430N/A }
430N/A g.setColor(Color.green);
430N/A g.fillRect(0, 0, getWidth(), getHeight());
430N/A }
430N/A
430N/A @Override
430N/A public void update(Graphics g) {
430N/A paint(g);
430N/A }
430N/A
430N/A boolean checkColor(int x, int y, BufferedImage bImg) {
430N/A int pixelColor;
430N/A int correctColor = Color.green.getRGB();
430N/A pixelColor = bImg.getRGB(x, y);
430N/A if (pixelColor != correctColor) {
430N/A System.out.println("FAILURE: pixelColor " +
430N/A Integer.toHexString(pixelColor) +
430N/A " != correctColor " +
430N/A Integer.toHexString(correctColor) +
430N/A " at coordinates (" + x + ", " + y + ")");
430N/A return false;
430N/A }
430N/A return true;
430N/A }
430N/A
430N/A void checkFSDisplay(boolean fsSupported) {
430N/A GraphicsConfiguration gc = getGraphicsConfiguration();
430N/A GraphicsDevice gd = gc.getDevice();
430N/A Rectangle r = gc.getBounds();
430N/A Insets in = null;
430N/A if (!fsSupported) {
430N/A in = Toolkit.getDefaultToolkit().getScreenInsets(gc);
430N/A r = new Rectangle(in.left, in.top,
430N/A r.width - (in.left + in.right),
430N/A r.height - (in.top + in.bottom));
430N/A }
430N/A BufferedImage bImg = robot.createScreenCapture(r);
430N/A // Check that all four corners and middle pixel match the window's
430N/A // fill color
430N/A if (robot == null) {
430N/A return;
430N/A }
430N/A boolean colorCorrect = true;
430N/A colorCorrect &= checkColor(0, 0, bImg);
430N/A colorCorrect &= checkColor(0, bImg.getHeight() - 1, bImg);
430N/A colorCorrect &= checkColor(bImg.getWidth() - 1, 0, bImg);
430N/A colorCorrect &= checkColor(bImg.getWidth() - 1, bImg.getHeight() - 1, bImg);
430N/A colorCorrect &= checkColor(bImg.getWidth() / 2, bImg.getHeight() / 2, bImg);
430N/A if (!colorCorrect) {
430N/A System.err.println("Test failed for mode: fsSupported="+fsSupported);
430N/A if (in != null) {
430N/A System.err.println("screen insets : " + in);
430N/A }
430N/A System.err.println("screen shot rect: " + r);
430N/A String name = "FSFrame_fs_"+
430N/A (fsSupported?"supported":"not_supported")+".png";
430N/A try {
430N/A ImageIO.write(bImg, "png", new File(name));
430N/A System.out.println("Dumped screen shot to "+name);
430N/A } catch (IOException ex) {}
430N/A throw new Error("Some pixel colors not correct; FS window may not" +
430N/A " have been displayed correctly");
430N/A }
430N/A }
430N/A
430N/A void checkFSFunctionality(boolean withSecurity) {
430N/A GraphicsDevice gd = getGraphicsConfiguration().getDevice();
430N/A if (withSecurity) {
430N/A SecurityManager sm = new SecurityManager();
430N/A System.setSecurityManager(sm);
430N/A }
430N/A try {
430N/A // None of these should throw an exception
430N/A final boolean fs = gd.isFullScreenSupported();
430N/A System.out.println("FullscreenSupported: " + (fs ? "yes" : "no"));
430N/A gd.setFullScreenWindow(this);
430N/A try {
430N/A // Give the system time to set the FS window and display it
430N/A // properly
430N/A Thread.sleep(2000);
430N/A } catch (Exception e) {}
430N/A if (!withSecurity) {
430N/A // See if FS window got displayed correctly
430N/A try {
430N/A EventQueue.invokeAndWait(new Runnable() {
430N/A public void run() {
430N/A repaint();
430N/A checkFSDisplay(fs);
430N/A }
430N/A });
430N/A } catch (InvocationTargetException ex) {
430N/A ex.printStackTrace();
430N/A } catch (InterruptedException ex) {
430N/A ex.printStackTrace();
430N/A }
430N/A }
430N/A // reset window
430N/A gd.setFullScreenWindow(null);
430N/A try {
430N/A // Give the system time to set the FS window and display it
430N/A // properly
430N/A Thread.sleep(2000);
430N/A } catch (Exception e) {}
430N/A } catch (SecurityException e) {
430N/A e.printStackTrace();
430N/A throw new Error("Failure: should not get an exception when " +
430N/A "calling isFSSupported or setFSWindow");
430N/A }
430N/A }
430N/A
430N/A public void run() {
430N/A boolean firstTime = true;
430N/A while (!done) {
430N/A if (visible) {
430N/A checkFSFunctionality(false);
430N/A checkFSFunctionality(true);
430N/A done = true;
430N/A } else {
430N/A // sleep while we wait
430N/A try {
430N/A // Give the system time to set the FS window and display it
430N/A // properly
430N/A Thread.sleep(100);
430N/A } catch (Exception e) {}
430N/A }
430N/A }
430N/A System.out.println("PASS");
430N/A }
430N/A
430N/A public static void main(String args[]) {
430N/A FSFrame frame = new FSFrame();
430N/A frame.setUndecorated(true);
430N/A Thread t = new Thread(frame);
430N/A frame.setSize(500, 500);
430N/A frame.setVisible(true);
430N/A t.start();
430N/A while (!done) {
430N/A try {
430N/A // Do not exit the main thread until the test is finished
430N/A Thread.sleep(1000);
430N/A } catch (Exception e) {}
430N/A }
430N/A frame.dispose();
430N/A }
430N/A}