1 /*
2  * Copyright (c) 2019, 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 /* @test
25    @bug 8215396
26    @key headful
27    @summary Verifies JTabbedPane preferred size calculation is wrong
28    for SCROLL_TAB_LAYOUT.
29    @run main TabProb
30  */
31 
32 import java.awt.Component;
33 import java.awt.Container;
34 import java.awt.Color;
35 import java.awt.BorderLayout;
36 import java.awt.Dimension;
37 import java.awt.LayoutManager;
38 import java.awt.Insets;
39 import java.awt.Robot;
40 import javax.swing.BorderFactory;
41 import javax.swing.JFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JPanel;
44 import javax.swing.JTabbedPane;
45 import javax.swing.SwingUtilities;
46 import javax.swing.UIManager;
47 
48 public class TabProb extends JFrame {
49     static TabProb tb1;
50     static TabProb tb2;
51     class FixLayout implements LayoutManager {
52         @Override
layoutContainer(Container C)53         public void layoutContainer(Container C) {
54             Insets in = C.getInsets();
55             int w = 200 - in.left - in.right;
56             int h = 100 - in.top - in.bottom;
57             C.getComponents()[0].setBounds(in.top, in.left, w, h);
58         }
59         @Override
minimumLayoutSize(Container C)60         public Dimension minimumLayoutSize(Container C) {
61             return new Dimension(200, 100);
62         }
63         @Override
preferredLayoutSize(Container C)64         public Dimension preferredLayoutSize(Container C) {
65             return new Dimension(200, 100);
66         }
67         @Override
removeLayoutComponent(Component c)68         public void removeLayoutComponent(Component c) {
69         }
70         @Override
addLayoutComponent(String s, Component c)71         public void addLayoutComponent(String s, Component c) {
72         }
73     }
74 
TabProb(int layoutPolicy)75     public TabProb(int layoutPolicy) {
76         JTabbedPane tabpanel = new JTabbedPane();
77         tabpanel.setTabPlacement(JTabbedPane.TOP);
78         tabpanel.setTabLayoutPolicy(layoutPolicy);
79         JPanel panel = new JPanel(new FixLayout());
80         JLabel label = new JLabel("Label");
81         label.setBorder(BorderFactory.createLineBorder(Color.green, 3));
82         panel.add(label);
83         tabpanel.add("TEST", panel);
84         add(tabpanel, BorderLayout.CENTER);
85         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
86     }
87 
main(String[] args)88     public static void main(String[] args) throws Exception {
89         for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
90             System.out.println("Test for LookAndFeel " + laf.getClassName());
91             UIManager.setLookAndFeel(laf.getClassName());
92             test();
93             System.out.println("Test passed for LookAndFeel " + laf.getClassName());
94         }
95     }
96 
test()97     public static void test() throws Exception {
98         Robot robot = new Robot();
99         SwingUtilities.invokeAndWait(new Runnable() {
100             @Override
101             public void run() {
102                 tb1 = new TabProb(JTabbedPane.SCROLL_TAB_LAYOUT);
103                 tb1.pack();
104                 tb1.setVisible(true);
105 
106                 tb2 = new TabProb(JTabbedPane.WRAP_TAB_LAYOUT);
107                 tb2.pack();
108                 tb2.setVisible(true);
109             }
110         });
111         double tb1height = tb1.getPreferredSize().getHeight();
112         double tb2height = tb2.getPreferredSize().getHeight();
113         System.out.println(tb1height);
114         System.out.println(tb2height);
115 
116         robot.waitForIdle();
117         robot.delay(2000);
118 
119         SwingUtilities.invokeAndWait(() -> tb1.dispose());
120         SwingUtilities.invokeAndWait(() -> tb2.dispose());
121 
122         if (tb1height != tb2height) {
123             throw new RuntimeException("JTabbedPane preferred size calculation is wrong for SCROLL_TAB_LAYOUT");
124         }
125     }
126 }
127