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 Anton Avtamonov, Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.event;
021:
022: import java.awt.Component;
023: import java.awt.event.ComponentEvent;
024: import java.awt.event.ComponentListener;
025: import javax.swing.BasicSwingTestCase;
026: import javax.swing.JButton;
027: import javax.swing.JComponent;
028: import javax.swing.JFrame;
029: import javax.swing.JPanel;
030:
031: public class AncestorEventTest extends BasicSwingTestCase {
032: protected static class TestAncestorListener extends
033: EventsController implements AncestorListener {
034: private static final long serialVersionUID = 1L;
035:
036: public TestAncestorListener() {
037: super (false);
038: }
039:
040: public TestAncestorListener(final boolean verbose) {
041: super (verbose);
042: }
043:
044: public void ancestorAdded(final AncestorEvent e) {
045: addEvent(new Integer(getNumEvents()), e);
046: if (isVerbose()) {
047: System.out.println(">>>>>>> added: owner="
048: + getName(e.getComponent()) + ", ancestor="
049: + getName(e.getAncestor())
050: + ", ancestorParent="
051: + getName(e.getAncestorParent()));
052: }
053: }
054:
055: public void ancestorMoved(final AncestorEvent e) {
056: addEvent(new Integer(getNumEvents()), e);
057: if (isVerbose()) {
058: System.out.println(">>>>>>> moved: owner="
059: + getName(e.getComponent()) + ", ancestor="
060: + getName(e.getAncestor())
061: + ", ancestorParent="
062: + getName(e.getAncestorParent()));
063: }
064: }
065:
066: public void ancestorRemoved(final AncestorEvent e) {
067: addEvent(new Integer(getNumEvents()), e);
068: if (isVerbose()) {
069: System.out.println(">>>>>>> removed: owner="
070: + getName(e.getComponent()) + ", ancestor="
071: + getName(e.getAncestor())
072: + ", ancestorParent="
073: + getName(e.getAncestorParent()));
074: }
075: }
076:
077: @Override
078: public void reset() {
079: super .reset();
080: if (isVerbose()) {
081: System.out.println("listener's been reset");
082: }
083: }
084:
085: public AncestorEvent getEvent() {
086: return (AncestorEvent) getLastEvent();
087: }
088:
089: public int getEventType() {
090: return getEvent().getID();
091: }
092:
093: private String getName(final Object obj) {
094: return obj != null ? obj.getClass().getName() : "null";
095: }
096: }
097:
098: private JPanel ancestor;
099:
100: private JButton component;
101:
102: private TestAncestorListener listener;
103:
104: private JFrame frame;
105:
106: @Override
107: protected void setUp() throws Exception {
108: ancestor = new JPanel();
109: component = new JButton();
110: ancestor.add(component);
111: listener = new TestAncestorListener();
112: component.addAncestorListener(listener);
113: waitForIdle();
114: }
115:
116: @Override
117: protected void tearDown() throws Exception {
118: listener.setVerbose(false);
119: ancestor = null;
120: component = null;
121: listener = null;
122: if (frame != null) {
123: frame.dispose();
124: frame = null;
125: }
126: }
127:
128: public void testAncestorEvent() throws Exception {
129: int id = 123;
130: JComponent source = new JPanel();
131: AncestorEvent event = new AncestorEvent(source, id, null, null);
132: assertEquals(id, event.getID());
133: assertEquals(source, event.getSource());
134: }
135:
136: public void testGetComponent() throws Exception {
137: AncestorEvent event = new AncestorEvent(component, 0, null,
138: null);
139: assertEquals(component, event.getComponent());
140: }
141:
142: public void testGetAncestor() throws Exception {
143: AncestorEvent event = new AncestorEvent(component, 0, ancestor,
144: null);
145: assertEquals(ancestor, event.getAncestor());
146: }
147:
148: public void testGetAncestorParent() throws Exception {
149: AncestorEvent event = new AncestorEvent(component, 0, null,
150: ancestor);
151: assertEquals(ancestor, event.getAncestorParent());
152: }
153:
154: public void testAncestorAddedEventWhenAncestorVisible()
155: throws Exception {
156: frame = createVisibleFrameWithAncestor();
157: ancestor.setVisible(false);
158: component.setVisible(true);
159: waitForIdle();
160: listener.reset();
161: ancestor.setVisible(true);
162: waitForIdle();
163: assertTrue(listener.getNumEvents() >= 1);
164: performChecksInQueue(ancestor, frame.getContentPane(),
165: AncestorEvent.ANCESTOR_ADDED);
166: }
167:
168: public void testAncestorAddedEventWhenComponentVisible()
169: throws Exception {
170: frame = createVisibleFrameWithAncestor();
171: ancestor.setVisible(true);
172: component.setVisible(false);
173: waitForIdle();
174: listener.reset();
175: component.setVisible(true);
176: waitForIdle();
177: assertTrue(listener.getNumEvents() >= 1);
178: performChecksInQueue(component, ancestor,
179: AncestorEvent.ANCESTOR_ADDED);
180: }
181:
182: public void testAncestorAddedEventWhenInvisibleComponentAncestorVisible()
183: throws Exception {
184: frame = createVisibleFrameWithAncestor();
185: ancestor.setVisible(false);
186: component.setVisible(false);
187: waitForIdle();
188: listener.reset();
189: ancestor.setVisible(true);
190: waitForIdle();
191: assertEquals(0, listener.getNumEvents());
192: }
193:
194: public void testAncestorAddedEventWhenInvisibleAncestorComponentVisible()
195: throws Exception {
196: frame = createVisibleFrameWithAncestor();
197: ancestor.setVisible(false);
198: component.setVisible(false);
199: waitForIdle();
200: listener.reset();
201: component.setVisible(true);
202: waitForIdle();
203: assertEquals(0, listener.getNumEvents());
204: }
205:
206: public void testAncestorAddedEventWhenComponentAdded()
207: throws Exception {
208: frame = createVisibleFrameWithAncestor();
209: ancestor.remove(component);
210: waitForIdle();
211: listener.reset();
212: ComponentListener compListener = addComponentListener(component);
213: ancestor.add(component);
214: waitForIdle();
215: synchronized (compListener) {
216: compListener.wait(1000);
217: }
218: assertTrue(listener.getNumEvents() >= 1);
219: performChecksInQueue(component, ancestor,
220: AncestorEvent.ANCESTOR_ADDED);
221: }
222:
223: public void testAncestorAddedEventWhenAncestorAdded()
224: throws Exception {
225: frame = createVisibleFrameWithAncestor();
226: waitForIdle();
227: assertTrue(listener.getNumEvents() >= 3);
228: performChecksInQueue(ancestor.getRootPane(), frame,
229: AncestorEvent.ANCESTOR_MOVED);
230: performChecksInQueue(component, ancestor,
231: AncestorEvent.ANCESTOR_MOVED);
232: if (!isHarmony()) {
233: performChecksInQueue(ancestor, frame.getContentPane(),
234: AncestorEvent.ANCESTOR_ADDED);
235: } else {
236: performChecksInQueue(frame, null,
237: AncestorEvent.ANCESTOR_ADDED);
238: }
239: }
240:
241: public void testAncestorMovedEventWhenAncestorInvisibleAncestorMoved()
242: throws Exception {
243: frame = createVisibleFrameWithAncestor();
244: ancestor.setBounds(10, 10, 10, 10);
245: ancestor.setVisible(false);
246: waitForIdle();
247: listener.reset();
248: ComponentListener compListener = addComponentListener(component);
249: ancestor.setBounds(20, 20, 20, 20);
250: waitForIdle();
251: synchronized (compListener) {
252: compListener.wait(1000);
253: }
254: assertTrue(listener.getNumEvents() >= 1);
255: performChecksInQueue(ancestor, frame.getContentPane(),
256: AncestorEvent.ANCESTOR_MOVED);
257: }
258:
259: public void testAncestorMovedEventWhenComponentInvisibleAncestorMoved()
260: throws Exception {
261: frame = createVisibleFrameWithAncestor();
262: ancestor.setBounds(10, 10, 10, 10);
263: component.setVisible(false);
264: waitForIdle();
265: listener.reset();
266: ComponentListener compListener = addComponentListener(component);
267: ancestor.setBounds(20, 20, 20, 20);
268: waitForIdle();
269: synchronized (compListener) {
270: compListener.wait(1000);
271: }
272: assertEquals(0, listener.getNumEvents());
273: }
274:
275: public void testAncestorMovedEventWhenAncestorInvisibleComponentMoved()
276: throws Exception {
277: frame = createVisibleFrameWithAncestor();
278: component.setBounds(10, 10, 10, 10);
279: ancestor.setVisible(false);
280: waitForIdle();
281: listener.reset();
282: ComponentListener compListener = addComponentListener(component);
283: component.setBounds(20, 20, 20, 20);
284: waitForIdle();
285: synchronized (compListener) {
286: compListener.wait(1000);
287: }
288: assertTrue(listener.getNumEvents() >= 1);
289: performChecksInQueue(component, ancestor,
290: AncestorEvent.ANCESTOR_MOVED);
291: }
292:
293: public void testAncestorMovedEventWhenComponentInvisibleComponentMoved()
294: throws Exception {
295: frame = createVisibleFrameWithAncestor();
296: component.setBounds(10, 10, 10, 10);
297: component.setVisible(false);
298: waitForIdle();
299: listener.reset();
300: ComponentListener compListener = addComponentListener(component);
301: component.setBounds(20, 20, 20, 20);
302: waitForIdle();
303: synchronized (compListener) {
304: compListener.wait(1000);
305: }
306: assertEquals(1, listener.getNumEvents());
307: performChecks(component, ancestor, AncestorEvent.ANCESTOR_MOVED);
308: }
309:
310: public void testAncestorMovedEventWhenAncestorMoved()
311: throws Exception {
312: frame = createVisibleFrameWithAncestor();
313: ancestor.setBounds(10, 10, 10, 10);
314: waitForIdle();
315: listener.reset();
316: ComponentListener compListener = addComponentListener(component);
317: ancestor.setBounds(20, 20, 20, 20);
318: waitForIdle();
319: synchronized (compListener) {
320: compListener.wait(1000);
321: }
322: assertTrue(listener.getNumEvents() >= 1);
323: performChecksInQueue(ancestor, frame.getContentPane(),
324: AncestorEvent.ANCESTOR_MOVED);
325: }
326:
327: public void testAncestorMovedEventWhenComponentMoved()
328: throws Exception {
329: component.setSize(200, 200);
330: waitForIdle();
331: assertEquals(0, listener.getNumEvents());
332: ComponentListener compListener = addComponentListener(component);
333: component.setLocation(20, 20);
334: waitForIdle();
335: synchronized (compListener) {
336: compListener.wait(1000);
337: }
338: assertEquals(1, listener.getNumEvents());
339: performChecks(component, ancestor, AncestorEvent.ANCESTOR_MOVED);
340: }
341:
342: public void testAncestorRemovedEventWhenComponentInvisible()
343: throws Exception {
344: frame = createVisibleFrameWithAncestor();
345: waitForIdle();
346: listener.reset();
347: component.setVisible(false);
348: waitForIdle();
349: assertEquals(1, listener.getNumEvents());
350: performChecks(component, ancestor,
351: AncestorEvent.ANCESTOR_REMOVED);
352: }
353:
354: public void testAncestorRemovedEventWhenComponentRemoved()
355: throws Exception {
356: frame = createVisibleFrameWithAncestor();
357: waitForIdle();
358: listener.reset();
359: ancestor.remove(component);
360: waitForIdle();
361: assertEquals(1, listener.getNumEvents());
362: performChecks(component, ancestor,
363: AncestorEvent.ANCESTOR_REMOVED);
364: }
365:
366: public void testAncestorRemovedEventWhenAncestorInvisible()
367: throws Exception {
368: frame = createVisibleFrameWithAncestor();
369: waitForIdle();
370: listener.reset();
371: ancestor.setVisible(false);
372: waitForIdle();
373: assertTrue(listener.getNumEvents() >= 1);
374: performChecksInQueue(ancestor, frame.getContentPane(),
375: AncestorEvent.ANCESTOR_REMOVED);
376: }
377:
378: public void testAncestorRemovedEventWhenAncestorRemoved()
379: throws Exception {
380: frame = createVisibleFrameWithAncestor();
381: waitForIdle();
382: listener.reset();
383: frame.getContentPane().remove(ancestor);
384: waitForIdle();
385: assertEquals(1, listener.getNumEvents());
386: if (isHarmony()) {
387: performChecks(ancestor, frame.getContentPane(),
388: AncestorEvent.ANCESTOR_REMOVED);
389: }
390: }
391:
392: private JFrame createVisibleFrameWithAncestor() {
393: JFrame result = new JFrame();
394: result.getContentPane().add(ancestor);
395: result.setSize(200, 200);
396: result.setVisible(true);
397: return result;
398: }
399:
400: private void performChecks(final Component ancestor,
401: final Component ancestorParent, final int eventType) {
402: final AncestorEvent event = listener.getEvent();
403: performChecks(event, ancestor, ancestorParent, eventType);
404: }
405:
406: private void performChecksInQueue(final Component ancestor,
407: final Component ancestorParent, final int eventType) {
408: AncestorEvent event = null;
409: for (int i = 0; i < listener.getNumEvents(); i++) {
410: final Integer key = new Integer(i);
411: AncestorEvent e = (AncestorEvent) listener.getEvent(key);
412: if (e != null && e.getID() == eventType
413: && e.getAncestor() == ancestor) {
414: event = e;
415: break;
416: }
417: }
418: assertNotNull("no event is found for given parameters", event);
419: performChecks(event, ancestor, ancestorParent, eventType);
420: }
421:
422: private void performChecks(final AncestorEvent event,
423: final Component ancestor, final Component ancestorParent,
424: final int eventType) {
425: assertNotNull(event);
426: assertEquals("source", component, event.getSource());
427: assertEquals("component", component, event.getComponent());
428: assertEquals("eventType", eventType, event.getID());
429: assertEquals("ancestor", ancestor, event.getAncestor());
430: assertEquals("ancestorParent", ancestorParent, event
431: .getAncestorParent());
432: }
433:
434: private ComponentListener addComponentListener(final Component c) {
435: final ComponentListener compListener = new ComponentListener() {
436: public void componentMoved(final ComponentEvent e) {
437: synchronized (this ) {
438: notifyAll();
439: }
440: }
441:
442: public void componentResized(final ComponentEvent e) {
443: }
444:
445: public void componentShown(final ComponentEvent e) {
446: }
447:
448: public void componentHidden(final ComponentEvent e) {
449: }
450: };
451: c.addComponentListener(compListener);
452: return compListener;
453: }
454: }
|