1/*
2 * Copyright (c) 2013, 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
24import java.awt.BorderLayout;
25
26import java.awt.Container;
27import java.awt.Rectangle;
28import java.awt.Toolkit;
29
30import javax.swing.JButton;
31import javax.swing.JCheckBox;
32import javax.swing.JTabbedPane;
33
34import javax.swing.JFrame;
35import javax.swing.SwingUtilities;
36import javax.swing.UIManager;
37import javax.swing.UIManager.LookAndFeelInfo;
38
39/*
40 * @test
41 * @key headful
42 * @bug 7024235
43 * @summary Tests JFrame.pack() with the JTabbedPane
44 * @library ../../../../lib/testlibrary/
45 * @build ExtendedRobot
46 * @author Sergey Malenkov
47 * @run main Test7024235
48 */
49
50public class Test7024235 implements Runnable {
51
52    private static final boolean AUTO = null != System.getProperty("test.src", null);
53
54    public static void main(String[] args) throws Exception {
55        Test7024235 test = new Test7024235();
56        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
57            UIManager.setLookAndFeel(info.getClassName());
58
59            test.test();
60            try {
61                ExtendedRobot robot = new ExtendedRobot();
62                robot.waitForIdle(1000);
63            }catch(Exception ex) {
64                ex.printStackTrace();
65                throw new Error("Unexpected Failure");
66            }
67            test.test();
68        }
69    }
70
71    private volatile JTabbedPane pane;
72    private volatile boolean passed;
73
74    public void run() {
75        if (this.pane == null) {
76            this.pane = new JTabbedPane();
77            this.pane.addTab("1", new Container());
78            this.pane.addTab("2", new JButton());
79            this.pane.addTab("3", new JCheckBox());
80
81            JFrame frame = new JFrame();
82            frame.add(BorderLayout.WEST, this.pane);
83            frame.pack();
84            frame.setVisible(true);
85
86            test("first");
87        }
88        else {
89            test("second");
90            if (this.passed || AUTO) { // do not close a frame for manual review
91                SwingUtilities.getWindowAncestor(this.pane).dispose();
92            }
93            this.pane = null;
94        }
95    }
96
97    private void test() throws Exception {
98        SwingUtilities.invokeAndWait(this);
99        if (!this.passed && AUTO) { // error reporting only for automatic testing
100            throw new Error("TEST FAILED");
101        }
102    }
103
104    private void test(String step) {
105        this.passed = true;
106        for (int index = 0; index < this.pane.getTabCount(); index++) {
107            Rectangle bounds = this.pane.getBoundsAt(index);
108            int centerX = bounds.x + bounds.width / 2;
109            int centerY = bounds.y + bounds.height / 2;
110            int actual = this.pane.indexAtLocation(centerX, centerY);
111            if (index != actual) {
112                System.out.println("name = " + UIManager.getLookAndFeel().getName());
113                System.out.println("step = " + step);
114                System.out.println("index = " + index);
115                System.out.println("bounds = " + bounds);
116                System.out.println("indexAtLocation(" + centerX + "," + centerX + ") returns " + actual);
117                this.passed = false;
118            }
119        }
120    }
121}
122