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