001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.interception.WindowInterceptor;
005: import org.uispec4j.utils.AssertionFailureNotDetectedError;
006: import org.uispec4j.utils.UIComponentFactory;
007: import org.uispec4j.xml.XmlAssert;
008:
009: import javax.swing.*;
010: import java.awt.*;
011:
012: public abstract class WindowTestCase extends UIComponentTestCase {
013:
014: public void testAssertTitleEquals() throws Exception {
015: Window window = createWindowWithTitle("me");
016: assertTrue(window.titleEquals("me"));
017:
018: try {
019: assertTrue(window.titleEquals("you"));
020: throw new AssertionFailureNotDetectedError();
021: } catch (AssertionFailedError failure) {
022: assertEquals("expected:<you> but was:<me>", failure
023: .getMessage());
024: }
025: }
026:
027: public void testGetTitle() throws Exception {
028: assertEquals("me", createWindowWithTitle("me").getTitle());
029: }
030:
031: public void testGetDescription() throws Exception {
032: Window window = createWindowWithTitle("my title");
033: window.getAwtComponent().setName("myFrame");
034:
035: JTextField textField = new JTextField();
036: textField.setName("myText");
037: addComponent(window, textField);
038:
039: XmlAssert.assertEquivalent("<window title='my title'>"
040: + " <textBox name='myText'/>" + "</window>", window
041: .getDescription());
042: }
043:
044: protected UIComponent createComponent() {
045: return createWindowWithTitle("title");
046: }
047:
048: public void testFinder() throws Exception {
049: Window window = createWindow();
050:
051: JTextField textField = new JTextField();
052: textField.setName("myText");
053: addComponent(window, textField);
054:
055: TextBox uiComp = window.getTextBox("myText");
056:
057: assertSame(textField, uiComp.getAwtComponent());
058: }
059:
060: public void testWindowManagesMenuBars() throws Exception {
061: JMenuBar jMenuBar = new JMenuBar();
062: jMenuBar.add(new JMenu("Menu 1"));
063: jMenuBar.add(new JMenu("Menu 2"));
064: jMenuBar.add(new JMenu("Menu 3"));
065:
066: Window window = createWindowWithMenu(jMenuBar);
067:
068: window.getMenuBar().contentEquals(
069: new String[] { "Menu 1", "Menu 2", "Menu 3", });
070: }
071:
072: public void testGetComponentTypeName() {
073: assertEquals("window", createWindow().getDescriptionTypeName());
074: }
075:
076: public void testFactory() {
077: Component component = createWindow().getAwtComponent();
078: UIComponent uiComponent = UIComponentFactory
079: .createUIComponent(component);
080: assertNotNull(uiComponent);
081: assertTrue(Window.class.isInstance(uiComponent));
082: }
083:
084: public void testWindowClosed() throws Exception {
085: final Window window = createWindow();
086: assertFalse(window.isVisible());
087: WindowInterceptor.run(new Trigger() {
088: public void run() throws Exception {
089: show(window);
090: }
091: });
092:
093: assertTrue(window.isVisible());
094:
095: close(window);
096:
097: assertFalse(window.isVisible());
098: }
099:
100: protected abstract Window createWindowWithMenu(JMenuBar jMenuBar);
101:
102: protected Window createWindow() {
103: return createWindowWithTitle("title");
104: }
105:
106: protected abstract Window createWindowWithTitle(String title);
107:
108: protected void addComponent(Window window, JComponent component) {
109: JScrollPane scroller = new JScrollPane();
110: scroller.getViewport().add(component);
111: JPanel panel = new JPanel();
112: panel.add(scroller);
113: window.getInternalAwtContainer().add(panel);
114: }
115:
116: protected void checkIsModal(Window window, boolean modal) {
117: assertEquals(modal, window.isModal());
118: try {
119: assertEquals(!modal, window.isModal());
120: throw new AssertionFailureNotDetectedError();
121: } catch (AssertionFailedError e) {
122: }
123: }
124:
125: protected void show(final Window window) {
126: window.getAwtContainer().setVisible(true);
127: }
128:
129: protected abstract void close(Window window);
130: }
|