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