001: package org.uispec4j.interception;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.TextBox;
005: import org.uispec4j.Trigger;
006: import org.uispec4j.UIComponent;
007: import org.uispec4j.Window;
008: import org.uispec4j.interception.handlers.InterceptionHandler;
009:
010: import java.util.ArrayList;
011: import java.util.Iterator;
012: import java.util.List;
013:
014: /**
015: * Ready-to-use window interception handler, designed for simple dialogs. <p>
016: * Sample usage:
017: * <pre><code>
018: * WindowInterceptor
019: * .init(panel.getButton("Change value").triggerClick())
020: * .process(BasicHandler.init()
021: * .assertContainsText("Enter new value")
022: * .setText("13")
023: * .triggerButtonClick("OK"))
024: * .run();
025: * </code></pre>
026: * The last call must be {@link #triggerButtonClick(String)}, which returns the created WindowHandler.
027: *
028: * @see WindowInterceptor
029: */
030: public class BasicHandler {
031:
032: private List handlers = new ArrayList();
033:
034: /**
035: * Starts the definition of the handler.
036: */
037: public static BasicHandler init() {
038: return new BasicHandler();
039: }
040:
041: private BasicHandler() {
042: }
043:
044: /**
045: * Checks that there is a text component in the dialog displaying the given text.
046: */
047: public BasicHandler assertContainsText(final String text) {
048: handlers.add(new InterceptionHandler() {
049: public void process(Window window) {
050: UIComponent component = window.findUIComponent(
051: TextBox.class, text);
052: if (component == null) {
053: throw new AssertionFailedError("Text not found: "
054: + text);
055: }
056: }
057: });
058: return this ;
059: }
060:
061: /**
062: * Clicks on a button given its displayed label. This method will throw an exception if
063: * no button with this text is found.
064: */
065: public BasicHandler clickButton(final String buttonName) {
066: handlers.add(new InterceptionHandler() {
067: public void process(Window window) {
068: window.getButton(buttonName).click();
069: }
070: });
071: return this ;
072: }
073:
074: /**
075: * Enters a text value, provided that there is only one input text field in the dialog.
076: */
077: public BasicHandler setText(final String text) {
078: handlers.add(new InterceptionHandler() {
079: public void process(Window window) {
080: window.getInputTextBox().setText(text);
081: }
082: });
083: return this ;
084: }
085:
086: /**
087: * Returns the created window handler with a trigger for clicking on a button.
088: */
089: public WindowHandler triggerButtonClick(final String buttonName) {
090: return new WindowHandler() {
091: public Trigger process(Window window) throws Exception {
092: for (Iterator iterator = handlers.iterator(); iterator
093: .hasNext();) {
094: InterceptionHandler handler = (InterceptionHandler) iterator
095: .next();
096: handler.process(window);
097: }
098: return window.getButton(buttonName).triggerClick();
099: }
100: };
101: }
102: }
|