001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024: import java.util.Enumeration;
025: import java.util.Locale;
026: import java.util.ResourceBundle;
027: import junit.framework.TestCase;
028:
029: public class WindowTest extends TestCase {
030: Frame f;
031: Window w;
032:
033: private boolean listenerCalled;
034: private String propName;
035: private Object oldValue;
036: private Object newValue;
037: private Object src;
038: PropertyChangeListener propListener = new PropertyChangeListener() {
039: public void propertyChange(PropertyChangeEvent pce) {
040: listenerCalled = true;
041: propName = pce.getPropertyName();
042: oldValue = pce.getOldValue();
043: newValue = pce.getNewValue();
044: src = pce.getSource();
045: }
046: };
047:
048: @Override
049: protected void setUp() throws Exception {
050: super .setUp();
051: f = new Frame("Window Test");
052: w = new Window(f);
053: cleanPropertyFields();
054: }
055:
056: @Override
057: protected void tearDown() throws Exception {
058: super .tearDown();
059: if (w != null) {
060: w.dispose();
061: w = null;
062: }
063: if (f != null) {
064: f.dispose();
065: f = null;
066: }
067: }
068:
069: @SuppressWarnings("deprecation")
070: public void testSetLocationRelativeTo() {
071: Rectangle screenRect = f.getGraphicsConfiguration().getBounds();
072: Point centerScreen = screenRect.getLocation();
073: centerScreen.translate((screenRect.width - 1) / 2,
074: screenRect.height / 2);
075: assertNotNull(f);
076: assertNotNull(w);
077: f.show();
078: f.setLocationRelativeTo(null);
079: Point center = f.getLocation();
080: Dimension size = f.getSize();
081: center.translate(size.width / 2, size.height / 2);
082: assertEquals(centerScreen, center);
083: f.setLocationRelativeTo(w);
084: assertEquals(centerScreen, center);
085: f.setBounds(0, 0, 200, 200);
086: w.setSize(100, 100);
087: w.setLocationRelativeTo(f);
088: assertEquals(new Point(50, 50), w.getLocation());
089:
090: }
091:
092: @SuppressWarnings("deprecation")
093: public final void testApplyResourceBundle() {
094: assertNotNull(w);
095: assertSame(ComponentOrientation.UNKNOWN, w
096: .getComponentOrientation());
097: w.applyResourceBundle(new ResourceBundle() {
098:
099: @Override
100: public Enumeration<String> getKeys() {
101: return null;
102: }
103:
104: @Override
105: protected Object handleGetObject(String arg0) {
106: return null;
107: }
108:
109: @Override
110: public Locale getLocale() {
111: return new Locale("ar");
112: }
113:
114: });
115: assertSame(ComponentOrientation.RIGHT_TO_LEFT, w
116: .getComponentOrientation());
117: }
118:
119: @SuppressWarnings("deprecation")
120: public final void testApplyResourceBundleString() {
121: assertNotNull(w);
122: assertSame(ComponentOrientation.UNKNOWN, w
123: .getComponentOrientation());
124: w.applyResourceBundle("java.awt.MyResourceBundle");
125: assertSame(ComponentOrientation.RIGHT_TO_LEFT, w
126: .getComponentOrientation());
127: }
128:
129: @SuppressWarnings("deprecation")
130: public final void testSetGetCursorType() {
131: assertNotNull(f);
132: assertEquals(Frame.DEFAULT_CURSOR, f.getCursorType());
133: int newCursor = Frame.CROSSHAIR_CURSOR;
134: f.setCursor(newCursor);
135: assertEquals(newCursor, f.getCursorType());
136: newCursor = -1;
137: boolean exception = false;
138: try {
139: f.setCursor(newCursor);
140: } catch (IllegalArgumentException e) {
141: exception = true;
142: }
143: assertTrue(exception);
144: }
145:
146: private void cleanPropertyFields() {
147: listenerCalled = false;
148: propName = null;
149: oldValue = newValue = src = null;
150: }
151:
152: private void checkPropertyFields(String propName, Object source,
153: Object newVal) {
154: assertTrue(listenerCalled);
155: assertEquals(propName, this .propName);
156: assertSame(source, src);
157: assertEquals(newVal, newValue);
158: if (newVal != null) {
159: assertFalse(newVal.equals(oldValue));
160: }
161: }
162:
163: public final void testAddPropertyChangeListener() {
164: w.addPropertyChangeListener(propListener);
165: w.setFocusableWindowState(false);
166: checkPropertyFields("focusableWindowState", w, Boolean.FALSE);
167: assertEquals(Boolean.TRUE, oldValue);
168: cleanPropertyFields();
169: w.setAlwaysOnTop(true);
170: checkPropertyFields("alwaysOnTop", w, Boolean.TRUE);
171: assertEquals(Boolean.FALSE, oldValue);
172: }
173:
174: /*
175: * Check if getFont() returns null for if font wasn't set before.
176: */
177: public void testGetFont_Default() {
178: // regression test for Harmony-1605
179: assertEquals(null, w.getFont());
180: }
181:
182: public void testPack() {
183: final Button b = new Button();
184:
185: assertNull(b.getFont());
186: f.add(b);
187: assertNull(b.getFont());
188: assertFalse(b.isDisplayable());
189: f.pack();
190: assertTrue(f.isDisplayable());
191: assertTrue(b.isDisplayable());
192: assertNotNull(b.getFont());
193: assertNotNull(f.getFont());
194: f.dispose();
195: }
196: }
|