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.Container;
021: import java.awt.EventQueue;
022: import java.awt.FocusTraversalPolicy;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.FocusListener;
025:
026: import javax.swing.plaf.ComponentUI;
027: import javax.swing.plaf.basic.BasicLookAndFeel;
028:
029: public class JComponent_MultithreadedTest extends BasicSwingTestCase {
030: protected JComponent panel;
031:
032: protected JFrame window;
033:
034: class MyInputVerifier extends InputVerifier {
035: public boolean invoked = false;
036:
037: private boolean returnedValue = true;
038:
039: MyInputVerifier() {
040: }
041:
042: MyInputVerifier(final boolean returnedValue) {
043: this .returnedValue = returnedValue;
044: }
045:
046: @Override
047: public boolean verify(final JComponent input) {
048: invoked = true;
049: return returnedValue;
050: }
051: }
052:
053: class RunnableResulted implements Runnable {
054: public boolean result = false;
055:
056: public void run() {
057: result = true;
058: }
059: }
060:
061: /*
062: * Requests focus for the component and waits until it really
063: * becomes focused.
064: */
065: protected boolean requestFocusInWindowForComponent(
066: final JComponent c, int maxWaitTime) throws Exception {
067: FocusListener listener = addFocusListener(c);
068: RunnableResulted thread = new RunnableResulted() {
069: @Override
070: public void run() {
071: result = c.requestFocusInWindow();
072: }
073: };
074: SwingUtilities.invokeAndWait(thread);
075: if (!thread.result) {
076: return false;
077: }
078: synchronized (listener) {
079: listener.wait(maxWaitTime);
080: }
081: waitForIdle();
082: if (!c.isFocusOwner()) {
083: fail();
084: }
085: return true;
086: }
087:
088: /*
089: * Requests focus for the component and waits until it really
090: * becomes focused.
091: */
092: protected boolean requestFocusInWindowForComponent(
093: final JComponent c, final boolean temporarily,
094: int maxWaitTime) throws Exception {
095: FocusListener listener = addFocusListener(c);
096: RunnableResulted thread = new RunnableResulted() {
097: @Override
098: public void run() {
099: result = c.requestFocusInWindow(temporarily);
100: }
101: };
102: SwingUtilities.invokeAndWait(thread);
103: if (!thread.result) {
104: return false;
105: }
106: synchronized (listener) {
107: listener.wait(maxWaitTime);
108: }
109: waitForIdle();
110: if (!c.isFocusOwner()) {
111: fail();
112: }
113: return true;
114: }
115:
116: /*
117: * Requests focus for the component and waits until it really
118: * becomes focused.
119: */
120: protected void requestFocusForComponent(final JComponent c,
121: int maxWaitTime) throws Exception {
122: FocusListener listener = addFocusListener(c);
123: SwingUtilities.invokeAndWait(new Runnable() {
124: public void run() {
125: c.requestFocus();
126: }
127: });
128: synchronized (listener) {
129: listener.wait(maxWaitTime);
130: }
131: waitForIdle();
132: }
133:
134: /*
135: * Requests focus for the component and waits until it really
136: * becomes focused.
137: */
138: protected void requestDefaultFocusForComponent(final JComponent c,
139: final Component newFocusOwner, int maxWaitTime)
140: throws Exception {
141: FocusListener listener = addFocusListener(newFocusOwner);
142: SwingUtilities.invokeAndWait(new Runnable() {
143: public void run() {
144: c.requestDefaultFocus();
145: }
146: });
147: synchronized (listener) {
148: listener.wait(maxWaitTime);
149: }
150: waitForIdle();
151: }
152:
153: /*
154: * Requests focus for the component and waits until it really
155: * becomes focused.
156: */
157: protected void requestFocusForComponent(final JComponent c,
158: final boolean temporarily, int maxWaitTime)
159: throws Exception {
160: FocusListener listener = addFocusListener(c);
161: SwingUtilities.invokeAndWait(new Runnable() {
162: public void run() {
163: c.requestFocus(temporarily);
164: }
165: });
166:
167: synchronized (listener) {
168: listener.wait(maxWaitTime);
169: }
170: waitForIdle();
171: if (!c.isFocusOwner()) {
172: fail();
173: }
174: }
175:
176: /*
177: * Requests focus for the component and waits until it really
178: * becomes focused.
179: */
180: protected void grabFocusForComponent(final JComponent c,
181: int maxWaitTime) throws Exception {
182: FocusListener listener = addFocusListener(c);
183: SwingUtilities.invokeAndWait(new Runnable() {
184: public void run() {
185: c.grabFocus();
186: }
187: });
188:
189: synchronized (listener) {
190: listener.wait(maxWaitTime);
191: }
192: waitForIdle();
193: if (!c.isFocusOwner()) {
194: fail();
195: }
196: }
197:
198: /**
199: * Constructor for JComponentTest_Multithreaded.
200: */
201: public JComponent_MultithreadedTest(final String str) {
202: super (str);
203: }
204:
205: /*
206: * @see TestCase#setUp()
207: */
208: @Override
209: protected void setUp() throws Exception {
210: super .setUp();
211: panel = new JPanel();
212: window = new JFrame();
213: }
214:
215: /*
216: * @see TestCase#tearDown()
217: */
218: @Override
219: protected void tearDown() throws Exception {
220: window.dispose();
221: super .tearDown();
222: }
223:
224: /*
225: * Class under test for void requestFocus()
226: */
227: @SuppressWarnings("deprecation")
228: public void testRequestFocus() throws Exception {
229: MyInputVerifier verifier = new MyInputVerifier();
230: JComponent panel1 = new JPanel();
231: JComponent panel2 = new JPanel();
232: JComponent panel3 = new JPanel();
233: JComponent panel4 = new JPanel();
234: window.getContentPane().add(panel1);
235: window.getContentPane().add(panel2);
236: window.getContentPane().add(panel3);
237: window.getContentPane().add(panel4);
238: window.pack();
239: window.show();
240: waitForIdle();
241: SwingWaitTestCase.requestFocusInWindowForComponent(panel1);
242: panel1.setInputVerifier(verifier);
243: panel2.setVerifyInputWhenFocusTarget(true);
244: requestFocusForComponent(panel2, 1000);
245: assertTrue("verifier's invoked ", verifier.invoked);
246: assertTrue("focus's gained ", panel2.isFocusOwner());
247: verifier.invoked = false;
248: requestFocusForComponent(panel1, 1000);
249: assertTrue("focus's gained ", panel1.isFocusOwner());
250: panel2.setVerifyInputWhenFocusTarget(false);
251: requestFocusForComponent(panel2, 1000);
252: assertFalse("verifier's not invoked ", verifier.invoked);
253: assertTrue("focus's gained ", panel2.isFocusOwner());
254: }
255:
256: /*
257: * Class under test for boolean requestFocusInWindow()
258: */
259: @SuppressWarnings("deprecation")
260: public void testRequestFocusInWindow() throws Exception {
261: MyInputVerifier verifier = new MyInputVerifier();
262: MyInputVerifier verifier2 = new MyInputVerifier(false);
263: JComponent panel1 = new JPanel();
264: JComponent panel2 = new JPanel();
265: JComponent panel3 = new JPanel();
266: JComponent panel4 = new JPanel();
267: window.getContentPane().add(panel1);
268: window.getContentPane().add(panel2);
269: window.getContentPane().add(panel3);
270: window.getContentPane().add(panel4);
271: window.pack();
272: window.show();
273: waitForIdle();
274: SwingWaitTestCase.requestFocusInWindowForComponent(panel1);
275: panel1.setInputVerifier(verifier);
276: panel2.setVerifyInputWhenFocusTarget(true);
277: assertTrue("focus can be gained ",
278: requestFocusInWindowForComponent(panel2, 1000));
279: assertTrue("verifier's invoked ", verifier.invoked);
280: assertTrue("focus's gained ", panel2.isFocusOwner());
281: verifier.invoked = false;
282: assertTrue("focus can be gained ",
283: requestFocusInWindowForComponent(panel1, 1000));
284: assertTrue("focus's gained ", panel1.isFocusOwner());
285: panel2.setVerifyInputWhenFocusTarget(false);
286: assertTrue("focus can be gained ",
287: requestFocusInWindowForComponent(panel2, 1000));
288: assertFalse("verifier's not invoked ", verifier.invoked);
289: assertTrue("focus's gained ", panel2.isFocusOwner());
290: panel1.setVerifyInputWhenFocusTarget(true);
291: panel2.setInputVerifier(verifier2);
292: assertFalse("focus can be gained ",
293: requestFocusInWindowForComponent(panel1, true, 1000));
294: assertTrue("verifier's invoked ", verifier2.invoked);
295: assertFalse("focus's gained ", panel1.isFocusOwner());
296: verifier.invoked = false;
297: }
298:
299: /*
300: * Class under test for boolean requestFocus(boolean)
301: */
302: @SuppressWarnings("deprecation")
303: public void testRequestFocusboolean() throws Exception {
304: MyInputVerifier verifier = new MyInputVerifier();
305: JComponent panel1 = new JPanel();
306: JComponent panel2 = new JPanel();
307: JComponent panel3 = new JPanel();
308: JComponent panel4 = new JPanel();
309: window.getContentPane().add(panel1);
310: window.getContentPane().add(panel2);
311: window.getContentPane().add(panel3);
312: window.getContentPane().add(panel4);
313: window.pack();
314: window.show();
315: waitForIdle();
316: SwingWaitTestCase.requestFocusInWindowForComponent(panel1);
317: panel1.setInputVerifier(verifier);
318: panel2.setVerifyInputWhenFocusTarget(true);
319: requestFocusForComponent(panel2, false, 1000);
320: assertTrue("verifier's invoked ", verifier.invoked);
321: assertTrue("focus's gained ", panel2.isFocusOwner());
322: verifier.invoked = false;
323: requestFocusForComponent(panel1, false, 1000);
324: assertTrue("focus's gained ", panel1.isFocusOwner());
325: panel2.setVerifyInputWhenFocusTarget(false);
326: requestFocusForComponent(panel2, false, 1000);
327: assertFalse("verifier's not invoked ", verifier.invoked);
328: assertTrue("focus's gained ", panel2.isFocusOwner());
329: requestFocusForComponent(panel1, false, 1000);
330: assertEquals("focus's gained ", true, panel1.isFocusOwner());
331: panel2.setVerifyInputWhenFocusTarget(true);
332: requestFocusForComponent(panel2, true, 1000);
333: assertTrue("verifier's invoked ", verifier.invoked);
334: assertTrue("focus's gained ", panel2.isFocusOwner());
335: verifier.invoked = false;
336: requestFocusForComponent(panel1, true, 1000);
337: assertTrue("focus's gained ", panel1.isFocusOwner());
338: panel2.setVerifyInputWhenFocusTarget(false);
339: requestFocusForComponent(panel2, true, 1000);
340: assertFalse("verifier's not invoked ", verifier.invoked);
341: assertTrue("focus's gained ", panel2.isFocusOwner());
342: }
343:
344: /*
345: * Class under test for boolean requestFocusInWindow(boolean)
346: */
347: @SuppressWarnings("deprecation")
348: public void testRequestFocusInWindowboolean() throws Exception {
349: MyInputVerifier verifier = new MyInputVerifier();
350: MyInputVerifier verifier2 = new MyInputVerifier(false);
351: JComponent panel1 = new JPanel();
352: JComponent panel2 = new JPanel();
353: JComponent panel3 = new JPanel();
354: JComponent panel4 = new JPanel();
355: window.getContentPane().add(panel1);
356: window.getContentPane().add(panel2);
357: window.getContentPane().add(panel3);
358: window.getContentPane().add(panel4);
359: window.pack();
360: window.show();
361: waitForIdle();
362: SwingWaitTestCase.requestFocusInWindowForComponent(panel1);
363: panel1.setInputVerifier(verifier);
364: panel2.setVerifyInputWhenFocusTarget(true);
365: assertTrue("focus can be gained ",
366: requestFocusInWindowForComponent(panel2, false, 1000));
367: assertTrue("verifier's invoked ", verifier.invoked);
368: assertTrue("focus's gained ", panel2.isFocusOwner());
369: verifier.invoked = false;
370: assertTrue("focus can be gained ",
371: requestFocusInWindowForComponent(panel1, false, 1000));
372: assertTrue("focus's gained ", panel1.isFocusOwner());
373: panel2.setVerifyInputWhenFocusTarget(false);
374: assertTrue("focus can be gained ",
375: requestFocusInWindowForComponent(panel2, false, 1000));
376: assertFalse("verifier's not invoked ", verifier.invoked);
377: assertTrue("focus's gained ", panel2.isFocusOwner());
378: assertTrue("focus can be gained ",
379: requestFocusInWindowForComponent(panel1, false, 1000));
380: assertTrue("focus's gained ", panel1.isFocusOwner());
381: panel2.setVerifyInputWhenFocusTarget(true);
382: assertTrue("focus can be gained ",
383: requestFocusInWindowForComponent(panel2, true, 1000));
384: assertTrue("verifier's invoked ", verifier.invoked);
385: assertTrue("focus's gained ", panel2.isFocusOwner());
386: verifier.invoked = false;
387: assertTrue("focus can be gained ",
388: requestFocusInWindowForComponent(panel1, true, 1000));
389: assertTrue("focus's gained ", panel1.isFocusOwner());
390: panel2.setVerifyInputWhenFocusTarget(false);
391: assertTrue("focus can be gained ",
392: requestFocusInWindowForComponent(panel2, true, 1000));
393: assertFalse("verifier's not invoked ", verifier.invoked);
394: assertTrue("focus's gained ", panel2.isFocusOwner());
395: panel1.setVerifyInputWhenFocusTarget(true);
396: panel2.setInputVerifier(verifier2);
397: assertFalse("focus can be gained ",
398: requestFocusInWindowForComponent(panel1, true, 1000));
399: assertTrue("verifier's invoked ", verifier2.invoked);
400: assertFalse("focus's gained ", panel1.isFocusOwner());
401: verifier.invoked = false;
402: }
403:
404: @SuppressWarnings("deprecation")
405: public void testGrabFocus() throws Exception {
406: MyInputVerifier verifier = new MyInputVerifier();
407: JComponent panel1 = new JPanel();
408: JComponent panel2 = new JPanel();
409: JComponent panel3 = new JPanel();
410: JComponent panel4 = new JPanel();
411: window.getContentPane().add(panel1);
412: window.getContentPane().add(panel2);
413: window.getContentPane().add(panel3);
414: window.getContentPane().add(panel4);
415: window.pack();
416: window.show();
417: waitForIdle();
418: SwingWaitTestCase.requestFocusInWindowForComponent(panel1);
419: panel1.setInputVerifier(verifier);
420: panel2.setVerifyInputWhenFocusTarget(true);
421: grabFocusForComponent(panel2, 1000);
422: assertTrue("verifier's invoked ", verifier.invoked);
423: assertTrue("focus's gained ", panel2.isFocusOwner());
424: verifier.invoked = false;
425: grabFocusForComponent(panel1, 1000);
426: assertTrue("focus's gained ", panel1.isFocusOwner());
427: panel2.setVerifyInputWhenFocusTarget(false);
428: grabFocusForComponent(panel2, 1000);
429: assertFalse("verifier's not invoked ", verifier.invoked);
430: assertTrue("focus's gained ", panel2.isFocusOwner());
431: }
432:
433: @SuppressWarnings("deprecation")
434: public void testRequestDefaultFocus() throws Exception {
435: final JComponent panel1 = new JPanel();
436: final JComponent panel2 = new JPanel();
437: final JComponent panel3 = new JPanel();
438: final JComponent panel4 = new JPanel(); // this component is to be returned
439: // by our FocusTraversalPolicy()
440: FocusTraversalPolicy policy = new FocusTraversalPolicy() {
441: @Override
442: public Component getComponentAfter(final Container a0,
443: final Component a1) {
444: return null;
445: }
446:
447: @Override
448: public Component getComponentBefore(final Container a0,
449: final Component a1) {
450: return null;
451: }
452:
453: @Override
454: public Component getDefaultComponent(final Container a0) {
455: return panel4;
456: }
457:
458: @Override
459: public Component getFirstComponent(final Container a0) {
460: return null;
461: }
462:
463: @Override
464: public Component getLastComponent(final Container a0) {
465: return null;
466: }
467: };
468: window.getContentPane().add(panel1);
469: window.getContentPane().add(panel2);
470: window.getContentPane().add(panel3);
471: window.getContentPane().add(panel4);
472: window.pack();
473: window.show();
474: waitForIdle();
475: requestDefaultFocusForComponent(panel2, window, 100);
476: assertTrue("focus's gained ", window.isFocusOwner());
477: panel3.setFocusCycleRoot(false);
478: panel3.setFocusTraversalPolicy(policy);
479: requestDefaultFocusForComponent(panel3, window, 100);
480: assertTrue("focus's gained ", window.isFocusOwner());
481: panel3.setFocusCycleRoot(true);
482: requestDefaultFocusForComponent(panel3, panel4, 100);
483: assertTrue("focus's gained ", panel4.isFocusOwner());
484: panel3.setFocusCycleRoot(false);
485: requestDefaultFocusForComponent(panel3, window, 100);
486: assertFalse("focus's gained ", window.isFocusOwner());
487: }
488:
489: public void testUpdateUI() throws Exception {
490: LookAndFeel laf = UIManager.getLookAndFeel();
491: try {
492: JButton button = new JButton();
493: BasicLookAndFeel lookAndFeel1 = new BasicLookAndFeel() {
494: private static final long serialVersionUID = 1L;
495:
496: @Override
497: public boolean isSupportedLookAndFeel() {
498: return true;
499: }
500:
501: @Override
502: public boolean isNativeLookAndFeel() {
503: return false;
504: }
505:
506: @Override
507: public String getName() {
508: return "BasicLookAndFeel";
509: }
510:
511: @Override
512: public String getID() {
513: return "BasicLookAndFeel";
514: }
515:
516: @Override
517: public String getDescription() {
518: return "BasicLookAndFeel";
519: }
520: };
521: UIManager.setLookAndFeel(lookAndFeel1);
522: ComponentUI ui = button.getUI();
523: assertTrue("current component's ui is correct ", ui
524: .getClass().getName().endsWith("MetalButtonUI"));
525: button.updateUI();
526: ui = button.getUI();
527: assertTrue("L&F change affected component's ui ", ui
528: .getClass().getName().endsWith("BasicButtonUI"));
529: } finally {
530: UIManager.setLookAndFeel(laf);
531: }
532: }
533:
534: public void testRevalidate() throws Exception {
535: final JButton button = new JButton("test");
536: final JFrame frame = new JFrame();
537: EventQueue.invokeAndWait(new Runnable() {
538: public void run() {
539: frame.getContentPane().add(button);
540: frame.setVisible(true);
541: assertTrue(button.isValid());
542: button.revalidate();
543: assertFalse(button.isValid());
544: }
545: });
546: waitForIdle();
547: EventQueue.invokeAndWait(new Thread());
548: assertTrue(button.isValid());
549: EventQueue.invokeLater(new Runnable() {
550: public void run() {
551: frame.dispose();
552: }
553: });
554: }
555:
556: @SuppressWarnings("deprecation")
557: public void testAddNotify() throws Exception {
558: PropertyChangeController listener = new PropertyChangeController();
559: JButton panel1 = new JButton();
560: JPanel panel2 = new JPanel();
561: JPanel panel3 = new JPanel();
562: panel1.addPropertyChangeListener(listener);
563: window.getContentPane().add(panel2);
564: window.getContentPane().add(panel3);
565: window.pack();
566: window.show();
567: waitForIdle();
568: SwingWaitTestCase.requestFocusInWindowForComponent(panel2);
569: panel2.add(panel1);
570: listener.checkPropertyFired(panel1, "ancestor", null, panel2);
571: listener.reset();
572: panel3.add(panel1);
573: listener.checkPropertyFired(panel1, "ancestor", null, panel3);
574: }
575:
576: @SuppressWarnings("deprecation")
577: public void testRemoveNotify() throws Exception {
578: PropertyChangeController listener = new PropertyChangeController();
579: JPanel panel1 = new JPanel();
580: JPanel panel2 = new JPanel();
581: JPanel panel3 = new JPanel();
582: panel1.addPropertyChangeListener(listener);
583: window.getContentPane().add(panel2);
584: window.getContentPane().add(panel3);
585: window.pack();
586: window.show();
587: waitForIdle();
588: SwingWaitTestCase.requestFocusInWindowForComponent(panel2);
589: panel2.add(panel1);
590: listener.checkPropertyFired(panel1, "ancestor", null, panel2);
591: listener.reset();
592: panel3.add(panel1);
593: listener.checkPropertyFired(panel1, "ancestor", null, panel3);
594: }
595:
596: private FocusListener addFocusListener(final Component c) {
597: FocusListener listener = new FocusListener() {
598: public void focusGained(FocusEvent e) {
599: synchronized (this ) {
600: notifyAll();
601: }
602: }
603:
604: public void focusLost(FocusEvent e) {
605: }
606: };
607: c.addFocusListener(listener);
608: return listener;
609: }
610: }
|