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.Color;
  35 import java.awt.Component;
  36 import java.awt.Dialog;
  37 import java.awt.FlowLayout;
  38 import java.awt.Frame;
  39 import java.awt.Robot;
  40 import java.awt.TextField;
  41 import java.awt.event.ActionEvent;
  42 import java.awt.event.ActionListener;
  43 import java.awt.event.FocusAdapter;
  44 import java.awt.event.FocusEvent;
  45 
  46 import test.java.awt.regtesthelpers.Util;
  47 
  48 public class FocusTransitionTest {
  49     private static volatile boolean updatePending = false;
  50     private static Frame frame;
  51     private static TextField textField1;
  52     private static Button button;
  53 
  54     public static void main(String[] args) {
  55         try {
  56             Robot robot = Util.createRobot();
  57 
  58             createAndShowGUI();
  59             Util.waitForIdle(robot);
  60 
  61             for (int i = 0; i < 100; i++) {
  62                 Util.clickOnComp(button, robot);
  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         textField1.addFocusListener(new FocusAdapter() {
  82             @Override
  83             public void focusGained(FocusEvent e) {
  84                 if (updatePending) {
  85                     startUpdate();
  86                 }
  87             }
  88         });
  89 
  90         TextField textField2 = new TextField(5);
  91         TextField textField3 = new TextField(5);
  92 
  93         button = new Button("Go");
  94         button.addActionListener(new ActionListener() {
  95             @Override
  96             public void actionPerformed(ActionEvent e) {
  97                 textField1.requestFocusInWindow();
  98                 updatePending = true;
  99             }
 100         });
 101 
 102         frame.setLayout(new FlowLayout());
 103         frame.setSize(400, 200);
 104         frame.add(textField1);
 105         frame.add(textField2);
 106         frame.add(textField3);
 107         frame.add(button);
 108         frame.setVisible(true);
 109     }
 110 
 111     private static void startUpdate() {
 112         if (!updatePending) {
 113             return;
 114         }
 115         updatePending = false;
 116         UpdateThread updateThread = new UpdateThread(frame, 2);
 117         updateThread.start();
 118         Thread testThread = new TestThread(updateThread, 2, 20);
 119         testThread.start();
 120     }
 121 }
 122 
 123 class TestThread extends Thread {
 124     private UpdateThread thread;
 125     private int steps;
 126     private long delay;
 127 
 128     public TestThread(UpdateThread thread, int steps, int delay) {
 129         this.thread = thread;
 130         this.steps = steps;
 131         this.delay = delay;
 132     }
 133 
 134     public void run() {
 135         for (int i = 0; i <= steps; i++) {
 136             thread.setUpdate(i);
 137             try {
 138                 this.sleep(delay);
 139             } catch (InterruptedException ie) {
 140                 ie.printStackTrace();
 141             }
 142         }
 143     }
 144 }
 145 
 146 class UpdateThread extends Thread {
 147     private Frame frame;
 148     private int steps;
 149     private int update;
 150     private TestDialog dialog;
 151 
 152     public UpdateThread(Frame frame, int steps) {
 153         this.frame = frame;
 154         this.steps = steps;
 155     }
 156 
 157     public synchronized void setUpdate(int update) {
 158         this.update = update;
 159         this.notify();
 160     }
 161 
 162     public void run() {
 163         dialog = new TestDialog(frame);
 164         dialog.setVisible(true);
 165         synchronized (this) {
 166             while (update < steps) {
 167                 dialog.doUpdate(steps);
 168                 try {
 169                     this.wait();
 170                 } catch (InterruptedException ie) {
 171                     ie.printStackTrace();
 172                 }
 173             }
 174         }
 175         dialog.setVisible(false);
 176         dialog.dispose();
 177     }
 178 }
 179 
 180 class TestDialog extends Dialog {
 181     private static Color[] colors = new Color[]{
 182             Color.BLACK, Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN,
 183             Color.PINK, Color.CYAN, Color.MAGENTA, Color.DARK_GRAY
 184     };
 185 
 186     public TestDialog(Frame frame) {
 187         super(frame);
 188         this.setBackground(colors[4]);
 189         this.setSize(300, 100);
 190     }
 191 
 192     public void doUpdate(int i) {
 193         i %= colors.length;
 194         this.setBackground(colors[i]);
 195         this.repaint();
 196     }
 197 }