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