21 * have any questions. |
21 * have any questions. |
22 */ |
22 */ |
23 |
23 |
24 /* |
24 /* |
25 test |
25 test |
26 @bug 4041703 4096228 4025223 4260929 |
26 @bug 4041703 4096228 4025223 4260929 |
27 @summary Ensures that appletviewer sets a reasonable default focus |
27 @summary Ensures that appletviewer sets a reasonable default focus for an Applet on start |
28 for an Applet on start |
|
29 @author das area=appletviewer |
28 @author das area=appletviewer |
30 @run shell AppletInitialFocusTest.sh |
29 @run applet AppletInitialFocusTest.html |
31 */ |
30 */ |
32 |
31 |
33 import java.applet.Applet; |
32 import java.applet.Applet; |
34 import java.awt.*; |
33 import java.awt.Button; |
35 import java.awt.event.*; |
34 import java.awt.Component; |
36 |
35 import java.awt.Robot; |
37 class MyKeyboardFocusManager extends DefaultKeyboardFocusManager { |
36 import java.awt.Window; |
38 public Window getGlobalFocusedWindow() { |
37 import test.java.awt.regtesthelpers.Util; |
39 return super.getGlobalFocusedWindow(); |
|
40 } |
|
41 } |
|
42 |
38 |
43 public class AppletInitialFocusTest extends Applet { |
39 public class AppletInitialFocusTest extends Applet { |
44 |
40 Robot robot = Util.createRobot(); |
45 Window window; |
|
46 Button button = new Button("Button"); |
41 Button button = new Button("Button"); |
47 MyKeyboardFocusManager manager = new MyKeyboardFocusManager(); |
|
48 |
|
49 Object lock = new Object(); |
|
50 |
42 |
51 public void init() { |
43 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); |
44 add(button); |
79 } |
45 } |
80 |
46 |
81 public void start() { |
47 public void start() { |
82 Thread thread = new Thread(new Runnable() { |
48 new Thread(new Runnable() { |
83 public void run() { |
49 public void run() { |
84 try { |
50 Util.waitTillShown(button); |
85 Thread.sleep(1000); |
51 robot.delay(1000); // delay the thread to let EDT to start dispatching focus events |
86 synchronized (lock) { |
52 Util.waitForIdle(robot); |
87 Window focused = manager.getGlobalFocusedWindow(); |
53 if (!button.hasFocus()) { |
88 if (window == focused) { |
54 throw new RuntimeException("Appletviewer doesn't set default focus correctly."); |
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 } |
55 } |
97 } catch(InterruptedException e) { |
|
98 } |
56 } |
99 } |
57 }).start(); |
100 }); |
|
101 thread.start(); |
|
102 } |
58 } |
103 } |
59 } |