|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, 2024, 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 | +import java.awt.Color; |
| 25 | +import java.awt.Dimension; |
| 26 | +import java.awt.Point; |
| 27 | +import java.awt.Robot; |
| 28 | +import java.util.ArrayList; |
| 29 | + |
| 30 | +import javax.swing.JFrame; |
| 31 | +import javax.swing.JLabel; |
| 32 | +import javax.swing.JTabbedPane; |
| 33 | +import javax.swing.SwingUtilities; |
| 34 | +import javax.swing.UIManager; |
| 35 | +import javax.swing.UnsupportedLookAndFeelException; |
| 36 | + |
| 37 | +/* |
| 38 | + * @test |
| 39 | + * @key headful |
| 40 | + * @bug 8007563 |
| 41 | + * @summary Tests JTabbedPane background |
| 42 | + */ |
| 43 | + |
| 44 | +public class TestJTabbedPaneBackgroundColor { |
| 45 | + private static ArrayList<String> lafList = new ArrayList<>(); |
| 46 | + private static JFrame frame; |
| 47 | + private static JTabbedPane pane; |
| 48 | + private static Robot robot; |
| 49 | + private static volatile Dimension dim; |
| 50 | + private static volatile Point loc; |
| 51 | + |
| 52 | + public static void main(String[] args) throws Exception { |
| 53 | + robot = new Robot(); |
| 54 | + |
| 55 | + for (UIManager.LookAndFeelInfo laf : |
| 56 | + UIManager.getInstalledLookAndFeels()) { |
| 57 | + System.out.println("Testing: " + laf.getName()); |
| 58 | + setLookAndFeel(laf); |
| 59 | + |
| 60 | + try { |
| 61 | + SwingUtilities.invokeAndWait(TestJTabbedPaneBackgroundColor::createAndShowUI); |
| 62 | + robot.waitForIdle(); |
| 63 | + robot.delay(500); |
| 64 | + |
| 65 | + SwingUtilities.invokeAndWait(() -> { |
| 66 | + loc = pane.getLocationOnScreen(); |
| 67 | + dim = pane.getSize(); |
| 68 | + }); |
| 69 | + |
| 70 | + loc = new Point(loc.x + dim.width - 2, loc.y + 2); |
| 71 | + doTesting(loc, laf); |
| 72 | + |
| 73 | + if (!pane.isOpaque()) { |
| 74 | + pane.setOpaque(true); |
| 75 | + pane.repaint(); |
| 76 | + } |
| 77 | + robot.waitForIdle(); |
| 78 | + robot.delay(500); |
| 79 | + |
| 80 | + doTesting(loc, laf); |
| 81 | + |
| 82 | + } finally { |
| 83 | + SwingUtilities.invokeAndWait(() -> { |
| 84 | + if (frame != null) { |
| 85 | + frame.dispose(); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + if (!lafList.isEmpty()) { |
| 91 | + throw new RuntimeException(lafList.toString()); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) { |
| 96 | + try { |
| 97 | + UIManager.setLookAndFeel(laf.getClassName()); |
| 98 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 99 | + System.out.println("Unsupported LAF: " + laf.getClassName()); |
| 100 | + } catch (ClassNotFoundException | InstantiationException |
| 101 | + | IllegalAccessException e) { |
| 102 | + throw new RuntimeException(e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static void createAndShowUI() { |
| 107 | + pane = new JTabbedPane(); |
| 108 | + pane.setOpaque(false); |
| 109 | + pane.setBackground(Color.RED); |
| 110 | + for (int i = 0; i < 3; i++) { |
| 111 | + pane.addTab("Tab " + i, new JLabel("Content area " + i)); |
| 112 | + } |
| 113 | + frame = new JFrame("Test Background Color"); |
| 114 | + frame.getContentPane().setBackground(Color.BLUE); |
| 115 | + frame.add(pane); |
| 116 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 117 | + frame.setSize(400, 200); |
| 118 | + frame.setLocationRelativeTo(null); |
| 119 | + frame.setVisible(true); |
| 120 | + } |
| 121 | + |
| 122 | + private static void doTesting(Point p, UIManager.LookAndFeelInfo laf) { |
| 123 | + boolean isOpaque = pane.isOpaque(); |
| 124 | + Color actual = robot.getPixelColor(p.x, p.y); |
| 125 | + Color expected = isOpaque |
| 126 | + ? pane.getBackground() |
| 127 | + : frame.getContentPane().getBackground(); |
| 128 | + |
| 129 | + if (!expected.equals(actual)) { |
| 130 | + addOpaqueError(laf.getName(), isOpaque); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static void addOpaqueError(String lafName, boolean opaque) { |
| 135 | + lafList.add(lafName + " opaque=" + opaque); |
| 136 | + } |
| 137 | +} |
0 commit comments