1 /*
   2  * Copyright (c) 2015,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  * @bug 8145060
  27  * @summary Minimizing a JInternalFrame not shifting focus to frame below it
  28  * @library ../../regtesthelpers
  29  * @build Util
  30  * @run main TestJInternalFrameMinimize
  31  */
  32 import java.awt.Point;
  33 import java.awt.Robot;
  34 import java.awt.event.ActionEvent;
  35 import java.awt.event.ActionListener;
  36 import java.awt.event.InputEvent;
  37 import java.beans.PropertyVetoException;
  38 import javax.swing.JFrame;
  39 import javax.swing.JDesktopPane;
  40 import javax.swing.JMenu;
  41 import javax.swing.JMenuBar;
  42 import javax.swing.JMenuItem;
  43 import javax.swing.JInternalFrame;
  44 import javax.swing.SwingUtilities;
  45 import javax.swing.Timer;
  46 
  47 public class TestJInternalFrameMinimize {
  48 
  49     private static JDesktopPane desktopPane;
  50     private static JFrame frame = new JFrame("Test Frame");
  51     private static int count = 0;
  52     private static JMenu menu;
  53     private static JMenuBar menuBar;
  54     private static JMenuItem menuItem;
  55     private static Robot robot;
  56     private static ActionListener listener;
  57     private static Timer timer;
  58     private static int counter;
  59     private static boolean testFailed;
  60 
  61     public static void main(String[] args) throws Exception {
  62         robot = new Robot();
  63         SwingUtilities.invokeAndWait(new Runnable() {
  64             @Override
  65             public void run() {
  66                 createUI();
  67             }
  68         });
  69         robot.waitForIdle();
  70         executeTest();
  71         if (testFailed) {
  72             throw new RuntimeException("Test Failed");
  73         }
  74         dispose();
  75     }
  76 
  77     private static void createUI() {
  78 
  79         desktopPane = new JDesktopPane();
  80         frame.getContentPane().add(desktopPane);
  81         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  82 
  83         menuBar = new JMenuBar();
  84         frame.setJMenuBar(menuBar);
  85 
  86         menu = new JMenu("File");
  87         menuBar.add(menu);
  88 
  89         menuItem = new JMenuItem("New Child");
  90         menuItem.addActionListener(
  91                 new ActionListener() {
  92                     @Override
  93                     public void actionPerformed(ActionEvent e) {
  94                         JInternalFrame f = new JInternalFrame("Child "
  95                                 + (++count), true, true, true, true);
  96                         f.setSize(200, 300);
  97                         f.setLocation(count * 20, count * 20);
  98                         desktopPane.add(f);
  99                         f.setVisible(true);
 100                     }
 101                 });
 102         menu.add(menuItem);
 103         frame.setSize(500, 500);
 104         frame.setLocationRelativeTo(null);
 105         frame.setVisible(true);
 106     }
 107 
 108     private static void executeTest() throws Exception {
 109 
 110         Point point = Util.getCenterPoint(menu);
 111         performMouseOperations(point);
 112         point = Util.getCenterPoint(menuItem);
 113         performMouseOperations(point);
 114         point = Util.getCenterPoint(menu);
 115         performMouseOperations(point);
 116         point = Util.getCenterPoint(menuItem);
 117         performMouseOperations(point);
 118         point = Util.getCenterPoint(menu);
 119         performMouseOperations(point);
 120         point = Util.getCenterPoint(menuItem);
 121         performMouseOperations(point);
 122         point = Util.getCenterPoint(menu);
 123         performMouseOperations(point);
 124         point = Util.getCenterPoint(menuItem);
 125         performMouseOperations(point);
 126         SwingUtilities.invokeAndWait(new Runnable() {
 127 
 128             @Override
 129             public void run() {
 130                 listener = new ActionListener() {
 131                     @Override
 132                     public void actionPerformed(ActionEvent ae) {
 133                         JInternalFrame internalFrame
 134                                 = desktopPane.getSelectedFrame();
 135                         if (internalFrame != null) {
 136                             try {
 137                                 internalFrame.setIcon(true);
 138                                 ++counter;
 139                             } catch (PropertyVetoException ex) {
 140                             }
 141                         }
 142                         if (counter == 4) {
 143                             try {
 144                                 timer.stop();
 145                                 JInternalFrame currentSelectedFrame
 146                                         = desktopPane.getSelectedFrame();
 147                                 if (internalFrame.equals(currentSelectedFrame)) {
 148                                     frame.dispose();
 149                                     testFailed = true;
 150                                 }
 151                             } catch (Exception ex) {
 152                             }
 153                         }
 154                     }
 155                 };
 156             }
 157         });
 158         timer = new Timer(1000, listener);
 159         timer.start();
 160         robot.delay(1000);
 161     }
 162 
 163     private static void dispose() throws Exception {
 164         SwingUtilities.invokeAndWait(new Runnable() {
 165 
 166             @Override
 167             public void run() {
 168                 frame.dispose();
 169             }
 170         });
 171     }
 172 
 173     private static void performMouseOperations(Point point) {
 174         robot.mouseMove(point.x, point.y);
 175         robot.mousePress(InputEvent.BUTTON1_MASK);
 176         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 177         robot.delay(1000);
 178         robot.waitForIdle();
 179     }
 180 }