My favorites | Sign in
Project Home Downloads Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 174: Add support for interrupting/killing tests - Patch from Simeon Fitch
1 person starred this issue and may be notified of changes. Back to list
Status:  Fixed
Owner:  Alex.Rui...@gmail.com
Closed:  Jul 2008


Sign in to add a comment
 
Project Member Reported by Alex.Rui...@gmail.com, Jul 14, 2008
From Simeon:

So, let's say you're running a pretty big FEST-Swing test, one that
takes a couple minutes to run. In the middle of it, a Skype call comes
in. You need to answer the call, but you currently don't own the
mouse! Eventually, some frantic combination of Alt, Ctrl, Cmd, Tab,
Esc, F1-12, gets the window buried, and you can accept the call, but
you want something more "decisive" to kill the test run.

The class below offers one approach to addressing this problem, and
offer it to anyone who wants something similar.

Has anyone else implemented a similar feature and taken a different
approach?

Regards,

Simeon


---------------------


import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;

/**
 * This class provides an escape valve for the user to abort a running
FEST-Swing
 * test by pressing Ctrl + Shift + A. Easiest way to us is to call<br/
>
 * <pre>
 *     EmergencyAbortListener.register();
 * </pre>
 * from the @BeforeClass method.
 *
 * @version $Revision: 1.1 $
 * @author <a href="mailto:simeon.fitch@mseedsoft.com">Simeon H.K.
Fitch</a>
 * @since Jun 30, 2008
 */
public class EmergencyAbortListener implements AWTEventListener {

   public static void register() {
       Toolkit.getDefaultToolkit().addAWTEventListener(new
EmergencyAbortListener(), AWTEvent.KEY_EVENT_MASK);
   }

   /**
    * Private ctor because we don't want to allow use outside of the
{@link #register} method.
    */
   private EmergencyAbortListener() {
   }

   /**
    * Inspect key events for magic abort sequence.
    * {@inheritDoc}
    * @see
java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent)
    */
   public void eventDispatched(AWTEvent event) {
       if(event.getID() == KeyEvent.KEY_PRESSED) {
           KeyEvent e = (KeyEvent) event;
           if(e.isControlDown() &&  e.isShiftDown() &&
               e.getKeyCode() == KeyEvent.VK_A &&
               e.getID() == KeyEvent.KEY_PRESSED) {

               // We do three things to signal an abort.
               // 1) sent an interrupt signal to main thread
               // 2) dispose all available frames.
               // 3) throw RuntimeException on AWT event thread
               pokeMainThread();

               for(Frame f : Frame.getFrames()) {
                   f.dispose();
               }

               throw new RuntimeException("User aborted test.");
           }
       }
   }

   /**
    * Call {@link Thread#interrupt()} on main thread in attempt to
    * interrupt current FEST operation. Only affects thread if it is
    * in a {@link Object#wait()} or {@link Thread#sleep(long)}
method.
    */
   private void pokeMainThread() {
       Thread[] all = new Thread[Thread.activeCount()];
       Thread.enumerate(all);
       for(Thread t : all) {
           if("main".equalsIgnoreCase(t.getName())) {
               t.interrupt();
           }
       }
   }

   /** Last resort exception handler for AWT event thread, to make
sure we
    * can get a backtrace dump when use aborts;
    */
   public static class SimpleFallbackHandler {
       public void handle(Throwable ex) {
           ex.printStackTrace();
       }
   }

   static {
       // Make sure there's an exception handler that will dump a
stack trace on abort.
       System.setProperty("sun.awt.exception.handler",
SimpleFallbackHandler.class.getName());
   }

}
 
Jul 20, 2008
Project Member #1 Alex.Rui...@gmail.com
Added class org.fest.swing.core.EmergencyAbortListener to abort running FEST-Swing
tests using a predefined key combination.
Status: Fixed
Nov 30, 2008
Project Member #2 Alex.Rui...@gmail.com
Set the module as a label, instead of being part of the title.
Summary: Add support for interrupting/killing tests - Patch from Simeon Fitch
Labels: Module-fest-swing
Sign in to add a comment

Powered by Google Project Hosting