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