1 /*
   2  * Copyright (c) 2007, 2015, 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.*;
  25 import java.util.Locale;
  26 
  27 /*
  28  * @test
  29  * @summary Check that Panel constructors and methods do not throw unexpected
  30  *          exceptions in headless mode
  31  * @run main/othervm -Djava.awt.headless=true HeadlessPanel
  32  */
  33 
  34 public class HeadlessPanel {
  35     public static void main(String args[]) {
  36         Panel p;
  37         p = new Panel();
  38         p = new Panel(new FlowLayout());
  39         p.getAccessibleContext();
  40         Component c1 = p.add(new Component(){});
  41         Component c2 = p.add(new Component(){});
  42         Component c3 = p.add(new Component(){});
  43         p.getComponentCount();
  44         p.countComponents();
  45         p.getComponent(1);
  46         p.getComponent(2);
  47         Component[] cs = p.getComponents();
  48         Insets ins = p.getInsets();
  49         ins = p.insets();
  50         p.remove(0);
  51         p.remove((Component) c2);
  52         p.removeAll();
  53 
  54         p.add(c1);
  55         p.add(c2);
  56         p.add(c3);
  57         p.getLayout();
  58         p.setLayout(new FlowLayout());
  59         p.doLayout();
  60         p.layout();
  61         p.invalidate();
  62         p.validate();
  63 
  64         p.getPreferredSize();
  65         p.preferredSize();
  66         p.getMinimumSize();
  67         p.minimumSize();
  68         p.getMaximumSize();
  69         p.getAlignmentX();
  70         p.getAlignmentY();
  71         p.getComponentAt(1, 2);
  72         p.locate(1, 2);
  73         p.getComponentAt(new Point(1, 2));
  74         p.isFocusCycleRoot(new Container());
  75         p.transferFocusBackward();
  76         p.setName("goober");
  77         p.getName();
  78         p.getParent();
  79         p.getGraphicsConfiguration();
  80         p.getTreeLock();
  81         p.getToolkit();
  82         p.isValid();
  83         p.isDisplayable();
  84         p.isVisible();
  85         p.isShowing();
  86         p.isEnabled();
  87         p.setEnabled(false);
  88         p.setEnabled(true);
  89         p.enable();
  90         p.enable(false);
  91         p.enable(true);
  92         p.disable();
  93         p.isDoubleBuffered();
  94         p.enableInputMethods(false);
  95         p.enableInputMethods(true);
  96         p.setVisible(false);
  97         p.setVisible(true);
  98         p.show();
  99         p.show(false);
 100         p.show(true);
 101         p.hide();
 102         p.getForeground();
 103         p.setForeground(Color.red);
 104         p.isForegroundSet();
 105         p.getBackground();
 106         p.setBackground(Color.red);
 107         p.isBackgroundSet();
 108         p.getFont();
 109         p.isFontSet();
 110         p.getColorModel();
 111         p.getLocation();
 112         p.location();
 113         p.setLocation(1, 2);
 114         p.move(1, 2);
 115         p.setLocation(new Point(1, 2));
 116         p.getSize();
 117         p.size();
 118         p.setSize(1, 32);
 119         p.resize(1, 32);
 120         p.setSize(new Dimension(1, 32));
 121         p.resize(new Dimension(1, 32));
 122         p.getBounds();
 123         p.bounds();
 124         p.setBounds(10, 10, 10, 10);
 125         p.reshape(10, 10, 10, 10);
 126         p.setBounds(new Rectangle(10, 10, 10, 10));
 127         p.getX();
 128         p.getY();
 129         p.getWidth();
 130         p.getHeight();
 131         p.getBounds(new Rectangle(1, 1, 1, 1));
 132         p.getSize(new Dimension(1, 2));
 133         p.getLocation(new Point(1, 2));
 134         p.isOpaque();
 135         p.isLightweight();
 136         p.getGraphics();
 137 
 138 
 139         for (String font : Toolkit.getDefaultToolkit().getFontList()) {
 140             for (int j = 8; j < 17; j++) {
 141                 Font f1 = new Font(font, Font.PLAIN, j);
 142                 Font f2 = new Font(font, Font.BOLD, j);
 143                 Font f3 = new Font(font, Font.ITALIC, j);
 144                 Font f4 = new Font(font, Font.BOLD | Font.ITALIC, j);
 145 
 146                 p.setFont(f1);
 147                 p.setFont(f2);
 148                 p.setFont(f3);
 149                 p.setFont(f4);
 150             }
 151         }
 152 
 153         boolean exceptions = false;
 154         try {
 155             Container c = new Container();
 156             p = new Panel();
 157             c.add(p);
 158             p.getLocale();
 159         } catch (IllegalComponentStateException e) {
 160             exceptions = true;
 161         }
 162         if (!exceptions)
 163             throw new RuntimeException("IllegalComponentStateException did not occur when expected");
 164 
 165         for (Locale locale : Locale.getAvailableLocales())
 166             p.setLocale(locale);
 167 
 168         exceptions = false;
 169         try {
 170             p.getLocationOnScreen();
 171         } catch (IllegalComponentStateException e) {
 172             exceptions = true;
 173         }
 174         if (!exceptions)
 175             throw new RuntimeException("IllegalComponentStateException did not occur when expected");
 176 
 177         for (String font : Toolkit.getDefaultToolkit().getFontList()) {
 178             for (int j = 8; j
 179                     < 17; j++) {
 180                 Font f1 = new Font(font, Font.PLAIN, j);
 181                 Font f2 = new Font(font, Font.BOLD, j);
 182                 Font f3 = new Font(font, Font.ITALIC, j);
 183                 Font f4 = new Font(font, Font.BOLD | Font.ITALIC, j);
 184 
 185                 p.getFontMetrics(f1);
 186                 p.getFontMetrics(f2);
 187                 p.getFontMetrics(f3);
 188                 p.getFontMetrics(f4);
 189             }
 190         }
 191 
 192         Cursor c = new Cursor(Cursor.CROSSHAIR_CURSOR);
 193         p.setCursor(c);
 194         p.getCursor();
 195         p.isCursorSet();
 196         p.contains(1, 2);
 197         p.inside(1, 2);
 198         p.contains(new Point(1, 2));
 199         p.isFocusTraversable();
 200         p.isFocusable();
 201         p.setFocusable(true);
 202         p.setFocusable(false);
 203         p.requestFocus();
 204         p.requestFocusInWindow();
 205         p.transferFocus();
 206         p.getFocusCycleRootAncestor();
 207         p.nextFocus();
 208         p.transferFocusUpCycle();
 209         p.hasFocus();
 210         p.isFocusOwner();
 211         p.toString();
 212         p.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 213         p.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
 214         p.setComponentOrientation(ComponentOrientation.UNKNOWN);
 215         p.getComponentOrientation();
 216     }
 217 }