1 /*
  2  * Copyright (c) 2002, 2023, 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 4768127
 27  * @summary ToolTipManager not removed from components
 28  * @key headful
 29  */
 30 
 31 import java.awt.Point;
 32 import java.awt.Rectangle;
 33 import java.awt.Robot;
 34 import java.awt.event.InputEvent;
 35 import java.awt.event.MouseMotionListener;
 36 import javax.swing.JDesktopPane;
 37 import javax.swing.JFrame;
 38 import javax.swing.JInternalFrame;
 39 import javax.swing.JLabel;
 40 import javax.swing.SwingUtilities;
 41 import javax.swing.ToolTipManager;
 42 
 43 public class bug4768127 {
 44     static JFrame fr;
 45     static volatile Point p;
 46     static volatile JLabel[] label = new JLabel[2];
 47 
 48     public static void main(String[] args) throws Exception {
 49         try {
 50             SwingUtilities.invokeAndWait(() -> {
 51                 fr = new JFrame("bug4768127");
 52 
 53                 JDesktopPane jdp = new JDesktopPane();
 54                 JInternalFrame jif1 = new JInternalFrame("jif 1");
 55                 JInternalFrame jif2 = new JInternalFrame("jif 2");
 56                 label[0] = new JLabel("Label 1");
 57                 label[1] = new JLabel("Label 2");
 58 
 59                 label[0].setToolTipText("tooltip 1");
 60                 jif1.getContentPane().add(label[0]);
 61                 jif1.setBounds(0, 0, 130, 160);
 62                 jif1.setVisible(true);
 63                 jdp.add(jif1);
 64 
 65                 label[1].setToolTipText("tooltip 2");
 66                 jif2.getContentPane().add(label[1]);
 67                 jif2.setBounds(210, 0, 130, 220);
 68                 jif2.setVisible(true);
 69                 jdp.add(jif2);
 70 
 71                 fr.getContentPane().add(jdp);
 72                 fr.setLocationRelativeTo(null);
 73 
 74                 fr.setSize(400, 300);
 75                 fr.setVisible(true);
 76             });
 77 
 78             Robot robot = new Robot();
 79             robot.setAutoDelay(10);
 80             robot.waitForIdle();
 81             robot.delay(3000);
 82 
 83             clickLabel(0, robot);
 84             robot.waitForIdle();
 85             robot.delay(3000);
 86 
 87             clickLabel(1, robot);
 88             robot.waitForIdle();
 89             robot.delay(3000);
 90 
 91             clickLabel(0, robot);
 92             robot.waitForIdle();
 93             robot.delay(3000);
 94 
 95             clickLabel(1, robot);
 96             robot.waitForIdle();
 97             robot.delay(3000);
 98 
 99             MouseMotionListener[] mml = label[0].getMouseMotionListeners();
100             if (mml.length > 0 && mml[0] instanceof ToolTipManager) {
101                 throw new RuntimeException("Extra MouseMotionListeners were added to the label \"Label 1\" by ToolTipManager");
102             }
103         } finally {
104             SwingUtilities.invokeAndWait(() -> {
105                 if (fr != null) {
106                     fr.dispose();
107                 }
108             });
109         }
110     }
111 
112     static void clickLabel(int i, Robot robot) throws Exception {
113         SwingUtilities.invokeAndWait(() -> {
114             p = label[i].getLocationOnScreen();
115         });
116         final Rectangle rect = label[i].getBounds();
117         robot.mouseMove(p.x + rect.width / 2, p.y + rect.height / 2);
118         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
119         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
120 
121         //Generate mouseMotionEvent
122         robot.mouseMove(p.x + rect.width / 2 + 3, p.y + rect.height / 2 + 3);
123         robot.mouseMove(p.x + rect.width / 2, p.y + rect.height / 2);
124     }
125 }