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