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