1 /*
   2  * Copyright (c) 2007, 2011, 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 6256140
  28  * @summary Esc key doesn't restore old value in JFormattedtextField when ToolTip is set
  29  * @author Alexander Potochkin
  30  * @run main Test6256140
  31  */
  32 
  33 import sun.awt.SunToolkit;
  34 
  35 import javax.swing.*;
  36 import java.awt.*;
  37 import java.awt.event.KeyEvent;
  38 
  39 public class Test6256140 {
  40 
  41     private static volatile JFormattedTextField ft;
  42 
  43     private final static String initialText = "value";
  44     private final static JLabel toolTipLabel = new JLabel("tip");
  45 
  46     public static void main(String[] args) throws Exception {
  47 
  48         Robot robot = new Robot();
  49         robot.setAutoDelay(10);
  50         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  51 
  52         SwingUtilities.invokeAndWait(new Runnable() {
  53             public void run() {
  54                 createAndShowGUI();
  55             }
  56         });
  57         toolkit.realSync();
  58 
  59         Point point = ft.getLocationOnScreen();
  60         robot.mouseMove(point.x, point.y);
  61         robot.mouseMove(point.x + 3, point.y + 3);
  62 
  63         robot.keyPress(KeyEvent.VK_A);
  64         robot.keyRelease(KeyEvent.VK_A);
  65         toolkit.realSync();
  66 
  67         if (!isTooltipShowning()) {
  68             throw new RuntimeException("Tooltip is not shown");
  69         }
  70 
  71         robot.keyPress(KeyEvent.VK_ESCAPE);
  72         robot.keyRelease(KeyEvent.VK_ESCAPE);
  73         toolkit.realSync();
  74 
  75         if (isTooltipShowning()) {
  76             throw new RuntimeException("Tooltip must be hidden now");
  77         }
  78 
  79         if (isTextEqual()) {
  80             throw new RuntimeException("FormattedTextField must *not* cancel the updated value this time");
  81         }
  82 
  83         robot.keyPress(KeyEvent.VK_ESCAPE);
  84         robot.keyRelease(KeyEvent.VK_ESCAPE);
  85         toolkit.realSync();
  86 
  87         if (!isTextEqual()) {
  88             throw new RuntimeException("FormattedTextField must cancel the updated value");
  89         }
  90     }
  91 
  92     private static boolean isTooltipShowning() throws Exception {
  93         final boolean[] result = new boolean[1];
  94 
  95         SwingUtilities.invokeAndWait(new Runnable() {
  96             @Override
  97             public void run() {
  98                 result[0] = toolTipLabel.isShowing();
  99             }
 100         });
 101 
 102         return result[0];
 103     }
 104 
 105     private static boolean isTextEqual() throws Exception {
 106         final boolean[] result = new boolean[1];
 107 
 108         SwingUtilities.invokeAndWait(new Runnable() {
 109             @Override
 110             public void run() {
 111                 result[0] = initialText.equals(ft.getText());
 112             }
 113         });
 114 
 115         return result[0];
 116     }
 117 
 118     private static void createAndShowGUI() {
 119         ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);
 120         ToolTipManager.sharedInstance().setInitialDelay(0);
 121 
 122         final JFrame frame = new JFrame();
 123         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 124         frame.setLayout(new FlowLayout());
 125 
 126         ft = new JFormattedTextField() {
 127 
 128             public JToolTip createToolTip() {
 129                 JToolTip toolTip = super.createToolTip();
 130                 toolTip.setLayout(new BorderLayout());
 131                 toolTip.add(toolTipLabel);
 132                 return toolTip;
 133             }
 134         };
 135         ft.setToolTipText("   ");
 136         ft.setValue(initialText);
 137         frame.add(ft);
 138 
 139         frame.pack();
 140         frame.setLocationRelativeTo(null);
 141         frame.setVisible(true);
 142         ft.requestFocus();
 143     }
 144 }