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