Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/DirectX/InfiniteValidationLoopTest/InfiniteValidationLoopTest.java
38854 views
/*1* Copyright (c) 2007, 2008, 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* @bug 664801826* @summary Tests that we don't run into infinite validation loop when copying27a VolatileImage to the screen28* @author [email protected]: area=Graphics29* @run main/othervm InfiniteValidationLoopTest30* @run main/othervm -Dsun.java2d.d3d=false InfiniteValidationLoopTest31*/32import java.awt.Color;33import java.awt.Frame;34import java.awt.Graphics;35import java.awt.GraphicsConfiguration;36import static java.awt.image.VolatileImage.*;37import java.awt.image.VolatileImage;38import java.util.concurrent.CountDownLatch;3940public class InfiniteValidationLoopTest extends Frame {41private static volatile boolean failed = false;42private static final int LOOP_THRESHOLD = 50;43private static volatile CountDownLatch latch;44private VolatileImage vi;4546public InfiniteValidationLoopTest() {47super("InfiniteValidationLoopTest");48}4950@Override51public void paint(Graphics g) {52try {53runTest(g);54} finally {55latch.countDown();56}57}5859private void runTest(Graphics g) {60int status = IMAGE_OK;61int count1 = 0;62do {63GraphicsConfiguration gc = getGraphicsConfiguration();64int count2 = 0;65while (vi == null || (status = vi.validate(gc)) != IMAGE_OK) {66if (++count2 > LOOP_THRESHOLD) {67System.err.println("Infinite loop detected: count2="+count2);68failed = true;69return;70}71if (vi == null || status == IMAGE_INCOMPATIBLE) {72if (vi != null) { vi.flush(); vi = null; }73vi = gc.createCompatibleVolatileImage(100, 100);74continue;75}76if (status == IMAGE_RESTORED) {77Graphics gg = vi.getGraphics();78gg.setColor(Color.green);79gg.fillRect(0, 0, vi.getWidth(), vi.getHeight());80break;81}82}83g.drawImage(vi, getInsets().left, getInsets().top, null);84if (++count1 > LOOP_THRESHOLD) {85System.err.println("Infinite loop detected: count1="+count1);86failed = true;87return;88}89} while (vi.contentsLost());90}9192public static void main(String[] args) {93latch = new CountDownLatch(1);94InfiniteValidationLoopTest t1 = new InfiniteValidationLoopTest();95t1.pack();96t1.setSize(200, 200);97t1.setVisible(true);98try { latch.await(); } catch (InterruptedException ex) {}99t1.dispose();100101latch = new CountDownLatch(1);102t1 = new InfiniteValidationLoopTest();103t1.pack();104t1.setSize(50, 50);105t1.setVisible(true);106try { latch.await(); } catch (InterruptedException ex) {}107t1.dispose();108109if (failed) {110throw new111RuntimeException("Failed: infinite validattion loop detected");112}113System.out.println("Test PASSED");114}115}116117118