jdk/test/java/awt/Focus/AppletInitialFocusTest/AppletInitialFocusTest.java
author ant
Wed, 26 Mar 2008 16:20:01 +0300
changeset 423 1c607ab74068
child 424 c090ad2f18f2
permissions -rw-r--r--
6680135: A number of test/closed/java/awt/Focus/* tests should be opened Summary: The tests moved from the closed repository. Reviewed-by: son
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
423
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     1
/*
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     4
 *
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     8
 *
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    13
 * accompanied this code).
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    14
 *
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    18
 *
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    21
 * have any questions.
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    22
 */
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    23
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    24
/*
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    25
  test
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    26
  @bug 4041703 4096228 4025223 4260929
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    27
  @summary Ensures that appletviewer sets a reasonable default focus
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    28
           for an Applet on start
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    29
  @author  das area=appletviewer
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    30
  @run shell AppletInitialFocusTest.sh
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    31
*/
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    32
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    33
import java.applet.Applet;
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    34
import java.awt.*;
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    35
import java.awt.event.*;
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    36
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    37
class MyKeyboardFocusManager extends DefaultKeyboardFocusManager {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    38
    public Window getGlobalFocusedWindow() {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    39
        return super.getGlobalFocusedWindow();
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    40
    }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    41
}
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    42
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    43
public class AppletInitialFocusTest extends Applet {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    44
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    45
    Window window;
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    46
    Button button = new Button("Button");
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    47
    MyKeyboardFocusManager manager = new MyKeyboardFocusManager();
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    48
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    49
    Object lock = new Object();
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    50
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    51
    public void init() {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    52
        KeyboardFocusManager.setCurrentKeyboardFocusManager(manager);
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    53
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    54
        Component parent = this;
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    55
        while (parent != null && !(parent instanceof Window)) {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    56
            parent = parent.getParent();
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    57
        }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    58
        /*
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    59
         * This applet is designed to be run only with appletviewer,
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    60
         * so there always should be a toplevel frame.
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    61
         */
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    62
        if (parent == null) {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    63
            synchronized (lock) {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    64
                System.err.println("appletviewer not running");
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    65
                System.exit(3);
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    66
            }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    67
        }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    68
        window = (Window)parent;
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    69
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    70
        button.addFocusListener(new FocusAdapter() {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    71
            public void focusGained(FocusEvent e) {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    72
                synchronized (lock) {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    73
                    System.err.println("passed");
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    74
                    System.exit(0);
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    75
                }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    76
            }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    77
        });
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    78
        add(button);
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    79
    }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    80
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    81
    public void start() {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    82
        Thread thread = new Thread(new Runnable() {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    83
            public void run() {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    84
                try {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    85
                    Thread.sleep(1000);
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    86
                    synchronized (lock) {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    87
                        Window focused = manager.getGlobalFocusedWindow();
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    88
                        if (window == focused) {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    89
                            System.err.println("failed");
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    90
                            System.exit(2);
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    91
                        } else {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    92
                            System.err.println("window never activated");
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    93
                            System.err.println(focused);
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    94
                            System.exit(0);
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    95
                        }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    96
                    }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    97
                } catch(InterruptedException e) {
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    98
                }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
    99
            }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
   100
        });
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
   101
        thread.start();
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
   102
    }
1c607ab74068 6680135: A number of test/closed/java/awt/Focus/* tests should be opened
ant
parents:
diff changeset
   103
}