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   @key headful
  28   @summary REG: File Dialog throws ArrayIndexOutOfBounds Exception on XToolkit with b45
  29   @author Dmitry Cherepanov: area=awt.filedialog
  30   @run main/othervm -Dsun.awt.disableGtkFileDialogs=true ISCthrownByFileListTest
  31 */
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.lang.reflect.*;
  36 
  37 /*
  38   Since the "sun.awt.exception.handler" property will be removed in a future release
  39   this test will be rewritten using new future API. (<<< Done).
  40   It's important that the bug 6304979 is reproducible if the bug 6299853 is reproducible.
  41 */
  42 
  43 public class ISCthrownByFileListTest
  44 {
  45     private static Frame frame = null;
  46     private static FileDialog fd = null;
  47 
  48     // The handler load the class and instantiate this class
  49     // so the 'passed' variable is static
  50     static boolean passed = true;
  51 
  52     public static final void main(String args[]) {
  53         // It's not true that the native file dialog will be focused on Motif & Windows
  54         boolean isXToolkit = Toolkit.getDefaultToolkit().getClass().getName().equals("sun.awt.X11.XToolkit");
  55         if (!isXToolkit){
  56             return;
  57         }
  58 
  59         frame = new Frame("frame");
  60         frame.setLayout (new FlowLayout ());
  61         frame.setBounds(100, 100, 100, 100);
  62         frame.setVisible(true);
  63 
  64         fd = new FileDialog(frame, "file dialog", FileDialog.LOAD);
  65 
  66         // In order to handle all uncaught exceptions in the EDT
  67         final Thread.UncaughtExceptionHandler eh = new Thread.UncaughtExceptionHandler()
  68         {
  69             @Override
  70             public void uncaughtException(Thread t, Throwable e)
  71             {
  72                 e.printStackTrace();
  73                 ISCthrownByFileListTest.passed = false;
  74             }
  75         };
  76 
  77         test();
  78     }// start()
  79 
  80     private static void test (){
  81         Robot r;
  82 
  83         try {
  84             r = new Robot();
  85         } catch(AWTException e) {
  86             throw new RuntimeException(e.getMessage());
  87         }
  88 
  89         r.delay(500);
  90         new Thread(new Runnable() {
  91                 public void run() {
  92                     // The bug 6299853 is reproducible only if the file list is not empty
  93                     // since else the focus will be set to the directory list.
  94                     // But the focus index of the directory list equals 0.
  95                     // So goto the source directory (the file list is non empty)
  96                     fd.setDirectory(System.getProperty("test.src", "."));
  97                     fd.setVisible(true);
  98                 }
  99             }).start();
 100         r.delay(2000);
 101         r.waitForIdle();
 102 
 103         Component focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
 104         if (focusedWindow != fd) {
 105             throw new RuntimeException("Test failed - the file dialog isn't focused window, owner: " + focusedWindow);
 106         }
 107         r.waitForIdle();
 108 
 109         r.keyPress(KeyEvent.VK_SPACE);
 110         r.delay(50);
 111         r.keyRelease(KeyEvent.VK_SPACE);
 112         r.delay(1000);
 113         fd.setVisible(false);
 114         r.delay(1000);
 115         r.waitForIdle();
 116 
 117         if (!ISCthrownByFileListTest.passed){
 118             throw new RuntimeException("Test failed.");
 119         }
 120 
 121     }// test()
 122 }// class ISCthrownByFileListTest