VisClient/org/hfbk/util/Test.java

Go to the documentation of this file.
00001 
00004 package org.hfbk.util;
00005 
00006 import java.awt.Component;
00007 import java.awt.Point;
00008 import java.awt.Robot;
00009 
00010 public abstract class Test{
00011 
00012         static final int MOUSE_DELAY=500; //ms
00013         static final int KEYPRESS_DELAY=500; //ms
00014         static final int KEYTYPE_DELAY=200; //ms
00015 
00016         static String path=""; 
00017         static int number;
00018         
00019         String title;
00020         public Test(String title) {
00021                 this.title=title;
00022                 
00023                 path+="."+number++;
00024                 System.out.print("\n"+path.substring(1)+" Testing "+title+" ");
00025                 int tmp=number;
00026                 number=0;
00027                 try {
00028                         test();
00029                 } catch (Throwable e) {
00030                         throw new RuntimeException(e);
00031                 }
00032                 number=tmp;
00033                 System.out.print(" OK.");
00034                 int point=path.lastIndexOf('.');
00035                 if(point>=0){
00036                         path=path.substring(0, point);
00037                 }
00038         }
00039         protected void fail(){
00040                 fail("");
00041         }
00042         
00043         protected void fail(String reason){
00044                 System.out.println(" "+reason+" FAILED.");
00045                 System.exit(1);
00046         }
00047         
00048         protected abstract void test() throws Throwable;
00049 
00050         protected static void click(Component c, int buttons, int x1, int y1) {
00051                 try{
00052                         Robot r=new Robot();
00053                         Point pos=c.getLocationOnScreen();
00054                         r.mouseMove(pos.x+x1, pos.y+y1);
00055                         Thread.sleep(MOUSE_DELAY);
00056                         r.mousePress(buttons);
00057                         Thread.sleep(MOUSE_DELAY);
00058                         r.mouseRelease(buttons);
00059                 }catch(Exception e){
00060                         throw(new RuntimeException(e));
00061                 }
00062         }
00063         
00064         protected static void drag(Component c, int buttons, int x1, int y1, int x2, int y2) {
00065                 try{
00066                         Robot r=new Robot();
00067                         Point pos=c.getLocationOnScreen();
00068                         r.mouseMove(pos.x+x1, pos.y+y1);
00069                         Thread.sleep(MOUSE_DELAY);
00070                         r.mousePress(buttons);
00071                         Thread.sleep(MOUSE_DELAY);
00072                         r.mouseMove(pos.x+x2, pos.y+y2);
00073                         Thread.sleep(MOUSE_DELAY);
00074                         r.mouseRelease(buttons);
00075                         Thread.sleep(MOUSE_DELAY);              
00076                 }catch(Exception e){
00077                         throw(new RuntimeException(e));
00078                 }
00079         }
00080         
00081         protected static void press(Component c, int keycode) {
00082                 key(c, keycode, KEYPRESS_DELAY);
00083         }
00084         
00085         protected static void type(Component c, int keycode) {
00086                 key(c, keycode, KEYTYPE_DELAY);
00087         }
00088                         
00089         static void key(Component c, int keycode, int delay) {
00090                 try{
00091                         c.requestFocus();
00092                         Robot r=new Robot();
00093                         r.keyPress(keycode);
00094                         Thread.sleep(delay);
00095                         r.keyRelease(keycode);
00096                         Thread.sleep(delay);
00097                 }catch (Exception e){
00098                         throw(new RuntimeException(e));
00099                 }       
00100         }
00101 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1