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