Path: blob/master/test/jdk/sun/java2d/DirectX/InfiniteValidationLoopTest/InfiniteValidationLoopTest.java
41153 views
/*1* Copyright (c) 2007, 2016, 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 664801827* @summary Tests that we don't run into infinite validation loop when copying28a VolatileImage to the screen29* @author [email protected]: area=Graphics30* @run main/othervm InfiniteValidationLoopTest31* @run main/othervm -Dsun.java2d.d3d=false InfiniteValidationLoopTest32*/33import java.awt.Color;34import java.awt.Frame;35import java.awt.Graphics;36import java.awt.GraphicsConfiguration;37import static java.awt.image.VolatileImage.*;38import java.awt.image.VolatileImage;39import java.util.concurrent.CountDownLatch;4041public class InfiniteValidationLoopTest extends Frame {42private static volatile boolean failed = false;43private static final int LOOP_THRESHOLD = 50;44private static volatile CountDownLatch latch;45private VolatileImage vi;4647public InfiniteValidationLoopTest() {48super("InfiniteValidationLoopTest");49}5051@Override52public void paint(Graphics g) {53try {54runTest(g);55} finally {56latch.countDown();57}58}5960private void runTest(Graphics g) {61int status = IMAGE_OK;62int count1 = 0;63do {64GraphicsConfiguration gc = getGraphicsConfiguration();65int count2 = 0;66while (vi == null || (status = vi.validate(gc)) != IMAGE_OK) {67if (++count2 > LOOP_THRESHOLD) {68System.err.println("Infinite loop detected: count2="+count2);69failed = true;70return;71}72if (vi == null || status == IMAGE_INCOMPATIBLE) {73if (vi != null) { vi.flush(); vi = null; }74vi = gc.createCompatibleVolatileImage(100, 100);75continue;76}77if (status == IMAGE_RESTORED) {78Graphics gg = vi.getGraphics();79gg.setColor(Color.green);80gg.fillRect(0, 0, vi.getWidth(), vi.getHeight());81break;82}83}84g.drawImage(vi, getInsets().left, getInsets().top, null);85if (++count1 > LOOP_THRESHOLD) {86System.err.println("Infinite loop detected: count1="+count1);87failed = true;88return;89}90} while (vi.contentsLost());91}9293public static void main(String[] args) {94latch = new CountDownLatch(1);95InfiniteValidationLoopTest t1 = new InfiniteValidationLoopTest();96t1.pack();97t1.setSize(200, 200);98t1.setVisible(true);99try { latch.await(); } catch (InterruptedException ex) {}100t1.dispose();101102latch = new CountDownLatch(1);103t1 = new InfiniteValidationLoopTest();104t1.pack();105t1.setSize(50, 50);106t1.setVisible(true);107try { latch.await(); } catch (InterruptedException ex) {}108t1.dispose();109110if (failed) {111throw new112RuntimeException("Failed: infinite validattion loop detected");113}114System.out.println("Test PASSED");115}116}117118119