Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 138   Methods: 10
NCLOC: 97   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
FileDialogTester.java 72.2% 89.6% 100% 86.8%
coverage coverage
 1   
 package abbot.tester;
 2   
 
 3   
 import java.awt.*;
 4   
 import java.awt.event.*;
 5   
 import javax.swing.SwingUtilities;
 6   
 
 7   
 import abbot.*;
 8   
 
 9   
 /**
 10   
  * Tester for the java.awt.FileDialog.
 11   
  *
 12   
  * @author Vrata Venet, European Space Agency, Madrid-Spain (av@iso.vilspa.esa.es) 
 13   
  * @author Tim Wall (twall:users.sf.net)
 14   
  * NOTE: different platforms do different things when the dialog is hidden.
 15   
  * w32 returns null for the file if FileDialog.hide is invoked; other
 16   
  * platforms may leave the file as is.  OSX (as of 1.4.2) won't let you hide
 17   
  * the dialog.
 18   
  */
 19   
 public class FileDialogTester extends DialogTester {
 20   
 
 21   
     /**
 22   
      * This sets the file path for the fd
 23   
      */
 24  9
     public void actionSetFile(Component comp, final String file) {
 25  9
         if (null == file || "".equals(file))
 26  0
             throw new IllegalArgumentException("File name in FileDialog should be non-null and non-empty");
 27  9
         final FileDialog dialog = (FileDialog) comp;
 28  9
         invokeAndWait(new Runnable() {
 29  9
             public void run() {
 30  9
                 dialog.setFile(file);
 31   
             }
 32   
         });
 33   
     }
 34   
   
 35  2
     public void actionSetDirectory(Component comp, final String dir) {
 36  2
         if (null == dir || "".equals(dir))
 37  0
             throw new IllegalArgumentException("File name in FileDialog should be non-null and non-empty");
 38  2
         final FileDialog dialog = (FileDialog) comp;
 39  2
         invokeAndWait(new Runnable() {
 40  2
             public void run() {
 41  2
                 dialog.setDirectory(dir);
 42   
             }
 43   
         });
 44   
     }
 45   
 
 46   
     /** Accept the currently selected file. */
 47  5
     public void actionAccept(Component comp){
 48  5
         Log.debug("accept");
 49  5
         final FileDialog fd = (FileDialog)comp;
 50  5
         final String file = fd.getFile();
 51  5
         if (file == null)
 52  0
             throw new ActionFailedException("No file selected");
 53   
 
 54   
         // HACK:
 55   
         // sun.awt.windows.WFileDialogPeer posts an InvocationEvent which sets
 56   
         // the FileDialog file to null when Dialog.hide is called.
 57   
         // Install an event queue which can catch the posted event and ignore
 58   
         // it.  Only fully tested against sun.awt.windows.WFileDialogPeer.
 59  5
         FileDialogQueue queue = new FileDialogQueue();
 60  5
         try {
 61  5
             fd.getToolkit().getSystemEventQueue().push(queue);
 62   
             
 63   
             // 1.4.2 bug workaround: FileDialog.hide doesn't work
 64  5
             if (Platform.isOSX())
 65  0
                 actionKeyStroke(KeyEvent.VK_ESCAPE);
 66   
             
 67  5
             invokeAndWait(new Runnable() {
 68  5
                 public void run() {
 69  5
                     fd.hide();
 70   
                 }
 71   
             });
 72  5
             waitForIdle();
 73  5
             fd.setFile(file);
 74   
         }
 75   
         finally {
 76  5
             queue.dispose();
 77   
         }
 78   
     }
 79   
   
 80   
     private class FileDialogQueue extends EventQueue {
 81   
         private boolean disposed = false;
 82   
         private boolean installed = false;
 83  10
         public void dispose() {
 84  10
             boolean remove = false;
 85  10
             synchronized(this) {
 86  10
                 if (!disposed) {
 87  5
                     disposed = true;
 88  5
                     remove = true;
 89   
                 }
 90   
             }
 91  10
             if (remove) {
 92  5
                 try { pop(); }
 93   
                 catch(java.util.EmptyStackException es) { }
 94   
             }
 95   
         }
 96  36
         protected void dispatchEvent(AWTEvent e) {
 97  36
             synchronized(this) {
 98  36
                 if (!installed) {
 99  5
                     installed = true;
 100  5
                     String name = Thread.currentThread().getName();
 101  5
                     Thread.currentThread().setName(name + " (abbot FileDialogTester)");
 102   
                 }
 103   
             }
 104   
 
 105   
             // Ignore FileDialogPeer events while disposing the dialog
 106  36
             if (e.paramString().indexOf("FileDialogPeer") != -1) {
 107  5
                 Log.debug("ignoring peer event: " + e);
 108   
                 // Nothing else to handle, restore the original queue
 109  5
                 dispose();
 110   
             }
 111   
             else {
 112  31
                 super.dispatchEvent(e);
 113   
             }
 114   
         }
 115   
     }
 116   
 
 117   
     /** Close the file dialog without selecting a file. */
 118  4
     public void actionCancel(Component comp) {
 119  4
         final FileDialog fd = (FileDialog)comp;
 120  4
         if (Platform.isOSX()) {
 121   
             // bug in OSX 1.4.2: activate causes another dialog to appear!
 122   
             //activate(fd); 
 123   
             // Assume dialog has focus
 124  0
             actionKeyStroke(KeyEvent.VK_ESCAPE);
 125   
         }
 126   
         else {
 127   
             // the w32 native peer sets the file to null on dialog hide, but
 128   
             // other platforms might not, so do it explicitly.
 129  4
             fd.setFile(null);
 130  4
             invokeAndWait(new Runnable() {
 131  4
                 public void run() {
 132  4
                     fd.hide();
 133   
                 }
 134   
             });
 135   
         }
 136   
     }
 137   
 }
 138