1 /*
   2  * Copyright (c) 2006, 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       6382750
  27   @summary   Tests that modal dialog doesn't request extra initial focus on show.
  28   @author    anton.tarasov@sun.com: area=awt.focus
  29   @run       applet ModalDialogInitialFocusTest.html
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.applet.Applet;
  35 import java.util.concurrent.atomic.AtomicBoolean;
  36 import java.lang.reflect.InvocationTargetException;
  37 
  38 public class ModalDialogInitialFocusTest extends Applet {
  39     Robot robot;
  40 
  41     Dialog dialog = new Dialog((Window)null, "Test Dialog", Dialog.ModalityType.TOOLKIT_MODAL);
  42     Button button = new Button("button");
  43 
  44     volatile static boolean passed = true;
  45 
  46     public static void main(String[] args) {
  47         ModalDialogInitialFocusTest app = new ModalDialogInitialFocusTest();
  48         app.init();
  49         app.start();
  50     }
  51 
  52     public void init() {
  53         try {
  54             robot = new Robot();
  55         } catch (AWTException e) {
  56             throw new RuntimeException("Error: unable to create robot", e);
  57         }
  58         // Create instructions for the user here, as well as set up
  59         // the environment -- set the layout manager, add buttons,
  60         // etc.
  61         this.setLayout (new BorderLayout ());
  62     }
  63 
  64     public void start() {
  65 
  66         dialog.setLayout(new FlowLayout());
  67         dialog.add(button);
  68         dialog.setBounds(800, 0, 100, 100);
  69 
  70         dialog.addFocusListener(new FocusAdapter() {
  71                 // The only expected FOCUS_GAINED is on the button.
  72                 public void focusGained(FocusEvent e) {
  73                     passed = false;
  74                 }
  75             });
  76 
  77         test();
  78     }
  79 
  80     void test() {
  81         new Thread(new Runnable() {
  82                 public void run() {
  83                   dialog.setVisible(true);
  84                 }
  85             }).start();
  86 
  87         waitTillShown(dialog);
  88 
  89         robot.waitForIdle();
  90 
  91         dialog.dispose();
  92 
  93         if (passed) {
  94             System.out.println("Test passed.");
  95         } else {
  96             throw new RuntimeException("Test failed: dialog requests extra focus on show!");
  97         }
  98     }
  99 
 100     void waitTillShown(Component c) {
 101         while (true) {
 102             try {
 103                 Thread.sleep(100);
 104                 c.getLocationOnScreen();
 105                 break;
 106             } catch (InterruptedException ie) {
 107                 ie.printStackTrace();
 108                 break;
 109             } catch (IllegalComponentStateException e) {
 110             }
 111         }
 112     }
 113 }