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