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