package de.fitsample;

/**
 * An extension of the {@link fit.ActionFixture fit.ActionFixture}with some
 * utility functions. <br/>You have an additional action called
 * {@link #slowMotion() slowMotion}which takes an integer as parameter. This
 * integer specifies the time (in miliseconds) to sleep after each action. This
 * is useful for demonstrations, so the audience can follow the actions
 * visually. <br/>E.g. the following Fixture will pause fo 300ms after each
 * action: <table border="1" cellspacing="0">
 * <tr>
 * <td colspan="3">de.fitsample.ActionFixture</td>
 * </tr>
 * <tr>
 * <td>slowMotion</td>
 * <td colspan="2">300</td>
 * </tr>
 * <tr>
 * <td>start</td>
 * <td colspan="2">com.dvag.fitnesse.sample.CalculatorActionFixture</td>
 * </tr>
 * <tr>
 * <td>enter</td>
 * <td>value</td>
 * <td>3,5</td>
 * </tr>
 * <tr>
 * <td>press</td>
 * <td colspan="2">plus</td>
 * </tr>
 * <tr>
 * <td>enter</td>
 * <td>value</td>
 * <td>4</td>
 * </tr>
 * <tr>
 * <td>press</td>
 * <td colspan="2">calculate</td>
 * </tr>
 * <tr>
 * <td>check</td>
 * <td>result</td>
 * <td>7,5</td>
 * </tr>
 * </table>
 * 
 * @author Ralf Stuckert
 */
public class ActionFixture extends fit.ActionFixture {

    /**
     * The miliseconds to {@link #sleep() sleep}in slow motion mode.
     */
    private long slowMotionMs;

    /**
     * Sets the ms to sleep after every action (default is <code>0</code>).
     */
    public void slowMotion() {
        slowMotionMs = Long.parseLong(cells.more.text());
    }

    /**
     * Override in order to support slow motion.
     * 
     * @see fit.ActionFixture#enter()
     */
    public void enter() throws Exception {
        super.enter();
        sleep();
    }

    /**
     * Override in order to support slow motion.
     * 
     * @see fit.ActionFixture#check()
     */
    public void check() throws Exception {
        super.check();
        sleep();
    }

    /**
     * Override in order to support slow motion.
     * 
     * @see fit.ActionFixture#press()
     */
    public void press() throws Exception {
        super.press();
        sleep();
    }

    /**
     * Override in order to support slow motion.
     * 
     * @see fit.ActionFixture#start()
     */
    public void start() throws Exception {
        super.start();
        sleep();
    }

    /**
     * Sleeps for the time specified in the {@link #slowMotion() slowMotion}
     * row. This method is called by all actions.
     */
    protected void sleep() {
        if (slowMotionMs < 1) {
            return;
        }
        try {
            Thread.sleep(slowMotionMs);
        } catch (InterruptedException excep) {
            excep.printStackTrace();
        }
    }

}
