1 /*
   2  * Copyright (c) 2005, 2008, 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 /*
  25  * @test
  26  * @bug 6240507 6662642
  27  * @summary verify that isFullScreenSupported and getFullScreenWindow work
  28  * correctly with and without a SecurityManager. Note that the test may fail
  29  * on older Gnome versions (see bug 6500686).
  30  * @run main FSFrame
  31  * @run main/othervm -Dsun.java2d.noddraw=true FSFrame
  32  * @author cheth
  33  */
  34 
  35 import java.awt.*;
  36 import java.awt.image.*;
  37 import java.applet.*;
  38 import java.io.File;
  39 import java.io.IOException;
  40 import java.lang.reflect.InvocationTargetException;
  41 import javax.imageio.ImageIO;
  42 
  43 public class FSFrame extends Frame implements Runnable {
  44 
  45     // Don't start the test until the window is visible
  46     boolean visible = false;
  47     Robot robot = null;
  48     static volatile boolean done = false;
  49 
  50     public void paint(Graphics g) {
  51         if (!visible && getWidth() != 0 && getHeight() != 0) {
  52             visible = true;
  53             try {
  54                 GraphicsDevice gd = getGraphicsConfiguration().getDevice();
  55                 robot = new Robot(gd);
  56             } catch (Exception e) {
  57                 System.out.println("Problem creating robot: cannot verify FS " +
  58                                    "window display");
  59             }
  60         }
  61         g.setColor(Color.green);
  62         g.fillRect(0, 0, getWidth(), getHeight());
  63     }
  64 
  65     @Override
  66     public void update(Graphics g) {
  67         paint(g);
  68     }
  69 
  70     boolean checkColor(int x, int y, BufferedImage bImg) {
  71         int pixelColor;
  72         int correctColor = Color.green.getRGB();
  73         pixelColor = bImg.getRGB(x, y);
  74         if (pixelColor != correctColor) {
  75             System.out.println("FAILURE: pixelColor " +
  76                                Integer.toHexString(pixelColor) +
  77                                " != correctColor " +
  78                                Integer.toHexString(correctColor) +
  79                                " at coordinates (" + x + ", " + y + ")");
  80             return false;
  81         }
  82         return true;
  83     }
  84 
  85     void checkFSDisplay(boolean fsSupported) {
  86         GraphicsConfiguration gc = getGraphicsConfiguration();
  87         GraphicsDevice gd = gc.getDevice();
  88         Rectangle r = gc.getBounds();
  89         Insets in = null;
  90         if (!fsSupported) {
  91             in = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  92             r = new Rectangle(in.left, in.top,
  93                               r.width -  (in.left + in.right),
  94                               r.height - (in.top  + in.bottom));
  95         }
  96         BufferedImage bImg = robot.createScreenCapture(r);
  97         // Check that all four corners and middle pixel match the window's
  98         // fill color
  99         if (robot == null) {
 100             return;
 101         }
 102         boolean colorCorrect = true;
 103         colorCorrect &= checkColor(0, 0, bImg);
 104         colorCorrect &= checkColor(0, bImg.getHeight() - 1, bImg);
 105         colorCorrect &= checkColor(bImg.getWidth() - 1, 0, bImg);
 106         colorCorrect &= checkColor(bImg.getWidth() - 1, bImg.getHeight() - 1, bImg);
 107         colorCorrect &= checkColor(bImg.getWidth() / 2, bImg.getHeight() / 2, bImg);
 108         if (!colorCorrect) {
 109             System.err.println("Test failed for mode: fsSupported="+fsSupported);
 110             if (in != null) {
 111                 System.err.println("screen insets   : " + in);
 112             }
 113             System.err.println("screen shot rect: " + r);
 114             String name = "FSFrame_fs_"+
 115                     (fsSupported?"supported":"not_supported")+".png";
 116             try {
 117                 ImageIO.write(bImg, "png", new File(name));
 118                 System.out.println("Dumped screen shot to "+name);
 119             } catch (IOException ex) {}
 120             throw new Error("Some pixel colors not correct; FS window may not" +
 121                             " have been displayed correctly");
 122         }
 123     }
 124 
 125     void checkFSFunctionality(boolean withSecurity) {
 126         GraphicsDevice gd = getGraphicsConfiguration().getDevice();
 127         if (withSecurity) {
 128             SecurityManager sm = new SecurityManager();
 129             System.setSecurityManager(sm);
 130         }
 131         try {
 132             // None of these should throw an exception
 133             final boolean fs = gd.isFullScreenSupported();
 134             System.out.println("FullscreenSupported: " + (fs ? "yes" : "no"));
 135             gd.setFullScreenWindow(this);
 136             try {
 137                 // Give the system time to set the FS window and display it
 138                 // properly
 139                 Thread.sleep(2000);
 140             } catch (Exception e) {}
 141             if (!withSecurity) {
 142                 // See if FS window got displayed correctly
 143                 try {
 144                     EventQueue.invokeAndWait(new Runnable() {
 145                         public void run() {
 146                             repaint();
 147                             checkFSDisplay(fs);
 148                         }
 149                     });
 150                 } catch (InvocationTargetException ex) {
 151                     ex.printStackTrace();
 152                 } catch (InterruptedException ex) {
 153                     ex.printStackTrace();
 154                 }
 155             }
 156             // reset window
 157             gd.setFullScreenWindow(null);
 158             try {
 159                 // Give the system time to set the FS window and display it
 160                 // properly
 161                 Thread.sleep(2000);
 162             } catch (Exception e) {}
 163         } catch (SecurityException e) {
 164             e.printStackTrace();
 165             throw new Error("Failure: should not get an exception when " +
 166                             "calling isFSSupported or setFSWindow");
 167         }
 168     }
 169 
 170     public void run() {
 171         boolean firstTime = true;
 172         while (!done) {
 173             if (visible) {
 174                 checkFSFunctionality(false);
 175                 checkFSFunctionality(true);
 176                 done = true;
 177             } else {
 178                 // sleep while we wait
 179                 try {
 180                     // Give the system time to set the FS window and display it
 181                     // properly
 182                     Thread.sleep(100);
 183                 } catch (Exception e) {}
 184             }
 185         }
 186         System.out.println("PASS");
 187     }
 188 
 189     public static void main(String args[]) {
 190         FSFrame frame = new FSFrame();
 191         frame.setUndecorated(true);
 192         Thread t = new Thread(frame);
 193         frame.setSize(500, 500);
 194         frame.setVisible(true);
 195         t.start();
 196         while (!done) {
 197             try {
 198                 // Do not exit the main thread until the test is finished
 199                 Thread.sleep(1000);
 200             } catch (Exception e) {}
 201         }
 202         frame.dispose();
 203     }
 204 }