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  @bug 6211389
  27  @summary Verify MostRecentFocusOwner when Window Gained Focus.
  28  @run main MostRecentFocusOwnerTest
  29  */
  30 import java.awt.Component;
  31 import java.awt.Frame;
  32 import java.awt.Robot;
  33 import java.awt.TextArea;
  34 import java.awt.TextField;
  35 import java.awt.event.WindowAdapter;
  36 import java.awt.event.WindowEvent;
  37 
  38 public class MostRecentFocusOwnerTest {
  39 
  40     private static Component focusComponent = null;
  41     private static Frame frame;
  42     private static TextArea textArea;
  43     private static TextField textField;
  44 
  45     private static void CreateAndShowUI() {
  46 
  47         frame = new Frame("Test");
  48         frame.setLayout(new java.awt.FlowLayout());
  49         textArea = new TextArea("TextArea", 5, 10);
  50         textField = new TextField("TextField", 10);
  51 
  52         frame.add(textArea);
  53         frame.add(textField);
  54         frame.pack();
  55 
  56         frame.addWindowFocusListener(new WindowAdapter() {
  57             public void windowGainedFocus(WindowEvent e) {
  58                 focusComponent = frame.getMostRecentFocusOwner();
  59             }
  60 
  61         });
  62         frame.setVisible(true);
  63 
  64     }
  65 
  66     public static void main(String[] args) throws Exception {
  67 
  68         CreateAndShowUI();
  69 
  70         Robot robot = new Robot();
  71         frame.setExtendedState(Frame.ICONIFIED);
  72         robot.waitForIdle();
  73         frame.setExtendedState(Frame.NORMAL);
  74         robot.waitForIdle();
  75         if (focusComponent != textArea) {
  76             throw new RuntimeException("MostRecentFocusOwner is Null"
  77                     + " When window GainedFocus");
  78         }
  79 
  80         robot.waitForIdle();
  81         textField.requestFocus();
  82         frame.setExtendedState(Frame.ICONIFIED);
  83         robot.waitForIdle();
  84         frame.setExtendedState(Frame.NORMAL);
  85         robot.waitForIdle();
  86         if (focusComponent != textField) {
  87             throw new RuntimeException("MostRecentFocusOwner is Null"
  88                     + " When window GainedFocus");
  89         }
  90 
  91         frame.dispose();
  92 
  93     }
  94 
  95 }