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 8137169
  28  * @summary verifies TabbedScrollPane minimum height for all Look and Feels
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @run main ScrollableTabbedPaneTest
  32  */
  33 
  34 import java.awt.Robot;
  35 import javax.swing.JFrame;
  36 import javax.swing.JPanel;
  37 import javax.swing.JTabbedPane;
  38 import javax.swing.SwingConstants;
  39 import javax.swing.SwingUtilities;
  40 import javax.swing.UIManager;
  41 import javax.swing.UnsupportedLookAndFeelException;
  42 
  43 public class ScrollableTabbedPaneTest {
  44 
  45     private static JFrame frame;
  46     private static JTabbedPane pane;
  47     private static Robot robot;
  48     private static volatile String errorString = "";
  49 
  50     public static void main(String[] args) throws Exception {
  51         robot = new Robot();
  52         robot.delay(1000);
  53         UIManager.LookAndFeelInfo[] lookAndFeelArray
  54                 = UIManager.getInstalledLookAndFeels();
  55         for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
  56             executeCase(lookAndFeelItem.getClassName(),
  57                         lookAndFeelItem.getName());
  58         }
  59         if (!"".equals(errorString)) {
  60             throw new RuntimeException("Error Log:\n" + errorString);
  61         }
  62     }
  63 
  64     private static void executeCase(String lookAndFeelString, String shortLAF)
  65             throws Exception {
  66         if (tryLookAndFeel(lookAndFeelString)) {
  67             createUI(shortLAF);
  68             stepsToExecute(shortLAF);
  69 
  70             createLeftUI(shortLAF);
  71             stepsToExecute(shortLAF);
  72 
  73             createRightUI(shortLAF);
  74             stepsToExecute(shortLAF);
  75         }
  76     }
  77 
  78     private static void stepsToExecute(String shortLAF) throws Exception {
  79         robot.delay(100);
  80         runTestCase(shortLAF);
  81         robot.delay(1000);
  82         cleanUp();
  83         robot.delay(1000);
  84     }
  85 
  86     private static boolean tryLookAndFeel(String lookAndFeelString)
  87             throws Exception {
  88         try {
  89             UIManager.setLookAndFeel(
  90                     lookAndFeelString);
  91 
  92         } catch (UnsupportedLookAndFeelException
  93                 | ClassNotFoundException
  94                 | InstantiationException
  95                 | IllegalAccessException e) {
  96             return false;
  97         }
  98         return true;
  99     }
 100 
 101     private static void cleanUp() throws Exception {
 102         SwingUtilities.invokeAndWait(new Runnable() {
 103             @Override
 104             public void run() {
 105                 frame.dispose();
 106             }
 107         });
 108     }
 109 
 110     private static void createUI(final String shortLAF)
 111             throws Exception {
 112         SwingUtilities.invokeAndWait(new Runnable() {
 113             @Override
 114             public void run() {
 115                 frame = new JFrame(shortLAF);
 116                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 117                 frame.setVisible(true);
 118                 pane = new JTabbedPane();
 119                 pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 120                 frame.add(pane);
 121                 frame.setSize(500, 500);
 122             }
 123         });
 124     }
 125     private static void createLeftUI(final String shortLAF)
 126             throws Exception {
 127         SwingUtilities.invokeAndWait(new Runnable() {
 128             @Override
 129             public void run() {
 130                 frame = new JFrame(shortLAF);
 131                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 132                 frame.setVisible(true);
 133                 pane = new JTabbedPane();
 134                 pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 135                 pane.setTabPlacement(SwingConstants.LEFT);
 136                 frame.add(pane);
 137                 frame.setSize(500, 500);
 138             }
 139         });
 140     }
 141 
 142     private static void createRightUI(final String shortLAF)
 143             throws Exception {
 144         SwingUtilities.invokeAndWait(new Runnable() {
 145             @Override
 146             public void run() {
 147                 frame = new JFrame(shortLAF);
 148                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 149                 frame.setVisible(true);
 150                 pane = new JTabbedPane();
 151                 pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 152                 pane.setTabPlacement(SwingConstants.RIGHT);
 153                 frame.add(pane);
 154                 frame.setSize(500, 500);
 155             }
 156         });
 157     }
 158 
 159     private static void runTestCase(String shortLAF) throws Exception {
 160         SwingUtilities.invokeAndWait(new Runnable() {
 161             @Override
 162             public void run() {
 163                 int i = 0;
 164                 int value= 0;
 165                 do {
 166                     String title = "Tab" + (i + 1);
 167                     pane.addTab(title, new JPanel());
 168                     int tempValue = pane.getMinimumSize().height;
 169                     if(value==0) {
 170                         value = tempValue;
 171                     }
 172                     if(value != tempValue) {
 173                         String error = "[" + shortLAF
 174                             + "]: [Error]: TabbedScrollPane fails";
 175                     errorString += error;
 176                     }
 177 
 178                     ++i;
 179                 } while (i < 10);
 180             }
 181         });
 182     }
 183 }
 184