001: package com.canoo.webtest.extension.applet.jemmy;
002:
003: import com.canoo.webtest.boundary.JemmyBoundary;
004: import com.canoo.webtest.extension.applet.runner.Context;
005: import org.netbeans.jemmy.JemmyProperties;
006: import org.netbeans.jemmy.Outputable;
007: import org.netbeans.jemmy.TestOut;
008: import org.netbeans.jemmy.Timeoutable;
009: import org.netbeans.jemmy.Timeouts;
010: import org.netbeans.jemmy.Waitable;
011: import org.netbeans.jemmy.Waiter;
012:
013: import java.net.URL;
014:
015: /**
016: * @author Denis N. Antonioli
017: */
018:
019: public class ContextOperator implements Outputable, Timeoutable {
020: /**
021: * Should be in org.netbeans.jemmy.operators.AbstractOperator
022: */
023: private TestOut fOutput;
024: /**
025: * Should be in org.netbeans.jemmy.operators.AbstractOperator
026: */
027: private Timeouts fTimeouts;
028: /**
029: * Should be in org.netbeans.jemmy.operators.AbstractOperator
030: */
031: private JemmyProperties fProperties;
032:
033: public static final String WAIT_TIMEOUT_NAME = "ContextOperator.WaitStateTimeout";
034:
035: private final Context fContext;
036:
037: static {
038: Timeouts.initDefault(WAIT_TIMEOUT_NAME, 3600000);
039: }
040:
041: /**
042: * Should be in org.netbeans.jemmy.operators.AbstractOperator
043: */
044: {
045: setTimeouts(JemmyProperties.getProperties().getTimeouts());
046: setOutput(JemmyProperties.getProperties().getOutput());
047: setProperties(JemmyProperties.getProperties());
048: }
049:
050: /**
051: * Should be in org.netbeans.jemmy.operators.AbstractOperator
052: *
053: * @see org.netbeans.jemmy.operators.Operator
054: */
055: public void setOutput(TestOut output) {
056: fOutput = output;
057: }
058:
059: /**
060: * Should be in org.netbeans.jemmy.operators.AbstractOperator
061: *
062: * @see org.netbeans.jemmy.operators.Operator
063: */
064: public TestOut getOutput() {
065: return fOutput;
066: }
067:
068: /**
069: * Should be in org.netbeans.jemmy.operators.AbstractOperator
070: *
071: * @see org.netbeans.jemmy.operators.Operator
072: */
073: public void setTimeouts(Timeouts timeouts) {
074: fTimeouts = timeouts;
075: }
076:
077: /**
078: * Should be in org.netbeans.jemmy.operators.AbstractOperator
079: *
080: * @see org.netbeans.jemmy.operators.Operator
081: */
082: public Timeouts getTimeouts() {
083: return fTimeouts;
084: }
085:
086: /**
087: * Should be in org.netbeans.jemmy.operators.AbstractOperator
088: *
089: * @see org.netbeans.jemmy.operators.Operator
090: */
091: public JemmyProperties getProperties() {
092: return fProperties;
093: }
094:
095: /**
096: * Should be in org.netbeans.jemmy.operators.AbstractOperator
097: *
098: * @see org.netbeans.jemmy.operators.Operator
099: */
100: public JemmyProperties setProperties(JemmyProperties properties) {
101: JemmyProperties oldProperties = getProperties();
102: fProperties = properties;
103: return oldProperties;
104: }
105:
106: /**
107: * Waits a state specified by a Waitable instance. Should be in org.netbeans.jemmy.operators.AbstractOperator
108: *
109: * @see org.netbeans.jemmy.operators.Operator
110: */
111: public void waitState(final Waitable state) {
112: Waiter stateWaiter = new Waiter(state);
113: stateWaiter.setTimeouts(getTimeouts().cloneThis());
114: stateWaiter.getTimeouts().setTimeout("Waiter.WaitingTime",
115: getTimeouts().getTimeout(WAIT_TIMEOUT_NAME));
116: stateWaiter.setOutput(getOutput().createErrorOutput());
117: JemmyBoundary.tryWaitAction(stateWaiter, state, fContext);
118: }
119:
120: public ContextOperator(Context context) {
121: fContext = context;
122: }
123:
124: public void waitShowDocument() {
125: waitShowDocument(Context.DEFAULT_TARGET);
126: }
127:
128: public void waitShowDocument(String target) {
129: getOutput().printLine("Wait any url in frame " + target);
130: getOutput().printGolden("Wait any url in frame " + target);
131: waitState(new WaitFrame(target));
132: }
133:
134: public void waitShowDocument(URL url) {
135: waitShowDocument(url, Context.DEFAULT_TARGET);
136: }
137:
138: public void waitShowDocument(URL url, String target) {
139: getOutput()
140: .printLine("Wait \"" + url + "\" in frame " + target);
141: getOutput().printGolden(
142: "Wait \"" + url + "\" in frame " + target);
143: waitState(new WaitUrlInFrame(url, target));
144: }
145:
146: public Context getContext() {
147: return fContext;
148: }
149:
150: /**
151: * Checks that the applet asked for any document to be displayed.
152: */
153: static class WaitFrame implements Waitable {
154: private final String fTarget;
155:
156: WaitFrame(String target) {
157: fTarget = target;
158: }
159:
160: public Object actionProduced(final Object obj) {
161: return ((Context) obj).getAppletPluginResults().getFrames()
162: .get(getTarget());
163: }
164:
165: public String getDescription() {
166: return "Wait for showDocument(" + getTarget() + ", ...)";
167: }
168:
169: public String getTarget() {
170: return fTarget;
171: }
172: }
173:
174: /**
175: * Checks that the applet asked for a specific document to be displayed.
176: */
177: static class WaitUrlInFrame extends WaitFrame {
178: private final URL fUrl;
179:
180: WaitUrlInFrame(URL url, String target) {
181: super (target);
182: fUrl = url;
183: }
184:
185: public Object actionProduced(Object obj) {
186: URL url = (URL) super .actionProduced(obj);
187: if (fUrl.equals(url)) {
188: return url;
189: }
190: return null;
191: }
192:
193: public String getDescription() {
194: return "Wait for showDocument(" + getTarget() + ", "
195: + fUrl.toExternalForm() + ")";
196: }
197: }
198: }
|