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