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 Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.EventQueue;
023: import java.awt.Toolkit;
024: import org.apache.harmony.x.swing.StringConstants;
025:
026: public class JComponent_AddRemoveNotifyTest extends BasicSwingTestCase {
027: private JPanel ancestorParent;
028:
029: private JPanel ancestor;
030:
031: private JButton component;
032:
033: private PropertyChangeController listener;
034:
035: private JFrame frame;
036:
037: @Override
038: protected void setUp() throws Exception {
039: ancestorParent = new JPanel();
040: ancestor = new JPanel();
041: component = new JButton();
042: ancestor.add(component);
043: listener = new PropertyChangeController();
044: component.addPropertyChangeListener(
045: StringConstants.ANCESTOR_PROPERTY_NAME, listener);
046: }
047:
048: @Override
049: protected void tearDown() throws Exception {
050: listener.setVerbose(false);
051: ancestor = null;
052: component = null;
053: listener = null;
054: if (frame != null) {
055: frame.dispose();
056: frame = null;
057: }
058: }
059:
060: public void testAddNotifyWhenComponentAdded() throws Exception {
061: frame = createVisibleFrameWithAncestor();
062: ancestor.remove(component);
063: waitEventQueueEmptiness();
064: listener.reset();
065: ancestor.add(component);
066: waitEventQueueEmptiness();
067: assertEquals(1, listener.getNumEvents());
068: listener.checkLastPropertyFired(component,
069: StringConstants.ANCESTOR_PROPERTY_NAME, null, ancestor);
070: }
071:
072: public void testAddNotifyWhenAncestorAdded() throws Exception {
073: frame = createVisibleFrameWithAncestor();
074: waitEventQueueEmptiness();
075: assertEquals(1, listener.getNumEvents());
076: listener.checkLastPropertyFired(component,
077: StringConstants.ANCESTOR_PROPERTY_NAME, null, ancestor);
078: }
079:
080: public void testAddNotifyWhenAncestorParentAdded() throws Exception {
081: frame = createVisibleFrameWithAncestorParent();
082: waitEventQueueEmptiness();
083: assertEquals(1, listener.getNumEvents());
084: listener.checkLastPropertyFired(component,
085: StringConstants.ANCESTOR_PROPERTY_NAME, null, ancestor);
086: }
087:
088: public void testRemoveNotifyWhenComponentRemoved() throws Exception {
089: frame = createVisibleFrameWithAncestor();
090: listener.reset();
091: ancestor.remove(component);
092: waitEventQueueEmptiness();
093: assertEquals(1, listener.getNumEvents());
094: listener.checkLastPropertyFired(component,
095: StringConstants.ANCESTOR_PROPERTY_NAME, ancestor, null);
096: }
097:
098: public void testRemoveNotifyWhenAncestorRemoved() throws Exception {
099: frame = createVisibleFrameWithAncestor();
100: listener.reset();
101: frame.getContentPane().remove(ancestor);
102: waitEventQueueEmptiness();
103: assertEquals(1, listener.getNumEvents());
104: listener.checkLastPropertyFired(component,
105: StringConstants.ANCESTOR_PROPERTY_NAME, ancestor, null);
106: }
107:
108: public void testRemoveNotifyWhenAncestorParentRemoved()
109: throws Exception {
110: frame = createVisibleFrameWithAncestorParent();
111: listener.reset();
112: frame.getContentPane().remove(ancestorParent);
113: waitEventQueueEmptiness();
114: assertEquals(1, listener.getNumEvents());
115: listener.checkLastPropertyFired(component,
116: StringConstants.ANCESTOR_PROPERTY_NAME, ancestor, null);
117: }
118:
119: private JFrame createVisibleFrameWithAncestor() {
120: JFrame result = new JFrame();
121: result.getContentPane().add(ancestor);
122: result.setSize(200, 200);
123: result.setVisible(true);
124: return result;
125: }
126:
127: private JFrame createVisibleFrameWithAncestorParent() {
128: JFrame result = new JFrame();
129: ancestorParent.add(ancestor);
130: result.getContentPane().add(ancestorParent);
131: result.setSize(200, 200);
132: result.setVisible(true);
133: return result;
134: }
135:
136: private void waitEventQueueEmptiness() {
137: try {
138: while (Toolkit.getDefaultToolkit().getSystemEventQueue()
139: .peekEvent() != null
140: || EventQueue.getCurrentEvent() != null) {
141: Thread.sleep(10);
142: }
143: } catch (InterruptedException e) {
144: }
145: }
146: }
|