1 /*
   2  * Copyright (c) 2013, 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 import java.awt.Component;
  25 import java.awt.Container;
  26 import java.awt.Point;
  27 import java.awt.Robot;
  28 import java.awt.Toolkit;
  29 import java.awt.event.InputEvent;
  30 import java.awt.event.KeyEvent;
  31 import java.io.File;
  32 import java.io.IOException;
  33 import javax.swing.JFileChooser;
  34 import javax.swing.SwingUtilities;
  35 
  36 import java.nio.file.Files;
  37 import javax.swing.AbstractButton;
  38 import javax.swing.JTable;
  39 import javax.swing.UIManager;
  40 import sun.awt.SunToolkit;
  41 
  42 /**
  43  * @test
  44  * @bug 7199708
  45  * @author Alexander Scherbatiy
  46  * @summary FileChooser crashs when opening large folder
  47  * @run main bug7199708
  48  */
  49 public class bug7199708 {
  50 
  51     private static int FILE_NUMBER = 30000;
  52     private static volatile JFileChooser fileChooser;
  53     private static volatile int locationX;
  54     private static volatile int locationY;
  55     private static volatile int width;
  56 
  57     public static void main(String[] args) throws Exception {
  58 
  59         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  60         Robot robot = new Robot();
  61         robot.setAutoDelay(50);
  62 
  63         final File folder = createLargeFolder();
  64         UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  65 
  66         SwingUtilities.invokeLater(new Runnable() {
  67             public void run() {
  68                 fileChooser = new JFileChooser(folder);
  69                 fileChooser.showSaveDialog(null);
  70             }
  71         });
  72 
  73         toolkit.realSync();
  74 
  75         SwingUtilities.invokeLater(new Runnable() {
  76             public void run() {
  77                 final String detailsTooltip = UIManager.getString("FileChooser."
  78                         + "detailsViewButtonToolTipText", fileChooser.getLocale());
  79 
  80                 doAction(fileChooser, new ComponentAction() {
  81                     @Override
  82                     public boolean accept(Component component) {
  83                         return (component instanceof AbstractButton)
  84                                 && detailsTooltip.equals(
  85                                 ((AbstractButton) component).getToolTipText());
  86                     }
  87 
  88                     @Override
  89                     public void perform(Component component) {
  90                         ((AbstractButton) component).doClick();
  91                     }
  92                 });
  93 
  94                 doAction(fileChooser, new ComponentAction() {
  95                     @Override
  96                     public boolean accept(Component component) {
  97                         return (component instanceof JTable);
  98                     }
  99 
 100                     @Override
 101                     public void perform(Component component) {
 102                         Point tableLocation = component.getLocationOnScreen();
 103                         locationX = (int) tableLocation.getX();
 104                         locationY = (int) tableLocation.getY();
 105                         width = (int) fileChooser.getBounds().getWidth();
 106                     }
 107                 });
 108             }
 109         });
 110 
 111         toolkit.realSync();
 112 
 113         int d = 25;
 114         for (int i = 0; i < width / d; i++) {
 115             robot.mouseMove(locationX + i * d, locationY + 5);
 116             robot.mousePress(InputEvent.BUTTON1_MASK);
 117             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 118             toolkit.realSync();
 119         }
 120 
 121         robot.keyPress(KeyEvent.VK_ESCAPE);
 122         robot.keyRelease(KeyEvent.VK_ESCAPE);
 123     }
 124 
 125     static void doAction(Component component, ComponentAction action) {
 126         if (action.accept(component)) {
 127             action.perform(component);
 128         } else if (component instanceof Container) {
 129             for (Component comp : ((Container) component).getComponents()) {
 130                 doAction(comp, action);
 131             }
 132         }
 133     }
 134 
 135     private static File createLargeFolder() {
 136         try {
 137 
 138             File largeFolder = Files.createTempDirectory("large_folder").toFile();
 139             largeFolder.deleteOnExit();
 140 
 141             for (int i = 0; i < FILE_NUMBER; i++) {
 142                 File file = new File(largeFolder, "File_" + i + ".txt");
 143                 file.createNewFile();
 144                 file.deleteOnExit();
 145             }
 146             return largeFolder;
 147         } catch (IOException ex) {
 148             throw new RuntimeException(ex);
 149         }
 150     }
 151 
 152     interface ComponentAction {
 153 
 154         boolean accept(Component component);
 155 
 156         void perform(Component component);
 157     }
 158 }