CreateImage.java revision 12994:b66591d98c6b
1/*
2 * Copyright (c) 2015, 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
24import java.awt.AWTException;
25import java.awt.Button;
26import java.awt.Component;
27import java.awt.EventQueue;
28import java.awt.Frame;
29import java.awt.GraphicsEnvironment;
30
31import javax.swing.JButton;
32
33/**
34 * @test
35 * @bug 6815345
36 * @run main CreateImage
37 * @run main/othervm -Djava.awt.headless=true CreateImage
38 */
39public final class CreateImage {
40
41    public static void main(final String[] args) throws Exception {
42        EventQueue.invokeAndWait(CreateImage::test);
43    }
44
45    private static void test() {
46        final JButton jbutton1 = new JButton();
47        final JButton jbutton2 = new JButton();
48
49        if (GraphicsEnvironment.isHeadless()) {
50            checkCreateImage(jbutton1, true);
51            checkCreateImage(jbutton2, true);
52            return;
53        }
54
55        final Frame frame = new Frame();
56        final Button button1 = new Button();
57        final Button button2 = new Button();
58        try {
59            // all components are not displayable
60            checkCreateImage(frame, true);
61            checkCreateImage(button1, true);
62            checkCreateImage(button2, true);
63            checkCreateImage(jbutton1, true);
64            checkCreateImage(jbutton2, true);
65
66            // some components added to the non-displayable frame
67            frame.add(button1);
68            frame.add(jbutton1);
69            checkCreateImage(button1, true);
70            checkCreateImage(jbutton1, true);
71            frame.pack();
72
73            // tests previously added components when the frame is displayable
74            checkCreateImage(frame, false);
75            checkCreateImage(button1, false);
76            checkCreateImage(jbutton1, false);
77
78            // some components added to the displayable frame
79            frame.add(button2);
80            frame.add(jbutton2);
81            checkCreateImage(button2, false);
82            checkCreateImage(jbutton2, false);
83
84        } finally {
85            frame.dispose();
86        }
87        // tests all components after the frame became non-displayable again
88        checkCreateImage(frame, true);
89        checkCreateImage(button1, true);
90        checkCreateImage(button2, true);
91        checkCreateImage(jbutton1, true);
92        checkCreateImage(jbutton2, true);
93    }
94
95    private static void checkCreateImage(final Component comp,
96                                         final boolean isNull) {
97        if ((comp.createImage(10, 10) != null) == isNull) {
98            throw new RuntimeException("Image is wrong");
99        }
100        if ((comp.createVolatileImage(10, 10) != null) == isNull) {
101            throw new RuntimeException("Image is wrong");
102        }
103        try {
104            if ((comp.createVolatileImage(10, 10, null) != null) == isNull) {
105                throw new RuntimeException("Image is wrong");
106            }
107        } catch (final AWTException ignored) {
108            // this check is not applicable
109        }
110    }
111}
112