test/java/awt/Mouse/EnterExitEvents/ResizingFrameTest.java

Print this page




  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 7154048
  27  * @summary Programmatically resized  window does not receive mouse entered/exited events
  28  * @author  alexandr.scherbatiy area=awt.event
  29  * @run main ResizingFrameTest
  30  */
  31 /*
  32  * To change this template, choose Tools | Templates
  33  * and open the template in the editor.
  34  */
  35 
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 import javax.swing.*;
  39 import sun.awt.SunToolkit;
  40 
  41 public class ResizingFrameTest {
  42 
  43     private static volatile int mouseEnteredCount = 0;
  44     private static volatile int mouseExitedCount = 0;
  45     private static JFrame frame;
  46 
  47     public static void main(String[] args) throws Exception {
  48 
  49         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  50         Robot robot = new Robot();
  51         robot.setAutoDelay(50);
  52         robot.mouseMove(100, 100);

  53 
  54         // create a frame under the mouse cursor
  55         SwingUtilities.invokeAndWait(new Runnable() {
  56 
  57             @Override
  58             public void run() {
  59                 createAndShowGUI();
  60             }
  61         });
  62 
  63 
  64         toolkit.realSync();

  65 
  66         if (mouseEnteredCount != 1 || mouseExitedCount != 0) {
  67             throw new RuntimeException("No Mouse Entered/Exited events!");
  68         }
  69 
  70         // iconify frame
  71         SwingUtilities.invokeAndWait(new Runnable() {
  72 
  73             @Override
  74             public void run() {
  75                 frame.setExtendedState(Frame.ICONIFIED);
  76             }
  77         });
  78 
  79         toolkit.realSync();
  80         robot.delay(200);
  81 
  82         if (mouseEnteredCount != 1 || mouseExitedCount != 1) {
  83             throw new RuntimeException("No Mouse Entered/Exited events!");
  84         }
  85 
  86         // deiconify frame
  87         SwingUtilities.invokeAndWait(new Runnable() {
  88 
  89             @Override
  90             public void run() {
  91                 frame.setExtendedState(Frame.NORMAL);
  92             }
  93         });
  94 
  95         toolkit.realSync();
  96         robot.delay(200);
  97 
  98         if (mouseEnteredCount != 2 || mouseExitedCount != 1) {
  99             throw new RuntimeException("No Mouse Entered/Exited events!");
 100         }
 101 
 102         // move the mouse out of the frame
 103         robot.mouseMove(500, 500);
 104         toolkit.realSync();
 105         robot.delay(200);
 106 
 107         if (mouseEnteredCount != 2 || mouseExitedCount != 2) {
 108             throw new RuntimeException("No Mouse Entered/Exited events!");
 109         }
 110 
 111         // maximize the frame
 112         SwingUtilities.invokeAndWait(new Runnable() {
 113 
 114             @Override
 115             public void run() {
 116                 frame.setExtendedState(Frame.MAXIMIZED_BOTH);
 117             }
 118         });
 119 
 120         toolkit.realSync();
 121         robot.delay(200);
 122 
 123         if (mouseEnteredCount != 3 || mouseExitedCount != 2) {
 124             throw new RuntimeException("No Mouse Entered/Exited events!");
 125         }
 126 
 127 
 128         // demaximize the frame
 129         SwingUtilities.invokeAndWait(new Runnable() {
 130 
 131             @Override
 132             public void run() {
 133                 frame.setExtendedState(Frame.NORMAL);
 134             }
 135         });
 136 
 137         toolkit.realSync();
 138         robot.delay(200);
 139 
 140         if (mouseEnteredCount != 3 || mouseExitedCount != 3) {
 141             throw new RuntimeException("No Mouse Entered/Exited events!");
 142 
 143         }
 144 
 145         // move the frame under the mouse
 146         SwingUtilities.invokeAndWait(new Runnable() {
 147 
 148             @Override
 149             public void run() {
 150                 frame.setLocation(400, 400);
 151             }
 152         });
 153 
 154         toolkit.realSync();
 155         robot.delay(200);
 156 
 157         if (mouseEnteredCount != 4 || mouseExitedCount != 3) {
 158             throw new RuntimeException("No Mouse Entered/Exited events!");
 159         }
 160 
 161         // move the frame out of the mouse
 162         SwingUtilities.invokeAndWait(new Runnable() {
 163 
 164             @Override
 165             public void run() {
 166                 frame.setLocation(100, 100);
 167             }
 168         });
 169 
 170         toolkit.realSync();
 171         robot.delay(400);
 172 
 173         if (mouseEnteredCount != 4 || mouseExitedCount != 4) {
 174             throw new RuntimeException("No Mouse Entered/Exited events!");
 175         }
 176 
 177         // enlarge the frame bounds
 178         SwingUtilities.invokeAndWait(new Runnable() {
 179 
 180             @Override
 181             public void run() {
 182                 frame.setBounds(100, 100, 800, 800);
 183             }
 184         });
 185 
 186         toolkit.realSync();
 187         robot.delay(200);
 188 
 189         if (mouseEnteredCount != 5 || mouseExitedCount != 4) {
 190             throw new RuntimeException("No Mouse Entered/Exited events!");
 191         }
 192 
 193         // make the frame bounds smaller
 194         SwingUtilities.invokeAndWait(new Runnable() {
 195 
 196             @Override
 197             public void run() {
 198                 frame.setBounds(100, 100, 200, 300);
 199             }
 200         });
 201 
 202         toolkit.realSync();
 203         robot.delay(400);
 204 
 205 
 206         if (mouseEnteredCount != 5 || mouseExitedCount != 5) {
 207             throw new RuntimeException("No Mouse Entered/Exited events!");
 208         }
 209     }
 210 
 211     private static void createAndShowGUI() {
 212 
 213         frame = new JFrame("Main Frame");
 214         frame.setSize(300, 200);
 215         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 216 
 217         frame.addMouseListener(new MouseAdapter() {
 218 
 219             @Override
 220             public void mouseEntered(MouseEvent e) {
 221                 mouseEnteredCount++;
 222             }


  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 7154048
  27  * @summary Programmatically resized  window does not receive mouse entered/exited events
  28  * @author  alexandr.scherbatiy area=awt.event
  29  * @run main ResizingFrameTest
  30  */




  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import javax.swing.*;

  35 
  36 public class ResizingFrameTest {
  37 
  38     private static volatile int mouseEnteredCount = 0;
  39     private static volatile int mouseExitedCount = 0;
  40     private static JFrame frame;
  41 
  42     public static void main(String[] args) throws Exception {
  43 

  44         Robot robot = new Robot();
  45         robot.setAutoDelay(50);
  46         robot.mouseMove(100, 100);
  47         robot.delay(200);
  48 
  49         // create a frame under the mouse cursor
  50         SwingUtilities.invokeAndWait(new Runnable() {
  51 
  52             @Override
  53             public void run() {
  54                 createAndShowGUI();
  55             }
  56         });
  57 
  58 
  59         robot.waitForIdle();
  60         robot.delay(1000);
  61 
  62         if (mouseEnteredCount != 1 || mouseExitedCount != 0) {
  63             throw new RuntimeException("No Mouse Entered/Exited events!");
  64         }
  65 
  66         // iconify frame
  67         SwingUtilities.invokeAndWait(new Runnable() {
  68 
  69             @Override
  70             public void run() {
  71                 frame.setExtendedState(Frame.ICONIFIED);
  72             }
  73         });
  74 
  75         robot.waitForIdle();
  76         robot.delay(1000);
  77 
  78         if (mouseEnteredCount != 1 || mouseExitedCount != 1) {
  79             throw new RuntimeException("No Mouse Entered/Exited events! "+mouseEnteredCount+", "+mouseExitedCount);
  80         }
  81 
  82         // deiconify frame
  83         SwingUtilities.invokeAndWait(new Runnable() {
  84 
  85             @Override
  86             public void run() {
  87                 frame.setExtendedState(Frame.NORMAL);
  88             }
  89         });
  90 
  91         robot.waitForIdle();
  92         robot.delay(1000);
  93 
  94         if (mouseEnteredCount != 2 || mouseExitedCount != 1) {
  95             throw new RuntimeException("No Mouse Entered/Exited events!");
  96         }
  97 
  98         // move the mouse out of the frame
  99         robot.mouseMove(500, 500);
 100         robot.waitForIdle();
 101         robot.delay(1000);
 102 
 103         if (mouseEnteredCount != 2 || mouseExitedCount != 2) {
 104             throw new RuntimeException("No Mouse Entered/Exited events!");
 105         }
 106 
 107         // maximize the frame
 108         SwingUtilities.invokeAndWait(new Runnable() {
 109 
 110             @Override
 111             public void run() {
 112                 frame.setExtendedState(Frame.MAXIMIZED_BOTH);
 113             }
 114         });
 115 
 116         robot.waitForIdle();
 117         robot.delay(1000);
 118 
 119         if (mouseEnteredCount != 3 || mouseExitedCount != 2) {
 120             throw new RuntimeException("No Mouse Entered/Exited events!");
 121         }
 122 
 123 
 124         // demaximize the frame
 125         SwingUtilities.invokeAndWait(new Runnable() {
 126 
 127             @Override
 128             public void run() {
 129                 frame.setExtendedState(Frame.NORMAL);
 130             }
 131         });
 132 
 133         robot.waitForIdle();
 134         robot.delay(1000);
 135 
 136         if (mouseEnteredCount != 3 || mouseExitedCount != 3) {
 137             throw new RuntimeException("No Mouse Entered/Exited events!");
 138 
 139         }
 140 
 141         // move the frame under the mouse
 142         SwingUtilities.invokeAndWait(new Runnable() {
 143 
 144             @Override
 145             public void run() {
 146                 frame.setLocation(400, 400);
 147             }
 148         });
 149 
 150         robot.waitForIdle();
 151         robot.delay(1000);
 152 
 153         if (mouseEnteredCount != 4 || mouseExitedCount != 3) {
 154             throw new RuntimeException("No Mouse Entered/Exited events!");
 155         }
 156 
 157         // move the frame out of the mouse
 158         SwingUtilities.invokeAndWait(new Runnable() {
 159 
 160             @Override
 161             public void run() {
 162                 frame.setLocation(100, 100);
 163             }
 164         });
 165 
 166         robot.waitForIdle();
 167         robot.delay(400);
 168 
 169         if (mouseEnteredCount != 4 || mouseExitedCount != 4) {
 170             throw new RuntimeException("No Mouse Entered/Exited events!");
 171         }
 172 
 173         // enlarge the frame bounds
 174         SwingUtilities.invokeAndWait(new Runnable() {
 175 
 176             @Override
 177             public void run() {
 178                 frame.setBounds(100, 100, 800, 800);
 179             }
 180         });
 181 
 182         robot.waitForIdle();
 183         robot.delay(200);
 184 
 185         if (mouseEnteredCount != 5 || mouseExitedCount != 4) {
 186             throw new RuntimeException("No Mouse Entered/Exited events!");
 187         }
 188 
 189         // make the frame bounds smaller
 190         SwingUtilities.invokeAndWait(new Runnable() {
 191 
 192             @Override
 193             public void run() {
 194                 frame.setBounds(100, 100, 200, 300);
 195             }
 196         });
 197 
 198         robot.waitForIdle();
 199         robot.delay(400);
 200 
 201 
 202         if (mouseEnteredCount != 5 || mouseExitedCount != 5) {
 203             throw new RuntimeException("No Mouse Entered/Exited events!");
 204         }
 205     }
 206 
 207     private static void createAndShowGUI() {
 208 
 209         frame = new JFrame("Main Frame");
 210         frame.setSize(300, 200);
 211         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 212 
 213         frame.addMouseListener(new MouseAdapter() {
 214 
 215             @Override
 216             public void mouseEntered(MouseEvent e) {
 217                 mouseEnteredCount++;
 218             }