Path: blob/master/test/jdk/java/awt/FullScreen/BufferStrategyExceptionTest/BufferStrategyExceptionTest.java
41153 views
/*1* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 6366813 6459844 819861327* @summary Tests that no exception is thrown if a frame is resized just28* before we create a bufferStrategy29* @author Dmitri.Trembovetski area=FullScreen/BufferStrategy30* @run main/othervm BufferStrategyExceptionTest31*/3233import java.awt.AWTException;34import java.awt.BufferCapabilities;35import java.awt.Color;36import java.awt.Dimension;37import java.awt.Frame;38import java.awt.Graphics;39import java.awt.GraphicsConfiguration;40import java.awt.GraphicsDevice;41import java.awt.GraphicsEnvironment;42import java.awt.ImageCapabilities;43import java.awt.image.BufferStrategy;44import java.awt.image.BufferedImage;4546/**47* The purpose of this test is to make sure that we do not throw an48* IllegalStateException during the creation of BufferStrategy if49* a window has been resized just before our creation attempt.50*51* We test both windowed and fullscreen mode, although the exception has52* been observed in full screen mode only.53*/54public class BufferStrategyExceptionTest {55private static final int TEST_REPS = 20;5657public static void main(String[] args) {58GraphicsDevice gd =59GraphicsEnvironment.getLocalGraphicsEnvironment().60getDefaultScreenDevice();6162for (int i = 0; i < TEST_REPS; i++) {63TestFrame f = new TestFrame();64f.pack();65f.setSize(400, 400);66f.setVisible(true);67if (i % 2 == 0) {68gd.setFullScreenWindow(f);69}70// generate a resize event which will invalidate the peer's71// surface data and hopefully cause an exception during72// BufferStrategy creation in TestFrame.render()73Dimension d = f.getSize();74d.width -= 5; d.height -= 5;75f.setSize(d);7677f.render();78gd.setFullScreenWindow(null);79sleep(100);80f.dispose();81}82System.out.println("Test passed.");83}8485private static void sleep(long msecs) {86try {87Thread.sleep(msecs);88} catch (InterruptedException ex) {89ex.printStackTrace();90}91}9293private static final BufferedImage bi =94new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);9596static class TestFrame extends Frame {97TestFrame() {98setUndecorated(true);99setIgnoreRepaint(true);100setSize(400, 400);101}102103public void render() {104ImageCapabilities imgBackBufCap = new ImageCapabilities(true);105ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);106BufferCapabilities bufCap =107new BufferCapabilities(imgFrontBufCap,108imgBackBufCap, BufferCapabilities.FlipContents.COPIED);109try {110111createBufferStrategy(2, bufCap);112} catch (AWTException ex) {113createBufferStrategy(2);114}115116BufferStrategy bs = getBufferStrategy();117do {118Graphics g = bs.getDrawGraphics();119g.setColor(Color.green);120g.fillRect(0, 0, getWidth(), getHeight());121122g.setColor(Color.red);123g.drawString("Rendering test", 20, 20);124125g.drawImage(bi, 50, 50, null);126127g.dispose();128bs.show();129} while (bs.contentsLost()||bs.contentsRestored());130}131}132133}134135136