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