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