1 /*
   2  * Copyright (c) 2006, 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 6366813 6459844
  28  * @summary Tests that no exception is thrown if a frame is resized just
  29  * before we create a bufferStrategy
  30  * @author Dmitri.Trembovetski area=FullScreen/BufferStrategy
  31  * @run main/othervm -Dsun.java2d.opengl=true BufferStrategyExceptionTest
  32  * @run main/othervm BufferStrategyExceptionTest
  33  */
  34 
  35 import java.awt.AWTException;
  36 import java.awt.BufferCapabilities;
  37 import java.awt.Color;
  38 import java.awt.Dimension;
  39 import java.awt.Frame;
  40 import java.awt.Graphics;
  41 import java.awt.GraphicsConfiguration;
  42 import java.awt.GraphicsDevice;
  43 import java.awt.GraphicsEnvironment;
  44 import java.awt.ImageCapabilities;
  45 import java.awt.image.BufferStrategy;
  46 import java.awt.image.BufferedImage;
  47 
  48 /**
  49  * The purpose of this test is to make sure that we do not throw an
  50  * IllegalStateException during the creation of BufferStrategy if
  51  * a window has been resized just before our creation attempt.
  52  *
  53  * We test both windowed and fullscreen mode, although the exception has
  54  * been observed in full screen mode only.
  55  */
  56 public class BufferStrategyExceptionTest {
  57     private static final int TEST_REPS = 20;
  58 
  59     public static void main(String[] args) {
  60         GraphicsDevice gd =
  61             GraphicsEnvironment.getLocalGraphicsEnvironment().
  62                 getDefaultScreenDevice();
  63 
  64         for (int i = 0; i < TEST_REPS; i++) {
  65             TestFrame f = new TestFrame();
  66             f.pack();
  67             f.setSize(400, 400);
  68             f.setVisible(true);
  69             if (i % 2 == 0) {
  70                 gd.setFullScreenWindow(f);
  71             }
  72             // generate a resize event which will invalidate the peer's
  73             // surface data and hopefully cause an exception during
  74             // BufferStrategy creation in TestFrame.render()
  75             Dimension d = f.getSize();
  76             d.width -= 5; d.height -= 5;
  77             f.setSize(d);
  78 
  79             f.render();
  80             gd.setFullScreenWindow(null);
  81             sleep(100);
  82             f.dispose();
  83         }
  84         System.out.println("Test passed.");
  85     }
  86 
  87     private static void sleep(long msecs) {
  88         try {
  89             Thread.sleep(msecs);
  90         } catch (InterruptedException ex) {
  91             ex.printStackTrace();
  92         }
  93     }
  94 
  95     private static final BufferedImage bi =
  96         new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
  97 
  98     static class TestFrame extends Frame {
  99         TestFrame() {
 100             setUndecorated(true);
 101             setIgnoreRepaint(true);
 102             setSize(400, 400);
 103         }
 104 
 105         public void render() {
 106             ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
 107             ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
 108             BufferCapabilities bufCap =
 109                 new BufferCapabilities(imgFrontBufCap,
 110                     imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
 111             try {
 112 
 113                 createBufferStrategy(2, bufCap);
 114             } catch (AWTException ex) {
 115                 createBufferStrategy(2);
 116             }
 117 
 118             BufferStrategy bs = getBufferStrategy();
 119             do {
 120                 Graphics g =  bs.getDrawGraphics();
 121                 g.setColor(Color.green);
 122                 g.fillRect(0, 0, getWidth(), getHeight());
 123 
 124                 g.setColor(Color.red);
 125                 g.drawString("Rendering test", 20, 20);
 126 
 127                 g.drawImage(bi, 50, 50, null);
 128 
 129                 g.dispose();
 130                 bs.show();
 131             } while (bs.contentsLost()||bs.contentsRestored());
 132         }
 133     }
 134 
 135 }