Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JToolTip/4846413/bug4846413.java
38854 views
/*1* Copyright (c) 2012, 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/*24* @test25* @bug 484641326* @summary Checks if No tooltip modification when no KeyStroke modifier27* @library ../../regtesthelpers28* @build Util29* @author Konstantin Eremin30* @run main bug484641331*/32import java.awt.Dimension;33import java.awt.Point;34import java.awt.Robot;35import java.awt.Toolkit;36import javax.swing.*;37import java.awt.event.*;38import javax.swing.plaf.metal.MetalToolTipUI;3940public class bug4846413 {4142private static volatile boolean isTooltipAdded;43private static JButton button;4445public static void main(String[] args) throws Exception {4647Robot robot = new Robot();48robot.setAutoDelay(50);4950UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");5152javax.swing.SwingUtilities.invokeAndWait(new Runnable() {5354public void run() {55createAndShowGUI();56}57});5859robot.waitForIdle();6061Point movePoint = getButtonPoint();62robot.mouseMove(movePoint.x, movePoint.y);63robot.waitForIdle();6465long timeout = System.currentTimeMillis() + 9000;66while (!isTooltipAdded && (System.currentTimeMillis() < timeout)) {67try {Thread.sleep(500);} catch (Exception e) {}68}6970checkToolTip();71}7273private static void checkToolTip() throws Exception {74SwingUtilities.invokeAndWait(new Runnable() {7576@Override77public void run() {78JToolTip tooltip = (JToolTip) Util.findSubComponent(79JFrame.getFrames()[0], "JToolTip");8081if (tooltip == null) {82throw new RuntimeException("Tooltip has not been found!");83}8485MetalToolTipUI tooltipUI = (MetalToolTipUI) MetalToolTipUI.createUI(tooltip);86tooltipUI.installUI(tooltip);8788if (!"-Insert".equals(tooltipUI.getAcceleratorString())) {89throw new RuntimeException("Tooltip acceleration is not properly set!");90}9192}93});94}9596private static Point getButtonPoint() throws Exception {97final Point[] result = new Point[1];9899SwingUtilities.invokeAndWait(new Runnable() {100101@Override102public void run() {103Point p = button.getLocationOnScreen();104Dimension size = button.getSize();105result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);106}107});108return result[0];109}110111private static void createAndShowGUI() {112JFrame frame = new JFrame("Test");113frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);114frame.setSize(200, 200);115116button = new JButton("Press me");117button.setToolTipText("test");118button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(119KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0, true), "someCommand");120button.getActionMap().put("someCommand", null);121frame.getContentPane().add(button);122123JLayeredPane layeredPane = (JLayeredPane) Util.findSubComponent(124frame, "JLayeredPane");125layeredPane.addContainerListener(new ContainerAdapter() {126127@Override128public void componentAdded(ContainerEvent e) {129isTooltipAdded = true;130}131});132133frame.setVisible(true);134}135}136137138