001: package abbot.util;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005:
006: import javax.swing.*;
007: import javax.swing.border.TitledBorder;
008:
009: import junit.extensions.abbot.*;
010: import junit.framework.AssertionFailedError;
011: import abbot.Log;
012: import abbot.Platform;
013: import abbot.tester.ComponentTester;
014: import abbot.tester.Robot;
015: import abbot.tester.WindowTester;
016:
017: public class AWTTest extends ComponentTestFixture {
018:
019: private static boolean ONLY_HEAVYWEIGHT_POPUPS = Platform.isOSX();
020:
021: public void testDefaultNames() throws Throwable {
022: Class[] classes = { Button.class, Canvas.class, Checkbox.class,
023: Choice.class, Frame.class, List.class, Label.class,
024: Panel.class, Scrollbar.class, TextArea.class,
025: TextField.class, };
026: for (int i = 0; i < classes.length; i++) {
027: Component c = (Component) classes[i].newInstance();
028: assertTrue("Expected default name on " + c, AWT
029: .hasDefaultName(c));
030: c.setName("bobo");
031: assertFalse("Expected no default name on " + c, AWT
032: .hasDefaultName(c));
033: }
034: Frame f = new Frame(getName());
035: Component[] comps = { new Dialog(f), new Window(f),
036: new FileDialog(f), };
037: for (int i = 0; i < comps.length; i++) {
038: assertTrue("Expected default name on " + comps[i], AWT
039: .hasDefaultName(comps[i]));
040: comps[i].setName("bobo");
041: assertTrue("Expected no default name on " + comps[i], !AWT
042: .hasDefaultName(comps[i]));
043: }
044: JFrame jf = new JFrame(getName());
045: JRootPane rp = (JRootPane) jf.getComponent(0);
046: assertEquals("Unexpected root pane name", null, rp.getName());
047: Component c = rp.getComponent(0);
048: assertTrue("Expected default name on glass pane: "
049: + c.getName(), AWT.hasDefaultName(c));
050: c = rp.getComponent(1);
051: assertTrue("Expected default name on layered pane: "
052: + c.getName(), AWT.hasDefaultName(c));
053: c = jf.getContentPane();
054: assertTrue("Expected default name on content pane: "
055: + c.getName(), AWT.hasDefaultName(c));
056: }
057:
058: public void testLightweightTransientPopups() {
059: Frame frame = showFrame(new JLabel(getName()), new Dimension(
060: 200, 200));
061: JPanel pane = (JPanel) ((JFrame) frame).getContentPane();
062: JPopupMenu popup = new JPopupMenu("menu");
063: popup.add(new JMenuItem("item 1"));
064: popup.add(new JMenuItem("item 2"));
065: showPopup(popup, pane, 1, 1);
066: Component container = popup.getParent();
067: Log.debug("Popup parent is " + Robot.toString(container) + " ("
068: + container.getClass() + ")");
069: while (!AWT.isTransientPopup(container)) {
070: if (container.getParent() == null)
071: throw new AssertionFailedError(
072: "No transient popup ancestor to "
073: + Robot.toString(popup) + " (ancestor "
074: + Robot.toString(container) + ")");
075: container = container.getParent();
076: Log.debug("Next ancestor is " + Robot.toString(container));
077: }
078: assertTrue("No transient popup ancestor detected for "
079: + Robot.toString(container), AWT
080: .isTransientPopup(container));
081: // This depends on the platform
082: if (ONLY_HEAVYWEIGHT_POPUPS) {
083: assertTrue("Root expected to be heavyweight", AWT
084: .isHeavyweightPopup(container));
085: } else {
086: assertTrue("Root should be lightweight", AWT
087: .isLightweightPopup(container));
088: }
089: }
090:
091: public void testHeavyweightTransientPopups() {
092: Frame frame = showFrame(new JLabel(getName()), new Dimension(
093: 200, 200));
094: JPanel pane = (JPanel) ((JFrame) frame).getContentPane();
095: JPopupMenu popup = new JPopupMenu("menu");
096: popup.add(new JMenuItem("item 1"));
097: popup.add(new JMenuItem("item 2"));
098: popup.add(new JMenuItem("item 3"));
099: popup.add(new JMenuItem("item 4"));
100: popup.add(new JMenuItem("item 5"));
101: popup.add(new JMenuItem("item 6"));
102: showPopup(popup, pane, 1, pane.getHeight() - 5);
103:
104: Component container = popup.getParent();
105: Log.debug("Popup parent is " + Robot.toString(container) + " ("
106: + container.getClass() + ")");
107: while (!AWT.isTransientPopup(container)) {
108: if (container.getParent() == null)
109: throw new AssertionFailedError(
110: "No transient popup ancestor to "
111: + Robot.toString(popup) + " (ancestor "
112: + Robot.toString(container) + ")");
113: container = container.getParent();
114: Log.debug("Next ancestor is " + Robot.toString(container));
115: }
116: assertTrue("No transient popup ancestor detected for "
117: + Robot.toString(container), AWT
118: .isTransientPopup(container));
119: assertTrue("Container should be a window",
120: container instanceof Window);
121: Window w = (Window) container.getParent();
122: assertFalse("Ancestor " + Robot.toString(w)
123: + " of popup should not be transient", AWT
124: .isTransientPopup(w));
125: assertTrue("Root should be heavyweight", AWT
126: .isHeavyweightPopup(container));
127: assertFalse("Content pane should not think it's lightweight",
128: AWT.isLightweightPopup(((JWindow) container)
129: .getContentPane()));
130: }
131:
132: public void testLightweightPopup() {
133: JPopupMenu menu = new JPopupMenu();
134: menu.add(new JMenuItem("item"));
135: JLabel label = new JLabel(getName());
136: showFrame(label, new Dimension(200, 200));
137: showPopup(menu, label);
138: if (ONLY_HEAVYWEIGHT_POPUPS) {
139: Window w = SwingUtilities.getWindowAncestor(menu);
140: assertTrue("Popup menu expected on a heavyweight popup: "
141: + w, AWT.isHeavyweightPopup(w));
142: } else {
143: assertTrue(
144: "Popup menu parent should be detected as lightweight: "
145: + menu.getParent(), AWT
146: .isLightweightPopup(menu.getParent()));
147: }
148: }
149:
150: public void testIsContentPane() {
151: JFrame f = new JFrame(getName());
152: f.getContentPane().add(new JLabel(getName()));
153: JMenuBar mb = new JMenuBar();
154: mb.add(new JMenu("file"));
155: f.setJMenuBar(mb);
156: showWindow(f);
157: assertTrue("Content pane not detected", AWT.isContentPane(f
158: .getContentPane()));
159: assertFalse("Layered pane is not the content pane", AWT
160: .isContentPane(f.getContentPane().getParent()));
161: assertFalse("Menu bar is not the content pane", AWT
162: .isContentPane(mb));
163: }
164:
165: private void checkChildren(Container c, boolean expected) {
166: Component[] kids = c.getComponents();
167: // All descendents of the root pane are not decorations
168: if (c instanceof JRootPane)
169: expected = false;
170: for (int i = 0; i < kids.length; i++) {
171: if (kids[i] instanceof Container) {
172: checkChildren((Container) kids[i], expected);
173: }
174: }
175: if (expected && !(c instanceof JInternalFrame))
176: assertTrue("Decoration not detected: " + c, AWT
177: .isInternalFrameDecoration(c));
178: else
179: assertFalse("Not a decoration: " + c, AWT
180: .isInternalFrameDecoration(c));
181: }
182:
183: public void testInternalFrameDecorationCheck() {
184: JInternalFrame frame = new JInternalFrame();
185: JLabel label = new JLabel(getName());
186: frame.getContentPane().add(label);
187: checkChildren(frame, true);
188: }
189:
190: public void testEventTypeEnabled() {
191: JFrame frame = new JFrame(getName());
192: Container c = frame.getContentPane();
193: JLabel label = new JLabel(getName());
194: c.add(label);
195: assertFalse("JLabel should not be enabled w/o listeners", AWT
196: .eventTypeEnabled(label, MouseEvent.MOUSE_PRESSED));
197: label.addMouseListener(new MouseAdapter() {
198: });
199: assertTrue("JLabel should be enabled w/listeners", AWT
200: .eventTypeEnabled(label, MouseEvent.MOUSE_PRESSED));
201: }
202:
203: public void testDetectTransientDialogComponents() {
204: JOptionPane pane = new JOptionPane("Dialog",
205: JOptionPane.INFORMATION_MESSAGE);
206: Dialog d = pane.createDialog(null, "Dialog");
207: assertTrue("Dialog is a JOptionPane dialog", AWT
208: .isTransientDialog(d));
209: assertTrue(
210: "Content pane of transient dialog should be transient",
211: AWT.isTransientDialog(((JDialog) d).getContentPane()));
212: assertFalse("Option pane should not be transient", AWT
213: .isTransientDialog(pane));
214: }
215:
216: public void testDetectNonTransientDialog() {
217: Frame frame = showFrame(new JLabel(getName()));
218: JDialog dialog = new JDialog(frame, "dialog");
219: showWindow(dialog);
220: assertTrue("Dialog is not transient", !AWT
221: .isTransientDialog(dialog));
222: }
223:
224: public void testInvokerAndWindow() {
225: JFrame frame = new JFrame(getName());
226: JMenuBar mb = new JMenuBar();
227: JMenu menu = new JMenu("File");
228: JMenu submenu = new JMenu("submenu");
229: JMenuItem mi = new JMenuItem("Item");
230: menu.add(mi);
231: menu.add(submenu);
232: assertEquals("Wrong invoker", menu, AWT.getInvoker(mi));
233: assertEquals("Wrong invoker", menu, AWT.getInvoker(submenu));
234: assertEquals("Wrong invoker", null, AWT.getInvoker(menu));
235: assertEquals("Wrong window", null, AWT.getWindow(mi));
236: assertEquals("Wrong window", null, AWT.getWindow(submenu));
237: assertEquals("Wrong window", null, AWT.getWindow(menu));
238:
239: mb.add(menu);
240: frame.setJMenuBar(mb);
241: showWindow(frame);
242: assertEquals("Wrong window", frame, AWT.getWindow(mi));
243: assertEquals("Wrong window", frame, AWT.getWindow(submenu));
244: assertEquals("Wrong window", frame, AWT.getWindow(menu));
245: }
246:
247: public void testAWTMenuItemPath() {
248: Frame frame = new Frame(getName());
249: MenuBar mb = new MenuBar();
250: Menu menu = new Menu("File");
251: Menu menu2 = new Menu("Edit");
252: MenuItem dupMB1 = new MenuItem("Open");
253: MenuItem dupMB2 = new MenuItem("Open");
254: MenuItem uniqueMB = new MenuItem("Unique");
255: menu.add(dupMB1);
256: menu.add(uniqueMB);
257: menu2.add(dupMB2);
258: mb.add(menu);
259: mb.add(menu2);
260: frame.setMenuBar(mb);
261: PopupMenu p1 = new PopupMenu();
262: MenuItem dup = new MenuItem("Open");
263: MenuItem uniquePopup = new MenuItem("Close");
264: p1.add(dup);
265: p1.add(uniquePopup);
266: PopupMenu p2 = new PopupMenu();
267: MenuItem dup2 = new MenuItem("Open");
268: p2.add(dup2);
269: frame.add(p1);
270: frame.add(p2);
271:
272: assertFalse("Identical labels on different popups not unique: "
273: + AWT.getPath(dup), AWT.getPath(dup).equals(
274: AWT.getPath(dup2)));
275: assertFalse("Identical labels on different menus not unique: "
276: + AWT.getPath(dupMB1), AWT.getPath(dupMB1).equals(
277: AWT.getPath(dupMB2)));
278: assertEquals("Unique Menubar item includes full path", "File|"
279: + uniqueMB.getLabel(), AWT.getPath(uniqueMB));
280: assertEquals("Unique popup item includes full path",
281: uniquePopup.getLabel(), AWT.getPath(uniquePopup));
282:
283: MenuItem[] items = AWT.findAWTPopupMenuItems(frame, AWT
284: .getPath(dup));
285: assertEquals("Duplicate popup item not uniquely found", 1,
286: items.length);
287: assertEquals("Wrong duplicate popup item found", dup, items[0]);
288:
289: items = AWT.findAWTPopupMenuItems(frame, AWT.getPath(dup2));
290: assertEquals("Duplicate popup item not uniquely found (2)", 1,
291: items.length);
292: assertEquals("Wrong duplicate popup item found (2)", dup2,
293: items[0]);
294: }
295:
296: public void testAWTGetInvoker() {
297: Frame frame = new Frame(getName());
298: PopupMenu popup = new PopupMenu();
299: MenuItem mi = new MenuItem("Open");
300: popup.add(mi);
301: Label label = new Label(getName());
302: frame.add(label);
303: label.add(popup);
304:
305: assertEquals("Wrong invoker", label, AWT.getInvoker(mi));
306:
307: MenuBar mb = new MenuBar();
308: Menu menu = new Menu("File");
309: menu.add(mi);
310: mb.add(menu);
311: frame.setMenuBar(mb);
312:
313: assertNull("Invoker should be null for menubar-based items",
314: AWT.getInvoker(mi));
315: }
316:
317: public void testAWTIsOnPopup() {
318: Frame frame = new Frame(getName());
319: MenuBar mb = new MenuBar();
320: Menu menu = new Menu("File");
321: MenuItem mi = new MenuItem("Open");
322: menu.add(mi);
323: mb.add(menu);
324: frame.setMenuBar(mb);
325: assertTrue("Menu item should not register as on a popup", !AWT
326: .isOnPopup(mi));
327:
328: PopupMenu popup = new PopupMenu();
329: popup.add(menu);
330: frame.add(popup);
331: assertTrue("Menu item should register as on a popup", AWT
332: .isOnPopup(mi));
333: }
334:
335: public void testFindAWTPopupMenuItems() throws Exception {
336: Frame frame = new Frame(getName());
337: PopupMenu p1 = new PopupMenu();
338: PopupMenu p2 = new PopupMenu();
339: MenuItem mi = new MenuItem("Open");
340: MenuItem mi2 = new MenuItem("Open");
341: p1.add(mi);
342: p1.add(new MenuItem("Close"));
343: p2.add(mi2);
344: final Label label = new Label(getName());
345: frame.add(label);
346: label.add(p1);
347: label.add(p2);
348:
349: MenuItem[] items = AWT.findAWTPopupMenuItems(label, "Open");
350: assertEquals("Wrong number of items found", 2, items.length);
351: assertEquals("Wrong item #1", mi, items[0]);
352: assertEquals("Wrong item #2", mi2, items[1]);
353: }
354:
355: public void testFindAWTMenuItems() throws Exception {
356: Frame frame = new Frame(getName());
357: MenuBar mb = new MenuBar();
358: Menu fileMenu = new Menu("File");
359: Menu editMenu = new Menu("Edit");
360: MenuItem open = new MenuItem("Open");
361: fileMenu.add(open);
362: MenuItem close = new MenuItem("Close");
363: fileMenu.add(close);
364: mb.add(fileMenu);
365: MenuItem open2 = new MenuItem("Open");
366: editMenu.add(open2);
367: mb.add(editMenu);
368: frame.setMenuBar(mb);
369:
370: final Label label = new Label(getName());
371: frame.add(label);
372:
373: MenuItem[] items = AWT.findAWTMenuItems(frame, "Open");
374: assertEquals("Wrong number of items found", 2, items.length);
375: assertEquals("Wrong item #1", open, items[0]);
376: assertEquals("Wrong item #2", open2, items[1]);
377:
378: String PATH = "Edit|Open";
379: items = AWT.findAWTMenuItems(frame, PATH);
380: assertEquals("Wrong number of items found for " + PATH, 1,
381: items.length);
382: assertEquals("Wrong item found for " + PATH, open2, items[0]);
383:
384: PATH = "File|Close";
385: items = AWT.findAWTMenuItems(frame, PATH);
386: assertEquals("Wrong number of items found for " + PATH, 1,
387: items.length);
388: assertEquals("Wrong item found for " + PATH, close, items[0]);
389: }
390:
391: // FIXME fails on linux/1.4.2 only when run w/full suite
392: public void testAWTPopupControl() throws Exception {
393: boolean blocks = Bugs.showAWTPopupMenuBlocks();
394: if (Robot.getEventMode() == Robot.EM_AWT && blocks) {
395: return;
396: }
397:
398: Frame frame = new Frame(getName());
399: final PopupMenu popup = new PopupMenu();
400: MenuItem mi = new MenuItem("Open");
401: popup.add(mi);
402: final Label label = new Label(getName());
403: frame.add(label);
404: label.add(popup);
405: showWindow(frame);
406: assertFalse("Incorrect initial AWT popup blocking state", AWT
407: .isAWTPopupMenuBlocking());
408: try {
409: Runnable show = new Runnable() {
410: public void run() {
411: popup.show(label, 0, label.getHeight());
412: }
413: };
414: SwingUtilities.invokeLater(show);
415:
416: // Give the popup time to show; waitforidle would block
417: Thread.sleep(200);
418: if (blocks) {
419: assertEquals("Incorrect AWT popup blocking state",
420: true, AWT.isAWTPopupMenuBlocking());
421: }
422:
423: AWT.dismissAWTPopup();
424: assertFalse("AWT Popup could not be dismissed", AWT
425: .isAWTPopupMenuBlocking());
426: } finally {
427: // have to close the popup before doing any waitforidle or
428: // anything that requires the AWT tree lock
429: AWT.dismissAWTPopup();
430: }
431: }
432:
433: public void testRootFrameCheck() {
434: Frame f = JOptionPane.getRootFrame();
435: assertTrue("Wrong class name for root frame: actual="
436: + f.getClass(), f.getClass().getName().startsWith(
437: AWT.ROOT_FRAME_CLASSNAME));
438: assertTrue("Test for root name should pass for root frame", AWT
439: .isSharedInvisibleFrame(f));
440: assertFalse(
441: "Test for root name should not pass on something else",
442: AWT.isSharedInvisibleFrame(new JFrame(getName())));
443: }
444:
445: public void testGetWindow() {
446: JFrame frame = new JFrame(getName());
447: JMenuBar bar = new JMenuBar();
448: JMenu menu = new JMenu("file");
449: JMenu submenu = new JMenu("open");
450: JMenuItem item = new JMenuItem("open");
451: submenu.add(item);
452: menu.add(submenu);
453: bar.add(menu);
454: frame.setJMenuBar(bar);
455: assertEquals("Wrong window for menu item", frame, AWT
456: .getWindow(item));
457: assertEquals("Wrong window for submenu", frame, AWT
458: .getWindow(submenu));
459: assertEquals("Wrong window for menu", frame, AWT
460: .getWindow(menu));
461: assertEquals("Wrong window for menu bar", frame, AWT
462: .getWindow(bar));
463: assertEquals("Wrong window for frame", frame, AWT
464: .getWindow(frame));
465: }
466:
467: public void testMenuActive() {
468: JLabel label = new JLabel(getName());
469: JFrame frame = new JFrame(getName());
470: frame.getContentPane().add(label);
471: JMenuBar mb = new JMenuBar();
472: JMenu menu = new JMenu("File");
473: JMenuItem menuItem = new JMenuItem("Open");
474: menu.add(menuItem);
475: mb.add(menu);
476: frame.setJMenuBar(mb);
477: showWindow(frame);
478:
479: ComponentTester tester = new ComponentTester();
480: tester.actionClick(menu);
481: assertTrue("Should detect an active menu", AWT
482: .isMenuActive(label));
483: }
484:
485: public void testEnsureOnScreen() {
486: final Frame f = showFrame(new JLabel(getName()));
487: WindowTester tester = new WindowTester();
488: Rectangle bounds = f.getGraphicsConfiguration().getBounds();
489: tester.actionMove(f, bounds.x + bounds.width + 100, f.getY());
490:
491: invokeAndWait(new Runnable() {
492: public void run() {
493: AWT.ensureOnScreen(f);
494: }
495: });
496: assertTrue("Frame not moved on screen", bounds.contains(f
497: .getLocation()));
498:
499: tester.actionMove(f, -f.getWidth() - 10, f.getY());
500: invokeAndWait(new Runnable() {
501: public void run() {
502: AWT.ensureOnScreen(f);
503: }
504: });
505: assertTrue("Frame not moved on screen", bounds
506: .contains(new Point(f.getX() + f.getWidth(), f.getY())));
507: }
508:
509: public void testEncoding() throws Exception {
510: String utf8 = "\u0444\u0438\u0441\u0432\u0443";
511: JTextField tf = new JTextField(utf8);
512: showFrame(tf);
513: assertEquals("Wrong text", utf8, tf.getText());
514: /*
515: class Flag { boolean flag; }
516: final Flag flag = new Flag();
517: tf.addActionListener(new ActionListener() {
518: public void actionPerformed(ActionEvent e) {
519: flag.flag = true;
520: }
521: });
522: while (!flag.flag) {
523: Thread.sleep(100);
524: }
525: utf8 = tf.getText();
526: File file = new File("native.txt");
527: FileOutputStream os = new FileOutputStream(file);
528: os.write(utf8.getBytes("utf8"));
529: os.close();
530: */
531: }
532:
533: public static void main(String[] args) {
534: RepeatHelper.runTests(args, AWTTest.class);
535: }
536: }
|