test/java/awt/regtesthelpers/Util.java

Print this page




  59 import java.awt.event.ActionEvent;
  60 import java.awt.event.FocusEvent;
  61 import java.awt.event.WindowListener;
  62 import java.awt.event.WindowFocusListener;
  63 import java.awt.event.FocusListener;
  64 import java.awt.event.ActionListener;
  65 
  66 import java.lang.reflect.Constructor;
  67 import java.lang.reflect.Field;
  68 import java.lang.reflect.InvocationTargetException;
  69 import java.lang.reflect.Method;
  70 
  71 import java.security.PrivilegedAction;
  72 import java.security.AccessController;
  73 
  74 import java.util.concurrent.atomic.AtomicBoolean;
  75 
  76 public final class Util {
  77     private Util() {} // this is a helper class with static methods :)
  78 


  79     /*
  80      * @throws RuntimeException when creation failed
  81      */
  82     public static Robot createRobot() {
  83         try {
  84             return new Robot();



  85         } catch (AWTException e) {
  86             throw new RuntimeException("Error: unable to create robot", e);
  87         }
  88     }
  89 
  90 
  91     /**
  92      * Makes the window visible and waits until it's shown.
  93      */
  94     public static void showWindowWait(Window win) {
  95         win.setVisible(true);
  96         waitTillShown(win);
  97     }
  98 
  99     /**
 100      * Moves mouse pointer in the center of given {@code comp} component
 101      * using {@code robot} parameter.
 102      */
 103     public static void pointOnComp(final Component comp, final Robot robot) {
 104         Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());


 183      * @param color expected color
 184      * @param attempts number of attempts to undertake
 185      * @param delay delay before each attempt
 186      * @param robot a robot to use for retrieving pixel color
 187      * @return true if area color matches the color expected, otherwise false
 188      */
 189     public static boolean testBoundsColor(final Rectangle bounds, final Color color, int attempts, int delay, final Robot robot) {
 190         int right = bounds.x + bounds.width - 1;
 191         int bottom = bounds.y + bounds.height - 1;
 192         while (attempts-- > 0) {
 193             if (testPixelColor(bounds.x, bounds.y, color, 1, delay, robot)
 194                 && testPixelColor(right, bounds.y, color, 1, 0, robot)
 195                 && testPixelColor(right, bottom, color, 1, 0, robot)
 196                 && testPixelColor(bounds.x, bottom, color, 1, 0, robot)) {
 197                 return true;
 198             }
 199         }
 200         return false;
 201     }
 202 
 203     public static void waitForIdle(final Robot robot) {



 204         robot.waitForIdle();
 205     }
 206 
 207     public static Field getField(final Class klass, final String fieldName) {
 208         return AccessController.doPrivileged(new PrivilegedAction<Field>() {
 209             public Field run() {
 210                 try {
 211                     Field field = klass.getDeclaredField(fieldName);
 212                     assert (field != null);
 213                     field.setAccessible(true);
 214                     return field;
 215                 } catch (SecurityException se) {
 216                     throw new RuntimeException("Error: unexpected exception caught!", se);
 217                 } catch (NoSuchFieldException nsfe) {
 218                     throw new RuntimeException("Error: unexpected exception caught!", nsfe);
 219                 }
 220             }
 221         });
 222     }
 223 




  59 import java.awt.event.ActionEvent;
  60 import java.awt.event.FocusEvent;
  61 import java.awt.event.WindowListener;
  62 import java.awt.event.WindowFocusListener;
  63 import java.awt.event.FocusListener;
  64 import java.awt.event.ActionListener;
  65 
  66 import java.lang.reflect.Constructor;
  67 import java.lang.reflect.Field;
  68 import java.lang.reflect.InvocationTargetException;
  69 import java.lang.reflect.Method;
  70 
  71 import java.security.PrivilegedAction;
  72 import java.security.AccessController;
  73 
  74 import java.util.concurrent.atomic.AtomicBoolean;
  75 
  76 public final class Util {
  77     private Util() {} // this is a helper class with static methods :)
  78 
  79     private volatile static Robot robot;
  80 
  81     /*
  82      * @throws RuntimeException when creation failed
  83      */
  84     public static Robot createRobot() {
  85         try {
  86             if (robot == null) {
  87                 robot = new Robot();
  88             }
  89             return robot;
  90         } catch (AWTException e) {
  91             throw new RuntimeException("Error: unable to create robot", e);
  92         }
  93     }
  94 
  95 
  96     /**
  97      * Makes the window visible and waits until it's shown.
  98      */
  99     public static void showWindowWait(Window win) {
 100         win.setVisible(true);
 101         waitTillShown(win);
 102     }
 103 
 104     /**
 105      * Moves mouse pointer in the center of given {@code comp} component
 106      * using {@code robot} parameter.
 107      */
 108     public static void pointOnComp(final Component comp, final Robot robot) {
 109         Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());


 188      * @param color expected color
 189      * @param attempts number of attempts to undertake
 190      * @param delay delay before each attempt
 191      * @param robot a robot to use for retrieving pixel color
 192      * @return true if area color matches the color expected, otherwise false
 193      */
 194     public static boolean testBoundsColor(final Rectangle bounds, final Color color, int attempts, int delay, final Robot robot) {
 195         int right = bounds.x + bounds.width - 1;
 196         int bottom = bounds.y + bounds.height - 1;
 197         while (attempts-- > 0) {
 198             if (testPixelColor(bounds.x, bounds.y, color, 1, delay, robot)
 199                 && testPixelColor(right, bounds.y, color, 1, 0, robot)
 200                 && testPixelColor(right, bottom, color, 1, 0, robot)
 201                 && testPixelColor(bounds.x, bottom, color, 1, 0, robot)) {
 202                 return true;
 203             }
 204         }
 205         return false;
 206     }
 207 
 208     public static void waitForIdle(Robot robot) {
 209         if (robot == null) {
 210             robot = createRobot();
 211         }
 212         robot.waitForIdle();
 213     }
 214 
 215     public static Field getField(final Class klass, final String fieldName) {
 216         return AccessController.doPrivileged(new PrivilegedAction<Field>() {
 217             public Field run() {
 218                 try {
 219                     Field field = klass.getDeclaredField(fieldName);
 220                     assert (field != null);
 221                     field.setAccessible(true);
 222                     return field;
 223                 } catch (SecurityException se) {
 224                     throw new RuntimeException("Error: unexpected exception caught!", se);
 225                 } catch (NoSuchFieldException nsfe) {
 226                     throw new RuntimeException("Error: unexpected exception caught!", nsfe);
 227                 }
 228             }
 229         });
 230     }
 231