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