1 /*
   2  * Copyright (c) 2016, 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 /*
  25  * @test
  26  * @key headful
  27  * @bug 8139213
  28  * @summary Mac OS Aqua X LAF: JOptionPane truncates the first button
  29  * @run main OptionPaneTest
  30  */
  31 import java.awt.Component;
  32 import java.awt.Insets;
  33 import java.awt.Robot;
  34 import javax.swing.JButton;
  35 import javax.swing.JDialog;
  36 import javax.swing.JOptionPane;
  37 import javax.swing.JPanel;
  38 import javax.swing.SwingUtilities;
  39 
  40 public class OptionPaneTest {
  41 
  42     private volatile static boolean testFailed;
  43     private static JDialog dialog;
  44     private static Robot robot;
  45 
  46     public static void main(final String[] args) throws Exception {
  47         robot = new Robot();
  48         SwingUtilities.invokeAndWait(new Runnable() {
  49             @Override
  50             public void run() {
  51                 try {
  52                     JOptionPane optionPane = new JOptionPane("JOptionPane",
  53                             JOptionPane.INFORMATION_MESSAGE,
  54                             JOptionPane.DEFAULT_OPTION,
  55                             null,
  56                             new String[]{"3", "2", "1"},
  57                             null);
  58                     dialog = optionPane.createDialog("JOptionPane");
  59                     int width = 0;
  60                     Component[] comps = optionPane.getComponents();
  61                     for (Component comp : comps) {
  62                         if (comp instanceof JPanel) {
  63                             Component[] child = ((JPanel) comp).getComponents();
  64                             for (Component c : child) {
  65                                 if (c instanceof JButton) {
  66                                     width += c.getWidth();
  67                                 }
  68                             }
  69                         }
  70                     }
  71                     Insets in = optionPane.getInsets();
  72                     width += in.left + in.right;
  73                     if (width > optionPane.getWidth()) {
  74                         testFailed = true;
  75                     }
  76                 } finally {
  77                     dialog.dispose();
  78                 }
  79             }
  80         });
  81         robot.waitForIdle();
  82         if (testFailed) {
  83             throw new RuntimeException("Test Failed");
  84         }
  85     }
  86 }