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