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   @bug       6271849
  27   @summary   Tests that component in modal excluded Window which parent is blocked responses to mouse clicks.
  28   @author    anton.tarasov@sun.com: area=awt.focus
  29   @run       applet ModalExcludedWindowClickTest.html
  30 */
  31 
  32 import java.applet.Applet;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.lang.reflect.*;
  36 
  37 public class ModalExcludedWindowClickTest extends Applet {
  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         // Create instructions for the user here, as well as set up
  58         // the environment -- set the layout manager, add buttons,
  59         // etc.
  60         this.setLayout (new BorderLayout ());
  61     }
  62 
  63     public void start() {
  64 
  65         if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
  66             System.out.println("No testing on MToolkit.");
  67             return;
  68         }
  69 
  70         button.addActionListener(new ActionListener() {
  71                 public void actionPerformed(ActionEvent e) {
  72                     actionPerformed = true;
  73                     System.out.println(e.paramString());
  74                 }
  75             });
  76 
  77         EventQueue.invokeLater(new Runnable() {
  78                 public void run() {
  79                     frame.setSize(200, 200);
  80                     frame.setVisible(true);
  81 
  82                     w.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  83                     w.add(button);
  84                     w.setSize(200, 200);
  85                     w.setLocation(230, 230);
  86                     w.setVisible(true);
  87 
  88                     d.setSize(200, 200);
  89                     d.setLocation(0, 230);
  90                     d.setVisible(true);
  91 
  92                 }
  93             });
  94 
  95         waitTillShown(d);
  96 
  97         test();
  98     }
  99 
 100     void test() {
 101         clickOn(button);
 102         waitForIdle();
 103         if (!actionPerformed) {
 104             throw new RuntimeException("Test failed!");
 105         }
 106         System.out.println("Test passed.");
 107     }
 108 
 109     void clickOn(Component c) {
 110         Point p = c.getLocationOnScreen();
 111         Dimension d = c.getSize();
 112 
 113         System.out.println("Clicking " + c);
 114 
 115         if (c instanceof Frame) {
 116             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
 117         } else {
 118             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 119         }
 120         robot.mousePress(InputEvent.BUTTON1_MASK);
 121         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 122         waitForIdle();
 123     }
 124     void waitTillShown(Component c) {
 125         while (true) {
 126             try {
 127                 Thread.sleep(100);
 128                 c.getLocationOnScreen();
 129                 break;
 130             } catch (InterruptedException e) {
 131                 throw new RuntimeException(e);
 132             } catch (IllegalComponentStateException e) {}
 133         }
 134     }
 135     void waitForIdle() {
 136         try {
 137             robot.waitForIdle();
 138             EventQueue.invokeAndWait( new Runnable() {
 139                     public void run() {} // Dummy implementation
 140                 });
 141         } catch(InterruptedException ie) {
 142             System.out.println("waitForIdle, non-fatal exception caught:");
 143             ie.printStackTrace();
 144         } catch(InvocationTargetException ite) {
 145             System.out.println("waitForIdle, non-fatal exception caught:");
 146             ite.printStackTrace();
 147         }
 148 
 149         // wait longer...
 150         robot.delay(200);
 151     }
 152 }