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 8146276
  28  * @summary Right aligned toolbar component does not appear
  29  * @run main NimbusGlueTest
  30  */
  31 import java.awt.BorderLayout;
  32 import java.awt.Component;
  33 import java.awt.Dimension;
  34 import javax.swing.Box;
  35 import javax.swing.JButton;
  36 import javax.swing.JFrame;
  37 import javax.swing.JToolBar;
  38 import javax.swing.SwingUtilities;
  39 import javax.swing.UIManager;
  40 import java.awt.Robot;
  41 import javax.swing.UnsupportedLookAndFeelException;
  42 
  43 public class NimbusGlueTest {
  44 
  45     private static JFrame frame;
  46     private static Robot robot;
  47     private static volatile String errorMessage = "";
  48     private static JToolBar bar;
  49 
  50     public static void main(String[] args) throws Exception {
  51         robot = new Robot();
  52         UIManager.LookAndFeelInfo[] lookAndFeelArray
  53                 = UIManager.getInstalledLookAndFeels();
  54         for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
  55             String lookAndFeelString = lookAndFeelItem.getClassName();
  56             if (tryLookAndFeel(lookAndFeelString)) {
  57                 createUI();
  58                 performTest();
  59                 robot.waitForIdle();
  60             }
  61         }
  62         if (!"".equals(errorMessage)) {
  63             throw new RuntimeException(errorMessage);
  64         }
  65     }
  66 
  67     private static boolean tryLookAndFeel(String lookAndFeelString) {
  68         try {
  69             UIManager.setLookAndFeel(lookAndFeelString);
  70             return true;
  71         } catch (UnsupportedLookAndFeelException | ClassNotFoundException |
  72                 InstantiationException | IllegalAccessException e) {
  73             errorMessage += e.getMessage() + "\n";
  74             System.err.println("Caught Exception: " + e.getMessage());
  75             return false;
  76         }
  77     }
  78 
  79     private static void performTest() throws Exception {
  80         SwingUtilities.invokeAndWait(new Runnable() {
  81             public void run() {
  82                 try {
  83                     int width = 0;
  84                     for (Component comp : bar.getComponents()) {
  85                         width += comp.getWidth();
  86                     }
  87                     if (width > 600) {
  88                         errorMessage = "Test Failed";
  89                     }
  90                 } finally {
  91                     frame.dispose();
  92                 }
  93 
  94             }
  95         });
  96     }
  97 
  98     private static void createUI() throws Exception {
  99         SwingUtilities.invokeAndWait(new Runnable() {
 100             public void run() {
 101                 frame = new JFrame();
 102                 bar = new JToolBar();
 103                 bar.add(createButton(1));
 104                 bar.add(createButton(2));
 105                 bar.add(Box.createHorizontalGlue());
 106                 bar.add(createButton(3));
 107                 frame.add(bar, BorderLayout.NORTH);
 108                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 109                 frame.setSize(600, 400);
 110                 frame.setVisible(true);
 111             }
 112         });
 113     }
 114 
 115     private static JButton createButton(int id) {
 116         JButton b = new JButton("B: " + id);
 117         b.setPreferredSize(new Dimension(60, b.getPreferredSize().height));
 118         return b;
 119     }
 120 }