1 /*
   2  * Copyright (c) 2005, 2018, 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        6271849
  28   @summary    Tests that component in modal excluded Window which parent is blocked responses to mouse clicks.
  29   @modules java.desktop/sun.awt
  30   @run        main ModalExcludedWindowClickTest
  31 */
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.lang.reflect.*;
  36 
  37 public class ModalExcludedWindowClickTest {
  38     Robot robot;
  39     Frame frame = new Frame("Frame");
  40     Window w = new Window(frame);
  41     Dialog d = new Dialog ((Dialog)null, "NullParentDialog", true);
  42     Button button = new Button("Button");
  43     boolean actionPerformed = false;
  44 
  45     public static void main (String args[]) {
  46         ModalExcludedWindowClickTest app = new ModalExcludedWindowClickTest();
  47         app.init();
  48         app.start();
  49     }
  50 
  51     public void init() {
  52         try {
  53             robot = new Robot();
  54         } catch (AWTException e) {
  55             throw new RuntimeException("Error: unable to create robot", e);
  56         }
  57     }
  58 
  59     public void start() {
  60 
  61         if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
  62             System.out.println("No testing on MToolkit.");
  63             return;
  64         }
  65 
  66         button.addActionListener(new ActionListener() {
  67                 public void actionPerformed(ActionEvent e) {
  68                     actionPerformed = true;
  69                     System.out.println(e.paramString());
  70                 }
  71             });
  72 
  73         EventQueue.invokeLater(new Runnable() {
  74                 public void run() {
  75                     frame.setSize(200, 200);
  76                     frame.setVisible(true);
  77 
  78                     w.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  79                     w.add(button);
  80                     w.setSize(200, 200);
  81                     w.setLocation(230, 230);
  82                     w.setVisible(true);
  83 
  84                     d.setSize(200, 200);
  85                     d.setLocation(0, 230);
  86                     d.setVisible(true);
  87 
  88                 }
  89             });
  90 
  91         waitTillShown(d);
  92 
  93         test();
  94     }
  95 
  96     void test() {
  97         clickOn(button);
  98         waitForIdle();
  99         if (!actionPerformed) {
 100             throw new RuntimeException("Test failed!");
 101         }
 102         System.out.println("Test passed.");
 103     }
 104 
 105     void clickOn(Component c) {
 106         Point p = c.getLocationOnScreen();
 107         Dimension d = c.getSize();
 108 
 109         System.out.println("Clicking " + c);
 110 
 111         if (c instanceof Frame) {
 112             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
 113         } else {
 114             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 115         }
 116         robot.mousePress(InputEvent.BUTTON1_MASK);
 117         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 118         waitForIdle();
 119     }
 120     void waitTillShown(Component c) {
 121         while (true) {
 122             try {
 123                 Thread.sleep(100);
 124                 c.getLocationOnScreen();
 125                 break;
 126             } catch (InterruptedException e) {
 127                 throw new RuntimeException(e);
 128             } catch (IllegalComponentStateException e) {}
 129         }
 130     }
 131     void waitForIdle() {
 132         try {
 133             robot.waitForIdle();
 134             EventQueue.invokeAndWait( new Runnable() {
 135                     public void run() {} // Dummy implementation
 136                 });
 137         } catch(InterruptedException ie) {
 138             System.out.println("waitForIdle, non-fatal exception caught:");
 139             ie.printStackTrace();
 140         } catch(InvocationTargetException ite) {
 141             System.out.println("waitForIdle, non-fatal exception caught:");
 142             ite.printStackTrace();
 143         }
 144 
 145         // wait longer...
 146         robot.delay(200);
 147     }
 148 }