1 /*
   2  * Copyright (c) 2007, 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       6562853 6562853 6562853
  27   @summary   Tests that focus can not be set to removed component.
  28   @author    Oleg Sukhodolsky: area=awt.focus
  29   @library   ../../regtesthelpers
  30   @build     Util
  31   @run       main RequestFocusAndHideTest
  32 */
  33 
  34 import java.awt.Button;
  35 import java.awt.Component;
  36 import java.awt.EventQueue;
  37 import java.awt.FlowLayout;
  38 import java.awt.Frame;
  39 import java.awt.KeyboardFocusManager;
  40 import java.awt.Robot;
  41 
  42 import test.java.awt.regtesthelpers.Util;
  43 
  44 public class RequestFocusAndHideTest {
  45     public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
  46     {
  47         final Frame frame = new Frame("the test");
  48         frame.setLayout(new FlowLayout());
  49         final Button btn1 = new Button("button 1");
  50         frame.add(btn1);
  51         frame.add(new Button("button 2"));
  52         frame.add(new Button("button 3"));
  53         frame.pack();
  54         frame.setVisible(true);
  55 
  56         Robot r = Util.createRobot();
  57         Util.waitForIdle(r);
  58         Util.clickOnComp(btn1, r);
  59         Util.waitForIdle(r);
  60         KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
  61         if (kfm.getFocusOwner() != btn1) {
  62             throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
  63         }
  64 
  65         EventQueue.invokeAndWait(new Runnable() {
  66                 public void run() {
  67                     final int n_comps = frame.getComponentCount();
  68                     for (int i = 0; i < n_comps; ++i) {
  69                         frame.getComponent(i).setVisible(false);
  70                     }
  71                 }
  72             });
  73         Util.waitForIdle(r);
  74         final Component focus_owner = kfm.getFocusOwner();
  75 
  76         if (focus_owner != null && !focus_owner.isVisible()) {
  77             throw new RuntimeException("we have invisible focus owner");
  78         }
  79         System.out.println("test passed");
  80         frame.dispose();
  81     }
  82 }