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: package javax.swing;
018:
019: import java.awt.Component;
020: import java.awt.EventQueue;
021: import java.awt.Graphics;
022: import java.awt.KeyboardFocusManager;
023: import java.awt.event.InputEvent;
024: import java.awt.event.KeyEvent;
025: import java.awt.image.BufferedImage;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028: import java.util.HashSet;
029: import java.util.Set;
030: import javax.accessibility.AccessibleRole;
031: import javax.swing.plaf.LabelUI;
032:
033: public class JLabelTest extends BasicSwingTestCase {
034: private JLabel label;
035:
036: private TestPropertyChangeListener listener;
037:
038: public JLabelTest(final String name) {
039: super (name);
040: }
041:
042: @Override
043: protected void setUp() throws Exception {
044: label = new JLabel();
045: listener = new TestPropertyChangeListener();
046: label.addPropertyChangeListener(listener);
047: }
048:
049: @Override
050: protected void tearDown() throws Exception {
051: label = null;
052: }
053:
054: public void testJLabel() throws Exception {
055: label = new JLabel();
056: assertEquals(SwingConstants.CENTER, label
057: .getVerticalAlignment());
058: assertEquals(SwingConstants.LEADING, label
059: .getHorizontalAlignment());
060: assertEquals("", label.getText());
061: assertTrue(label.getAlignmentX() == 0);
062: assertTrue(label.getAlignmentY() == 0.5);
063: final Icon icon = createTestIcon();
064: label = new JLabel(icon);
065: assertEquals(SwingConstants.CENTER, label
066: .getVerticalAlignment());
067: assertEquals(SwingConstants.CENTER, label
068: .getHorizontalAlignment());
069: assertNull(label.getText());
070: assertTrue(label.getAlignmentX() == 0);
071: assertTrue(label.getAlignmentY() == 0.5);
072: label = new JLabel(icon, SwingConstants.RIGHT);
073: assertEquals(SwingConstants.CENTER, label
074: .getVerticalAlignment());
075: assertEquals(SwingConstants.RIGHT, label
076: .getHorizontalAlignment());
077: assertNull(label.getText());
078: assertTrue(label.getAlignmentX() == 0);
079: assertTrue(label.getAlignmentY() == 0.5);
080: label = new JLabel("any");
081: assertEquals(SwingConstants.CENTER, label
082: .getVerticalAlignment());
083: assertEquals(SwingConstants.LEADING, label
084: .getHorizontalAlignment());
085: assertTrue(label.getAlignmentX() == 0);
086: assertTrue(label.getAlignmentY() == 0.5);
087: label = new JLabel("any", SwingConstants.TRAILING);
088: assertEquals(SwingConstants.CENTER, label
089: .getVerticalAlignment());
090: assertEquals(SwingConstants.TRAILING, label
091: .getHorizontalAlignment());
092: assertTrue(label.getAlignmentX() == 0);
093: assertTrue(label.getAlignmentY() == 0.5);
094: label = new JLabel("any", icon, SwingConstants.RIGHT);
095: assertEquals(SwingConstants.CENTER, label
096: .getVerticalAlignment());
097: assertEquals(SwingConstants.RIGHT, label
098: .getHorizontalAlignment());
099: assertEquals(SwingConstants.TRAILING, label
100: .getHorizontalTextPosition());
101: assertTrue(label.getAlignmentX() == 0);
102: assertTrue(label.getAlignmentY() == 0.5);
103: testExceptionalCase(new IllegalArgumentCase() {
104: @Override
105: public void exceptionalAction() throws Exception {
106: new JLabel(icon, SwingConstants.BOTTOM);
107: }
108: });
109: testExceptionalCase(new IllegalArgumentCase() {
110: @Override
111: public void exceptionalAction() throws Exception {
112: new JLabel("any", SwingConstants.TOP);
113: }
114: });
115: testExceptionalCase(new IllegalArgumentCase() {
116: @Override
117: public void exceptionalAction() throws Exception {
118: new JLabel("any", icon, SwingConstants.TOP);
119: }
120: });
121: }
122:
123: public void testCheckHorizontalKey() throws Exception {
124: JLabel label = new JLabel();
125: label.checkHorizontalKey(SwingConstants.LEFT, null);
126: label.checkHorizontalKey(SwingConstants.CENTER, null);
127: label.checkHorizontalKey(SwingConstants.RIGHT, null);
128: label.checkHorizontalKey(SwingConstants.LEADING, null);
129: label.checkHorizontalKey(SwingConstants.TRAILING, null);
130: checkHorizontalKey(label, SwingConstants.BOTTOM);
131: checkHorizontalKey(label, SwingConstants.TOP);
132: }
133:
134: public void testCheckVerticalKey() throws Exception {
135: JLabel label = new JLabel();
136: label.checkVerticalKey(SwingConstants.TOP, null);
137: label.checkVerticalKey(SwingConstants.CENTER, null);
138: label.checkVerticalKey(SwingConstants.BOTTOM, null);
139: checkVerticalKey(label, SwingConstants.LEADING);
140: checkVerticalKey(label, SwingConstants.TRAILING);
141: checkVerticalKey(label, SwingConstants.LEFT);
142: checkVerticalKey(label, SwingConstants.RIGHT);
143: }
144:
145: public void testGetAccessibleContext() throws Exception {
146: assertNotNull(label.getAccessibleContext());
147: assertEquals(AccessibleRole.LABEL, label.getAccessibleContext()
148: .getAccessibleRole());
149: }
150:
151: public void testGetSetIcons() throws Exception {
152: assertNull(label.getIcon());
153: assertNull(label.getDisabledIcon());
154: Icon icon = createTestIcon();
155: label.setIcon(icon);
156: assertEquals(icon, label.getIcon());
157: assertNotNull(label.getDisabledIcon());
158: assertNotSame(icon, label.getDisabledIcon());
159: assertEquals("new instances should not be produced", label
160: .getDisabledIcon(), label.getDisabledIcon());
161: label.setDisabledIcon(null);
162: assertNotNull(label.getDisabledIcon());
163: label.setIcon(new Icon() {
164: public void paintIcon(final Component c, final Graphics g,
165: final int x, final int y) {
166: }
167:
168: public int getIconWidth() {
169: return 10;
170: }
171:
172: public int getIconHeight() {
173: return 10;
174: }
175: });
176: assertNull(label.getDisabledIcon());
177: TestPropertyChangeListener listener = new TestPropertyChangeListener();
178: label.addPropertyChangeListener(listener);
179: label.setDisabledIcon(icon);
180: assertEquals(icon, label.getDisabledIcon());
181: assertTrue(listener.isPropertyChanged("disabledIcon"));
182: }
183:
184: public void testGetSetDisplayedMnemonic() throws Exception {
185: listener.reset();
186: label.setDisplayedMnemonic('a');
187: assertEquals(KeyEvent.VK_A, label.getDisplayedMnemonic());
188: label.setDisplayedMnemonic(KeyEvent.VK_B);
189: assertEquals(KeyEvent.VK_B, label.getDisplayedMnemonic());
190: assertTrue(listener.isPropertyChanged("displayedMnemonic"));
191: label.setLabelFor(new JButton());
192: assertNull(label.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
193: .get(
194: KeyStroke.getKeyStroke(KeyEvent.VK_A,
195: InputEvent.ALT_DOWN_MASK)));
196: assertNotNull(label.getInputMap(
197: JComponent.WHEN_IN_FOCUSED_WINDOW).get(
198: KeyStroke.getKeyStroke(KeyEvent.VK_B,
199: InputEvent.ALT_DOWN_MASK)));
200: }
201:
202: public void testGetSetDisplayedMnemonicIndex() throws Exception {
203: listener.reset();
204: label = new JLabel("abcd");
205: label.addPropertyChangeListener(listener);
206: assertEquals(-1, label.getDisplayedMnemonicIndex());
207: label.setDisplayedMnemonic('a');
208: assertEquals(0, label.getDisplayedMnemonicIndex());
209: assertTrue(listener.isPropertyChanged("displayedMnemonicIndex"));
210: label.setDisplayedMnemonic('e');
211: assertEquals(-1, label.getDisplayedMnemonicIndex());
212: assertTrue(listener.isPropertyChanged("displayedMnemonicIndex"));
213: listener.reset();
214: label.setDisplayedMnemonicIndex(3);
215: assertEquals(3, label.getDisplayedMnemonicIndex());
216: assertTrue(listener.isPropertyChanged("displayedMnemonicIndex"));
217: label.setDisplayedMnemonicIndex(-1);
218: assertEquals(-1, label.getDisplayedMnemonicIndex());
219: label.setText(null);
220: label.setDisplayedMnemonicIndex(-1);
221: assertEquals(-1, label.getDisplayedMnemonicIndex());
222: testExceptionalCase(new IllegalArgumentCase() {
223: @Override
224: public void exceptionalAction() throws Exception {
225: label.setDisplayedMnemonicIndex(5);
226: }
227: });
228: testExceptionalCase(new IllegalArgumentCase() {
229: @Override
230: public void exceptionalAction() throws Exception {
231: label.setDisplayedMnemonicIndex(-10);
232: }
233: });
234: }
235:
236: public void testGetSetHorizontalAlignment() throws Exception {
237: assertEquals(SwingConstants.LEADING, label
238: .getHorizontalAlignment());
239: label.setHorizontalAlignment(SwingConstants.RIGHT);
240: assertEquals(SwingConstants.RIGHT, label
241: .getHorizontalAlignment());
242: assertTrue(listener.isPropertyChanged("horizontalAlignment"));
243: label = new JLabel(createTestIcon());
244: assertEquals(SwingConstants.CENTER, label
245: .getHorizontalAlignment());
246: testExceptionalCase(new IllegalArgumentCase() {
247: @Override
248: public void exceptionalAction() throws Exception {
249: label.setHorizontalAlignment(SwingConstants.BOTTOM);
250: }
251: });
252: }
253:
254: public void testGetSetHorizontalTextPosition() throws Exception {
255: assertEquals(SwingConstants.TRAILING, label
256: .getHorizontalTextPosition());
257: label.setHorizontalTextPosition(SwingConstants.RIGHT);
258: assertEquals(SwingConstants.RIGHT, label
259: .getHorizontalTextPosition());
260: assertTrue(listener.isPropertyChanged("horizontalTextPosition"));
261: testExceptionalCase(new IllegalArgumentCase() {
262: @Override
263: public void exceptionalAction() throws Exception {
264: label.setHorizontalTextPosition(SwingConstants.BOTTOM);
265: }
266: });
267: }
268:
269: public void testGetSetIconTextGap() throws Exception {
270: assertEquals(4, label.getIconTextGap());
271: label.setIconTextGap(7);
272: assertEquals(7, label.getIconTextGap());
273: assertTrue(listener.isPropertyChanged("iconTextGap"));
274: }
275:
276: public void testGetSetLabelFor() throws Exception {
277: assertNull(label.getLabelFor());
278: JComponent c = new JPanel();
279: label.setLabelFor(c);
280: assertEquals(c, label.getLabelFor());
281: assertTrue(listener.isPropertyChanged("labelFor"));
282: }
283:
284: public void testGetSetText() throws Exception {
285: assertEquals("", label.getText());
286: String text = "any";
287: label.setText(text);
288: assertEquals(text, label.getText());
289: assertTrue(listener.isPropertyChanged("text"));
290: label = new JLabel(text);
291: assertEquals(text, label.getText());
292: label.setDisplayedMnemonic('y');
293: assertEquals('Y', label.getDisplayedMnemonic());
294: assertEquals(2, label.getDisplayedMnemonicIndex());
295: label.setText("handy");
296: assertEquals('Y', label.getDisplayedMnemonic());
297: assertEquals(4, label.getDisplayedMnemonicIndex());
298: label.setText("ok");
299: assertEquals('Y', label.getDisplayedMnemonic());
300: assertEquals(-1, label.getDisplayedMnemonicIndex());
301: }
302:
303: public void testGetSetUpdateUI() throws Exception {
304: LabelUI defaultUI = label.getUI();
305: assertNotNull(defaultUI);
306: LabelUI ui = new LabelUI() {
307: };
308: label.setUI(ui);
309: assertEquals(ui, label.getUI());
310: label.updateUI();
311: assertEquals(defaultUI, label.getUI());
312: }
313:
314: public void testGetUIClassID() throws Exception {
315: assertEquals("LabelUI", label.getUIClassID());
316: }
317:
318: public void testGetSetVerticalAlignment() throws Exception {
319: assertEquals(SwingConstants.CENTER, label
320: .getVerticalAlignment());
321: label.setVerticalAlignment(SwingConstants.TOP);
322: assertEquals(SwingConstants.TOP, label.getVerticalAlignment());
323: assertTrue(listener.isPropertyChanged("verticalAlignment"));
324: testExceptionalCase(new IllegalArgumentCase() {
325: @Override
326: public void exceptionalAction() throws Exception {
327: label.setVerticalAlignment(SwingConstants.RIGHT);
328: }
329: });
330: }
331:
332: public void testGetSetVerticalTextPosition() throws Exception {
333: assertEquals(SwingConstants.CENTER, label
334: .getVerticalTextPosition());
335: label.setVerticalTextPosition(SwingConstants.TOP);
336: assertEquals(SwingConstants.TOP, label
337: .getVerticalTextPosition());
338: assertTrue(listener.isPropertyChanged("verticalTextPosition"));
339: testExceptionalCase(new IllegalArgumentCase() {
340: @Override
341: public void exceptionalAction() throws Exception {
342: label.setVerticalAlignment(SwingConstants.RIGHT);
343: }
344: });
345: }
346:
347: public void testImageUpdate() throws Exception {
348: Icon icon = createTestIcon();
349: label.setIcon(icon);
350: assertFalse(label.imageUpdate(((ImageIcon) createTestIcon())
351: .getImage(), 0, 0, 0, 0, 0));
352: }
353:
354: public void testMnemonicProcessing() throws Exception {
355: final JFrame frame = new JFrame();
356: final JLabel label = new JLabel("label");
357: final JButton button = new JButton("button");
358: SwingUtilities.invokeAndWait(new Runnable() {
359: public void run() {
360: frame.setSize(100, 100);
361: frame.setLocation(100, 100);
362: frame.getContentPane().add(label);
363: frame.getContentPane().add(button);
364: label.setLabelFor(button);
365: label.setDisplayedMnemonic(KeyEvent.VK_A);
366: label.setFocusable(true);
367: frame.setVisible(true);
368: label.requestFocus();
369: }
370: });
371: SwingUtilities.invokeAndWait(new Runnable() {
372: public void run() {
373: assertEquals(label, KeyboardFocusManager
374: .getCurrentKeyboardFocusManager()
375: .getFocusOwner());
376: label.dispatchEvent(new KeyEvent(label,
377: KeyEvent.KEY_PRESSED, EventQueue
378: .getMostRecentEventTime(),
379: InputEvent.ALT_DOWN_MASK, KeyEvent.VK_A, 'a'));
380: label.dispatchEvent(new KeyEvent(label,
381: KeyEvent.KEY_RELEASED, EventQueue
382: .getMostRecentEventTime(),
383: InputEvent.ALT_DOWN_MASK, KeyEvent.VK_A, 'a'));
384: }
385: });
386: SwingUtilities.invokeAndWait(new Runnable() {
387: public void run() {
388: assertEquals(button, KeyboardFocusManager
389: .getCurrentKeyboardFocusManager()
390: .getFocusOwner());
391: }
392: });
393: frame.dispose();
394: }
395:
396: public void testGetInheritsPopupMenu() throws Exception {
397: // Regression test for HARMONY-2570
398: assertTrue(label.getInheritsPopupMenu());
399: }
400:
401: private void checkHorizontalKey(final JLabel label, final int key) {
402: final String message = "any";
403: testExceptionalCase(new IllegalArgumentCase() {
404: @Override
405: public void exceptionalAction() throws Exception {
406: label.checkHorizontalKey(key, message);
407: }
408:
409: @Override
410: public String expectedExceptionMessage() {
411: return message;
412: }
413: });
414: }
415:
416: private void checkVerticalKey(final JLabel label, final int key) {
417: final String message = "any";
418: testExceptionalCase(new IllegalArgumentCase() {
419: @Override
420: public void exceptionalAction() throws Exception {
421: label.checkVerticalKey(key, message);
422: }
423:
424: @Override
425: public String expectedExceptionMessage() {
426: return message;
427: }
428: });
429: }
430:
431: private Icon createTestIcon() {
432: return new ImageIcon(new BufferedImage(16, 16,
433: BufferedImage.TYPE_INT_BGR));
434: }
435:
436: private class TestPropertyChangeListener implements
437: PropertyChangeListener {
438: private Set<String> changedPropertyNames = new HashSet<String>();
439:
440: public void propertyChange(final PropertyChangeEvent event) {
441: changedPropertyNames.add(event.getPropertyName());
442: }
443:
444: public void reset() {
445: changedPropertyNames.clear();
446: }
447:
448: public boolean isPropertyChanged(final String name) {
449: return changedPropertyNames.contains(name);
450: }
451: }
452: }
|