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