1 /*
   2  * Copyright (c) 2013, 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      8013611
  27   @summary  Tests showing a modal dialog with requesting focus in frame.
  28   @author   Anton.Tarasov: area=awt.focus
  29   @library  ../../regtesthelpers
  30   @build    Util
  31   @run      main JDK8013611
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import javax.swing.*;
  37 import test.java.awt.regtesthelpers.Util;
  38 
  39 import java.awt.*;
  40 
  41 public class JDK8013611 extends JFrame {
  42     static JTextField textField = new JTextField("text");
  43     static JButton button1 = new JButton("button1");
  44     static JButton button2 = new JButton("button2");
  45     static Robot robot;
  46 
  47     static JDialog dialog;
  48     static JButton button3 = new JButton("button3");
  49 
  50     public static void main(String[] args) {
  51         robot = Util.createRobot();
  52 
  53         JDK8013611 frame = new JDK8013611();
  54         frame.setLayout(new FlowLayout());
  55         frame.add(textField);
  56         frame.add(button1);
  57         frame.add(button2);
  58         frame.pack();
  59 
  60         dialog = new JDialog(frame, true);
  61         dialog.add(button3);
  62         dialog.pack();
  63 
  64         textField.addFocusListener(new FocusAdapter() {
  65             @Override
  66             public void focusLost(FocusEvent e) {
  67                 dialog.setVisible(true);
  68             }
  69         });
  70 
  71         button1.addFocusListener(new FocusAdapter() {
  72             @Override
  73             public void focusGained(FocusEvent e) {
  74                 button2.requestFocusInWindow();
  75             }
  76         });
  77 
  78         frame.setVisible(true);
  79 
  80         frame.test();
  81     }
  82 
  83     public void test() {
  84         if (!testFocused(textField)) {
  85             Util.clickOnComp(textField, robot);
  86             if (!testFocused(textField)) {
  87                 throw new RuntimeException("Error: couldn't focus " + textField);
  88             }
  89         }
  90 
  91         robot.keyPress(KeyEvent.VK_TAB);
  92         robot.delay(50);
  93         robot.keyRelease(KeyEvent.VK_TAB);
  94 
  95         if (!testFocused(button3)) {
  96             throw new RuntimeException("Test failed: dialog didn't get focus!");
  97         }
  98 
  99         System.out.println("Test passed.");
 100     }
 101 
 102     boolean testFocused(Component c) {
 103         for (int i=0; i<10; i++) {
 104             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) {
 105                 return true;
 106             }
 107             Util.waitForIdle(robot);
 108         }
 109         return false;
 110     }
 111 }