1 /*
   2  * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   @test
  26   @bug 6304979
  27   @summary REG: File Dialog throws ArrayIndexOutOfBounds Exception on XToolkit with b45
  28   @author Dmitry Cherepanov: area=awt.filedialog
  29   @run main/othervm -Dsun.awt.disableGtkFileDialogs=true ISCthrownByFileListTest
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.lang.reflect.*;
  35 
  36 /*
  37   Since the "sun.awt.exception.handler" property will be removed in a future release
  38   this test will be rewritten using new future API. (<<< Done).
  39   It's important that the bug 6304979 is reproducible if the bug 6299853 is reproducible.
  40 */
  41 
  42 public class ISCthrownByFileListTest
  43 {
  44     private static Frame frame = null;
  45     private static FileDialog fd = null;
  46 
  47     // The handler load the class and instantiate this class
  48     // so the 'passed' variable is static
  49     static boolean passed = true;
  50 
  51     public static final void main(String args[]) {
  52         // It's not true that the native file dialog will be focused on Motif & Windows
  53         boolean isXToolkit = Toolkit.getDefaultToolkit().getClass().getName().equals("sun.awt.X11.XToolkit");
  54         if (!isXToolkit){
  55             return;
  56         }
  57 
  58         frame = new Frame("frame");
  59         frame.setLayout (new FlowLayout ());
  60         frame.setBounds(100, 100, 100, 100);
  61         frame.setVisible(true);
  62 
  63         fd = new FileDialog(frame, "file dialog", FileDialog.LOAD);
  64 
  65         // In order to handle all uncaught exceptions in the EDT
  66         final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler()
  67         {
  68             @Override
  69             public void uncaughtException(Thread t, Throwable e)
  70             {
  71                 e.printStackTrace();
  72                 ISCthrownByFileListTest.passed = false;
  73             }
  74         };
  75 
  76         test();
  77     }// start()
  78 
  79     private static void test (){
  80         Robot r;
  81 
  82         try {
  83             r = new Robot();
  84         } catch(AWTException e) {
  85             throw new RuntimeException(e.getMessage());
  86         }
  87 
  88         r.delay(500);
  89         new Thread(new Runnable() {
  90                 public void run() {
  91                     // The bug 6299853 is reproducible only if the file list is not empty
  92                     // since else the focus will be set to the directory list.
  93                     // But the focus index of the directory list equals 0.
  94                     // So goto the source directory (the file list is non empty)
  95                     fd.setDirectory(System.getProperty("test.src", "."));
  96                     fd.setVisible(true);
  97                 }
  98             }).start();
  99         r.delay(2000);
 100         r.waitForIdle();
 101 
 102         Component focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
 103         if (focusedWindow != fd) {
 104             throw new RuntimeException("Test failed - the file dialog isn't focused window, owner: " + focusedWindow);
 105         }
 106         r.waitForIdle();
 107 
 108         r.keyPress(KeyEvent.VK_SPACE);
 109         r.delay(50);
 110         r.keyRelease(KeyEvent.VK_SPACE);
 111         r.delay(1000);
 112         fd.setVisible(false);
 113         r.delay(1000);
 114         r.waitForIdle();
 115 
 116         if (!ISCthrownByFileListTest.passed){
 117             throw new RuntimeException("Test failed.");
 118         }
 119 
 120     }// test()
 121 }// class ISCthrownByFileListTest