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