jdk/test/java/awt/Focus/AppletInitialFocusTest/AppletInitialFocusTest.java
changeset 423 1c607ab74068
child 424 c090ad2f18f2
equal deleted inserted replaced
421:85ea363337e6 423:1c607ab74068
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25   test
       
    26   @bug 4041703 4096228 4025223 4260929
       
    27   @summary Ensures that appletviewer sets a reasonable default focus
       
    28            for an Applet on start
       
    29   @author  das area=appletviewer
       
    30   @run shell AppletInitialFocusTest.sh
       
    31 */
       
    32 
       
    33 import java.applet.Applet;
       
    34 import java.awt.*;
       
    35 import java.awt.event.*;
       
    36 
       
    37 class MyKeyboardFocusManager extends DefaultKeyboardFocusManager {
       
    38     public Window getGlobalFocusedWindow() {
       
    39         return super.getGlobalFocusedWindow();
       
    40     }
       
    41 }
       
    42 
       
    43 public class AppletInitialFocusTest extends Applet {
       
    44 
       
    45     Window window;
       
    46     Button button = new Button("Button");
       
    47     MyKeyboardFocusManager manager = new MyKeyboardFocusManager();
       
    48 
       
    49     Object lock = new Object();
       
    50 
       
    51     public void init() {
       
    52         KeyboardFocusManager.setCurrentKeyboardFocusManager(manager);
       
    53 
       
    54         Component parent = this;
       
    55         while (parent != null && !(parent instanceof Window)) {
       
    56             parent = parent.getParent();
       
    57         }
       
    58         /*
       
    59          * This applet is designed to be run only with appletviewer,
       
    60          * so there always should be a toplevel frame.
       
    61          */
       
    62         if (parent == null) {
       
    63             synchronized (lock) {
       
    64                 System.err.println("appletviewer not running");
       
    65                 System.exit(3);
       
    66             }
       
    67         }
       
    68         window = (Window)parent;
       
    69 
       
    70         button.addFocusListener(new FocusAdapter() {
       
    71             public void focusGained(FocusEvent e) {
       
    72                 synchronized (lock) {
       
    73                     System.err.println("passed");
       
    74                     System.exit(0);
       
    75                 }
       
    76             }
       
    77         });
       
    78         add(button);
       
    79     }
       
    80 
       
    81     public void start() {
       
    82         Thread thread = new Thread(new Runnable() {
       
    83             public void run() {
       
    84                 try {
       
    85                     Thread.sleep(1000);
       
    86                     synchronized (lock) {
       
    87                         Window focused = manager.getGlobalFocusedWindow();
       
    88                         if (window == focused) {
       
    89                             System.err.println("failed");
       
    90                             System.exit(2);
       
    91                         } else {
       
    92                             System.err.println("window never activated");
       
    93                             System.err.println(focused);
       
    94                             System.exit(0);
       
    95                         }
       
    96                     }
       
    97                 } catch(InterruptedException e) {
       
    98                 }
       
    99             }
       
   100         });
       
   101         thread.start();
       
   102     }
       
   103 }