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