|
| 1 | +/* |
| 2 | + * Copyright (c) 2006, 2025, 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 4148057 6178004 |
| 27 | + * @summary REGRESSION: setToolTipText does not work if the |
| 28 | + * component is not focused |
| 29 | + * @library /java/awt/regtesthelpers |
| 30 | + * @build PassFailJFrame |
| 31 | + * @run main/manual bug6178004 |
| 32 | + */ |
| 33 | + |
| 34 | +import java.awt.Point; |
| 35 | +import java.awt.Window; |
| 36 | +import java.awt.event.MouseEvent; |
| 37 | +import java.util.List; |
| 38 | +import javax.swing.ButtonGroup; |
| 39 | +import javax.swing.JButton; |
| 40 | +import javax.swing.JCheckBoxMenuItem; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.JMenu; |
| 43 | +import javax.swing.JMenuBar; |
| 44 | +import javax.swing.LookAndFeel; |
| 45 | +import javax.swing.SwingUtilities; |
| 46 | +import javax.swing.ToolTipManager; |
| 47 | +import javax.swing.UIManager; |
| 48 | + |
| 49 | +public class bug6178004 { |
| 50 | + private static JFrame frame1; |
| 51 | + private static JFrame frame2; |
| 52 | + private static final int SIZE = 300; |
| 53 | + |
| 54 | + private static final String INSTRUCTIONS = """ |
| 55 | + You can change Look And Feel using the menu "Change LaF". |
| 56 | +
|
| 57 | + Make sure that Frame2 or instruction window is active. |
| 58 | + Move mouse over the button inside "Frame 1". |
| 59 | + If tooltip is NOT shown or Frame 1 jumped on top of |
| 60 | + the Frame2, press FAIL. |
| 61 | +
|
| 62 | + For Metal/Windows LaF: |
| 63 | + Tooltips are shown only if one of the frames (or the instruction |
| 64 | + window) is active. To test it click on any other application to |
| 65 | + make frames and instruction window inactive and then verify that |
| 66 | + tooltips are not shown any more. |
| 67 | +
|
| 68 | + For Motif/GTK/Nimbus/Aqua LaF: |
| 69 | + Tooltips should be shown for all frames irrespective of whether |
| 70 | + the application is active or inactive. |
| 71 | +
|
| 72 | + Note: Tooltip for Frame1 is always shown at the top-left corner. |
| 73 | + Tooltips could be shown partly covered by another frame. |
| 74 | +
|
| 75 | + If above is true press PASS else FAIL. |
| 76 | + """; |
| 77 | + |
| 78 | + public static void main(String[] args) throws Exception { |
| 79 | + PassFailJFrame.builder() |
| 80 | + .title("bug6178004 Test Instructions") |
| 81 | + .instructions(INSTRUCTIONS) |
| 82 | + .testTimeOut(10) |
| 83 | + .columns(40) |
| 84 | + .testUI(createAndShowUI()) |
| 85 | + .positionTestUI(bug6178004::positionTestWindows) |
| 86 | + .build() |
| 87 | + .awaitAndCheck(); |
| 88 | + } |
| 89 | + |
| 90 | + private static List<Window> createAndShowUI() { |
| 91 | + ToolTipManager.sharedInstance().setInitialDelay(0); |
| 92 | + |
| 93 | + frame1 = new JFrame("bug6178004 Frame1"); |
| 94 | + frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 95 | + JButton button = new JButton("Test") { |
| 96 | + public Point getToolTipLocation(MouseEvent event) { |
| 97 | + return new Point(10, 10); |
| 98 | + } |
| 99 | + }; |
| 100 | + button.setToolTipText("Tooltip-1"); |
| 101 | + frame1.add(button); |
| 102 | + frame1.setSize(SIZE, SIZE); |
| 103 | + |
| 104 | + frame2 = new JFrame("bug6178004 Frame2"); |
| 105 | + frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 106 | + JButton button2 = new JButton("Click me") ; |
| 107 | + button2.setToolTipText("Tooltip-2"); |
| 108 | + frame2.add(button2); |
| 109 | + frame2.setSize(SIZE, SIZE); |
| 110 | + |
| 111 | + JMenuBar bar = new JMenuBar(); |
| 112 | + JMenu lafMenu = new JMenu("Change LaF"); |
| 113 | + ButtonGroup lafGroup = new ButtonGroup(); |
| 114 | + |
| 115 | + LookAndFeel currentLaf = UIManager.getLookAndFeel(); |
| 116 | + UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels(); |
| 117 | + for (final UIManager.LookAndFeelInfo lafInfo : lafs) { |
| 118 | + JCheckBoxMenuItem lafItem = new JCheckBoxMenuItem(lafInfo.getName()); |
| 119 | + lafItem.addActionListener(e -> setLaF(lafInfo.getClassName())); |
| 120 | + if (lafInfo.getClassName().equals(currentLaf.getClass().getName())) { |
| 121 | + lafItem.setSelected(true); |
| 122 | + } |
| 123 | + |
| 124 | + lafGroup.add(lafItem); |
| 125 | + lafMenu.add(lafItem); |
| 126 | + } |
| 127 | + |
| 128 | + bar.add(lafMenu); |
| 129 | + frame2.setJMenuBar(bar); |
| 130 | + return List.of(frame1, frame2); |
| 131 | + } |
| 132 | + |
| 133 | + private static void setLaF(String laf) { |
| 134 | + try { |
| 135 | + UIManager.setLookAndFeel(laf); |
| 136 | + SwingUtilities.updateComponentTreeUI(frame1); |
| 137 | + SwingUtilities.updateComponentTreeUI(frame2); |
| 138 | + } catch (Exception e) { |
| 139 | + e.printStackTrace(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // custom window layout required for this test |
| 144 | + private static void positionTestWindows(List<? extends Window> testWindows, |
| 145 | + PassFailJFrame.InstructionUI instructionUI) { |
| 146 | + int gap = 5; |
| 147 | + int x = instructionUI.getLocation().x + instructionUI.getSize().width + gap; |
| 148 | + // the two test frames need to overlap for this test |
| 149 | + testWindows.get(0).setLocation(x, instructionUI.getLocation().y); |
| 150 | + testWindows.get(1).setLocation((x + SIZE / 2), instructionUI.getLocation().y); |
| 151 | + } |
| 152 | +} |
0 commit comments