1 /*
   2  * Copyright (c) 2008, 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 /* @test
  25  * @bug 6647340
  26  * @summary Checks that iconified internal frame follows
  27  *          the main frame borders properly.
  28  * @author Mikhail Lapshin
  29  * @library ../../../../lib/testlibrary/
  30  * @build ExtendedRobot
  31  * @run main bug6647340
  32  */
  33 
  34 import javax.swing.*;
  35 import java.awt.*;
  36 import java.beans.PropertyVetoException;
  37 
  38 public class bug6647340 {
  39     private JFrame frame;
  40     private Point location;
  41     private JInternalFrame jif;
  42     private static ExtendedRobot robot = createRobot();
  43 
  44     public static void main(String[] args) throws Exception {
  45         final bug6647340 test = new bug6647340();
  46         try {
  47             SwingUtilities.invokeAndWait(new Runnable() {
  48                 public void run() {
  49                     test.setupUI();
  50                 }
  51             });
  52             test.test();
  53         } finally {
  54             if (test.frame != null) {
  55                 test.frame.dispose();
  56             }
  57         }
  58     }
  59 
  60     private void setupUI() {
  61         frame = new JFrame();
  62         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  63 
  64         JDesktopPane desktop = new JDesktopPane();
  65         frame.add(desktop);
  66 
  67         jif = new JInternalFrame("Internal Frame", true, true, true, true);
  68         jif.setBounds(20, 20, 200, 100);
  69         desktop.add(jif);
  70         jif.setVisible(true);
  71 
  72         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  73         frame.setBounds((screen.width - 400) / 2, (screen.height - 400) / 2, 400, 400);
  74         frame.setLocationRelativeTo(null);
  75         frame.setVisible(true);
  76     }
  77 
  78     private void test() throws Exception {
  79         sync();
  80         test1();
  81         sync();
  82         check1();
  83         sync();
  84         test2();
  85         sync();
  86         check2();
  87     }
  88 
  89     private void test1() throws Exception {
  90         SwingUtilities.invokeAndWait(new Runnable() {
  91             public void run() {
  92                 setIcon(true);
  93                 location = jif.getDesktopIcon().getLocation();
  94                 Dimension size = frame.getSize();
  95                 frame.setSize(size.width + 100, size.height + 100);
  96             }
  97         });
  98     }
  99 
 100     private void test2() throws Exception {
 101         SwingUtilities.invokeAndWait(new Runnable() {
 102             public void run() {
 103                 setIcon(false);
 104             }
 105         });
 106         sync();
 107         SwingUtilities.invokeAndWait(new Runnable() {
 108             public void run() {
 109                 Dimension size = frame.getSize();
 110                 frame.setSize(size.width - 100, size.height - 100);
 111             }
 112         });
 113         sync();
 114         SwingUtilities.invokeAndWait(new Runnable() {
 115             public void run() {
 116                 setIcon(true);
 117             }
 118         });
 119     }
 120 
 121     private void check1() {
 122         if (!jif.getDesktopIcon().getLocation().equals(location)) {
 123             System.out.println("First test passed");
 124         } else {
 125             throw new RuntimeException("Icon isn't shifted with the frame bounds");
 126         }
 127     }
 128 
 129     private void check2() {
 130         if (jif.getDesktopIcon().getLocation().equals(location)) {
 131             System.out.println("Second test passed");
 132         } else {
 133             throw new RuntimeException("Icon isn't located near the frame bottom");
 134         }
 135     }
 136 
 137     private static void sync() {
 138         robot.waitForIdle();
 139     }
 140     private static ExtendedRobot createRobot() {
 141         try {
 142              ExtendedRobot robot = new ExtendedRobot();
 143              return robot;
 144          }catch(Exception ex) {
 145              ex.printStackTrace();
 146              throw new Error("Unexpected Failure");
 147          }
 148     }
 149 
 150     private void setIcon(boolean b) {
 151         try {
 152             jif.setIcon(b);
 153         } catch (PropertyVetoException e) {
 154             e.printStackTrace();
 155         }
 156     }
 157 }