001: package abbot.finder;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006:
007: import junit.extensions.abbot.*;
008: import abbot.Log;
009: import abbot.tester.*;
010: import abbot.util.AWT;
011: import abbot.finder.matchers.*;
012:
013: public class TestHierarchyTest extends ComponentTestFixture {
014:
015: private TestHierarchy hierarchy;
016:
017: protected Hierarchy createHierarchy() {
018: return new TestHierarchy();
019: }
020:
021: protected void setUp() {
022: hierarchy = (TestHierarchy) getHierarchy();
023: Log.addDebugClass(TestHierarchy.class);
024: Log.setShowThreads(true);
025: }
026:
027: protected void hideWindow(Window w) {
028: // Explicitly wait for the window close event
029: // before checking for results to avoid timing errors; not sure why
030: // there's a delay though (waitForIdle doesn't work in this case).
031: class Flag {
032: volatile boolean closed;
033: }
034: final Flag flag = new Flag();
035: w.addWindowListener(new WindowAdapter() {
036: public void windowClosed(WindowEvent e) {
037: e.getWindow().removeWindowListener(this );
038: flag.closed = true;
039: }
040: });
041: super .hideWindow(w);
042: while (!flag.closed) {
043: getRobot().sleep();
044: }
045: }
046:
047: public void testFilterNewIgnoredWindows() throws Exception {
048: TestHierarchy hierarchy = new TestHierarchy();
049: Frame f = showFrame(new JLabel(getName()));
050: assertTrue("Frame should be in hierarchy", hierarchy
051: .contains(f));
052:
053: hierarchy.dispose(f);
054: showWindow(f);
055: assertTrue("Frame should no longer appear in hierarchy",
056: !hierarchy.contains(f));
057:
058: JDialog d = new JDialog(f, getName());
059: d.getContentPane().add(new JLabel("dialog"));
060: showWindow(d);
061: assertTrue(
062: "Filtered frame dialog should not appear in hierarchy",
063: !hierarchy.contains(d));
064:
065: disposeWindow(d);
066: assertTrue("Disposed dialog should not appear in hierarhcy",
067: !hierarchy.contains(d));
068:
069: showWindow(d);
070: assertTrue("Redisplayed dialog should not appear in hierarchy",
071: !hierarchy.contains(d));
072: }
073:
074: public void testAutoFilterDisposedWindows() throws Exception {
075: JButton openButton = new JButton("open");
076: final Frame f = showFrame(openButton);
077: class Flag {
078: volatile boolean flag = true;
079: }
080: final Flag flag = new Flag();
081: final String CLOSE = "close";
082: class TestDialog extends JDialog {
083: public TestDialog(final boolean dispose) {
084: super (f, TestHierarchyTest.this .getName());
085: JButton close = new JButton(CLOSE);
086: close.setName(CLOSE);
087: getContentPane().add(close);
088: close.addActionListener(new ActionListener() {
089: public void actionPerformed(ActionEvent e) {
090: if (dispose)
091: TestDialog.this .dispose();
092: else
093: TestDialog.this .setVisible(false);
094: }
095: });
096: setModal(true);
097: }
098: }
099: openButton.addActionListener(new ActionListener() {
100: public void actionPerformed(ActionEvent e) {
101: JDialog d = new TestDialog(flag.flag);
102: d.pack();
103: d.setVisible(true);
104: }
105: });
106: ComponentTester tester = new ComponentTester();
107: tester.actionClick(openButton);
108: JButton closeButton = (JButton) getFinder().find(
109: new NameMatcher(CLOSE));
110: tester.actionClick(closeButton);
111:
112: // This will bring up a new dialog; matching should match the new
113: // dialog
114: tester.actionClick(openButton);
115: JButton closeButton2 = (JButton) getFinder().find(
116: new NameMatcher(CLOSE));
117: assertTrue("Should pick up new button, not old one: "
118: + closeButton, !closeButton2.equals(closeButton));
119: tester.actionClick(closeButton2);
120:
121: // Now don't do the dispose, and we expect to always get the same match
122: flag.flag = false;
123: tester.actionClick(openButton);
124: closeButton = (JButton) getFinder()
125: .find(new NameMatcher(CLOSE));
126: tester.actionClick(closeButton);
127:
128: tester.actionClick(openButton);
129: closeButton2 = (JButton) getFinder().find(
130: new NameMatcher(CLOSE));
131: assertEquals("Second lookup should match first", closeButton,
132: closeButton2);
133: }
134:
135: public void testFilterMenuItem() throws Exception {
136: JFrame frame = new JFrame(getName());
137: JMenuBar mb = new JMenuBar();
138: JMenu menu = new JMenu("File");
139: JMenuItem mi = new JMenuItem("Item");
140: menu.add(mi);
141: mb.add(menu);
142: frame.setJMenuBar(mb);
143: showWindow(frame);
144: hierarchy.dispose(frame);
145: assertTrue("Hierarchy should no longer contain frame",
146: !hierarchy.contains(frame));
147: assertTrue("Hierarchy should no longer contain menu bar",
148: !hierarchy.contains(mb));
149: assertTrue("Hierarchy should no longer contain menu",
150: !hierarchy.contains(menu));
151: assertTrue("Hierarchy should no longer contain menu item",
152: !hierarchy.contains(mi));
153: }
154:
155: public void testAutoRemoveFileChooserDialogs() throws Exception {
156: final JFileChooser chooser = new JFileChooser();
157: final Frame frame = showFrame(new JLabel(getName()));
158: Dialog d1 = showModalDialog(new Runnable() {
159: public void run() {
160: chooser.showOpenDialog(frame);
161: }
162: });
163: hideWindow(d1);
164: assertTrue(
165: "Transient file chooser dialog should now be filtered: "
166: + d1.getName(), hierarchy.isFiltered(d1));
167: assertTrue(
168: "Transient file chooser dialog should no longer be in the hierarchy",
169: !hierarchy.contains(d1));
170: }
171:
172: public void testAutoRemoveJOptionPaneShowConfirm() throws Exception {
173: final JLabel confirm = new JLabel("confirm");
174: Runnable r = new Runnable() {
175: public void run() {
176: JOptionPane.showConfirmDialog(null, confirm);
177: }
178: };
179: Dialog d = showModalDialog(r);
180: hideWindow(d);
181: assertTrue(
182: "Transient option pane dialog should now be filtered: "
183: + d.getName(), hierarchy.isFiltered(d));
184: assertTrue(
185: "Transient option pane dialog should no longer be in the hierarchy",
186: !hierarchy.contains(d));
187: }
188:
189: public void testAutoRemoveJOptionPaneShowInput() throws Exception {
190: final JLabel input = new JLabel("input");
191: Runnable r = new Runnable() {
192: public void run() {
193: JOptionPane.showInputDialog(null, input);
194: }
195: };
196: Dialog d = showModalDialog(r);
197: hideWindow(d);
198: assertTrue(
199: "Transient option pane dialog should now be filtered: "
200: + d.getName(), hierarchy.isFiltered(d));
201: assertTrue(
202: "Transient option pane dialog should no longer be in the hierarchy",
203: !hierarchy.contains(d));
204: }
205:
206: public void testAutoRemoveJOptionPaneShowMessage() throws Exception {
207: final JLabel message = new JLabel("message");
208: Runnable r = new Runnable() {
209: public void run() {
210: JOptionPane.showMessageDialog(null, message);
211: }
212: };
213: Dialog d = showModalDialog(r);
214: hideWindow(d);
215: assertTrue(
216: "Transient option pane dialog should now be filtered: "
217: + d.getName(), hierarchy.isFiltered(d));
218: assertTrue(
219: "Transient option pane dialog should no longer be in the hierarchy",
220: !hierarchy.contains(d));
221: }
222:
223: // 1.4+ only
224: private Popup p1, p2;
225:
226: public void testFindReusedPopup() {
227: final JFrame frame = (JFrame) showFrame(new JTextField(
228: getName()));
229: final JLabel message = new JLabel(getName());
230: message.setSize(message.getPreferredSize());
231: final PopupFactory f = PopupFactory.getSharedInstance();
232: invokeAndWait(new Runnable() {
233: public void run() {
234: p1 = f.getPopup(frame.getContentPane(), message, frame
235: .getX()
236: + frame.getWidth(), frame.getY());
237: p1.show();
238: }
239: });
240: try {
241: Component c = getFinder().find(
242: new ClassMatcher(JLabel.class));
243: assertEquals("Wrong component found", message, c);
244: } catch (Exception e) {
245: fail("Popup and contents not found");
246: }
247: // force a redisplay before the dispose of the first gets processed
248: invokeAndWait(new Runnable() {
249: public void run() {
250: p1.hide();
251: p2 = f.getPopup(frame.getContentPane(), message, frame
252: .getX()
253: + frame.getWidth(), frame.getY());
254: p2.show();
255: }
256: });
257: //getRobot().delay(300000);
258: try {
259: Component c = getFinder().find(
260: new ClassMatcher(JLabel.class));
261: assertEquals("Wrong component found after hide/show",
262: message, c);
263: } catch (Exception e) {
264: fail("Popup and contents not found after hide/show");
265: }
266: }
267:
268: public static void main(String[] args) {
269: RepeatHelper.runTests(args, TestHierarchyTest.class);
270: }
271: }
|