org.mozilla.mcp
Class TimeoutHandler

java.lang.Object
  extended by org.mozilla.mcp.TimeoutHandler

public class TimeoutHandler
extends java.lang.Object

This class provides a simple facility for placing a time bound on browser interactions (clicks, Ajax transactions, etc).

Usage

A useful pattern is to use this as an inner class within a JUnit testscase:


        final Thread testThread = Thread.currentThread();
        timeoutHandler = new TimeoutHandler() {
            public void timeout() {
                super.timeout();
                testThread.interrupt();
                fail("Action timed out");
            }
        };        
        mcp.setTimeoutHandler(timeoutHandler);

TimeoutHandler has a boolean JavaBeans property called didTimeout that can be used after blocking operations to test if a timeout happened.


        if (timeoutHandler.isDidTimeout()) {
            fail("timed out waiting for load");
        }

Another useful pattern is to combine the previous inner class approach with having the browser perform a non-blocking operation, and then causing the main thread to enter a loop until either a condition is met, or the timeout occurs:


        bitSet.clear();
        mcp.clickElement(inplaceFields.get(1));
        makeAjaxAssertions(bitSet);
//...
    private void makeAjaxAssertions(BitSet bitSet) throws Exception {
        // Artifically wait for the ajax transaction to complete, or the timeout to be reached.
        int i = 0;
        while (true) {
            if (bitSet.get(TestFeature.STOP_WAITING.ordinal())) {
                break;
            }
            i++;
            Thread.currentThread().sleep(mcp.getTimeoutWaitInterval());
        }

        // assert that the ajax transaction succeeded
        assertTrue(bitSet.get(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal()));
    }

The above code will either exit normally, by virtuo of the AjaxListener being called and it setting the STOP_WAITING bit in the bitset, or it will terminate due to timeout, in which case the inner class timeout method will be called.

Author:
edburns

Constructor Summary
TimeoutHandler()
           
 
Method Summary
 boolean isDidTimeout()
          Getter for boolean JavaBeans property didTimeout.
 void setDidTimeout(boolean didTimeout)
          Setter for boolean JavaBeans property didTimeout.
 void timeout()
          The default implementation sets the value of the didTimeout JavaBeans property to true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TimeoutHandler

public TimeoutHandler()
Method Detail

timeout

public void timeout()

The default implementation sets the value of the didTimeout JavaBeans property to true.


isDidTimeout

public boolean isDidTimeout()

Getter for boolean JavaBeans property didTimeout.


setDidTimeout

public void setDidTimeout(boolean didTimeout)

Setter for boolean JavaBeans property didTimeout.



Copyright © 2002-2007 Mozilla.org All Rights Reserved.