Path: blob/master/test/jdk/javax/swing/JFileChooser/7199708/bug7199708.java
41155 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Component;24import java.awt.Container;25import java.awt.Point;26import java.awt.Robot;27import java.awt.Toolkit;28import java.awt.event.InputEvent;29import java.awt.event.KeyEvent;30import java.io.File;31import java.io.IOException;32import javax.swing.JFileChooser;33import javax.swing.SwingUtilities;3435import java.nio.file.Files;36import javax.swing.AbstractButton;37import javax.swing.JTable;38import javax.swing.UIManager;3940/**41* @test42* @key headful43* @bug 7199708 8159587 819800544* @author Alexander Scherbatiy45* @summary FileChooser crashs when opening large folder46* @run main/timeout=240 bug719970847*/48public class bug7199708 {4950private static int FILE_NUMBER = 30000;51private static volatile JFileChooser fileChooser;52private static volatile int locationX;53private static volatile int locationY;54private static volatile int width;55private static File largeFolder;56private static File files[] = new File[FILE_NUMBER];5758public static void main(String[] args) throws Exception {5960Robot robot = new Robot();61robot.setAutoDelay(50);6263try {64final File folder = createLargeFolder();65UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");6667SwingUtilities.invokeLater(new Runnable() {68public void run() {69fileChooser = new JFileChooser(folder);70fileChooser.showSaveDialog(null);71}72});7374robot.waitForIdle();7576SwingUtilities.invokeLater(new Runnable() {77public void run() {78final String detailsTooltip =79UIManager.getString("FileChooser."80+ "detailsViewButtonToolTipText",81fileChooser.getLocale());8283doAction(fileChooser, new ComponentAction() {84@Override85public boolean accept(Component component) {86return (component instanceof AbstractButton)87&& detailsTooltip.equals(88((AbstractButton) component).getToolTipText());89}9091@Override92public void perform(Component component) {93((AbstractButton) component).doClick();94}95});9697doAction(fileChooser, new ComponentAction() {98@Override99public boolean accept(Component component) {100return (component instanceof JTable);101}102103@Override104public void perform(Component component) {105Point tableLocation = component.getLocationOnScreen();106locationX = (int) tableLocation.getX();107locationY = (int) tableLocation.getY();108width = (int) fileChooser.getBounds().getWidth();109}110});111}112});113114robot.waitForIdle();115116int d = 25;117for (int i = 0; i < width / d; i++) {118robot.mouseMove(locationX + i * d, locationY + 5);119robot.waitForIdle();120robot.mousePress(InputEvent.BUTTON1_MASK);121robot.waitForIdle();122robot.mouseRelease(InputEvent.BUTTON1_MASK);123robot.waitForIdle();124}125126robot.keyPress(KeyEvent.VK_ESCAPE);127robot.waitForIdle();128robot.keyRelease(KeyEvent.VK_ESCAPE);129robot.waitForIdle();130131} finally {132for (int i = 0; i < FILE_NUMBER; i++) {133Files.delete(files[i].toPath());134}135Files.delete(largeFolder.toPath());136}137}138139static void doAction(Component component, ComponentAction action) {140if (action.accept(component)) {141action.perform(component);142} else if (component instanceof Container) {143for (Component comp : ((Container) component).getComponents()) {144doAction(comp, action);145}146}147}148149private static File createLargeFolder() {150try {151152largeFolder = Files.createTempDirectory("large_folder").toFile();153154for (int i = 0; i < FILE_NUMBER; i++) {155files[i] = new File(largeFolder, "File_" + i + ".txt");156files[i].createNewFile();157}158return largeFolder;159} catch (IOException ex) {160throw new RuntimeException(ex);161}162}163164interface ComponentAction {165166boolean accept(Component component);167168void perform(Component component);169}170}171172173