benjamin84 / fest

Automatically exported from code.google.com/p/fest
0 stars 0 forks source link

Add support for interrupting/killing tests - Patch from Simeon Fitch #174

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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());
   }

}

Original issue reported on code.google.com by Alex.Rui...@gmail.com on 14 Jul 2008 at 7:32

GoogleCodeExporter commented 9 years ago
Added class org.fest.swing.core.EmergencyAbortListener to abort running 
FEST-Swing
tests using a predefined key combination.

Original comment by Alex.Rui...@gmail.com on 20 Jul 2008 at 9:56

GoogleCodeExporter commented 9 years ago
Set the module as a label, instead of being part of the title.

Original comment by Alex.Rui...@gmail.com on 1 Dec 2008 at 2:08