1 /*
   2  * Copyright (c) 2017, 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 /* @test
  25  * @bug 8155197
  26  * @summary Tests whether the value of mostRecentFocusOwner for a window is correct, if
  27  *          another window is displayed during focus transition
  28  * @library ../../regtesthelpers
  29  * @build Util
  30  * @run main FocusTransitionTest
  31  */
  32 
  33 import java.awt.Button;
  34 import java.awt.Component;
  35 import java.awt.Dialog;
  36 import java.awt.FlowLayout;
  37 import java.awt.Frame;
  38 import java.awt.Robot;
  39 import java.awt.TextField;
  40 import java.awt.event.ActionEvent;
  41 import java.awt.event.ActionListener;
  42 
  43 import test.java.awt.regtesthelpers.Util;
  44 
  45 public class FocusTransitionTest {
  46     private static Frame frame;
  47     private static TextField textField1;
  48     private static Button button;
  49 
  50     public static void main(String[] args) throws InterruptedException {
  51         try {
  52             Robot robot = Util.createRobot();
  53 
  54             createAndShowGUI();
  55             Util.waitForIdle(robot);
  56 
  57             for (int i = 0; i < 100; i++) {
  58                 Util.clickOnComp(button, robot);
  59 
  60                 synchronized (frame) {
  61                     frame.wait();
  62                 }
  63                 Util.waitForIdle(robot);
  64 
  65                 Component focusOwner = frame.getMostRecentFocusOwner();
  66                 if (focusOwner != textField1) {
  67                     throw new RuntimeException("Test FAILED: incorrect focus owner!");
  68                 }
  69             }
  70         } finally {
  71             if (frame != null) {
  72                 frame.dispose();
  73             }
  74         }
  75     }
  76 
  77     private static void createAndShowGUI() {
  78         frame = new Frame("Test Frame");
  79 
  80         textField1 = new TextField(5);
  81 
  82         button = new Button("Go");
  83         button.addActionListener(new ActionListener() {
  84             @Override
  85             public void actionPerformed(ActionEvent e) {
  86                 textField1.requestFocusInWindow();
  87                 startUpdate();
  88             }
  89         });
  90 
  91         frame.setLayout(new FlowLayout());
  92         frame.setSize(400, 200);
  93         frame.add(textField1);
  94         frame.add(new TextField(5));
  95         frame.add(new TextField(5));
  96         frame.add(button);
  97         frame.setVisible(true);
  98     }
  99 
 100     private static void startUpdate() {
 101         UpdateThread updateThread = new UpdateThread(frame, 10);
 102         updateThread.start();
 103     }
 104 }
 105 
 106 class UpdateThread extends Thread {
 107     private final Frame frame;
 108     private final int delay;
 109 
 110     UpdateThread(Frame frame, int delay) {
 111         this.frame = frame;
 112         this.delay = delay;
 113     }
 114 
 115     @Override
 116     public void run() {
 117         TestDialog dialog = new TestDialog(frame);
 118         dialog.setVisible(true);
 119 
 120         try {
 121             sleep(delay);
 122         } catch (InterruptedException ie) {
 123             ie.printStackTrace();
 124         }
 125 
 126         dialog.setVisible(false);
 127         dialog.dispose();
 128 
 129         synchronized (frame) {
 130             frame.notify();
 131         }
 132     }
 133 }
 134 
 135 class TestDialog extends Dialog {
 136     TestDialog(Frame frame) {
 137         super(frame);
 138         setSize(300, 100);
 139     }
 140 }
 141