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 /*
  25  * @test
  26  * @key headful
  27  * @bug 4769772
  28  * @summary JInternalFrame.setIcon(true) before JDesktopPane.add(JIF) causes wrong state
  29  * @run main TestJInternalFrameIconify
  30  */
  31 import java.beans.PropertyVetoException;
  32 import javax.swing.JFrame;
  33 import javax.swing.JDesktopPane;
  34 import javax.swing.JInternalFrame;
  35 import javax.swing.UIManager;
  36 import javax.swing.UnsupportedLookAndFeelException;
  37 import java.awt.Robot;
  38 import javax.swing.SwingUtilities;
  39 
  40 public class TestJInternalFrameIconify {
  41 
  42     private static JDesktopPane desktopPane;
  43     private static JFrame frame;
  44     private static Robot robot;
  45     private static volatile String errorMessage = "";
  46 
  47     public static void main(String[] args) throws Exception {
  48         robot = new java.awt.Robot();
  49         UIManager.LookAndFeelInfo[] lookAndFeelArray
  50                 = UIManager.getInstalledLookAndFeels();
  51         for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
  52             String lookAndFeelString = lookAndFeelItem.getClassName();
  53             if (tryLookAndFeel(lookAndFeelString)) {
  54                 createUI(lookAndFeelString);
  55                 robot.waitForIdle();
  56                 executeTest(lookAndFeelString);
  57             }
  58         }
  59         if (!"".equals(errorMessage)) {
  60             throw new RuntimeException(errorMessage);
  61         }
  62     }
  63 
  64     private static boolean tryLookAndFeel(String lookAndFeelString) {
  65         try {
  66             UIManager.setLookAndFeel(lookAndFeelString);
  67             return true;
  68         } catch (UnsupportedLookAndFeelException | ClassNotFoundException |
  69                 InstantiationException | IllegalAccessException e) {
  70             errorMessage += e.getMessage() + "\n";
  71             System.err.println("Caught Exception: " + e.getMessage());
  72             return false;
  73         }
  74     }
  75 
  76     private static void createUI(String lookAndFeelString) throws Exception {
  77         SwingUtilities.invokeAndWait(new Runnable() {
  78             @Override
  79             public void run() {
  80                 frame = new JFrame(lookAndFeelString);
  81                 desktopPane = new JDesktopPane();
  82                 frame.getContentPane().add(desktopPane);
  83                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  84 
  85                 JInternalFrame f = new JInternalFrame("Child ", true, true,
  86                         true, true);
  87                 f.setSize(200, 300);
  88                 f.setLocation(20, 20);
  89                 try {
  90                     f.setIcon(true);
  91                 } catch (PropertyVetoException ex) {
  92                     errorMessage += ex.getMessage() + "\n";
  93                 }
  94                 desktopPane.add(f);
  95                 f.setVisible(true);
  96 
  97                 frame.setSize(500, 500);
  98                 frame.setLocationRelativeTo(null);
  99                 frame.setVisible(true);
 100             }
 101         });
 102 
 103     }
 104 
 105     private static void executeTest(String lookAndFeelString) throws Exception {
 106         SwingUtilities.invokeAndWait(new Runnable() {
 107 
 108             @Override
 109             public void run() {
 110                 try {
 111                     JInternalFrame internalFrames[]
 112                             = desktopPane.getAllFrames();
 113                     if (internalFrames[0].isShowing()) {
 114                         errorMessage += "Test Failed for "
 115                                 + lookAndFeelString + " look and feel\n";
 116                         System.err.println(errorMessage);
 117                     }
 118                 } finally {
 119                     frame.dispose();
 120                 }
 121             }
 122         });
 123     }
 124 }