1 /*
   2  * Copyright (c) 2019, 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 * @bug 8153090 8223788 8224149
  27 * @summary 8153090: TAB key cannot change input focus after the radio button in
  28 *                   the Color Selection dialog.
  29 *          8223788: JSpinner buttons in JColorChooser dialog may capture focus
  30 *                   using TAB Key.
  31 *          8224149: JColorChoser doesn't update the color diagram when the
  32 *                   selection is changed by keys.
  33 *
  34 * @run main JColorChooserContainerFocus
  35 */
  36 
  37 import javax.swing.*;
  38 import java.awt.*;
  39 import java.awt.event.KeyEvent;
  40 
  41 public class JColorChooserContainerFocus {
  42     static JFrame frame;
  43     static JColorChooser cc;
  44     static Robot robot;
  45     static Color color;
  46     static Point point;
  47     static Color diagram;
  48 
  49     public static void main(String args[]) throws Exception {
  50 
  51         if( !(UIManager.getLookAndFeel().getName().equals("Metal") ||
  52               UIManager.getLookAndFeel().getName().equals("Mac OS X"))) {
  53             return;
  54         }
  55 
  56         SwingUtilities.invokeAndWait(() -> {
  57             frame = new JFrame();
  58             cc = new JColorChooser(Color.WHITE);
  59             frame.getContentPane().add(cc);
  60             frame.pack();
  61             frame.setVisible(true);
  62         });
  63 
  64         robot = new Robot();
  65         robot.setAutoWaitForIdle(true);
  66         robot.setAutoDelay(50);
  67 
  68         robot.waitForIdle();
  69         robot.delay(200);
  70 
  71         if (UIManager.getLookAndFeel().isNativeLookAndFeel()) {
  72             robot.keyPress(KeyEvent.VK_SHIFT);
  73             robot.keyPress(KeyEvent.VK_TAB);
  74             robot.keyRelease(KeyEvent.VK_TAB);
  75             robot.keyRelease(KeyEvent.VK_SHIFT);
  76         }
  77 
  78         robot.keyPress(KeyEvent.VK_RIGHT);
  79         robot.keyRelease(KeyEvent.VK_RIGHT);
  80 
  81         robot.keyPress(KeyEvent.VK_TAB);
  82         robot.keyRelease(KeyEvent.VK_TAB);
  83 
  84         robot.waitForIdle();
  85         robot.delay(200);
  86 
  87         SwingUtilities.invokeAndWait(() -> {
  88             point = frame.getLocationOnScreen();
  89             point.translate(100, 100);
  90             diagram = robot.getPixelColor(point.x, point.y);
  91         });
  92 
  93         robot.keyPress(KeyEvent.VK_DOWN);
  94         robot.keyRelease(KeyEvent.VK_DOWN);
  95 
  96         robot.waitForIdle();
  97         robot.delay(200);
  98 
  99         SwingUtilities.invokeAndWait(() -> {
 100             if (diagram.equals(robot.getPixelColor(point.x, point.y))) {
 101                 diagram = null;
 102             }
 103         });
 104 
 105         if (diagram == null) {
 106             throw new AssertionError("Diagram was not updated.");
 107         }
 108 
 109         for (int i = 0; i < 6; i++) {
 110             robot.keyPress(KeyEvent.VK_TAB);
 111             robot.keyRelease(KeyEvent.VK_TAB);
 112         }
 113 
 114         robot.keyPress(KeyEvent.VK_1);
 115         robot.keyRelease(KeyEvent.VK_1);
 116 
 117         robot.keyPress(KeyEvent.VK_0);
 118         robot.keyRelease(KeyEvent.VK_0);
 119 
 120         robot.keyPress(KeyEvent.VK_0);
 121         robot.keyRelease(KeyEvent.VK_0);
 122 
 123         robot.keyPress(KeyEvent.VK_TAB);
 124         robot.keyRelease(KeyEvent.VK_TAB);
 125 
 126         SwingUtilities.invokeAndWait(() -> color =
 127                                      cc.getSelectionModel().getSelectedColor());
 128 
 129         SwingUtilities.invokeLater(frame::dispose);
 130 
 131         if (!Color.RED.equals(color)) {
 132             throw new AssertionError("Test failed: the color should be red.");
 133         }
 134 
 135     }
 136 }