1 /*
   2  * Copyright (c) 2010, 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 7004134
  28  * @summary JLabel containing a ToolTipText does no longer show ToolTip after browser refresh
  29  * @author Pavel Porvatov
  30  * @modules java.desktop/sun.awt
  31  */
  32 
  33 import sun.awt.SunToolkit;
  34 
  35 import javax.swing.*;
  36 import java.awt.*;
  37 import java.awt.event.MouseEvent;
  38 import java.lang.reflect.Field;
  39 
  40 public class bug7004134 {
  41     private static volatile JFrame frame;
  42 
  43     private static volatile JLabel label;
  44 
  45     private static volatile int toolTipWidth;
  46 
  47     public static void main(String[] args) throws Exception {
  48         SwingUtilities.invokeAndWait(new Runnable() {
  49             public void run() {
  50                 label = new JLabel("A JLabel used as object for an HTML-formatted tooltip");
  51                 label.setToolTipText("<html><body bgcolor=FFFFE1>An HTML-formatted ToolTip</body></html>");
  52 
  53                 frame = new JFrame();
  54 
  55                 frame.add(label);
  56                 frame.pack();
  57                 frame.setVisible(true);
  58             }
  59         });
  60 
  61         ((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();
  62 
  63         SwingUtilities.invokeAndWait(new Runnable() {
  64             public void run() {
  65                 ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
  66 
  67                 toolTipManager.setInitialDelay(0);
  68                 toolTipManager.mouseMoved(new MouseEvent(label, 0, 0, 0, 0, 0, 0, false));
  69             }
  70         });
  71 
  72         Thread.sleep(500);
  73 
  74         SwingUtilities.invokeAndWait(new Runnable() {
  75             public void run() {
  76                 toolTipWidth = getTipWindow().getWidth();
  77 
  78                 frame.dispose();
  79             }
  80         });
  81 
  82         Thread thread = new Thread(new ThreadGroup("Some ThreadGroup"), new Runnable() {
  83             public void run() {
  84                 SunToolkit.createNewAppContext();
  85 
  86                 try {
  87                     SwingUtilities.invokeAndWait(new Runnable() {
  88                         public void run() {
  89                             frame = new JFrame();
  90 
  91                             frame.add(label);
  92                             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93                             frame.pack();
  94                             frame.setVisible(true);
  95                         }
  96                     });
  97 
  98                     ((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();
  99 
 100                     SwingUtilities.invokeAndWait(new Runnable() {
 101                         public void run() {
 102                             ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
 103 
 104                             toolTipManager.setInitialDelay(0);
 105                             toolTipManager.mouseMoved(new MouseEvent(label, 0, 0, 0, 0, 0, 0, false));
 106                         }
 107                     });
 108 
 109                     Thread.sleep(500);
 110 
 111                     SwingUtilities.invokeAndWait(new Runnable() {
 112                         public void run() {
 113                             int newToolTipWidth = getTipWindow().getWidth();
 114 
 115                             frame.dispose();
 116 
 117                             if (toolTipWidth != newToolTipWidth) {
 118                                 throw new RuntimeException("Tooltip width is different. Initial: " + toolTipWidth +
 119                                         ", new: " + newToolTipWidth);
 120                             }
 121                         }
 122                     });
 123                 } catch (Exception e) {
 124                     throw new RuntimeException(e);
 125                 }
 126 
 127             }
 128         });
 129 
 130         thread.start();
 131         thread.join();
 132     }
 133 
 134     private static Component getTipWindow() {
 135         try {
 136             Field tipWindowField = ToolTipManager.class.getDeclaredField("tipWindow");
 137 
 138             tipWindowField.setAccessible(true);
 139 
 140             Popup value = (Popup) tipWindowField.get(ToolTipManager.sharedInstance());
 141 
 142             Field componentField = Popup.class.getDeclaredField("component");
 143 
 144             componentField.setAccessible(true);
 145 
 146             return (Component) componentField.get(value);
 147         } catch (Exception e) {
 148             throw new RuntimeException("getToolTipComponent failed", e);
 149         }
 150     }
 151 }