test/jdk/javax/swing/JFileChooser/7199708/bug7199708.java
changeset 49097 ef6d5152a5ae
parent 47216 71c04702a3d5
equal deleted inserted replaced
49096:eaef201ec301 49097:ef6d5152a5ae
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    39 import javax.swing.UIManager;
    39 import javax.swing.UIManager;
    40 
    40 
    41 /**
    41 /**
    42  * @test
    42  * @test
    43  * @key headful
    43  * @key headful
    44  * @bug 7199708 8159587
    44  * @bug 7199708 8159587 8198005
    45  * @author Alexander Scherbatiy
    45  * @author Alexander Scherbatiy
    46  * @summary FileChooser crashs when opening large folder
    46  * @summary FileChooser crashs when opening large folder
    47  * @run main bug7199708
    47  * @run main/timeout=240 bug7199708
    48  */
    48  */
    49 public class bug7199708 {
    49 public class bug7199708 {
    50 
    50 
    51     private static int FILE_NUMBER = 30000;
    51     private static int FILE_NUMBER = 30000;
    52     private static volatile JFileChooser fileChooser;
    52     private static volatile JFileChooser fileChooser;
    53     private static volatile int locationX;
    53     private static volatile int locationX;
    54     private static volatile int locationY;
    54     private static volatile int locationY;
    55     private static volatile int width;
    55     private static volatile int width;
       
    56     private static File largeFolder;
       
    57     private static File files[] = new File[FILE_NUMBER];
    56 
    58 
    57     public static void main(String[] args) throws Exception {
    59     public static void main(String[] args) throws Exception {
    58 
    60 
    59         Robot robot = new Robot();
    61         Robot robot = new Robot();
    60         robot.setAutoDelay(50);
    62         robot.setAutoDelay(50);
    61 
    63 
    62         final File folder = createLargeFolder();
    64         try {
    63         UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    65             final File folder = createLargeFolder();
       
    66             UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    64 
    67 
    65         SwingUtilities.invokeLater(new Runnable() {
    68             SwingUtilities.invokeLater(new Runnable() {
    66             public void run() {
    69                 public void run() {
    67                 fileChooser = new JFileChooser(folder);
    70                     fileChooser = new JFileChooser(folder);
    68                 fileChooser.showSaveDialog(null);
    71                     fileChooser.showSaveDialog(null);
    69             }
    72                 }
    70         });
    73             });
    71 
    74 
    72         robot.waitForIdle();
    75             robot.waitForIdle();
    73 
    76 
    74         SwingUtilities.invokeLater(new Runnable() {
    77             SwingUtilities.invokeLater(new Runnable() {
    75             public void run() {
    78                 public void run() {
    76                 final String detailsTooltip = UIManager.getString("FileChooser."
    79                     final String detailsTooltip =
    77                         + "detailsViewButtonToolTipText", fileChooser.getLocale());
    80                         UIManager.getString("FileChooser."
       
    81                         + "detailsViewButtonToolTipText",
       
    82                           fileChooser.getLocale());
    78 
    83 
    79                 doAction(fileChooser, new ComponentAction() {
    84                     doAction(fileChooser, new ComponentAction() {
    80                     @Override
    85                         @Override
    81                     public boolean accept(Component component) {
    86                         public boolean accept(Component component) {
    82                         return (component instanceof AbstractButton)
    87                             return (component instanceof AbstractButton)
    83                                 && detailsTooltip.equals(
    88                                 && detailsTooltip.equals(
    84                                 ((AbstractButton) component).getToolTipText());
    89                                 ((AbstractButton) component).getToolTipText());
    85                     }
    90                         }
    86 
    91 
    87                     @Override
    92                         @Override
    88                     public void perform(Component component) {
    93                         public void perform(Component component) {
    89                         ((AbstractButton) component).doClick();
    94                             ((AbstractButton) component).doClick();
    90                     }
    95                         }
    91                 });
    96                     });
    92 
    97 
    93                 doAction(fileChooser, new ComponentAction() {
    98                     doAction(fileChooser, new ComponentAction() {
    94                     @Override
    99                         @Override
    95                     public boolean accept(Component component) {
   100                         public boolean accept(Component component) {
    96                         return (component instanceof JTable);
   101                             return (component instanceof JTable);
    97                     }
   102                         }
    98 
   103 
    99                     @Override
   104                         @Override
   100                     public void perform(Component component) {
   105                         public void perform(Component component) {
   101                         Point tableLocation = component.getLocationOnScreen();
   106                             Point tableLocation = component.getLocationOnScreen();
   102                         locationX = (int) tableLocation.getX();
   107                             locationX = (int) tableLocation.getX();
   103                         locationY = (int) tableLocation.getY();
   108                             locationY = (int) tableLocation.getY();
   104                         width = (int) fileChooser.getBounds().getWidth();
   109                             width = (int) fileChooser.getBounds().getWidth();
   105                     }
   110                         }
   106                 });
   111                     });
       
   112                 }
       
   113             });
       
   114 
       
   115             robot.waitForIdle();
       
   116 
       
   117             int d = 25;
       
   118             for (int i = 0; i < width / d; i++) {
       
   119                 robot.mouseMove(locationX + i * d, locationY + 5);
       
   120                 robot.waitForIdle();
       
   121                 robot.mousePress(InputEvent.BUTTON1_MASK);
       
   122                 robot.waitForIdle();
       
   123                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
       
   124                 robot.waitForIdle();
   107             }
   125             }
   108         });
       
   109 
   126 
   110         robot.waitForIdle();
   127             robot.keyPress(KeyEvent.VK_ESCAPE);
       
   128             robot.waitForIdle();
       
   129             robot.keyRelease(KeyEvent.VK_ESCAPE);
       
   130             robot.waitForIdle();
   111 
   131 
   112         int d = 25;
   132         } finally {
   113         for (int i = 0; i < width / d; i++) {
   133             for (int i = 0; i < FILE_NUMBER; i++) {
   114             robot.mouseMove(locationX + i * d, locationY + 5);
   134                 Files.delete(files[i].toPath());
   115             robot.mousePress(InputEvent.BUTTON1_MASK);
   135             }
   116             robot.mouseRelease(InputEvent.BUTTON1_MASK);
   136             Files.delete(largeFolder.toPath());
   117             robot.waitForIdle();
       
   118         }
   137         }
   119 
       
   120         robot.keyPress(KeyEvent.VK_ESCAPE);
       
   121         robot.keyRelease(KeyEvent.VK_ESCAPE);
       
   122     }
   138     }
   123 
   139 
   124     static void doAction(Component component, ComponentAction action) {
   140     static void doAction(Component component, ComponentAction action) {
   125         if (action.accept(component)) {
   141         if (action.accept(component)) {
   126             action.perform(component);
   142             action.perform(component);
   132     }
   148     }
   133 
   149 
   134     private static File createLargeFolder() {
   150     private static File createLargeFolder() {
   135         try {
   151         try {
   136 
   152 
   137             File largeFolder = Files.createTempDirectory("large_folder").toFile();
   153             largeFolder = Files.createTempDirectory("large_folder").toFile();
   138             largeFolder.deleteOnExit();
       
   139 
   154 
   140             for (int i = 0; i < FILE_NUMBER; i++) {
   155             for (int i = 0; i < FILE_NUMBER; i++) {
   141                 File file = new File(largeFolder, "File_" + i + ".txt");
   156                 files[i] = new File(largeFolder, "File_" + i + ".txt");
   142                 file.createNewFile();
   157                 files[i].createNewFile();
   143                 file.deleteOnExit();
       
   144             }
   158             }
   145             return largeFolder;
   159             return largeFolder;
   146         } catch (IOException ex) {
   160         } catch (IOException ex) {
   147             throw new RuntimeException(ex);
   161             throw new RuntimeException(ex);
   148         }
   162         }