1 /*
   2  * Copyright (c) 2006, 2016, 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   @key headful
  27   @bug 6386592 8160766
  28   @summary Tests that disposing a dialog doesn't activate its invisible owner.
  29 */
  30 
  31 import java.awt.AWTException;
  32 import java.awt.Button;
  33 import java.awt.Component;
  34 import java.awt.Dialog;
  35 import java.awt.Dimension;
  36 import java.awt.Frame;
  37 import java.awt.Point;
  38 import java.awt.Robot;
  39 import java.awt.event.FocusAdapter;
  40 import java.awt.event.FocusEvent;
  41 import java.awt.event.InputEvent;
  42 
  43 public class DisposeDialogNotActivateOwnerTest {
  44     Robot robot;
  45     Frame frame;
  46     Frame dialogInvisibleOwner;
  47     Dialog dialog;
  48     Button frameButton;
  49     static boolean buttonReceivedFocus = false;
  50 
  51     public static void main(String[] args) {
  52         DisposeDialogNotActivateOwnerTest test =
  53                 new DisposeDialogNotActivateOwnerTest();
  54         test.performTest();
  55         test.dispose();
  56     }
  57 
  58     public DisposeDialogNotActivateOwnerTest() {
  59         try {
  60             robot = new Robot();
  61         } catch (AWTException e) {
  62             throw new RuntimeException("Error: unable to create robot");
  63         }
  64 
  65         dialogInvisibleOwner = new Frame("Dialog Invisible Owner Frame");
  66         dialog = new Dialog(dialogInvisibleOwner, "Owned Dialog");
  67 
  68         frame = new Frame("A Frame");
  69         frameButton = new Button("button");
  70         frameButton.addFocusListener(new FocusAdapter() {
  71             public void focusGained(FocusEvent e) {
  72                 buttonReceivedFocus = true;
  73             }
  74         });
  75         frame.setBounds(0, 0, 400, 200);
  76         frame.add(frameButton);
  77         dialog.setBounds(100, 50, 200, 100);
  78     }
  79 
  80     public void performTest() {
  81         frame.setVisible(true);
  82         robot.waitForIdle();
  83         clickOnTitle(frame);
  84         robot.waitForIdle();
  85         robot.delay(200);
  86         if (!frame.isFocused()) {
  87             dispose();
  88             throw new RuntimeException("Error: frame didn't get initial focus");
  89         }
  90 
  91         dialog.setVisible(true);
  92         robot.waitForIdle();
  93         robot.delay(200);
  94         if (!dialog.isFocused()) {
  95             dispose();
  96             throw new RuntimeException("Error: dialog didn't get initial focus");
  97         }
  98 
  99         dialog.dispose();
 100         robot.waitForIdle();
 101         robot.delay(200);
 102         if (!buttonReceivedFocus) {
 103             dispose();
 104             throw new RuntimeException(
 105                 "Test failed: Dialog activates invisible owner when disposed!");
 106         }
 107     }
 108 
 109     public void dispose() {
 110         frame.dispose();
 111         dialog.dispose();
 112         dialogInvisibleOwner.dispose();
 113     }
 114 
 115     void clickOnTitle(Component c) {
 116         Point p = c.getLocationOnScreen();
 117         Dimension d = c.getSize();
 118         robot.mouseMove(p.x + (int)(d.getWidth() / 2),
 119                         p.y + ((Frame)c).getInsets().top / 2);
 120         robot.mousePress(InputEvent.BUTTON1_MASK);
 121         robot.delay(20);
 122         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 123     }
 124 }