7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application.
authorant
Thu, 17 May 2012 21:48:57 +0400
changeset 12661 6cf8b7116579
parent 12660 76c54631149e
child 12662 184d8d3b7fff
7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application. Summary: forward port from 7u4 Reviewed-by: art
jdk/src/share/classes/java/awt/Dialog.java
jdk/src/share/classes/java/awt/Frame.java
jdk/src/share/classes/java/awt/Window.java
jdk/src/share/classes/javax/swing/JApplet.java
jdk/src/share/classes/javax/swing/JDialog.java
jdk/src/share/classes/javax/swing/JFrame.java
jdk/src/share/classes/javax/swing/JInternalFrame.java
jdk/src/share/classes/javax/swing/JWindow.java
jdk/src/share/classes/javax/swing/UIManager.java
jdk/src/share/classes/sun/awt/SunToolkit.java
jdk/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP.java
jdk/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP_AWT.java
jdk/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP_Swing.java
jdk/test/java/awt/event/KeyEvent/SwallowKeyEvents/SwallowKeyEvents.java
--- a/jdk/src/share/classes/java/awt/Dialog.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/java/awt/Dialog.java	Thu May 17 21:48:57 2012 +0400
@@ -670,7 +670,7 @@
 
         this.title = title;
         setModalityType(modalityType);
-        SunToolkit.checkAndSetPolicy(this, false);
+        SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
@@ -721,7 +721,7 @@
 
         this.title = title;
         setModalityType(modalityType);
-        SunToolkit.checkAndSetPolicy(this, false);
+        SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/jdk/src/share/classes/java/awt/Frame.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/java/awt/Frame.java	Thu May 17 21:48:57 2012 +0400
@@ -449,7 +449,7 @@
 
     private void init(String title, GraphicsConfiguration gc) {
         this.title = title;
-        SunToolkit.checkAndSetPolicy(this, false);
+        SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/jdk/src/share/classes/java/awt/Window.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/java/awt/Window.java	Thu May 17 21:48:57 2012 +0400
@@ -506,7 +506,7 @@
 
         modalExclusionType = Dialog.ModalExclusionType.NO_EXCLUDE;
 
-        SunToolkit.checkAndSetPolicy(this, false);
+        SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/jdk/src/share/classes/javax/swing/JApplet.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/javax/swing/JApplet.java	Thu May 17 21:48:57 2012 +0400
@@ -149,7 +149,7 @@
         setRootPaneCheckingEnabled(true);
 
         setFocusTraversalPolicyProvider(true);
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
 
         enableEvents(AWTEvent.KEY_EVENT_MASK);
     }
--- a/jdk/src/share/classes/javax/swing/JDialog.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/javax/swing/JDialog.java	Thu May 17 21:48:57 2012 +0400
@@ -654,7 +654,7 @@
                 getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
             }
         }
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/jdk/src/share/classes/javax/swing/JFrame.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/javax/swing/JFrame.java	Thu May 17 21:48:57 2012 +0400
@@ -266,7 +266,7 @@
                 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
             }
         }
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/jdk/src/share/classes/javax/swing/JInternalFrame.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/javax/swing/JInternalFrame.java	Thu May 17 21:48:57 2012 +0400
@@ -349,7 +349,7 @@
         setRootPaneCheckingEnabled(true);
         desktopIcon = new JDesktopIcon(this);
         updateUI();
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
         addPropertyChangeListenerIfNecessary();
     }
 
--- a/jdk/src/share/classes/javax/swing/JWindow.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/javax/swing/JWindow.java	Thu May 17 21:48:57 2012 +0400
@@ -264,7 +264,7 @@
         setLocale( JComponent.getDefaultLocale() );
         setRootPane(createRootPane());
         setRootPaneCheckingEnabled(true);
-        sun.awt.SunToolkit.checkAndSetPolicy(this, true);
+        sun.awt.SunToolkit.checkAndSetPolicy(this);
     }
 
     /**
--- a/jdk/src/share/classes/javax/swing/UIManager.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/javax/swing/UIManager.java	Thu May 17 21:48:57 2012 +0400
@@ -191,6 +191,7 @@
         private UIDefaults[] tables = new UIDefaults[2];
 
         boolean initialized = false;
+        boolean focusPolicyInitialized = false;
         MultiUIDefaults multiUIDefaults = new MultiUIDefaults(tables);
         LookAndFeel lookAndFeel;
         LookAndFeel multiLookAndFeel = null;
@@ -1000,6 +1001,7 @@
      */
     public static ComponentUI getUI(JComponent target) {
         maybeInitialize();
+        maybeInitializeFocusPolicy(target);
         ComponentUI ui = null;
         LookAndFeel multiLAF = getLAFState().multiLookAndFeel;
         if (multiLAF != null) {
@@ -1422,6 +1424,27 @@
         }
     }
 
+    /*
+     * Sets default swing focus traversal policy.
+     */
+    private static void maybeInitializeFocusPolicy(JComponent comp) {
+        // Check for JRootPane which indicates that a swing toplevel
+        // is coming, in which case a swing default focus policy
+        // should be instatiated. See 7125044.
+        if (comp instanceof JRootPane) {
+            synchronized (classLock) {
+                if (!getLAFState().focusPolicyInitialized) {
+                    getLAFState().focusPolicyInitialized = true;
+
+                    if (FocusManager.isFocusManagerEnabled()) {
+                        KeyboardFocusManager.getCurrentKeyboardFocusManager().
+                            setDefaultFocusTraversalPolicy(
+                                new LayoutFocusTraversalPolicy());
+                    }
+                }
+            }
+        }
+    }
 
     /*
      * Only called by maybeInitialize().
@@ -1433,17 +1456,6 @@
         initializeAuxiliaryLAFs(swingProps);
         initializeInstalledLAFs(swingProps);
 
-        // Enable the Swing default LayoutManager.
-        String toolkitName = Toolkit.getDefaultToolkit().getClass().getName();
-        // don't set default policy if this is XAWT.
-        if (!"sun.awt.X11.XToolkit".equals(toolkitName)) {
-            if (FocusManager.isFocusManagerEnabled()) {
-                KeyboardFocusManager.getCurrentKeyboardFocusManager().
-                    setDefaultFocusTraversalPolicy(
-                        new LayoutFocusTraversalPolicy());
-            }
-        }
-
         // Install Swing's PaintEventDispatcher
         if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
             sun.awt.PaintEventDispatcher.setPaintEventDispatcher(
--- a/jdk/src/share/classes/sun/awt/SunToolkit.java	Thu May 17 21:31:55 2012 +0400
+++ b/jdk/src/share/classes/sun/awt/SunToolkit.java	Thu May 17 21:48:57 2012 +0400
@@ -461,48 +461,11 @@
         AWTAccessor.getWindowAccessor().setLWRequestStatus(changed, status);
     };
 
-    public static void checkAndSetPolicy(Container cont, boolean isSwingCont)
-    {
-        FocusTraversalPolicy defaultPolicy = KeyboardFocusManager
-            .getCurrentKeyboardFocusManager().getDefaultFocusTraversalPolicy();
-
-        String toolkitName = Toolkit.getDefaultToolkit().getClass().getName();
-        // if this is not XAWT then use default policy
-        // because Swing change it
-        if (!"sun.awt.X11.XToolkit".equals(toolkitName)) {
-            cont.setFocusTraversalPolicy(defaultPolicy);
-            return;
-        }
-
-        String policyName = defaultPolicy.getClass().getName();
+    public static void checkAndSetPolicy(Container cont) {
+        FocusTraversalPolicy defaultPolicy = KeyboardFocusManager.
+            getCurrentKeyboardFocusManager().
+                getDefaultFocusTraversalPolicy();
 
-        if (DefaultFocusTraversalPolicy.class != defaultPolicy.getClass()) {
-            // Policy was changed
-            // Check if it is awt policy or swing policy
-            // If it is Swing policy we shouldn't use it in AWT frames
-            // If it is AWT policy  we shouldn't use it in Swing frames
-            // Otherwise we should use this policy
-            if (policyName.startsWith("java.awt.")) {
-                // AWT
-                if (isSwingCont) {
-                    // Can't use AWT policy in Swing windows - should use Swing's one.
-                    defaultPolicy = createLayoutPolicy();
-                } else {
-                    // New awt policy.
-                }
-            } else if (policyName.startsWith("javax.swing.")) {
-                if (isSwingCont) {
-                    // New Swing's policy
-                } else {
-                    defaultPolicy = new DefaultFocusTraversalPolicy();
-                }
-            }
-        } else {
-            // Policy is default, use different default policy for swing
-            if (isSwingCont) {
-                defaultPolicy = createLayoutPolicy();
-            }
-        }
         cont.setFocusTraversalPolicy(defaultPolicy);
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP.java	Thu May 17 21:48:57 2012 +0400
@@ -0,0 +1,26 @@
+/*
+  @test
+  @bug       7125044
+  @summary   Tests defaut focus traversal policy in AWT & Swing toplevel windows.
+  @author    anton.tarasov@sun.com: area=awt.focus
+  @run       main InitialFTP_AWT
+  @run       main InitialFTP_Swing
+*/
+
+import java.awt.FocusTraversalPolicy;
+import java.awt.Window;
+
+public class InitialFTP {
+    public static void test(Window win, Class<? extends FocusTraversalPolicy> expectedPolicy) {
+        FocusTraversalPolicy ftp = win.getFocusTraversalPolicy();
+
+        System.out.println("==============" + "\n" +
+                           "Tested window:    " + win + "\n" +
+                           "Expected policy:  " + expectedPolicy + "\n" +
+                           "Effective policy: " + ftp.getClass());
+
+        if (!expectedPolicy.equals(ftp.getClass())) {
+            throw new RuntimeException("Test failed: wrong effective focus policy");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP_AWT.java	Thu May 17 21:48:57 2012 +0400
@@ -0,0 +1,50 @@
+/*
+  @bug       7125044
+  @summary   Tests default focus traversal policy in AWT toplevel windows.
+  @author    anton.tarasov@sun.com: area=awt.focus
+*/
+
+import java.awt.Button;
+import java.awt.DefaultFocusTraversalPolicy;
+import java.awt.FlowLayout;
+import java.awt.FocusTraversalPolicy;
+import java.awt.Frame;
+import java.awt.List;
+import java.awt.TextArea;
+import java.awt.Window;
+
+public class InitialFTP_AWT {
+    public static void main(String[] args) {
+        AWTFrame f0 = new AWTFrame("frame0");
+        f0.setVisible(true);
+
+        InitialFTP.test(f0, DefaultFocusTraversalPolicy.class);
+
+        AWTFrame f1 = new AWTFrame("frame1");
+        f1.setVisible(true);
+
+        InitialFTP.test(f1, DefaultFocusTraversalPolicy.class);
+
+        System.out.println("Test passed.");
+    }
+}
+
+class AWTFrame extends Frame {
+    Button button = new Button("button");
+    TextArea text = new TextArea("qwerty");
+    List list = new List();
+
+    public AWTFrame(String title) {
+        super(title);
+
+        list.add("one");
+        list.add("two");
+        list.add("three");
+
+        this.setLayout(new FlowLayout());
+        this.add(button);
+        this.add(text);
+        this.add(list);
+        this.pack();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Focus/FocusTraversalPolicy/InitialFTP_Swing.java	Thu May 17 21:48:57 2012 +0400
@@ -0,0 +1,46 @@
+/*
+  @bug       7125044
+  @summary   Tests default focus traversal policy in Swing toplevel windows.
+  @author    anton.tarasov@sun.com: area=awt.focus
+*/
+
+import java.awt.FlowLayout;
+import java.awt.FocusTraversalPolicy;
+import java.awt.Window;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JList;
+import javax.swing.JTextArea;
+import javax.swing.LayoutFocusTraversalPolicy;
+
+public class InitialFTP_Swing {
+    public static void main(String[] args) {
+        SwingFrame f0 = new SwingFrame("frame0");
+        f0.setVisible(true);
+
+        InitialFTP.test(f0, LayoutFocusTraversalPolicy.class);
+
+        SwingFrame f1 = new SwingFrame("frame1");
+        f1.setVisible(true);
+
+        InitialFTP.test(f1, LayoutFocusTraversalPolicy.class);
+
+        System.out.println("Test passed.");
+    }
+}
+
+class SwingFrame extends JFrame {
+    JButton button = new JButton("button");
+    JTextArea text = new JTextArea("qwerty");
+    JList list = new JList(new String[] {"one", "two", "three"});
+
+    public SwingFrame(String title) {
+        super(title);
+
+        this.setLayout(new FlowLayout());
+        this.add(button);
+        this.add(text);
+        this.add(list);
+        this.pack();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/event/KeyEvent/SwallowKeyEvents/SwallowKeyEvents.java	Thu May 17 21:48:57 2012 +0400
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+  @test
+  @bug       7154072
+  @summary   Tests that key events with modifiers are not swallowed.
+  @author    anton.tarasov: area=awt.focus
+  @library   ../../../regtesthelpers
+  @build     Util
+  @run       main SwallowKeyEvents
+*/
+
+import java.awt.AWTException;
+import java.awt.Frame;
+import java.awt.Robot;
+import java.awt.TextField;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import test.java.awt.regtesthelpers.Util;
+
+public class SwallowKeyEvents {
+    static final int PRESS_COUNT = 10;
+
+    static int keyPressedCount = 0;
+
+    static Frame f = new Frame("Frame");
+    static TextField t = new TextField("text");
+    static Robot r;
+
+    public static void main(String[] args) {
+        f.add(t);
+        f.pack();
+        f.setVisible(true);
+
+        t.requestFocus();
+
+        try {
+            r = new Robot();
+        } catch (AWTException ex) {
+            throw new RuntimeException(ex);
+        }
+
+        Util.waitForIdle(r);
+
+        t.addKeyListener(new KeyAdapter() {
+            public void keyPressed(KeyEvent ke) {
+                System.out.println(ke);
+                if (ke.getKeyCode() == KeyEvent.VK_M) {
+                    keyPressedCount++;
+                }
+            }
+        });
+
+        test();
+
+        System.out.println("key_pressed count: " + keyPressedCount);
+
+        if (keyPressedCount != PRESS_COUNT) {
+            throw new RuntimeException("Test failed!");
+        } else {
+            System.out.println("Test passed.");
+        }
+    }
+
+    public static void test() {
+        r.keyPress(KeyEvent.VK_SHIFT);
+        r.keyPress(KeyEvent.VK_META);
+
+        for (int i=0; i<PRESS_COUNT; i++) {
+            r.delay(100);
+            r.keyPress(KeyEvent.VK_M);
+            r.delay(100);
+            r.keyRelease(KeyEvent.VK_M);
+        }
+
+        r.keyRelease(KeyEvent.VK_META);
+        r.keyRelease(KeyEvent.VK_SHIFT);
+    }
+}