001: package org.uispec4j;
002:
003: import junit.framework.Assert;
004: import org.uispec4j.assertion.Assertion;
005:
006: import javax.swing.Icon;
007: import javax.swing.JLabel;
008: import javax.swing.text.JTextComponent;
009: import java.awt.Component;
010:
011: /**
012: * Wrapper for JTextComponent/JLabel components.
013: */
014: public class TextBox extends AbstractUIComponent {
015: public static final String TYPE_NAME = "textBox";
016: public static final Class[] SWING_CLASSES = { JTextComponent.class,
017: JLabel.class };
018:
019: private Handler handler;
020:
021: public TextBox(JTextComponent textComponent) {
022: this .handler = TextBoxHandlerForHtmlTextComponent
023: .init(textComponent);
024: if (handler == null) {
025: this .handler = new TextBoxHandlerForRawTextComponent(
026: textComponent);
027: }
028: }
029:
030: public TextBox(JLabel label) {
031: this .handler = new TextBoxHandlerForLabel(label);
032: }
033:
034: public String getDescriptionTypeName() {
035: return TYPE_NAME;
036: }
037:
038: public Component getAwtComponent() {
039: return handler.getAwtComponent();
040: }
041:
042: /**
043: * Simulates pressing a key while the focus is in the text box.<br>
044: * Warning: the default cursor position is 0.
045: */
046: public void pressKey(Key key) {
047: handler.pressKey(key);
048: }
049:
050: /**
051: * Replaces the text box contents and simulates pressing the Enter key.
052: */
053: public void setText(String text) {
054: handler.setText(text);
055: }
056:
057: /**
058: * Inserts text at the given position without pressing Enter.
059: */
060: public void insertText(String text, int position) {
061: handler.insertText(text, position);
062: }
063:
064: public String getText() {
065: return handler.getText();
066: }
067:
068: public Assertion textIsEmpty() {
069: return handler.textIsEmpty();
070: }
071:
072: /**
073: * Checks the displayed text in cases where HTML text is used. This is
074: * different from {@link #textIsEmpty()} in that whitespaces, carriage return
075: * and other formatting adjustments are ignored.
076: */
077: public Assertion htmlEquals(String html) {
078: return handler.htmlEquals(html);
079: }
080:
081: /**
082: * Checks that the text box contains a number of substrings, in a given order.
083: * This method is useful for checking key information in the displayed string,
084: * without being too dependent on the actual wording.
085: */
086: public Assertion textContains(final String[] orderedTexts) {
087: return new Assertion() {
088: public void check() {
089: String actual = handler.getText();
090: int index = 0;
091: for (int i = 0; i < orderedTexts.length; i++) {
092: String text = orderedTexts[i];
093: index = actual.indexOf(text, index);
094: if (index < 0) {
095: if (actual.indexOf(text) < 0) {
096: Assert
097: .fail("The component text does not contain '"
098: + text
099: + "' "
100: + "- actual content is:"
101: + actual);
102: } else {
103: Assert
104: .fail("The component text does not contain '"
105: + text
106: + "' at the expected position "
107: + "- actual content is:"
108: + actual);
109: }
110: } else {
111: index += text.length();
112: }
113: }
114: }
115: };
116: }
117:
118: public Assertion textEquals(String text) {
119: return handler.textEquals(text);
120: }
121:
122: public Assertion textContains(String text) {
123: return handler.textContains(text);
124: }
125:
126: public Assertion textDoesNotContain(String text) {
127: return handler.textDoesNotContain(text);
128: }
129:
130: public Assertion isEditable() {
131: return handler.isEditable();
132: }
133:
134: /**
135: * Checks the icon displayed by the component. Please note that equals()
136: * not being defined for Icon implementations, you will have to provide a pointer
137: * to the actual Icon instance that is used in the production code. This make
138: * this method mostly suited to unit testing.
139: */
140: public Assertion iconEquals(Icon icon) {
141: return handler.iconEquals(icon);
142: }
143:
144: /**
145: * Simulates a click on an hyperlink given a part of the link text.
146: * An exception will be thrown if zero or more than one hyperlinks are
147: * found with this text.
148: */
149: public void clickOnHyperlink(String link) {
150: handler.clickOnHyperlink(link);
151: }
152:
153: /**
154: * @see #clickOnHyperlink(String)
155: */
156: public Trigger triggerClickOnHyperlink(final String name) {
157: return new Trigger() {
158: public void run() throws Exception {
159: clickOnHyperlink(name);
160: }
161: };
162: }
163:
164: interface Handler {
165: Component getAwtComponent();
166:
167: void setText(String text);
168:
169: void insertText(String text, int position);
170:
171: String getText();
172:
173: Assertion textIsEmpty();
174:
175: Assertion textEquals(String text);
176:
177: Assertion textContains(String text);
178:
179: Assertion textDoesNotContain(String text);
180:
181: Assertion isEditable();
182:
183: void clickOnHyperlink(String link);
184:
185: void pressKey(Key key);
186:
187: Assertion iconEquals(Icon icon);
188:
189: Assertion htmlEquals(String html);
190: }
191: }
|