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