1 /*
   2  * Copyright (c) 2009, 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 6411406
  28   @summary Components automatically transfer focus on removal, even if developer requests focus elsewhere first
  29   @author oleg.sukhodolsky, anton.tarasov: area=awt.focus
  30   @library   ../../regtesthelpers
  31   @build Util
  32   @run main RemoveAfterRequest
  33 */
  34 
  35 /**
  36  * RemoveAfterRequest.java
  37  *
  38  * summary: Components automatically transfer focus on removal, even if developer requests focus elsewhere first
  39  */
  40 
  41 import java.awt.*;
  42 import java.awt.event.*;
  43 import test.java.awt.regtesthelpers.Util;
  44 
  45 public class RemoveAfterRequest {
  46     final static Frame frame = new Frame("test frame");
  47     final static Button btn1 = new Button("btn1");
  48     final static Button btn2 = new Button("btn2");
  49     final static Button btn3 = new Button("btn3");
  50 
  51     public static void main(String[] args) {
  52         frame.setLayout(new GridLayout(3, 1));
  53         frame.add(btn1);
  54         frame.add(btn2);
  55         frame.add(btn3);
  56         frame.pack();
  57         frame.setVisible(true);
  58 
  59         Util.waitForIdle(null);
  60 
  61         if (!btn1.hasFocus()) {
  62             btn1.requestFocus();
  63             Util.waitForIdle(null);
  64             if (!btn1.hasFocus()) {
  65                 throw new TestErrorException("couldn't focus " + btn1);
  66             }
  67         }
  68 
  69         if (!Util.trackFocusGained(btn3, new Runnable() {
  70                 public void run() {
  71                     btn3.requestFocus();
  72                     frame.remove(btn1);
  73                     frame.invalidate();
  74                                 frame.validate();
  75                                 frame.repaint();
  76                 }
  77             }, 2000, true))
  78         {
  79             throw new TestFailedException("focus request on removal failed");
  80         }
  81 
  82         System.out.println("Test passed.");
  83     }
  84 }
  85 
  86 /**
  87  * Thrown when the behavior being verified is found wrong.
  88  */
  89 class TestFailedException extends RuntimeException {
  90     TestFailedException(String msg) {
  91         super("Test failed: " + msg);
  92     }
  93 }
  94 
  95 /**
  96  * Thrown when an error not related to the behavior being verified is encountered.
  97  */
  98 class TestErrorException extends RuntimeException {
  99     TestErrorException(String msg) {
 100         super("Unexpected error: " + msg);
 101     }
 102 }
 103