1 /*
   2  * Copyright (c) 2007, 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  * @test
  25  * @bug 6646411
  26  * @summary Tests that full screen window and its children receive resize
  27             event when display mode changes
  28  * @author Dmitri.Trembovetski@sun.com: area=Graphics
  29  * @run main/othervm NoResizeEventOnDMChangeTest
  30  * @run main/othervm -Dsun.java2d.d3d=false NoResizeEventOnDMChangeTest
  31  */
  32 
  33 import java.awt.Canvas;
  34 import java.awt.Color;
  35 import java.awt.Component;
  36 import java.awt.DisplayMode;
  37 import java.awt.EventQueue;
  38 import java.awt.Frame;
  39 import java.awt.Graphics;
  40 import java.awt.GraphicsDevice;
  41 import java.awt.GraphicsEnvironment;
  42 import java.awt.Window;
  43 import java.awt.event.ComponentAdapter;
  44 import java.awt.event.ComponentEvent;
  45 import java.awt.event.WindowAdapter;
  46 import java.awt.event.WindowEvent;
  47 
  48 public class NoResizeEventOnDMChangeTest {
  49     public static void main(String[] args) {
  50         final GraphicsDevice gd = GraphicsEnvironment.
  51             getLocalGraphicsEnvironment().getDefaultScreenDevice();
  52 
  53         if (!gd.isFullScreenSupported()) {
  54             System.out.println("Full screen not supported, test passed");
  55             return;
  56         }
  57 
  58         DisplayMode dm = gd.getDisplayMode();
  59         final DisplayMode dms[] = new DisplayMode[2];
  60         for (DisplayMode dm1 : gd.getDisplayModes()) {
  61             if (dm1.getWidth()  != dm.getWidth() ||
  62                 dm1.getHeight() != dm.getHeight())
  63             {
  64                 dms[0] = dm1;
  65                 break;
  66             }
  67         }
  68         if (dms[0] == null) {
  69             System.out.println("Test Passed: all DMs have same dimensions");
  70             return;
  71         }
  72         dms[1] = dm;
  73 
  74         Frame f = new Frame() {
  75             @Override
  76             public void paint(Graphics g) {
  77                 g.setColor(Color.red);
  78                 g.fillRect(0, 0, getWidth(), getHeight());
  79                 g.setColor(Color.green);
  80                 g.drawRect(0, 0, getWidth()-1, getHeight()-1);
  81             }
  82         };
  83         f.setUndecorated(true);
  84         testFSWindow(gd, dms, f);
  85 
  86         Window w = new Window(f) {
  87             @Override
  88             public void paint(Graphics g) {
  89                 g.setColor(Color.magenta);
  90                 g.fillRect(0, 0, getWidth(), getHeight());
  91                 g.setColor(Color.cyan);
  92                 g.drawRect(0, 0, getWidth()-1, getHeight()-1);
  93             }
  94         };
  95         testFSWindow(gd, dms, w);
  96         System.out.println("Test Passed.");
  97     }
  98 
  99     private static void testFSWindow(final GraphicsDevice gd,
 100                                      final DisplayMode dms[],
 101                                      final Window fsWin)
 102     {
 103         System.out.println("Testing FS window: "+fsWin);
 104         Component c = new Canvas() {
 105             @Override
 106             public void paint(Graphics g) {
 107                 g.setColor(Color.blue);
 108                 g.fillRect(0, 0, getWidth(), getHeight());
 109                 g.setColor(Color.magenta);
 110                 g.drawRect(0, 0, getWidth()-1, getHeight()-1);
 111                 g.setColor(Color.red);
 112                 g.drawString("FS Window   : " + fsWin, 50, 50);
 113                 DisplayMode dm =
 114                     getGraphicsConfiguration().getDevice().getDisplayMode();
 115                 g.drawString("Display Mode: " +
 116                              dm.getWidth() + "x" + dm.getHeight(), 50, 75);
 117             }
 118         };
 119         fsWin.add("Center", c);
 120         fsWin.addWindowListener(new WindowAdapter() {
 121             @Override
 122             public void windowClosing(WindowEvent e) {
 123                 fsWin.dispose();
 124                 if (fsWin.getOwner() != null) {
 125                     fsWin.getOwner().dispose();
 126                 }
 127             }
 128         });
 129 
 130         try {
 131             EventQueue.invokeAndWait(new Runnable() {
 132                 public void run() {
 133                     gd.setFullScreenWindow(fsWin);
 134                 }
 135             });
 136         } catch (Exception ex) {}
 137 
 138         sleep(1000);
 139 
 140         final ResizeEventChecker r1 = new ResizeEventChecker();
 141         final ResizeEventChecker r2 = new ResizeEventChecker();
 142 
 143         if (gd.isDisplayChangeSupported()) {
 144             fsWin.addComponentListener(r1);
 145             c.addComponentListener(r2);
 146             for (final DisplayMode dm1 : dms) {
 147                 try {
 148                     EventQueue.invokeAndWait(new Runnable() {
 149                         public void run() {
 150                             System.err.printf("----------- Setting DM %dx%d:\n",
 151                                               dm1.getWidth(), dm1.getHeight());
 152                             try {
 153                                 gd.setDisplayMode(dm1);
 154                                 r1.incDmChanges();
 155                                 r2.incDmChanges();
 156                             } catch (IllegalArgumentException iae) {}
 157                         }
 158                     });
 159                 } catch (Exception ex) {}
 160                 for (int i = 0; i < 3; i++) {
 161                     fsWin.repaint();
 162                     sleep(1000);
 163                 }
 164             }
 165             fsWin.removeComponentListener(r1);
 166             c.removeComponentListener(r2);
 167         }
 168         try {
 169            EventQueue.invokeAndWait(new Runnable() {
 170                public void run() {
 171                    gd.setFullScreenWindow(null);
 172                     fsWin.dispose();
 173                     if (fsWin.getOwner() != null) {
 174                         fsWin.getOwner().dispose();
 175                     }
 176                 }
 177             });
 178         } catch (Exception ex) {}
 179 
 180         System.out.printf("FS Window: resizes=%d, dm changes=%d\n",
 181                            r1.getResizes(), r1.getDmChanges());
 182         System.out.printf("Component: resizes=%d, dm changes=%d\n",
 183                           r2.getResizes(), r2.getDmChanges());
 184         if (r1.getResizes() < r1.getDmChanges()) {
 185             throw new RuntimeException("FS Window didn't receive all resizes!");
 186         }
 187         if (r2.getResizes() < r2.getDmChanges()) {
 188             throw new RuntimeException("Component didn't receive all resizes!");
 189         }
 190     }
 191 
 192     static void sleep(long ms) {
 193         try {
 194             Thread.sleep(ms);
 195         } catch (InterruptedException ex) {}
 196     }
 197     static class ResizeEventChecker extends ComponentAdapter {
 198         int dmChanges;
 199         int resizes;
 200 
 201         @Override
 202         public synchronized void componentResized(ComponentEvent e) {
 203             System.out.println("Received resize event for "+e.getSource());
 204             resizes++;
 205         }
 206         public synchronized int getResizes() {
 207             return resizes;
 208         }
 209         public synchronized void incDmChanges() {
 210             dmChanges++;
 211         }
 212         public synchronized int getDmChanges() {
 213             return dmChanges;
 214         }
 215     }
 216 }