InfiniteValidationLoopTest.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2007, 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 6648018
28 * @summary Tests that we don't run into infinite validation loop when copying
29            a VolatileImage to the screen
30 * @author Dmitri.Trembovetski@sun.com: area=Graphics
31 * @run main/othervm InfiniteValidationLoopTest
32 * @run main/othervm -Dsun.java2d.d3d=false InfiniteValidationLoopTest
33 */
34import java.awt.Color;
35import java.awt.Frame;
36import java.awt.Graphics;
37import java.awt.GraphicsConfiguration;
38import static java.awt.image.VolatileImage.*;
39import java.awt.image.VolatileImage;
40import java.util.concurrent.CountDownLatch;
41
42public class InfiniteValidationLoopTest extends Frame {
43    private static volatile boolean failed = false;
44    private static final int LOOP_THRESHOLD = 50;
45    private static volatile CountDownLatch latch;
46    private VolatileImage vi;
47
48    public InfiniteValidationLoopTest() {
49        super("InfiniteValidationLoopTest");
50    }
51
52    @Override
53    public void paint(Graphics g) {
54        try {
55            runTest(g);
56        } finally {
57            latch.countDown();
58        }
59    }
60
61    private void runTest(Graphics g) {
62        int status = IMAGE_OK;
63        int count1 = 0;
64        do {
65            GraphicsConfiguration gc = getGraphicsConfiguration();
66            int count2 = 0;
67            while (vi == null || (status = vi.validate(gc)) != IMAGE_OK) {
68                if (++count2 > LOOP_THRESHOLD) {
69                    System.err.println("Infinite loop detected: count2="+count2);
70                    failed = true;
71                    return;
72                }
73                if (vi == null || status == IMAGE_INCOMPATIBLE) {
74                    if (vi != null) { vi.flush(); vi = null; }
75                    vi = gc.createCompatibleVolatileImage(100, 100);
76                    continue;
77                }
78                if (status == IMAGE_RESTORED) {
79                    Graphics gg = vi.getGraphics();
80                    gg.setColor(Color.green);
81                    gg.fillRect(0, 0, vi.getWidth(), vi.getHeight());
82                    break;
83                }
84            }
85            g.drawImage(vi, getInsets().left, getInsets().top, null);
86            if (++count1 > LOOP_THRESHOLD) {
87                System.err.println("Infinite loop detected: count1="+count1);
88                failed = true;
89                return;
90            }
91        } while (vi.contentsLost());
92    }
93
94    public static void main(String[] args) {
95        latch = new CountDownLatch(1);
96        InfiniteValidationLoopTest t1 = new InfiniteValidationLoopTest();
97        t1.pack();
98        t1.setSize(200, 200);
99        t1.setVisible(true);
100        try { latch.await(); } catch (InterruptedException ex) {}
101        t1.dispose();
102
103        latch = new CountDownLatch(1);
104        t1 = new InfiniteValidationLoopTest();
105        t1.pack();
106        t1.setSize(50, 50);
107        t1.setVisible(true);
108        try { latch.await(); } catch (InterruptedException ex) {}
109        t1.dispose();
110
111        if (failed) {
112            throw new
113                RuntimeException("Failed: infinite validattion loop detected");
114        }
115        System.out.println("Test PASSED");
116    }
117}
118