001    /*
002     * Created on Mar 30, 2010
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2010 the original author or authors.
014     */
015    package org.fest.swing.driver;
016    
017    import static org.fest.swing.edt.GuiActionRunner.execute;
018    
019    import java.applet.AppletContext;
020    import java.net.URL;
021    
022    import javax.swing.JApplet;
023    
024    import org.fest.swing.annotation.RunsInEDT;
025    import org.fest.swing.core.Robot;
026    import org.fest.swing.edt.GuiQuery;
027    import org.fest.swing.edt.GuiTask;
028    
029    /**
030     * Understands functional testing of <code>{@link JApplet}</code>s:
031     * <ul>
032     * <li>user input simulation</li>
033     * <li>state verification</li>
034     * <li>property value query</li>
035     * </ul>
036     * This class is intended for internal use only. Please use the classes in the package
037     * <code>{@link org.fest.swing.fixture}</code> in your tests.
038     *
039     * @author Mel Llaguno
040     * @author Alex Ruiz
041     *
042     * @since 1.2
043     */
044    public class JAppletDriver extends ComponentDriver {
045    
046      /**
047       * Creates a new </code>{@link JAppletDriver}</code>.
048       * @param robot the robot to use simulate user input.
049       */
050      public JAppletDriver(Robot robot) {
051        super(robot);
052      }
053    
054      /**
055       * Returns the <code>{@link AppletContext}</code> of the given <code>{@link JApplet}</code>.
056       * @param applet the given {@code JApplet}.
057       * @return the {@code AppletContext} of the given {@code JApplet}.
058       */
059      @RunsInEDT
060      public AppletContext appletContextOf(JApplet applet) {
061        return appletContext(applet);
062      }
063    
064      @RunsInEDT
065      private static AppletContext appletContext(final JApplet applet) {
066        return execute(new GuiQuery<AppletContext>() {
067          protected AppletContext executeInEDT() {
068            return applet.getAppletContext();
069          }
070        });
071      }
072    
073      /**
074       * Requests the given <code>{@link JApplet}</code> to be resized.
075       * @param applet the given {@code JApplet}.
076       * @param width the new width.
077       * @param height the new height.
078       */
079      @RunsInEDT
080      public void resize(JApplet applet, int width, int height) {
081        doResize(applet, width, height);
082      }
083    
084      @RunsInEDT
085      private static void doResize(final JApplet applet, final int width, final int height) {
086        execute(new GuiTask() {
087          protected void executeInEDT() {
088            applet.resize(width, height);
089          }
090        });
091      }
092    
093      /**
094       * Returns the URL of the directory that contains the given <code>{@link JApplet}</code>.
095       * @param applet the given {@code JApplet}.
096       * @return the URL of the directory that contains the given {@code JApplet}.
097       */
098      @RunsInEDT
099      public URL codeBaseOf(JApplet applet) {
100        return codeBase(applet);
101      }
102    
103      @RunsInEDT
104      private static URL codeBase(final JApplet applet) {
105        return execute(new GuiQuery<URL>() {
106          protected URL executeInEDT() {
107            return applet.getCodeBase();
108          }
109        });
110      }
111    
112      /**
113       * Returns the URL of the document the given <code>{@link JApplet}</code> is embedded.
114       * @param applet the given {@code JApplet}.
115       * @return the URL of the document the given {@code JApplet} is embedded.
116       */
117      @RunsInEDT
118      public URL documentBaseOf(JApplet applet) {
119        return documentBase(applet);
120      }
121    
122      @RunsInEDT
123      private static URL documentBase(final JApplet applet) {
124        return execute(new GuiQuery<URL>() {
125          protected URL executeInEDT() {
126            return applet.getDocumentBase();
127          }
128        });
129      }
130    
131      /**
132       * Returns the value of the named parameter in the given <code>{@link JApplet}</code> in the HTML tag, or
133       * <code>null</code> if not set.
134       * @param applet the given {@code JApplet}.
135       * @param parameterName a parameter name.
136       * @return the value of the named parameter in the given {code JApplet} in the HTML tag, or <code>null</code> if not
137       * set.
138       */
139      @RunsInEDT
140      public String parameterValue(JApplet applet, String parameterName) {
141        return parameter(applet, parameterName);
142      }
143    
144      @RunsInEDT
145      private static String parameter(final JApplet applet, final String parameterName) {
146        return execute(new GuiQuery<String>() {
147          protected String executeInEDT() {
148            return applet.getParameter(parameterName);
149          }
150        });
151      }
152    
153      /**
154       * Indicates whether the given <code>{@link JApplet}</code> is active or not.
155       * @param applet the given {@code JApplet}.
156       * @return <code>true</code> if the given {@code JApplet} is active; <code>false</code> otherwise.
157       */
158      @RunsInEDT
159      public boolean isActive(JApplet applet) {
160        return active(applet);
161      }
162    
163      @RunsInEDT
164      private static boolean active(final JApplet applet) {
165        return execute(new GuiQuery<Boolean>() {
166          protected Boolean executeInEDT() {
167            return applet.isActive();
168          }
169        });
170      }
171    }