1 /*
   2   @test
   3   @bug 6304979
   4   @summary REG: File Dialog throws ArrayIndexOutOfBounds Exception on XToolkit with b45
   5   @author Dmitry Cherepanov: area=awt.filedialog
   6   @run main/othervm -Dsun.awt.disableGtkFileDialogs=true ISCthrownByFileListTest
   7 */
   8 
   9 import java.awt.*;
  10 import java.awt.event.*;
  11 import java.lang.reflect.*;
  12 import sun.awt.SunToolkit;
  13 
  14 /*
  15   Since the "sun.awt.exception.handler" property will be removed in a future release
  16   this test will be rewritten using new future API.
  17   It's important that the bug 6304979 is reproducible if the bug 6299853 is reproducible.
  18 */
  19 
  20 public class ISCthrownByFileListTest
  21 {
  22     private static Frame frame = null;
  23     private static FileDialog fd = null;
  24 
  25     // The handler load the class and instantiate this class
  26     // so the 'passed' variable is static
  27     static boolean passed = true;
  28 
  29     public static final void main(String args[]) {
  30         // It's not true that the native file dialog will be focused on Motif & Windows
  31         boolean isXToolkit = Toolkit.getDefaultToolkit().getClass().getName().equals("sun.awt.X11.XToolkit");
  32         if (!isXToolkit){
  33             return;
  34         }
  35 
  36         frame = new Frame("frame");
  37         frame.setLayout (new FlowLayout ());
  38         frame.setBounds(100, 100, 100, 100);
  39         frame.setVisible(true);
  40 
  41         fd = new FileDialog(frame, "file dialog", FileDialog.LOAD);
  42         // In order to handle all uncaught exceptions in the EDT
  43         System.setProperty("sun.awt.exception.handler", "ISCthrownByFileListTest");
  44 
  45         test();
  46     }// start()
  47 
  48     private static void test (){
  49         Robot r;
  50 
  51         try {
  52             r = new Robot();
  53         } catch(AWTException e) {
  54             throw new RuntimeException(e.getMessage());
  55         }
  56 
  57         r.delay(500);
  58         new Thread(new Runnable() {
  59                 public void run() {
  60                     // The bug 6299853 is reproducible only if the file list is not empty
  61                     // since else the focus will be set to the directory list.
  62                     // But the focus index of the directory list equals 0.
  63                     // So goto the source directory (the file list is non empty)
  64                     fd.setDirectory(System.getProperty("test.src", "."));
  65                     fd.setVisible(true);
  66                 }
  67             }).start();
  68         r.delay(2000);
  69         ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
  70 
  71         Component focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
  72         if (focusedWindow != fd) {
  73             throw new RuntimeException("Test failed - the file dialog isn't focused window, owner: " + focusedWindow);
  74         }
  75         System.out.println(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
  76 
  77         r.keyPress(KeyEvent.VK_SPACE);
  78         r.delay(50);
  79         r.keyRelease(KeyEvent.VK_SPACE);
  80         r.delay(1000);
  81         fd.setVisible(false);
  82         r.delay(1000);
  83         ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
  84 
  85         if (!ISCthrownByFileListTest.passed){
  86             throw new RuntimeException("Test failed.");
  87         }
  88 
  89     }// test()
  90 
  91     // handle the uncaught exception
  92     public void handle(Throwable e) {
  93         e.printStackTrace();
  94         ISCthrownByFileListTest.passed = false;
  95     }
  96 
  97 }// class ISCthrownByFileListTest