1 /*
   2  * Copyright (c) 2013, 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      8013773
  28   @summary  Tests that disabled component is not retained as most recent focus owner.
  29   @library  ../../regtesthelpers
  30   @build    Util
  31   @run      main ResetMostRecentFocusOwnerTest
  32 */
  33 
  34 import java.awt.AWTEvent;
  35 import java.awt.Robot;
  36 import java.awt.Toolkit;
  37 import java.awt.event.AWTEventListener;
  38 import java.awt.event.FocusEvent;
  39 import java.awt.event.WindowEvent;
  40 import javax.swing.JButton;
  41 import javax.swing.JFrame;
  42 import test.java.awt.regtesthelpers.Util;
  43 
  44 public class ResetMostRecentFocusOwnerTest {
  45 
  46     public static void main(String[] args) {
  47         ResetMostRecentFocusOwnerTest app = new ResetMostRecentFocusOwnerTest();
  48         app.start();
  49     }
  50 
  51     public void start() {
  52 
  53         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  54             public void eventDispatched(AWTEvent e) {
  55                 System.err.println(e);
  56             }
  57         }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
  58 
  59         boolean gained = false;
  60         final Robot robot = Util.createRobot();
  61 
  62         JFrame frame1 = new JFrame("Main Frame");
  63         final JButton b1 = new JButton("button1");
  64         frame1.add(b1);
  65         frame1.pack();
  66         frame1.setLocation(100, 300);
  67 
  68         Util.showWindowWait(frame1);
  69 
  70         final JFrame frame2 = new JFrame("Test Frame");
  71         final JButton b2 = new JButton("button2");
  72         frame2.add(b2);
  73         frame2.pack();
  74         frame2.setLocation(300, 300);
  75 
  76         b2.setEnabled(false);
  77         b2.requestFocus();
  78 
  79         Util.showWindowWait(frame2);
  80 
  81         robot.delay(500);
  82 
  83         //
  84         // It's expeced that the focus is restored to <button1>.
  85         // If not, click <button1> to set focus on it.
  86         //
  87         if (!b1.hasFocus()) {
  88             gained = Util.trackFocusGained(b1, new Runnable() {
  89                 public void run() {
  90                     Util.clickOnComp(b1, robot);
  91                 }
  92             }, 5000, false);
  93 
  94             if (!gained) {
  95                 throw new RuntimeException("Unexpected state: focus is not on <button1>");
  96             }
  97         }
  98 
  99         robot.delay(500);
 100 
 101         //
 102         // Click <button2>, check that focus is set on the parent frame.
 103         //
 104         gained = false;
 105         gained = Util.trackFocusGained(frame2, new Runnable() {
 106             public void run() {
 107                 Util.clickOnComp(b2, robot);
 108             }
 109         }, 5000, false);
 110 
 111         if (!gained) {
 112             throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
 113         }
 114 
 115         System.out.println("Test passed.");
 116     }
 117 }