Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JLabel/7004134/bug7004134.java
38854 views
/*1* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 700413425@summary JLabel containing a ToolTipText does no longer show ToolTip after browser refresh26@author Pavel Porvatov27*/2829import sun.awt.SunToolkit;3031import javax.swing.*;32import java.awt.*;33import java.awt.event.MouseEvent;34import java.lang.reflect.Field;3536public class bug7004134 {37private static volatile JFrame frame;3839private static volatile JLabel label;4041private static volatile int toolTipWidth;4243public static void main(String[] args) throws Exception {44SwingUtilities.invokeAndWait(new Runnable() {45public void run() {46label = new JLabel("A JLabel used as object for an HTML-formatted tooltip");47label.setToolTipText("<html><body bgcolor=FFFFE1>An HTML-formatted ToolTip</body></html>");4849frame = new JFrame();5051frame.add(label);52frame.pack();53frame.setVisible(true);54}55});5657((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();5859SwingUtilities.invokeAndWait(new Runnable() {60public void run() {61ToolTipManager toolTipManager = ToolTipManager.sharedInstance();6263toolTipManager.setInitialDelay(0);64toolTipManager.mouseMoved(new MouseEvent(label, 0, 0, 0, 0, 0, 0, false));65}66});6768Thread.sleep(500);6970SwingUtilities.invokeAndWait(new Runnable() {71public void run() {72toolTipWidth = getTipWindow().getWidth();7374frame.dispose();75}76});7778Thread thread = new Thread(new ThreadGroup("Some ThreadGroup"), new Runnable() {79public void run() {80SunToolkit.createNewAppContext();8182try {83SwingUtilities.invokeAndWait(new Runnable() {84public void run() {85frame = new JFrame();8687frame.add(label);88frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);89frame.pack();90frame.setVisible(true);91}92});9394((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();9596SwingUtilities.invokeAndWait(new Runnable() {97public void run() {98ToolTipManager toolTipManager = ToolTipManager.sharedInstance();99100toolTipManager.setInitialDelay(0);101toolTipManager.mouseMoved(new MouseEvent(label, 0, 0, 0, 0, 0, 0, false));102}103});104105Thread.sleep(500);106107SwingUtilities.invokeAndWait(new Runnable() {108public void run() {109int newToolTipWidth = getTipWindow().getWidth();110111frame.dispose();112113if (toolTipWidth != newToolTipWidth) {114throw new RuntimeException("Tooltip width is different. Initial: " + toolTipWidth +115", new: " + newToolTipWidth);116}117}118});119} catch (Exception e) {120throw new RuntimeException(e);121}122123}124});125126thread.start();127thread.join();128}129130private static Component getTipWindow() {131try {132Field tipWindowField = ToolTipManager.class.getDeclaredField("tipWindow");133134tipWindowField.setAccessible(true);135136Popup value = (Popup) tipWindowField.get(ToolTipManager.sharedInstance());137138Field componentField = Popup.class.getDeclaredField("component");139140componentField.setAccessible(true);141142return (Component) componentField.get(value);143} catch (Exception e) {144throw new RuntimeException("getToolTipComponent failed", e);145}146}147}148149150