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