VisClient/org/hfbk/vis/VisTests.java

Go to the documentation of this file.
00001 package org.hfbk.vis;
00002 
00003 import java.awt.Frame;
00004 import java.awt.event.InputEvent;
00005 import java.awt.event.KeyEvent;
00006 import java.util.LinkedList;
00007 import java.util.List;
00008 import java.util.Map;
00009 
00010 import org.hfbk.nubsi.Spacemouse;
00011 import org.hfbk.util.Counter;
00012 import org.hfbk.util.Sleeper;
00013 import org.hfbk.util.Test;
00014 import org.hfbk.vid.AVStreamingThread;
00015 import org.hfbk.vis.visnode.VisImagefield;
00016 import org.hfbk.vis.visnode.VisKeyword;
00017 import org.hfbk.vis.visnode.VisLog;
00018 import org.hfbk.vis.visnode.VisMenu;
00019 import org.hfbk.vis.visnode.VisNode;
00020 import org.hfbk.vis.visnode.VisStructure;
00021 import org.lwjgl.opengl.AWTGLCanvas;
00022 import org.lwjgl.util.vector.Vector3f;
00023 
00024 public class VisTests {
00025         
00026         
00027         static final float DISTANCE_TOLERANCE=.1f;
00028         static final float ANGULAR_TOLERANCE= .01f;
00029         
00030         static void waitTransition(VisClient client){
00031                 while (client.transViewpoint==null) Sleeper.sleep(10);
00032                 while (client.transViewpoint!=null) Sleeper.sleep(10);          
00033         }
00034 
00035         static float fps(VisClient c) {
00036                 int n=10;
00037                 float dt=0;
00038                 for (int i=0; i<n; i++){
00039                         dt+=c.dt;
00040                         Sleeper.sleep(100);
00041                 }               
00042                 return n/dt;
00043         }
00044         
00045         static boolean vpEquals(Viewpoint v1, Viewpoint v2){
00046                 return (Math.abs(v1.angle-v2.angle)        <ANGULAR_TOLERANCE && 
00047                                 Math.abs(v1.elevation-v2.elevation)<ANGULAR_TOLERANCE &&
00048                                 Vector3f.sub(v1, v2, null).length()<DISTANCE_TOLERANCE); 
00049         } 
00050         
00051         static void printProperty(String name){
00052                 System.out.println("\t"+name+":\t"+System.getProperty(name));
00053         }
00054         
00055         static void sysInfo(){
00056                 System.out.println("System information:");
00057                 Runtime rt=Runtime.getRuntime();
00058                 System.out.println("\t#CPUs usable:\t"+rt.availableProcessors());
00059                 System.out.println("\tmem usable:\t"+rt.maxMemory());
00060                 printProperty("os.name");        
00061                 printProperty("os.arch"); 
00062                 printProperty("os.version");            
00063                 System.out.println();
00064         }
00065         
00066         
00067         static void profile(){
00068                 Map<Thread,StackTraceElement[]> stacks=Thread.getAllStackTraces();
00069                 
00070                 for(Map.Entry<Thread,StackTraceElement[]> entry: stacks.entrySet()){
00071                         Thread t=entry.getKey();
00072                         System.out.println(t.getClass().getName()+"("+t.getName()+"):");
00073                         for (StackTraceElement ste: entry.getValue())
00074                                 System.out.println("\t"+ste);
00075                 }
00076         }
00077         
00078         static void countNodes(VisNode root){
00079                 List<VisNode> stack=new LinkedList<VisNode>();
00080                 Counter<String> counter=new Counter<String>();
00081                 stack.add(root);
00082                 int count=0;
00083                 while(stack.size()>0){
00084                         VisNode n=stack.get(0);
00085                         count++;
00086                         counter.add(n.getClass().getSimpleName());
00087                         stack.remove(0);
00088                         stack.addAll(n.children);
00089                 }
00090                 System.out.print("We now have "+count+" visnodes: ");
00091                 
00092                 String text="";
00093                 for(Map.Entry<String,Integer> entry : counter.entrySet()){
00094                         text+=entry.getValue()+" "+entry.getKey()+", ";                                         
00095                 }
00096                 System.out.println(text);
00097         }
00098         
00099         public static void main(String[] args) throws Exception{
00100                 
00101                 sysInfo();
00102                 Prefs.current.verbose=false;
00103                 Prefs.current.debug=false;
00104                 Prefs.current.parse(args);
00105                 
00106                 new Test("prerequisites"){protected void test() {
00107                         new Test("vm version"){protected void test() {
00108                                 String version=System.getProperty("java.version");
00109                                 System.out.print(version);
00110                                 if (version.compareTo("1.5")==-1) {
00111                                         System.out.print(" need min. 1.5!  FAIL.");
00112                                         System.exit(1);
00113                                 }else if (version.compareTo("1.6")==-1)
00114                                         System.out.print(" need 1.6 for some feats. ");
00115                         }};
00116                         new Test("vm version"){protected void test() {
00117                                 Prefs.current.version=Prefs.getVersion();
00118                                 System.out.println(Prefs.current.version);
00119                         }};
00120                         new Test("native librarys"){@SuppressWarnings("deprecation")
00121                         protected void test() {
00122                                 new Test("open gl"){protected void test() throws Throwable {
00123                                         AWTGLCanvas canvas=new AWTGLCanvas();
00124                                         Frame f=new Frame();
00125                                         f.add(canvas);
00126                                         f.setVisible(true);
00127                                         f.dispose();
00128                                         canvas=null;
00129                                 }};
00130                                 new Test("libav"){protected void test() {
00131                                         new AVStreamingThread("icons/vis.png", 0x100000);
00132                                 }};
00133                                 new Test("spacemouse"){protected void test() {
00134                                         Spacemouse sm=Spacemouse.getInstance();
00135                                         sm.stop();
00136                                 }};
00137                         }};
00138                         
00139                         new Test("UDP"){protected void test() throws Exception {
00140                                 UdpListener udpl=new UdpListener(null);
00141                                 Thread.sleep(1000);
00142                                 udpl.close();
00143                         }};
00144                         
00145                         new Test("servers"){protected void test() {
00146                                 Prefs.current.baseURL = new ServerManager().getServer();
00147                                 System.out.print("found: " + Prefs.current.baseURL);
00148                         }};     
00149                 }};
00150                 
00151                 new Test("vis/client"){
00152                         VisClientWindow window;
00153                         VisClientPanel panel;
00154                         VisClient client;
00155                         protected void test() {
00156                         new Test("startup"){protected void test() throws Throwable {
00157                                 window=new VisClientWindow(); //returns if root and hud structure is complete!
00158                                 panel=window.clientPanel;
00159                                 int w=panel.getWidth(), h=panel.getHeight();
00160                                 System.out.print(" got panel "+w+"x"+h+"px.");
00161                                 client=panel.client;
00162                                 Thread.sleep(1000);
00163                                 new Test("basic structure"){protected void test() {
00164                                         if (client.root==null) {
00165                                                 System.out.println(" no root! FAILED.");
00166                                                 System.exit(0);
00167                                         }else if (client.root.findNode(VisStructure.class)==null){
00168                                                 System.out.println(" no structure! FAILED.");
00169                                                 System.exit(0);
00170                                         }
00171         
00172                                 }};
00173                                 
00174                                 new Test("log"){protected void test() throws InterruptedException {
00175                                         VisNode log=client.root.findNode(VisStructure.class).findNode(VisLog.class);
00176                                         if (log==null){
00177                                                 Prefs.current.log=false;
00178                                                 fail();
00179                                         }else{
00180                                                 while (log.children.size()==0) Thread.sleep(100); 
00181                                         }
00182                                         Prefs.current.log=false;
00183                                 }};
00184                         }};
00185                         
00186                         new Test("navigation"){
00187                                 
00188                                 abstract class NaviTest extends Test{
00189                                         float distance;
00190                                         MouseViewpoint vp; 
00191                                         Viewpoint vpBefore;
00192                                         
00193                                         public NaviTest(String title) { super(title); }
00194                                         abstract void move();
00195                                         boolean check(){
00196                                                 return distance>DISTANCE_TOLERANCE;
00197                                         }
00198                                         abstract void moveBack();
00199                                         protected void test(){
00200                                                 vp=client.mouseViewpoint; 
00201                                                 vpBefore=new Viewpoint();
00202                                                 vpBefore.set(vp);
00203                                         
00204                                                 move();
00205                                         
00206                                                 distance=Vector3f.sub(vpBefore, vp, null).length();
00207                                 
00208                                                 if (!check())
00209                                                         fail(" no movement! ");
00210                                                 else {
00211                                                         moveBack();
00212                                                         
00213                                                         //check if we reach startpoint again. 
00214                                                         if (!vpEquals(vp, vpBefore) )
00215                                                                 fail(" akward movement! ");
00216                                                 }
00217                                                 vp.set(vpBefore);
00218                                         }
00219                                 }
00220                                 
00221                                 protected void test() throws Exception {
00222                                         
00223                                 final int w=panel.getWidth(); //h=panel.getHeight();
00224                                 
00225                                 while (client.transViewpoint!=null) Thread.sleep(100);
00226                                 
00227                                 new NaviTest("Move forward"){
00228                                         void move() { drag(panel, InputEvent.BUTTON1_MASK, w-10,50, w-10, 10);}
00229                                         void moveBack() {       vp.forward(-distance);}
00230                                 };
00231                                 
00232                                 new NaviTest("Move forward by keys"){
00233                                         void move() { press(panel, KeyEvent.VK_UP);}
00234                                         void moveBack() {       vp.forward(-distance); }
00235                                 };
00236                                 
00237                                 new NaviTest("Move sideaways"){
00238                                         void move() { drag(panel, InputEvent.BUTTON2_MASK, w-10,10, w-60, 10);}
00239                                         void moveBack() { vp.strafe(distance); }
00240                                 };
00241 
00242                                 new NaviTest("Move upward"){
00243                                         void move() {drag(panel, InputEvent.BUTTON2_MASK, w-10,60, w-10, 10); }
00244                                         void moveBack() {vp.y-=distance; }
00245                                 };
00246                                 
00247                                 new NaviTest("Move upward by keys"){
00248                                         void move() { press(panel, KeyEvent.VK_PAGE_UP); }
00249                                         void moveBack() {vp.y-=distance; }
00250                                 };
00251                                 
00252                                 new NaviTest("Spin"){
00253                                         void move() {drag(panel, InputEvent.BUTTON1_MASK, w-10,10, w-60, 10); }
00254                                         boolean check() {
00255                                                 return vp.angle-vpBefore.angle > ANGULAR_TOLERANCE;
00256                                         }
00257                                         void moveBack() {vp.angle=vpBefore.angle; }
00258                                 };
00259         
00260                                 new NaviTest("Spin by keys"){
00261                                         void move() {press(panel, KeyEvent.VK_LEFT);}
00262                                         boolean check() {
00263                                                 return vp.angle-vpBefore.angle > ANGULAR_TOLERANCE;
00264                                         }
00265                                         void moveBack() {vp.angle=vpBefore.angle; }
00266                                 };
00267                                 
00268                                 new NaviTest("Pitch"){
00269                                         void move() {drag(panel, InputEvent.BUTTON3_MASK, w-10,60, w-10, 10); }
00270                                         boolean check() {
00271                                                 return vp.elevation-vpBefore.elevation > ANGULAR_TOLERANCE; 
00272                                         }
00273                                         void moveBack() {vp.elevation=vpBefore.elevation;}
00274                                 };
00275                         }};     
00276         
00277 
00278                         new Test("fetching"){protected void test() {
00279                                 new Test("'test' @ google"){protected void test() {
00280                                         client.reset();
00281                                         client.root.fetch("google", "test", null);
00282                                         waitTransition(client);
00283                                         if (!((VisKeyword)client.root.lastVisNode).keyword.equals("test"))
00284                                                 fail("returns bad keyword");
00285                                         client.root.lastVisNode=null;           
00286                                 }};
00287                                 new Test("'test' @ google images"){protected void test() {
00288                                         client.root.fetch("googleImages", "test", null);
00289                                         waitTransition(client);
00290                                         VisNode imf=client.root.lastVisNode;
00291                                         if (imf.getClass()!=VisImagefield.class) 
00292                                                 fail("no imagefield!");
00293                                         else if(imf.children.size()==0)
00294                                                 fail("no images");
00295                                         else    
00296                                                 System.out.print(imf.children.size()+"images, "+fps(client)+"fps.");
00297                                 }};
00298                         
00299                                 new Test("Fetching a little... "){protected void test() {
00300                                         for (int i=0; i<5; i++){                        
00301                                                 client.root.fetch("google", ""+i, null);
00302                                                 //wait til a viewpoint transition marks the arrival of fetched nodes
00303                                                 waitTransition(client);
00304                                         }
00305                                 }};
00306                                 new Test("Fetching concurrently"){protected void test() {
00307                                         int threadsBefore=Thread.activeCount();
00308                                         for (int i=5; i<10; i++)
00309                                                 client.root.fetch("google", ""+i, null);
00310                                         while (Thread.activeCount()>threadsBefore) Sleeper.sleep(100); //wait fetchers complete.
00311                                         while (client.transViewpoint!=null) Sleeper.sleep(10); //wait til transition ends
00312                                         client.mouseViewpoint.fov=3f;
00313                                         client.mouseViewpoint.y=client.root.epicenter.y/2;
00314                                         System.out.println(" still "+fps(client)+"fps. OK.");
00315                                         client.mouseViewpoint.fov=Viewpoint.DEFAULT_FOV;
00316                                 }};
00317                         }};
00318 
00319                         new Test("manipulation"){protected void test() {
00320                                 final int w=panel.getWidth(), h=panel.getHeight();
00321                                 new Test("navigate to keyword"){protected void test() {
00322                                         type(panel,KeyEvent.VK_SHIFT);
00323                                         waitTransition(client);
00324                                 }};
00325                                 new Test("erasing childs"){protected void test() {
00326                                         client.root.lastVisNode.children.clear();
00327                                 }};
00328                                 new Test("Opening Keyword Menu"){protected void test() {
00329                                         click(panel, InputEvent.BUTTON1_MASK, w/2, h/2);
00330                                         Sleeper.sleep(500);
00331                                         VisNode last=client.root.lastVisNode;
00332                                         if (last.children.size()==0 || !(last.children.get(0) instanceof VisMenu)) 
00333                                                 fail();
00334                                 }};
00335                                 new Test("Fetching by Keyword Menu"){protected void test() {
00336                                         click(panel, InputEvent.BUTTON1_MASK, w/2, h/2-h/5);            
00337                                         waitTransition(client);
00338                                 }};
00339                         }};
00340                         new Test("UI"){protected void test() {
00341                                 new Test("Entering some keyword"){protected void test() {
00342                                         click(panel,InputEvent.BUTTON1_MASK,1, 1);
00343                                         int[] keys=new int[]{KeyEvent.VK_T, KeyEvent.VK_E, KeyEvent.VK_S, KeyEvent.VK_T, KeyEvent.VK_ENTER};
00344                                         for (int key:keys) type(panel, key);
00345                                 }};
00346                         }};
00347                         /*new Test("VisNode types"){protected void test() {
00348                                 client.root.create("profiler", "");             
00349                                 client.root.create("ducts", "");                
00350                         }};*/
00351                         countNodes(client.root);
00352                 }};
00353                 System.out.println("FINISHED.");
00354                 System.exit(0);
00355         }
00356 }

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