-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8352997: Open source several Swing JTabbedPane tests #24370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| /* | ||
| * @test | ||
| * @bug 4287208 | ||
| * @summary Tests if JTabbedPane's setEnabledAt properly renders bounds of Tabs | ||
| * @library /java/awt/regtesthelpers | ||
| * @build PassFailJFrame | ||
| * @run main/manual bug4287208 | ||
| */ | ||
|
|
||
| import java.awt.BorderLayout; | ||
| import java.awt.event.ActionEvent; | ||
| import java.awt.event.ActionListener; | ||
| import javax.swing.ImageIcon; | ||
| import javax.swing.JButton; | ||
| import javax.swing.JFrame; | ||
| import javax.swing.JPanel; | ||
| import javax.swing.JTabbedPane; | ||
|
|
||
| public class bug4287208 implements ActionListener { | ||
|
|
||
| static final String INSTRUCTIONS = """ | ||
| There are two tabs in the test window. Press the "Test" button 5 times. | ||
| If this causes tabs to overlap at any time, the test FAILS, otherwise | ||
| the test PASSES. | ||
| """; | ||
|
|
||
| static boolean state = true; | ||
| static JTabbedPane jtp; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| PassFailJFrame.builder() | ||
| .title("bug4287208 Test Instructions") | ||
| .instructions(INSTRUCTIONS) | ||
| .columns(40) | ||
| .testUI(bug4287208::createUI) | ||
| .build() | ||
| .awaitAndCheck(); | ||
| } | ||
|
|
||
| static JFrame createUI() { | ||
| JFrame frame = new JFrame("bug4287208"); | ||
|
|
||
| JButton start = new JButton("Test"); | ||
| start.addActionListener(new bug4287208()); | ||
| JPanel buttonPanel = new JPanel(); | ||
| buttonPanel.add(start); | ||
| frame.add(buttonPanel,BorderLayout.SOUTH); | ||
|
|
||
| jtp = new JTabbedPane(); | ||
| jtp.addTab("Panel One", new JPanel()); | ||
| String s = System.getProperty("test.src",".") + | ||
| System.getProperty("file.separator") + "duke.gif"; | ||
| ImageIcon ii = new ImageIcon(s); | ||
| jtp.addTab("Panel Two", ii, new JPanel()); | ||
|
|
||
| frame.add(jtp, BorderLayout.CENTER); | ||
| frame.setSize(500, 300); | ||
| return frame; | ||
| } | ||
|
|
||
| public void actionPerformed(ActionEvent evt) { | ||
| jtp.setEnabledAt(0, state); | ||
| jtp.setEnabledAt(1, !state); | ||
| state = !state; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| /* | ||
| * @test | ||
| * @bug 4273320 | ||
| * @summary JTabbedPane.setTitleAt() should refresh when using HTML text | ||
| * @key headful | ||
| */ | ||
|
|
||
| import java.awt.BorderLayout; | ||
| import java.awt.Rectangle; | ||
| import java.awt.Robot; | ||
| import javax.swing.JFrame; | ||
| import javax.swing.JPanel; | ||
| import javax.swing.JTabbedPane; | ||
| import javax.swing.SwingUtilities; | ||
| import javax.swing.plaf.TabbedPaneUI; | ||
|
|
||
| public class bug4273320 { | ||
|
|
||
| static JFrame frame; | ||
| static volatile JTabbedPane tabs; | ||
|
|
||
| static final String PLAIN = "Plain"; | ||
| static final String HTML = "<html>A fairly long HTML text label</html>"; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| try { | ||
| SwingUtilities.invokeAndWait(bug4273320::createUI); | ||
|
|
||
| Robot robot = new Robot(); | ||
| robot.waitForIdle(); | ||
| robot.delay(1000); | ||
|
|
||
| TabbedPaneUI ui = tabs.getUI(); | ||
| Rectangle origSize = ui.getTabBounds(tabs, 0); | ||
|
|
||
| SwingUtilities.invokeAndWait(() -> { | ||
| tabs.setTitleAt(0, HTML); | ||
| }); | ||
| robot.waitForIdle(); | ||
| robot.delay(1000); | ||
|
|
||
| Rectangle newSize = ui.getTabBounds(tabs, 0); | ||
| // The tab should be resized larger if the longer HTML text is added | ||
| System.out.println("orig = " + origSize.width + " x " + origSize.height); | ||
| System.out.println("new = " + newSize.width + " x " + newSize.height); | ||
| if (origSize.width >= newSize.width) { | ||
| throw new RuntimeException("Tab text is not updated."); | ||
| } | ||
| } finally { | ||
| SwingUtilities.invokeAndWait(() -> { | ||
| if (frame != null) { | ||
| frame.dispose(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| static void createUI() { | ||
| frame = new JFrame("bug4273320"); | ||
| tabs = new JTabbedPane(); | ||
| JPanel panel = new JPanel(); | ||
| tabs.addTab(PLAIN, panel); | ||
| frame.getContentPane().add(tabs, BorderLayout.CENTER); | ||
| frame.setSize(500, 300); | ||
| frame.setVisible(true); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| /* | ||
| * @test | ||
| * @bug 4287268 | ||
| * @summary Tests if setIconAt(index,Icon) does not set Tab's disabled icon | ||
| * @key headful | ||
| */ | ||
|
|
||
| import java.awt.BorderLayout; | ||
| import java.awt.Color; | ||
| import java.awt.Graphics; | ||
| import java.awt.Point; | ||
| import java.awt.Rectangle; | ||
| import java.awt.Robot; | ||
| import java.awt.image.BufferedImage; | ||
| import javax.swing.ImageIcon; | ||
| import javax.swing.JFrame; | ||
| import javax.swing.JPanel; | ||
| import javax.swing.JTabbedPane; | ||
| import javax.swing.SwingUtilities; | ||
|
|
||
| public class bug4287268 { | ||
|
|
||
| static JFrame frame; | ||
| static volatile JTabbedPane jtp; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| try { | ||
| SwingUtilities.invokeAndWait(bug4287268::createUI); | ||
|
|
||
| Robot robot = new Robot(); | ||
| robot.waitForIdle(); | ||
| robot.delay(1000); | ||
|
|
||
| Point point = jtp.getLocationOnScreen(); | ||
| int width = jtp.getWidth(); | ||
| int height = jtp.getHeight(); | ||
|
Comment on lines
+57
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be good to wrap it in EDT. It was the following
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I said when it was discussed, I like to avoid dependencies just for the sake of a few lines of code.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree it does create extra layer of dependency if Util is used. |
||
| Rectangle r = new Rectangle(point.x, point.y, width, height); | ||
| BufferedImage cap = robot.createScreenCapture(r); | ||
|
|
||
| int red = Color.red.getRGB(); | ||
| for (int x = 0; x < cap.getWidth(); x++) { | ||
| for (int y = 0; y < cap.getHeight(); y++) { | ||
| int rgb = cap.getRGB(x, y); | ||
| if (rgb == red) { | ||
| try { | ||
| javax.imageio.ImageIO.write(cap, "png", new java.io.File("cap.png")); | ||
| } catch (Exception ee) { | ||
| } | ||
| throw new RuntimeException("Test failed : found red"); | ||
| } | ||
| } | ||
| } | ||
| } finally { | ||
| SwingUtilities.invokeAndWait(() -> { | ||
| if (frame != null) { | ||
| frame.dispose(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| static void createUI() { | ||
| frame = new JFrame("bug4287268"); | ||
| jtp = new JTabbedPane(); | ||
| JPanel panel = new JPanel(); | ||
| jtp.add("Panel One", panel); | ||
| int size = 64; | ||
| BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); | ||
| Graphics g = img.createGraphics(); | ||
| g.setColor(Color.red); | ||
| g.fillRect(0, 0, size, size); | ||
| ImageIcon ii = new ImageIcon(img); | ||
| jtp.setIconAt(0, ii); | ||
| jtp.setEnabledAt(0, false); | ||
| frame.getContentPane().add(jtp, BorderLayout.CENTER); | ||
| frame.pack(); | ||
| frame.setVisible(true); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| /* | ||
| * @test | ||
| * @bug 4362226 | ||
| * @summary JTabbedPane's HTML title should have proper offsets | ||
| * @library /java/awt/regtesthelpers | ||
| * @build PassFailJFrame | ||
| * @run main/manual bug4362226 | ||
| */ | ||
|
|
||
| import java.awt.BorderLayout; | ||
| import javax.swing.JFrame; | ||
| import javax.swing.JPanel; | ||
| import javax.swing.JTabbedPane; | ||
| import javax.swing.UIManager; | ||
| import javax.swing.plaf.metal.MetalLookAndFeel; | ||
|
|
||
| public class bug4362226 { | ||
|
|
||
| static final String PLAIN = "Label"; | ||
| static final String HTML = "<html>Label</html>"; | ||
|
|
||
| static final String INSTRUCTIONS = """ | ||
| The test window contains a JTabbedPane with two tabs. | ||
| The titles for both tabs should look similar and drawn with the same fonts. | ||
| The text of the tabs should start in a position that is offset from the left | ||
| boundary of the tab, so there is clear space between them. | ||
| If there is no space, then the test FAILS. | ||
| """; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| PassFailJFrame.builder() | ||
| .title("bug4362226 Test Instructions") | ||
| .instructions(INSTRUCTIONS) | ||
| .columns(60) | ||
| .testUI(bug4362226::createUI) | ||
| .build() | ||
| .awaitAndCheck(); | ||
| } | ||
|
|
||
| static JFrame createUI() { | ||
| try { | ||
| UIManager.setLookAndFeel(new MetalLookAndFeel()); | ||
| } catch (Exception e) { | ||
| } | ||
| JFrame frame = new JFrame("bug4362226"); | ||
| JTabbedPane tabs = new JTabbedPane(); | ||
| tabs.addTab(PLAIN, new JPanel()); | ||
| tabs.addTab(HTML, new JPanel()); | ||
| frame.add(tabs, BorderLayout.CENTER); | ||
| frame.setSize(500, 300); | ||
| return frame; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On EDT ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is needed here because the UI is stable and so access should be safe.