Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 233   Methods: 18
NCLOC: 199   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Event.java 14.7% 25.6% 44.4% 23.7%
coverage coverage
 1   
 package abbot.script;
 2   
 
 3   
 import java.awt.*;
 4   
 import java.awt.event.*;
 5   
 import java.util.*;
 6   
 
 7   
 import abbot.*;
 8   
 import abbot.finder.*;
 9   
 import abbot.tester.ComponentTester;
 10   
 import abbot.util.AWT;
 11   
 
 12   
 /** Script step to generate a single AWT event to a component.   Currently
 13   
  * used for key down/up and mouse motion.
 14   
  */
 15   
 // TODO: Save mouse motion/enter/leave as relative to containing window/frame,
 16   
 // in case frame moves
 17   
 public class Event extends Step {
 18   
     private static final String USAGE = 
 19   
         "<event type=\"...\" kind=\"...\" [...]/>";
 20   
 
 21   
     private String componentID = null;
 22   
     private String type = null;
 23   
     private String kind = null;
 24   
     private Map eventAttributes = new HashMap();
 25   
 
 26  0
     public Event(Resolver resolver, Map attributes) {
 27  0
         super(resolver, attributes);
 28  0
         componentID = (String)attributes.get(TAG_COMPONENT);
 29   
         // can't create events without a component, so creation of the event
 30   
         // is deferred.  we do check for validity, though.
 31  0
         parseEvent(attributes);
 32   
     }
 33   
 
 34   
     /** Create one based on the given event. */
 35  96
     public Event(Resolver resolver, String desc, AWTEvent event) {
 36  96
         super(resolver, desc);
 37  96
         int id = event.getID();
 38  96
         type = simpleClassName(event.getClass());
 39  96
         kind = ComponentTester.getEventID(event);
 40  96
         Component comp = ((ComponentEvent)event).getComponent();
 41  96
         if (event instanceof MouseEvent) {
 42  1
             MouseEvent me = (MouseEvent)event;
 43  1
             ComponentReference ref = resolver.addComponent(comp);
 44  1
             componentID = ref.getID();
 45  1
             eventAttributes.put(TAG_X, String.valueOf(me.getX()));
 46  1
             eventAttributes.put(TAG_Y, String.valueOf(me.getY()));
 47   
             // Convert enter/exit to mouse moved
 48  1
             if (id == MouseEvent.MOUSE_ENTERED 
 49   
                 || id == MouseEvent.MOUSE_EXITED
 50   
                 || id == MouseEvent.MOUSE_DRAGGED)
 51  0
                 kind = "MOUSE_MOVED";
 52   
             // No need to include modifiers in a captured event; it is assumed
 53   
             // that the modifiers are captured separately
 54  1
             if (me.isPopupTrigger())
 55  0
                 eventAttributes.put(TAG_TRIGGER, "true");
 56   
         }
 57  95
         else if (event instanceof KeyEvent) {
 58  95
             KeyEvent ke = (KeyEvent)event;
 59  95
             ComponentReference ref = resolver.addComponent(comp);
 60  95
             componentID = ref.getID();
 61  95
             if (ke.getModifiers() != 0) {
 62  32
                 eventAttributes.put(TAG_MODIFIERS, AWT.
 63   
                                     getModifiers(ke));
 64   
             }
 65  95
             if (id == KeyEvent.KEY_TYPED) {
 66   
                 // Must encode keychars (e.g. '<')
 67  0
                 eventAttributes.put(TAG_KEYCHAR, 
 68   
                                     String.valueOf(ke.getKeyChar()));
 69   
             }
 70   
             else {
 71  95
                 eventAttributes.put(TAG_KEYCODE, AWT.
 72   
                                     getKeyCode(ke.getKeyCode()));
 73   
             }
 74   
         }
 75   
         else {
 76  0
             throw new IllegalArgumentException("Unimplemented event type " 
 77   
                                                + event);
 78   
         }
 79   
     }
 80   
 
 81  102
     public String getDefaultDescription() { 
 82  102
         String desc = type + "." + kind;
 83  102
         if (type.equals("KeyEvent"))
 84  102
             desc += " (" + eventAttributes.get(TAG_KEYCODE) + ")";
 85  102
         if (componentID != null)
 86  102
             desc += " on ${" + componentID + "}";
 87  102
         return desc;
 88   
     }
 89  2
     public String getXMLTag() { return TAG_EVENT; }
 90  2
     public String getUsage() { return USAGE; }
 91  0
     public Map getAttributes() {
 92  0
         Map map = super.getAttributes();
 93  0
         map.put(TAG_COMPONENT, componentID);
 94  0
         map.put(TAG_TYPE, type);
 95  0
         if (kind != null)
 96  0
             map.put(TAG_KIND, kind);
 97  0
         map.putAll(eventAttributes);
 98  0
         return map;
 99   
     }
 100   
 
 101   
     /** Send our event to the component's event queue. */
 102  0
     public void runStep() throws Throwable {
 103  0
         ComponentTester.getTester(java.awt.Component.class).
 104   
             sendEvent(createEvent(System.currentTimeMillis()));
 105   
     }
 106   
 
 107   
     /** Validate the attributes are sufficient to construct an event. */
 108  0
     private void parseEvent(Map map) {
 109  0
         type = (String)map.get(TAG_TYPE);
 110  0
         componentID = (String)map.get(TAG_COMPONENT);
 111  0
         kind = (String)map.get(TAG_KIND);
 112  0
         if (type == null)
 113  0
             usage("AWT event type missing");
 114  0
         if (type.endsWith("MouseEvent")) {
 115  0
             String modifiers = (String)map.get(TAG_MODIFIERS);
 116  0
             String x = (String)map.get(TAG_X);
 117  0
             String y = (String)map.get(TAG_Y);
 118  0
             String count = (String)map.get(TAG_COUNT);
 119  0
             String trigger = (String)map.get(TAG_TRIGGER);
 120  0
             if (kind == null)
 121  0
                 usage("MouseEvent must specify a kind");
 122  0
             if (modifiers != null)
 123  0
                 eventAttributes.put(TAG_MODIFIERS, modifiers);
 124  0
             if (x != null) 
 125  0
                 eventAttributes.put(TAG_X, x);
 126  0
             if (y != null) 
 127  0
                 eventAttributes.put(TAG_Y, y);
 128  0
             if (count != null) 
 129  0
                 eventAttributes.put(TAG_COUNT, count);
 130  0
             if (trigger != null)
 131  0
                 eventAttributes.put(TAG_TRIGGER, trigger);
 132  0
             if (type.equals("MenuDragMouseEvent")) {
 133   
                 // FIXME
 134   
             }
 135   
         }
 136  0
         else if (type.equals("KeyEvent")) {
 137  0
             if (kind == null)
 138  0
                 usage("KeyEvent must specify a kind");
 139  0
             String keyCode = (String)map.get(TAG_KEYCODE);
 140  0
             String modifiers = (String)map.get(TAG_MODIFIERS);
 141   
             // Saved characters might be XML-encoded
 142  0
             String keyChar = (String)map.get(TAG_KEYCHAR);
 143  0
             if (keyCode == null) {
 144  0
                 if (!kind.equals("KEY_TYPED"))
 145  0
                     usage("KeyPress/Release require a keyCode");
 146   
             }
 147  0
             else if (!kind.equals("KEY_TYPED"))
 148  0
                 eventAttributes.put(TAG_KEYCODE, keyCode);
 149  0
             if (keyChar == null) {
 150  0
                 if (kind.equals("KEY_TYPED"))
 151  0
                     usage("KeyTyped requires a keyChar");
 152   
             }
 153  0
             else if (kind.equals("KEY_TYPED")) {
 154  0
                 eventAttributes.put(TAG_KEYCHAR, keyChar);
 155   
             }
 156  0
             if (modifiers != null && !"".equals(modifiers)) {
 157  0
                 eventAttributes.put(TAG_MODIFIERS, modifiers);
 158   
             }
 159   
         }
 160   
         // FIXME what others are important? window events?
 161   
         else {
 162  0
             Log.warn("Unimplemented event type '" + type + "', placeholder");
 163   
             //usage("Unimplemented event type '" + type + "'");
 164   
         }
 165   
     }
 166   
     
 167   
     /** Resolve the given name into a component. */
 168  0
     protected java.awt.Component resolve(String name) 
 169   
         throws NoSuchReferenceException,
 170   
                ComponentNotFoundException,
 171   
                MultipleComponentsFoundException {
 172  0
         ComponentReference ref = getResolver().getComponentReference(name);
 173  0
         if (ref != null) {
 174  0
             return ref.getComponent();
 175   
         }
 176  0
         throw new NoSuchReferenceException(name);
 177   
     }
 178   
 
 179   
     /** Create an event based on the parameters we've collected */
 180  0
     private AWTEvent createEvent(long timestamp) 
 181   
         throws ComponentSearchException, NoSuchReferenceException {
 182  0
         Component comp = null;
 183  0
         if (componentID != null) {
 184  0
             comp = resolve(componentID);
 185   
         }
 186  0
         long when = timestamp;
 187  0
         if (type.endsWith("MouseEvent")) {
 188  0
             int x = (comp.getSize().width + 1)/2;
 189  0
             int y = (comp.getSize().height + 1)/2;
 190  0
             int count = 1;
 191  0
             boolean trigger = false;
 192  0
             String modifiers = (String)eventAttributes.get(TAG_MODIFIERS);
 193  0
             int mods = modifiers != null
 194   
                 ? AWT.getModifiers(modifiers) : 0;
 195  0
             try { x = Integer.parseInt((String)eventAttributes.get(TAG_X)); }
 196   
             catch(Exception exc) {}
 197  0
             try { y = Integer.parseInt((String)eventAttributes.get(TAG_Y)); }
 198   
             catch(Exception exc) {}
 199  0
             try { count = Integer.parseInt((String)eventAttributes.get(TAG_COUNT)); }
 200   
             catch(Exception exc) {}
 201  0
             try { trigger = Boolean.getBoolean((String)eventAttributes.get(TAG_TRIGGER)); }
 202   
             catch(Exception exc) {}
 203  0
             int id = ComponentTester.getEventID(MouseEvent.class, kind);
 204  0
             return new MouseEvent(comp, id, when, mods, x, y, count, trigger);
 205   
         }
 206  0
         else if (type.equals("KeyEvent")) {
 207  0
             String modifiers = (String)eventAttributes.get(TAG_MODIFIERS);
 208  0
             int mods = modifiers != null
 209   
                 ? AWT.getModifiers(modifiers) : 0;
 210  0
             int code = AWT.getKeyCode((String)eventAttributes.get(TAG_KEYCODE));
 211  0
             String ch = (String)eventAttributes.get(TAG_KEYCHAR);
 212  0
             char keyChar = ch != null ? ch.charAt(0) : (char)code;
 213  0
             int id = ComponentTester.getEventID(KeyEvent.class, kind);
 214  0
             return new KeyEvent(comp, id, when, mods, code, keyChar);
 215   
         }
 216  0
         throw new IllegalArgumentException("Bad event type " + type);
 217   
     }
 218   
 
 219  150
     public String getType() { return type; }
 220  0
     public void setType(String type) { this.type = type; }
 221  150
     public String getKind() { return kind; }
 222  0
     public void setKind(String kind) { this.kind = kind; }
 223  40
     public String getComponentID() { return componentID; }
 224  0
     public void setComponentID(String id) { componentID = id; }
 225  129
     public String getAttribute(String tag) {
 226  129
         return (String)eventAttributes.get(tag);
 227   
     }
 228   
 
 229  0
     public void setAttribute(String tag, String value) {
 230  0
         eventAttributes.put(tag, value);
 231   
     }
 232   
 }
 233