1 /*
   2  * Copyright (c) 2016, 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       8139218
  28   @summary   Dialog that opens and closes quickly changes focus in original
  29              focusowner
  30   @run       main RollbackFocusFromAnotherWindowTest
  31  */
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 
  36 import javax.swing.*;
  37 
  38 public class RollbackFocusFromAnotherWindowTest extends JFrame
  39                                                           implements KeyListener
  40 {
  41     private static RollbackFocusFromAnotherWindowTest tfs;
  42     private static Robot robot;
  43 
  44     public static void main(String[] args) throws Exception {
  45         robot = new Robot();
  46 
  47         SwingUtilities.invokeAndWait(() -> {
  48             tfs = new RollbackFocusFromAnotherWindowTest();
  49             tfs.setVisible(true);
  50         });
  51 
  52         robot.waitForIdle();
  53         robot.delay(200);
  54 
  55         try {
  56             for (int i = 0; i < 10; i++) {
  57                 robot.keyPress(KeyEvent.VK_A);
  58                 robot.delay(10);
  59                 robot.keyRelease(KeyEvent.VK_A);
  60                 robot.waitForIdle();
  61                 robot.delay(200);
  62                 SwingUtilities.invokeAndWait(() -> {
  63                     String name = tfs.getFocusOwner().getName();
  64                     System.out.println(name);
  65                     if (!"Comp0".equals(name)) {
  66                         throw new RuntimeException(
  67                                 "Focus is not restored correctly");
  68                     }
  69                 });
  70             }
  71             System.out.println("ok");
  72         } finally {
  73             SwingUtilities.invokeLater(() -> tfs.dispose());
  74         }
  75     }
  76 
  77     public RollbackFocusFromAnotherWindowTest()
  78     {
  79         setUndecorated(true);
  80         Container c = getContentPane();
  81         c.setLayout(new FlowLayout());
  82         for (int i = 0; i < 10; i++)
  83         {
  84             JTextField tf = new JTextField("" + i, 10);
  85             tf.setName("Comp" + i);
  86             c.add(tf);
  87             tf.addKeyListener(this);
  88         }
  89         pack();
  90     }
  91 
  92     @Override
  93     public void keyTyped(KeyEvent e) {
  94 
  95     }
  96 
  97     @Override
  98     public void keyPressed(KeyEvent e) {
  99         Frame frame = new Frame();
 100         frame.setVisible(true);
 101         try {
 102             Thread.sleep(2);
 103         } catch (InterruptedException e1) {
 104             e1.printStackTrace();
 105         }
 106         frame.dispose();
 107     }
 108 
 109     @Override
 110     public void keyReleased(KeyEvent e) {
 111 
 112     }
 113 }