1 /*
   2  * Copyright (c) 2020, 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  * @key headful
  27  * @bug 8223788
  28  * @summary JSpinner buttons in JColorChooser dialog may capture focus
  29  *          using TAB Key
  30  * @run main JSpinnerButtonFocusTest
  31  */
  32 
  33 import javax.swing.*;
  34 import java.awt.*;
  35 import java.awt.event.KeyEvent;
  36 import javax.swing.JSpinner.DefaultEditor;
  37 
  38 public class JSpinnerButtonFocusTest {
  39     static JFrame frame;
  40     static Robot robot;
  41     static JSpinner spinner1,spinner2;
  42     static DefaultEditor editor1,editor2;
  43     static boolean JTextField_Focus_Status;
  44 
  45     public static void main(String args[]) throws Exception {
  46 
  47         for (UIManager.LookAndFeelInfo LF : UIManager.getInstalledLookAndFeels()) {
  48             try {
  49                 UIManager.setLookAndFeel(LF.getClassName());
  50                 robot = new Robot();
  51                 robot.setAutoDelay(50);
  52                 robot.setAutoWaitForIdle(true);
  53 
  54                 SwingUtilities.invokeAndWait(() -> {
  55                     frame = new JFrame();
  56                     spinner1 = new JSpinner();
  57                     spinner2 = new JSpinner();
  58 
  59                     frame.setLayout(new BorderLayout());
  60                     frame.getContentPane().add(spinner1, BorderLayout.NORTH);
  61                     frame.getContentPane().add(spinner2, BorderLayout.SOUTH);
  62 
  63                     editor1 = (DefaultEditor) spinner1.getEditor();
  64                     editor1.setFocusable(false);
  65                     spinner1.setFocusable(false);
  66 
  67                     editor2 = (DefaultEditor) spinner2.getEditor();
  68                     editor2.setFocusable(false);
  69                     spinner2.setFocusable(false);
  70 
  71                     frame.setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy());
  72                     frame.setFocusTraversalPolicyProvider(true);
  73 
  74                     frame.pack();
  75                     frame.setVisible(true);
  76                 });
  77 
  78                 robot.waitForIdle();
  79                 pressTab(5);
  80                 robot.waitForIdle();
  81 
  82                 SwingUtilities.invokeAndWait(() -> {
  83                     JTextField_Focus_Status = editor2.getTextField().isFocusOwner();
  84                 });
  85 
  86                 if (!JTextField_Focus_Status) {
  87                     throw new RuntimeException("Spinner's Text Field doesn't have focus ");
  88                 }
  89             } finally {
  90                 if(frame != null){
  91                     SwingUtilities.invokeAndWait(frame::dispose);
  92                 }
  93             }
  94         }
  95     }
  96 
  97     public static void pressTab(int n) {
  98         for (int i = 0; i < n; i++) {
  99             robot.keyPress(KeyEvent.VK_TAB);
 100             robot.keyRelease(KeyEvent.VK_TAB);
 101         }
 102     }
 103 }