Path: blob/master/test/jdk/java/awt/Component/Headless/HeadlessComponent.java
41153 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.awt.event.*;25import java.util.Locale;2627/*28* @test29* @summary Check that Component methods do not throw unexpected exceptions30* in headless mode31* @run main/othervm -Djava.awt.headless=true HeadlessComponent32*/3334public class HeadlessComponent {35public static void main(String args[]) {36Component comp = new Component(){};37comp.addComponentListener(new ComponentAdapter() {});38comp.addFocusListener(new FocusAdapter(){});39comp.addHierarchyBoundsListener(new HierarchyBoundsAdapter(){});40comp.addHierarchyListener(e -> {});41comp.addInputMethodListener(new InputMethodListener() {42public void inputMethodTextChanged(InputMethodEvent event) {}43public void caretPositionChanged(InputMethodEvent event) {}44});45comp.addKeyListener(new KeyAdapter() {});46comp.addMouseListener(new MouseAdapter() {});47comp.addMouseMotionListener(new MouseMotionAdapter() {});48comp.addMouseWheelListener(e -> {});49comp.addPropertyChangeListener(e -> {});50comp.addNotify();51comp.getName();52comp.setName("goober");53comp.getParent();54comp.getGraphicsConfiguration();55comp.getTreeLock();56comp.getToolkit();57comp.isValid();58comp.isDisplayable();59comp.isVisible();60comp.isShowing();61comp.isEnabled();62comp.setEnabled(false);63comp.setEnabled(true);64comp.enable();65comp.enable(false);66comp.enable(true);67comp.disable();68comp.isDoubleBuffered();69comp.enableInputMethods(false);70comp.enableInputMethods(true);71comp.setVisible(false);72comp.setVisible(true);73comp.show();74comp.show(false);75comp.show(true);76comp.hide();77comp.getForeground();78comp.setForeground(Color.red);79comp.isForegroundSet();80comp.getBackground();81comp.setBackground(Color.red);82comp.isBackgroundSet();83comp.getFont();84for (String font : Toolkit.getDefaultToolkit().getFontList()) {85for (int i = 8; i < 17; i++) {86Font f1 = new Font(font, Font.PLAIN, i);87Font f2 = new Font(font, Font.BOLD, i);88Font f3 = new Font(font, Font.ITALIC, i);89Font f4 = new Font(font, Font.BOLD | Font.ITALIC, i);9091comp.setFont(f1);92comp.getFontMetrics(f1);93comp.setFont(f2);94comp.getFontMetrics(f2);95comp.setFont(f3);96comp.getFontMetrics(f3);97comp.setFont(f4);98comp.getFontMetrics(f4);99}100}101comp.isFontSet();102103boolean exceptions = false;104try {105Container c = new Container();106c.add(comp);107comp.getLocale();108} catch (IllegalComponentStateException ex) {109exceptions = true;110}111if (!exceptions)112throw new RuntimeException("IllegalComponentStateException did not occur when expected");113114for (Locale locale : Locale.getAvailableLocales())115comp.setLocale(locale);116117comp.getColorModel();118comp.getLocation();119120exceptions = false;121try {122comp = new Component(){};123comp.getLocationOnScreen();124} catch (IllegalComponentStateException e) {125exceptions = true;126}127if (!exceptions)128throw new RuntimeException("IllegalComponentStateException did not occur when expected");129130comp.location();131comp.setLocation(1, 2);132comp.move(1, 2);133comp.setLocation(new Point(1, 2));134comp.getSize();135comp.size();136comp.setSize(1, 32);137comp.resize(1, 32);138comp.setSize(new Dimension(1, 32));139comp.resize(new Dimension(1, 32));140comp.getBounds();141comp.bounds();142comp.setBounds(10, 10, 10, 10);143comp.reshape(10, 10, 10, 10);144comp.setBounds(new Rectangle(10, 10, 10, 10));145comp.getX();146comp.getY();147comp.getWidth();148comp.getHeight();149comp.getBounds(new Rectangle(1, 1, 1, 1));150comp.getSize(new Dimension(1, 2));151comp.getLocation(new Point(1, 2));152comp.isOpaque();153comp.isLightweight();154comp.getPreferredSize();155comp.preferredSize();156comp.getMinimumSize();157comp.minimumSize();158comp.getMaximumSize();159comp.getAlignmentX();160comp.getAlignmentY();161comp.doLayout();162comp.layout();163comp.validate();164comp.invalidate();165comp.getGraphics();166Cursor c = new Cursor(Cursor.CROSSHAIR_CURSOR);167comp.setCursor(c);168comp.getCursor();169comp.isCursorSet();170comp.contains(1, 2);171comp.inside(1, 2);172comp.contains(new Point(1, 2));173comp.getComponentAt(1, 2);174comp.locate(1, 2);175comp.getComponentAt(new Point(1, 2));176comp.isFocusTraversable();177comp.isFocusable();178comp.setFocusable(true);179comp.setFocusable(false);180comp.requestFocus();181comp.requestFocusInWindow();182comp.transferFocus();183comp.getFocusCycleRootAncestor();184comp.isFocusCycleRoot(new Container());185comp.nextFocus();186comp.transferFocusBackward();187comp.transferFocusUpCycle();188comp.hasFocus();189comp.isFocusOwner();190comp.toString();191comp.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);192comp.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);193comp.setComponentOrientation(ComponentOrientation.UNKNOWN);194comp.getComponentOrientation();195comp.getAccessibleContext();196}197}198199200