1 /*
   2  * Copyright (c) 2012, 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 7128738
  27   @summary dragged dialog freezes system on dispose
  28   @author Oleg Pekhovskiy: area=dnd
  29   @library ../../regtesthelpers
  30   @run main WindowDragTest
  31 */
  32 
  33 import java.awt.Frame;
  34 import java.awt.event.InputEvent;
  35 import java.awt.AWTException;
  36 import test.java.awt.regtesthelpers.Util;
  37 import java.awt.Robot;
  38 import java.awt.Point;
  39 import java.awt.Dimension;
  40 import java.awt.event.MouseAdapter;
  41 import java.awt.event.MouseEvent;
  42 
  43 public class WindowDragTest {
  44 
  45     static boolean passed = false;
  46 
  47     public static void main(String[] args) {
  48         Frame frame1 = new Frame();
  49         frame1.setBounds(50, 50, 300, 200);
  50         frame1.setVisible(true);
  51         frame1.toFront();
  52         frame1.addMouseListener(new MouseAdapter() {
  53             @Override
  54             public void mouseClicked(MouseEvent e) {
  55                 // Clicking frame1 succeeded - mouse is not captured
  56                 passed = true;
  57             }
  58         });
  59         Frame frame2 = new Frame();
  60         frame2.setBounds(100, 100, 300, 200);
  61         frame2.setVisible(true);
  62         frame2.toFront();
  63 
  64         try {
  65             Robot robot = new Robot();
  66 
  67             Point p = frame2.getLocationOnScreen();
  68             Dimension d = frame2.getSize();
  69 
  70             // Move cursor to frame2 title bar to drag
  71             robot.mouseMove(p.x + (int)(d.getWidth() / 2), p.y + (int)frame2.getInsets().top / 2);
  72             Util.waitForIdle(robot);
  73 
  74             // Start window dragging
  75             robot.mousePress(InputEvent.BUTTON1_MASK);
  76             Util.waitForIdle(robot);
  77 
  78             // Dispose window being dragged
  79             frame2.dispose();
  80             Util.waitForIdle(robot);
  81 
  82             // Release mouse button to be able to get MOUSE_CLICKED event on Util.clickOnComp()
  83             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  84             Util.waitForIdle(robot);
  85 
  86             // Click frame1 to check whether mouse is not captured by frame2
  87             Util.clickOnComp(frame1, robot);
  88             Util.waitForIdle(robot);
  89         }
  90         catch (AWTException e) {
  91             throw new RuntimeException("AWTException occurred - problem creating robot!");
  92         }
  93 
  94         frame1.dispose();
  95         if (passed) {
  96             System.out.println("Test passed.");
  97         }
  98         else {
  99             System.out.println("Test failed.");
 100             throw new RuntimeException("Test failed.");
 101         }
 102     }
 103 }