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=awt.toplevel
  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         try {
  49             Robot robot = new Robot();
  50             robot.setAutoDelay(1000);
  51 
  52             Frame frame1 = new Frame();
  53             frame1.setBounds(50, 50, 300, 200);
  54             frame1.setVisible(true);
  55             frame1.toFront();
  56             frame1.addMouseListener(new MouseAdapter() {
  57                 @Override
  58                 public void mouseClicked(MouseEvent e) {
  59                     // Clicking frame1 succeeded - mouse is not captured
  60                     passed = true;
  61                 }
  62             });
  63             robot.delay(1000);
  64 
  65             Frame frame2 = new Frame();
  66             frame2.setBounds(100, 100, 300, 200);
  67             frame2.setVisible(true);
  68             frame2.toFront();
  69             robot.delay(1000);
  70 
  71             Point p = frame2.getLocationOnScreen();
  72             Dimension d = frame2.getSize();
  73 
  74             // Move cursor to frame2 title bar to drag
  75             robot.mouseMove(p.x + (int)(d.getWidth() / 2), p.y + (int)frame2.getInsets().top / 2);
  76             Util.waitForIdle(robot);
  77 
  78             // Start window dragging
  79             robot.mousePress(InputEvent.BUTTON1_MASK);
  80             Util.waitForIdle(robot);
  81 
  82             // Dispose window being dragged
  83             frame2.dispose();
  84             Util.waitForIdle(robot);
  85 
  86             // Release mouse button to be able to get MOUSE_CLICKED event on Util.clickOnComp()
  87             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  88             Util.waitForIdle(robot);
  89 
  90             // Click frame1 to check whether mouse is not captured by frame2
  91             Util.clickOnComp(frame1, robot);
  92             Util.waitForIdle(robot);
  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         catch (AWTException e) {
 104             throw new RuntimeException("AWTException occurred - problem creating robot!");
 105         }
 106     }
 107 }